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