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"
34 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
37 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
42 s.beginGroup(QString("SessionData/%1").arg(user));
44 foreach(QString key, s.allKeys()) {
45 sessionData[key] = s.value(key);
49 SignalProxy *p = signalProxy();
51 p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
52 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
53 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
54 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
55 p->attachSignal(this, SIGNAL(displayMsg(Message)));
56 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
57 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
58 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
59 p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
60 p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
61 p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
62 /* Autoconnect. (When) do we actually do this?
63 --> session restore should be enough!
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);
76 CoreSession::~CoreSession() {
79 UserId CoreSession::userId() const {
83 QVariant CoreSession::state() const {
86 foreach(Server *server, servers.values()) {
87 if(server->isConnected()) {
89 m["Network"] = server->networkName();
90 m["State"] = server->state();
94 res["ConnectedServers"] = conn;
98 void CoreSession::restoreState(const QVariant &previousState) {
100 QVariantMap state = previousState.toMap();
101 if(state.contains("ConnectedServers")) {
102 foreach(QVariant v, state["ConnectedServers"].toList()) {
103 QVariantMap m = v.toMap();
104 QString net = m["Network"].toString();
105 if(!net.isEmpty()) connectToNetwork(net, m["State"]);
111 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
113 s.beginGroup(QString("SessionData/%1").arg(user));
115 sessionData[key] = data;
116 s.setValue(key, data);
119 emit sessionDataChanged(key, data);
120 emit sessionDataChanged(key);
123 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
126 if(!sessionData.contains(key)) data = def;
127 else data = sessionData[key];
132 // FIXME switch to NetworkIDs
133 void CoreSession::connectToNetwork(QString network, const QVariant &previousState) {
134 uint networkid = getNetworkId(network);
136 qWarning() << "unable to connect to Network" << network << "(User:" << userId() << "): unable to determine NetworkId";
139 if(!servers.contains(networkid)) {
140 Server *server = new Server(userId(), networkid, network, previousState);
141 servers[networkid] = server;
142 attachServer(server);
145 emit connectToIrc(network);
148 void CoreSession::attachServer(Server *server) {
149 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
150 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
151 connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
153 connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
154 connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
155 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
156 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
158 // connect serversignals to proxy
159 signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
160 signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
161 signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
162 // TODO add error handling
165 void CoreSession::serverStateRequested() {
168 void CoreSession::addClient(QIODevice *device) {
169 signalProxy()->addPeer(device);
172 SignalProxy *CoreSession::signalProxy() const {
176 void CoreSession::serverConnected(uint networkid) {
177 storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
180 void CoreSession::serverDisconnected(uint networkid) {
181 Q_ASSERT(servers.contains(networkid));
182 servers.take(networkid)->deleteLater();
183 Q_ASSERT(!servers.contains(networkid));
186 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
187 emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
190 // ALL messages coming pass through these functions before going to the GUI.
191 // So this is the perfect place for storing the backlog and log stuff.
192 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
193 Server *s = qobject_cast<Server*>(this->sender());
196 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
197 buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
199 buf = storage->getBufferInfo(user, s->networkName(), target);
201 Message msg(buf, type, text, sender, flags);
202 msg.setMsgId(storage->logMessage(msg));
203 Q_ASSERT(msg.msgId());
204 emit displayMsg(msg);
207 void CoreSession::recvStatusMsgFromServer(QString msg) {
208 Server *s = qobject_cast<Server*>(sender());
210 emit displayStatusMsg(s->networkName(), msg);
214 uint CoreSession::getNetworkId(const QString &net) const {
215 return storage->getNetworkId(user, net);
218 QList<BufferInfo> CoreSession::buffers() const {
219 return storage->requestBuffers(user);
223 QVariant CoreSession::sessionState() {
227 foreach(BufferInfo id, storage->requestBuffers(user))
228 bufs.append(QVariant::fromValue(id));
232 v["SessionData"] = sessionData;
235 QVariantList networks;
236 foreach(uint networkid, servers.keys())
237 networks.append(QVariant(networkid));
238 v["Networks"] = QVariant(networks);
240 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
244 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
246 QList<Message> msglist;
247 if(v1.type() == QVariant::DateTime) {
251 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
254 // Send messages out in smaller packages - we don't want to make the signal data too large!
255 for(int i = 0; i < msglist.count(); i++) {
256 log.append(QVariant::fromValue(msglist[i]));
257 if(log.count() >= 5) {
258 emit backlogData(id, log, i >= msglist.count() - 1);
262 if(log.count() > 0) emit backlogData(id, log, true);