File indexing completed on 2024-11-24 04:44:20
0001 /* 0002 SPDX-FileCopyrightText: 2009 Bertjan Broeksema <broeksema@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "compactpage.h" 0008 0009 #include <Akonadi/CollectionFetchJob> 0010 #include <Akonadi/CollectionModifyJob> 0011 #include <KLocalizedString> 0012 #include <KMbox/MBox> 0013 #include <QUrl> 0014 0015 #include "deleteditemsattribute.h" 0016 0017 #include <QFileInfo> 0018 0019 using namespace Akonadi; 0020 0021 CompactPage::CompactPage(const QString &collectionId, QWidget *parent) 0022 : QWidget(parent) 0023 , mCollectionId(collectionId) 0024 { 0025 ui.setupUi(this); 0026 0027 connect(ui.compactButton, &QPushButton::clicked, this, &CompactPage::compact); 0028 0029 checkCollectionId(); 0030 } 0031 0032 void CompactPage::checkCollectionId() 0033 { 0034 if (!mCollectionId.isEmpty()) { 0035 Collection collection; 0036 collection.setRemoteId(mCollectionId); 0037 auto fetchJob = new CollectionFetchJob(collection, CollectionFetchJob::Base); 0038 0039 connect(fetchJob, &CollectionFetchJob::result, this, &CompactPage::onCollectionFetchCheck); 0040 } 0041 } 0042 0043 void CompactPage::compact() 0044 { 0045 ui.compactButton->setEnabled(false); 0046 0047 Collection collection; 0048 collection.setRemoteId(mCollectionId); 0049 auto fetchJob = new CollectionFetchJob(collection, CollectionFetchJob::Base); 0050 0051 connect(fetchJob, &CollectionFetchJob::result, this, &CompactPage::onCollectionFetchCompact); 0052 } 0053 0054 void CompactPage::onCollectionFetchCheck(KJob *job) 0055 { 0056 if (job->error()) { 0057 // If we cannot fetch the collection, than also disable compacting. 0058 ui.compactButton->setEnabled(false); 0059 return; 0060 } 0061 0062 auto fetchJob = qobject_cast<CollectionFetchJob *>(job); 0063 Q_ASSERT(fetchJob); 0064 Q_ASSERT(fetchJob->collections().size() == 1); 0065 0066 Collection mboxCollection = fetchJob->collections().at(0); 0067 auto attr = mboxCollection.attribute<DeletedItemsAttribute>(Akonadi::Collection::AddIfMissing); 0068 0069 if (!attr->deletedItemOffsets().isEmpty()) { 0070 ui.compactButton->setEnabled(true); 0071 ui.messageLabel->setText(i18np("(1 message marked for deletion)", "(%1 messages marked for deletion)", attr->deletedItemOffsets().size())); 0072 } 0073 } 0074 0075 void CompactPage::onCollectionFetchCompact(KJob *job) 0076 { 0077 if (job->error()) { 0078 ui.messageLabel->setText(i18n("Failed to fetch the collection.")); 0079 ui.compactButton->setEnabled(true); 0080 return; 0081 } 0082 0083 auto fetchJob = qobject_cast<CollectionFetchJob *>(job); 0084 Q_ASSERT(fetchJob); 0085 Q_ASSERT(fetchJob->collections().size() == 1); 0086 0087 Collection mboxCollection = fetchJob->collections().at(0); 0088 auto attr = mboxCollection.attribute<DeletedItemsAttribute>(Akonadi::Collection::AddIfMissing); 0089 0090 KMBox::MBox mbox; 0091 // TODO: Set lock method. 0092 const QString fileName = QUrl::fromLocalFile(mCollectionId).toLocalFile(); 0093 if (!mbox.load(fileName)) { 0094 ui.messageLabel->setText(i18n("Failed to load the mbox file")); 0095 } else { 0096 ui.messageLabel->setText(i18np("(Deleting 1 message)", "(Deleting %1 messages)", attr->offsetCount())); 0097 // TODO: implement and connect to messageProcessed signal. 0098 if (mbox.purge(attr->deletedItemEntries()) || (QFileInfo(fileName).size() == 0)) { 0099 // even if purge() failed but the file is now empty. 0100 // it was probably deleted/emptied by an external prog. For whatever reason 0101 // doesn't matter here. We know the file is empty so we can get rid 0102 // of our stored DeletedItemsAttribute 0103 mboxCollection.removeAttribute<DeletedItemsAttribute>(); 0104 auto modifyJob = new CollectionModifyJob(mboxCollection); 0105 connect(modifyJob, &CollectionModifyJob::result, this, &CompactPage::onCollectionModify); 0106 } else { 0107 ui.messageLabel->setText(i18n("Failed to compact the mbox file.")); 0108 } 0109 } 0110 } 0111 0112 void CompactPage::onCollectionModify(KJob *job) 0113 { 0114 if (job->error()) { 0115 ui.messageLabel->setText(i18n("Failed to compact the mbox file.")); 0116 } else { 0117 ui.messageLabel->setText(i18n("MBox file compacted.")); 0118 } 0119 } 0120 0121 #include "moc_compactpage.cpp"