83cb8d97fa215faafdbdb88f5eb304ee51b01d5f
[quassel.git] / src / qtui / systemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include <QApplication>
21 #include <QMenu>
22
23 #include "systemtray.h"
24
25 #include "action.h"
26 #include "actioncollection.h"
27 #include "client.h"
28 #include "iconloader.h"
29 #include "qtui.h"
30
31 #ifdef HAVE_KDE
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(DesktopIcon("quassel_inactive")),
43   _activeIcon(DesktopIcon("quassel")),
44   _needsAttentionIcon(DesktopIcon("quassel_message")),
45   _trayMenu(0),
46   _associatedWidget(parent)
47 {
48   Q_ASSERT(parent);
49 }
50
51 SystemTray::~SystemTray() {
52   _trayMenu->deleteLater();
53 }
54
55 QWidget *SystemTray::associatedWidget() const {
56   return _associatedWidget;
57 }
58
59 void SystemTray::init() {
60   ActionCollection *coll = QtUi::actionCollection("General");
61   _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
62
63 #ifdef HAVE_KDE
64   KMenu *kmenu;
65   _trayMenu = kmenu = new KMenu();
66   kmenu->addTitle(_activeIcon, "Quassel IRC");
67 #else
68   _trayMenu = new QMenu(associatedWidget());
69 #endif
70
71   _trayMenu->setTitle("Quassel IRC");
72
73 #ifndef HAVE_KDE
74   _trayMenu->setAttribute(Qt::WA_Hover);
75 #endif
76
77   _trayMenu->addAction(coll->action("ConnectCore"));
78   _trayMenu->addAction(coll->action("DisconnectCore"));
79   _trayMenu->addAction(coll->action("CoreInfo"));
80   _trayMenu->addSeparator();
81   _trayMenu->addAction(_minimizeRestoreAction);
82   _trayMenu->addAction(coll->action("Quit"));
83
84   connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
85
86   NotificationSettings notificationSettings;
87   notificationSettings.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
88 }
89
90 void SystemTray::trayMenuAboutToShow() {
91   if(GraphicalUi::isMainWidgetVisible())
92     _minimizeRestoreAction->setText(tr("&Minimize"));
93   else
94     _minimizeRestoreAction->setText(tr("&Restore"));
95 }
96
97 void SystemTray::setMode(Mode mode_) {
98   if(mode_ != _mode) {
99     _mode = mode_;
100 #ifdef HAVE_KDE
101     if(_trayMenu) {
102       if(_mode == Legacy) {
103         _trayMenu->setWindowFlags(Qt::Popup);
104       } else {
105         _trayMenu->setWindowFlags(Qt::Window);
106       }
107     }
108 #endif
109   }
110 }
111
112 Icon SystemTray::stateIcon() const {
113   return stateIcon(state());
114 }
115
116 Icon SystemTray::stateIcon(State state) const {
117   switch(state) {
118   case Passive:
119     return _passiveIcon;
120   case Active:
121     return _activeIcon;
122   case NeedsAttention:
123     return _needsAttentionIcon;
124   }
125   return Icon();
126 }
127
128 void SystemTray::setState(State state) {
129   if(_state != state) {
130     _state = state;
131   }
132 }
133
134 void SystemTray::setAlert(bool alerted) {
135   if(alerted)
136     setState(NeedsAttention);
137   else
138     setState(Client::isConnected() ? Active : Passive);
139 }
140
141 void SystemTray::setVisible(bool visible) {
142   _shouldBeVisible = visible;
143 }
144
145 void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
146   _toolTipTitle = title;
147   _toolTipSubTitle = subtitle;
148   emit toolTipChanged(title, subtitle);
149 }
150
151 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id) {
152   Q_UNUSED(title)
153   Q_UNUSED(message)
154   Q_UNUSED(icon)
155   Q_UNUSED(millisecondsTimeoutHint)
156   Q_UNUSED(id)
157 }
158
159 void SystemTray::activate(SystemTray::ActivationReason reason) {
160   emit activated(reason);
161 }
162
163 void SystemTray::minimizeRestore() {
164   GraphicalUi::toggleMainWidget();
165 }
166
167 void SystemTray::enableAnimationChanged(const QVariant &v) {
168   _animationEnabled = v.toBool();
169   emit animationEnabledChanged(v.toBool());
170 }