Fixed linker error caused by not running MOC on ircuser.h.
[quassel.git] / src / common / global.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by The Quassel Team                                *
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) 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef _GLOBAL_H_
22 #define _GLOBAL_H_
23
24 /** The protocol version we use fo the communication between core and GUI */
25 #define GUI_PROTOCOL 3
26
27 #define BACKLOG_FORMAT 2
28 #define BACKLOG_STRING "QuasselIRC Backlog File"
29
30 class Global;
31
32 #include <QHash>
33 #include <QMutex>
34 #include <QString>
35 #include <QVariant>
36
37 /* Some global stuff */
38 typedef QMap<QString, QVariant> VarMap;
39
40 typedef uint UserId;
41 typedef uint MsgId;
42
43 /**
44  * This class is mostly a globally synchronized data store, meant for storing systemwide settings such
45  * as identities or network lists. This class is a singleton, but not static as we'd like to use signals and
46  * slots with it.
47  * The global object is used in both Core and GUI clients. Storing and retrieving data is thread-safe.
48  * \note While updated data is propagated to all the remote parts of Quassel quite quickly, the synchronization
49  *       protocol is in no way designed to guarantee strict consistency at all times. In other words, it may
50  *       well happen that different instances of global data differ from one another for a little while until
51  *       all update messages have been processed. You should never rely on all global data stores being consistent.
52 */
53 class Global : public QObject {
54   Q_OBJECT
55
56   public:
57     //static Logger *getLogger();
58     //static void setLogger(Logger *);
59
60 //    static QIcon *getIcon(QString symbol);
61
62     static Global *instance();
63     static void destroy();
64     static void setGuiUser(UserId);
65
66     static QVariant data(QString key, QVariant defaultValue = QVariant());
67     static QVariant data(UserId, QString key, QVariant defaultValue = QVariant());
68     static QStringList keys();
69     static QStringList keys(UserId);
70
71     static void putData(QString key, QVariant data);      ///< Store data changed locally, will be propagated to all other clients and the core
72     static void putData(UserId, QString key, QVariant data);
73
74     static void updateData(QString key, QVariant data);   ///< Update stored data if requested by the core or other clients
75     static void updateData(UserId, QString key, QVariant data);
76
77   signals:
78     void dataPutLocally(UserId, QString key);
79     void dataUpdatedRemotely(UserId, QString key);  // sent by remote update only!
80
81   public:
82     enum RunMode { Monolithic, ClientOnly, CoreOnly };
83     static RunMode runMode;
84     static QString quasselDir;
85
86   private:
87     Global();
88     ~Global();
89     static Global *instanceptr;
90
91     static UserId guiUser;
92     //static void initIconMap();
93
94     //static Logger *logger;
95
96 //    static QString iconPath;
97     //QHash<QString, QString> iconMap;
98     static QMutex mutex;
99     QHash<UserId, QHash<QString, QVariant> > datastore;
100 };
101
102 struct Exception {
103     Exception(QString msg = "Unknown Exception") : _msg(msg) {};
104     virtual ~Exception() {}; // make gcc happy
105     virtual inline QString msg() { return _msg; }
106
107   protected:
108     QString _msg;
109
110 };
111
112 class BufferId {
113   public:
114     BufferId() { id = gid = 0; } // FIXME
115     BufferId(uint uid, QString net, QString buf, uint gid = 0);
116
117     inline uint uid() const { return id; }
118     inline uint groupId() const { return gid; }
119     inline QString network() const { return net; }
120     QString buffer() const; // nickfrommask?
121
122     void setGroupId(uint _gid) { gid = _gid; }
123
124     inline bool operator==(const BufferId &other) const { return id == other.id; }
125
126   private:
127     uint id;
128     uint gid;
129     QString net;
130     QString buf;
131
132     friend uint qHash(const BufferId &);
133     friend QDataStream &operator<<(QDataStream &out, const BufferId &bufferId);
134     friend QDataStream &operator>>(QDataStream &in, BufferId &bufferId);
135 };
136
137 QDataStream &operator<<(QDataStream &out, const BufferId &bufferId);
138 QDataStream &operator>>(QDataStream &in, BufferId &bufferId);
139
140 Q_DECLARE_METATYPE(BufferId);
141
142 uint qHash(const BufferId &);
143
144 #endif