1 /***************************************************************************
2 * Copyright (C) 2005/06 by The Quassel Team *
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) any later version. *
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 ***************************************************************************/
25 #include "coreproxy.h"
26 #include "sqlitestorage.h"
32 if(core) qFatal("Trying to instantiate more than one Core object!");
34 if(!SqliteStorage::isAvailable()) {
35 qFatal("Sqlite is currently required! Please make sure your Qt library has sqlite support enabled.");
37 //SqliteStorage::init();
38 storage = new SqliteStorage();
39 user = storage->validateUser("Default", "password");
40 if(!user) user = storage->addUser("Default", "password");
43 connect(coreProxy, SIGNAL(requestServerStates()), this, SIGNAL(serverStateRequested()));
44 connect(coreProxy, SIGNAL(gsRequestConnect(QStringList)), this, SLOT(connectToIrc(QStringList)));
45 connect(coreProxy, SIGNAL(gsUserInput(BufferId, QString)), this, SLOT(msgFromGUI(BufferId, QString)));
46 connect(coreProxy, SIGNAL(gsImportBacklog()), storage, SLOT(importOldBacklog()));
47 connect(coreProxy, SIGNAL(gsRequestBacklog(BufferId, QVariant, QVariant)), this, SLOT(sendBacklog(BufferId, QVariant, QVariant)));
48 connect(this, SIGNAL(displayMsg(Message)), coreProxy, SLOT(csDisplayMsg(Message)));
49 connect(this, SIGNAL(displayStatusMsg(QString, QString)), coreProxy, SLOT(csDisplayStatusMsg(QString, QString)));
50 connect(this, SIGNAL(backlogData(BufferId, QList<QVariant>, bool)), coreProxy, SLOT(csBacklogData(BufferId, QList<QVariant>, bool)));
51 connect(storage, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
52 connect(this, SIGNAL(bufferIdUpdated(BufferId)), coreProxy, SLOT(csUpdateBufferId(BufferId)));
53 // Read global settings from config file
55 s.beginGroup("Global");
57 foreach(key, s.childKeys()) {
58 global->updateData(key, s.value(key));
61 global->updateData("CoreReady", true);
62 // Now that we are in sync, we can connect signals to automatically store further updates.
63 // I don't think we care if global data changed locally or if it was updated by a client.
64 connect(global, SIGNAL(dataUpdatedRemotely(QString)), SLOT(globalDataUpdated(QString)));
65 connect(global, SIGNAL(dataPutLocally(QString)), SLOT(globalDataUpdated(QString)));
70 //foreach(Server *s, servers) {
76 void Core::globalDataUpdated(QString key) {
77 QVariant data = global->getData(key);
79 s.setValue(QString("Global/")+key, data);
82 void Core::connectToIrc(QStringList networks) {
83 foreach(QString net, networks) {
84 if(servers.contains(net)) {
87 Server *server = new Server(net);
88 connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState()));
89 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
90 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
91 connect(this, SIGNAL(msgFromGUI(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
92 connect(server, SIGNAL(serverState(QString, VarMap)), coreProxy, SLOT(csServerState(QString, VarMap)));
93 //connect(server, SIGNAL(displayMsg(Message)), this, SLOT(recvMessageFromServer(Message)));
94 connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8)));
95 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
96 connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
97 connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
98 connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
99 connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
100 connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
101 connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
102 connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
103 connect(server, SIGNAL(queryRequested(QString, QString)), coreProxy, SLOT(csQueryRequested(QString, QString)));
104 // add error handling
105 connect(server, SIGNAL(connected(QString)), coreProxy, SLOT(csServerConnected(QString)));
106 connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString)));
109 servers[net] = server;
111 emit connectToIrc(net);
115 void Core::serverDisconnected(QString net) {
118 coreProxy->csServerDisconnected(net);
121 void Core::msgFromGUI(BufferId bufid, QString msg) {
122 emit msgFromGUI(bufid.network(), bufid.buffer(), msg);
125 // ALL messages coming pass through these functions before going to the GUI.
126 // So this is the perfect place for storing the backlog and log stuff.
128 void Core::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
129 Server *s = qobject_cast<Server*>(this->sender());
132 if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
133 buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
135 buf = storage->getBufferId(user, s->getNetwork(), target);
137 Message msg(buf, type, text, sender, flags);
138 msg.msgId = storage->logMessage(msg); //qDebug() << msg.msgId;
140 emit displayMsg(msg);
143 void Core::recvStatusMsgFromServer(QString msg) {
144 Server *s = qobject_cast<Server*>(sender());
146 emit displayStatusMsg(s->getNetwork(), msg);
149 QList<BufferId> Core::getBuffers() {
150 return storage->requestBuffers(user);
153 void Core::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
155 QList<Message> msglist;
156 if(v1.type() == QVariant::DateTime) {
160 msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
163 // Send messages out in smaller packages - we don't want to make the signal data too large!
164 for(int i = 0; i < msglist.count(); i++) {
165 log.append(QVariant::fromValue(msglist[i]));
166 if(log.count() >= 5) {
167 emit backlogData(id, log, i >= msglist.count() - 1);
171 if(log.count() > 0) emit backlogData(id, log, true);