1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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"
33 #include "coreusersettings.h"
37 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
40 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
42 scriptEngine(new QScriptEngine(this))
45 SignalProxy *p = signalProxy();
47 CoreUserSettings s(user);
48 sessionData = s.sessionData();
51 foreach(QString id, cs.localChildKeys(QString("Identities/%1").arg(user))) {
52 Identity *i = new Identity(cs.localValue(QString("Identities/%1/%2").arg(user).arg(id)).value<Identity>(), this);
54 qDebug() << QString("Invalid identity!");
57 if(_identities.contains(i->id())) {
58 qDebug() << "Duplicate identity, ignoring!";
61 qDebug() << "loaded identity" << id;
62 _identities[i->id()] = i;
66 if(!_identities.count()) {
69 //_identities[i->id()] = i;
70 createOrUpdateIdentity(i);
74 p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
75 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
76 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
77 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
78 p->attachSignal(this, SIGNAL(displayMsg(Message)));
79 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
80 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
81 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
82 p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
83 p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
84 p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
86 p->attachSignal(this, SIGNAL(identityCreated(const Identity &)));
87 p->attachSignal(this, SIGNAL(identityRemoved(IdentityId)));
88 p->attachSlot(SIGNAL(createIdentity(const Identity &)), this, SLOT(createOrUpdateIdentity(const Identity &)));
89 p->attachSlot(SIGNAL(updateIdentity(const Identity &)), this, SLOT(createOrUpdateIdentity(const Identity &)));
90 p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
94 foreach(Identity *id, _identities.values()) {
99 CoreSession::~CoreSession() {
102 UserId CoreSession::userId() const {
106 QVariant CoreSession::state() const {
108 QList<QVariant> conn;
109 foreach(Server *server, servers.values()) {
110 if(server->isConnected()) {
112 m["Network"] = server->networkName();
113 m["State"] = server->state();
117 res["ConnectedServers"] = conn;
121 void CoreSession::restoreState(const QVariant &previousState) {
123 QVariantMap state = previousState.toMap();
124 if(state.contains("ConnectedServers")) {
125 foreach(QVariant v, state["ConnectedServers"].toList()) {
126 QVariantMap m = v.toMap();
127 QString net = m["Network"].toString();
128 if(!net.isEmpty()) connectToNetwork(net, m["State"]);
134 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
135 CoreUserSettings s(user);
137 s.setSessionValue(key, data);
138 sessionData[key] = data;
140 emit sessionDataChanged(key, data);
141 emit sessionDataChanged(key);
144 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
147 if(!sessionData.contains(key)) data = def;
148 else data = sessionData[key];
153 // FIXME switch to NetworkIDs
154 void CoreSession::connectToNetwork(QString network, const QVariant &previousState) {
155 uint networkid = getNetworkId(network);
157 qWarning() << "unable to connect to Network" << network << "(User:" << userId() << "): unable to determine NetworkId";
160 if(!servers.contains(networkid)) {
161 Server *server = new Server(userId(), networkid, network, previousState);
162 servers[networkid] = server;
163 attachServer(server);
166 emit connectToIrc(network);
169 void CoreSession::attachServer(Server *server) {
170 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
171 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
172 connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
174 connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
175 connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
176 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
177 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
179 // connect serversignals to proxy
180 signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
181 signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
182 signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
183 // TODO add error handling
186 void CoreSession::serverStateRequested() {
189 void CoreSession::addClient(QIODevice *device) {
190 signalProxy()->addPeer(device);
193 SignalProxy *CoreSession::signalProxy() const {
197 void CoreSession::serverConnected(uint networkid) {
198 storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
201 void CoreSession::serverDisconnected(uint networkid) {
202 Q_ASSERT(servers.contains(networkid));
203 servers.take(networkid)->deleteLater();
204 Q_ASSERT(!servers.contains(networkid));
207 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
208 emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
211 // ALL messages coming pass through these functions before going to the GUI.
212 // So this is the perfect place for storing the backlog and log stuff.
213 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
214 Server *s = qobject_cast<Server*>(this->sender());
217 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
218 buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
220 buf = storage->getBufferInfo(user, s->networkName(), target);
222 Message msg(buf, type, text, sender, flags);
223 msg.setMsgId(storage->logMessage(msg));
224 Q_ASSERT(msg.msgId());
225 emit displayMsg(msg);
228 void CoreSession::recvStatusMsgFromServer(QString msg) {
229 Server *s = qobject_cast<Server*>(sender());
231 emit displayStatusMsg(s->networkName(), msg);
235 uint CoreSession::getNetworkId(const QString &net) const {
236 return storage->getNetworkId(user, net);
239 QList<BufferInfo> CoreSession::buffers() const {
240 return storage->requestBuffers(user);
244 QVariant CoreSession::sessionState() {
248 foreach(BufferInfo id, storage->requestBuffers(user))
249 bufs.append(QVariant::fromValue(id));
253 v["SessionData"] = sessionData;
256 QVariantList networks;
257 foreach(NetworkId networkid, servers.keys())
258 networks.append(QVariant(networkid));
259 v["Networks"] = QVariant(networks);
261 QList<QVariant> idlist;
262 foreach(Identity *i, _identities.values()) idlist << QVariant::fromValue<Identity>(*i);
263 v["Identities"] = idlist;
265 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
269 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
271 QList<Message> msglist;
272 if(v1.type() == QVariant::DateTime) {
276 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
279 // Send messages out in smaller packages - we don't want to make the signal data too large!
280 for(int i = 0; i < msglist.count(); i++) {
281 log.append(QVariant::fromValue(msglist[i]));
282 if(log.count() >= 5) {
283 emit backlogData(id, log, i >= msglist.count() - 1);
287 if(log.count() > 0) emit backlogData(id, log, true);
291 void CoreSession::initScriptEngine() {
292 signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
293 signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
295 QScriptValue storage_ = scriptEngine->newQObject(storage);
296 scriptEngine->globalObject().setProperty("storage", storage_);
299 void CoreSession::scriptRequest(QString script) {
300 emit scriptResult(scriptEngine->evaluate(script).toString());
303 void CoreSession::createOrUpdateIdentity(const Identity &id) {
304 if(!_identities.contains(id.id())) {
306 _identities[id.id()] = new Identity(id, this);
307 signalProxy()->synchronize(_identities[id.id()]);
308 emit identityCreated(id.id());
311 _identities[id.id()]->update(id);
315 s.beginGroup(QString("Identities/%1").arg(user));
316 s.setValue(QString::number(id.id()), QVariant::fromValue<Identity>(*_identities[id.id()]));
320 void CoreSession::removeIdentity(IdentityId id) {
321 Identity *i = _identities.take(id);
323 emit identityRemoved(id);