Improve systray activation behavior; more refactoring
[quassel.git] / src / qtui / systemtray.h
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
21 #ifndef SYSTEMTRAY_H_
22 #define SYSTEMTRAY_H_
23
24 #include "icon.h"
25
26 #ifdef Q_WS_WIN
27 #  include <windows.h>
28 #endif
29
30 class QMenu;
31
32 class SystemTray : public QObject {
33   Q_OBJECT
34   Q_ENUMS(State Mode MessageIcon ActivationReason)
35
36 public:
37   enum State {
38     Passive,
39     Active,
40     NeedsAttention
41   };
42
43   enum Mode {
44     Invalid,
45     Legacy,
46     StatusNotifier
47   };
48
49   // same as in QSystemTrayIcon
50   enum MessageIcon {
51     NoIcon,
52     Information,
53     Warning,
54     Critical
55   };
56
57   // same as in QSystemTrayIcon
58   enum ActivationReason {
59     Unknown,
60     Context,
61     DoubleClick,
62     Trigger,
63     MiddleClick
64   };
65
66   explicit SystemTray(QWidget *parent);
67   virtual ~SystemTray();
68   virtual void init() {}
69
70   inline State state() const;
71   inline bool isAlerted() const;
72   virtual inline bool isSystemTrayAvailable() const;
73
74   void setAlert(bool alerted);
75   inline void setInhibitActivation();
76   inline virtual bool isVisible() const { return false; }
77   inline bool isActivationInhibited() const;
78
79   QWidget *associatedWidget() const;
80
81 public slots:
82   virtual void setState(State);
83   virtual void setVisible(bool visible = true);
84   virtual void setToolTip(const QString &title, const QString &subtitle);
85   virtual void showMessage(const QString &title, const QString &message, MessageIcon icon = Information, int millisecondsTimeoutHint = 10000);
86   void toggleMainWidget();
87   void hideMainWidget();
88
89 signals:
90   void activated(SystemTray::ActivationReason);
91   void iconChanged(const Icon &);
92   void toolTipChanged(const QString &title, const QString &subtitle);
93   void messageClicked();
94
95 protected slots:
96   virtual void activate(SystemTray::ActivationReason = Trigger);
97
98   void minimizeRestore(bool restore);
99
100 protected:
101   virtual void setMode(Mode mode);
102   inline Mode mode() const;
103
104   virtual Icon stateIcon() const;
105   Icon stateIcon(State state) const;
106   inline QString toolTipTitle() const;
107   inline QString toolTipSubTitle() const;
108   inline QMenu *trayMenu() const;
109   void setTrayMenu(QMenu *);
110
111   virtual bool eventFilter(QObject *obj, QEvent *event);
112
113 private slots:
114
115 private:
116   bool checkVisibility(bool performToggle = true);
117
118   Mode _mode;
119   State _state;
120
121   bool _inhibitActivation;
122
123   QString _toolTipTitle, _toolTipSubTitle;
124   Icon _passiveIcon, _activeIcon, _needsAttentionIcon;
125
126   QMenu *_trayMenu;
127   QWidget *_associatedWidget;
128
129 #ifdef Q_WS_WIN
130     DWORD _dwTickCount;
131 #endif
132
133 };
134
135 // inlines
136
137 bool SystemTray::isSystemTrayAvailable() const { return false; }
138 bool SystemTray::isAlerted() const { return state() == NeedsAttention; }
139 void SystemTray::setInhibitActivation() { _inhibitActivation = true; }
140 bool SystemTray::isActivationInhibited() const { return _inhibitActivation; }
141 SystemTray::Mode SystemTray::mode() const { return _mode; }
142 SystemTray::State SystemTray::state() const { return _state; }
143 QMenu *SystemTray::trayMenu() const { return _trayMenu; }
144 QString SystemTray::toolTipTitle() const { return _toolTipTitle; }
145 QString SystemTray::toolTipSubTitle() const { return _toolTipSubTitle; }
146
147
148 #endif