Rename HAVE_KDE define to HAVE_KDE4
[quassel.git] / src / qtui / systemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 #include <QApplication>
22 #include <QMenu>
23
24 #include "systemtray.h"
25
26 #include "action.h"
27 #include "actioncollection.h"
28 #include "client.h"
29 #include "qtui.h"
30
31 #ifdef HAVE_KDE4
32 #  include <KMenu>
33 #  include <KWindowInfo>
34 #  include <KWindowSystem>
35 #endif
36
37 SystemTray::SystemTray(QWidget *parent)
38     : QObject(parent),
39     _mode(Invalid),
40     _state(Passive),
41     _shouldBeVisible(true),
42     _passiveIcon(QIcon::fromTheme("quassel-inactive", QIcon(":/icons/quassel-inactive.png"))),
43     _activeIcon(QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png"))),
44     _needsAttentionIcon(QIcon::fromTheme("quassel-message", QIcon(":/icons/quassel-message.png"))),
45     _trayMenu(0),
46     _associatedWidget(parent)
47 {
48     Q_ASSERT(parent);
49 }
50
51
52 SystemTray::~SystemTray()
53 {
54     _trayMenu->deleteLater();
55 }
56
57
58 QWidget *SystemTray::associatedWidget() const
59 {
60     return _associatedWidget;
61 }
62
63
64 void SystemTray::init()
65 {
66     ActionCollection *coll = QtUi::actionCollection("General");
67     _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
68
69 #ifdef HAVE_KDE4
70     KMenu *kmenu;
71     _trayMenu = kmenu = new KMenu();
72     kmenu->addTitle(_activeIcon, "Quassel IRC");
73 #else
74     _trayMenu = new QMenu(associatedWidget());
75 #endif
76
77     _trayMenu->setTitle("Quassel IRC");
78
79 #ifndef HAVE_KDE4
80     _trayMenu->setAttribute(Qt::WA_Hover);
81 #endif
82
83     _trayMenu->addAction(coll->action("ConnectCore"));
84     _trayMenu->addAction(coll->action("DisconnectCore"));
85     _trayMenu->addAction(coll->action("CoreInfo"));
86     _trayMenu->addSeparator();
87     _trayMenu->addAction(_minimizeRestoreAction);
88     _trayMenu->addAction(coll->action("Quit"));
89
90     connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
91
92     NotificationSettings notificationSettings;
93     notificationSettings.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
94 }
95
96
97 void SystemTray::trayMenuAboutToShow()
98 {
99     if (GraphicalUi::isMainWidgetVisible())
100         _minimizeRestoreAction->setText(tr("&Minimize"));
101     else
102         _minimizeRestoreAction->setText(tr("&Restore"));
103 }
104
105
106 void SystemTray::setMode(Mode mode_)
107 {
108     if (mode_ != _mode) {
109         _mode = mode_;
110 #ifdef HAVE_KDE4
111         if (_trayMenu) {
112             if (_mode == Legacy) {
113                 _trayMenu->setWindowFlags(Qt::Popup);
114             }
115             else {
116                 _trayMenu->setWindowFlags(Qt::Window);
117             }
118         }
119 #endif
120     }
121 }
122
123
124 QIcon SystemTray::stateIcon() const
125 {
126     return stateIcon(state());
127 }
128
129
130 QIcon SystemTray::stateIcon(State state) const
131 {
132     switch (state) {
133     case Passive:
134         return _passiveIcon;
135     case Active:
136         return _activeIcon;
137     case NeedsAttention:
138         return _needsAttentionIcon;
139     }
140     return QIcon();
141 }
142
143
144 void SystemTray::setState(State state)
145 {
146     if (_state != state) {
147         _state = state;
148     }
149 }
150
151
152 void SystemTray::setAlert(bool alerted)
153 {
154     if (alerted)
155         setState(NeedsAttention);
156     else
157         setState(Client::isConnected() ? Active : Passive);
158 }
159
160
161 void SystemTray::setVisible(bool visible)
162 {
163     _shouldBeVisible = visible;
164 }
165
166
167 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
168 {
169     _toolTipTitle = title;
170     _toolTipSubTitle = subtitle;
171     emit toolTipChanged(title, subtitle);
172 }
173
174
175 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
176 {
177     Q_UNUSED(title)
178     Q_UNUSED(message)
179     Q_UNUSED(icon)
180     Q_UNUSED(millisecondsTimeoutHint)
181     Q_UNUSED(id)
182 }
183
184
185 void SystemTray::activate(SystemTray::ActivationReason reason)
186 {
187     emit activated(reason);
188 }
189
190
191 void SystemTray::minimizeRestore()
192 {
193     GraphicalUi::toggleMainWidget();
194 }
195
196
197 void SystemTray::enableAnimationChanged(const QVariant &v)
198 {
199     _animationEnabled = v.toBool();
200     emit animationEnabledChanged(v.toBool());
201 }