File indexing completed on 2023-12-03 08:28:33

0001 /*
0002  * Copyright (C) 2013  Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0017  *
0018  */
0019 
0020 #include "log-manager.h"
0021 #include "log-manager-private.h"
0022 #include "abstract-logger-plugin.h"
0023 #include "log-entity.h"
0024 
0025 #include "pending-logger-dates-impl.h"
0026 #include "pending-logger-logs-impl.h"
0027 #include "pending-logger-entities-impl.h"
0028 #include "pending-logger-search-impl.h"
0029 
0030 #include <KService>
0031 #include <KServiceTypeTrader>
0032 #include <KPluginInfo>
0033 
0034 #include "debug.h"
0035 
0036 using namespace KTp;
0037 
0038 #define KTP_LOGGER_PLUGIN_VERSION "1"
0039 
0040 LogManager* LogManager::Private::s_logManagerInstance = nullptr;
0041 
0042 void LogManager::Private::loadPlugins()
0043 {
0044     const KService::List services = KServiceTypeTrader::self()->query(
0045                                          QLatin1String("KTpLogger/Plugin"),
0046                                          QLatin1String("[X-KTp-PluginInfo-Version] == " KTP_LOGGER_PLUGIN_VERSION));
0047 
0048     const KPluginInfo::List pluginInfos = KPluginInfo::fromServices(services);
0049     Q_FOREACH (const KPluginInfo &pluginInfo, pluginInfos) {
0050         const KService::Ptr service = pluginInfo.service();
0051         KPluginFactory *factory = KPluginLoader(service->library()).factory();
0052         if (factory) {
0053             qCDebug(KTP_LOGGER) << "loaded factory :" << factory;
0054             AbstractLoggerPlugin *plugin = factory->create<AbstractLoggerPlugin>(q);
0055 
0056             if (plugin) {
0057                 qCDebug(KTP_LOGGER) << "loaded logger plugin : " << plugin;
0058                 plugins << plugin;
0059             }
0060         } else {
0061             qCWarning(KTP_LOGGER) << "error loading plugin :" << service->library();
0062         }
0063     }
0064 }
0065 
0066 LogManager::Private::Private(LogManager *parent):
0067     q(parent)
0068 {
0069     loadPlugins();
0070 }
0071 
0072 LogManager* LogManager::instance()
0073 {
0074     if (Private::s_logManagerInstance == nullptr) {
0075         Private::s_logManagerInstance = new LogManager();
0076     }
0077 
0078     return Private::s_logManagerInstance;
0079 }
0080 
0081 LogManager::LogManager():
0082     AbstractLoggerPlugin(),
0083     d(new Private(this))
0084 {
0085 }
0086 
0087 LogManager::~LogManager()
0088 {
0089     delete d;
0090 }
0091 
0092 Tp::AccountManagerPtr LogManager::accountManager() const
0093 {
0094     if (d->plugins.isEmpty()) {
0095         return Tp::AccountManagerPtr();
0096     }
0097 
0098     return d->plugins.first()->accountManager();
0099 }
0100 
0101 void LogManager::setAccountManager(const Tp::AccountManagerPtr &accountManager)
0102 {
0103     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
0104         plugin->setAccountManager(accountManager);
0105     }
0106 }
0107 
0108 
0109 PendingLoggerDates* LogManager::queryDates(const Tp::AccountPtr &account,
0110                                            const KTp::LogEntity &entity)
0111 {
0112     return new PendingLoggerDatesImpl(account, entity, this);
0113 }
0114 
0115 PendingLoggerLogs* LogManager::queryLogs(const Tp::AccountPtr &account,
0116                                          const KTp::LogEntity &entity,
0117                                          const QDate &date)
0118 {
0119     return new PendingLoggerLogsImpl(account, entity, date, this);
0120 }
0121 
0122 PendingLoggerEntities* LogManager::queryEntities(const Tp::AccountPtr& account)
0123 {
0124     return new PendingLoggerEntitiesImpl(account, this);
0125 }
0126 
0127 void LogManager::clearAccountLogs(const Tp::AccountPtr &account)
0128 {
0129    Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
0130         if (!plugin->handlesAccount(account)) {
0131             continue;
0132         }
0133 
0134         plugin->clearAccountLogs(account);
0135     }
0136 }
0137 
0138 void LogManager::clearContactLogs(const Tp::AccountPtr &account,
0139                                   const KTp::LogEntity &entity)
0140 {
0141     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
0142         if (!plugin->handlesAccount(account)) {
0143             continue;
0144         }
0145 
0146         plugin->clearContactLogs(account, entity);
0147     }
0148 }
0149 
0150 PendingLoggerSearch* LogManager::search(const QString& term)
0151 {
0152     return new PendingLoggerSearchImpl(term, this);
0153 }
0154 
0155 bool LogManager::logsExist(const Tp::AccountPtr &account, const KTp::LogEntity &contact)
0156 {
0157     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
0158         if (!plugin->handlesAccount(account)) {
0159             continue;
0160         }
0161 
0162         if (plugin->logsExist(account, contact)) {
0163             return true;
0164         }
0165     }
0166 
0167     return false;
0168 }
0169 
0170 
0171 using namespace KTp;