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