Added install rules (at least for *nix)
[quassel.git] / core / storage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 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 "storage.h"
22
23
24 // OBSOLETE
25 // This is kept here for importing the old file-based backlog.
26
27 /* This is a sample!
28
29 void Storage::importOldBacklog() {
30   qDebug() << "Deleting backlog database...";
31   logDb.exec(QString("DELETE FROM 'Backlog$%1$' WHERE SenderId != '$VERSION$'").arg(user));
32   logDb.exec(QString("DELETE FROM 'Senders$%1$'").arg(user));
33   logDb.exec(QString("DELETE FROM 'Buffers$%1$'").arg(user));
34   nextMsgId = 1; nextBufferId = 1; nextSenderId = 1;
35   qDebug() << "Importing old backlog files...";
36   initBackLogOld();
37   if(!backLogEnabledOld) return;
38   logDb.exec("VACUUM");
39   qDebug() << "Backlog successfully imported, you have to restart Quassel now!";
40   exit(0);
41
42 }
43 */
44
45 // file name scheme: quassel-backlog-2006-29-10.bin
46 void Storage::initBackLogOld(UserId uid) {
47   backLogDir = QDir(Global::quasselDir + "/backlog");
48   if(!backLogDir.exists()) {
49     qWarning(QString("Creating backlog directory \"%1\"...").arg(backLogDir.absolutePath()).toAscii());
50     if(!backLogDir.mkpath(backLogDir.absolutePath())) {
51       qWarning(QString("Could not create backlog directory! Disabling logging...").toAscii());
52       backLogEnabledOld = false;
53       return;
54     }
55   }
56   backLogDir.refresh();
57   //if(!backLogDir.isReadable()) {
58   //  qWarning(QString("Cannot read directory \"%1\". Disabling logging...").arg(backLogDir.absolutePath()).toAscii());
59   //  backLogEnabled = false;
60   //  return;
61   //}
62   QStringList networks = backLogDir.entryList(QDir::Dirs|QDir::NoDotAndDotDot|QDir::Readable, QDir::Name);
63   foreach(QString net, networks) {
64     QDir dir(backLogDir.absolutePath() + "/" + net);
65     if(!dir.exists()) {
66       qWarning(QString("Could not change to directory \"%1\"!").arg(dir.absolutePath()).toAscii());
67       continue;
68     }
69     QStringList logs = dir.entryList(QStringList("quassel-backlog-*.bin"), QDir::Files|QDir::Readable, QDir::Name);
70     foreach(QString name, logs) {
71       QFile f(dir.absolutePath() + "/" + name);
72       if(!f.open(QIODevice::ReadOnly)) {
73         qWarning(QString("Could not open \"%1\" for reading!").arg(f.fileName()).toAscii());
74         continue;
75       }
76       QDataStream in(&f);
77       in.setVersion(QDataStream::Qt_4_2);
78       QByteArray verstring; quint8 vernum; in >> verstring >> vernum;
79       if(verstring != BACKLOG_STRING) {
80         qWarning(QString("\"%1\" is not a Quassel backlog file!").arg(f.fileName()).toAscii());
81         f.close(); continue;
82       }
83       if(vernum != BACKLOG_FORMAT) {
84         qWarning(QString("\"%1\": Version mismatch!").arg(f.fileName()).toAscii());
85         f.close(); continue;
86       }
87       qDebug() << "Reading backlog from" << f.fileName();
88       logFileDates[net] = QDate::fromString(f.fileName(),
89                                             QString("'%1/quassel-backlog-'yyyy-MM-dd'.bin'").arg(dir.absolutePath()));
90       if(!logFileDates[net].isValid()) {
91         qWarning(QString("\"%1\" has an invalid file name!").arg(f.fileName()).toAscii());
92       }
93       while(!in.atEnd()) {
94         quint8 t, f;
95         quint32 ts;
96         QByteArray s, m, targ;
97         in >> ts >> t >> f >> targ >> s >> m;
98         QString target = QString::fromUtf8(targ);
99         QString sender = QString::fromUtf8(s);
100         QString text = QString::fromUtf8(m);
101         BufferId id;
102         if((f & Message::PrivMsg) && !(f & Message::Self)) {
103           id = getBufferId(uid, net, sender);
104         } else {
105           id = getBufferId(uid, net, target);
106         }
107         Message msg(QDateTime::fromTime_t(ts), id, (Message::Type)t, text, sender, f);
108         //backLog[net].append(m);
109         logMessage(msg);
110       }
111       f.close();
112     }
113   }
114   backLogEnabledOld = true;
115 }
116
117