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
)
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()) {
81 const Akonadi::Collection::Id id
= mMainWidget
->currentFolder()->collection().id();
82 QPointer
<Akonadi::CollectionPropertiesDialog
> dlg
= mHashDialogBox
.value(id
);
84 if (!pageToShow
.isEmpty()) {
85 dlg
->setCurrentPage(pageToShow
);
87 dlg
->activateWindow();
91 if (!KMKernel::self()->isOffline()) {
92 const Akonadi::AgentInstance agentInstance
= Akonadi::AgentManager::self()->instance(mMainWidget
->currentFolder()->collection().resource());
93 bool isOnline
= agentInstance
.isOnline();
95 showCollectionPropertiesContinued(pageToShow
, QPointer
<KPIM::ProgressItem
>());
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
*)),
110 connect(progressItem
.data(), &KPIM::ProgressItem::progressItemCanceled
,
111 KPIM::ProgressManager::instance(), &KPIM::ProgressManager::slotStandardCancelHandler
);
115 KMessageBox::information(
117 i18n("Network is unconnected. Folder information cannot be updated."));
118 showCollectionPropertiesContinued(pageToShow
, QPointer
<KPIM::ProgressItem
>());
122 void ManageShowCollectionProperties::slotCollectionPropertiesContinued(KJob
*job
)
125 QPointer
<KPIM::ProgressItem
> progressItem
;
128 Akonadi::CollectionAttributesSynchronizationJob
*sync
129 = dynamic_cast<Akonadi::CollectionAttributesSynchronizationJob
*>(job
);
131 if (sync
->property("collectionId") != mMainWidget
->currentFolder()->collection().id()) {
134 pageToShow
= sync
->property("pageToShow").toString();
135 progressItem
= sync
->property("progressItem").value
< QPointer
<KPIM::ProgressItem
> >();
137 disconnect(progressItem
, SIGNAL(progressItemCanceled(KPIM::ProgressItem
*)),
140 // progressItem does not exist anymore, operation has been canceled
145 showCollectionPropertiesContinued(pageToShow
, progressItem
);
148 void ManageShowCollectionProperties::showCollectionPropertiesContinued(const QString
&pageToShow
, QPointer
<KPIM::ProgressItem
> 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
)
176 QPointer
<KPIM::ProgressItem
> progressItem
= job
->property("progressItem").value
< QPointer
<KPIM::ProgressItem
> >();
177 // progressItem does not exist anymore, operation has been canceled
182 progressItem
->setComplete();
183 progressItem
->setStatus(i18n("Done"));
185 Akonadi::CollectionFetchJob
*fetch
= dynamic_cast<Akonadi::CollectionFetchJob
*>(job
);
187 if (fetch
->collections().isEmpty()) {
188 qCWarning(KMAIL_LOG
) << "no collection";
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
);
202 mHashDialogBox
.insert(collection
.id(), dlg
);