modernize: Migrate action-related things to PMF connects
[quassel.git] / src / qtui / legacysystemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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
34 #ifndef Q_OS_MAC
35     connect(_trayIcon, &QSystemTrayIcon::activated,
36         this, &LegacySystemTray::onActivated);
37 #endif
38     connect(_trayIcon, &QSystemTrayIcon::messageClicked,
39         this, &LegacySystemTray::onMessageClicked);
40
41     _trayIcon->setContextMenu(trayMenu());
42     _trayIcon->setVisible(false);
43
44     setMode(Mode::Legacy);
45
46     connect(this, &SystemTray::visibilityChanged, this, &LegacySystemTray::onVisibilityChanged);
47     connect(this, &SystemTray::modeChanged, this, &LegacySystemTray::onModeChanged);
48     connect(this, &SystemTray::toolTipChanged, this, &LegacySystemTray::updateToolTip);
49     connect(this, &SystemTray::iconsChanged, this, &LegacySystemTray::updateIcon);
50     connect(this, &SystemTray::currentIconNameChanged, this, &LegacySystemTray::updateIcon);
51
52     updateIcon();
53     updateToolTip();
54 }
55
56
57 bool LegacySystemTray::isSystemTrayAvailable() const
58 {
59     return mode() == Mode::Legacy
60             ? QSystemTrayIcon::isSystemTrayAvailable()
61             : SystemTray::isSystemTrayAvailable();
62 }
63
64
65 void LegacySystemTray::onVisibilityChanged(bool isVisible)
66 {
67     if (mode() == Legacy) {
68         _trayIcon->setVisible(isVisible);
69     }
70 }
71
72
73 void LegacySystemTray::onModeChanged(Mode mode)
74 {
75     if (mode == Mode::Legacy) {
76         _trayIcon->setVisible(isVisible());
77     }
78     else {
79         _trayIcon->hide();
80     }
81 }
82
83
84 void LegacySystemTray::updateIcon()
85 {
86     QString iconName = (state() == NeedsAttention) ? currentAttentionIconName() : currentIconName();
87     _trayIcon->setIcon(icon::get(iconName, QString{":/icons/hicolor/24x24/status/%1.svg"}.arg(iconName)));
88 }
89
90
91 void LegacySystemTray::updateToolTip()
92 {
93 #if defined Q_OS_MAC || defined Q_OS_WIN
94     QString tooltip = QString("%1").arg(toolTipTitle());
95     if (!toolTipSubTitle().isEmpty())
96         tooltip += QString("\n%1").arg(toolTipSubTitle());
97 #else
98     QString tooltip = QString("<b>%1</b>").arg(toolTipTitle());
99     if (!toolTipSubTitle().isEmpty())
100         tooltip += QString("<br>%1").arg(toolTipSubTitle());
101 #endif
102
103     _trayIcon->setToolTip(tooltip);
104 }
105
106
107 void LegacySystemTray::onActivated(QSystemTrayIcon::ActivationReason reason)
108 {
109     activate((SystemTray::ActivationReason)reason);
110 }
111
112
113 void LegacySystemTray::onMessageClicked()
114 {
115     emit messageClicked(_lastMessageId);
116 }
117
118
119 void LegacySystemTray::showMessage(const QString &title, const QString &message, SystemTray::MessageIcon icon, int msTimeout, uint id)
120 {
121     // fancy stuff later: show messages in order
122     // for now, we just show the last message
123     _lastMessageId = id;
124     _trayIcon->showMessage(title, message, (QSystemTrayIcon::MessageIcon)icon, msTimeout);
125 }
126
127
128 void LegacySystemTray::closeMessage(uint notificationId)
129 {
130     Q_UNUSED(notificationId)
131
132     // there really seems to be no sane way to close the bubble... :(
133 #ifdef Q_WS_X11
134     showMessage("", "", NoIcon, 1);
135 #endif
136 }
137
138
139 #endif /* QT_NO_SYSTEMTRAYICON */