-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifier.h
181 lines (150 loc) · 3.85 KB
/
Notifier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar
* 14 rue de Plaisance, 75014 Paris, France
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#ifndef NOTIFIER_H
#define NOTIFIER_H
#include <QWidget>
#include <QSystemTrayIcon>
#include <QSound>
#include <QMap>
#include <QTimer>
/**
* Utility function.
*
* Indicate whether the assignment changes the destination's value.
*/
template<typename T>
bool confirm_assign(T *dst, const T &src)
{
if(*dst != src)
{
*dst = src;
return true;
}
else
return false;
}
class ServerError : public std::exception {
private:
QString w;
public:
ServerError(const QString error);
virtual const char *what() const throw();
virtual ~ServerError() throw() {}
};
/**
* A server we can query for players information.
*/
class Server : public QObject {
Q_OBJECT
public:
/**
* The number of players currently in-game.
*/
virtual unsigned int numPlayers() const = 0;
/**
* The maximum number of players that can join the game, or 0.
*/
virtual unsigned int maxPlayers() const;
/**
* The map's name, if relevant, or QString().
*/
virtual QString map() const;
/**
* The game mode, if relevant, or QString().
*/
virtual QString mode() const;
public slots:
/**
* Forcibly update the information by querying the server.
*/
virtual void refresh() = 0;
signals:
/**
* Signal emitted when the informations have changed.
*
* @param gamestarted Boolean indicating whether the game just started, ie
* there was no player before and there are players now. It should therefore
* never be set two consecutive times.
*/
void infosChanged(unsigned int players, unsigned int max, QString map,
QString mode, bool gamestarted);
/**
* Signal emitted on errors.
*/
void errorEncountered(QString text);
};
/**
* A server's notification configuration.
*/
struct ServerConf {
QString name;
bool play_sound;
bool change_color;
bool display_popup;
inline ServerConf() {}
inline ServerConf(QString p_Name, bool p_Sound, bool p_Color, bool p_Popup)
: name(p_Name), play_sound(p_Sound), change_color(p_Color),
display_popup(p_Popup)
{
}
};
/**
* The notifier.
*/
class Notifier : public QWidget {
Q_OBJECT
private:
QSystemTrayIcon *m_pTrayIcon;
QSound *m_pBeep;
#ifdef _WIN32
QAction *m_pAutoStart;
QString m_sPath;
#endif
QMap<Server*, ServerConf> m_aServers;
struct Notification {
QString title;
QString message;
};
QList<Notification> m_lNotifications;
QList<Notification> m_lErrors;
QTimer *m_pMessageTimer;
QIcon m_IconEmpty;
QIcon m_IconPlayers;
private:
void addServer(Server *serv, const ServerConf &conf);
void appendNotification(QString name, unsigned int players,
unsigned int max, QString map, QString mode);
#ifdef _WIN32
void loadAutoStartState();
#endif
private slots:
void displayError(QString error);
void infosChanged(int players, int max, QString map, QString mode,
bool gamestarted);
void updateMessage();
void flushNotifications();
void updateIcon();
void tellAgain();
void iconActivated(QSystemTrayIcon::ActivationReason reason);
void setAutoStart();
public:
Notifier(QWidget *pParent = NULL);
#ifdef _WIN32
void setExecutablePath(const char *path);
#endif
signals:
void refreshAll();
};
#endif