1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel IRC Development 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) any later version. *
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"
23 #include "signalproxy.h"
27 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent) : QObject(parent), user(uid), storage(_storage) {
28 _signalProxy = new SignalProxy(SignalProxy::Server, 0, this);
31 s.beginGroup(QString("SessionData/%1").arg(user));
33 foreach(QString key, s.allKeys()) {
34 sessionData[key] = s.value(key);
38 SignalProxy *p = signalProxy();
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 /* Autoconnect. (When) do we actually do this?
55 QVariantMap networks = retrieveSessionData("Networks").toMap();
56 foreach(QString net, networks.keys()) {
57 if(networks[net].toMap()["AutoConnect"].toBool()) {
61 if(list.count()) connectToIrc(list);
65 CoreSession::~CoreSession() {
69 UserId CoreSession::userId() const {
73 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
75 s.beginGroup(QString("SessionData/%1").arg(user));
77 sessionData[key] = data;
78 s.setValue(key, data);
81 emit sessionDataChanged(key, data);
82 emit sessionDataChanged(key);
85 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
88 if(!sessionData.contains(key)) data = def;
89 else data = sessionData[key];
94 void CoreSession::connectToNetwork(QString network) {
95 QStringList networks; networks << network; // FIXME obsolete crap
96 foreach(QString net, networks) {
97 if(servers.contains(net)) {
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)));
106 connect(server, SIGNAL(connected(QString)), this, SLOT(serverConnected(QString)));
107 connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
108 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
109 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
111 SignalProxy *p = signalProxy();
112 p->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
113 p->attachSignal(server, SIGNAL(modeSet(QString, QString, QString)));
114 p->attachSignal(server, SIGNAL(nickAdded(QString, QString, QVariantMap)));
115 p->attachSignal(server, SIGNAL(nickRenamed(QString, QString, QString)));
116 p->attachSignal(server, SIGNAL(nickRemoved(QString, QString)));
117 p->attachSignal(server, SIGNAL(nickUpdated(QString, QString, QVariantMap)));
118 p->attachSignal(server, SIGNAL(ownNickSet(QString, QString)));
119 p->attachSignal(server, SIGNAL(queryRequested(QString, QString)));
120 // TODO add error handling
121 p->attachSignal(server, SIGNAL(connected(QString)), SIGNAL(networkConnected(QString)));
122 p->attachSignal(server, SIGNAL(disconnected(QString)), SIGNAL(networkDisconnected(QString)));
125 servers[net] = server;
127 emit connectToIrc(net);
131 void CoreSession::addClient(QIODevice *device) {
132 signalProxy()->addPeer(device);
135 SignalProxy *CoreSession::signalProxy() const {
139 void CoreSession::serverConnected(QString net) {
140 storage->getBufferId(userId(), net); // create status buffer
143 void CoreSession::serverDisconnected(QString net) {
146 signalProxy()->sendSignal(SIGNAL(networkDisconnected(QString)), net); // FIXME does this work?
149 void CoreSession::msgFromGui(BufferId bufid, QString msg) {
150 emit msgFromGui(bufid.network(), bufid.buffer(), msg);
153 // ALL messages coming pass through these functions before going to the GUI.
154 // So this is the perfect place for storing the backlog and log stuff.
156 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
157 Server *s = qobject_cast<Server*>(this->sender());
160 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
161 buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
163 buf = storage->getBufferId(user, s->getNetwork(), target);
165 Message msg(buf, type, text, sender, flags);
166 msg.setMsgId(storage->logMessage(msg));
167 Q_ASSERT(msg.msgId());
168 emit displayMsg(msg);
171 void CoreSession::recvStatusMsgFromServer(QString msg) {
172 Server *s = qobject_cast<Server*>(sender());
174 emit displayStatusMsg(s->getNetwork(), msg);
178 QList<BufferId> CoreSession::buffers() const {
179 return storage->requestBuffers(user);
183 QVariant CoreSession::sessionState() {
185 QList<QVariant> bufs;
186 foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); }
189 v["SessionData"] = sessionData;
191 v["Networks"] = QVariant(servers.keys());
192 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
196 void CoreSession::sendServerStates() {
197 emit serverStateRequested();
200 void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
202 QList<Message> msglist;
203 if(v1.type() == QVariant::DateTime) {
207 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
210 // Send messages out in smaller packages - we don't want to make the signal data too large!
211 for(int i = 0; i < msglist.count(); i++) {
212 log.append(QVariant::fromValue(msglist[i]));
213 if(log.count() >= 5) {
214 emit backlogData(id, log, i >= msglist.count() - 1);
218 if(log.count() > 0) emit backlogData(id, log, true);