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 "pending-logger-logs-impl.h"
21 #include "abstract-logger-plugin.h"
22 #include "debug.h"
23 
PendingLoggerLogsImpl(const Tp::AccountPtr & account,const KTp::LogEntity & entity,const QDate & date,QObject * parent)24 PendingLoggerLogsImpl::PendingLoggerLogsImpl(const Tp::AccountPtr &account,
25                                              const KTp::LogEntity &entity,
26                                              const QDate &date,
27                                              QObject* parent):
28     PendingLoggerLogs(account, entity, date, parent)
29 {
30     if (plugins().isEmpty()) {
31         emitFinished();
32         return;
33     }
34 
35     Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, plugins()) {
36         if (!plugin->handlesAccount(account)) {
37             continue;
38         }
39 
40         PendingLoggerOperation *op = plugin->queryLogs(account, entity, date);
41         if (!op) {
42             continue;
43         }
44 
45         connect(op, SIGNAL(finished(KTp::PendingLoggerOperation*)),
46                 this, SLOT(operationFinished(KTp::PendingLoggerOperation*)));
47         mRunningOps << op;
48     }
49 }
50 
51 
~PendingLoggerLogsImpl()52 PendingLoggerLogsImpl::~PendingLoggerLogsImpl()
53 {
54 }
55 
operationFinished(KTp::PendingLoggerOperation * op)56 void PendingLoggerLogsImpl::operationFinished(KTp::PendingLoggerOperation *op)
57 {
58     Q_ASSERT(mRunningOps.contains(op));
59     mRunningOps.removeAll(op);
60 
61     KTp::PendingLoggerLogs *operation = qobject_cast<KTp::PendingLoggerLogs*>(op);
62     Q_ASSERT(operation);
63 
64     const QList<KTp::LogMessage> newLogs = operation->logs();
65     qCDebug(KTP_LOGGER) << "Plugin" << op->parent() << "returned" << newLogs.count() << "logs";
66 
67     // FIXME: Maybe handle duplicates?
68     appendLogs(newLogs);
69 
70     if (mRunningOps.isEmpty()) {
71         emitFinished();
72     }
73 }
74