1 /*
2  * Copyright (C) 2013  Daniel Vrátil <dvratil@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19 
20 #include "log-manager.h"
21 #include "log-manager-private.h"
22 #include "abstract-logger-plugin.h"
23 #include "log-entity.h"
24 
25 #include "pending-logger-dates-impl.h"
26 #include "pending-logger-logs-impl.h"
27 #include "pending-logger-entities-impl.h"
28 #include "pending-logger-search-impl.h"
29 
30 #include <KService>
31 #include <KServiceTypeTrader>
32 #include <KPluginInfo>
33 
34 #include "debug.h"
35 
36 using namespace KTp;
37 
38 #define KTP_LOGGER_PLUGIN_VERSION "1"
39 
40 LogManager* LogManager::Private::s_logManagerInstance = nullptr;
41 
loadPlugins()42 void LogManager::Private::loadPlugins()
43 {
44     const KService::List services = KServiceTypeTrader::self()->query(
45                                          QLatin1String("KTpLogger/Plugin"),
46                                          QLatin1String("[X-KTp-PluginInfo-Version] == " KTP_LOGGER_PLUGIN_VERSION));
47 
48     const KPluginInfo::List pluginInfos = KPluginInfo::fromServices(services);
49     Q_FOREACH (const KPluginInfo &pluginInfo, pluginInfos) {
50         const KService::Ptr service = pluginInfo.service();
51         KPluginFactory *factory = KPluginLoader(service->library()).factory();
52         if (factory) {
53             qCDebug(KTP_LOGGER) << "loaded factory :" << factory;
54             AbstractLoggerPlugin *plugin = factory->create<AbstractLoggerPlugin>(q);
55 
56             if (plugin) {
57                 qCDebug(KTP_LOGGER) << "loaded logger plugin : " << plugin;
58                 plugins << plugin;
59             }
60         } else {
61             qCWarning(KTP_LOGGER) << "error loading plugin :" << service->library();
62         }
63     }
64 }
65 
Private(LogManager * parent)66 LogManager::Private::Private(LogManager *parent):
67     q(parent)
68 {
69     loadPlugins();
70 }
71 
instance()72 LogManager* LogManager::instance()
73 {
74     if (Private::s_logManagerInstance == nullptr) {
75         Private::s_logManagerInstance = new LogManager();
76     }
77 
78     return Private::s_logManagerInstance;
79 }
80 
LogManager()81 LogManager::LogManager():
82     AbstractLoggerPlugin(),
83     d(new Private(this))
84 {
85 }
86 
~LogManager()87 LogManager::~LogManager()
88 {
89     delete d;
90 }
91 
accountManager() const92 Tp::AccountManagerPtr LogManager::accountManager() const
93 {
94     if (d->plugins.isEmpty()) {
95         return Tp::AccountManagerPtr();
96     }
97 
98     return d->plugins.first()->accountManager();
99 }
100 
setAccountManager(const Tp::AccountManagerPtr & accountManager)101 void LogManager::setAccountManager(const Tp::AccountManagerPtr &accountManager)
102 {
103     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
104         plugin->setAccountManager(accountManager);
105     }
106 }
107 
108 
queryDates(const Tp::AccountPtr & account,const KTp::LogEntity & entity)109 PendingLoggerDates* LogManager::queryDates(const Tp::AccountPtr &account,
110                                            const KTp::LogEntity &entity)
111 {
112     return new PendingLoggerDatesImpl(account, entity, this);
113 }
114 
queryLogs(const Tp::AccountPtr & account,const KTp::LogEntity & entity,const QDate & date)115 PendingLoggerLogs* LogManager::queryLogs(const Tp::AccountPtr &account,
116                                          const KTp::LogEntity &entity,
117                                          const QDate &date)
118 {
119     return new PendingLoggerLogsImpl(account, entity, date, this);
120 }
121 
queryEntities(const Tp::AccountPtr & account)122 PendingLoggerEntities* LogManager::queryEntities(const Tp::AccountPtr& account)
123 {
124     return new PendingLoggerEntitiesImpl(account, this);
125 }
126 
clearAccountLogs(const Tp::AccountPtr & account)127 void LogManager::clearAccountLogs(const Tp::AccountPtr &account)
128 {
129    Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
130         if (!plugin->handlesAccount(account)) {
131             continue;
132         }
133 
134         plugin->clearAccountLogs(account);
135     }
136 }
137 
clearContactLogs(const Tp::AccountPtr & account,const KTp::LogEntity & entity)138 void LogManager::clearContactLogs(const Tp::AccountPtr &account,
139                                   const KTp::LogEntity &entity)
140 {
141     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
142         if (!plugin->handlesAccount(account)) {
143             continue;
144         }
145 
146         plugin->clearContactLogs(account, entity);
147     }
148 }
149 
search(const QString & term)150 PendingLoggerSearch* LogManager::search(const QString& term)
151 {
152     return new PendingLoggerSearchImpl(term, this);
153 }
154 
logsExist(const Tp::AccountPtr & account,const KTp::LogEntity & contact)155 bool LogManager::logsExist(const Tp::AccountPtr &account, const KTp::LogEntity &contact)
156 {
157     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, d->plugins) {
158         if (!plugin->handlesAccount(account)) {
159             continue;
160         }
161 
162         if (plugin->logsExist(account, contact)) {
163             return true;
164         }
165     }
166 
167     return false;
168 }
169 
170 
171 using namespace KTp;
172