File indexing completed on 2024-04-21 04:58:42
0001 /* 0002 Copyright (C) 2010 Collabora Ltd <info@collabora.co.uk> 0003 @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk> 0004 Copyright (C) 2001-2002 Tim Jansen <tim@tjansen.de> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU Lesser General Public License 0017 along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 #include "trayicon.h" 0020 0021 #include "mainwindow.h" 0022 #include "rfbservermanager.h" 0023 #include "rfbclient.h" 0024 0025 #include <QIcon> 0026 #include <QMenu> 0027 0028 #include <KAboutApplicationDialog> 0029 #include <KActionCollection> 0030 #include <QDialog> 0031 #include <KHelpMenu> 0032 #include <KLocalizedString> 0033 #include <KStandardAction> 0034 #include <KToggleAction> 0035 #include <KConfigGroup> 0036 0037 class ClientActions 0038 { 0039 public: 0040 ClientActions(RfbClient *client, QMenu *menu, QAction *before); 0041 virtual ~ClientActions(); 0042 0043 private: 0044 QMenu *m_menu = nullptr; 0045 QAction *m_title = nullptr; 0046 QAction *m_disconnectAction = nullptr; 0047 QAction *m_enableControlAction = nullptr; 0048 QAction *m_separator = nullptr; 0049 }; 0050 0051 ClientActions::ClientActions(RfbClient* client, QMenu* menu, QAction* before) 0052 : m_menu(menu) 0053 { 0054 m_title = m_menu->insertSection(before, client->name()); 0055 0056 m_disconnectAction = new QAction(i18n("Disconnect"), m_menu); 0057 m_menu->insertAction(before, m_disconnectAction); 0058 0059 QObject::connect(m_disconnectAction, &QAction::triggered, client, &RfbClient::closeConnection); 0060 0061 if (client->controlCanBeEnabled()) { 0062 m_enableControlAction = new KToggleAction(i18n("Enable Remote Control"), m_menu); 0063 m_enableControlAction->setChecked(client->controlEnabled()); 0064 m_menu->insertAction(before, m_enableControlAction); 0065 0066 QObject::connect(m_enableControlAction, &KToggleAction::triggered, 0067 client, &RfbClient::setControlEnabled); 0068 QObject::connect(client, &RfbClient::controlEnabledChanged, 0069 m_enableControlAction, &KToggleAction::setChecked); 0070 } else { 0071 m_enableControlAction = nullptr; 0072 } 0073 0074 m_separator = m_menu->insertSeparator(before); 0075 } 0076 0077 ClientActions::~ClientActions() 0078 { 0079 m_menu->removeAction(m_title); 0080 delete m_title; 0081 0082 m_menu->removeAction(m_disconnectAction); 0083 delete m_disconnectAction; 0084 0085 if (m_enableControlAction) { 0086 m_menu->removeAction(m_enableControlAction); 0087 delete m_enableControlAction; 0088 } 0089 0090 m_menu->removeAction(m_separator); 0091 delete m_separator; 0092 } 0093 0094 //********** 0095 0096 TrayIcon::TrayIcon(QWidget *mainWindow) 0097 : KStatusNotifierItem(mainWindow) 0098 { 0099 setIconByPixmap(QIcon::fromTheme(QStringLiteral("krfb")).pixmap(22, 22, QIcon::Disabled)); 0100 0101 setToolTipTitle(i18n("Desktop Sharing - disconnected")); 0102 setCategory(KStatusNotifierItem::ApplicationStatus); 0103 0104 connect(RfbServerManager::instance(), &RfbServerManager::clientConnected, 0105 this, &TrayIcon::onClientConnected); 0106 connect(RfbServerManager::instance(), &RfbServerManager::clientDisconnected, 0107 this, &TrayIcon::onClientDisconnected); 0108 0109 m_aboutAction = KStandardAction::aboutApp(this, SLOT(showAbout()), this); 0110 contextMenu()->addAction(m_aboutAction); 0111 } 0112 0113 void TrayIcon::onClientConnected(RfbClient* client) 0114 { 0115 if (m_clientActions.isEmpty()) { //first client connected 0116 setIconByName(QStringLiteral("krfb")); 0117 setToolTipTitle(i18n("Desktop Sharing - connected with %1", client->name())); 0118 setStatus(KStatusNotifierItem::Active); 0119 } else { //Nth client connected, N != 1 0120 setToolTipTitle(i18n("Desktop Sharing - connected")); 0121 } 0122 0123 m_clientActions[client] = new ClientActions(client, contextMenu(), m_aboutAction); 0124 } 0125 0126 void TrayIcon::onClientDisconnected(RfbClient* client) 0127 { 0128 ClientActions *actions = m_clientActions.take(client); 0129 delete actions; 0130 0131 if (m_clientActions.isEmpty()) { 0132 setIconByPixmap(QIcon::fromTheme(QStringLiteral("krfb")).pixmap(22, 22, QIcon::Disabled)); 0133 setToolTipTitle(i18n("Desktop Sharing - disconnected")); 0134 setStatus(KStatusNotifierItem::Passive); 0135 } else if (m_clientActions.size() == 1) { //clients number dropped back to 1 0136 RfbClient *client = m_clientActions.constBegin().key(); 0137 setToolTipTitle(i18n("Desktop Sharing - connected with %1", client->name())); 0138 } 0139 } 0140 0141 void TrayIcon::showAbout() 0142 { 0143 KHelpMenu menu; 0144 menu.aboutApplication(); 0145 }