File indexing completed on 2024-05-05 09:12:55

0001 /*
0002     SPDX-FileCopyrightText: 2007-2010 Urs Wolfer <uwolfer@kde.org>
0003     SPDX-FileCopyrightText: 2021 RafaƂ Lalik <rafallalik@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "hostpreferences.h"
0009 #include "krdc_debug.h"
0010 #include "settings.h"
0011 
0012 #include <KLocalizedString>
0013 #include <KPageDialog>
0014 
0015 #include <QCheckBox>
0016 #include <QDialog>
0017 #include <QFile>
0018 #include <QIcon>
0019 #include <QLabel>
0020 #include <QVBoxLayout>
0021 
0022 HostPreferences::HostPreferences(KConfigGroup configGroup, QObject *parent)
0023         : QObject(parent),
0024         m_configGroup(configGroup),
0025         m_connected(false),
0026         showAgainCheckBox(nullptr),
0027         walletSupportCheckBox(nullptr)
0028 {
0029     m_hostConfigured = m_configGroup.hasKey("showConfigAgain");
0030 }
0031 
0032 HostPreferences::~HostPreferences()
0033 {
0034 }
0035 
0036 KConfigGroup HostPreferences::configGroup()
0037 {
0038     return m_configGroup;
0039 }
0040 
0041 void HostPreferences::acceptConfig()
0042 {
0043     setShowConfigAgain(showAgainCheckBox->isChecked());
0044     setWalletSupport(walletSupportCheckBox->isChecked());
0045 }
0046 
0047 bool HostPreferences::hostConfigured()
0048 {
0049     return m_hostConfigured;
0050 }
0051 
0052 void HostPreferences::setShowConfigAgain(bool show)
0053 {
0054     m_configGroup.writeEntry("showConfigAgain", show);
0055 }
0056 
0057 bool HostPreferences::showConfigAgain()
0058 {
0059     return m_configGroup.readEntry("showConfigAgain", true);
0060 }
0061 
0062 void HostPreferences::setWalletSupport(bool walletSupport)
0063 {
0064     m_configGroup.writeEntry("walletSupport", walletSupport);
0065 }
0066 
0067 bool HostPreferences::walletSupport()
0068 {
0069     return m_configGroup.readEntry("walletSupport", true);
0070 }
0071 
0072 void HostPreferences::setHeight(int height)
0073 {
0074     if (height >= 0)
0075         m_configGroup.writeEntry("height", height);
0076 }
0077 
0078 int HostPreferences::height()
0079 {
0080     return m_configGroup.readEntry("height", Settings::height());
0081 }
0082 
0083 void HostPreferences::setWidth(int width)
0084 {
0085     if (width >= 0)
0086         m_configGroup.writeEntry("width", width);
0087 }
0088 
0089 int HostPreferences::width()
0090 {
0091     return m_configGroup.readEntry("width", Settings::width());
0092 }
0093 
0094 bool HostPreferences::fullscreenScale()
0095 {
0096     return m_configGroup.readEntry("fullscreenScale", false);
0097 }
0098 
0099 void HostPreferences::setFullscreenScale(bool scale)
0100 {
0101     m_configGroup.writeEntry("fullscreenScale", scale);
0102 }
0103 
0104 bool HostPreferences::windowedScale()
0105 {
0106     return m_configGroup.readEntry("windowedScale", false);
0107 }
0108 
0109 void HostPreferences::setWindowedScale(bool scale)
0110 {
0111     m_configGroup.writeEntry("windowedScale", scale);
0112 }
0113 
0114 int HostPreferences::scaleFactor()
0115 {
0116     return m_configGroup.readEntry("scaleFactor", 0);
0117 }
0118 
0119 void HostPreferences::setScaleFactor(int factor)
0120 {
0121     m_configGroup.writeEntry("scaleFactor", factor);
0122 }
0123 
0124 bool HostPreferences::grabAllKeys()
0125 {
0126     return m_configGroup.readEntry("grabAllKeys", false);
0127 }
0128 
0129 void HostPreferences::setGrabAllKeys(bool grab)
0130 {
0131     m_configGroup.writeEntry("grabAllKeys", grab);
0132 }
0133 
0134 bool HostPreferences::showLocalCursor()
0135 {
0136     return m_configGroup.readEntry("showLocalCursor", false);
0137 }
0138 
0139 void HostPreferences::setShowLocalCursor(bool show)
0140 {
0141     m_configGroup.writeEntry("showLocalCursor", show);
0142 }
0143 
0144 bool HostPreferences::viewOnly()
0145 {
0146     return m_configGroup.readEntry("viewOnly", false);
0147 }
0148 
0149 void HostPreferences::setViewOnly(bool view)
0150 {
0151     m_configGroup.writeEntry("viewOnly", view);
0152 }
0153 
0154 bool HostPreferences::showDialogIfNeeded(QWidget *parent)
0155 {
0156     if (hostConfigured()) {
0157         if (showConfigAgain()) {
0158             qCDebug(KRDC) << "Show config dialog again";
0159             return showDialog(parent);
0160         } else
0161             return true; // no changes, no need to save
0162     } else {
0163         qCDebug(KRDC) << "No config found, create new";
0164         if (Settings::showPreferencesForNewConnections())
0165             return showDialog(parent);
0166         else
0167             return true;
0168     }
0169 }
0170 
0171 
0172 bool HostPreferences::showDialog(QWidget *parent)
0173 {
0174     // Prepare dialog
0175     KPageDialog *dialog = new KPageDialog(parent);
0176     dialog->setWindowTitle(i18n("Host Configuration"));
0177 
0178     QWidget *mainWidget = new QWidget(parent);
0179     QVBoxLayout *layout = new QVBoxLayout(mainWidget);
0180     dialog->addPage(mainWidget, i18n("Host Configuration"));
0181 
0182     if (m_connected) {
0183         const QString noteText  = i18n("Note that settings might only apply when you connect next time to this host.");
0184         const QString format = QLatin1String("<i>%1</i>");
0185         QLabel *commentLabel = new QLabel(format.arg(noteText), mainWidget);
0186         layout->addWidget(commentLabel);
0187     }
0188 
0189     QWidget* widget = createProtocolSpecificConfigPage();
0190 
0191     if (widget) {
0192         if (widget->layout())
0193             widget->layout()->setContentsMargins(0, 0, 0, 0);
0194 
0195         layout->addWidget(widget);
0196     }
0197 
0198     showAgainCheckBox = new QCheckBox(mainWidget);
0199     showAgainCheckBox->setText(i18n("Show this dialog again for this host"));
0200     showAgainCheckBox->setChecked(showConfigAgain());
0201 
0202     walletSupportCheckBox = new QCheckBox(mainWidget);
0203     walletSupportCheckBox->setText(i18n("Remember password (KWallet)"));
0204     walletSupportCheckBox->setChecked(walletSupport());
0205 
0206     layout->addWidget(showAgainCheckBox);
0207     layout->addWidget(walletSupportCheckBox);
0208     layout->addStretch(1);
0209 
0210     // Show dialog
0211     if (dialog->exec() == QDialog::Accepted) {
0212         qCDebug(KRDC) << "HostPreferences config dialog accepted";
0213         acceptConfig();
0214         return true;
0215     } else {
0216         return false;
0217     }
0218 }
0219 
0220 void HostPreferences::setShownWhileConnected(bool connected)
0221 {
0222     m_connected = connected;
0223 }
0224