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