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