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 &)));
54 /* Autoconnect. (When) do we actually do this?
56 QVariantMap networks = retrieveSessionData("Networks").toMap();
57 foreach(QString net, networks.keys()) {
58 if(networks[net].toMap()["AutoConnect"].toBool()) {
62 if(list.count()) connectToIrc(list);
66 CoreSession::~CoreSession() {
70 UserId CoreSession::userId() const {
74 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
76 s.beginGroup(QString("SessionData/%1").arg(user));
78 sessionData[key] = data;
79 s.setValue(key, data);
82 emit sessionDataChanged(key, data);
83 emit sessionDataChanged(key);
86 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
89 if(!sessionData.contains(key)) data = def;
90 else data = sessionData[key];
95 void CoreSession::connectToNetwork(QString network) {
96 QStringList networks; networks << network; // FIXME obsolete crap
97 foreach(QString net, networks) {
98 if(servers.contains(net)) {
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)));
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)));
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)));
126 servers[net] = server;
128 emit connectToIrc(net);
132 void CoreSession::addClient(QIODevice *device) {
133 signalProxy()->addPeer(device);
136 SignalProxy *CoreSession::signalProxy() const {
140 void CoreSession::serverConnected(QString net) {
141 storage->getBufferId(userId(), net); // create status buffer
144 void CoreSession::serverDisconnected(QString net) {
147 signalProxy()->sendSignal(SIGNAL(networkDisconnected(QString)), net); // FIXME does this work?
150 void CoreSession::msgFromGui(BufferId bufid, QString msg) {
151 emit msgFromGui(bufid.network(), bufid.buffer(), msg);
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.
157 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
158 Server *s = qobject_cast<Server*>(this->sender());
161 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
162 buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
164 buf = storage->getBufferId(user, s->getNetwork(), target);
166 Message msg(buf, type, text, sender, flags);
167 msg.msgId = storage->logMessage(msg);
169 emit displayMsg(msg);
172 void CoreSession::recvStatusMsgFromServer(QString msg) {
173 Server *s = qobject_cast<Server*>(sender());
175 emit displayStatusMsg(s->getNetwork(), msg);
179 QList<BufferId> CoreSession::buffers() const {
180 return storage->requestBuffers(user);
184 QVariant CoreSession::sessionState() {
186 QList<QVariant> bufs;
187 foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); }
190 v["SessionData"] = sessionData;
192 v["Networks"] = QVariant(servers.keys());
193 // v["Payload"] = QByteArray(100000000, 'a');
197 void CoreSession::sendServerStates() {
198 emit serverStateRequested();
201 void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
203 QList<Message> msglist;
204 if(v1.type() == QVariant::DateTime) {
208 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
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);
219 if(log.count() > 0) emit backlogData(id, log, true);