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();
50 foreach(IdentityId id, s.identityIds()) {
51 Identity *i = new Identity(s.identity(id), this);
53 qDebug() << QString("Invalid identity!");
57 if(_identities.contains(i->id())) {
58 qDebug() << "Duplicate identity, ignoring!";
62 _identities[i->id()] = i;
64 if(!_identities.count()) {
71 p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
72 p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
73 p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(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)));
79 p->attachSignal(storage, 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()) {
96 CoreSession::~CoreSession() {
99 UserId CoreSession::userId() const {
103 QVariant CoreSession::state() const {
105 QList<QVariant> conn;
106 foreach(Server *server, servers.values()) {
107 if(server->isConnected()) {
109 m["Network"] = server->networkName();
110 m["State"] = server->state();
114 res["ConnectedServers"] = conn;
118 void CoreSession::restoreState(const QVariant &previousState) {
120 QVariantMap state = previousState.toMap();
121 if(state.contains("ConnectedServers")) {
122 foreach(QVariant v, state["ConnectedServers"].toList()) {
123 QVariantMap m = v.toMap();
124 QString net = m["Network"].toString();
125 if(!net.isEmpty()) connectToNetwork(net, m["State"]);
131 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
132 CoreUserSettings s(user);
134 s.setSessionValue(key, data);
135 sessionData[key] = data;
137 emit sessionDataChanged(key, data);
138 emit sessionDataChanged(key);
141 QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) {
144 if(!sessionData.contains(key)) data = def;
145 else data = sessionData[key];
150 // FIXME switch to NetworkIDs
151 void CoreSession::connectToNetwork(QString network, const QVariant &previousState) {
152 uint networkid = getNetworkId(network);
154 qWarning() << "unable to connect to Network" << network << "(User:" << userId() << "): unable to determine NetworkId";
157 if(!servers.contains(networkid)) {
158 Server *server = new Server(userId(), networkid, network, previousState);
159 servers[networkid] = server;
160 attachServer(server);
163 emit connectToIrc(network);
166 void CoreSession::attachServer(Server *server) {
167 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
168 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
169 connect(this, SIGNAL(msgFromGui(uint, QString, QString)), server, SLOT(userInput(uint, QString, QString)));
171 connect(server, SIGNAL(connected(uint)), this, SLOT(serverConnected(uint)));
172 connect(server, SIGNAL(disconnected(uint)), this, SLOT(serverDisconnected(uint)));
173 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
174 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
176 // connect serversignals to proxy
177 signalProxy()->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap)));
178 signalProxy()->attachSignal(server, SIGNAL(connected(uint)), SIGNAL(networkConnected(uint)));
179 signalProxy()->attachSignal(server, SIGNAL(disconnected(uint)), SIGNAL(networkDisconnected(uint)));
180 // TODO add error handling
183 void CoreSession::serverStateRequested() {
186 void CoreSession::addClient(QIODevice *device) {
187 signalProxy()->addPeer(device);
190 SignalProxy *CoreSession::signalProxy() const {
194 void CoreSession::serverConnected(uint networkid) {
195 storage->getBufferInfo(userId(), servers[networkid]->networkName()); // create status buffer
198 void CoreSession::serverDisconnected(uint networkid) {
199 Q_ASSERT(servers.contains(networkid));
200 servers.take(networkid)->deleteLater();
201 Q_ASSERT(!servers.contains(networkid));
204 void CoreSession::msgFromGui(BufferInfo bufid, QString msg) {
205 emit msgFromGui(bufid.networkId(), bufid.buffer(), msg);
208 // ALL messages coming pass through these functions before going to the GUI.
209 // So this is the perfect place for storing the backlog and log stuff.
210 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
211 Server *s = qobject_cast<Server*>(this->sender());
214 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
215 buf = storage->getBufferInfo(user, s->networkName(), nickFromMask(sender));
217 buf = storage->getBufferInfo(user, s->networkName(), target);
219 Message msg(buf, type, text, sender, flags);
220 msg.setMsgId(storage->logMessage(msg));
221 Q_ASSERT(msg.msgId());
222 emit displayMsg(msg);
225 void CoreSession::recvStatusMsgFromServer(QString msg) {
226 Server *s = qobject_cast<Server*>(sender());
228 emit displayStatusMsg(s->networkName(), msg);
232 uint CoreSession::getNetworkId(const QString &net) const {
233 return storage->getNetworkId(user, net);
236 QList<BufferInfo> CoreSession::buffers() const {
237 return storage->requestBuffers(user);
241 QVariant CoreSession::sessionState() {
245 foreach(BufferInfo id, storage->requestBuffers(user))
246 bufs.append(QVariant::fromValue(id));
250 v["SessionData"] = sessionData;
253 QVariantList networks;
254 foreach(NetworkId networkid, servers.keys())
255 networks.append(QVariant(networkid));
256 v["Networks"] = QVariant(networks);
258 QList<QVariant> idlist;
259 foreach(Identity *i, _identities.values()) idlist << QVariant::fromValue<Identity>(*i);
260 v["Identities"] = idlist;
262 // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
266 void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
268 QList<Message> msglist;
269 if(v1.type() == QVariant::DateTime) {
273 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
276 // Send messages out in smaller packages - we don't want to make the signal data too large!
277 for(int i = 0; i < msglist.count(); i++) {
278 log.append(QVariant::fromValue(msglist[i]));
279 if(log.count() >= 5) {
280 emit backlogData(id, log, i >= msglist.count() - 1);
284 if(log.count() > 0) emit backlogData(id, log, true);
288 void CoreSession::initScriptEngine() {
289 signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
290 signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
292 QScriptValue storage_ = scriptEngine->newQObject(storage);
293 scriptEngine->globalObject().setProperty("storage", storage_);
296 void CoreSession::scriptRequest(QString script) {
297 emit scriptResult(scriptEngine->evaluate(script).toString());
300 void CoreSession::createIdentity(const Identity &id) {
303 for(i = 1; i <= _identities.count(); i++) {
304 if(!_identities.keys().contains(i)) break;
306 //qDebug() << "found free id" << i;
307 Identity *newId = new Identity(id, this);
309 _identities[i] = newId;
310 signalProxy()->synchronize(newId);
311 CoreUserSettings s(user);
312 s.storeIdentity(*newId);
313 emit identityCreated(i);
316 void CoreSession::updateIdentity(const Identity &id) {
317 if(!_identities.contains(id.id())) {
318 qWarning() << "Update request for unknown identity received!";
321 _identities[id.id()]->update(id);
323 CoreUserSettings s(user);
327 void CoreSession::removeIdentity(IdentityId id) {
328 Identity *i = _identities.take(id);
330 emit identityRemoved(id);
331 CoreUserSettings s(user);
332 s.removeIdentity(id);