collectionmaintenancepage.cpp 7.43 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2009-2016 Montel Laurent <montel@kde.org>
3

Laurent Montel's avatar
Laurent Montel committed
4
5
6
7
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
8

Laurent Montel's avatar
Laurent Montel committed
9
10
11
12
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
13

Laurent Montel's avatar
Laurent Montel committed
14
15
16
17
   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18
19
20
*/

#include "collectionmaintenancepage.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "mailcommon/mailutil.h"
Laurent Montel's avatar
Laurent Montel committed
22
23
#include "kmkernel.h"

Laurent Montel's avatar
Laurent Montel committed
24
#include <AkonadiCore/collectionstatistics.h>
Laurent Montel's avatar
Laurent Montel committed
25
#include <AkonadiCore/collection.h>
Laurent Montel's avatar
Laurent Montel committed
26
#include <AkonadiCore/AgentManager>
Laurent Montel's avatar
Laurent Montel committed
27
#include <AkonadiCore/ChangeRecorder>
Laurent Montel's avatar
Laurent Montel committed
28

29
30
31
#include <QDBusInterface>
#include <QDBusConnectionInterface>

Laurent Montel's avatar
Laurent Montel committed
32
#include <QDialog>
Laurent Montel's avatar
Laurent Montel committed
33
#include <QPushButton>
34
#include <kio/global.h>
Laurent Montel's avatar
Laurent Montel committed
35
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
36
#include "kmail_debug.h"
37
38
39
#include <QGroupBox>
#include <QLabel>
#include <QFormLayout>
40
#include <QCheckBox>
Laurent Montel's avatar
Laurent Montel committed
41
#include <AkonadiCore/indexpolicyattribute.h>
42
#include <AkonadiCore/CachePolicy>
43
#include <KFormat>
Laurent Montel's avatar
Laurent Montel committed
44
#include <KConfigGroup>
45
#include <PimCommon/PimUtil>
46
47
48

using namespace Akonadi;

Laurent Montel's avatar
Laurent Montel committed
49
50
51
CollectionMaintenancePage::CollectionMaintenancePage(QWidget *parent) :
    CollectionPropertiesPage(parent),
    mIsNotAVirtualCollection(true),
Laurent Montel's avatar
Laurent Montel committed
52
53
    mFolderSizeLabel(Q_NULLPTR),
    mCollectionCount(Q_NULLPTR)
54
{
Laurent Montel's avatar
Laurent Montel committed
55
    setObjectName(QStringLiteral("KMail::CollectionMaintenancePage"));
Laurent Montel's avatar
Laurent Montel committed
56
    setPageTitle(i18n("Maintenance"));
57
58
}

