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 "automaticaddcontactsconfiguretab.h"
0008 #include "automaticaddcontactstabwidget.h"
0009 
0010 #include <KIdentityManagementCore/Identity>
0011 #include <KIdentityManagementCore/IdentityManager>
0012 #include <KSharedConfig>
0013 #include <QHBoxLayout>
0014 #include <QRegularExpression>
0015 #include <QTabWidget>
0016 
0017 AutomaticAddContactsConfigureTab::AutomaticAddContactsConfigureTab(QWidget *parent)
0018     : QWidget(parent)
0019     , mTabWidget(new QTabWidget(this))
0020 {
0021     auto mainLayout = new QHBoxLayout(this);
0022     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0023     mainLayout->setContentsMargins({});
0024     mTabWidget->setObjectName(QLatin1StringView("tabwidget"));
0025     mainLayout->addWidget(mTabWidget);
0026     initTab(KIdentityManagementCore::IdentityManager::self());
0027 }
0028 
0029 AutomaticAddContactsConfigureTab::~AutomaticAddContactsConfigureTab() = default;
0030 
0031 void AutomaticAddContactsConfigureTab::initTab(KIdentityManagementCore::IdentityManager *identityManager)
0032 {
0033     KIdentityManagementCore::IdentityManager::ConstIterator end = identityManager->end();
0034     for (KIdentityManagementCore::IdentityManager::ConstIterator it = identityManager->begin(); it != end; ++it) {
0035         auto w = new AutomaticAddContactsTabWidget(this);
0036         connect(w, &AutomaticAddContactsTabWidget::configureChanged, this, &AutomaticAddContactsConfigureTab::configureChanged);
0037         mTabWidget->addTab(w, QStringLiteral("%1 (%2)").arg((*it).identityName(), (*it).primaryEmailAddress()));
0038         w->setIdentity((*it).uoid());
0039         mListTabWidget.append(w);
0040     }
0041 }
0042 
0043 void AutomaticAddContactsConfigureTab::loadSettings()
0044 {
0045     for (AutomaticAddContactsTabWidget *w : std::as_const(mListTabWidget)) {
0046         w->loadSettings();
0047     }
0048 }
0049 
0050 void AutomaticAddContactsConfigureTab::saveSettings()
0051 {
0052     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0053     // first, delete all filter groups:
0054     const QStringList filterGroups = config->groupList().filter(QRegularExpression(QStringLiteral("Automatic Add Contacts \\d+")));
0055     for (const QString &group : filterGroups) {
0056         config->deleteGroup(group);
0057     }
0058     for (AutomaticAddContactsTabWidget *w : std::as_const(mListTabWidget)) {
0059         w->saveSettings();
0060     }
0061 }
0062 
0063 void AutomaticAddContactsConfigureTab::resetSettings()
0064 {
0065     for (AutomaticAddContactsTabWidget *w : std::as_const(mListTabWidget)) {
0066         w->resetSettings();
0067     }
0068 }
0069 
0070 #include "moc_automaticaddcontactsconfiguretab.cpp"