f9f4f23a44a71e27583f4c4b560a0bba8318edb6
[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 "coreproxy.h"
25
26 #include <QSettings>
27
28 Core::Core() {
29   if(core) qFatal("Trying to instantiate more than one Core object!");
30
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)));
35
36   // Read global settings from config file
37   QSettings s;
38   s.beginGroup("Global");
39   QString key;
40   foreach(key, s.childKeys()) {
41     global->updateData(key, s.value(key));
42   }
43   initBackLog();
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)));
49
50 }
51
52 void Core::globalDataUpdated(QString key) {
53   QVariant data = global->getData(key);
54   QSettings s;
55   s.setValue(QString("Global/")+key, data);
56 }
57
58 void Core::connectToIrc(QStringList networks) {
59   foreach(QString net, networks) {
60     if(servers.contains(net)) {
61
62     } else {
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)));
77       // add error handling
78
79       server->start();
80       servers[net] = server;
81     }
82     emit connectToIrc(net);
83   }
84 }
85
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());
90   Q_ASSERT(s);
91   logMessage(msg);
92   emit displayMsg(s->getNetwork(), buf, msg);
93 }
94
95 void Core::recvStatusMsgFromServer(QString msg) {
96   Server *s = qobject_cast<Server*>(sender());
97   Q_ASSERT(s);
98   emit displayStatusMsg(s->getNetwork(), msg);
99 }
100
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;
109       return;
110     }
111   }
112   backLogDir.refresh();
113   //if(!backLogDir.isReadable()) {
114   //  qWarning(QString("Cannot read directory \"%1\". Disabling logging...").arg(backLogDir.absolutePath()).toAscii());
115   //  backLogEnabled = false;
116   //  return;
117   //}
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());
123       continue;
124     }
125     QDataStream in(&f);
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());
130       f.close(); continue;
131     }
132     if(vernum != BACKLOG_FORMAT) {
133       qWarning(QString("\"%1\": Version mismatch!").arg(f.fileName()).toAscii());
134       f.close(); continue;
135     }
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());
140     }
141     while(!in.atEnd()) {
142       Message m;
143       in >> m;
144       backLog.append(m);
145     }
146     f.close();
147   }
148   backLogEnabled = true;
149 }
150
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.
155  */
156 void Core::logMessage(Message msg) {
157   backLog.append(msg);
158   if(!currentLogFileDate.isValid() || currentLogFileDate < QDate::currentDate()) {
159     if(currentLogFile.isOpen()) currentLogFile.close();
160     currentLogFileDate = QDate::currentDate();
161   }
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());
166       return;
167     }
168     logStream.setDevice(&currentLogFile); logStream.setVersion(QDataStream::Qt_4_2);
169     if(!currentLogFile.size()) logStream << BACKLOG_STRING << (quint8)BACKLOG_FORMAT;
170   }
171   logStream << msg;
172 }
173
174 Core *core = 0;