Commit 2b7bbc6b authored by Camilo Higuita's avatar Camilo Higuita

closed #2

parent 5b3126d2
......@@ -21,7 +21,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \
src/db/collectionDB.cpp \
src/utils/pix.cpp \
src/pix.cpp \
src/db/db.cpp \
src/db/dbactions.cpp
......@@ -45,7 +45,7 @@ RESOURCES += \
kirigami-icons.qrc
HEADERS += \
src/utils/pix.h \
src/pix.h \
src/db/collectionDB.h \
src/db/fileloader.h \
src/db/db.h \
......
......@@ -2,9 +2,14 @@
#include <QUuid>
#include <QString>
#include <QStringList>
#include <QSqlQuery>
DB::DB(QObject *parent) : QObject(parent)
{
QDir collectionDBPath_dir(PIX::CollectionDBPath);
if (!collectionDBPath_dir.exists())
collectionDBPath_dir.mkpath(".");
this->name = QUuid::createUuid().toString();
if(!PIX::fileExists(PIX::CollectionDBPath + PIX::DBName))
{
......@@ -92,22 +97,90 @@ void DB::prepareCollectionDB() const
file.close();
}
bool DB::checkExistance(const QString &tableName, const QString &searchId, const QString &search)
{
auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(searchId, tableName, searchId, search);
auto query = this->getQuery(queryStr);
if (query.exec())
{
if (query.next()) return true;
}else qDebug()<<query.lastError().text();
return false;
}
QSqlQuery DB::getQuery(const QString &queryTxt)
{
QSqlQuery query(queryTxt, this->m_db);
return query;
}
bool DB::insert(const QString &tableName, const QVariantMap &insertData)
{
if (tableName.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The table name is empty!");
return false;
} else if (insertData.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The insertData is empty!");
return false;
}
QStringList strValues;
QStringList fields = insertData.keys();
QVariantList values = insertData.values();
int totalFields = fields.size();
for (int i = 0; i < totalFields; ++i)
strValues.append("?");
QString sqlQueryString = "INSERT INTO " + tableName + " (" + QString(fields.join(",")) + ") VALUES(" + QString(strValues.join(",")) + ")";
QSqlQuery query(this->m_db);
query.prepare(sqlQueryString);
int k = 0;
foreach (const QVariant &value, values)
query.bindValue(k++, value);
return query.exec();
}
bool DB::update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where)
{
if (tableName.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The table name is empty!");
return false;
} else if (updateData.isEmpty())
{
qDebug()<<QStringLiteral("Fatal error on insert! The insertData is empty!");
return false;
}
QStringList set;
for (auto key : updateData.keys())
set.append(PIX::KEYMAP[key]+" = '"+updateData[key]+"'");
QStringList condition;
for (auto key : where.keys())
condition.append(key+" = '"+where[key].toString()+"'");
QString sqlQueryString = "UPDATE " + tableName + " SET " + QString(set.join(",")) + " WHERE " + QString(condition.join(",")) ;
auto query = this->getQuery(sqlQueryString);
qDebug()<<sqlQueryString;
return query.exec();
}
bool DB::update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id)
{
auto queryStr = QString("UPDATE %1 SET %2 = \"%3\" WHERE %4 = \"%5\"").arg(table, column, newValue.toString().replace("\"","\"\""), op.toString(), id);
auto query = this->getQuery(queryStr);
return query.exec();
}
bool DB::remove()
{
return false;
}
......@@ -33,7 +33,13 @@ class DB : public QObject
/*basic public actions*/
void prepareCollectionDB() const;
/* utils*/
bool checkExistance(const QString &tableName, const QString &searchId, const QString &search);
protected:
QSqlQuery getQuery(const QString &queryTxt);
bool insert(const QString &tableName, const QVariantMap &insertData);
bool update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where);
bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
......
......@@ -9,3 +9,97 @@ DBActions::~DBActions()
{
}
PIX::DB_LIST DBActions::getDBData(const QString &queryTxt)
{
PIX::DB_LIST mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
PIX::DB data;
for(auto key : PIX::KEYMAP.keys())
if(query.record().indexOf(PIX::KEYMAP[key])>-1)
data.insert(key, query.value(PIX::KEYMAP[key]).toString());
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
QVariantList DBActions::getDBDataQML(const QString &queryTxt)
{
QVariantList mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
QVariantMap data;
for(auto key : PIX::KEYMAP.keys())
if(query.record().indexOf(PIX::KEYMAP[key])>-1)
data[PIX::KEYMAP[key]] = query.value(PIX::KEYMAP[key]).toString();
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
bool DBActions::execQuery(const QString &queryTxt)
{
auto query = this->getQuery(queryTxt);
return query.exec();
}
void DBActions::addPic(const PIX::DB &img)
{
auto query = this->getQuery("PRAGMA synchronous=OFF");
if(query.exec())
{
auto url = img[PIX::KEY::URL];
auto title = img[PIX::KEY::TITLE];
auto rate = img[PIX::KEY::RATE];
auto fav = img[PIX::KEY::FAV];
auto color = img[PIX::KEY::COLOR];
auto addDate = img[PIX::KEY::ADD_DATE];
auto sourceUrl = img[PIX::KEY::SOURCES_URL];
auto picDate = img[PIX::KEY::PIC_DATE];
auto note = img[PIX::KEY::NOTE];
auto place = img[PIX::KEY::PLACE];
auto format = img[PIX::KEY::FORMAT];
qDebug()<< "writting to db: "<<title<<url;
/* first needs to insert album and artist*/
QVariantMap sourceMap {{PIX::KEYMAP[PIX::KEY::URL],sourceUrl}};
insert(PIX::TABLEMAP[PIX::TABLE::SOURCES], sourceMap);
QVariantMap imgMap {{PIX::KEYMAP[PIX::KEY::URL], url},
{PIX::KEYMAP[PIX::KEY::SOURCES_URL], sourceUrl},
{PIX::KEYMAP[PIX::KEY::TITLE], title},
{PIX::KEYMAP[PIX::KEY::RATE], rate},
{PIX::KEYMAP[PIX::KEY::FAV], fav},
{PIX::KEYMAP[PIX::KEY::COLOR], color},
{PIX::KEYMAP[PIX::KEY::FORMAT], format},
{PIX::KEYMAP[PIX::KEY::PIC_DATE], picDate},
{PIX::KEYMAP[PIX::KEY::NOTE], note},
{PIX::KEYMAP[PIX::KEY::PLACE], place},
{PIX::KEYMAP[PIX::KEY::ADD_DATE], QDateTime::currentDateTime()}};
insert(PIX::TABLEMAP[PIX::TABLE::IMAGES], imgMap);
}else
{
qDebug()<< "Failed to insert async";
}
}
......@@ -9,6 +9,14 @@ class DBActions : public DB
public:
DBActions(QObject *parent = nullptr);
~DBActions();
PIX::DB_LIST getDBData(const QString &queryTxt);
QVariantList getDBDataQML(const QString &queryTxt);
bool execQuery(const QString &queryTxt);
void addPic(const PIX::DB &img);
};
#endif // DBACTIONS_H
......@@ -5,20 +5,18 @@
#include <QObject>
#include <QDirIterator>
#include <QFileInfo>
#include "collectionDB.h"
#include "dbactions.h"
class FileLoader : public QObject
class FileLoader : public DBActions
{
Q_OBJECT
public:
FileLoader() : QObject()
public:
FileLoader() : DBActions()
{
qRegisterMetaType<PIX::DB>("PIX::DB");
qRegisterMetaType<PIX::TABLE>("PIX::TABLE");
qRegisterMetaType<QMap<PIX::TABLE, bool>>("QMap<PIX::TABLE,bool>");
this->con = new CollectionDB(this);
this->moveToThread(&t);
t.start();
}
......@@ -50,7 +48,7 @@ public:
this->wait = !this->wait;
}
public slots:
public slots:
void getPics(QString path)
{
......@@ -73,7 +71,7 @@ public slots:
{
if(go)
{
if(!con->check_existance(PIX::TABLEMAP[PIX::TABLE::IMAGES], PIX::KEYMAP[PIX::KEY::URL],url))
if(!this->checkExistance(PIX::TABLEMAP[PIX::TABLE::IMAGES], PIX::KEYMAP[PIX::KEY::URL],url))
{
QFileInfo info(url);
auto title = info.baseName();
......@@ -92,7 +90,7 @@ public slots:
{PIX::KEY::PIC_DATE, info.created().toString()}
};
this->con->addPic(trackMap);
this->addPic(trackMap);
newTracks++;
}
......@@ -107,17 +105,16 @@ public slots:
this->go = false;
}
signals:
signals:
void trackReady(PIX::DB track);
void finished();
void collectionSize(int size);
private:
private:
QThread t;
bool go = false;
bool wait = true;
QStringList queue;
CollectionDB *con;
};
......
#include "pix.h"
#include "../db/fileloader.h"
#include "db/fileloader.h"
#include <QFileSystemWatcher>
#include <QTimer>
#include <QApplication>
......@@ -12,7 +12,7 @@
using namespace PIX;
Pix::Pix(QObject *parent) : QObject(parent)
Pix::Pix(QObject *parent) : DBActions(parent)
{
qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath;
qDebug() << "Getting settings info from: " << PIX::SettingPath;
......@@ -27,25 +27,19 @@ Pix::Pix(QObject *parent) : QObject(parent)
// qDebug()<<"the knotify file got copied";
// }
QDir collectionDBPath_dir(PIX::CollectionDBPath);
QDir cachePath_dir(PIX::CachePath);
if (!collectionDBPath_dir.exists())
collectionDBPath_dir.mkpath(".");
if (!cachePath_dir.exists())
cachePath_dir.mkpath(".");
this->con = new CollectionDB(this);
this->fileLoader = new FileLoader;
this->watcher = new QFileSystemWatcher(this);
connect(this->watcher, &QFileSystemWatcher::directoryChanged, this, &Pix::handleDirectoryChanged);
connect(this, &Pix::populate, this, &Pix::populateDB);
connect(this->fileLoader, &FileLoader::finished,[this]()
{
this->collectionWatcher();
emit refreshTables({{PIX::TABLEMAP[TABLE::ALBUMS], true},
{PIX::TABLEMAP[TABLE::TAGS], true},
{PIX::TABLEMAP[TABLE::IMAGES], true}});
......@@ -76,12 +70,12 @@ QVariantList Pix::getList(const QStringList &urls)
QVariantList Pix::get(const QString &queryTxt)
{
return this->con->getDBDataQML(queryTxt);
return this->getDBDataQML(queryTxt);
}
bool Pix::run(const QString &query)
{
return this->con->execQuery(query);
return this->execQuery(query);
}
void Pix::populateDB(const QString &path)
......@@ -95,58 +89,6 @@ void Pix::populateDB(const QString &path)
fileLoader->requestPath(newPath);
}
void Pix::collectionWatcher()
{
auto queryTxt = QString("SELECT %1 FROM %2").arg(PIX::KEYMAP[PIX::KEY::URL], PIX::TABLEMAP[PIX::TABLE::IMAGES]);
for (auto track : this->con->getDBData(queryTxt))
{
auto location = track[PIX::KEY::URL];
if (!this->dirs.contains(QFileInfo(location).dir().path()) && PIX::fileExists(location)) //check if parent dir isn't already in list and it exists
{
QString dir = QFileInfo(location).dir().path();
this->dirs << dir;
QDirIterator it(dir, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); // get all the subdirectories to watch
while (it.hasNext())
{
QString subDir = QFileInfo(it.next()).path();
if(QFileInfo(subDir).isDir() && !this->dirs.contains(subDir) && PIX::fileExists(subDir))
this->dirs <<subDir;
}
}
}
this->addToWatcher(this->dirs);
}
void Pix::addToWatcher(QStringList paths)
{
qDebug()<<"duplicated paths in watcher removd: "<<paths.removeDuplicates();
if(!paths.isEmpty()) watcher->addPaths(paths);
}
void Pix::handleDirectoryChanged(const QString &dir)
{
qDebug()<<"directory changed:"<<dir;
auto wait = new QTimer(this);
wait->setSingleShot(true);
wait->setInterval(1000);
connect(wait, &QTimer::timeout,[=]()
{
emit populate(dir);
wait->deleteLater();
});
wait->start();
}
QString Pix::backgroundColor()
{
#if defined(Q_OS_ANDROID)
......
......@@ -11,16 +11,15 @@
#include <QSettings>
#include <QDirIterator>
#include <QVariantList>
#include "db/dbactions.h"
using namespace std;
class CollectionDB;
class FileLoader;
class QFileSystemWatcher;
class Pix : public QObject
class Pix : public DBActions
{
Q_OBJECT
......@@ -51,15 +50,9 @@ public:
private:
CollectionDB *con;
FileLoader *fileLoader;
QStringList dirs;
QFileSystemWatcher *watcher;
void populateDB(const QString &path);
void collectionWatcher();
void addToWatcher(QStringList paths);
void handleDirectoryChanged(const QString &dir);
signals:
void refreshTables(QVariantMap tables);
......
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