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, QObject *parent) : QObject(parent),
40 _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
41 scriptEngine(new QScriptEngine(this))
44 SignalProxy *p = signalProxy();
46 CoreUserSettings s(user());
47 sessionData = s.sessionData();
49 foreach(IdentityId id, s.identityIds()) {
50 Identity *i = new Identity(s.identity(id), this);
52 qWarning() << QString("Invalid identity! Removing...");
57 if(_identities.contains(i->id())) {
58 qWarning() << "Duplicate identity, ignoring!";
62 _identities[i->id()] = i;
64 if(!_identities.count()) {
67 i.setIdentityName(tr("Default Identity"));
71 //p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(networkStateRequested()));
72 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
73 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromClient(BufferInfo, QString)));
74 p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
75 p->attachSignal(this, SIGNAL(displayMsg(Message)));
76 p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
77 p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
78 p->attachSignal(this, SIGNAL(bufferInfoUpdated(BufferInfo)));
80 p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
81 p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
83 p->attachSignal(this, SIGNAL(identityCreated(const Identity &)));
84 p->attachSignal(this, SIGNAL(identityRemoved(IdentityId)));
85 p->attachSlot(SIGNAL(createIdentity(const Identity &)), this, SLOT(createIdentity(const Identity &)));
86 p->attachSlot(SIGNAL(updateIdentity(const Identity &)), this, SLOT(updateIdentity(const Identity &)));
87 p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
91 foreach(Identity *id, _identities.values()) {
95 // Load and init networks.
96 // FIXME For now we use the old info from sessionData...
98 QVariantMap networks = retrieveSessionData("Networks").toMap();
99 foreach(QString netname, networks.keys()) {
100 QVariantMap network = networks[netname].toMap();
101 NetworkId netid = Core::networkId(user(), netname);
102 Network *net = new Network(netid, this);
103 connect(net, SIGNAL(connectRequested(NetworkId)), this, SLOT(connectToNetwork(NetworkId)));
104 net->setNetworkName(netname);
105 net->setIdentity(1); // FIXME default identity for now
106 net->setCodecForEncoding("ISO-8859-15"); // FIXME
107 net->setCodecForDecoding("ISO-8859-15"); // FIXME
108 QList<QVariantMap> slist;
109 foreach(QVariant v, network["Servers"].toList()) slist << v.toMap();
110 net->setServerList(slist);
112 _networks[netid] = net;
119 CoreSession::~CoreSession() {
122 UserId CoreSession::user() const {
126 Network *CoreSession::network(NetworkId id) const {
127 if(_networks.contains(id)) return _networks[id];
131 NetworkConnection *CoreSession::networkConnection(NetworkId id) const {
132 if(_connections.contains(id)) return _connections[id];
136 Identity *CoreSession::identity(IdentityId id) const {
137 if(_identities.contains(id)) return _identities[id];
141 QVariant CoreSession::state() const { // FIXME
144 QList<QVariant> conn;
145 foreach(NetworkConnection *net, connections.values()) {
146 if(net->isConnected()) {
148 m["Network"] = net->networkName();
149 m["State"] = net->state();
153 res["ConnectedServers"] = conn;
158 void CoreSession::restoreState(const QVariant &previousState) { // FIXME
161 QVariantMap state = previousState.toMap();
162 if(state.contains("ConnectedServers")) {
163 foreach(QVariant v, state["ConnectedServers"].toList()) {
164 QVariantMap m = v.toMap();
165 QString net = m["Network"].toString();
166 if(!net.isEmpty()) connectToNetwork(net, m["State"]);
173 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
174 CoreUserSettings s(user());
175 s.setSessionValue(key, data);
176 sessionData[key] = data;
177 emit sessionDataChanged(key, data);
178 emit sessionDataChanged(key);
181 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
183 if(!sessionData.contains(key)) data = def;
184 else data = sessionData[key];
188 void CoreSession::updateBufferInfo(UserId uid, const BufferInfo &bufinfo) {
189 if(uid == user()) emit bufferInfoUpdated(bufinfo);
193 void CoreSession::connectToNetwork(QString netname, const QVariant &previousState) {
195 foreach(Network *n, _networks.values()) {
196 if(n->networkName() == netname) {
201 qWarning() << "Connect to unknown network requested, ignoring!";
204 connectToNetwork(net->networkId(), previousState);
207 void CoreSession::connectToNetwork(NetworkId id, const QVariant &previousState) {
208 Network *net = network(id);
210 qWarning() << "Connect to unknown network requested! net:" << id << "user:" << user();
214 NetworkConnection *conn = networkConnection(id);
216 conn = new NetworkConnection(net, this, previousState);
217 _connections[id] = conn;
218 attachNetworkConnection(conn);
219 conn->connectToIrc();
223 void CoreSession::attachNetworkConnection(NetworkConnection *conn) {
224 //connect(this, SIGNAL(connectToIrc(QString)), network, SLOT(connectToIrc(QString)));
225 //connect(this, SIGNAL(disconnectFromIrc(QString)), network, SLOT(disconnectFromIrc(QString)));
226 //connect(this, SIGNAL(msgFromGui(uint, QString, QString)), network, SLOT(userInput(uint, QString, QString)));
228 connect(conn, SIGNAL(connected(NetworkId)), this, SLOT(networkConnected(NetworkId)));
229 connect(conn, SIGNAL(disconnected(NetworkId)), this, SLOT(networkDisconnected(NetworkId)));
230 signalProxy()->attachSignal(conn, SIGNAL(connected(NetworkId)), SIGNAL(networkConnected(NetworkId)));
231 signalProxy()->attachSignal(conn, SIGNAL(disconnected(NetworkId)), SIGNAL(networkDisconnected(NetworkId)));
233 connect(conn, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
234 connect(conn, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
236 // TODO add error handling
239 void CoreSession::networkStateRequested() {
242 void CoreSession::addClient(QObject *dev) { // this is QObject* so we can use it in signal connections
243 QIODevice *device = qobject_cast<QIODevice *>(dev);
245 qWarning() << "Invoking CoreSession::addClient with a QObject that is not a QIODevice!";
247 signalProxy()->addPeer(device);
249 reply["MsgType"] = "SessionInit";
250 reply["SessionState"] = sessionState();
251 SignalProxy::writeDataToDevice(device, reply);
255 SignalProxy *CoreSession::signalProxy() const {
259 void CoreSession::networkConnected(uint networkid) {
260 Core::bufferInfo(user(), networkConnection(networkid)->networkName()); // create status buffer
263 void CoreSession::networkDisconnected(uint networkid) {
265 // connection should only go away on explicit /part, and handle reconnections etcpp internally otherwise
266 Q_ASSERT(_connections.contains(networkid));
267 _connections.take(networkid)->deleteLater();
268 Q_ASSERT(!_connections.contains(networkid));
271 // FIXME switch to BufferId
272 void CoreSession::msgFromClient(BufferInfo bufinfo, QString msg) {
273 NetworkConnection *conn = networkConnection(bufinfo.networkId());
275 conn->userInput(bufinfo.buffer(), msg);
277 qWarning() << "Trying to send to unconnected network!";
281 // ALL messages coming pass through these functions before going to the GUI.
282 // So this is the perfect place for storing the backlog and log stuff.
283 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
284 NetworkConnection *s = qobject_cast<NetworkConnection*>(this->sender());
287 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
288 buf = Core::bufferInfo(user(), s->networkName(), nickFromMask(sender));
290 buf = Core::bufferInfo(user(), s->networkName(), target);
292 Message msg(buf, type, text, sender, flags);
293 msg.setMsgId(Core::storeMessage(msg));
294 Q_ASSERT(msg.msgId());
295 emit displayMsg(msg);
298 void CoreSession::recvStatusMsgFromServer(QString msg) {
299 NetworkConnection *s = qobject_cast<NetworkConnection*>(sender());
301 emit displayStatusMsg(s->networkName(), msg);
304 QList<BufferInfo> CoreSession::buffers() const {
305 return Core::requestBuffers(user());
309 QVariant CoreSession::sessionState() {
313 foreach(BufferInfo id, buffers()) bufs << QVariant::fromValue(id);
314 v["BufferInfos"] = bufs;
315 QVariantList networkids;
316 foreach(NetworkId id, _networks.keys()) networkids << QVariant::fromValue(id);
317 v["NetworkIds"] = networkids;
319 quint32 ircusercount = 0;
320 quint32 ircchannelcount = 0;
321 foreach(Network *net, _networks.values()) {
322 ircusercount += net->ircUserCount();
323 ircchannelcount += net->ircChannelCount();
325 v["IrcUserCount"] = ircusercount;
326 v["IrcChannelCount"] = ircchannelcount;
327 qDebug() << "nets:" << _networks.count() << " chans:" << ircchannelcount << " users:" << ircusercount;
329 QList<QVariant> idlist;
330 foreach(Identity *i, _identities.values()) idlist << QVariant::fromValue<Identity>(*i);
331 v["Identities"] = idlist;
333 v["SessionData"] = sessionData;
335 //v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
339 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
341 QList<Message> msglist;
342 if(v1.type() == QVariant::DateTime) {
346 msglist = Core::requestMsgs(id, v1.toInt(), v2.toInt());
349 // Send messages out in smaller packages - we don't want to make the signal data too large!
350 for(int i = 0; i < msglist.count(); i++) {
351 log.append(QVariant::fromValue(msglist[i]));
352 if(log.count() >= 5) {
353 emit backlogData(id, log, i >= msglist.count() - 1);
357 if(log.count() > 0) emit backlogData(id, log, true);
361 void CoreSession::initScriptEngine() {
362 signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
363 signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
366 //QScriptValue storage_ = scriptEngine->newQObject(storage);
367 //scriptEngine->globalObject().setProperty("storage", storage_);
370 void CoreSession::scriptRequest(QString script) {
371 emit scriptResult(scriptEngine->evaluate(script).toString());
374 void CoreSession::createIdentity(const Identity &id) {
377 for(i = 1; i <= _identities.count(); i++) {
378 if(!_identities.keys().contains(i)) break;
380 //qDebug() << "found free id" << i;
381 Identity *newId = new Identity(id, this);
383 _identities[i] = newId;
384 signalProxy()->synchronize(newId);
385 CoreUserSettings s(user());
386 s.storeIdentity(*newId);
387 emit identityCreated(*newId);
390 void CoreSession::updateIdentity(const Identity &id) {
391 if(!_identities.contains(id.id())) {
392 qWarning() << "Update request for unknown identity received!";
395 _identities[id.id()]->update(id);
397 CoreUserSettings s(user());
401 void CoreSession::removeIdentity(IdentityId id) {
402 Identity *i = _identities.take(id);
404 emit identityRemoved(id);
405 CoreUserSettings s(user());
406 s.removeIdentity(id);