1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-03 23:50:33 +02:00
Files
joplin/QtClient/JoplinQtClient/models/folder.cpp

172 lines
5.1 KiB
C++
Raw Normal View History

2016-12-10 22:39:53 +00:00
#include "models/folder.h"
2017-01-01 23:20:06 +01:00
#include "database.h"
namespace jop {
2016-12-10 22:39:53 +00:00
2017-01-21 11:47:24 +00:00
Folder::Folder() : Item() {
table_ = jop::FoldersTable;
2016-12-10 22:39:53 +00:00
}
2017-01-01 23:20:06 +01:00
2017-01-21 11:47:24 +00:00
//Table Folder::table() const {
// return jop::FoldersTable;
//}
2017-01-02 15:04:27 +01:00
bool Folder::primaryKeyIsUuid() const {
return true;
}
2017-01-02 15:04:27 +01:00
bool Folder::trackChanges() const {
return true;
}
2017-01-01 23:20:06 +01:00
2017-01-12 17:59:19 +01:00
int Folder::noteCount() const {
QSqlQuery q = jop::db().prepare(QString("SELECT count(*) AS row_count FROM %1 WHERE parent_id = :parent_id").arg(BaseModel::tableName(jop::NotesTable)));
q.bindValue(":parent_id", id().toString());
jop::db().execQuery(q);
q.next();
return q.value(0).toInt();
}
2017-02-06 21:18:03 +00:00
std::unique_ptr<Folder> Folder::root() {
std::unique_ptr<Folder> folder(new Folder());
return std::move(folder);
}
std::vector<std::unique_ptr<BaseModel>> Folder::children(const QString &orderBy, int limit, int offset) const {
std::vector<std::unique_ptr<BaseModel>> output;
std::vector<jop::Table> tables;
tables.push_back(jop::FoldersTable);
tables.push_back(jop::NotesTable);
2017-02-07 19:42:35 +00:00
for (size_t tableIndex = 0; tableIndex < tables.size(); tableIndex++) {
2017-02-06 21:18:03 +00:00
jop::Table table = tables[tableIndex];
QString sql = QString("SELECT %1 FROM %2 WHERE parent_id = :parent_id ORDER BY %3 %4 %5")
.arg(BaseModel::sqlTableFields(table))
.arg(BaseModel::tableName(table))
.arg(orderBy)
.arg(limit ? QString("LIMIT %1").arg(limit) : "")
.arg(limit && offset ? QString("OFFSET %1").arg(offset) : "");
QSqlQuery q = jop::db().prepare(sql);
q.bindValue(":parent_id", idString());
jop::db().execQuery(q);
if (!jop::db().errorCheck(q)) return output;
while (q.next()) {
if (table == jop::FoldersTable) {
std::unique_ptr<BaseModel> folder(new Folder());
folder->loadSqlQuery(q);
output.push_back(std::move(folder));
} else if (table == jop::NotesTable) {
std::unique_ptr<BaseModel> note(new Note());
note->loadSqlQuery(q);
output.push_back(std::move(note));
}
}
}
return output;
}
std::vector<std::unique_ptr<Note>> Folder::notes(const QString &orderBy, int limit, int offset) const {
std::vector<std::unique_ptr<Note>> output;
2017-01-12 17:59:19 +01:00
2017-01-12 19:11:58 +01:00
QSqlQuery q = jop::db().prepare(QString("SELECT %1 FROM %2 WHERE parent_id = :parent_id ORDER BY %3 LIMIT %4 OFFSET %5")
2017-01-12 17:59:19 +01:00
.arg(BaseModel::sqlTableFields(jop::NotesTable))
2017-01-12 19:11:58 +01:00
.arg(BaseModel::tableName(jop::NotesTable))
2017-01-12 17:59:19 +01:00
.arg(orderBy)
.arg(limit)
.arg(offset));
q.bindValue(":parent_id", id().toString());
jop::db().execQuery(q);
if (!jop::db().errorCheck(q)) return output;
while (q.next()) {
std::unique_ptr<Note> note(new Note());
note->loadSqlQuery(q);
output.push_back(std::move(note));
2017-01-12 17:59:19 +01:00
}
return output;
}
2017-02-06 21:18:03 +00:00
std::vector<std::unique_ptr<Folder>> Folder::pathToFolders(const QString& path, bool returnLast, int& errorCode) {
2017-02-03 20:47:45 +00:00
std::vector<std::unique_ptr<Folder>> output;
2017-02-06 21:18:03 +00:00
if (!path.length()) return output;
2017-02-03 20:47:45 +00:00
QStringList parts = path.split('/');
2017-02-06 21:18:03 +00:00
QString parentId("");
int toIndex = returnLast ? parts.size() : parts.size() - 1;
for (int i = 0; i < toIndex; i++) {
2017-02-03 20:47:45 +00:00
std::unique_ptr<Folder> folder(new Folder());
bool ok = folder->loadByField(parentId, "title", parts[i]);
2017-02-06 21:18:03 +00:00
if (!ok) {
// qWarning() << "Folder does not exist" << parts[i];
errorCode = 1;
return output;
}
2017-02-03 20:47:45 +00:00
output.push_back(std::move(folder));
}
return output;
}
2017-02-06 21:18:03 +00:00
QString Folder::pathBaseName(const QString& path) {
QStringList parts = path.split('/');
return parts[parts.size() - 1];
}
2017-01-12 19:11:58 +01:00
int Folder::noteIndexById(const QString &orderBy, const QString& id) const {
2017-01-31 21:20:13 +00:00
qDebug() << "Folder::noteIndexById" << orderBy << id;
QSqlQuery q = jop::db().prepare(QString("SELECT id, %2 FROM %1 WHERE parent_id = :parent_id ORDER BY %2")
2017-01-12 19:11:58 +01:00
.arg(BaseModel::tableName(jop::NotesTable))
.arg(orderBy));
q.bindValue(":parent_id", idString());
jop::db().execQuery(q);
if (!jop::db().errorCheck(q)) return -1;
int index = 0;
while (q.next()) {
QString qId = q.value(0).toString();
2017-01-31 21:20:13 +00:00
QString qTitle = q.value(1).toString();
qDebug() << "CURRENT" << qId << qTitle;
2017-01-12 19:11:58 +01:00
if (qId == id) return index;
index++;
}
return -1;
}
2017-02-06 20:20:30 +00:00
int Folder::count(const QString &parentId) {
return BaseModel::count(jop::FoldersTable, parentId);
2017-01-01 23:20:06 +01:00
}
2017-02-07 19:42:35 +00:00
// std::vector<std::unique_ptr<Folder>> Folder::all(const QString& parentId, const QString &orderBy) {
// QSqlQuery q = jop::db().prepare(QString("SELECT %1 FROM %2 WHERE parent_id = :parent_id ORDER BY %3")
// .arg(BaseModel::tableFieldNames(jop::FoldersTable).join(","))
// .arg(BaseModel::tableName(jop::FoldersTable))
// .arg(orderBy));
// q.bindValue(":parent_id", parentId);
// jop::db().execQuery(q);
2017-01-01 23:20:06 +01:00
2017-02-07 19:42:35 +00:00
// std::vector<std::unique_ptr<Folder>> output;
2017-01-01 23:20:06 +01:00
2017-02-07 19:42:35 +00:00
// //if (!jop::db().errorCheck(q)) return output;
2017-02-06 20:20:30 +00:00
2017-02-07 19:42:35 +00:00
// while (q.next()) {
// std::unique_ptr<Folder> folder(new Folder());
// folder->loadSqlQuery(q);
// output.push_back(std::move(folder));
// }
2017-01-01 23:20:06 +01:00
2017-02-07 19:42:35 +00:00
// return output;
// }
2017-02-06 21:18:03 +00:00
QString Folder::displayTitle() const {
return QString("%1/").arg(value("title").toString());
}
}