]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/servicessettingspage.cpp
use KNewStuff3 instead of the deprecated KNewStuff2
[dolphin.git] / src / settings / servicessettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "servicessettingspage.h"
21
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kdesktopfileactions.h>
25 #include <kicon.h>
26 #include <klocale.h>
27 #include <knewstuff3/knewstuffbutton.h>
28 #include <kservice.h>
29 #include <kservicetypetrader.h>
30 #include <kstandarddirs.h>
31
32 #include <QEvent>
33 #include <QLabel>
34 #include <QListWidget>
35 #include <QPushButton>
36 #include <QVBoxLayout>
37
38 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
39 SettingsPageBase(parent),
40 m_initialized(false),
41 m_servicesList(0)
42 {
43 QVBoxLayout* topLayout = new QVBoxLayout(this);
44
45 QLabel* label = new QLabel(i18nc("@label:textbox",
46 "Configure which services should "
47 "be shown in the context menu."), this);
48 label->setWordWrap(true);
49
50 m_servicesList = new QListWidget(this);
51 m_servicesList->setSortingEnabled(true);
52 m_servicesList->setSelectionMode(QAbstractItemView::NoSelection);
53 connect(m_servicesList, SIGNAL(itemClicked(QListWidgetItem*)),
54 this, SIGNAL(changed()));
55
56 KNS3::Button* downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
57 "servicemenu.knsrc",
58 this);
59 connect(downloadButton, SIGNAL(dialogFinished(const Entry::List&)), this, SLOT(loadServices()));
60
61 topLayout->addWidget(label);
62 topLayout->addWidget(m_servicesList);
63 topLayout->addWidget(downloadButton);
64 }
65
66 ServicesSettingsPage::~ServicesSettingsPage()
67 {
68 }
69
70 void ServicesSettingsPage::applySettings()
71 {
72 KConfig config("kservicemenurc", KConfig::NoGlobals);
73 KConfigGroup showGroup = config.group("Show");
74
75 const int count = m_servicesList->count();
76 for (int i = 0; i < count; ++i) {
77 QListWidgetItem* item = m_servicesList->item(i);
78 const bool show = (item->checkState() == Qt::Checked);
79 const QString service = item->data(Qt::UserRole).toString();
80 showGroup.writeEntry(service, show);
81 }
82
83 showGroup.sync();
84 }
85
86 void ServicesSettingsPage::restoreDefaults()
87 {
88 const int count = m_servicesList->count();
89 for (int i = 0; i < count; ++i) {
90 QListWidgetItem* item = m_servicesList->item(i);
91 item->setCheckState(Qt::Checked);
92 }
93 }
94
95 bool ServicesSettingsPage::event(QEvent* event)
96 {
97 if ((event->type() == QEvent::Polish) && !m_initialized) {
98 QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection);
99 m_initialized = true;
100 }
101 return SettingsPageBase::event(event);
102 }
103
104 void ServicesSettingsPage::loadServices()
105 {
106 const KConfig config("kservicemenurc", KConfig::NoGlobals);
107 const KConfigGroup showGroup = config.group("Show");
108
109 const KService::List entries = KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
110 foreach (const KSharedPtr<KService>& service, entries) {
111 const QString file = KStandardDirs::locate("services", service->entryPath());
112 const QList<KServiceAction> serviceActions =
113 KDesktopFileActions::userDefinedServices(file, true);
114
115 foreach (const KServiceAction& action, serviceActions) {
116 const QString service = action.name();
117 const bool addService = !action.noDisplay()
118 && !action.isSeparator()
119 && !isInServicesList(service);
120
121 if (addService) {
122 QListWidgetItem* item = new QListWidgetItem(KIcon(action.icon()),
123 action.text(),
124 m_servicesList);
125 item->setData(Qt::UserRole, service);
126 const bool show = showGroup.readEntry(service, true);
127 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
128 }
129 }
130 }
131 }
132
133 bool ServicesSettingsPage::isInServicesList(const QString& service) const
134 {
135 const int count = m_servicesList->count();
136 for (int i = 0; i < count; ++i) {
137 QListWidgetItem* item = m_servicesList->item(i);
138 if (item->data(Qt::UserRole).toString() == service) {
139 return true;
140 }
141 }
142 return false;
143 }
144
145 #include "servicessettingspage.moc"