servicessettingspage.cpp 10.8 KB
Newer Older
1
2
3
4
5
/*
 * SPDX-FileCopyrightText: 2009-2010 Peter Penz <peter.penz19@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
6

7
#include "servicessettingspage.h"
8

Peter Penz's avatar
Peter Penz committed
9
#include "dolphin_generalsettings.h"
10
#include "dolphin_versioncontrolsettings.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
11
12
#include "settings/serviceitemdelegate.h"
#include "settings/servicemodel.h"
13

Peter Penz's avatar
Peter Penz committed
14
#include <KDesktopFile>
Laurent Montel's avatar
Laurent Montel committed
15
#include <KLocalizedString>
Peter Penz's avatar
Peter Penz committed
16
#include <KMessageBox>
17
#include <KNS3/Button>
18
#include <KPluginMetaData>
Peter Penz's avatar
Peter Penz committed
19
#include <KService>
20
#include <KServiceTypeTrader>
21
#include <KDesktopFileActions>
22

23
#include <QGridLayout>
24
25
#include <QLabel>
#include <QListWidget>
Antonio Rojas's avatar
Antonio Rojas committed
26
#include <QShowEvent>
27
#include <QSortFilterProxyModel>
28
#include <QLineEdit>
29

Peter Penz's avatar
Peter Penz committed
30
31
32
namespace
{
    const bool ShowDeleteDefault = false;
33
34
35
    const char VersionControlServicePrefix[] = "_version_control_";
    const char DeleteService[] = "_delete";
    const char CopyToMoveToService[] ="_copy_to_move_to";
Peter Penz's avatar
Peter Penz committed
36
37
}

38
ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
39
40
    SettingsPageBase(parent),
    m_initialized(false),
Kevin Funk's avatar
Kevin Funk committed
41
42
43
    m_serviceModel(nullptr),
    m_sortModel(nullptr),
    m_listView(nullptr),
44
    m_enabledVcsPlugins()
45
{
46
47
48
    QVBoxLayout* topLayout = new QVBoxLayout(this);

    QLabel* label = new QLabel(i18nc("@label:textbox",
49
                                     "Select which services should "
50
                                     "be shown in the context menu:"), this);
51
    label->setWordWrap(true);
52
53
    m_searchLineEdit = new QLineEdit(this);
    m_searchLineEdit->setPlaceholderText(i18nc("@label:textbox", "Search..."));
54
    connect(m_searchLineEdit, &QLineEdit::textChanged, this, [this](const QString &filter){
55
56
        m_sortModel->setFilterFixedString(filter);
    });
57

58
    m_listView = new QListView(this);
59
    auto *delegate = new ServiceItemDelegate(m_listView, m_listView);
Peter Penz's avatar
Peter Penz committed
60
61
62
63
    m_serviceModel = new ServiceModel(this);
    m_sortModel = new QSortFilterProxyModel(this);
    m_sortModel->setSourceModel(m_serviceModel);
    m_sortModel->setSortRole(Qt::DisplayRole);
64
    m_sortModel->setSortLocaleAware(true);
65
66
    m_sortModel->setFilterRole(Qt::DisplayRole);
    m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
Peter Penz's avatar
Peter Penz committed
67
    m_listView->setModel(m_sortModel);
68
69
    m_listView->setItemDelegate(delegate);
    m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
70
    connect(m_listView, &QListView::clicked, this, &ServicesSettingsPage::changed);
71

72
#ifndef Q_OS_WIN
73
74
75
    auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
                                                  QStringLiteral("servicemenu.knsrc"),
                                                  this);
76
    connect(downloadButton, &KNS3::Button::dialogFinished, this, &ServicesSettingsPage::loadServices);
77
#endif
78

79
    topLayout->addWidget(label);
80
    topLayout->addWidget(m_searchLineEdit);
81
    topLayout->addWidget(m_listView);
82
#ifndef Q_OS_WIN
83
    topLayout->addWidget(downloadButton);
84
#endif
85
86

    m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
Laurent Montel's avatar
Laurent Montel committed
87
    std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
88
}
89

90
ServicesSettingsPage::~ServicesSettingsPage() = default;
91

92
93
void ServicesSettingsPage::applySettings()
{
94
95
96
97
    if (!m_initialized) {
        return;
    }

98
    KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
99
    KConfigGroup showGroup = config.group("Show");
100

Peter Penz's avatar
Peter Penz committed
101
102
    QStringList enabledPlugins;

103
    const QAbstractItemModel *model = m_listView->model();
104
105
106
    for (int i = 0; i < model->rowCount(); ++i) {
        const QModelIndex index = model->index(i, 0);
        const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
Peter Penz's avatar
Peter Penz committed
107
        const bool checked = model->data(index, Qt::CheckStateRole).toBool();
108

Peter Penz's avatar
Peter Penz committed
109
110
111
112
113
        if (service.startsWith(VersionControlServicePrefix)) {
            if (checked) {
                enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
            }
        } else if (service == QLatin1String(DeleteService)) {
114
            KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
Peter Penz's avatar
Peter Penz committed
115
116
117
118
119
            KConfigGroup configGroup(globalConfig, "KDE");
            configGroup.writeEntry("ShowDeleteCommand", checked);
            configGroup.sync();
        } else if (service == QLatin1String(CopyToMoveToService)) {
            GeneralSettings::setShowCopyMoveMenu(checked);
Laurent Montel's avatar
Laurent Montel committed
120
            GeneralSettings::self()->save();
Peter Penz's avatar
Peter Penz committed
121
122
        } else {
            showGroup.writeEntry(service, checked);
123
124
125
        }
    }

Peter Penz's avatar
Peter Penz committed
126
127
    showGroup.sync();

128
129
    if (m_enabledVcsPlugins != enabledPlugins) {
        VersionControlSettings::setEnabledPlugins(enabledPlugins);
Laurent Montel's avatar
Laurent Montel committed
130
        VersionControlSettings::self()->save();
131
132
133
134
135

        KMessageBox::information(window(),
                                 i18nc("@info", "Dolphin must be restarted to apply the "
                                                "updated version control systems settings."),
                                 QString(), // default title
136
                                 QStringLiteral("ShowVcsRestartInformation"));
137
    }
138
}
139

140
141
void ServicesSettingsPage::restoreDefaults()
{
142
143
144
    QAbstractItemModel* model = m_listView->model();
    for (int i = 0; i < model->rowCount(); ++i) {
        const QModelIndex index = model->index(i, 0);
Peter Penz's avatar
Peter Penz committed
145
146
147
148
149
150
        const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();

        const bool checked = !service.startsWith(VersionControlServicePrefix)
                             && service != QLatin1String(DeleteService)
                             && service != QLatin1String(CopyToMoveToService);
        model->setData(index, checked, Qt::CheckStateRole);
151
    }
152
}
153

154
void ServicesSettingsPage::showEvent(QShowEvent* event)
155
{
156
    if (!event->spontaneous() && !m_initialized) {
157
        loadServices();
Peter Penz's avatar
Peter Penz committed
158

159
        loadVersionControlSystems();
Peter Penz's avatar
Peter Penz committed
160
161

        // Add "Show 'Delete' command" as service
162
        KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
Peter Penz's avatar
Peter Penz committed
163
        KConfigGroup configGroup(globalConfig, "KDE");
164
        addRow(QStringLiteral("edit-delete"),
Peter Penz's avatar
Peter Penz committed
165
166
167
168
169
               i18nc("@option:check", "Delete"),
               DeleteService,
               configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));

        // Add "Show 'Copy To' and 'Move To' commands" as service
170
        addRow(QStringLiteral("edit-copy"),
Peter Penz's avatar
Peter Penz committed
171
172
173
174
175
176
               i18nc("@option:check", "'Copy To' and 'Move To' commands"),
               CopyToMoveToService,
               GeneralSettings::showCopyMoveMenu());

        m_sortModel->sort(Qt::DisplayRole);

177
178
        m_initialized = true;
    }
179
    SettingsPageBase::showEvent(event);
180
181
182
183
}

void ServicesSettingsPage::loadServices()
{
184
    const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
185
    const KConfigGroup showGroup = config.group("Show");
186

187
    // Load generic services
188
    const KService::List entries = KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
189
    for (const KService::Ptr &service : entries) {
190
        const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
191
        const QList<KServiceAction> serviceActions = KDesktopFileActions::userDefinedServices(file, true);
192

193
        const KDesktopFile desktopFile(file);
194
195
        const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");

196
        for (const KServiceAction &action : serviceActions) {
197
            const QString serviceName = action.name();
198
            const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
199
200

            if (addService) {
201
202
203
                const QString itemName = subMenuName.isEmpty()
                                         ? action.text()
                                         : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
Peter Penz's avatar
Peter Penz committed
204
205
                const bool checked = showGroup.readEntry(serviceName, true);
                addRow(action.icon(), itemName, serviceName, checked);
206
207
208
            }
        }
    }
209
210

    // Load service plugins that implement the KFileItemActionPlugin interface
211
    const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
212
    for (const KService::Ptr &service : pluginServices) {
213
214
        const QString desktopEntryName = service->desktopEntryName();
        if (!isInServicesList(desktopEntryName)) {
215
            const bool checked = showGroup.readEntry(desktopEntryName, true);
Peter Penz's avatar
Peter Penz committed
216
            addRow(service->icon(), service->name(), desktopEntryName, checked);
217
218
        }
    }
219

220
    // Load JSON-based plugins that implement the KFileItemActionPlugin interface
221
    const auto jsonPlugins = KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData& metaData) {
222
        return metaData.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
223
224
    });

225
    for (const auto &jsonMetadata : jsonPlugins) {
226
227
228
229
230
231
232
        const QString desktopEntryName = jsonMetadata.pluginId();
        if (!isInServicesList(desktopEntryName)) {
            const bool checked = showGroup.readEntry(desktopEntryName, true);
            addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
        }
    }

Peter Penz's avatar
Peter Penz committed
233
    m_sortModel->sort(Qt::DisplayRole);
234
    m_searchLineEdit->setFocus(Qt::OtherFocusReason);
235
}
236

237
238
239
240
241
void ServicesSettingsPage::loadVersionControlSystems()
{
    const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();

    // Create a checkbox for each available version control plugin
242
    const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
243
244
    for (const auto &plugin : pluginServices) {
        const QString pluginName = plugin->name();
245
        addRow(QStringLiteral("code-class"),
Peter Penz's avatar
Peter Penz committed
246
247
248
               pluginName,
               VersionControlServicePrefix + pluginName,
               enabledPlugins.contains(pluginName));
249
250
    }

Peter Penz's avatar
Peter Penz committed
251
    m_sortModel->sort(Qt::DisplayRole);
252
253
}

254
bool ServicesSettingsPage::isInServicesList(const QString &service) const
255
{
Peter Penz's avatar
Peter Penz committed
256
257
258
    for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
        const QModelIndex index = m_serviceModel->index(i, 0);
        if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
259
260
261
262
263
264
            return true;
        }
    }
    return false;
}

265
266
267
void ServicesSettingsPage::addRow(const QString &icon,
                                  const QString &text,
                                  const QString &value,
Peter Penz's avatar
Peter Penz committed
268
269
270
271
272
273
274
275
276
277
278
                                  bool checked)
{
    m_serviceModel->insertRow(0);

    const QModelIndex index = m_serviceModel->index(0, 0);
    m_serviceModel->setData(index, icon, Qt::DecorationRole);
    m_serviceModel->setData(index, text, Qt::DisplayRole);
    m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
    m_serviceModel->setData(index, checked, Qt::CheckStateRole);
}