Laurent Montel's avatar
Laurent Montel committed
59
void CollectionMaintenancePage::init(const Akonadi::Collection &col)
60
{
Laurent Montel's avatar
Laurent Montel committed
61
62
    mCurrentCollection = col;

Laurent Montel's avatar
Laurent Montel committed
63
64
65
66
    QVBoxLayout *topLayout = new QVBoxLayout(this);
    QGroupBox *filesGroup = new QGroupBox(i18n("Files"), this);
    QFormLayout *box = new QFormLayout(filesGroup);
    mIsNotAVirtualCollection = !MailCommon::Util::isVirtualCollection(col);
67
    connect(KMKernel::self()->folderCollectionMonitor(), &Monitor::collectionStatisticsChanged, this, &CollectionMaintenancePage::updateCollectionStatistic);
Laurent Montel's avatar
Laurent Montel committed
68

Laurent Montel's avatar
Laurent Montel committed
69
    const AgentInstance instance = Akonadi::AgentManager::self()->instance(col.resource());
Laurent Montel's avatar
Laurent Montel committed
70
71
    const QString folderDesc = instance.type().name();

Laurent Montel's avatar
Laurent Montel committed
72
73
74
    if (mIsNotAVirtualCollection) {
        QLabel *label = new QLabel(folderDesc, filesGroup);
        box->addRow(new QLabel(i18n("Folder type:"), filesGroup), label);
Laurent Montel's avatar
Laurent Montel committed
75
    }
76

Laurent Montel's avatar
Laurent Montel committed
77
78
    mFolderSizeLabel = new QLabel(i18nc("folder size", "Not available"), filesGroup);
    box->addRow(new QLabel(i18n("Size:"), filesGroup), mFolderSizeLabel);
Laurent Montel's avatar
Laurent Montel committed
79

Laurent Montel's avatar
Laurent Montel committed
80
    topLayout->addWidget(filesGroup);
81

Laurent Montel's avatar
Laurent Montel committed
82
83
    QGroupBox *messagesGroup = new QGroupBox(i18n("Messages"), this);
    box = new QFormLayout(messagesGroup);
84

Laurent Montel's avatar
Laurent Montel committed
85
86
    mCollectionCount = new QLabel(messagesGroup);
    box->addRow(new QLabel(i18n("Total messages:"), messagesGroup), mCollectionCount);
87

Laurent Montel's avatar
Laurent Montel committed
88
89
    mCollectionUnread = new QLabel(messagesGroup);
    box->addRow(new QLabel(i18n("Unread messages:"), messagesGroup), mCollectionUnread);
90

Laurent Montel's avatar
Laurent Montel committed
91
    topLayout->addWidget(messagesGroup);
92
    QGroupBox *indexingGroupBox = new QGroupBox(i18n("Indexing"), this);
93
    QVBoxLayout *indexingLayout = new QVBoxLayout(indexingGroupBox);
Laurent Montel's avatar
Laurent Montel committed
94
95
    mIndexingEnabled = new QCheckBox(i18n("Enable Full Text Indexing"));
    indexingLayout->addWidget(mIndexingEnabled);
96

Laurent Montel's avatar
Laurent Montel committed
97
    mLastIndexed = new QLabel(i18n("Still not indexed."));
98

Laurent Montel's avatar
Laurent Montel committed
99
    indexingLayout->addWidget(mLastIndexed);
100
101
102
103
104

    mReindexCollection = new QPushButton(i18n("Reindex current Collection"), this);
    connect(mReindexCollection, &QPushButton::clicked, this, &CollectionMaintenancePage::slotReindexCollection);
    mReindexCollection->setObjectName(QStringLiteral("reindexbutton"));
    indexingLayout->addWidget(mReindexCollection);
105
106
107
108
    if (PimCommon::Util::isImapResource(col.resource()) && !col.cachePolicy().localParts().contains(QLatin1String("RFC822"))) {
        indexingGroupBox->hide();
    }

Laurent Montel's avatar
Laurent Montel committed
109
    mIndexedInfo = new QLabel(this);
Laurent Montel's avatar
Laurent Montel committed
110
111
112
113
    QFont f = mIndexedInfo->font();
    f.setBold(true);
    mIndexedInfo->setFont(f);
    indexingLayout->addWidget(mIndexedInfo, Qt::AlignHCenter);
114

115
    topLayout->addWidget(indexingGroupBox);
Laurent Montel's avatar
Laurent Montel committed
116
    topLayout->addStretch(100);
117
118
}

