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