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"
39 #include <QSystemTrayIcon>
42 #include "base/preferences.h"
43 #include "uithememanager.h"
46 #include "macutilities.h"
50 #include "notifications/dbusnotifier.h"
56 DesktopIntegration
*desktopIntegrationInstance
= nullptr;
58 bool handleDockClicked(id self
, SEL cmd
, ...)
63 Q_ASSERT(desktopIntegrationInstance
);
64 emit desktopIntegrationInstance
->activationRequested();
71 using namespace std::chrono_literals
;
73 #define SETTINGS_KEY(name) u"GUI/" name
74 #define NOTIFICATIONS_SETTINGS_KEY(name) (SETTINGS_KEY(u"Notifications/"_s) name)
76 DesktopIntegration::DesktopIntegration(QObject
*parent
)
78 , m_storeNotificationEnabled
{NOTIFICATIONS_SETTINGS_KEY(u
"Enabled"_s
), true}
80 , m_storeNotificationTimeOut
{NOTIFICATIONS_SETTINGS_KEY(u
"Timeout"_s
), -1}
84 desktopIntegrationInstance
= this;
85 MacUtils::overrideDockClickHandler(handleDockClicked
);
87 if (Preferences::instance()->systemTrayEnabled())
91 if (isNotificationsEnabled())
93 m_notifier
= new DBusNotifier(this);
94 connect(m_notifier
, &DBusNotifier::messageClicked
, this, &DesktopIntegration::notificationClicked
);
99 connect(Preferences::instance(), &Preferences::changed
, this, &DesktopIntegration::onPreferencesChanged
);
102 DesktopIntegration::~DesktopIntegration()
107 bool DesktopIntegration::isActive() const
112 return m_systrayIcon
&& QSystemTrayIcon::isSystemTrayAvailable();
116 QString
DesktopIntegration::toolTip() const
121 void DesktopIntegration::setToolTip(const QString
&toolTip
)
123 if (m_toolTip
== toolTip
)
129 m_systrayIcon
->setToolTip(m_toolTip
);
133 QMenu
*DesktopIntegration::menu() const
138 void DesktopIntegration::setMenu(QMenu
*menu
)
143 #if defined Q_OS_MACOS
150 m_menu
->setAsDockMenu();
151 #elif defined Q_OS_UNIX
152 const bool systemTrayEnabled
= m_systrayIcon
;
157 delete m_systrayIcon
;
158 m_systrayIcon
= nullptr;
165 if (systemTrayEnabled
&& !m_systrayIcon
)
174 m_systrayIcon
->setContextMenu(m_menu
);
178 bool DesktopIntegration::isNotificationsEnabled() const
180 return m_storeNotificationEnabled
;
183 void DesktopIntegration::setNotificationsEnabled(const bool value
)
185 if (m_storeNotificationEnabled
== value
)
188 m_storeNotificationEnabled
= value
;
193 m_notifier
= new DBusNotifier(this);
194 connect(m_notifier
, &DBusNotifier::messageClicked
, this, &DesktopIntegration::notificationClicked
);
199 m_notifier
= nullptr;
204 int DesktopIntegration::notificationTimeout() const
207 return m_storeNotificationTimeOut
;
214 void DesktopIntegration::setNotificationTimeout(const int value
)
216 m_storeNotificationTimeOut
= value
;
220 void DesktopIntegration::showNotification(const QString
&title
, const QString
&msg
) const
222 if (!isNotificationsEnabled())
226 MacUtils::displayNotification(title
, msg
);
229 m_notifier
->showMessage(title
, msg
, notificationTimeout());
231 if (m_systrayIcon
&& QSystemTrayIcon::supportsMessages())
232 m_systrayIcon
->showMessage(title
, msg
, QSystemTrayIcon::Information
, notificationTimeout());
237 void DesktopIntegration::onPreferencesChanged()
240 if (Preferences::instance()->systemTrayEnabled())
244 // Reload systray icon
245 m_systrayIcon
->setIcon(getSystrayIcon());
254 delete m_systrayIcon
;
255 m_systrayIcon
= nullptr;
262 void DesktopIntegration::createTrayIcon()
264 Q_ASSERT(!m_systrayIcon
);
266 m_systrayIcon
= new QSystemTrayIcon(getSystrayIcon(), this);
268 m_systrayIcon
->setToolTip(m_toolTip
);
271 m_systrayIcon
->setContextMenu(m_menu
);
273 connect(m_systrayIcon
, &QSystemTrayIcon::activated
, this
274 , [this](const QSystemTrayIcon::ActivationReason reason
)
276 if (reason
== QSystemTrayIcon::Trigger
)
277 emit
activationRequested();
279 #ifndef QBT_USES_DBUS
280 connect(m_systrayIcon
, &QSystemTrayIcon::messageClicked
, this, &DesktopIntegration::notificationClicked
);
283 m_systrayIcon
->show();
287 QIcon
DesktopIntegration::getSystrayIcon() const
289 const TrayIcon::Style style
= Preferences::instance()->trayIconStyle();
294 case TrayIcon::Style::Normal
:
295 icon
= UIThemeManager::instance()->getIcon(u
"qbittorrent-tray"_s
);
297 case TrayIcon::Style::MonoDark
:
298 icon
= UIThemeManager::instance()->getIcon(u
"qbittorrent-tray-dark"_s
);
300 case TrayIcon::Style::MonoLight
:
301 icon
= UIThemeManager::instance()->getIcon(u
"qbittorrent-tray-light"_s
);
305 // Workaround for invisible tray icon in KDE, https://bugreports.qt.io/browse/QTBUG-53550
306 if (qEnvironmentVariable("XDG_CURRENT_DESKTOP").compare(u
"KDE", Qt::CaseInsensitive
) == 0)
307 return icon
.pixmap(32);