Finaly got rid of the synchronizers, making Quassel quite a bit more lightweight...
[quassel.git] / src / core / coresession.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel IRC Development 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 #include "coresession.h"
22 #include "server.h"
23
24 #include "signalproxy.h"
25 #include "storage.h"
26
27 #include "networkinfo.h"
28 #include "ircuser.h"
29 #include "ircchannel.h"
30
31 #include "util.h"
32
33
34 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
35   : QObject(parent),
36     user(uid),
37     _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
38     storage(_storage)
39 {
40   
41   QSettings s;
42   s.beginGroup(QString("SessionData/%1").arg(user));
43   mutex.lock();
44   foreach(QString key, s.allKeys()) {
45     sessionData[key] = s.value(key);
46   }
47   mutex.unlock();
48
49   SignalProxy *p = signalProxy();
50
51   p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
52   p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
53   p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
54   p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
55   p->attachSignal(this, SIGNAL(displayMsg(Message)));
56   p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
57   p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
58   p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
59   p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
60   p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
61   p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
62   /* Autoconnect. (When) do we actually do this?
63   QStringList list;
64   QVariantMap networks = retrieveSessionData("Networks").toMap();
65   foreach(QString net, networks.keys()) {
66     if(networks[net].toMap()["AutoConnect"].toBool()) {
67       list << net;
68     }
69   } qDebug() << list;
70   if(list.count()) connectToIrc(list);
71   */
72 }
73
74 CoreSession::~CoreSession() {
75 }
76
77 UserId CoreSession::userId() const {
78   return user;
79 }
80
81 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
82   QSettings s;
83   s.beginGroup(QString("SessionData/%1").arg(user));
84   mutex.lock();
85   sessionData[key] = data;
86   s.setValue(key, data);
87   mutex.unlock();
88   s.endGroup();
89   emit sessionDataChanged(key, data);
90   emit sessionDataChanged(key);
91 }
92
93 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
94   QVariant data;
95   mutex.lock();
96   if(!sessionData.contains(key)) data = def;
97   else data = sessionData[key];
98   mutex.unlock();
99   return data;
100 }
101
102 // FIXME switch to NetworkIDs
103 void CoreSession::connectToNetwork(QString network) {
104   uint networkid = getNetworkId(network);
105   if(!servers.contains(networkid)) {
106     Server *server = new Server(userId(), networkid, network);
107     servers[networkid] = server;
108     attachServer(server);
109     server->start();
110   }
111   emit connectToIrc(network);
112 }
113
114 void CoreSession::attachServer(Server *server) {
115   connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
116   connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
117   connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
118   
119   connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
120   connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
121   connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
122   connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
123
124   // connect serversignals to proxy
125   signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
126   signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
127   signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
128   // TODO add error handling
129 }
130
131 void CoreSession::serverStateRequested() {
132 }
133
134 void CoreSession::addClient(QIODevice *device) {
135   signalProxy()->addPeer(device);
136 }
137
138 SignalProxy *CoreSession::signalProxy() const {
139   return _signalProxy;
140 }
141
142 void CoreSession::serverConnected(uint networkid) {
143   storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
144 }
145
146 void CoreSession::serverDisconnected(uint networkid) {
147   Q_ASSERT(servers.contains(networkid));
148   servers.take(networkid)->deleteLater();
149   Q_ASSERT(!servers.contains(networkid));
150 }
151
152 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
153   emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
154 }
155
156 // ALL messages coming pass through these functions before going to the GUI.
157 // So this is the perfect place for storing the backlog and log stuff.
158 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
159   Server *s = qobject_cast<Server*>(this->sender());
160   Q_ASSERT(s);
161   BufferInfo buf;
162   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
163     buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
164   } else {
165     buf = storage->getBufferInfo(user, s->networkName(), target);
166   }
167   Message msg(buf, type, text, sender, flags);
168   msg.setMsgId(storage->logMessage(msg));
169   Q_ASSERT(msg.msgId());
170   emit displayMsg(msg);
171 }
172
173 void CoreSession::recvStatusMsgFromServer(QString msg) {
174   Server *s = qobject_cast<Server*>(sender());
175   Q_ASSERT(s);
176   emit displayStatusMsg(s->networkName(), msg);
177 }
178
179
180 uint CoreSession::getNetworkId(const QString &net) const {
181   return storage->getNetworkId(user, net);
182 }
183
184 QList<BufferInfo> CoreSession::buffers() const {
185   return storage->requestBuffers(user);
186 }
187
188
189 QVariant CoreSession::sessionState() {
190   QVariantMap v;
191
192   QVariantList bufs;
193   foreach(BufferInfo id, storage->requestBuffers(user))
194     bufs.append(QVariant::fromValue(id));
195   v["Buffers"] = bufs;
196
197   mutex.lock();
198   v["SessionData"] = sessionData;
199   mutex.unlock();
200
201   QVariantList networks;
202   foreach(uint networkid, servers.keys())
203     networks.append(QVariant(networkid));
204   v["Networks"] = QVariant(networks);
205
206   // v["Payload"] = QByteArray(100000000, 'a');  // for testing purposes
207   return v;
208 }
209
210 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
211   QList<QVariant> log;
212   QList<Message> msglist;
213   if(v1.type() == QVariant::DateTime) {
214
215
216   } else {
217     msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
218   }
219
220   // Send messages out in smaller packages - we don't want to make the signal data too large!
221   for(int i = 0; i < msglist.count(); i++) {
222     log.append(QVariant::fromValue(msglist[i]));
223     if(log.count() >= 5) {
224       emit backlogData(id, log, i >= msglist.count() - 1);
225       log.clear();
226     }
227   }
228   if(log.count() > 0) emit backlogData(id, log, true);
229 }