Fixed and integrated SqliteStorage as new storage backend. This retires the old Backl...
[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 #include "sqlitestorage.h"
27
28 #include <QtSql>
29 #include <QSettings>
30
31 Core::Core() {
32   if(core) qFatal("Trying to instantiate more than one Core object!");
33
34   if(!SqliteStorage::isAvailable()) {
35     qFatal("Sqlite is currently required! Please make sure your Qt library has sqlite support enabled.");
36   }
37   //SqliteStorage::init();
38   storage = new SqliteStorage();
39   user = storage->validateUser("Default", "password");
40   if(!user) user = storage->addUser("Default", "password");
41   Q_ASSERT(user);
42
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
54   QSettings s;
55   s.beginGroup("Global");
56   QString key;
57   foreach(key, s.childKeys()) {
58     global->updateData(key, s.value(key));
59   }
60
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)));
66
67 }
68
69 Core::~Core() {
70   //foreach(Server *s, servers) {
71   //  delete s;
72   //}
73   delete storage;
74 }
75
76 void Core::globalDataUpdated(QString key) {
77   QVariant data = global->getData(key);
78   QSettings s;
79   s.setValue(QString("Global/")+key, data);
80 }
81
82 void Core::connectToIrc(QStringList networks) {
83   foreach(QString net, networks) {
84     if(servers.contains(net)) {
85
86     } else {
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)));
107
108       server->start();
109       servers[net] = server;
110     }
111     emit connectToIrc(net);
112   }
113 }
114
115 void Core::serverDisconnected(QString net) {
116   delete servers[net];
117   servers.remove(net);
118   coreProxy->csServerDisconnected(net);
119 }
120
121 void Core::msgFromGUI(BufferId bufid, QString msg) {
122   emit msgFromGUI(bufid.network(), bufid.buffer(), msg);
123 }
124
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.
127
128 void Core::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
129   Server *s = qobject_cast<Server*>(this->sender());
130   Q_ASSERT(s);
131   BufferId buf;
132   if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
133     buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender));
134   } else {
135     buf = storage->getBufferId(user, s->getNetwork(), target);
136   }
137   Message msg(buf, type, text, sender, flags);
138   msg.msgId = storage->logMessage(msg); //qDebug() << msg.msgId;
139   Q_ASSERT(msg.msgId);
140   emit displayMsg(msg);
141 }
142
143 void Core::recvStatusMsgFromServer(QString msg) {
144   Server *s = qobject_cast<Server*>(sender());
145   Q_ASSERT(s);
146   emit displayStatusMsg(s->getNetwork(), msg);
147 }
148
149 QList<BufferId> Core::getBuffers() {
150   return storage->requestBuffers(user);
151 }
152
153 void Core::sendBacklog(BufferId id, QVariant v1, QVariant v2) {
154   QList<QVariant> log;
155   QList<Message> msglist;
156   if(v1.type() == QVariant::DateTime) {
157
158
159   } else {
160     msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt());
161   }
162
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);
168       log.clear();
169     }
170   }
171   if(log.count() > 0) emit backlogData(id, log, true);
172 }
173
174
175 Core *core = 0;