07e0c73e73be792440431cd376af4cff04394a68
[quassel.git] / core / core.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel 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 "core.h"
22 #include "server.h"
23 #include "global.h"
24 #include "util.h"
25 #include "coreproxy.h"
26
27 #include <QtSql>
28 #include <QSettings>
29
30 Core::Core() {
31   if(core) qFatal("Trying to instantiate more than one Core object!");
32
33   connect(coreProxy, SIGNAL(requestServerStates()), this, SIGNAL(serverStateRequested()));
34   connect(coreProxy, SIGNAL(gsRequestConnect(QStringList)), this, SLOT(connectToIrc(QStringList)));
35   connect(coreProxy, SIGNAL(gsUserInput(BufferId, QString)), this, SLOT(msgFromGUI(BufferId, QString)));
36   connect(coreProxy, SIGNAL(gsImportBacklog()), &backlog, SLOT(importOldBacklog()));
37   connect(coreProxy, SIGNAL(gsRequestBacklog(BufferId, QVariant, QVariant)), this, SLOT(sendBacklog(BufferId, QVariant, QVariant)));
38   connect(this, SIGNAL(displayMsg(Message)), coreProxy, SLOT(csDisplayMsg(Message)));
39   connect(this, SIGNAL(displayStatusMsg(QString, QString)), coreProxy, SLOT(csDisplayStatusMsg(QString, QString)));
40   connect(this, SIGNAL(backlogData(BufferId, QList<QVariant>, bool)), coreProxy, SLOT(csBacklogData(BufferId, QList<QVariant>, bool)));
41
42   // Read global settings from config file
43   QSettings s;
44   s.beginGroup("Global");
45   QString key;
46   foreach(key, s.childKeys()) {
47     global->updateData(key, s.value(key));
48   }
49   backlog.init("Default"); // FIXME
50   global->updateData("CoreReady", true);
51   // Now that we are in sync, we can connect signals to automatically store further updates.
52   // I don't think we care if global data changed locally or if it was updated by a client. 
53   connect(global, SIGNAL(dataUpdatedRemotely(QString)), SLOT(globalDataUpdated(QString)));
54   connect(global, SIGNAL(dataPutLocally(QString)), SLOT(globalDataUpdated(QString)));
55
56 }
57
58 Core::~Core() {
59   //foreach(Server *s, servers) {
60   //  delete s;
61   //}
62 }
63
64 void Core::globalDataUpdated(QString key) {
65   QVariant data = global->getData(key);
66   QSettings s;
67   s.setValue(QString("Global/")+key, data);
68 }
69
70 void Core::connectToIrc(QStringList networks) {
71   foreach(QString net, networks) {
72     if(servers.contains(net)) {
73
74     } else {
75       Server *server = new Server(net);
76       connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState()));
77       connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
78       connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
79       connect(this, SIGNAL(msgFromGUI(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
80       connect(server, SIGNAL(serverState(QString, VarMap)), coreProxy, SLOT(csServerState(QString, VarMap)));
81       //connect(server, SIGNAL(displayMsg(Message)), this, SLOT(recvMessageFromServer(Message)));
82       connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
83       connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
84       connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
85       connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
86       connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
87       connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
88       connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
89       connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
90       connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
91       connect(server, SIGNAL(queryRequested(QString, QString)), coreProxy, SLOT(csQueryRequested(QString, QString)));
92       // add error handling
93       connect(server, SIGNAL(connected(QString)), coreProxy, SLOT(csServerConnected(QString)));
94       connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
95
96       server->start();
97       servers[net] = server;
98     }
99     emit connectToIrc(net);
100   }
101 }
102
103 void Core::serverDisconnected(QString net) {
104   delete servers[net];
105   servers.remove(net);
106   coreProxy->csServerDisconnected(net);
107 }
108
109 void Core::msgFromGUI(BufferId bufid, QString msg) {
110   emit msgFromGUI(bufid.network(), bufid.buffer(), msg);
111 }
112
113 // ALL messages coming pass through these functions before going to the GUI.
114 // So this is the perfect place for storing the backlog and log stuff.
115
116 void Core::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
117   Server *s = qobject_cast<Server*>(this->sender());
118   Q_ASSERT(s);
119   BufferId buf;
120   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
121     buf = backlog.getBufferId(s->getNetwork(), nickFromMask(sender));
122   } else {
123     buf = backlog.getBufferId(s->getNetwork(), target);
124   }
125   Message msg(buf, type, text, sender, flags);
126   msg.msgId = backlog.logMessage(msg);
127   emit displayMsg(msg);
128 }
129 /*
130 void Core::recvMessageFromServer(Message msg) {
131   Server *s = qobject_cast<Server*>(sender());
132   Q_ASSERT(s);
133   logMessage(s->getNetwork(), msg);
134   emit displayMsg(s->getNetwork(), msg);
135 }
136 */
137
138 void Core::recvStatusMsgFromServer(QString msg) {
139   Server *s = qobject_cast<Server*>(sender());
140   Q_ASSERT(s);
141   emit displayStatusMsg(s->getNetwork(), msg);
142 }
143
144 QList<BufferId> Core::getBuffers() {
145   return backlog.requestBuffers();
146 }
147
148 void Core::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
149   QList<QVariant> log;
150   QList<Message> msglist;
151   if(v1.type() == QVariant::DateTime) {
152
153
154   } else {
155     msglist = backlog.requestMsgs(id, v1.toInt(), v2.toInt());
156   }
157
158   // Send messages out in smaller packages - we don't want to make the signal data too large!
159   for(int i = 0; i < msglist.count(); i++) {
160     log.append(QVariant::fromValue(msglist[i]));
161     if(log.count() >= 5) {
162       emit backlogData(id, log, i >= msglist.count() - 1);
163       log.clear();
164     }
165   }
166   if(log.count() > 0) emit backlogData(id, log, true);
167 }
168
169
170 Core *core = 0;