File indexing completed on 2024-07-14 04:51:41

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 <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <QIcon>
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, i18n("The selected host cannot be handled."), i18n("Unusable URL"));
0100         }
0101     }
0102 }
0103 
0104 void HostPreferencesList::removeHost()
0105 {
0106     const QList<QListWidgetItem *> selectedItems = hostList->selectedItems();
0107 
0108     for (QListWidgetItem *selectedItem : selectedItems) {
0109         qCDebug(KRDC) << "Remove host: " << selectedItem->text();
0110 
0111         m_hostPrefsConfig.deleteGroup(selectedItem->text());
0112         delete (selectedItem);
0113     }
0114 
0115     saveSettings();
0116     hostList->clearSelection();
0117 }
0118 
0119 void HostPreferencesList::selectionChanged()
0120 {
0121     const bool enabled = hostList->selectedItems().isEmpty() ? false : true;
0122 
0123     configureButton->setEnabled(enabled);
0124     removeButton->setEnabled(enabled);
0125 }