diff --git a/qtox.pro b/qtox.pro index 67c3f20a2..d839c36e5 100644 --- a/qtox.pro +++ b/qtox.pro @@ -139,7 +139,10 @@ HEADERS += src/widget/form/addfriendform.h \ src/cameraworker.h \ src/widget/videosurface.h \ src/widget/form/loadhistorydialog.h \ - src/historykeeper.h + src/historykeeper.h \ + src/misc/db/genericddinterface.h \ + src/misc/db/plaindb.h \ + src/misc/db/encrypteddb.h SOURCES += \ src/widget/form/addfriendform.cpp \ @@ -188,4 +191,7 @@ SOURCES += \ src/cameraworker.cpp \ src/widget/videosurface.cpp \ src/widget/form/loadhistorydialog.cpp \ - src/historykeeper.cpp + src/historykeeper.cpp \ + src/misc/db/genericddinterface.cpp \ + src/misc/db/plaindb.cpp \ + src/misc/db/encrypteddb.cpp diff --git a/src/misc/db/encrypteddb.cpp b/src/misc/db/encrypteddb.cpp new file mode 100644 index 000000000..5c3f97932 --- /dev/null +++ b/src/misc/db/encrypteddb.cpp @@ -0,0 +1,60 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#include "encrypteddb.h" +#include "src/misc/settings.h" + +#include +#include + +EncryptedDb::EncryptedDb(const QString &fname, const QString &key) : + key(key), encrFile(fname), PlainDb(":memory:") +{ + QList sqlCommands = decryptFile(); + for (const QString &cmd : sqlCommands) + { + PlainDb::exec(true, cmd); + } +} + +EncryptedDb::~EncryptedDb() +{ + // save to file if necessary +} + +QSqlQuery EncryptedDb::exec(bool keep, const QString &query) +{ + QSqlQuery ret = PlainDb::exec(keep, query); + if (keep) + appendToEncrypted(query); + + return ret; +} + +bool EncryptedDb::save() +{ + return true; +} + +QList EncryptedDb::decryptFile() +{ + return QList(); +} + +void EncryptedDb::appendToEncrypted(const QString &sql) +{ + Q_UNUSED(sql) +} diff --git a/src/misc/db/encrypteddb.h b/src/misc/db/encrypteddb.h new file mode 100644 index 000000000..c4e4ce52c --- /dev/null +++ b/src/misc/db/encrypteddb.h @@ -0,0 +1,44 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#ifndef ENCRYPTEDDB_H +#define ENCRYPTEDDB_H + +#include "plaindb.h" + +#include + +class EncryptedDb : public PlainDb +{ +public: + EncryptedDb(const QString& fname, const QString &key); + virtual ~EncryptedDb(); + + virtual QSqlQuery exec(bool keep, const QString &query); + virtual bool save(); + +private: + QString getFileName(){return encrFile;} + QString getKey(){return key;} + + QList decryptFile(); + void appendToEncrypted(const QString &sql); + + QString key; + QString encrFile; +}; + +#endif // ENCRYPTEDDB_H diff --git a/src/misc/db/genericddinterface.cpp b/src/misc/db/genericddinterface.cpp new file mode 100644 index 000000000..a99eab4b7 --- /dev/null +++ b/src/misc/db/genericddinterface.cpp @@ -0,0 +1,21 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#include "genericddinterface.h" + +GenericDdInterface::~GenericDdInterface() +{ +} diff --git a/src/misc/db/genericddinterface.h b/src/misc/db/genericddinterface.h new file mode 100644 index 000000000..79b4c3ca2 --- /dev/null +++ b/src/misc/db/genericddinterface.h @@ -0,0 +1,32 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#ifndef GENERICDDINTERFACE_H +#define GENERICDDINTERFACE_H + +class QSqlQuery; +class QString; + +class GenericDdInterface +{ +public: + virtual ~GenericDdInterface(); + + virtual QSqlQuery exec(bool keep, const QString &query) = 0; + virtual bool save() = 0; +}; + +#endif // GENERICDDINTERFACE_H diff --git a/src/misc/db/plaindb.cpp b/src/misc/db/plaindb.cpp new file mode 100644 index 000000000..4b5300148 --- /dev/null +++ b/src/misc/db/plaindb.cpp @@ -0,0 +1,49 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#include "plaindb.h" +#include +#include +#include + +PlainDb::PlainDb(const QString &db_name) +{ + db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(db_name); + + if (!db.open()) + { + qWarning() << QString("Can't open file: %1, history will not be saved!").arg(db_name); + db.setDatabaseName(":memory:"); + db.open(); + } +} + +PlainDb::~PlainDb() +{ + db.close(); +} + +QSqlQuery PlainDb::exec(bool keep, const QString &query) +{ + Q_UNUSED(keep) + return db.exec(query); +} + +bool PlainDb::save() +{ + return true; +} diff --git a/src/misc/db/plaindb.h b/src/misc/db/plaindb.h new file mode 100644 index 000000000..d13d7c2a0 --- /dev/null +++ b/src/misc/db/plaindb.h @@ -0,0 +1,37 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#ifndef PLAINDB_H +#define PLAINDB_H + +#include "genericddinterface.h" + +#include + +class PlainDb : public GenericDdInterface +{ +public: + PlainDb(const QString &db_name); + virtual ~PlainDb(); + + virtual QSqlQuery exec(bool keep, const QString &query); + virtual bool save(); + +private: + QSqlDatabase db; +}; + +#endif // PLAINDB_H