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"
24 #include "signalproxy.h"
27 #include "synchronizer.h"
28 #include "networkinfo.h"
30 #include "ircchannel.h"
35 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
38 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
43 s.beginGroup(QString("SessionData/%1").arg(user));
45 foreach(QString key, s.allKeys()) {
46 sessionData[key] = s.value(key);
50 SignalProxy *p = signalProxy();
52 p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
53 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
54 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
55 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
56 p->attachSignal(this, SIGNAL(displayMsg(Message)));
57 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
58 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
59 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
60 p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
61 p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
62 p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
63 /* Autoconnect. (When) do we actually do this?
65 QVariantMap networks = retrieveSessionData("Networks").toMap();
66 foreach(QString net, networks.keys()) {
67 if(networks[net].toMap()["AutoConnect"].toBool()) {
71 if(list.count()) connectToIrc(list);
75 CoreSession::~CoreSession() {
78 UserId CoreSession::userId() const {
82 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
84 s.beginGroup(QString("SessionData/%1").arg(user));
86 sessionData[key] = data;
87 s.setValue(key, data);
90 emit sessionDataChanged(key, data);
91 emit sessionDataChanged(key);
94 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
97 if(!sessionData.contains(key)) data = def;
98 else data = sessionData[key];
103 // FIXME switch to NetworkIDs
104 void CoreSession::connectToNetwork(QString network) {
105 uint networkid = getNetworkId(network);
106 if(!servers.contains(networkid)) {
107 Server *server = new Server(userId(), networkid, network);
108 servers[networkid] = server;
109 attachServer(server);
112 emit connectToIrc(network);
115 void CoreSession::attachServer(Server *server) {
116 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
117 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
118 connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
120 connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
121 connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
122 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
123 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
125 // connect serversignals to proxy
126 signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
127 signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
128 signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
129 // TODO add error handling
132 void CoreSession::serverStateRequested() {
135 void CoreSession::addClient(QIODevice *device) {
136 signalProxy()->addPeer(device);
139 SignalProxy *CoreSession::signalProxy() const {
143 void CoreSession::serverConnected(uint networkid) {
144 storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
147 void CoreSession::serverDisconnected(uint networkid) {
148 Q_ASSERT(servers.contains(networkid));
149 servers.take(networkid)->deleteLater();
150 Q_ASSERT(!servers.contains(networkid));
153 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
154 emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
157 // ALL messages coming pass through these functions before going to the GUI.
158 // So this is the perfect place for storing the backlog and log stuff.
159 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
160 Server *s = qobject_cast<Server*>(this->sender());
163 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
164 buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
166 buf = storage->getBufferInfo(user, s->networkName(), target);
168 Message msg(buf, type, text, sender, flags);
169 msg.setMsgId(storage->logMessage(msg));
170 Q_ASSERT(msg.msgId());
171 emit displayMsg(msg);
174 void CoreSession::recvStatusMsgFromServer(QString msg) {
175 Server *s = qobject_cast<Server*>(sender());
177 emit displayStatusMsg(s->networkName(), msg);
181 uint CoreSession::getNetworkId(const QString &net) const {
182 return storage->getNetworkId(user, net);
185 QList<BufferInfo> CoreSession::buffers() const {
186 return storage->requestBuffers(user);
190 QVariant CoreSession::sessionState() {
194 foreach(BufferInfo id, storage->requestBuffers(user))
195 bufs.append(QVariant::fromValue(id));
199 v["SessionData"] = sessionData;
202 QVariantList networks;
203 foreach(uint networkid, servers.keys())
204 networks.append(QVariant(networkid));
205 v["Networks"] = QVariant(networks);
207 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
211 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
213 QList<Message> msglist;
214 if(v1.type() == QVariant::DateTime) {
218 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
221 // Send messages out in smaller packages - we don't want to make the signal data too large!
222 for(int i = 0; i < msglist.count(); i++) {
223 log.append(QVariant::fromValue(msglist[i]));
224 if(log.count() >= 5) {
225 emit backlogData(id, log, i >= msglist.count() - 1);
229 if(log.count() > 0) emit backlogData(id, log, true);