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 ***************************************************************************/
22 #include "coresession.h"
23 #include "networkconnection.h"
25 #include "signalproxy.h"
30 #include "ircchannel.h"
34 #include "coreusersettings.h"
38 CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent) : QObject(parent),
40 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
41 scriptEngine(new QScriptEngine(this))
44 SignalProxy *p = signalProxy();
46 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
47 p->attachSlot(SIGNAL(disconnectFromNetwork(NetworkId)), this, SLOT(disconnectFromNetwork(NetworkId))); // FIXME
48 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromClient(BufferInfo, QString)));
49 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
50 p->attachSignal(this, SIGNAL(displayMsg(Message)));
51 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
52 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
53 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
55 p->attachSignal(this, SIGNAL(identityCreated(const Identity &)));
56 p->attachSignal(this, SIGNAL(identityRemoved(IdentityId)));
57 p->attachSlot(SIGNAL(createIdentity(const Identity &)), this, SLOT(createIdentity(const Identity &)));
58 p->attachSlot(SIGNAL(updateIdentity(const Identity &)), this, SLOT(updateIdentity(const Identity &)));
59 p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
61 p->attachSignal(this, SIGNAL(networkCreated(NetworkId)));
62 p->attachSignal(this, SIGNAL(networkRemoved(NetworkId)));
63 p->attachSlot(SIGNAL(createNetwork(const NetworkInfo &)), this, SLOT(createNetwork(const NetworkInfo &)));
64 p->attachSlot(SIGNAL(updateNetwork(const NetworkInfo &)), this, SLOT(updateNetwork(const NetworkInfo &)));
65 p->attachSlot(SIGNAL(removeNetwork(NetworkId)), this, SLOT(removeNetwork(NetworkId)));
70 // Restore session state
71 if(restoreState) restoreSessionState();
76 CoreSession::~CoreSession() {
78 foreach(NetworkConnection *conn, _connections.values()) {
81 foreach(Network *net, _networks.values()) {
86 UserId CoreSession::user() const {
90 Network *CoreSession::network(NetworkId id) const {
91 if(_networks.contains(id)) return _networks[id];
95 NetworkConnection *CoreSession::networkConnection(NetworkId id) const {
96 if(_connections.contains(id)) return _connections[id];
100 Identity *CoreSession::identity(IdentityId id) const {
101 if(_identities.contains(id)) return _identities[id];
105 void CoreSession::loadSettings() {
106 CoreUserSettings s(user());
108 foreach(IdentityId id, s.identityIds()) {
109 Identity *i = new Identity(s.identity(id), this);
111 qWarning() << QString("Invalid identity! Removing...");
112 s.removeIdentity(id);
116 if(_identities.contains(i->id())) {
117 qWarning() << "Duplicate identity, ignoring!";
121 _identities[i->id()] = i;
122 signalProxy()->synchronize(i);
124 if(!_identities.count()) {
127 i.setIdentityName(tr("Default Identity"));
131 // FIXME switch to a pure DB storage
132 foreach(NetworkId id, s.networkIds()) {
133 NetworkInfo info = s.networkInfo(id);
137 // FIXME Migrate old settings if available...
138 if(!_networks.count()) {
139 QVariantMap networks = s.sessionValue("Networks").toMap();
140 if(networks.keys().count()) {
141 qWarning() << "Migrating your old network settings to the new format!";
142 foreach(QString netname, networks.keys()) {
143 QVariantMap network = networks[netname].toMap();
144 NetworkId netid = Core::networkId(user(), netname);
146 info.networkId = netid;
147 info.networkName = netname;
149 info.codecForEncoding = "ISO-8859-15";
150 info.codecForDecoding = "ISO-8859-15";
152 foreach(QVariant v, network["Servers"].toList()) {
154 server["Host"] = v.toMap()["Address"];
155 server["Port"] = v.toMap()["Port"];
158 info.serverList = slist;
165 void CoreSession::saveSessionState() const {
168 foreach(NetworkConnection *net, _connections.values()) {
170 m["NetworkId"] = QVariant::fromValue<NetworkId>(net->networkId());
171 m["State"] = net->state();
174 res["CoreBuild"] = Global::quasselBuild;
175 res["ConnectedNetworks"] = conn;
176 CoreUserSettings s(user());
177 s.setSessionState(res);
180 void CoreSession::restoreSessionState() {
181 CoreUserSettings s(user());
182 uint build = s.sessionState().toMap()["CoreBuild"].toUInt();
184 qWarning() << qPrintable(tr("Session state does not exist or is too old!"));
187 QVariantList conn = s.sessionState().toMap()["ConnectedNetworks"].toList();
188 foreach(QVariant v, conn) {
189 NetworkId id = v.toMap()["NetworkId"].value<NetworkId>();
190 if(_networks.keys().contains(id)) connectToNetwork(id, v.toMap()["State"]);
194 void CoreSession::updateBufferInfo(UserId uid, const BufferInfo &bufinfo) {
195 if(uid == user()) emit bufferInfoUpdated(bufinfo);
199 void CoreSession::connectToNetwork(QString netname, const QVariant &previousState) {
201 foreach(Network *n, _networks.values()) {
202 if(n->networkName() == netname) {
207 qWarning() << "Connect to unknown network requested, ignoring!";
210 connectToNetwork(net->networkId(), previousState);
213 void CoreSession::connectToNetwork(NetworkId id, const QVariant &previousState) {
214 Network *net = network(id);
216 qWarning() << "Connect to unknown network requested! net:" << id << "user:" << user();
220 NetworkConnection *conn = networkConnection(id);
222 conn = new NetworkConnection(net, this, previousState);
223 _connections[id] = conn;
224 attachNetworkConnection(conn);
226 conn->connectToIrc();
229 void CoreSession::attachNetworkConnection(NetworkConnection *conn) {
230 connect(conn, SIGNAL(connected(NetworkId)), this, SLOT(networkConnected(NetworkId)));
231 connect(conn, SIGNAL(disconnected(NetworkId)), this, SLOT(networkDisconnected(NetworkId)));
233 // I guess we don't need these anymore, client-side can just connect the network's signals directly
234 //signalProxy()->attachSignal(conn, SIGNAL(connected(NetworkId)), SIGNAL(networkConnected(NetworkId)));
235 //signalProxy()->attachSignal(conn, SIGNAL(disconnected(NetworkId)), SIGNAL(networkDisconnected(NetworkId)));
237 connect(conn, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
238 connect(conn, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
242 void CoreSession::disconnectFromNetwork(NetworkId id) {
243 if(!_connections.contains(id)) return;
244 _connections[id]->disconnectFromIrc();
247 void CoreSession::networkStateRequested() {
250 void CoreSession::addClient(QObject *dev) { // this is QObject* so we can use it in signal connections
251 QIODevice *device = qobject_cast<QIODevice *>(dev);
253 qWarning() << "Invoking CoreSession::addClient with a QObject that is not a QIODevice!";
255 signalProxy()->addPeer(device);
257 reply["MsgType"] = "SessionInit";
258 reply["SessionState"] = sessionState();
259 SignalProxy::writeDataToDevice(device, reply);
263 SignalProxy *CoreSession::signalProxy() const {
267 // FIXME we need a sane way for creating buffers!
268 void CoreSession::networkConnected(NetworkId networkid) {
269 Core::bufferInfo(user(), networkid); // create status buffer
272 void CoreSession::networkDisconnected(NetworkId networkid) {
274 // connection should only go away on explicit /part, and handle reconnections etcpp internally otherwise
276 Q_ASSERT(_connections.contains(networkid));
277 _connections.take(networkid)->deleteLater();
280 // FIXME switch to BufferId
281 void CoreSession::msgFromClient(BufferInfo bufinfo, QString msg) {
282 NetworkConnection *conn = networkConnection(bufinfo.networkId());
284 conn->userInput(bufinfo.buffer(), msg);
286 qWarning() << "Trying to send to unconnected network!";
290 // ALL messages coming pass through these functions before going to the GUI.
291 // So this is the perfect place for storing the backlog and log stuff.
292 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
293 NetworkConnection *netCon = qobject_cast<NetworkConnection*>(this->sender());
297 if((flags & Message::PrivMsg) && !(flags & Message::Self))
298 bufferName = nickFromMask(sender);
302 BufferInfo bufferInfo = Core::bufferInfo(user(), netCon->networkId(), bufferName);
303 Message msg(bufferInfo, type, text, sender, flags);
304 msg.setMsgId(Core::storeMessage(msg));
305 Q_ASSERT(msg.msgId() != 0);
306 emit displayMsg(msg);
309 void CoreSession::recvStatusMsgFromServer(QString msg) {
310 NetworkConnection *s = qobject_cast<NetworkConnection*>(sender());
312 emit displayStatusMsg(s->networkName(), msg);
315 QList<BufferInfo> CoreSession::buffers() const {
316 return Core::requestBuffers(user());
320 QVariant CoreSession::sessionState() {
324 foreach(BufferInfo id, buffers()) bufs << qVariantFromValue(id);
325 v["BufferInfos"] = bufs;
326 QVariantList networkids;
327 foreach(NetworkId id, _networks.keys()) networkids << qVariantFromValue(id);
328 v["NetworkIds"] = networkids;
330 quint32 ircusercount = 0;
331 quint32 ircchannelcount = 0;
332 foreach(Network *net, _networks.values()) {
333 ircusercount += net->ircUserCount();
334 ircchannelcount += net->ircChannelCount();
336 v["IrcUserCount"] = ircusercount;
337 v["IrcChannelCount"] = ircchannelcount;
339 QList<QVariant> idlist;
340 foreach(Identity *i, _identities.values()) idlist << qVariantFromValue(*i);
341 v["Identities"] = idlist;
343 //v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
347 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
349 QList<Message> msglist;
350 if(v1.type() == QVariant::DateTime) {
354 msglist = Core::requestMsgs(id, v1.toInt(), v2.toInt());
357 // Send messages out in smaller packages - we don't want to make the signal data too large!
358 for(int i = 0; i < msglist.count(); i++) {
359 log.append(qVariantFromValue(msglist[i]));
360 if(log.count() >= 5) {
361 emit backlogData(id, log, i >= msglist.count() - 1);
365 if(log.count() > 0) emit backlogData(id, log, true);
369 void CoreSession::initScriptEngine() {
370 signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
371 signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
374 //QScriptValue storage_ = scriptEngine->newQObject(storage);
375 //scriptEngine->globalObject().setProperty("storage", storage_);
378 void CoreSession::scriptRequest(QString script) {
379 emit scriptResult(scriptEngine->evaluate(script).toString());
382 /*** Identity Handling ***/
384 void CoreSession::createIdentity(const Identity &id) {
387 for(i = 1; i <= _identities.count(); i++) {
388 if(!_identities.keys().contains(i)) break;
390 //qDebug() << "found free id" << i;
391 Identity *newId = new Identity(id, this);
393 _identities[i] = newId;
394 signalProxy()->synchronize(newId);
395 CoreUserSettings s(user());
396 s.storeIdentity(*newId);
397 emit identityCreated(*newId);
400 void CoreSession::updateIdentity(const Identity &id) {
401 if(!_identities.contains(id.id())) {
402 qWarning() << "Update request for unknown identity received!";
405 _identities[id.id()]->update(id);
407 CoreUserSettings s(user());
411 void CoreSession::removeIdentity(IdentityId id) {
412 Identity *i = _identities.take(id);
414 emit identityRemoved(id);
415 CoreUserSettings s(user());
416 s.removeIdentity(id);
421 /*** Network Handling ***/
423 void CoreSession::createNetwork(const NetworkInfo &info_) {
424 NetworkInfo info = info_;
427 if(!info.networkId.isValid())
428 Core::createNetworkId(user(), info);
430 Q_ASSERT(info.networkId.isValid());
432 id = info.networkId.toInt();
433 Q_ASSERT(!_networks.contains(id));
435 Network *net = new Network(id, this);
436 connect(net, SIGNAL(connectRequested(NetworkId)), this, SLOT(connectToNetwork(NetworkId)));
437 connect(net, SIGNAL(disconnectRequested(NetworkId)), this, SLOT(disconnectFromNetwork(NetworkId)));
438 net->setNetworkInfo(info);
439 net->setProxy(signalProxy());
441 signalProxy()->synchronize(net);
442 CoreUserSettings s(user());
443 s.storeNetworkInfo(info);
444 emit networkCreated(id);
447 void CoreSession::updateNetwork(const NetworkInfo &info) {
448 if(!_networks.contains(info.networkId)) {
449 qWarning() << "Update request for unknown network received!";
452 _networks[info.networkId]->setNetworkInfo(info);
453 CoreUserSettings s(user());
454 s.storeNetworkInfo(info);
457 void CoreSession::removeNetwork(NetworkId id) {
458 Network *net = _networks.take(id);
460 emit networkRemoved(id);
461 CoreUserSettings s(user());
462 s.removeNetworkInfo(id);