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"
35 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
38 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
40 scriptEngine(new QScriptEngine(this))
44 s.beginGroup(QString("SessionData/%1").arg(user));
46 foreach(QString key, s.allKeys()) {
47 sessionData[key] = s.value(key);
51 SignalProxy *p = signalProxy();
53 p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
54 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
55 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
56 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
57 p->attachSignal(this, SIGNAL(displayMsg(Message)));
58 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
59 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
60 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
61 p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
62 p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
63 p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
64 /* Autoconnect. (When) do we actually do this?
65 --> session restore should be enough!
67 QVariantMap networks = retrieveSessionData("Networks").toMap();
68 foreach(QString net, networks.keys()) {
69 if(networks[net].toMap()["AutoConnect"].toBool()) {
73 if(list.count()) connectToIrc(list);
79 CoreSession::~CoreSession() {
82 UserId CoreSession::userId() const {
86 QVariant CoreSession::state() const {
89 foreach(Server *server, servers.values()) {
90 if(server->isConnected()) {
92 m["Network"] = server->networkName();
93 m["State"] = server->state();
97 res["ConnectedServers"] = conn;
101 void CoreSession::restoreState(const QVariant &previousState) {
103 QVariantMap state = previousState.toMap();
104 if(state.contains("ConnectedServers")) {
105 foreach(QVariant v, state["ConnectedServers"].toList()) {
106 QVariantMap m = v.toMap();
107 QString net = m["Network"].toString();
108 if(!net.isEmpty()) connectToNetwork(net, m["State"]);
114 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
116 s.beginGroup(QString("SessionData/%1").arg(user));
118 sessionData[key] = data;
119 s.setValue(key, data);
122 emit sessionDataChanged(key, data);
123 emit sessionDataChanged(key);
126 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
129 if(!sessionData.contains(key)) data = def;
130 else data = sessionData[key];
135 // FIXME switch to NetworkIDs
136 void CoreSession::connectToNetwork(QString network, const QVariant &previousState) {
137 uint networkid = getNetworkId(network);
139 qWarning() << "unable to connect to Network" << network << "(User:" << userId() << "): unable to determine NetworkId";
142 if(!servers.contains(networkid)) {
143 Server *server = new Server(userId(), networkid, network, previousState);
144 servers[networkid] = server;
145 attachServer(server);
148 emit connectToIrc(network);
151 void CoreSession::attachServer(Server *server) {
152 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
153 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
154 connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
156 connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
157 connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
158 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
159 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
161 // connect serversignals to proxy
162 signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
163 signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
164 signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
165 // TODO add error handling
168 void CoreSession::serverStateRequested() {
171 void CoreSession::addClient(QIODevice *device) {
172 signalProxy()->addPeer(device);
175 SignalProxy *CoreSession::signalProxy() const {
179 void CoreSession::serverConnected(uint networkid) {
180 storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
183 void CoreSession::serverDisconnected(uint networkid) {
184 Q_ASSERT(servers.contains(networkid));
185 servers.take(networkid)->deleteLater();
186 Q_ASSERT(!servers.contains(networkid));
189 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
190 emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
193 // ALL messages coming pass through these functions before going to the GUI.
194 // So this is the perfect place for storing the backlog and log stuff.
195 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
196 Server *s = qobject_cast<Server*>(this->sender());
199 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
200 buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
202 buf = storage->getBufferInfo(user, s->networkName(), target);
204 Message msg(buf, type, text, sender, flags);
205 msg.setMsgId(storage->logMessage(msg));
206 Q_ASSERT(msg.msgId());
207 emit displayMsg(msg);
210 void CoreSession::recvStatusMsgFromServer(QString msg) {
211 Server *s = qobject_cast<Server*>(sender());
213 emit displayStatusMsg(s->networkName(), msg);
217 uint CoreSession::getNetworkId(const QString &net) const {
218 return storage->getNetworkId(user, net);
221 QList<BufferInfo> CoreSession::buffers() const {
222 return storage->requestBuffers(user);
226 QVariant CoreSession::sessionState() {
230 foreach(BufferInfo id, storage->requestBuffers(user))
231 bufs.append(QVariant::fromValue(id));
235 v["SessionData"] = sessionData;
238 QVariantList networks;
239 foreach(uint networkid, servers.keys())
240 networks.append(QVariant(networkid));
241 v["Networks"] = QVariant(networks);
243 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
247 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
249 QList<Message> msglist;
250 if(v1.type() == QVariant::DateTime) {
254 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
257 // Send messages out in smaller packages - we don't want to make the signal data too large!
258 for(int i = 0; i < msglist.count(); i++) {
259 log.append(QVariant::fromValue(msglist[i]));
260 if(log.count() >= 5) {
261 emit backlogData(id, log, i >= msglist.count() - 1);
265 if(log.count() > 0) emit backlogData(id, log, true);
269 void CoreSession::initScriptEngine() {
270 signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
271 signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
273 QScriptValue storage_ = scriptEngine->newQObject(storage);
274 scriptEngine->globalObject().setProperty("storage", storage_);
277 void CoreSession::scriptRequest(QString script) {
278 emit scriptResult(scriptEngine->evaluate(script).toString());