Make disabling the tray icon animation work again
[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   _passiveIcon(DesktopIcon("quassel_inactive")),
42   _activeIcon(DesktopIcon("quassel")),
43   _needsAttentionIcon(DesktopIcon("quassel_message")),
44   _trayMenu(0),
45   _associatedWidget(parent)
46 {
47   Q_ASSERT(parent);
48 }
49
50 SystemTray::~SystemTray() {
51   _trayMenu->deleteLater();
52 }
53
54 QWidget *SystemTray::associatedWidget() const {
55   return _associatedWidget;
56 }
57
58 void SystemTray::init() {
59   ActionCollection *coll = QtUi::actionCollection("General");
60   _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
61
62 #ifdef HAVE_KDE
63   KMenu *kmenu;
64   _trayMenu = kmenu = new KMenu();
65   kmenu->addTitle(qApp->windowIcon(), "Quassel IRC");
66 #else
67   _trayMenu = new QMenu(associatedWidget());
68 #endif
69
70   _trayMenu->setTitle("Quassel IRC");
71
72 #ifndef HAVE_KDE
73   _trayMenu->setAttribute(Qt::WA_Hover);
74 #endif
75
76   _trayMenu->addAction(coll->action("ConnectCore"));
77   _trayMenu->addAction(coll->action("DisconnectCore"));
78   _trayMenu->addAction(coll->action("CoreInfo"));
79   _trayMenu->addSeparator();
80   _trayMenu->addAction(_minimizeRestoreAction);
81   _trayMenu->addAction(coll->action("Quit"));
82
83   connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
84
85   NotificationSettings notificationSettings;
86   notificationSettings.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
87 }
88
89 void SystemTray::trayMenuAboutToShow() {
90   if(GraphicalUi::isMainWidgetVisible())
91     _minimizeRestoreAction->setText(tr("&Minimize"));
92   else
93     _minimizeRestoreAction->setText(tr("&Restore"));
94 }
95
96 void SystemTray::setMode(Mode mode_) {
97   if(mode_ != _mode) {
98     _mode = mode_;
99 #ifdef HAVE_KDE
100     if(_trayMenu) {
101       if(_mode == Legacy) {
102         _trayMenu->setWindowFlags(Qt::Popup);
103       } else {
104         _trayMenu->setWindowFlags(Qt::Window);
105       }
106     }
107 #endif
108   }
109 }
110
111 Icon SystemTray::stateIcon() const {
112   return stateIcon(state());
113 }
114
115 Icon SystemTray::stateIcon(State state) const {
116   switch(state) {
117   case Passive:
118     return _passiveIcon;
119   case Active:
120     return _activeIcon;
121   case NeedsAttention:
122     return _needsAttentionIcon;
123   }
124   return Icon();
125 }
126
127 void SystemTray::setState(State state) {
128   if(_state != state) {
129     _state = state;
130   }
131 }
132
133 void SystemTray::setAlert(bool alerted) {
134   if(alerted)
135     setState(NeedsAttention);
136   else
137     setState(Client::isConnected() ? Active : Passive);
138 }
139
140 void SystemTray::setVisible(bool visible) {
141   Q_UNUSED(visible)
142 }
143
144 void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
145   _toolTipTitle = title;
146   _toolTipSubTitle = subtitle;
147   emit toolTipChanged(title, subtitle);
148 }
149
150 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id) {
151   Q_UNUSED(title)
152   Q_UNUSED(message)
153   Q_UNUSED(icon)
154   Q_UNUSED(millisecondsTimeoutHint)
155   Q_UNUSED(id)
156 }
157
158 void SystemTray::activate(SystemTray::ActivationReason reason) {
159   emit activated(reason);
160 }
161
162 void SystemTray::minimizeRestore() {
163   GraphicalUi::toggleMainWidget();
164 }
165
166 void SystemTray::enableAnimationChanged(const QVariant &v) {
167   _animationEnabled = v.toBool();
168   emit animationEnabledChanged(v.toBool());
169 }