summaryrefslogtreecommitdiff
path: root/src/kueapplication.cpp
blob: b459acff34570de986a6602826af2653c12511bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2024 Alexander Kavon <me+kue@alexkavon.com>

#include "kueapplication.h"
#include <KAuthorized>
#include <KLocalizedString>
#include <KDescendantsProxyModel>
#include <Akonadi/ServerManager>
#include <Akonadi/Session>
#include <Akonadi/Monitor>
#include <Akonadi/EntityTreeModel>
#include <Akonadi/CollectionFilterProxyModel>
#include <KMime/Message>
#include <QApplication>
#include <QDebug>

using namespace Qt::StringLiterals;

KueApplication::KueApplication(QObject *parent)
    : AbstractKirigamiApplication(parent)
    , m_loading(true)
{
    m_session = new Akonadi::Session(QByteArrayLiteral("Kue Kernel"), this);
    
    auto monitor = new Akonadi::Monitor(this);

    auto treeModel = new Akonadi::EntityTreeModel(monitor, this);
    treeModel->setItemPopulationStrategy(Akonadi::EntityTreeModel::LazyPopulation);

    auto entityTreeModel = new Akonadi::CollectionFilterProxyModel(this);
    entityTreeModel->setSourceModel(treeModel);
    entityTreeModel->addMimeTypeFilter(KMime::Message::mimeType());

    m_descendantsProxyModel = new KDescendantsProxyModel(this);
    m_descendantsProxyModel->setSourceModel(entityTreeModel);
    qDebug() << m_descendantsProxyModel->rowCount();

    if (Akonadi::ServerManager::isRunning()) {
        m_loading = false;
    } else {
        connect(Akonadi::ServerManager::self(), &Akonadi::ServerManager::stateChanged, this, [this](Akonadi::ServerManager::State state) {
            if (state == Akonadi::ServerManager::Broken) {
                qDebug() << "ServerManager Broken";
                qApp->exit(-1);
                return;
            }
            bool loading = state != Akonadi::ServerManager::State::Running;
            if (loading == m_loading) {
                return;
            }
            m_loading = loading;
            Q_EMIT loadingChanged();
            disconnect(Akonadi::ServerManager::self(), &Akonadi::ServerManager::stateChanged, this, nullptr);
        });
    }
}

bool KueApplication::loading() const
{
    return m_loading;
}

KDescendantsProxyModel *KueApplication::descendantsProxyModel() const
{
    return m_descendantsProxyModel;
}

Akonadi::Session *KueApplication::session() const
{
    return m_session;
}

#include "moc_kueapplication.cpp"