File indexing completed on 2024-12-22 04:57:33

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "configwidget.h"
0008 #include "resources/folderarchivesettings/folderarchivesettingpage.h"
0009 #include "settings.h"
0010 
0011 #include "maildir.h"
0012 
0013 #include <KConfigDialogManager>
0014 #include <KLineEdit>
0015 #include <QVBoxLayout>
0016 
0017 using KPIM::Maildir;
0018 using namespace Akonadi_Maildir_Resource;
0019 
0020 ConfigWidget::ConfigWidget(MaildirSettings *settings, const QString &identifier, QWidget *parent)
0021     : QWidget(parent)
0022     , mSettings(settings)
0023     , mToplevelIsContainer(false)
0024 {
0025     auto mainWidget = new QWidget(this);
0026     auto mainLayout = new QVBoxLayout(this);
0027     mainLayout->addWidget(mainWidget);
0028     ui.setupUi(mainWidget);
0029     mFolderArchiveSettingPage = new FolderArchiveSettingPage(identifier, this);
0030     ui.tabWidget->addTab(mFolderArchiveSettingPage, i18n("Archive Folder"));
0031 
0032     ui.kcfg_Path->setMode(KFile::Directory | KFile::ExistingOnly);
0033     ui.kcfg_Path->setUrl(QUrl::fromLocalFile(mSettings->path()));
0034 
0035     connect(ui.kcfg_Path->lineEdit(), &QLineEdit::textChanged, this, &ConfigWidget::checkPath);
0036     ui.kcfg_Path->lineEdit()->setFocus();
0037     checkPath();
0038 }
0039 
0040 ConfigWidget::~ConfigWidget() = default;
0041 
0042 void ConfigWidget::checkPath()
0043 {
0044     if (ui.kcfg_Path->url().isEmpty()) {
0045         ui.statusLabel->setText(i18n("The selected path is empty."));
0046         Q_EMIT okEnabled(false);
0047         return;
0048     }
0049     bool ok = false;
0050     mToplevelIsContainer = false;
0051     QDir d(ui.kcfg_Path->url().toLocalFile());
0052 
0053     if (d.exists()) {
0054         Maildir md(d.path());
0055         if (!md.isValid(false)) {
0056             Maildir md2(d.path(), true);
0057             if (md2.isValid(false)) {
0058                 ui.statusLabel->setText(i18n("The selected path contains valid Maildir folders."));
0059                 mToplevelIsContainer = true;
0060                 ok = true;
0061             } else {
0062                 ui.statusLabel->setText(md.lastError());
0063             }
0064         } else {
0065             ui.statusLabel->setText(i18n("The selected path is a valid Maildir."));
0066             ok = true;
0067         }
0068     } else {
0069         d.cdUp();
0070         if (d.exists()) {
0071             ui.statusLabel->setText(i18n("The selected path does not exist yet, a new Maildir will be created."));
0072             mToplevelIsContainer = true;
0073             ok = true;
0074         } else {
0075             ui.statusLabel->setText(i18n("The selected path does not exist."));
0076         }
0077     }
0078     Q_EMIT okEnabled(ok);
0079 }
0080 
0081 void ConfigWidget::load()
0082 {
0083     mFolderArchiveSettingPage->loadSettings();
0084     mManager = new KConfigDialogManager(this, mSettings);
0085     mManager->updateWidgets();
0086 }
0087 
0088 bool ConfigWidget::save() const
0089 {
0090     mFolderArchiveSettingPage->writeSettings();
0091     mManager->updateSettings();
0092     QString path = ui.kcfg_Path->url().isLocalFile() ? ui.kcfg_Path->url().toLocalFile() : ui.kcfg_Path->url().path();
0093     mSettings->setPath(path);
0094     mSettings->setTopLevelIsContainer(mToplevelIsContainer);
0095     mSettings->save();
0096 
0097     if (ui.kcfg_Path->url().isLocalFile()) {
0098         QDir d(path);
0099         if (!d.exists()) {
0100             d.mkpath(ui.kcfg_Path->url().toLocalFile());
0101         }
0102     }
0103 
0104     return true;
0105 }
0106 
0107 #include "moc_configwidget.cpp"