File indexing completed on 2024-11-17 04:45:00

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "folderarchivesettingpage.h"
0008 #include "folderarchivecombobox.h"
0009 #include "folderarchiveutil.h"
0010 
0011 #include <Akonadi/CollectionRequester>
0012 
0013 #include <KMime/Message>
0014 
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 
0018 #include <QCheckBox>
0019 #include <QDBusConnectionInterface>
0020 #include <QDBusInterface>
0021 #include <QDBusReply>
0022 #include <QFormLayout>
0023 
0024 FolderArchiveSettingPage::FolderArchiveSettingPage(const QString &instanceName, QWidget *parent)
0025     : QWidget(parent)
0026     , mInstanceName(instanceName)
0027     , mEnabled(new QCheckBox(i18n("Enable"), this))
0028     , mArchiveNamed(new FolderArchiveComboBox(this))
0029     , mArchiveFolder(new Akonadi::CollectionRequester(this))
0030 {
0031     auto lay = new QFormLayout(this);
0032     connect(mEnabled, &QCheckBox::toggled, this, &FolderArchiveSettingPage::slotEnableChanged);
0033     lay->addRow(QString(), mEnabled);
0034 
0035     mArchiveFolder->setMimeTypeFilter(QStringList() << KMime::Message::mimeType());
0036     lay->addRow(i18nc("@label:chooser for the folder that messages will be archived under", "Archive into:"), mArchiveFolder);
0037 
0038     lay->addRow(i18nc("@label:listbox", "Archive folder name:"), mArchiveNamed);
0039 }
0040 
0041 FolderArchiveSettingPage::~FolderArchiveSettingPage()
0042 {
0043     delete mInfo;
0044 }
0045 
0046 void FolderArchiveSettingPage::slotEnableChanged(bool enabled)
0047 {
0048     mArchiveFolder->setEnabled(enabled);
0049     mArchiveNamed->setEnabled(enabled);
0050 }
0051 
0052 void FolderArchiveSettingPage::loadSettings()
0053 {
0054     KConfig config(FolderArchive::FolderArchiveUtil::configFileName());
0055     const QString groupName = FolderArchive::FolderArchiveUtil::groupConfigPattern() + mInstanceName;
0056     if (config.hasGroup(groupName)) {
0057         KConfigGroup grp = config.group(groupName);
0058         mInfo = new FolderArchiveAccountInfo(grp);
0059         mEnabled->setChecked(mInfo->enabled());
0060         mArchiveFolder->setCollection(Akonadi::Collection(mInfo->archiveTopLevel()));
0061         mArchiveNamed->setType(mInfo->folderArchiveType());
0062     } else {
0063         mInfo = new FolderArchiveAccountInfo();
0064         mEnabled->setChecked(false);
0065     }
0066     slotEnableChanged(mEnabled->isChecked());
0067 }
0068 
0069 void FolderArchiveSettingPage::writeSettings()
0070 {
0071     KConfig config(FolderArchive::FolderArchiveUtil::configFileName());
0072     KConfigGroup grp = config.group(FolderArchive::FolderArchiveUtil::groupConfigPattern() + mInstanceName);
0073     mInfo->setInstanceName(mInstanceName);
0074     if (mArchiveFolder->collection().isValid()) {
0075         mInfo->setEnabled(mEnabled->isChecked());
0076         mInfo->setArchiveTopLevel(mArchiveFolder->collection().id());
0077     } else {
0078         mInfo->setEnabled(false);
0079         mInfo->setArchiveTopLevel(-1);
0080     }
0081 
0082     mInfo->setFolderArchiveType(mArchiveNamed->type());
0083     mInfo->writeConfig(grp);
0084 
0085     // Update cache from KMail
0086     const QString kmailInterface = QStringLiteral("org.kde.kmail");
0087     QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered(kmailInterface);
0088     if (!reply.isValid() || !reply.value()) {
0089         return;
0090     }
0091     QDBusInterface kmail(kmailInterface, QStringLiteral("/KMail"), QStringLiteral("org.kde.kmail.kmail"));
0092     kmail.asyncCall(QStringLiteral("reloadFolderArchiveConfig"));
0093 }
0094 
0095 #include "moc_folderarchivesettingpage.cpp"