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

closed #2

parent 5b3126d2
...@@ -21,7 +21,7 @@ DEFINES += QT_DEPRECATED_WARNINGS ...@@ -21,7 +21,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \ SOURCES += main.cpp \
src/db/collectionDB.cpp \ src/db/collectionDB.cpp \
src/utils/pix.cpp \ src/pix.cpp \
src/db/db.cpp \ src/db/db.cpp \
src/db/dbactions.cpp src/db/dbactions.cpp
...@@ -45,7 +45,7 @@ RESOURCES += \ ...@@ -45,7 +45,7 @@ RESOURCES += \
kirigami-icons.qrc kirigami-icons.qrc
HEADERS += \ HEADERS += \
src/utils/pix.h \ src/pix.h \
src/db/collectionDB.h \ src/db/collectionDB.h \
src/db/fileloader.h \ src/db/fileloader.h \
src/db/db.h \ src/db/db.h \
......
...@@ -2,9 +2,14 @@ ...@@ -2,9 +2,14 @@
#include <QUuid> #include <QUuid>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QSqlQuery>
DB::DB(QObject *parent) : QObject(parent) DB::DB(QObject *parent) : QObject(parent)
{ {
QDir collectionDBPath_dir(PIX::CollectionDBPath);
if (!collectionDBPath_dir.exists())
collectionDBPath_dir.mkpath(".");
this->name = QUuid::createUuid().toString(); this->name = QUuid::createUuid().toString();
if(!PIX::fileExists(PIX::CollectionDBPath + PIX::DBName)) if(!PIX::fileExists(PIX::CollectionDBPath + PIX::DBName))
{ {
...@@ -92,22 +97,90 @@ void DB::prepareCollectionDB() const ...@@ -92,22 +97,90 @@ void DB::prepareCollectionDB() const
file.close(); 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) 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) 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) 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() bool DB::remove()
{ {
return false;
} }
...@@ -33,7 +33,13 @@ class DB : public QObject ...@@ -33,7 +33,13 @@ class DB : public QObject
/*basic public actions*/ /*basic public actions*/
void prepareCollectionDB() const; void prepareCollectionDB() const;
/* utils*/
bool checkExistance(const QString &tableName, const QString &searchId, const QString &search);
protected: protected:
QSqlQuery getQuery(const QString &queryTxt);
bool insert(const QString &tableName, const QVariantMap &insertData); bool insert(const QString &tableName, const QVariantMap &insertData);
bool update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where); 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); bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
......
...@@ -9,3 +9,97 @@ DBActions::~DBActions() ...@@ -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 ...@@ -9,6 +9,14 @@ class DBActions : public DB
public: public:
DBActions(QObject *parent = nullptr); DBActions(QObject *parent = nullptr);
~DBActions(); ~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 #endif // DBACTIONS_H
...@@ -5,119 +5,116 @@ ...@@ -5,119 +5,116 @@
#include <QObject> #include <QObject>
#include <QDirIterator> #include <QDirIterator>
#include <QFileInfo> #include <QFileInfo>
#include "collectionDB.h" #include "dbactions.h"
class FileLoader : public QObject class FileLoader : public DBActions
{ {
Q_OBJECT Q_OBJECT
public: public:
FileLoader() : QObject() 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();
}
~FileLoader()
{
this->go = false;
this->t.quit();
this->t.wait();
}
void requestPath(QString path)
{
qDebug()<<"FROM file loader"<< path;
this->queue << path;
for(auto url : this->queue)
{ {
if(!go) qRegisterMetaType<PIX::DB>("PIX::DB");
qRegisterMetaType<PIX::TABLE>("PIX::TABLE");
qRegisterMetaType<QMap<PIX::TABLE, bool>>("QMap<PIX::TABLE,bool>");
this->moveToThread(&t);
t.start();
}
~FileLoader()
{
this->go = false;
this->t.quit();
this->t.wait();
}
void requestPath(QString path)
{
qDebug()<<"FROM file loader"<< path;
this->queue << path;
for(auto url : this->queue)
{ {
this->go = true; if(!go)
QMetaObject::invokeMethod(this, "getPics", Q_ARG(QString, url)); {
this->queue.removeOne(url); this->go = true;
QMetaObject::invokeMethod(this, "getPics", Q_ARG(QString, url));
this->queue.removeOne(url);
}
} }
} }
}
void nextTrack() void nextTrack()
{ {
this->wait = !this->wait; this->wait = !this->wait;
} }
public slots: public slots:
void getPics(QString path) void getPics(QString path)
{ {
qDebug()<<"GETTING TRACKS FROM SETTINGS"; qDebug()<<"GETTING TRACKS FROM SETTINGS";
QStringList urls; QStringList urls;
if (QFileInfo(path).isDir()) if (QFileInfo(path).isDir())
{ {
QDirIterator it(path, PIX::formats, QDir::Files, QDirIterator::Subdirectories); QDirIterator it(path, PIX::formats, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) urls<<it.next(); while (it.hasNext()) urls<<it.next();
} else if (QFileInfo(path).isFile()) urls<<path; } else if (QFileInfo(path).isFile()) urls<<path;
if(urls.size()>0) if(urls.size()>0)
{
int newTracks = 0;
for(auto url : urls)
{ {
if(go) int newTracks = 0;
for(auto url : urls)
{ {
if(!con->check_existance(PIX::TABLEMAP[PIX::TABLE::IMAGES], PIX::KEYMAP[PIX::KEY::URL],url)) if(go)
{ {
QFileInfo info(url); if(!this->checkExistance(PIX::TABLEMAP[PIX::TABLE::IMAGES], PIX::KEYMAP[PIX::KEY::URL],url))
auto title = info.baseName();
auto format = info.suffix();
auto sourceUrl = info.dir().path();
PIX::DB trackMap =
{ {
{PIX::KEY::URL, url}, QFileInfo info(url);
{PIX::KEY::TITLE, title}, auto title = info.baseName();
{PIX::KEY::FAV, "0"}, auto format = info.suffix();
{PIX::KEY::RATE, "0"}, auto sourceUrl = info.dir().path();
{PIX::KEY::COLOR, ""},
{PIX::KEY::SOURCES_URL, sourceUrl},
{PIX::KEY::PIC_DATE, info.created().toString()} PIX::DB trackMap =
}; {
{PIX::KEY::URL, url},
this->con->addPic(trackMap); {PIX::KEY::TITLE, title},
newTracks++; {PIX::KEY::FAV, "0"},
{PIX::KEY::RATE, "0"},
} {PIX::KEY::COLOR, ""},
}else break; {PIX::KEY::SOURCES_URL, sourceUrl},
{PIX::KEY::PIC_DATE, info.created().toString()}
};
this->addPic(trackMap);
newTracks++;
}
}else break;
}
emit collectionSize(newTracks);
} }
emit collectionSize(newTracks); this->t.msleep(100);
emit this->finished();
this->go = false;
} }
this->t.msleep(100); signals:
emit this->finished(); void trackReady(PIX::DB track);
this->go = false; void finished();
} void collectionSize(int size);
signals: private:
void trackReady(PIX::DB track); QThread t;
void finished(); bool go = false;
void collectionSize(int size); bool wait = true;
QStringList queue;
private:
QThread t;
bool go = false;
bool wait = true;
QStringList queue;
CollectionDB *con;
}; };
......
#include "pix.h" #include "pix.h"
#include "../db/fileloader.h" #include "db/fileloader.h"
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
#include <QTimer> #include <QTimer>
#include <QApplication> #include <QApplication>
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
using namespace PIX; using namespace PIX;
Pix::Pix(QObject *parent) : QObject(parent) Pix::Pix(QObject *parent) : DBActions(parent)
{ {
qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath; qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath;
qDebug() << "Getting settings info from: " << PIX::SettingPath; qDebug() << "Getting settings info from: " << PIX::SettingPath;
...@@ -27,25 +27,19 @@ Pix::Pix(QObject *parent) : QObject(parent) ...@@ -27,25 +27,19 @@ Pix::Pix(QObject *parent) : QObject(parent)
// qDebug()<<"the knotify file got copied"; // qDebug()<<"the knotify file got copied";
// } // }
QDir collectionDBPath_dir(PIX::CollectionDBPath);
QDir cachePath_dir(PIX::CachePath); QDir cachePath_dir(PIX::CachePath);
if (!collectionDBPath_dir.exists())
collectionDBPath_dir.mkpath(".");
if (!cachePath_dir.exists()) if (!cachePath_dir.exists())
cachePath_dir.mkpath("."); cachePath_dir.mkpath(".");
this->con = new CollectionDB(this);
this->fileLoader = new FileLoader; 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, &Pix::populate, this, &Pix::populateDB);
connect(this->fileLoader, &FileLoader::finished,[this]() connect(this->fileLoader, &FileLoader::finished,[this]()
{ {
this->collectionWatcher();
emit refreshTables({{PIX::TABLEMAP[TABLE::ALBUMS], true}, emit refreshTables({{PIX::TABLEMAP[TABLE::ALBUMS], true},
{PIX::TABLEMAP[TABLE::TAGS], true}, {PIX::TABLEMAP[TABLE::TAGS], true},
{PIX::TABLEMAP[TABLE::IMAGES], true}}); {PIX::TABLEMAP[TABLE::IMAGES], true}});
...@@ -76,12 +70,12 @@ QVariantList Pix::getList(const QStringList &urls) ...@@ -76,12 +70,12 @@ QVariantList Pix::getList(const QStringList &urls)
QVariantList Pix::get(const QString &queryTxt) QVariantList Pix::get(const QString &queryTxt)
{ {
return this->con->getDBDataQML(queryTxt); return this->getDBDataQML(queryTxt);
} }
bool Pix::run(const QString &query) bool Pix::run(const QString &query)
{ {
return this->con->execQuery(query); return this->execQuery(query);
} }
void Pix::populateDB(const QString &path) void Pix::populateDB(const QString &path)
...@@ -95,58 +89,6 @@ void Pix::populateDB(const QString &path) ...@@ -95,58 +89,6 @@ void Pix::populateDB(const QString &path)
fileLoader->requestPath(newPath); 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);
}