]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/settings/servicessettingspage.cpp
Start font requester with previously set custom font
[dolphin.git] / src / settings / servicessettingspage.cpp
index 09a1999d955ac4754f22cd17095b198bce39b7a0..a5e19725a51046261d33594940978214480b83dd 100644 (file)
 
 #include "servicessettingspage.h"
 
-#include "dolphinsettings.h"
-
-#include "dolphin_generalsettings.h"
-
-#include <kdialog.h>
+#include <kconfig.h>
+#include <kconfiggroup.h>
+#include <kdesktopfileactions.h>
+#include <kicon.h>
 #include <klocale.h>
-#include <kvbox.h>
+#include <knewstuff3/knewstuffbutton.h>
+#include <kservice.h>
+#include <kservicetypetrader.h>
+#include <kstandarddirs.h>
+
+#include <QEvent>
+#include <QLabel>
+#include <QListWidget>
+#include <QPushButton>
+#include <QVBoxLayout>
 
 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
-    SettingsPageBase(parent)
+    SettingsPageBase(parent),
+    m_initialized(false),
+    m_servicesList(0)
 {
-    loadSettings();
+    QVBoxLayout* topLayout = new QVBoxLayout(this);
+
+    QLabel* label = new QLabel(i18nc("@label:textbox",
+                                     "Configure which services should "
+                                     "be shown in the context menu."), this);
+    label->setWordWrap(true);
+
+    m_servicesList = new QListWidget(this);
+    m_servicesList->setSortingEnabled(true);
+    m_servicesList->setSelectionMode(QAbstractItemView::NoSelection);
+    connect(m_servicesList, SIGNAL(itemClicked(QListWidgetItem*)),
+            this, SIGNAL(changed()));
+
+    KNS3::Button* downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
+                                                    "servicemenu.knsrc",
+                                                    this);
+    connect(downloadButton, SIGNAL(dialogFinished(const Entry::List&)), this, SLOT(loadServices()));
+
+    topLayout->addWidget(label);
+    topLayout->addWidget(m_servicesList);
+    topLayout->addWidget(downloadButton);
 }
 
 ServicesSettingsPage::~ServicesSettingsPage()
@@ -39,18 +69,77 @@ ServicesSettingsPage::~ServicesSettingsPage()
 
 void ServicesSettingsPage::applySettings()
 {
+    KConfig config("kservicemenurc", KConfig::NoGlobals);
+    KConfigGroup showGroup = config.group("Show");
+
+    const int count = m_servicesList->count();
+    for (int i = 0; i < count; ++i) {
+        QListWidgetItem* item = m_servicesList->item(i);
+        const bool show = (item->checkState() == Qt::Checked);
+        const QString service = item->data(Qt::UserRole).toString();
+        showGroup.writeEntry(service, show);
+    }
+
+    showGroup.sync();
 }
 
 void ServicesSettingsPage::restoreDefaults()
 {
-    GeneralSettings* settings = DolphinSettings::instance().generalSettings();
-    settings->useDefaults(true);
-    loadSettings();
-    settings->useDefaults(false);
+    const int count = m_servicesList->count();
+    for (int i = 0; i < count; ++i) {
+        QListWidgetItem* item = m_servicesList->item(i);
+        item->setCheckState(Qt::Checked);
+    }
+}
+
+bool ServicesSettingsPage::event(QEvent* event)
+{
+    if ((event->type() == QEvent::Polish) && !m_initialized) {
+        QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection);
+        m_initialized = true;
+    }
+    return SettingsPageBase::event(event);
+}
+
+void ServicesSettingsPage::loadServices()
+{
+    const KConfig config("kservicemenurc", KConfig::NoGlobals);
+    const KConfigGroup showGroup = config.group("Show");
+
+    const KService::List entries = KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
+    foreach (const KSharedPtr<KService>& service, entries) {
+        const QString file = KStandardDirs::locate("services", service->entryPath());
+        const QList<KServiceAction> serviceActions =
+                                    KDesktopFileActions::userDefinedServices(file, true);
+
+        foreach (const KServiceAction& action, serviceActions) {
+            const QString service = action.name();
+            const bool addService = !action.noDisplay()
+                                    && !action.isSeparator()
+                                    && !isInServicesList(service);
+
+            if (addService) {
+                QListWidgetItem* item = new QListWidgetItem(KIcon(action.icon()),
+                                                            action.text(),
+                                                            m_servicesList);
+                item->setData(Qt::UserRole, service);
+                const bool show = showGroup.readEntry(service, true);
+                item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
+            }
+        }
+    }
 }
 
-void ServicesSettingsPage::loadSettings()
+bool ServicesSettingsPage::isInServicesList(const QString& service) const
 {
+    const int count = m_servicesList->count();
+    for (int i = 0; i < count; ++i) {
+        QListWidgetItem* item = m_servicesList->item(i);
+        if (item->data(Qt::UserRole).toString() == service) {
+            return true;
+        }
+    }
+    return false;
 }
 
 #include "servicessettingspage.moc"