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