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

0001 /*
0002     SPDX-FileCopyrightText: 2002-2003 Tim Jansen <tim@tjansen.de>
0003     SPDX-FileCopyrightText: 2007-2008 Urs Wolfer <uwolfer@kde.org>
0004     SPDX-FileCopyrightText: 2021 RafaƂ Lalik <rafallalik@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "remoteview.h"
0010 #include "krdc_debug.h"
0011 
0012 #include <QBitmap>
0013 #include <QStandardPaths>
0014 
0015 RemoteView::RemoteView(QWidget *parent)
0016     : QWidget(parent)
0017     , m_status(Disconnected)
0018     , m_host(QString())
0019     , m_port(0)
0020     , m_viewOnly(false)
0021     , m_grabAllKeys(false)
0022     , m_scale(false)
0023     , m_keyboardIsGrabbed(false)
0024     , m_factor(0.)
0025 #ifndef QTONLY
0026     , m_wallet(nullptr)
0027 #endif
0028     , m_localCursorState(CursorOff)
0029 {
0030     resize(0, 0);
0031 }
0032 
0033 RemoteView::~RemoteView()
0034 {
0035 #ifndef QTONLY
0036     delete m_wallet;
0037 #endif
0038 }
0039 
0040 RemoteView::RemoteStatus RemoteView::status()
0041 {
0042     return m_status;
0043 }
0044 
0045 void RemoteView::setStatus(RemoteView::RemoteStatus s)
0046 {
0047     if (m_status == s)
0048         return;
0049 
0050     if (((1 + m_status) != s) && (s != Disconnected)) {
0051         // follow state transition rules
0052 
0053         if (s == Disconnecting) {
0054             if (m_status == Disconnected)
0055                 return;
0056         } else {
0057             Q_ASSERT(((int)s) >= 0);
0058             if (m_status > s) {
0059                 m_status = Disconnected;
0060                 Q_EMIT statusChanged(Disconnected);
0061             }
0062             // smooth state transition
0063             RemoteStatus origState = m_status;
0064             for (int i = origState; i < s; ++i) {
0065                 m_status = (RemoteStatus)i;
0066                 Q_EMIT statusChanged((RemoteStatus)i);
0067             }
0068         }
0069     }
0070     m_status = s;
0071     Q_EMIT statusChanged(m_status);
0072 }
0073 
0074 bool RemoteView::supportsScaling() const
0075 {
0076     return false;
0077 }
0078 
0079 bool RemoteView::supportsLocalCursor() const
0080 {
0081     return false;
0082 }
0083 
0084 bool RemoteView::supportsViewOnly() const
0085 {
0086     return false;
0087 }
0088 
0089 QString RemoteView::host()
0090 {
0091     return m_host;
0092 }
0093 
0094 QSize RemoteView::framebufferSize()
0095 {
0096     return QSize(0, 0);
0097 }
0098 
0099 void RemoteView::startQuitting()
0100 {
0101 }
0102 
0103 bool RemoteView::isQuitting()
0104 {
0105     return false;
0106 }
0107 
0108 int RemoteView::port()
0109 {
0110     return m_port;
0111 }
0112 
0113 void RemoteView::updateConfiguration()
0114 {
0115 }
0116 
0117 void RemoteView::keyEvent(QKeyEvent *)
0118 {
0119 }
0120 
0121 bool RemoteView::viewOnly()
0122 {
0123     return m_viewOnly;
0124 }
0125 
0126 void RemoteView::setViewOnly(bool viewOnly)
0127 {
0128     m_viewOnly = viewOnly;
0129 }
0130 
0131 bool RemoteView::grabAllKeys()
0132 {
0133     return m_grabAllKeys;
0134 }
0135 
0136 void RemoteView::setGrabAllKeys(bool grabAllKeys)
0137 {
0138     m_grabAllKeys = grabAllKeys;
0139 
0140     if (grabAllKeys) {
0141         m_keyboardIsGrabbed = true;
0142         grabKeyboard();
0143     } else if (m_keyboardIsGrabbed) {
0144         releaseKeyboard();
0145     }
0146 }
0147 
0148 QPixmap RemoteView::takeScreenshot()
0149 {
0150     return grab();
0151 }
0152 
0153 void RemoteView::showLocalCursor(LocalCursorState state)
0154 {
0155     m_localCursorState = state;
0156 }
0157 
0158 RemoteView::LocalCursorState RemoteView::localCursorState() const
0159 {
0160     return m_localCursorState;
0161 }
0162 
0163 bool RemoteView::scaling() const
0164 {
0165     return m_scale;
0166 }
0167 
0168 void RemoteView::enableScaling(bool scale)
0169 {
0170     m_scale = scale;
0171 }
0172 
0173 void RemoteView::setScaleFactor(float factor)
0174 {
0175     m_factor = factor;
0176 }
0177 
0178 void RemoteView::switchFullscreen(bool)
0179 {
0180 }
0181 
0182 void RemoteView::scaleResize(int, int)
0183 {
0184 }
0185 
0186 QUrl RemoteView::url()
0187 {
0188     return m_url;
0189 }
0190 
0191 #ifndef QTONLY
0192 QString RemoteView::readWalletPassword(bool fromUserNameOnly)
0193 {
0194     return readWalletPasswordForKey(fromUserNameOnly ? m_url.userName() : m_url.toDisplayString(QUrl::StripTrailingSlash));
0195 }
0196 
0197 void RemoteView::saveWalletPassword(const QString &password, bool fromUserNameOnly)
0198 {
0199     saveWalletPasswordForKey(fromUserNameOnly ? m_url.userName() : m_url.toDisplayString(QUrl::StripTrailingSlash), password);
0200 }
0201 
0202 QString RemoteView::readWalletPasswordForKey(const QString &key)
0203 {
0204     const QString KRDCFOLDER = QLatin1String("KRDC");
0205 
0206     window()->setDisabled(true); // WORKAROUND: disable inputs so users cannot close the current tab (see #181230)
0207     m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), window()->winId(), KWallet::Wallet::OpenType::Synchronous);
0208     window()->setDisabled(false);
0209 
0210     if (m_wallet) {
0211         bool walletOK = m_wallet->hasFolder(KRDCFOLDER);
0212         if (!walletOK) {
0213             walletOK = m_wallet->createFolder(KRDCFOLDER);
0214             qCDebug(KRDC) << "Wallet folder created";
0215         }
0216         if (walletOK) {
0217             qCDebug(KRDC) << "Wallet OK";
0218             m_wallet->setFolder(KRDCFOLDER);
0219             QString password;
0220 
0221             if (m_wallet->hasEntry(key) && !m_wallet->readPassword(key, password)) {
0222                 qCDebug(KRDC) << "Password read OK";
0223 
0224                 return password;
0225             }
0226         }
0227     }
0228     return QString();
0229 }
0230 
0231 void RemoteView::saveWalletPasswordForKey(const QString &key, const QString &password)
0232 {
0233     if (m_wallet && m_wallet->isOpen()) {
0234         qCDebug(KRDC) << "Write wallet password";
0235         m_wallet->writePassword(key, password);
0236     }
0237 }
0238 #endif
0239 
0240 QCursor RemoteView::localDefaultCursor() const
0241 {
0242     return QCursor(Qt::ArrowCursor);
0243 }
0244 
0245 void RemoteView::focusInEvent(QFocusEvent *event)
0246 {
0247     if (m_grabAllKeys) {
0248         m_keyboardIsGrabbed = true;
0249         grabKeyboard();
0250     }
0251 
0252     QWidget::focusInEvent(event);
0253 }
0254 
0255 void RemoteView::focusOutEvent(QFocusEvent *event)
0256 {
0257     if (m_grabAllKeys || m_keyboardIsGrabbed) {
0258         m_keyboardIsGrabbed = false;
0259         releaseKeyboard();
0260     }
0261 
0262     QWidget::focusOutEvent(event);
0263 }
0264 
0265 #include "moc_remoteview.cpp"