119
120
121
122
123
void CollectionMaintenancePage::slotReindexCollection()
{
    if (mCurrentCollection.isValid()) {
        //Don't allow to reindex twice.
        mReindexCollection->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
124
125

        QDBusInterface interfaceBalooIndexer(QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_indexing_agent"), QStringLiteral("/"), QStringLiteral("org.freedesktop.Akonadi.Indexer"));
126
        if (interfaceBalooIndexer.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
127
            interfaceBalooIndexer.call(QStringLiteral("reindexCollection"), (qlonglong)mCurrentCollection.id());
Yuri Chornoivan's avatar
Yuri Chornoivan committed
128
            mIndexedInfo->setText(i18n("Remember that indexing can take some minutes."));
129
130
131
132
133
134
        } else {
            qCWarning(KMAIL_LOG) << "indexer interface not valid";
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
135
void CollectionMaintenancePage::load(const Collection &col)
136
{
Laurent Montel's avatar
Laurent Montel committed
137
138
139
    init(col);
    if (col.isValid()) {
        updateLabel(col.statistics().count(), col.statistics().unreadCount(), col.statistics().size());
Laurent Montel's avatar
Laurent Montel committed
140
141
        Akonadi::IndexPolicyAttribute *attr = col.attribute<Akonadi::IndexPolicyAttribute>();
        const bool indexingWasEnabled(!attr || attr->indexingEnabled());
Laurent Montel's avatar
Laurent Montel committed
142
143
        mIndexingEnabled->setChecked(indexingWasEnabled);
        if (!indexingWasEnabled) {
Laurent Montel's avatar
Laurent Montel committed
144
            mLastIndexed->hide();
Laurent Montel's avatar
Laurent Montel committed
145
        } else {
146
            QDBusInterface interfaceBalooIndexer(QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_indexing_agent"), QStringLiteral("/"));
Laurent Montel's avatar
Laurent Montel committed
147
            if (interfaceBalooIndexer.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
148
                if (!interfaceBalooIndexer.callWithCallback(QStringLiteral("indexedItems"), QList<QVariant>() << (qlonglong)mCurrentCollection.id(), this, SLOT(onIndexedItemsReceived(qint64)))) {
Laurent Montel's avatar
Laurent Montel committed
149
                    qCWarning(KMAIL_LOG) << "Failed to request indexed items";
Laurent Montel's avatar
Laurent Montel committed
150
                }
151
152
            } else {
                qCWarning(KMAIL_LOG) << "Dbus interface invalid.";
Laurent Montel's avatar
Laurent Montel committed
153
            }
154
        }
155
    }
156
157
}

158
159
void CollectionMaintenancePage::onIndexedItemsReceived(qint64 num)
{
Laurent Montel's avatar
Debug--    
Laurent Montel committed
160
    //qCDebug(KMAIL_LOG) << num;
161
    if (num == 0) {
Laurent Montel's avatar
Laurent Montel committed
162
        mLastIndexed->clear();
163
    } else {
Laurent Montel's avatar
Laurent Montel committed
164
        mLastIndexed->setText(i18np("Indexed %1 item of this collection", "Indexed %1 items of this collection", num));
165
    }
166
167
}

Laurent Montel's avatar
Laurent Montel committed
168
void CollectionMaintenancePage::updateLabel(qint64 nbMail, qint64 nbUnreadMail, qint64 size)
169
{
Laurent Montel's avatar
Laurent Montel committed
170
171
172
    mCollectionCount->setText(QString::number(qMax(0LL, nbMail)));
    mCollectionUnread->setText(QString::number(qMax(0LL, nbUnreadMail)));
    mFolderSizeLabel->setText(KFormat().formatByteSize(qMax(0LL, size)));
173
174
}

175
void CollectionMaintenancePage::save(Collection &collection)
176
{
Laurent Montel's avatar
Laurent Montel committed
177
    if (!collection.hasAttribute<Akonadi::IndexPolicyAttribute>() && mIndexingEnabled->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
178
        return;
Laurent Montel's avatar
Laurent Montel committed
179
180
181
182
183
184
    }
    Akonadi::IndexPolicyAttribute *attr = collection.attribute<Akonadi::IndexPolicyAttribute>(Akonadi::Collection::AddIfMissing);
    if (mIndexingEnabled->isChecked()) {
        attr->setIndexingEnabled(true);
    } else {
        attr->setIndexingEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
185
    }
186
187
}

Laurent Montel's avatar
Laurent Montel committed
188
void CollectionMaintenancePage::updateCollectionStatistic(Akonadi::Collection::Id id, const Akonadi::CollectionStatistics &statistic)
189
{
Laurent Montel's avatar
Laurent Montel committed
190
191
    if (id == mCurrentCollection.id()) {
        updateLabel(statistic.count(), statistic.unreadCount(), statistic.size());
Laurent Montel's avatar
Laurent Montel committed
192
    }
193
194
}