Clazy fix++
[kdepim.git] / kmail / manageshowcollectionproperties.cpp
blob203cd70a2335373c61276f9fa30679f0f452c14f
1 /*
2 Copyright (c) 2014-2015 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "manageshowcollectionproperties.h"
19 #include "kmmainwidget.h"
20 #include <KLocalizedString>
21 #include <KMessageBox>
22 #include <AkonadiCore/CollectionAttributesSynchronizationJob>
23 #include "kmail_debug.h"
24 #include <AkonadiCore/CollectionFetchJob>
25 #include <AkonadiWidgets/CollectionPropertiesDialog>
26 #include <AkonadiCore/CollectionFetchScope>
27 #include <AkonadiCore/AgentInstance>
28 #include <AkonadiCore/AgentManager>
30 Q_DECLARE_METATYPE(KPIM::ProgressItem *)
31 Q_DECLARE_METATYPE(Akonadi::Job *)
32 Q_DECLARE_METATYPE(QPointer<KPIM::ProgressItem>)
34 ManageShowCollectionProperties::ManageShowCollectionProperties(KMMainWidget *mainWidget, QObject *parent)
35 : QObject(parent),
36 mMainWidget(mainWidget)
38 mPages = QStringList() << QStringLiteral("MailCommon::CollectionGeneralPage")
39 << QStringLiteral("KMail::CollectionViewPage")
40 << QStringLiteral("Akonadi::CachePolicyPage")
41 << QStringLiteral("KMail::CollectionTemplatesPage")
42 << QStringLiteral("MailCommon::CollectionExpiryPage")
43 << QStringLiteral("PimCommon::CollectionAclPage")
44 << QStringLiteral("KMail::CollectionMailingListPage")
45 << QStringLiteral("KMail::CollectionQuotaPage")
46 << QStringLiteral("KMail::CollectionShortcutPage")
47 << QStringLiteral("KMail::CollectionMaintenancePage");
51 ManageShowCollectionProperties::~ManageShowCollectionProperties()
56 void ManageShowCollectionProperties::slotCollectionProperties()
58 showCollectionProperties(QString());
61 void ManageShowCollectionProperties::slotShowExpiryProperties()
63 showCollectionProperties(QStringLiteral("MailCommon::CollectionExpiryPage"));
66 void ManageShowCollectionProperties::slotFolderMailingListProperties()
68 showCollectionProperties(QStringLiteral("KMail::CollectionMailingListPage"));
71 void ManageShowCollectionProperties::slotShowFolderShortcutDialog()
73 showCollectionProperties(QStringLiteral("KMail::CollectionShortcutPage"));
76 void ManageShowCollectionProperties::showCollectionProperties(const QString &pageToShow)
78 if (!mMainWidget->currentFolder()) {
79 return;
81 const Akonadi::Collection::Id id = mMainWidget->currentFolder()->collection().id();
82 QPointer<Akonadi::CollectionPropertiesDialog> dlg = mHashDialogBox.value(id);
83 if (dlg) {
84 if (!pageToShow.isEmpty()) {
85 dlg->setCurrentPage(pageToShow);
87 dlg->activateWindow();
88 dlg->raise();
89 return;
91 if (!KMKernel::self()->isOffline()) {
92 const Akonadi::AgentInstance agentInstance = Akonadi::AgentManager::self()->instance(mMainWidget->currentFolder()->collection().resource());
93 bool isOnline = agentInstance.isOnline();
94 if (!isOnline) {
95 showCollectionPropertiesContinued(pageToShow, QPointer<KPIM::ProgressItem>());
96 } else {
97 QPointer<KPIM::ProgressItem> progressItem(KPIM::ProgressManager::createProgressItem(i18n("Retrieving folder properties")));
98 progressItem->setUsesBusyIndicator(true);
99 progressItem->setCryptoStatus(KPIM::ProgressItem::Unknown);
101 Akonadi::CollectionAttributesSynchronizationJob *sync
102 = new Akonadi::CollectionAttributesSynchronizationJob(mMainWidget->currentFolder()->collection());
103 sync->setProperty("collectionId", id);
104 sync->setProperty("pageToShow", pageToShow); // note for dialog later
105 sync->setProperty("progressItem", QVariant::fromValue(progressItem));
106 connect(sync, &KJob::result,
107 this, &ManageShowCollectionProperties::slotCollectionPropertiesContinued);
108 connect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
109 sync, SLOT(kill()));
110 connect(progressItem.data(), &KPIM::ProgressItem::progressItemCanceled,
111 KPIM::ProgressManager::instance(), &KPIM::ProgressManager::slotStandardCancelHandler);
112 sync->start();
114 } else {
115 KMessageBox::information(
116 mMainWidget,
117 i18n("Network is unconnected. Folder information cannot be updated."));
118 showCollectionPropertiesContinued(pageToShow, QPointer<KPIM::ProgressItem>());
122 void ManageShowCollectionProperties::slotCollectionPropertiesContinued(KJob *job)
124 QString pageToShow;
125 QPointer<KPIM::ProgressItem> progressItem;
127 if (job) {
128 Akonadi::CollectionAttributesSynchronizationJob *sync
129 = dynamic_cast<Akonadi::CollectionAttributesSynchronizationJob *>(job);
130 Q_ASSERT(sync);
131 if (sync->property("collectionId") != mMainWidget->currentFolder()->collection().id()) {
132 return;
134 pageToShow = sync->property("pageToShow").toString();
135 progressItem = sync->property("progressItem").value< QPointer<KPIM::ProgressItem> >();
136 if (progressItem) {
137 disconnect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
138 sync, SLOT(kill()));
139 } else {
140 // progressItem does not exist anymore, operation has been canceled
141 return;
145 showCollectionPropertiesContinued(pageToShow, progressItem);
148 void ManageShowCollectionProperties::showCollectionPropertiesContinued(const QString &pageToShow, QPointer<KPIM::ProgressItem> progressItem)
150 if (!progressItem) {
151 progressItem = KPIM::ProgressManager::createProgressItem(i18n("Retrieving folder properties"));
152 progressItem->setUsesBusyIndicator(true);
153 progressItem->setCryptoStatus(KPIM::ProgressItem::Unknown);
154 connect(progressItem.data(), &KPIM::ProgressItem::progressItemCanceled,
155 KPIM::ProgressManager::instance(), &KPIM::ProgressManager::slotStandardCancelHandler);
158 Akonadi::CollectionFetchJob *fetch = new Akonadi::CollectionFetchJob(mMainWidget->currentFolder()->collection(),
159 Akonadi::CollectionFetchJob::Base);
160 connect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), fetch, SLOT(kill()));
161 fetch->fetchScope().setIncludeStatistics(true);
162 fetch->setProperty("pageToShow", pageToShow);
163 fetch->setProperty("progressItem", QVariant::fromValue(progressItem));
164 connect(fetch, &KJob::result,
165 this, &ManageShowCollectionProperties::slotCollectionPropertiesFinished);
166 connect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
167 fetch, SLOT(kill()));
170 void ManageShowCollectionProperties::slotCollectionPropertiesFinished(KJob *job)
172 if (!job) {
173 return;
176 QPointer<KPIM::ProgressItem> progressItem = job->property("progressItem").value< QPointer<KPIM::ProgressItem> >();
177 // progressItem does not exist anymore, operation has been canceled
178 if (!progressItem) {
179 return;
182 progressItem->setComplete();
183 progressItem->setStatus(i18n("Done"));
185 Akonadi::CollectionFetchJob *fetch = dynamic_cast<Akonadi::CollectionFetchJob *>(job);
186 Q_ASSERT(fetch);
187 if (fetch->collections().isEmpty()) {
188 qCWarning(KMAIL_LOG) << "no collection";
189 return;
192 const Akonadi::Collection collection = fetch->collections().first();
194 QPointer<Akonadi::CollectionPropertiesDialog> dlg = new Akonadi::CollectionPropertiesDialog(collection, mPages, mMainWidget);
195 dlg->setWindowTitle(i18nc("@title:window", "Properties of Folder %1", collection.name()));
197 const QString pageToShow = fetch->property("pageToShow").toString();
198 if (!pageToShow.isEmpty()) { // show a specific page
199 dlg->setCurrentPage(pageToShow);
201 dlg->show();
202 mHashDialogBox.insert(collection.id(), dlg);