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