File indexing completed on 2024-11-24 04:43:38

0001 /*
0002     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003     SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "contactssettingswidget.h"
0009 #include "settings.h"
0010 
0011 #include <KConfigDialogManager>
0012 
0013 #include <KSharedConfig>
0014 #include <QPushButton>
0015 #include <QTimer>
0016 #include <QUrl>
0017 
0018 namespace
0019 {
0020 static const char myConfigGroupName[] = "ContactsSettingsDialog";
0021 }
0022 
0023 ContactsSettingsWidget::ContactsSettingsWidget(const KSharedConfigPtr &config, QWidget *parent, const QVariantList &args)
0024     : Akonadi::AgentConfigurationBase(config, parent, args)
0025 {
0026     ContactsResourceSettings::instance(config);
0027 
0028     auto mainWidget = new QWidget(parent);
0029 
0030     ui.setupUi(mainWidget);
0031     parent->layout()->addWidget(mainWidget);
0032     ui.kcfg_Path->setMode(KFile::LocalOnly | KFile::Directory);
0033 
0034     ui.label_3->setMinimumSize(ui.label_3->sizeHint());
0035     ui.label_2->setMinimumSize(ui.label_2->sizeHint());
0036     mManager = new KConfigDialogManager(mainWidget, ContactsResourceSettings::self());
0037 
0038     connect(ui.kcfg_Path, &KUrlRequester::textChanged, this, &ContactsSettingsWidget::validate);
0039     connect(ui.kcfg_ReadOnly, &QCheckBox::toggled, this, &ContactsSettingsWidget::validate);
0040 }
0041 
0042 ContactsSettingsWidget::~ContactsSettingsWidget() = default;
0043 
0044 void ContactsSettingsWidget::validate()
0045 {
0046     const QUrl currentUrl = ui.kcfg_Path->url();
0047     if (currentUrl.isEmpty()) {
0048         Q_EMIT enableOkButton(false);
0049         return;
0050     }
0051 
0052     const QFileInfo file(currentUrl.toLocalFile());
0053     if (file.exists() && !file.isWritable()) {
0054         ui.kcfg_ReadOnly->setEnabled(false);
0055         ui.kcfg_ReadOnly->setChecked(true);
0056     } else {
0057         ui.kcfg_ReadOnly->setEnabled(true);
0058     }
0059     Q_EMIT enableOkButton(true);
0060 }
0061 
0062 void ContactsSettingsWidget::load()
0063 {
0064     QTimer::singleShot(0, this, &ContactsSettingsWidget::validate);
0065 
0066     ui.kcfg_Path->setUrl(QUrl::fromLocalFile(ContactsResourceSettings::self()->path()));
0067     mManager->updateWidgets();
0068 }
0069 
0070 bool ContactsSettingsWidget::save() const
0071 {
0072     mManager->updateSettings();
0073     ContactsResourceSettings::self()->setPath(ui.kcfg_Path->url().toLocalFile());
0074     ContactsResourceSettings::self()->save();
0075     return true;
0076 }
0077 
0078 QSize ContactsSettingsWidget::restoreDialogSize() const
0079 {
0080     auto group = config()->group(QLatin1StringView(myConfigGroupName));
0081     const QSize size = group.readEntry("Size", QSize(600, 400));
0082     return size;
0083 }
0084 
0085 void ContactsSettingsWidget::saveDialogSize(const QSize &size)
0086 {
0087     auto group = config()->group(QLatin1StringView(myConfigGroupName));
0088     group.writeEntry("Size", size);
0089 }
0090 
0091 #include "moc_contactssettingswidget.cpp"