qtui: Clean up SystemTray
[quassel.git] / src / qtui / systemtray.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #pragma once
22
23 #include <QIcon>
24
25 class Action;
26 class QMenu;
27
28 class SystemTray : public QObject
29 {
30     Q_OBJECT
31     Q_ENUMS(State Mode MessageIcon ActivationReason)
32
33 public:
34     enum State {
35         Passive,
36         Active,
37         NeedsAttention
38     };
39
40     enum Mode {
41         Invalid,
42         Legacy,
43         StatusNotifier
44     };
45
46     // same as in QSystemTrayIcon
47     enum MessageIcon {
48         NoIcon,
49         Information,
50         Warning,
51         Critical
52     };
53
54     // same as in QSystemTrayIcon
55     enum ActivationReason {
56         Unknown,
57         Context,
58         DoubleClick,
59         Trigger,
60         MiddleClick
61     };
62
63     explicit SystemTray(QWidget *parent);
64     ~SystemTray() override;
65     virtual void init();
66
67     Mode mode() const;
68     State state() const;
69     bool isAlerted() const;
70     void setAlert(bool alerted);
71
72     virtual bool isVisible() const;
73     virtual bool isSystemTrayAvailable() const;
74
75     QWidget *associatedWidget() const;
76
77 public slots:
78     virtual void setState(State);
79     virtual void setVisible(bool visible = true);
80     virtual void setToolTip(const QString &title, const QString &subtitle);
81     virtual void showMessage(const QString &title, const QString &message, MessageIcon icon = Information, int msTimeout = 10000, uint notificationId = 0);
82     virtual void closeMessage(uint notificationId);
83
84 signals:
85     void activated(SystemTray::ActivationReason);
86     void iconChanged(const QIcon &icon);
87     void animationEnabledChanged(bool);
88     void toolTipChanged(const QString &title, const QString &subtitle);
89     void messageClicked(uint notificationId);
90     void messageClosed(uint notificationId);
91
92 protected slots:
93     virtual void activate(SystemTray::ActivationReason = Trigger);
94
95 protected:
96     virtual void setMode(Mode mode);
97     bool shouldBeVisible() const;
98
99     virtual QIcon stateIcon() const;
100     QIcon stateIcon(State state) const;
101     QString toolTipTitle() const;
102     QString toolTipSubTitle() const;
103     QMenu *trayMenu() const;
104
105     bool animationEnabled() const;
106
107 private slots:
108     void minimizeRestore();
109     void trayMenuAboutToShow();
110     void enableAnimationChanged(const QVariant &);
111
112 private:
113     Mode _mode{Mode::Invalid};
114     State _state{State::Passive};
115     bool _shouldBeVisible{true};
116     bool _animationEnabled{true};
117
118     QString _toolTipTitle, _toolTipSubTitle;
119     QIcon _passiveIcon, _activeIcon, _needsAttentionIcon;
120
121     QMenu *_trayMenu{nullptr};
122     QWidget *_associatedWidget{nullptr};
123     Action *_minimizeRestoreAction{nullptr};
124 };