qa: Replace deprecated qVariantFromValue() by QVariant::fromValue()
[quassel.git] / src / qtui / legacysystemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This file is free software; you can redistribute it and/or modify     *
6  *   it under the terms of the GNU Library General Public License (LGPL)   *
7  *   as published by the Free Software Foundation; either version 2 of the *
8  *   License, or (at your option) any later version.                       *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef QT_NO_SYSTEMTRAYICON
22
23 #    include "legacysystemtray.h"
24
25 #    include "icon.h"
26 #    include "mainwin.h"
27 #    include "qtui.h"
28
29 LegacySystemTray::LegacySystemTray(QWidget* parent)
30     : SystemTray(parent)
31     , _trayIcon{new QSystemTrayIcon(associatedWidget())}
32 {
33 #    ifndef Q_OS_MAC
34     connect(_trayIcon, &QSystemTrayIcon::activated, this, &LegacySystemTray::onActivated);
35 #    endif
36     connect(_trayIcon, &QSystemTrayIcon::messageClicked, this, &LegacySystemTray::onMessageClicked);
37
38     _trayIcon->setContextMenu(trayMenu());
39     _trayIcon->setVisible(false);
40
41     setMode(Mode::Legacy);
42
43     connect(this, &SystemTray::visibilityChanged, this, &LegacySystemTray::onVisibilityChanged);
44     connect(this, &SystemTray::modeChanged, this, &LegacySystemTray::onModeChanged);
45     connect(this, &SystemTray::toolTipChanged, this, &LegacySystemTray::updateToolTip);
46     connect(this, &SystemTray::iconsChanged, this, &LegacySystemTray::updateIcon);
47     connect(this, &SystemTray::currentIconNameChanged, this, &LegacySystemTray::updateIcon);
48
49     updateIcon();
50     updateToolTip();
51 }
52
53 bool LegacySystemTray::isSystemTrayAvailable() const
54 {
55     return mode() == Mode::Legacy ? QSystemTrayIcon::isSystemTrayAvailable() : SystemTray::isSystemTrayAvailable();
56 }
57
58 void LegacySystemTray::onVisibilityChanged(bool isVisible)
59 {
60     if (mode() == Legacy) {
61         _trayIcon->setVisible(isVisible);
62     }
63 }
64
65 void LegacySystemTray::onModeChanged(Mode mode)
66 {
67     if (mode == Mode::Legacy) {
68         _trayIcon->setVisible(isVisible());
69     }
70     else {
71         _trayIcon->hide();
72     }
73 }
74
75 void LegacySystemTray::updateIcon()
76 {
77     QString iconName = (state() == NeedsAttention) ? currentAttentionIconName() : currentIconName();
78     _trayIcon->setIcon(icon::get(iconName, QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(iconName)));
79 }
80
81 void LegacySystemTray::updateToolTip()
82 {
83 #    if defined Q_OS_MAC || defined Q_OS_WIN
84     QString tooltip = QString("%1").arg(toolTipTitle());
85     if (!toolTipSubTitle().isEmpty())
86         tooltip += QString("\n%1").arg(toolTipSubTitle());
87 #    else
88     QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
89     if (!toolTipSubTitle().isEmpty())
90         tooltip += QString("<br>%1").arg(toolTipSubTitle());
91 #    endif
92
93     _trayIcon->setToolTip(tooltip);
94 }
95
96 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
97 {
98     activate((SystemTray::ActivationReason)reason);
99 }
100
101 void LegacySystemTray::onMessageClicked()
102 {
103     emit messageClicked(_lastMessageId);
104 }
105
106 void LegacySystemTray::showMessage(const QString& title, const QString& message, SystemTray::MessageIcon icon, int msTimeout, uint id)
107 {
108     // fancy stuff later: show messages in order
109     // for now, we just show the last message
110     _lastMessageId = id;
111     _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
112 }
113
114 void LegacySystemTray::closeMessage(uint notificationId)
115 {
116     Q_UNUSED(notificationId)
117
118     // there really seems to be no sane way to close the bubble... :(
119 #    ifdef Q_WS_X11
120     showMessage("", "", NoIcon, 1);
121 #    endif
122 }
123
124 #endif /* QT_NO_SYSTEMTRAYICON */