Merge pull request #18104 from sledgehammer999/remove_dead_code
[qBittorrent.git] / src / gui / desktopintegration.cpp
blobc24c807ca1d217794d6027d6d59fc23a8a1476e2
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "desktopintegration.h"
32 #include <chrono>
34 #include <QMenu>
35 #include <QTimer>
37 #ifndef Q_OS_MACOS
38 #include <QSystemTrayIcon>
39 #endif
41 #include "base/logger.h"
42 #include "base/preferences.h"
43 #include "uithememanager.h"
45 #ifdef Q_OS_MACOS
46 #include "macutilities.h"
47 #endif
49 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
50 #include "notifications/dbusnotifier.h"
51 #endif
53 namespace
55 #ifdef Q_OS_MACOS
56 DesktopIntegration *desktopIntegrationInstance = nullptr;
58 bool handleDockClicked(id self, SEL cmd, ...)
60 Q_UNUSED(self);
61 Q_UNUSED(cmd);
63 Q_ASSERT(desktopIntegrationInstance);
64 emit desktopIntegrationInstance->activationRequested();
66 return true;
68 #endif
71 using namespace std::chrono_literals;
73 #define SETTINGS_KEY(name) u"GUI/" name
74 #define NOTIFICATIONS_SETTINGS_KEY(name) (SETTINGS_KEY(u"Notifications/"_qs) name)
76 DesktopIntegration::DesktopIntegration(QObject *parent)
77 : QObject(parent)
78 , m_storeNotificationEnabled {NOTIFICATIONS_SETTINGS_KEY(u"Enabled"_qs), true}
79 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
80 , m_storeNotificationTimeOut {NOTIFICATIONS_SETTINGS_KEY(u"Timeout"_qs), -1}
81 #endif
83 #ifdef Q_OS_MACOS
84 desktopIntegrationInstance = this;
85 MacUtils::overrideDockClickHandler(handleDockClicked);
86 #else
87 if (Preferences::instance()->systemTrayEnabled())
88 createTrayIcon();
90 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
91 if (isNotificationsEnabled())
93 m_notifier = new DBusNotifier(this);
94 connect(m_notifier, &DBusNotifier::messageClicked, this, &DesktopIntegration::notificationClicked);
96 #endif
97 #endif
99 connect(Preferences::instance(), &Preferences::changed, this, &DesktopIntegration::onPreferencesChanged);
102 bool DesktopIntegration::isActive() const
104 #ifdef Q_OS_MACOS
105 return true;
106 #else
107 return QSystemTrayIcon::isSystemTrayAvailable();
108 #endif
111 QString DesktopIntegration::toolTip() const
113 return m_toolTip;
116 void DesktopIntegration::setToolTip(const QString &toolTip)
118 if (m_toolTip == toolTip)
119 return;
121 m_toolTip = toolTip;
122 #ifndef Q_OS_MACOS
123 if (m_systrayIcon)
124 m_systrayIcon->setToolTip(m_toolTip);
125 #endif
128 QMenu *DesktopIntegration::menu() const
130 return m_menu;
133 void DesktopIntegration::setMenu(QMenu *menu)
135 if (menu == m_menu)
136 return;
138 m_menu = menu;
140 #ifdef Q_OS_MACOS
141 if (m_menu)
142 m_menu->setAsDockMenu();
143 #else
144 if (m_systrayIcon)
145 m_systrayIcon->setContextMenu(m_menu);
146 #endif
149 bool DesktopIntegration::isNotificationsEnabled() const
151 return m_storeNotificationEnabled;
154 void DesktopIntegration::setNotificationsEnabled(const bool value)
156 if (m_storeNotificationEnabled == value)
157 return;
159 m_storeNotificationEnabled = value;
161 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
162 if (value)
164 m_notifier = new DBusNotifier(this);
165 connect(m_notifier, &DBusNotifier::messageClicked, this, &DesktopIntegration::notificationClicked);
167 else
169 delete m_notifier;
170 m_notifier = nullptr;
172 #endif
175 int DesktopIntegration::notificationTimeout() const
177 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
178 return m_storeNotificationTimeOut;
179 #else
180 return 5000;
181 #endif
184 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
185 void DesktopIntegration::setNotificationTimeout(const int value)
187 m_storeNotificationTimeOut = value;
189 #endif
191 void DesktopIntegration::showNotification(const QString &title, const QString &msg) const
193 if (!isNotificationsEnabled())
194 return;
196 #ifdef Q_OS_MACOS
197 MacUtils::displayNotification(title, msg);
198 #else
199 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
200 m_notifier->showMessage(title, msg, notificationTimeout());
201 #else
202 if (m_systrayIcon && QSystemTrayIcon::supportsMessages())
203 m_systrayIcon->showMessage(title, msg, QSystemTrayIcon::Information, notificationTimeout());
204 #endif
205 #endif
208 void DesktopIntegration::onPreferencesChanged()
210 #ifndef Q_OS_MACOS
211 if (Preferences::instance()->systemTrayEnabled())
213 if (m_systrayIcon)
215 // Reload systray icon
216 m_systrayIcon->setIcon(UIThemeManager::instance()->getSystrayIcon());
218 else
220 createTrayIcon();
223 else
225 delete m_systrayIcon;
226 m_systrayIcon = nullptr;
227 emit stateChanged();
229 #endif
232 #ifndef Q_OS_MACOS
233 void DesktopIntegration::createTrayIcon()
235 Q_ASSERT(!m_systrayIcon);
237 m_systrayIcon = new QSystemTrayIcon(UIThemeManager::instance()->getSystrayIcon(), this);
239 m_systrayIcon->setToolTip(m_toolTip);
241 if (m_menu)
242 m_systrayIcon->setContextMenu(m_menu);
244 connect(m_systrayIcon, &QSystemTrayIcon::activated, this
245 , [this](const QSystemTrayIcon::ActivationReason reason)
247 if (reason == QSystemTrayIcon::Trigger)
248 emit activationRequested();
250 #ifndef QBT_USES_CUSTOMDBUSNOTIFICATIONS
251 connect(m_systrayIcon, &QSystemTrayIcon::messageClicked, this, &DesktopIntegration::notificationClicked);
252 #endif
254 m_systrayIcon->show();
255 emit stateChanged();
257 #endif // Q_OS_MACOS