Cleaning up the closet... or at least the code. Tried to reduce the number of #includ...
[quassel.git] / src / core / coresession.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel IRC Development Team             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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) any later version.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "coresession.h"
22 #include "server.h"
23 #include "storage.h"
24 #include "util.h"
25
26 CoreSession::CoreSession(UserId uid, Storage *_storage) : user(uid), storage(_storage) {
27   coreProxy = new CoreProxy();
28
29   connect(coreProxy, SIGNAL(send(CoreSignal, QVariant, QVariant, QVariant)), this, SIGNAL(proxySignal(CoreSignal, QVariant, QVariant, QVariant)));
30   connect(coreProxy, SIGNAL(requestServerStates()), this, SIGNAL(serverStateRequested()));
31   connect(coreProxy, SIGNAL(gsRequestConnect(QStringList)), this, SLOT(connectToIrc(QStringList)));
32   connect(coreProxy, SIGNAL(gsUserInput(BufferId, QString)), this, SLOT(msgFromGui(BufferId, QString)));
33   connect(coreProxy, SIGNAL(gsImportBacklog()), storage, SLOT(importOldBacklog()));
34   connect(coreProxy, SIGNAL(gsRequestBacklog(BufferId, QVariant, QVariant)), this, SLOT(sendBacklog(BufferId, QVariant, QVariant)));
35   connect(coreProxy, SIGNAL(gsRequestNetworkStates()), this, SLOT(sendServerStates()));
36   connect(this, SIGNAL(displayMsg(Message)), coreProxy, SLOT(csDisplayMsg(Message)));
37   connect(this, SIGNAL(displayStatusMsg(QString, QString)), coreProxy, SLOT(csDisplayStatusMsg(QString, QString)));
38   connect(this, SIGNAL(backlogData(BufferId, QList<QVariant>, bool)), coreProxy, SLOT(csBacklogData(BufferId, QList<QVariant>, bool)));
39   connect(this, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
40   connect(storage, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
41   connect(Global::instance(), SIGNAL(dataUpdatedRemotely(UserId, QString)), this, SLOT(globalDataUpdated(UserId, QString)));
42   connect(Global::instance(), SIGNAL(dataPutLocally(UserId, QString)), this, SLOT(globalDataUpdated(UserId, QString)));
43 }
44
45 CoreSession::~CoreSession() {
46
47 }
48
49 UserId CoreSession::userId() const {
50   return user;
51 }
52
53 void CoreSession::processSignal(ClientSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
54   coreProxy->recv(sig, arg1, arg2, arg3);
55 }
56
57 void CoreSession::globalDataUpdated(UserId uid, QString key) {
58   Q_ASSERT(uid == userId());
59   QVariant data = Global::data(userId(), key);
60   QSettings s;
61   s.setValue(QString("Global/%1/").arg(userId())+key, data);
62 }
63
64 void CoreSession::connectToIrc(QStringList networks) {
65   foreach(QString net, networks) {
66     if(servers.contains(net)) {
67
68     } else {
69       Server *server = new Server(userId(), net);
70       connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState()));
71       connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
72       connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
73       connect(this, SIGNAL(msgFromGui(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
74
75       connect(server, SIGNAL(connected(QString)), this, SLOT(serverConnected(QString)));
76       connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
77
78       connect(server, SIGNAL(serverState(QString, VarMap)), coreProxy, SLOT(csServerState(QString, VarMap)));
79       //connect(server, SIGNAL(displayMsg(Message)), this, SLOT(recvMessageFromServer(Message)));
80       connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
81       connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
82       connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
83       connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
84       connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
85       connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
86       connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
87       connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
88       connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
89       connect(server, SIGNAL(queryRequested(QString, QString)), coreProxy, SLOT(csQueryRequested(QString, QString)));
90       // TODO add error handling
91       connect(server, SIGNAL(connected(QString)), coreProxy, SLOT(csServerConnected(QString)));
92       connect(server, SIGNAL(disconnected(QString)), coreProxy, SLOT(csServerDisconnected(QString)));
93
94       server->start();
95       servers[net] = server;
96     }
97     emit connectToIrc(net);
98   }
99 }
100
101 void CoreSession::serverConnected(QString net) {
102   storage->getBufferId(userId(), net); // create status buffer
103 }
104
105 void CoreSession::serverDisconnected(QString net) {
106   delete servers[net];
107   servers.remove(net);
108   coreProxy->csServerDisconnected(net);
109 }
110
111 void CoreSession::msgFromGui(BufferId bufid, QString msg) {
112   emit msgFromGui(bufid.network(), bufid.buffer(), msg);
113 }
114
115 // ALL messages coming pass through these functions before going to the GUI.
116 // So this is the perfect place for storing the backlog and log stuff.
117
118 void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
119   Server *s = qobject_cast<Server*>(this->sender());
120   Q_ASSERT(s);
121   BufferId buf;
122   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
123     buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
124   } else {
125     buf = storage->getBufferId(user, s->getNetwork(), target);
126   }
127   Message msg(buf, type, text, sender, flags);
128   msg.msgId = storage->logMessage(msg); //qDebug() << msg.msgId;
129   Q_ASSERT(msg.msgId);
130   emit displayMsg(msg);
131 }
132
133 void CoreSession::recvStatusMsgFromServer(QString msg) {
134   Server *s = qobject_cast<Server*>(sender());
135   Q_ASSERT(s);
136   emit displayStatusMsg(s->getNetwork(), msg);
137 }
138
139
140 QList<BufferId> CoreSession::buffers() const {
141   return storage->requestBuffers(user);
142 }
143
144
145 QVariant CoreSession::sessionState() {
146   VarMap v;
147   QList<QVariant> bufs;
148   foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); }
149   v["Buffers"] = bufs;
150
151   return v;
152 }
153
154 void CoreSession::sendServerStates() {
155   emit serverStateRequested();
156 }
157
158 void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
159   QList<QVariant> log;
160   QList<Message> msglist;
161   if(v1.type() == QVariant::DateTime) {
162
163
164   } else {
165     msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
166   }
167
168   // Send messages out in smaller packages - we don't want to make the signal data too large!
169   for(int i = 0; i < msglist.count(); i++) {
170     log.append(QVariant::fromValue(msglist[i]));
171     if(log.count() >= 5) {
172       emit backlogData(id, log, i >= msglist.count() - 1);
173       log.clear();
174     }
175   }
176   if(log.count() > 0) emit backlogData(id, log, true);
177 }