1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "coresession.h"
24 #include "signalproxy.h"
27 #include "networkinfo.h"
29 #include "ircchannel.h"
36 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
39 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
41 scriptEngine(new QScriptEngine(this))
44 SignalProxy *p = signalProxy();
46 QSettings s; // FIXME don't use QSettings anymore
48 s.beginGroup(QString("SessionData/%1").arg(user));
49 foreach(QString key, s.allKeys()) {
50 sessionData[key] = s.value(key);
53 mutex.unlock(); // FIXME remove
54 /* temporarily disabled
55 s.beginGroup(QString("Identities/%1").arg(user));
56 foreach(QString id, s.childKeys()) {
57 Identity *i = new Identity(s.value(id).value<Identity>(), this);
59 qDebug() << QString("Invalid identity!");
62 if(_identities.contains(i->id())) {
63 qDebug() << "Duplicate identity, ignoring!";
66 qDebug() << "loaded identity" << id;
67 _identities[i->id()] = i;
71 if(!_identities.count()) {
74 //_identities[i->id()] = i;
75 createOrUpdateIdentity(i);
79 p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
80 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
81 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
82 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
83 p->attachSignal(this, SIGNAL(displayMsg(Message)));
84 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
85 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
86 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
87 p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
88 p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
89 p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
91 p->attachSignal(this, SIGNAL(identityCreated(const Identity &)));
92 p->attachSignal(this, SIGNAL(identityRemoved(IdentityId)));
93 p->attachSlot(SIGNAL(createIdentity(const Identity &)), this, SLOT(createOrUpdateIdentity(const Identity &)));
94 p->attachSlot(SIGNAL(updateIdentity(const Identity &)), this, SLOT(createOrUpdateIdentity(const Identity &)));
95 p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
99 foreach(Identity *id, _identities.values()) {
104 CoreSession::~CoreSession() {
107 UserId CoreSession::userId() const {
111 QVariant CoreSession::state() const {
113 QList<QVariant> conn;
114 foreach(Server *server, servers.values()) {
115 if(server->isConnected()) {
117 m["Network"] = server->networkName();
118 m["State"] = server->state();
122 res["ConnectedServers"] = conn;
126 void CoreSession::restoreState(const QVariant &previousState) {
128 QVariantMap state = previousState.toMap();
129 if(state.contains("ConnectedServers")) {
130 foreach(QVariant v, state["ConnectedServers"].toList()) {
131 QVariantMap m = v.toMap();
132 QString net = m["Network"].toString();
133 if(!net.isEmpty()) connectToNetwork(net, m["State"]);
139 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
141 s.beginGroup(QString("SessionData/%1").arg(user));
143 sessionData[key] = data;
144 s.setValue(key, data);
147 emit sessionDataChanged(key, data);
148 emit sessionDataChanged(key);
151 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
154 if(!sessionData.contains(key)) data = def;
155 else data = sessionData[key];
160 // FIXME switch to NetworkIDs
161 void CoreSession::connectToNetwork(QString network, const QVariant &previousState) {
162 uint networkid = getNetworkId(network);
164 qWarning() << "unable to connect to Network" << network << "(User:" << userId() << "): unable to determine NetworkId";
167 if(!servers.contains(networkid)) {
168 Server *server = new Server(userId(), networkid, network, previousState);
169 servers[networkid] = server;
170 attachServer(server);
173 emit connectToIrc(network);
176 void CoreSession::attachServer(Server *server) {
177 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
178 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
179 connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
181 connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
182 connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
183 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
184 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
186 // connect serversignals to proxy
187 signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
188 signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
189 signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
190 // TODO add error handling
193 void CoreSession::serverStateRequested() {
196 void CoreSession::addClient(QIODevice *device) {
197 signalProxy()->addPeer(device);
200 SignalProxy *CoreSession::signalProxy() const {
204 void CoreSession::serverConnected(uint networkid) {
205 storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
208 void CoreSession::serverDisconnected(uint networkid) {
209 Q_ASSERT(servers.contains(networkid));
210 servers.take(networkid)->deleteLater();
211 Q_ASSERT(!servers.contains(networkid));
214 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
215 emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
218 // ALL messages coming pass through these functions before going to the GUI.
219 // So this is the perfect place for storing the backlog and log stuff.
220 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
221 Server *s = qobject_cast<Server*>(this->sender());
224 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
225 buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
227 buf = storage->getBufferInfo(user, s->networkName(), target);
229 Message msg(buf, type, text, sender, flags);
230 msg.setMsgId(storage->logMessage(msg));
231 Q_ASSERT(msg.msgId());
232 emit displayMsg(msg);
235 void CoreSession::recvStatusMsgFromServer(QString msg) {
236 Server *s = qobject_cast<Server*>(sender());
238 emit displayStatusMsg(s->networkName(), msg);
242 uint CoreSession::getNetworkId(const QString &net) const {
243 return storage->getNetworkId(user, net);
246 QList<BufferInfo> CoreSession::buffers() const {
247 return storage->requestBuffers(user);
251 QVariant CoreSession::sessionState() {
255 foreach(BufferInfo id, storage->requestBuffers(user))
256 bufs.append(QVariant::fromValue(id));
260 v["SessionData"] = sessionData;
263 QVariantList networks;
264 foreach(NetworkId networkid, servers.keys())
265 networks.append(QVariant(networkid));
266 v["Networks"] = QVariant(networks);
268 QList<QVariant> idlist;
269 foreach(Identity *i, _identities.values()) idlist << QVariant::fromValue<Identity>(*i);
270 v["Identities"] = idlist;
272 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
276 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
278 QList<Message> msglist;
279 if(v1.type() == QVariant::DateTime) {
283 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
286 // Send messages out in smaller packages - we don't want to make the signal data too large!
287 for(int i = 0; i < msglist.count(); i++) {
288 log.append(QVariant::fromValue(msglist[i]));
289 if(log.count() >= 5) {
290 emit backlogData(id, log, i >= msglist.count() - 1);
294 if(log.count() > 0) emit backlogData(id, log, true);
298 void CoreSession::initScriptEngine() {
299 signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
300 signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
302 QScriptValue storage_ = scriptEngine->newQObject(storage);
303 scriptEngine->globalObject().setProperty("storage", storage_);
306 void CoreSession::scriptRequest(QString script) {
307 emit scriptResult(scriptEngine->evaluate(script).toString());
310 void CoreSession::createOrUpdateIdentity(const Identity &id) {
311 if(!_identities.contains(id.id())) {
313 _identities[id.id()] = new Identity(id, this);
314 signalProxy()->synchronize(_identities[id.id()]);
315 emit identityCreated(id.id());
318 _identities[id.id()]->update(id);
320 QSettings s; // FIXME don't use QSettings
321 s.beginGroup(QString("Identities/%1").arg(user));
322 s.setValue(QString::number(id.id()), QVariant::fromValue<Identity>(*_identities[id.id()]));
326 void CoreSession::removeIdentity(IdentityId id) {
327 Identity *i = _identities.take(id);
329 emit identityRemoved(id);