First check-in of the Qtopia build system. See qtopia-build/README.
[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
54   /* Autoconnect. (When) do we actually do this?
55   QStringList list;
56   QVariantMap networks = retrieveSessionData("Networks").toMap();
57   foreach(QString net, networks.keys()) {
58     if(networks[net].toMap()["AutoConnect"].toBool()) {
59       list << net;
60     }
61   } qDebug() << list;
62   if(list.count()) connectToIrc(list);
63   */
64 }
65
66 CoreSession::~CoreSession() {
67
68 }
69
70 UserId CoreSession::userId() const {
71   return user;
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::connectToNetwork(QString network) {
96   QStringList networks; networks << network; // FIXME obsolete crap
97   foreach(QString net, networks) {
98     if(servers.contains(net)) {
99
100     } else {
101       Server *server = new Server(userId(), net);
102       connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState()));
103       connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
104       connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
105       connect(this, SIGNAL(msgFromGui(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
106
107       connect(server, SIGNAL(connected(QString)), this, SLOT(serverConnected(QString)));
108       connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
109       connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
110       connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
111
112       SignalProxy *p = signalProxy();
113       p->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
114       p->attachSignal(server, SIGNAL(modeSet(QString, QString, QString)));
115       p->attachSignal(server, SIGNAL(nickAdded(QString, QString, QVariantMap)));
116       p->attachSignal(server, SIGNAL(nickRenamed(QString, QString, QString)));
117       p->attachSignal(server, SIGNAL(nickRemoved(QString, QString)));
118       p->attachSignal(server, SIGNAL(nickUpdated(QString, QString, QVariantMap)));
119       p->attachSignal(server, SIGNAL(ownNickSet(QString, QString)));
120       p->attachSignal(server, SIGNAL(queryRequested(QString, QString)));
121       // TODO add error handling
122       p->attachSignal(server, SIGNAL(connected(QString)), SIGNAL(networkConnected(QString)));
123       p->attachSignal(server, SIGNAL(disconnected(QString)), SIGNAL(networkDisconnected(QString)));
124
125       server->start();
126       servers[net] = server;
127     }
128     emit connectToIrc(net);
129   }
130 }
131
132 void CoreSession::addClient(QIODevice *device) {
133   signalProxy()->addPeer(device);
134 }
135
136 SignalProxy *CoreSession::signalProxy() const {
137   return _signalProxy;
138 }
139
140 void CoreSession::serverConnected(QString net) {
141   storage->getBufferId(userId(), net); // create status buffer
142 }
143
144 void CoreSession::serverDisconnected(QString net) {
145   delete servers[net];
146   servers.remove(net);
147   signalProxy()->sendSignal(SIGNAL(networkDisconnected(QString)), net);  // FIXME does this work?
148 }
149
150 void CoreSession::msgFromGui(BufferId bufid, QString msg) {
151   emit msgFromGui(bufid.network(), bufid.buffer(), msg);
152 }
153
154 // ALL messages coming pass through these functions before going to the GUI.
155 // So this is the perfect place for storing the backlog and log stuff.
156
157 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
158   Server *s = qobject_cast<Server*>(this->sender());
159   Q_ASSERT(s);
160   BufferId buf;
161   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
162     buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
163   } else {
164     buf = storage->getBufferId(user, s->getNetwork(), target);
165   }
166   Message msg(buf, type, text, sender, flags);
167   msg.msgId = storage->logMessage(msg);
168   Q_ASSERT(msg.msgId);
169   emit displayMsg(msg);
170 }
171
172 void CoreSession::recvStatusMsgFromServer(QString msg) {
173   Server *s = qobject_cast<Server*>(sender());
174   Q_ASSERT(s);
175   emit displayStatusMsg(s->getNetwork(), msg);
176 }
177
178
179 QList<BufferId> CoreSession::buffers() const {
180   return storage->requestBuffers(user);
181 }
182
183
184 QVariant CoreSession::sessionState() {
185   QVariantMap v;
186   QList<QVariant> bufs;
187   foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); }
188   v["Buffers"] = bufs;
189   mutex.lock();
190   v["SessionData"] = sessionData;
191   mutex.unlock();
192   v["Networks"] = QVariant(servers.keys());
193   // v["Payload"] = QByteArray(100000000, 'a');
194   return v;
195 }
196
197 void CoreSession::sendServerStates() {
198   emit serverStateRequested();
199 }
200
201 void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
202   QList<QVariant> log;
203   QList<Message> msglist;
204   if(v1.type() == QVariant::DateTime) {
205
206
207   } else {
208     msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
209   }
210
211   // Send messages out in smaller packages - we don't want to make the signal data too large!
212   for(int i = 0; i < msglist.count(); i++) {
213     log.append(QVariant::fromValue(msglist[i]));
214     if(log.count() >= 5) {
215       emit backlogData(id, log, i >= msglist.count() - 1);
216       log.clear();
217     }
218   }
219   if(log.count() > 0) emit backlogData(id, log, true);
220 }