File: hostpreferenceslist.cpp

package info (click to toggle)
krdc 4%3A22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,240 kB
  • sloc: cpp: 6,392; xml: 135; makefile: 8; sh: 5
file content (128 lines) | stat: -rw-r--r-- 3,934 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
    SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "hostpreferenceslist.h"
#include "hostpreferences.h"
#include "krdc_debug.h"

#include <QIcon>
#include <KLocalizedString>
#include <KMessageBox>

#include <QFile>
#include <QLayout>
#include <QListWidget>

HostPreferencesList::HostPreferencesList(QWidget *parent, MainWindow *mainWindow, KConfigGroup hostPrefsConfig)
        : QWidget(parent)
        , m_hostPrefsConfig(hostPrefsConfig)
        , m_mainWindow(mainWindow)
{
    hostList = new QListWidget(this);
    connect(hostList, SIGNAL(itemSelectionChanged()), SLOT(selectionChanged()));
    connect(hostList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), SLOT(configureHost()));

    configureButton = new QPushButton(this);
    configureButton->setEnabled(false);
    configureButton->setText(i18n("Configure..."));
    configureButton->setIcon(QIcon::fromTheme(QLatin1String("configure")));
    connect(configureButton, SIGNAL(clicked()), SLOT(configureHost()));

    removeButton = new QPushButton(this);
    removeButton->setEnabled(false);
    removeButton->setText(i18n("Remove"));
    removeButton->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
    connect(removeButton, SIGNAL(clicked()), SLOT(removeHost()));

    QVBoxLayout *buttonLayout = new QVBoxLayout;
    buttonLayout->addWidget(configureButton);
    buttonLayout->addWidget(removeButton);
    buttonLayout->addStretch();

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(hostList);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);

    readConfig();
}

HostPreferencesList::~HostPreferencesList()
{
}

void HostPreferencesList::readConfig()
{
    QStringList urls = m_hostPrefsConfig.groupList();

    for (int i = 0; i < urls.size(); ++i)
        hostList->addItem(new QListWidgetItem(urls.at(i)));
}

void HostPreferencesList::saveSettings()
{
    m_hostPrefsConfig.sync();
}

void HostPreferencesList::configureHost()
{
    const QList<QListWidgetItem *> selectedItems = hostList->selectedItems();

    for (QListWidgetItem *selectedItem : selectedItems) {
        const QString urlString = selectedItem->text();
        const QUrl url = QUrl(urlString);

        qCDebug(KRDC) << "Configure host: " << urlString;

        HostPreferences* prefs = nullptr;

        const QList<RemoteViewFactory *> remoteViewFactories(m_mainWindow->remoteViewFactoriesList());
        for (RemoteViewFactory *factory : remoteViewFactories) {
            if (factory->supportsUrl(url)) {
                prefs = factory->createHostPreferences(m_hostPrefsConfig.group(urlString), this);
                if (prefs) {
                    qCDebug(KRDC) << "Found plugin to handle url (" << urlString << "): " << prefs->metaObject()->className();
                } else {
                    qCDebug(KRDC) << "Found plugin to handle url (" << urlString << "), but plugin does not provide preferences";
                }
            }
        }

        if (prefs) {
            prefs->showDialog(this);
            delete prefs;
        } else {
            KMessageBox::error(this,
                               i18n("The selected host cannot be handled."),
                               i18n("Unusable URL"));
        }
    }
}

void HostPreferencesList::removeHost()
{
    const QList<QListWidgetItem *> selectedItems = hostList->selectedItems();

    for (QListWidgetItem *selectedItem : selectedItems) {
        qCDebug(KRDC) << "Remove host: " <<  selectedItem->text();

        m_hostPrefsConfig.deleteGroup(selectedItem->text());
        delete(selectedItem);
    }

    saveSettings();
    hostList->clearSelection();
}

void HostPreferencesList::selectionChanged()
{
    const bool enabled = hostList->selectedItems().isEmpty() ? false : true;

    configureButton->setEnabled(enabled);
    removeButton->setEnabled(enabled);
}