diff --git a/QtClient/JoplinQtClient/application.cpp b/QtClient/JoplinQtClient/application.cpp index ae1e61cc7..f64033e85 100755 --- a/QtClient/JoplinQtClient/application.cpp +++ b/QtClient/JoplinQtClient/application.cpp @@ -101,7 +101,7 @@ void Application::afterSessionInitialization() { QString sessionId = settings.value("sessionId").toString(); qDebug() << "Session:" << sessionId; api_.setSessionId(sessionId); - synchronizer_.start(); + //synchronizer_.start(); } void Application::view_currentFolderChanged() { diff --git a/QtClient/JoplinQtClient/models/folder.cpp b/QtClient/JoplinQtClient/models/folder.cpp index d9e3d38b1..de40cbc22 100755 --- a/QtClient/JoplinQtClient/models/folder.cpp +++ b/QtClient/JoplinQtClient/models/folder.cpp @@ -1,7 +1,68 @@ #include "models/folder.h" +#include "database.h" +#include "uuid.h" + using namespace jop; Folder::Folder() { } + +bool Folder::isNew() const { + return id().isEmpty(); +} + +bool Folder::save() { + bool isNew = this->isNew(); + + QStringList fields; + VariantVector values; + if (isNew) { + setId(uuid::createUuid()); + fields << "id"; + values << id(); + } + fields << "title" << "synced"; + values << title() << QVariant(0); + + if (isNew) { + QSqlQuery q = jop::db().buildSqlQuery(Database::Insert, "folders", fields, values); + q.exec(); + return jop::db().errorCheck(q); + } else { + QSqlQuery q = jop::db().buildSqlQuery(Database::Update, "folders", fields, values, "id = \"" + id() + "\""); + q.exec(); + return jop::db().errorCheck(q); + } +} + +bool Folder::dispose() { + QSqlQuery q(jop::db().database()); + q.prepare("DELETE FROM folders WHERE id = :id"); + q.bindValue(":id", id()); + q.exec(); + return jop::db().errorCheck(q); +} + +int Folder::count() { + QSqlQuery q = jop::db().query("SELECT count(*) as row_count FROM folders"); + q.exec(); + q.next(); + return q.value(0).toInt(); +} + +QVector Folder::all(const QString &orderBy) { + QSqlQuery q = jop::db().query("SELECT " + Folder::dbFields().join(",") + " FROM folders ORDER BY " + orderBy); + q.exec(); + + QVector output; + + while (q.next()) { + Folder folder; + folder.fromSqlQuery(q); + output.push_back(folder); + } + + return output; +} diff --git a/QtClient/JoplinQtClient/models/folder.h b/QtClient/JoplinQtClient/models/folder.h index 12cf38846..2b65eb198 100755 --- a/QtClient/JoplinQtClient/models/folder.h +++ b/QtClient/JoplinQtClient/models/folder.h @@ -11,8 +11,12 @@ class Folder : public Item { public: Folder(); + bool isNew() const; + bool save(); + bool dispose(); - + static int count(); + static QVector all(const QString& orderBy); private: diff --git a/QtClient/JoplinQtClient/models/foldermodel.cpp b/QtClient/JoplinQtClient/models/foldermodel.cpp index d856c46da..35ff75075 100755 --- a/QtClient/JoplinQtClient/models/foldermodel.cpp +++ b/QtClient/JoplinQtClient/models/foldermodel.cpp @@ -8,11 +8,7 @@ FolderModel::FolderModel(Database &database) : QAbstractListModel(), db_(databas } int FolderModel::rowCount(const QModelIndex & parent) const { - Q_UNUSED(parent); - QSqlQuery q = db_.query("SELECT count(*) as row_count FROM folders"); - q.exec(); - q.next(); - return q.value(0).toInt() + (virtualItemShown_ ? 1 : 0); + return Folder::count() + (virtualItemShown_ ? 1 : 0); } // NOTE: to lazy load - send back "Loading..." if item not currently loaded @@ -42,17 +38,8 @@ bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int r Folder folder = atIndex(index.row()); if (role == Qt::EditRole) { - emit dataChanging(); - - QStringList fields; - VariantVector values; - fields << "title" << "synced"; - values << value << QVariant(0); - - QSqlQuery q = db_.buildSqlQuery(Database::Update, "folders", fields, values, "id = \"" + folder.id() + "\""); - q.exec(); - if (!db_.errorCheck(q)) return false; - + folder.setTitle(value.toString()); + if (!folder.save()) return false; cache_.clear(); QVector roles; @@ -77,14 +64,7 @@ Folder FolderModel::atIndex(int index) const { cache_.clear(); - QSqlQuery q = db_.query("SELECT " + Folder::dbFields().join(",") + " FROM folders ORDER BY " + orderBy_); - q.exec(); - - while (q.next()) { - Folder folder; - folder.fromSqlQuery(q); - cache_.push_back(folder); - } + cache_ = Folder::all(orderBy_); if (!cache_.size()) { qWarning() << "Invalid folder index:" << index; @@ -144,19 +124,13 @@ QHash FolderModel::roleNames() const { } void FolderModel::addData(const QString &title) { - QStringList fields; - VariantVector values; - QString folderId = uuid::createUuid(); - fields << "id" << "title" << "synced"; - values << folderId << QVariant(title) << QVariant(0); - - QSqlQuery q = db_.buildSqlQuery(Database::Insert, "folders", fields, values); - q.exec(); - if (!db_.errorCheck(q)) return; + Folder folder; + folder.setTitle(title); + if (!folder.save()) return; cache_.clear(); - lastInsertId_ = folderId; + lastInsertId_ = folder.id(); QVector roles; roles << Qt::DisplayRole; @@ -173,13 +147,8 @@ void FolderModel::addData(const QString &title) { } void FolderModel::deleteData(const int index) { - QString folderId = idAtIndex(index); - - QSqlQuery q(db_.database()); - q.prepare("DELETE FROM folders WHERE id = :id"); - q.bindValue(":id", folderId); - q.exec(); - if (!db_.errorCheck(q)) return; + Folder folder = atIndex(index); + if (!folder.dispose()) return; cache_.clear();