File indexing completed on 2025-01-05 04:49:30

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "automaticaddcontactstabwidget.h"
0008 #include <Akonadi/Collection>
0009 #include <Akonadi/CollectionComboBox>
0010 #include <KConfigGroup>
0011 #include <KContacts/Addressee>
0012 #include <KLocalizedString>
0013 #include <KSharedConfig>
0014 #include <QCheckBox>
0015 #include <QLabel>
0016 #include <QVBoxLayout>
0017 
0018 namespace
0019 {
0020 QString configGroupName()
0021 {
0022     return QStringLiteral("Automatic Add Contacts %1");
0023 }
0024 }
0025 AutomaticAddContactsTabWidget::AutomaticAddContactsTabWidget(QWidget *parent, QAbstractItemModel *model)
0026     : QWidget(parent)
0027     , mEnabled(new QCheckBox(i18n("Automatic Add Contacts"), this))
0028     , mCollectionCombobox(new Akonadi::CollectionComboBox(model, this))
0029 {
0030     auto mainLayout = new QVBoxLayout(this);
0031     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0032     mEnabled->setObjectName(QLatin1StringView("enabled"));
0033     connect(mEnabled, &QCheckBox::clicked, this, &AutomaticAddContactsTabWidget::configureChanged);
0034     mainLayout->addWidget(mEnabled);
0035 
0036     auto hlay = new QHBoxLayout;
0037     hlay->setContentsMargins({});
0038     hlay->setObjectName(QLatin1StringView("folderlayout"));
0039     mainLayout->addLayout(hlay);
0040 
0041     auto lab = new QLabel(i18n("Select the addressbook in which to store contacts:"), this);
0042     lab->setObjectName(QLatin1StringView("labelfolder"));
0043     hlay->addWidget(lab);
0044 
0045     mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
0046     mCollectionCombobox->setMinimumWidth(250);
0047     mCollectionCombobox->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
0048     mCollectionCombobox->setObjectName(QLatin1StringView("akonadicombobox"));
0049     hlay->addWidget(mCollectionCombobox);
0050     hlay->addStretch(1);
0051     connect(mCollectionCombobox, &Akonadi::CollectionComboBox::currentIndexChanged, this, &AutomaticAddContactsTabWidget::configureChanged);
0052     connect(mCollectionCombobox, &Akonadi::CollectionComboBox::activated, this, &AutomaticAddContactsTabWidget::configureChanged);
0053 
0054     mainLayout->addStretch(1);
0055 }
0056 
0057 AutomaticAddContactsTabWidget::~AutomaticAddContactsTabWidget() = default;
0058 
0059 void AutomaticAddContactsTabWidget::resetSettings()
0060 {
0061     mEnabled->setChecked(false);
0062 }
0063 
0064 void AutomaticAddContactsTabWidget::loadSettings()
0065 {
0066     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0067     KConfigGroup grp = config->group(configGroupName().arg(mIdentity));
0068     mEnabled->setChecked(grp.readEntry("Enabled", false));
0069     mCollectionCombobox->setDefaultCollection(Akonadi::Collection(grp.readEntry("Collection", -1)));
0070 }
0071 
0072 void AutomaticAddContactsTabWidget::setIdentity(uint identity)
0073 {
0074     mIdentity = identity;
0075 }
0076 
0077 void AutomaticAddContactsTabWidget::saveSettings()
0078 {
0079     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0080     KConfigGroup grp = config->group(configGroupName().arg(mIdentity));
0081     grp.writeEntry("Enabled", mEnabled->isChecked());
0082     grp.writeEntry("Collection", mCollectionCombobox->currentCollection().id());
0083 }
0084 
0085 #include "moc_automaticaddcontactstabwidget.cpp"