File indexing completed on 2024-04-21 04:58:36

0001 /*
0002     SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "vnchostpreferences.h"
0008 
0009 #include "settings.h"
0010 
0011 #include <QGuiApplication>
0012 #include <QScreen>
0013 #include <QWindow>
0014 
0015 static const char quality_config_key[] = "quality";
0016 static const char use_ssh_tunnel_config_key[] = "use_ssh_tunnel";
0017 static const char use_ssh_tunnel_loopback_config_key[] = "use_ssh_tunnel_loopback";
0018 static const char ssh_tunnel_port_config_key[] = "ssh_tunnel_port";
0019 static const char ssh_tunnel_user_name_config_key[] = "ssh_tunnel_user_name";
0020 static const char dont_copy_passwords_config_key[] = "dont_copy_passwords";
0021 
0022 VncHostPreferences::VncHostPreferences(KConfigGroup configGroup, QObject *parent)
0023     : HostPreferences(configGroup, parent)
0024 {
0025 }
0026 
0027 VncHostPreferences::~VncHostPreferences()
0028 {
0029 }
0030 
0031 QWidget *VncHostPreferences::createProtocolSpecificConfigPage()
0032 {
0033     QWidget *vncPage = new QWidget();
0034     vncUi.setupUi(vncPage);
0035 
0036     vncUi.kcfg_Quality->setCurrentIndex(quality() - 1);
0037     vncUi.kcfg_Scaling->setChecked(windowedScale());
0038     vncUi.kcfg_ScalingWidth->setValue(width());
0039     vncUi.kcfg_ScalingHeight->setValue(height());
0040 
0041     connect(vncUi.resolutionComboBox, SIGNAL(currentIndexChanged(int)), SLOT(updateScalingWidthHeight(int)));
0042     connect(vncUi.kcfg_Scaling, SIGNAL(toggled(bool)), SLOT(updateScaling(bool)));
0043 
0044     const QString resolutionString = QString::number(width()) + QLatin1Char('x') + QString::number(height());
0045     const int resolutionIndex = vncUi.resolutionComboBox->findText(resolutionString, Qt::MatchContains);
0046     vncUi.resolutionComboBox->setCurrentIndex((resolutionIndex == -1) ? vncUi.resolutionComboBox->count() - 1 : resolutionIndex);
0047 
0048     updateScaling(windowedScale());
0049 
0050 #ifdef LIBSSH_FOUND
0051     connect(vncUi.use_ssh_tunnel, &QCheckBox::toggled, vncUi.ssh_groupBox, &QWidget::setVisible);
0052 
0053     vncUi.ssh_groupBox->setVisible(useSshTunnel());
0054     vncUi.use_ssh_tunnel->setChecked(useSshTunnel());
0055     vncUi.use_loopback->setChecked(useSshTunnelLoopback());
0056     vncUi.ssh_tunnel_port->setValue(sshTunnelPort());
0057     vncUi.ssh_tunnel_user_name->setText(sshTunnelUserName());
0058 #else
0059     vncUi.ssh_groupBox->hide();
0060     vncUi.use_ssh_tunnel->hide();
0061 #endif
0062 
0063     vncUi.dont_copy_passwords->setChecked(dontCopyPasswords());
0064 
0065     return vncPage;
0066 }
0067 
0068 void VncHostPreferences::updateScalingWidthHeight(int index)
0069 {
0070     switch (index) {
0071     case 0:
0072         vncUi.kcfg_ScalingHeight->setValue(480);
0073         vncUi.kcfg_ScalingWidth->setValue(640);
0074         break;
0075     case 1:
0076         vncUi.kcfg_ScalingHeight->setValue(600);
0077         vncUi.kcfg_ScalingWidth->setValue(800);
0078         break;
0079     case 2:
0080         vncUi.kcfg_ScalingHeight->setValue(768);
0081         vncUi.kcfg_ScalingWidth->setValue(1024);
0082         break;
0083     case 3:
0084         vncUi.kcfg_ScalingHeight->setValue(1024);
0085         vncUi.kcfg_ScalingWidth->setValue(1280);
0086         break;
0087     case 4:
0088         vncUi.kcfg_ScalingHeight->setValue(1200);
0089         vncUi.kcfg_ScalingWidth->setValue(1600);
0090         break;
0091     case 5: {
0092         QWindow *window = vncUi.kcfg_ScalingWidth->window()->windowHandle();
0093         QScreen *screen = window ? window->screen() : qGuiApp->primaryScreen();
0094         const QSize size = screen->size() * screen->devicePixelRatio();
0095 
0096         vncUi.kcfg_ScalingWidth->setValue(size.width());
0097         vncUi.kcfg_ScalingHeight->setValue(size.height());
0098         break;
0099     }
0100     case 6:
0101     default:
0102         break;
0103     }
0104 
0105     checkEnableCustomSize(index);
0106 }
0107 
0108 void VncHostPreferences::updateScaling(bool enabled)
0109 {
0110     vncUi.resolutionComboBox->setEnabled(enabled);
0111     if (enabled) {
0112         checkEnableCustomSize(vncUi.resolutionComboBox->currentIndex());
0113     } else {
0114         checkEnableCustomSize(-1);
0115     }
0116 }
0117 
0118 void VncHostPreferences::checkEnableCustomSize(int index)
0119 {
0120     const bool enabled = (index == 6);
0121 
0122     vncUi.kcfg_ScalingHeight->setEnabled(enabled);
0123     vncUi.kcfg_ScalingWidth->setEnabled(enabled);
0124     vncUi.heightLabel->setEnabled(enabled);
0125     vncUi.widthLabel->setEnabled(enabled);
0126 }
0127 
0128 void VncHostPreferences::acceptConfig()
0129 {
0130     HostPreferences::acceptConfig();
0131     setQuality((RemoteView::Quality)(vncUi.kcfg_Quality->currentIndex() + 1));
0132     setWindowedScale(vncUi.kcfg_Scaling->isChecked());
0133     if (vncUi.kcfg_Scaling->isChecked()) {
0134         setHeight(vncUi.kcfg_ScalingHeight->value());
0135         setWidth(vncUi.kcfg_ScalingWidth->value());
0136     }
0137 
0138     setUseSshTunnel(vncUi.use_ssh_tunnel->isChecked());
0139     setUseSshTunnelLoopback(vncUi.use_loopback->isChecked());
0140     setSshTunnelPort(vncUi.ssh_tunnel_port->value());
0141     setSshTunnelUserName(vncUi.ssh_tunnel_user_name->text());
0142     setDontCopyPasswords(vncUi.dont_copy_passwords->isChecked());
0143 }
0144 
0145 void VncHostPreferences::setQuality(RemoteView::Quality quality)
0146 {
0147     if (quality >= 0 && quality <= 3)
0148         m_configGroup.writeEntry(quality_config_key, (int)quality);
0149 }
0150 
0151 RemoteView::Quality VncHostPreferences::quality()
0152 {
0153     return (RemoteView::Quality)m_configGroup.readEntry(quality_config_key, (int)Settings::quality() + 1);
0154 }
0155 
0156 bool VncHostPreferences::useSshTunnel() const
0157 {
0158     return m_configGroup.readEntry(use_ssh_tunnel_config_key, false);
0159 }
0160 
0161 void VncHostPreferences::setUseSshTunnel(bool useSshTunnel)
0162 {
0163     m_configGroup.writeEntry(use_ssh_tunnel_config_key, useSshTunnel);
0164 }
0165 
0166 bool VncHostPreferences::useSshTunnelLoopback() const
0167 {
0168     return m_configGroup.readEntry(use_ssh_tunnel_loopback_config_key, false);
0169 }
0170 
0171 void VncHostPreferences::setUseSshTunnelLoopback(bool useSshTunnelLoopback)
0172 {
0173     m_configGroup.writeEntry(use_ssh_tunnel_loopback_config_key, useSshTunnelLoopback);
0174 }
0175 
0176 int VncHostPreferences::sshTunnelPort() const
0177 {
0178     return m_configGroup.readEntry(ssh_tunnel_port_config_key, 22);
0179 }
0180 
0181 void VncHostPreferences::setSshTunnelPort(int port)
0182 {
0183     m_configGroup.writeEntry(ssh_tunnel_port_config_key, port);
0184 }
0185 
0186 QString VncHostPreferences::sshTunnelUserName() const
0187 {
0188     return m_configGroup.readEntry(ssh_tunnel_user_name_config_key, QString());
0189 }
0190 
0191 void VncHostPreferences::setSshTunnelUserName(const QString &userName)
0192 {
0193     m_configGroup.writeEntry(ssh_tunnel_user_name_config_key, userName);
0194 }
0195 
0196 bool VncHostPreferences::dontCopyPasswords() const
0197 {
0198     return m_configGroup.readEntry(dont_copy_passwords_config_key, false);
0199 }
0200 
0201 void VncHostPreferences::setDontCopyPasswords(bool dontCopyPasswords)
0202 {
0203     m_configGroup.writeEntry(dont_copy_passwords_config_key, dontCopyPasswords);
0204 }