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