File indexing completed on 2024-04-28 08:50:59

0001 /*
0002     SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "hostpreferenceslist.h"
0008 #include "hostpreferences.h"
0009 #include "krdc_debug.h"
0010 
0011 #include <QIcon>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 
0015 #include <QFile>
0016 #include <QLayout>
0017 #include <QListWidget>
0018 
0019 HostPreferencesList::HostPreferencesList(QWidget *parent, MainWindow *mainWindow, KConfigGroup hostPrefsConfig)
0020         : QWidget(parent)
0021         , m_hostPrefsConfig(hostPrefsConfig)
0022         , m_mainWindow(mainWindow)
0023 {
0024     hostList = new QListWidget(this);
0025     connect(hostList, SIGNAL(itemSelectionChanged()), SLOT(selectionChanged()));
0026     connect(hostList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), SLOT(configureHost()));
0027 
0028     configureButton = new QPushButton(this);
0029     configureButton->setEnabled(false);
0030     configureButton->setText(i18n("Configure..."));
0031     configureButton->setIcon(QIcon::fromTheme(QLatin1String("configure")));
0032     connect(configureButton, SIGNAL(clicked()), SLOT(configureHost()));
0033 
0034     removeButton = new QPushButton(this);
0035     removeButton->setEnabled(false);
0036     removeButton->setText(i18n("Remove"));
0037     removeButton->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
0038     connect(removeButton, SIGNAL(clicked()), SLOT(removeHost()));
0039 
0040     QVBoxLayout *buttonLayout = new QVBoxLayout;
0041     buttonLayout->addWidget(configureButton);
0042     buttonLayout->addWidget(removeButton);
0043     buttonLayout->addStretch();
0044 
0045     QHBoxLayout *mainLayout = new QHBoxLayout(this);
0046     mainLayout->addWidget(hostList);
0047     mainLayout->addLayout(buttonLayout);
0048 
0049     setLayout(mainLayout);
0050 
0051     readConfig();
0052 }
0053 
0054 HostPreferencesList::~HostPreferencesList()
0055 {
0056 }
0057 
0058 void HostPreferencesList::readConfig()
0059 {
0060     QStringList urls = m_hostPrefsConfig.groupList();
0061 
0062     for (int i = 0; i < urls.size(); ++i)
0063         hostList->addItem(new QListWidgetItem(urls.at(i)));
0064 }
0065 
0066 void HostPreferencesList::saveSettings()
0067 {
0068     m_hostPrefsConfig.sync();
0069 }
0070 
0071 void HostPreferencesList::configureHost()
0072 {
0073     const QList<QListWidgetItem *> selectedItems = hostList->selectedItems();
0074 
0075     for (QListWidgetItem *selectedItem : selectedItems) {
0076         const QString urlString = selectedItem->text();
0077         const QUrl url = QUrl(urlString);
0078 
0079         qCDebug(KRDC) << "Configure host: " << urlString;
0080 
0081         HostPreferences* prefs = nullptr;
0082 
0083         const QList<RemoteViewFactory *> remoteViewFactories(m_mainWindow->remoteViewFactoriesList());
0084         for (RemoteViewFactory *factory : remoteViewFactories) {
0085             if (factory->supportsUrl(url)) {
0086                 prefs = factory->createHostPreferences(m_hostPrefsConfig.group(urlString), this);
0087                 if (prefs) {
0088                     qCDebug(KRDC) << "Found plugin to handle url (" << urlString << "): " << prefs->metaObject()->className();
0089                 } else {
0090                     qCDebug(KRDC) << "Found plugin to handle url (" << urlString << "), but plugin does not provide preferences";
0091                 }
0092             }
0093         }
0094 
0095         if (prefs) {
0096             prefs->showDialog(this);
0097             delete prefs;
0098         } else {
0099             KMessageBox::error(this,
0100                                i18n("The selected host cannot be handled."),
0101                                i18n("Unusable URL"));
0102         }
0103     }
0104 }
0105 
0106 void HostPreferencesList::removeHost()
0107 {
0108     const QList<QListWidgetItem *> selectedItems = hostList->selectedItems();
0109 
0110     for (QListWidgetItem *selectedItem : selectedItems) {
0111         qCDebug(KRDC) << "Remove host: " <<  selectedItem->text();
0112 
0113         m_hostPrefsConfig.deleteGroup(selectedItem->text());
0114         delete(selectedItem);
0115     }
0116 
0117     saveSettings();
0118     hostList->clearSelection();
0119 }
0120 
0121 void HostPreferencesList::selectionChanged()
0122 {
0123     const bool enabled = hostList->selectedItems().isEmpty() ? false : true;
0124 
0125     configureButton->setEnabled(enabled);
0126     removeButton->setEnabled(enabled);
0127 }
0128