kcmkontactsummary.cpp 5.05 KB
Newer Older
1
2
3
/*
  This file is part of KDE Kontact.

4
5
6
7
  SPDX-FileCopyrightText: 2004 Tobias Koenig <tokoe@kde.org>
  SPDX-FileCopyrightText: 2008 Allen Winter <winter@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
8
9
10
11
12
*/

#include "kcmkontactsummary.h"

#include <KAboutData>
Laurent Montel's avatar
Laurent Montel committed
13
#include <KConfig>
14
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
15
#include <KPluginFactory>
16
#include <KPluginInfo>
17
#include <KPluginMetaData>
Laurent Montel's avatar
Laurent Montel committed
18
#include <KontactInterface/Plugin>
Laurent Montel's avatar
Laurent Montel committed
19
#include <QIcon>
20
21
22
23
24
25
26
27

#include <QLabel>
#include <QVBoxLayout>

class PluginItem : public QTreeWidgetItem
{
public:
    PluginItem(const KPluginInfo &info, QTreeWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
28
29
        : QTreeWidgetItem(parent)
        , mInfo(info)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    {
        setIcon(0, QIcon::fromTheme(mInfo.icon()));
        setText(0, mInfo.name());
        setToolTip(0, mInfo.comment());
        setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
    }

    KPluginInfo pluginInfo() const
    {
        return mInfo;
    }

    virtual QString text(int column) const
    {
        if (column == 0) {
            return mInfo.name();
        } else if (column == 1) {
            return mInfo.comment();
        } else {
Laurent Montel's avatar
Laurent Montel committed
49
            return {};
50
51
52
53
        }
    }

private:
54
    Q_DISABLE_COPY(PluginItem)
Laurent Montel's avatar
Laurent Montel committed
55
    const KPluginInfo mInfo;
56
57
58
59
60
61
62
63
64
65
};

PluginView::PluginView(QWidget *parent)
    : QTreeWidget(parent)
{
    setColumnCount(1);
    setHeaderLabel(i18nc("@title:column plugin name", "Summary Plugin Name"));
    setRootIsDecorated(false);
}

Laurent Montel's avatar
Laurent Montel committed
66
PluginView::~PluginView() = default;
67

68
K_PLUGIN_CLASS_WITH_JSON(KCMKontactSummary, "kcmkontactsummary.json")
Laurent Montel's avatar
Laurent Montel committed
69
70
71

KCMKontactSummary::KCMKontactSummary(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
72
73
{
    setButtons(NoAdditionalButton);
74
    auto layout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
75
    layout->setContentsMargins({});
76

77
    auto label = new QLabel(i18n("Select the plugin summaries to show on the summary page."), this);
78
79
80
81
82
83
84
85
    layout->addWidget(label);

    mPluginView = new PluginView(this);
    layout->addWidget(mPluginView);

    layout->setStretchFactor(mPluginView, 1);

    load();
Laurent Montel's avatar
Laurent Montel committed
86
    connect(mPluginView, &QTreeWidget::itemChanged, this, &KCMKontactSummary::markAsChanged);
Laurent Montel's avatar
Laurent Montel committed
87
    auto about = new KAboutData(QStringLiteral("kontactsummary"),
Laurent Montel's avatar
Laurent Montel committed
88
89
90
91
92
                                i18n("kontactsummary"),
                                QString(),
                                i18n("KDE Kontact Summary"),
                                KAboutLicense::GPL,
                                i18n("(c), 2004 Tobias Koenig"));
93
94
95
96
97
98
    about->addAuthor(ki18n("Tobias Koenig").toString(), QString(), QStringLiteral("tokoe@kde.org"));
    setAboutData(about);
}

void KCMKontactSummary::load()
{
99
100
101
    const QVector<KPluginMetaData> pluginMetaDatas = KPluginLoader::findPlugins(QStringLiteral("kontact5"), [](const KPluginMetaData &data) {
        return data.rawData().value(QStringLiteral("X-KDE-KontactPluginVersion")).toInt() == KONTACT_PLUGIN_VERSION;
    });
102
103
104
105
106

    QStringList activeSummaries;

    KConfig config(QStringLiteral("kontact_summaryrc"));
    KConfigGroup grp(&config, QString());
Laurent Montel's avatar
Laurent Montel committed
107
108
109
    if (grp.hasKey("ActiveSummaries")) {
        activeSummaries = grp.readEntry("ActiveSummaries", QStringList());
    } else {
110
111
112
113
114
115
116
117
118
119
120
121
122
        activeSummaries << QStringLiteral("kontact_kaddressbookplugin");
        activeSummaries << QStringLiteral("kontact_specialdatesplugin");
        activeSummaries << QStringLiteral("kontact_korganizerplugin");
        activeSummaries << QStringLiteral("kontact_todoplugin");
        activeSummaries << QStringLiteral("kontact_knotesplugin");
        activeSummaries << QStringLiteral("kontact_kmailplugin");
        activeSummaries << QStringLiteral("kontact_weatherplugin");
        activeSummaries << QStringLiteral("kontact_newstickerplugin");
        activeSummaries << QStringLiteral("kontact_plannerplugin");
    }

    mPluginView->clear();

123
    KPluginInfo::List pluginList = KPluginInfo::fromMetaData(pluginMetaDatas);
124
125
126
    KPluginInfo::List::Iterator it;
    KPluginInfo::List::Iterator end(pluginList.end());
    for (it = pluginList.begin(); it != end; ++it) {
127
        it->setConfig(KConfigGroup(&config, "Plugins"));
128
129
130
131
132
133
134
135
        it->load();

        if (!it->isPluginEnabled()) {
            continue;
        }

        QVariant var = it->property(QStringLiteral("X-KDE-KontactPluginHasSummary"));
        if (var.isValid() && var.toBool() == true) {
136
            auto item = new PluginItem(*it, mPluginView);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

            if (activeSummaries.contains(it->pluginName())) {
                item->setCheckState(0, Qt::Checked);
            } else {
                item->setCheckState(0, Qt::Unchecked);
            }
        }
    }
}

void KCMKontactSummary::save()
{
    QStringList activeSummaries;

    QTreeWidgetItemIterator it(mPluginView);
    while (*it) {
153
        auto item = static_cast<PluginItem *>(*it);
154
155
156
157
158
159
160
161
162
163
        if (item->checkState(0) == Qt::Checked) {
            activeSummaries.append(item->pluginInfo().pluginName());
        }
        ++it;
    }

    KConfig config(QStringLiteral("kontact_summaryrc"));
    KConfigGroup grp(&config, QString());
    grp.writeEntry("ActiveSummaries", activeSummaries);
}
Laurent Montel's avatar
Laurent Montel committed
164
#include "kcmkontactsummary.moc"