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 ***************************************************************************/
24 #include "coreproxy.h"
29 if(core) qFatal("Trying to instantiate more than one Core object!");
31 connect(coreProxy, SIGNAL(gsRequestConnect(QStringList)), this, SLOT(connectToIrc(QStringList)));
32 connect(coreProxy, SIGNAL(gsUserInput(QString, QString, QString)), this, SIGNAL(msgFromGUI(QString, QString, QString)));
33 connect(this, SIGNAL(displayMsg(QString, QString, Message)), coreProxy, SLOT(csDisplayMsg(QString, QString, Message)));
34 connect(this, SIGNAL(displayStatusMsg(QString, QString)), coreProxy, SLOT(csDisplayStatusMsg(QString, QString)));
36 // Read global settings from config file
38 s.beginGroup("Global");
40 foreach(key, s.childKeys()) {
41 global->updateData(key, s.value(key));
44 global->updateData("CoreReady", true);
45 // Now that we are in sync, we can connect signals to automatically store further updates.
46 // I don't think we care if global data changed locally or if it was updated by a client.
47 connect(global, SIGNAL(dataUpdatedRemotely(QString)), SLOT(globalDataUpdated(QString)));
48 connect(global, SIGNAL(dataPutLocally(QString)), SLOT(globalDataUpdated(QString)));
52 void Core::globalDataUpdated(QString key) {
53 QVariant data = global->getData(key);
55 s.setValue(QString("Global/")+key, data);
58 void Core::connectToIrc(QStringList networks) {
59 foreach(QString net, networks) {
60 if(servers.contains(net)) {
63 Server *server = new Server(net);
64 connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString)));
65 connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString)));
66 connect(this, SIGNAL(msgFromGUI(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString)));
67 connect(server, SIGNAL(displayMsg(QString, Message)), this, SLOT(recvMessageFromServer(QString, Message)));
68 connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString)));
69 connect(server, SIGNAL(modeSet(QString, QString, QString)), coreProxy, SLOT(csModeSet(QString, QString, QString)));
70 connect(server, SIGNAL(topicSet(QString, QString, QString)), coreProxy, SLOT(csTopicSet(QString, QString, QString)));
71 connect(server, SIGNAL(setNicks(QString, QString, QStringList)), coreProxy, SLOT(csSetNicks(QString, QString, QStringList)));
72 connect(server, SIGNAL(nickAdded(QString, QString, VarMap)), coreProxy, SLOT(csNickAdded(QString, QString, VarMap)));
73 connect(server, SIGNAL(nickRenamed(QString, QString, QString)), coreProxy, SLOT(csNickRenamed(QString, QString, QString)));
74 connect(server, SIGNAL(nickRemoved(QString, QString)), coreProxy, SLOT(csNickRemoved(QString, QString)));
75 connect(server, SIGNAL(nickUpdated(QString, QString, VarMap)), coreProxy, SLOT(csNickUpdated(QString, QString, VarMap)));
76 connect(server, SIGNAL(ownNickSet(QString, QString)), coreProxy, SLOT(csOwnNickSet(QString, QString)));
80 servers[net] = server;
82 emit connectToIrc(net);
86 // ALL messages coming pass through these functions before going to the GUI.
87 // So this is the perfect place for storing the backlog and log stuff.
88 void Core::recvMessageFromServer(QString buf, Message msg) {
89 Server *s = qobject_cast<Server*>(sender());
92 emit displayMsg(s->getNetwork(), buf, msg);
95 void Core::recvStatusMsgFromServer(QString msg) {
96 Server *s = qobject_cast<Server*>(sender());
98 emit displayStatusMsg(s->getNetwork(), msg);
101 // file name scheme: quassel-backlog-2006-29-10.bin
102 void Core::initBackLog() {
103 backLogDir = QDir(Global::quasselDir + "/backlog");
104 if(!backLogDir.exists()) {
105 qWarning(QString("Creating backlog directory \"%1\"...").arg(backLogDir.absolutePath()).toAscii());
106 if(!backLogDir.mkpath(backLogDir.absolutePath())) {
107 qWarning(QString("Could not create backlog directory! Disabling logging...").toAscii());
108 backLogEnabled = false;
112 backLogDir.refresh();
113 //if(!backLogDir.isReadable()) {
114 // qWarning(QString("Cannot read directory \"%1\". Disabling logging...").arg(backLogDir.absolutePath()).toAscii());
115 // backLogEnabled = false;
118 QStringList logs = backLogDir.entryList(QStringList("quassel-backlog-*.bin"), QDir::Files|QDir::Readable, QDir::Name);
119 foreach(QString name, logs) {
120 QFile f(backLogDir.absolutePath() + "/" + name);
121 if(!f.open(QIODevice::ReadOnly)) {
122 qWarning(QString("Could not open \"%1\" for reading!").arg(f.fileName()).toAscii());
126 in.setVersion(QDataStream::Qt_4_2);
127 QByteArray verstring; quint8 vernum; in >> verstring >> vernum;
128 if(verstring != BACKLOG_STRING) {
129 qWarning(QString("\"%1\" is not a Quassel backlog file!").arg(f.fileName()).toAscii());
132 if(vernum != BACKLOG_FORMAT) {
133 qWarning(QString("\"%1\": Version mismatch!").arg(f.fileName()).toAscii());
136 qDebug() << "Reading backlog from" << f.fileName();
137 currentLogFileDate = QDate::fromString(f.fileName(), QString("'%1/quassel-backlog-'yyyy-MM-dd'.bin'").arg(backLogDir.absolutePath()));
138 if(!currentLogFileDate.isValid()) {
139 qWarning(QString("\"%1\" has an invalid file name!").arg(f.fileName()).toAscii());
148 backLogEnabled = true;
151 /** Log a core message (emitted via a displayMsg() signal) to the backlog file.
152 * If a file for the current day does not exist, one will be created. Otherwise, messages will be appended.
153 * The file header is the string defined by BACKLOG_STRING, followed by a quint8 specifying the format
154 * version (BACKLOG_FORMAT). The rest is simply serialized Message objects.
156 void Core::logMessage(Message msg) {
158 if(!currentLogFileDate.isValid() || currentLogFileDate < QDate::currentDate()) {
159 if(currentLogFile.isOpen()) currentLogFile.close();
160 currentLogFileDate = QDate::currentDate();
162 if(!currentLogFile.isOpen()) {
163 currentLogFile.setFileName(backLogDir.absolutePath() + "/" + currentLogFileDate.toString("'quassel-backlog-'yyyy-MM-dd'.bin'"));
164 if(!currentLogFile.open(QIODevice::WriteOnly|QIODevice::Append)) {
165 qWarning(QString("Could not open \"%1\" for writing: %2").arg(currentLogFile.fileName()).arg(currentLogFile.errorString()).toAscii());
168 logStream.setDevice(¤tLogFile); logStream.setVersion(QDataStream::Qt_4_2);
169 if(!currentLogFile.size()) logStream << BACKLOG_STRING << (quint8)BACKLOG_FORMAT;