File indexing completed on 2024-12-22 05:01:13

0001 /*
0002   This file is part of KDE Kontact.
0003 
0004   SPDX-FileCopyrightText: 2004 Tobias Koenig <tokoe@kde.org>
0005   SPDX-FileCopyrightText: 2008 Allen Winter <winter@kde.org>
0006 
0007   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0008 */
0009 
0010 #include "kcmkontactsummary.h"
0011 #include <KAboutData>
0012 #include <KConfig>
0013 #include <KConfigGroup>
0014 #include <KLocalizedString>
0015 #include <KPluginFactory>
0016 #include <KPluginMetaData>
0017 #include <KontactInterface/Plugin>
0018 #include <QIcon>
0019 
0020 #include <QLabel>
0021 #include <QVBoxLayout>
0022 
0023 class PluginItem : public QTreeWidgetItem
0024 {
0025 public:
0026     PluginItem(const KPluginMetaData &info, QTreeWidget *parent)
0027         : QTreeWidgetItem(parent)
0028         , mInfo(info)
0029     {
0030         setIcon(0, QIcon::fromTheme(mInfo.iconName()));
0031         setText(0, mInfo.name());
0032         setToolTip(0, mInfo.description());
0033         setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
0034     }
0035 
0036     [[nodiscard]] KPluginMetaData pluginInfo() const
0037     {
0038         return mInfo;
0039     }
0040 
0041     [[nodiscard]] virtual QString text(int column) const
0042     {
0043         if (column == 0) {
0044             return mInfo.name();
0045         } else if (column == 1) {
0046             return mInfo.description();
0047         } else {
0048             return {};
0049         }
0050     }
0051 
0052 private:
0053     Q_DISABLE_COPY(PluginItem)
0054     const KPluginMetaData mInfo;
0055 };
0056 
0057 PluginView::PluginView(QWidget *parent)
0058     : QTreeWidget(parent)
0059 {
0060     setColumnCount(1);
0061     setHeaderLabel(i18nc("@title:column plugin name", "Summary Plugin Name"));
0062     setRootIsDecorated(false);
0063 }
0064 
0065 PluginView::~PluginView() = default;
0066 
0067 K_PLUGIN_CLASS_WITH_JSON(KCMKontactSummary, "kcmkontactsummary.json")
0068 
0069 KCMKontactSummary::KCMKontactSummary(QObject *parent, const KPluginMetaData &data)
0070     : KCModule(parent, data)
0071     , mPluginView(new PluginView(widget()))
0072 {
0073     auto layout = new QVBoxLayout(widget());
0074 
0075     layout->setContentsMargins({});
0076     auto label = new QLabel(i18n("Select the plugin summaries to show on the summary page."), widget());
0077     layout->addWidget(label);
0078 
0079     layout->addWidget(mPluginView);
0080 
0081     layout->setStretchFactor(mPluginView, 1);
0082 
0083     load();
0084     connect(mPluginView, &QTreeWidget::itemChanged, this, &KCMKontactSummary::markAsChanged);
0085 }
0086 
0087 void KCMKontactSummary::load()
0088 {
0089     const QList<KPluginMetaData> pluginMetaDatas = KPluginMetaData::findPlugins(QStringLiteral("pim6/kontact"), [](const KPluginMetaData &data) {
0090         return data.rawData().value(QStringLiteral("X-KDE-KontactPluginVersion")).toInt() == KONTACT_PLUGIN_VERSION;
0091     });
0092 
0093     QStringList activeSummaries;
0094 
0095     KConfig config(QStringLiteral("kontact_summaryrc"));
0096     KConfigGroup grp(&config, QString());
0097     if (grp.hasKey("ActiveSummaries")) {
0098         activeSummaries = grp.readEntry("ActiveSummaries", QStringList());
0099     } else {
0100         activeSummaries << QStringLiteral("kontact_kaddressbookplugin");
0101         activeSummaries << QStringLiteral("kontact_specialdatesplugin");
0102         activeSummaries << QStringLiteral("kontact_korganizerplugin");
0103         activeSummaries << QStringLiteral("kontact_todoplugin");
0104         activeSummaries << QStringLiteral("kontact_knotesplugin");
0105         activeSummaries << QStringLiteral("kontact_kmailplugin");
0106     }
0107 
0108     mPluginView->clear();
0109 
0110     for (const auto &plugin : std::as_const(pluginMetaDatas)) {
0111         QVariant var = plugin.value(QStringLiteral("X-KDE-KontactPluginHasSummary"), false);
0112         if (var.isValid() && var.toBool() == true) {
0113             auto item = new PluginItem(plugin, mPluginView);
0114 
0115             if (activeSummaries.contains(plugin.pluginId())) {
0116                 item->setCheckState(0, Qt::Checked);
0117             } else {
0118                 item->setCheckState(0, Qt::Unchecked);
0119             }
0120         }
0121     }
0122 }
0123 
0124 void KCMKontactSummary::save()
0125 {
0126     QStringList activeSummaries;
0127 
0128     QTreeWidgetItemIterator it(mPluginView);
0129     while (*it) {
0130         auto item = static_cast<PluginItem *>(*it);
0131         if (item->checkState(0) == Qt::Checked) {
0132             activeSummaries.append(item->pluginInfo().pluginId());
0133         }
0134         ++it;
0135     }
0136 
0137     KConfig config(QStringLiteral("kontact_summaryrc"));
0138     KConfigGroup grp(&config, QString());
0139     grp.writeEntry("ActiveSummaries", activeSummaries);
0140 }
0141 #include "kcmkontactsummary.moc"
0142 
0143 #include "moc_kcmkontactsummary.cpp"