OK Folks, my first commit after quite a while, and while Quassel looks the same as...
[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(Global::instance(), SIGNAL(dataUpdatedRemotely(UserId, QString)), this, SLOT(globalDataUpdated(UserId, QString)));
50   connect(Global::instance(), SIGNAL(dataPutLocally(UserId, QString)), this, SLOT(globalDataUpdated(UserId, QString)));
51   connect(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), coreProxy, SLOT(csSessionDataChanged(const QString &, const QVariant &)));
52   connect(coreProxy, SIGNAL(gsSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
53 }
54
55 CoreSession::~CoreSession() {
56
57 }
58
59 UserId CoreSession::userId() const {
60   return user;
61 }
62
63 void CoreSession::processSignal(ClientSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
64   coreProxy->recv(sig, arg1, arg2, arg3);
65 }
66
67 void CoreSession::globalDataUpdated(UserId uid, QString key) {
68   Q_ASSERT(uid == userId());
69   QVariant data = Global::data(userId(), key);
70   QSettings s;
71   s.setValue(QString("Global/%1/").arg(userId())+key, data);
72 }
73
74 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
75   QSettings s;
76   s.beginGroup(QString("SessionData/%1").arg(user));
77   mutex.lock();
78   sessionData[key] = data;
79   s.setValue(key, data);
80   mutex.unlock();
81   s.endGroup();
82   emit sessionDataChanged(key, data);
83   emit sessionDataChanged(key);
84 }
85
86 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
87   QVariant data;
88   mutex.lock();
89   if(!sessionData.contains(key)) data = def;
90   else data = sessionData[key];
91   mutex.unlock();
92   return data;
93 }
94
95 void CoreSession::connectToIrc(QStringList networks) {
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
109       connect(server, SIGNAL(serverState(QString, VarMap)), coreProxy, SLOT(csServerState(QString, VarMap)));
110       //connect(server, SIGNAL(displayMsg(Message)), this, SLOT(recvMessageFromServer(Message)));
111       connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
112       connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
113       connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
114       connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
115       connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
116       connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
117       connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
118       connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
119       connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
120       connect(server, SIGNAL(queryRequested(QString, QString)), coreProxy, SLOT(csQueryRequested(QString, QString)));
121       // TODO add error handling
122       connect(server, SIGNAL(connected(QString)), coreProxy, SLOT(csServerConnected(QString)));
123       connect(server, SIGNAL(disconnected(QString)), coreProxy, SLOT(csServerDisconnected(QString)));
124
125       server->start();
126       servers[net] = server;
127     }
128     emit connectToIrc(net);
129   }
130 }
131
132 void CoreSession::serverConnected(QString net) {
133   storage->getBufferId(userId(), net); // create status buffer
134 }
135
136 void CoreSession::serverDisconnected(QString net) {
137   delete servers[net];
138   servers.remove(net);
139   coreProxy->csServerDisconnected(net);
140 }
141
142 void CoreSession::msgFromGui(BufferId bufid, QString msg) {
143   emit msgFromGui(bufid.network(), bufid.buffer(), msg);
144 }
145
146 // ALL messages coming pass through these functions before going to the GUI.
147 // So this is the perfect place for storing the backlog and log stuff.
148
149 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
150   Server *s = qobject_cast<Server*>(this->sender());
151   Q_ASSERT(s);
152   BufferId buf;
153   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
154     buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
155   } else {
156     buf = storage->getBufferId(user, s->getNetwork(), target);
157   }
158   Message msg(buf, type, text, sender, flags);
159   msg.msgId = storage->logMessage(msg); //qDebug() << msg.msgId;
160   Q_ASSERT(msg.msgId);
161   emit displayMsg(msg);
162 }
163
164 void CoreSession::recvStatusMsgFromServer(QString msg) {
165   Server *s = qobject_cast<Server*>(sender());
166   Q_ASSERT(s);
167   emit displayStatusMsg(s->getNetwork(), msg);
168 }
169
170
171 QList<BufferId> CoreSession::buffers() const {
172   return storage->requestBuffers(user);
173 }
174
175
176 QVariant CoreSession::sessionState() {
177   VarMap v;
178   QList<QVariant> bufs;
179   foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); }
180   v["Buffers"] = bufs;
181   mutex.lock();
182   v["SessionData"] = sessionData;
183   mutex.unlock();
184   v["Networks"] = QVariant(servers.keys());
185   return v;
186 }
187
188 void CoreSession::sendServerStates() {
189   emit serverStateRequested();
190 }
191
192 void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
193   QList<QVariant> log;
194   QList<Message> msglist;
195   if(v1.type() == QVariant::DateTime) {
196
197
198   } else {
199     msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
200   }
201
202   // Send messages out in smaller packages - we don't want to make the signal data too large!
203   for(int i = 0; i < msglist.count(); i++) {
204     log.append(QVariant::fromValue(msglist[i]));
205     if(log.count() >= 5) {
206       emit backlogData(id, log, i >= msglist.count() - 1);
207       log.clear();
208     }
209   }
210   if(log.count() > 0) emit backlogData(id, log, true);
211 }