Commit e65b7e0f authored by Lukas Appelhans's avatar Lukas Appelhans

Stuff needed for hooks

parent b8659725
......@@ -27,6 +27,7 @@
#include <archive.h>
#include <archive_entry.h>
#include "akabeihook.h"
Q_DECLARE_METATYPE(QUuid)
Q_DECLARE_METATYPE(QList< Akabei::Package* >)
......@@ -730,6 +731,26 @@ Group* Backend::loadGroupFromFile(const QString &path)
return group;
}
Hook* Backend::loadHookFromFile(const QString& path, QList<Package*> pkgs)
{
Q_D(Backend);
Hook * hook = 0;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return hook;
QString name = path.split('/').last().remove(".hook");
QString content;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
content = line + "\n";
}
hook = new Hook(name, content, pkgs);
return hook;
}
}
#include "akabeibackend.moc"
......@@ -22,6 +22,7 @@ class OperationRunner;
class Group;
class Package;
class Database;
class Hook;
class BackendPrivate;
class AKABEICORESHARED_EXPORT Backend : public QObject
......@@ -60,6 +61,7 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
Package *loadPackageFromFile(const QString &path);
Delta *loadDeltaFromFile(const QString &path);
Group *loadGroupFromFile(const QString &path);
Hook *loadHookFromFile(const QString &path, QList<Package*> pkgs);
public Q_SLOTS:
void initialize();
......
......@@ -70,6 +70,12 @@ namespace Groups {
const int ICONNAME = 3;
}
namespace Hooks {
const int NAME = 0;
const int HOOK = 1;
const int PACKAGES = 2;
}
}
const QUrl DEFAULT_DATABASE_DIR = QUrl("/var/local/akabei");
......
......@@ -17,6 +17,7 @@
#include "akabeidelta_p.h"
#include <sqlite3.h>
#include "akabeihook.h"
namespace Akabei {
......@@ -220,8 +221,6 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
Delta *DatabasePrivate::deltaFromRow(sqlite3_stmt *stmt)
{
Q_Q(Database);
const QString filename = QSTRING_FROM_DB(stmt, Tables::Deltas::FILENAME);
//Check if the deltaCache contains the dle
......@@ -248,6 +247,31 @@ Delta *DatabasePrivate::deltaFromRow(sqlite3_stmt *stmt)
return delta;
}
Hook *DatabasePrivate::hookFromRow(sqlite3_stmt *stmt)
{
const QString name = QSTRING_FROM_DB(stmt, Tables::Hooks::NAME);
//Check if the deltaCache contains the dle
if (hookCache.contains(name))
return hookCache[name];
using namespace Tables::Hooks;
QStringList pkgs = Helpers::stringlistFromDb(stmt, PACKAGES);
QList<Akabei::Package*> aPkgs;
foreach (const QString &p, pkgs)
aPkgs << packageCache[p.toInt()];
const QString &h = QSTRING_FROM_DB(stmt, HOOK);
// Otherwise go ahead, create the delta, add it to the cache, and return it.
Hook *hook = new Hook(name, h, aPkgs);
hookCache[name] = hook;
return hook;
}
Database::Database(const QString &pathToDatabase)
: d_ptr(new DatabasePrivate(this, pathToDatabase))
{
......@@ -392,4 +416,33 @@ QList<Delta*> Database::queryDeltas(const QString &sql)
return retlist;
}
QList<Hook*> Database::queryHooks(const QString &sql)
{
Q_D(Database);
QMutexLocker locker(d->mutex);
sqlite3_stmt *statement = 0;
int result = sqlite3_prepare16_v2(d->dbHandle, sql.constData(),
(sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
break;
default:
qDebug() << "Error while querying database";
return QList<Hook*>();
break;
}
QList<Hook*> retlist;
while (sqlite3_step(statement) == SQLITE_ROW) {
retlist << d->hookFromRow(statement);
}
sqlite3_finalize(statement);
return retlist;
}
}
......@@ -20,6 +20,7 @@ namespace Akabei {
class Group;
class Package;
class Delta;
class Hook;
class DatabasePrivate;
class AKABEICORESHARED_EXPORT Database
......@@ -42,6 +43,7 @@ public:
QList<Package*> queryPackages(const QString &sql);
QList<Group*> queryGroups(const QString &sql);
QList<Delta*> queryDeltas(const QString &sql);
QList<Hook*> queryHooks(const QString &sql);
private:
Database(const QString &pathToDatabase);
......
......@@ -37,12 +37,14 @@ public:
Package *packageFromRow(sqlite3_stmt *stmt);
Group *groupFromRow(sqlite3_stmt *stmt);
Delta *deltaFromRow(sqlite3_stmt *stmt);
Hook *hookFromRow(sqlite3_stmt *stmt);
QString pathToDb;
sqlite3 *dbHandle;
QHash<int, Package*> packageCache;
QHash<QString, Group*> groupCache;
QHash<QString, Delta*> deltaCache;
QHash<QString, Hook*> hookCache;
QMutex *mutex;
};
......
......@@ -14,8 +14,8 @@
#include <QTemporaryFile>
#include <qtextstream.h>
Akabei::Hook::Hook(const QString& name, const QString &content, Akabei::Database* db)
: d_ptr(new HookPrivate(name, content, db))
Akabei::Hook::Hook(const QString& name, const QString &content, QList<Akabei::Package*> pkgs)
: d_ptr(new HookPrivate(name, content, pkgs))
{
}
......@@ -31,10 +31,10 @@ QString Akabei::Hook::name() const
return d->name;
}
Akabei::Database* Akabei::Hook::database() const
QList< Akabei::Package* > Akabei::Hook::packages() const
{
Q_D(const Hook);
return d->database;
return d->packages;
}
QString Akabei::Hook::path()
......
......@@ -21,6 +21,7 @@ namespace Akabei {
*/
class HookPrivate;
class Database;
class Package;
class Hook
{
Q_DISABLE_COPY(Hook)
......@@ -30,7 +31,7 @@ class Hook
QString name() const;
Akabei::Database * database() const;
QList<Akabei::Package*> packages() const;
/**
* A temporary script file gets created.
......@@ -38,7 +39,7 @@ class Hook
QString path();
private:
Hook(const QString &name, const QString &content, Akabei::Database *db);
Hook(const QString &name, const QString &content, QList<Akabei::Package*> pkgs);
HookPrivate * const d_ptr;
......
......@@ -20,10 +20,10 @@ namespace Akabei {
class HookPrivate {
public:
HookPrivate(const QString &n, const QString &c, Akabei::Database * db)
HookPrivate(const QString &n, const QString &c, QList<Akabei::Package*> pkgs)
: name(n)
, content(c)
, database(db)
, packages(pkgs)
, file(0)
{}
......@@ -31,7 +31,7 @@ class HookPrivate {
QString name;
QString content;
Akabei::Database * database;
QList<Akabei::Package*> packages;
QTemporaryFile * file;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment