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