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"
26 CoreSession::CoreSession(UserId uid, Storage *_storage) : user(uid), storage(_storage) {
27 coreProxy = new CoreProxy();
30 s.beginGroup(QString("SessionData/%1").arg(user));
32 foreach(QString key, s.allKeys()) {
33 sessionData[key] = s.value(key);
37 connect(coreProxy, SIGNAL(send(CoreSignal, QVariant, QVariant, QVariant)), this, SIGNAL(proxySignal(CoreSignal, QVariant, QVariant, QVariant)));
38 connect(coreProxy, SIGNAL(requestServerStates()), this, SIGNAL(serverStateRequested()));
39 connect(coreProxy, SIGNAL(gsRequestConnect(QStringList)), this, SLOT(connectToIrc(QStringList)));
40 connect(coreProxy, SIGNAL(gsUserInput(BufferId, QString)), this, SLOT(msgFromGui(BufferId, QString)));
41 connect(coreProxy, SIGNAL(gsImportBacklog()), storage, SLOT(importOldBacklog()));
42 connect(coreProxy, SIGNAL(gsRequestBacklog(BufferId, QVariant, QVariant)), this, SLOT(sendBacklog(BufferId, QVariant, QVariant)));
43 connect(coreProxy, SIGNAL(gsRequestNetworkStates()), this, SLOT(sendServerStates()));
44 connect(this, SIGNAL(displayMsg(Message)), coreProxy, SLOT(csDisplayMsg(Message)));
45 connect(this, SIGNAL(displayStatusMsg(QString, QString)), coreProxy, SLOT(csDisplayStatusMsg(QString, QString)));
46 connect(this, SIGNAL(backlogData(BufferId, QList<QVariant>, bool)), coreProxy, SLOT(csBacklogData(BufferId, QList<QVariant>, bool)));
47 connect(this, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
48 connect(storage, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
49 connect(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), coreProxy, SLOT(csSessionDataChanged(const QString &, const QVariant &)));
50 connect(coreProxy, SIGNAL(gsSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
53 CoreSession::~CoreSession() {
57 UserId CoreSession::userId() const {
61 void CoreSession::processSignal(ClientSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
62 coreProxy->recv(sig, arg1, arg2, arg3);
65 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
67 s.beginGroup(QString("SessionData/%1").arg(user));
69 sessionData[key] = data;
70 s.setValue(key, data);
73 emit sessionDataChanged(key, data);
74 emit sessionDataChanged(key);
77 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
80 if(!sessionData.contains(key)) data = def;
81 else data = sessionData[key];
86 void CoreSession::connectToIrc(QStringList networks) {
87 foreach(QString net, networks) {
88 if(servers.contains(net)) {
91 Server *server = new Server(userId(), net);
92 connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState()));
93 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
94 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
95 connect(this, SIGNAL(msgFromGui(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
97 connect(server, SIGNAL(connected(QString)), this, SLOT(serverConnected(QString)));
98 connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
100 connect(server, SIGNAL(serverState(QString, VarMap)), coreProxy, SLOT(csServerState(QString, VarMap)));
101 //connect(server, SIGNAL(displayMsg(Message)), this, SLOT(recvMessageFromServer(Message)));
102 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
103 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
104 connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
105 connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
106 connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
107 connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
108 connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
109 connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
110 connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
111 connect(server, SIGNAL(queryRequested(QString, QString)), coreProxy, SLOT(csQueryRequested(QString, QString)));
112 // TODO add error handling
113 connect(server, SIGNAL(connected(QString)), coreProxy, SLOT(csServerConnected(QString)));
114 connect(server, SIGNAL(disconnected(QString)), coreProxy, SLOT(csServerDisconnected(QString)));
117 servers[net] = server;
119 emit connectToIrc(net);
123 void CoreSession::serverConnected(QString net) {
124 storage->getBufferId(userId(), net); // create status buffer
127 void CoreSession::serverDisconnected(QString net) {
130 coreProxy->csServerDisconnected(net);
133 void CoreSession::msgFromGui(BufferId bufid, QString msg) {
134 emit msgFromGui(bufid.network(), bufid.buffer(), msg);
137 // ALL messages coming pass through these functions before going to the GUI.
138 // So this is the perfect place for storing the backlog and log stuff.
140 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
141 Server *s = qobject_cast<Server*>(this->sender());
144 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
145 buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
147 buf = storage->getBufferId(user, s->getNetwork(), target);
149 Message msg(buf, type, text, sender, flags);
150 msg.msgId = storage->logMessage(msg);
152 emit displayMsg(msg);
155 void CoreSession::recvStatusMsgFromServer(QString msg) {
156 Server *s = qobject_cast<Server*>(sender());
158 emit displayStatusMsg(s->getNetwork(), msg);
162 QList<BufferId> CoreSession::buffers() const {
163 return storage->requestBuffers(user);
167 QVariant CoreSession::sessionState() {
169 QList<QVariant> bufs;
170 foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); }
173 v["SessionData"] = sessionData;
175 v["Networks"] = QVariant(servers.keys());
179 void CoreSession::sendServerStates() {
180 emit serverStateRequested();
183 void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
185 QList<Message> msglist;
186 if(v1.type() == QVariant::DateTime) {
190 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
193 // Send messages out in smaller packages - we don't want to make the signal data too large!
194 for(int i = 0; i < msglist.count(); i++) {
195 log.append(QVariant::fromValue(msglist[i]));
196 if(log.count() >= 5) {
197 emit backlogData(id, log, i >= msglist.count() - 1);
201 if(log.count() > 0) emit backlogData(id, log, true);