pending-logger-dates-impl.cpp 2.52 KB
Newer Older
Daniel Vrátil's avatar
Daniel Vrátil committed
1
2
3
/*
 * Copyright (C) 2013  Daniel Vrátil <dvratil@redhat.com>
 *
4
5
6
7
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
Daniel Vrátil's avatar
Daniel Vrátil committed
8
 *
9
 * This library is distributed in the hope that it will be useful,
Daniel Vrátil's avatar
Daniel Vrátil committed
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Daniel Vrátil's avatar
Daniel Vrátil committed
13
 *
14
15
16
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Daniel Vrátil's avatar
Daniel Vrátil committed
17
18
19
20
21
 *
 */

#include "pending-logger-dates-impl.h"
#include "abstract-logger-plugin.h"
22
#include "debug.h"
Daniel Vrátil's avatar
Daniel Vrátil committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

PendingLoggerDatesImpl::PendingLoggerDatesImpl(const Tp::AccountPtr &account,
                                               const KTp::LogEntity &entity,
                                               QObject* parent):
    PendingLoggerDates(account, entity, parent)
{
    if (plugins().isEmpty()) {
        emitFinished();
        return;
    }

    Q_FOREACH (KTp::AbstractLoggerPlugin *plugin, plugins()) {
        if (!plugin->handlesAccount(account)) {
            continue;
        }

        PendingLoggerOperation *op = plugin->queryDates(account, entity);
        if (!op) {
            continue;
        }

        connect(op, SIGNAL(finished(KTp::PendingLoggerOperation*)),
                this, SLOT(operationFinished(KTp::PendingLoggerOperation*)));
        mRunningOps << op;
    }
}

PendingLoggerDatesImpl::~PendingLoggerDatesImpl()
{
}

void PendingLoggerDatesImpl::operationFinished(KTp::PendingLoggerOperation *op)
{
    Q_ASSERT(mRunningOps.contains(op));
    mRunningOps.removeAll(op);

    KTp::PendingLoggerDates *operation = qobject_cast<KTp::PendingLoggerDates*>(op);
    Q_ASSERT(operation);

    const QList<QDate> newDates = operation->dates();
    QList<QDate> existingDates = dates();
64
    qCDebug(KTP_LOGGER) << "Plugin" << op->parent() << "returned" << newDates.count() << "dates";
Daniel Vrátil's avatar
Daniel Vrátil committed
65
66
67
68
69
70
71
72
73
74
    Q_FOREACH (const QDate &date, newDates) {
        if (!existingDates.contains(date)) {
            existingDates << date;
        }
    }

    setDates(existingDates);

    if (mRunningOps.isEmpty()) {
        QList<QDate> allDates = dates();
Jonah Brüchert's avatar
Jonah Brüchert committed
75
        std::sort(allDates.begin(), allDates.end());
Daniel Vrátil's avatar
Daniel Vrátil committed
76
77
78
79
80
        setDates(allDates);

        emitFinished();
    }
}