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"
38 #include <QSystemTrayIcon>
41 #include "base/logger.h"
42 #include "base/preferences.h"
43 #include "uithememanager.h"
46 #include "macutilities.h"
49 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
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/"_qs) name)
76 DesktopIntegration::DesktopIntegration(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}
84 desktopIntegrationInstance
= this;
85 MacUtils::overrideDockClickHandler(handleDockClicked
);
87 if (Preferences::instance()->systemTrayEnabled())
90 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
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 bool DesktopIntegration::isActive() const
107 return QSystemTrayIcon::isSystemTrayAvailable();
111 QString
DesktopIntegration::toolTip() const
116 void DesktopIntegration::setToolTip(const QString
&toolTip
)
118 if (m_toolTip
== toolTip
)
124 m_systrayIcon
->setToolTip(m_toolTip
);
128 QMenu
*DesktopIntegration::menu() const
133 void DesktopIntegration::setMenu(QMenu
*menu
)
142 m_menu
->setAsDockMenu();
145 m_systrayIcon
->setContextMenu(m_menu
);
149 bool DesktopIntegration::isNotificationsEnabled() const
151 return m_storeNotificationEnabled
;
154 void DesktopIntegration::setNotificationsEnabled(const bool value
)
156 if (m_storeNotificationEnabled
== value
)
159 m_storeNotificationEnabled
= value
;
161 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
164 m_notifier
= new DBusNotifier(this);
165 connect(m_notifier
, &DBusNotifier::messageClicked
, this, &DesktopIntegration::notificationClicked
);
170 m_notifier
= nullptr;
175 int DesktopIntegration::notificationTimeout() const
177 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
178 return m_storeNotificationTimeOut
;
184 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
185 void DesktopIntegration::setNotificationTimeout(const int value
)
187 m_storeNotificationTimeOut
= value
;
191 void DesktopIntegration::showNotification(const QString
&title
, const QString
&msg
) const
193 if (!isNotificationsEnabled())
197 MacUtils::displayNotification(title
, msg
);
199 #ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
200 m_notifier
->showMessage(title
, msg
, notificationTimeout());
202 if (m_systrayIcon
&& QSystemTrayIcon::supportsMessages())
203 m_systrayIcon
->showMessage(title
, msg
, QSystemTrayIcon::Information
, notificationTimeout());
208 void DesktopIntegration::onPreferencesChanged()
211 if (Preferences::instance()->systemTrayEnabled())
215 // Reload systray icon
216 m_systrayIcon
->setIcon(UIThemeManager::instance()->getSystrayIcon());
225 delete m_systrayIcon
;
226 m_systrayIcon
= nullptr;
233 void DesktopIntegration::createTrayIcon()
235 Q_ASSERT(!m_systrayIcon
);
237 m_systrayIcon
= new QSystemTrayIcon(UIThemeManager::instance()->getSystrayIcon(), this);
239 m_systrayIcon
->setToolTip(m_toolTip
);
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
);
254 m_systrayIcon
->show();