mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
More CLI commands
This commit is contained in:
parent
83f5bc2916
commit
d197710716
@ -106,47 +106,74 @@ int CliApplication::exec() {
|
||||
|
||||
args = parser.positionalArguments();
|
||||
|
||||
int errorCode = 0;
|
||||
|
||||
if (command == "mkdir") {
|
||||
QString title = args.size() ? args[0] : QString();
|
||||
QString path = args.size() ? args[0] : QString();
|
||||
|
||||
if (path.isEmpty()) {
|
||||
qStdout() << "Please provide a path or name for the folder.";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Folder>> folders = Folder::pathToFolders(path, false, errorCode);
|
||||
if (errorCode) {
|
||||
qStdout() << "Invalid path: " << path << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Folder folder;
|
||||
folder.setValue("title", title);
|
||||
folder.setValue("parent_id", folders.size() ? folders[folders.size() - 1]->idString() : "");
|
||||
folder.setValue("title", Folder::pathBaseName(path));
|
||||
folder.save();
|
||||
}
|
||||
|
||||
if (command == "ls") {
|
||||
QString path = args.size() ? args[0] : QString();
|
||||
if (path == "") {
|
||||
std::vector<std::unique_ptr<Folder>> folders = Folder::all();
|
||||
for (size_t i = 0; i < folders.size(); i++) {
|
||||
qDebug().noquote() << folders[i].get()->value("title").toString();
|
||||
}
|
||||
std::vector<std::unique_ptr<Folder>> folders = Folder::pathToFolders(path, true, errorCode);
|
||||
|
||||
if (errorCode) {
|
||||
qStdout() << "Invalid path: " << path << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<BaseModel>> children;
|
||||
if (folders.size()) {
|
||||
children = folders[folders.size() - 1]->children();
|
||||
} else {
|
||||
Folder folder;
|
||||
bool found = folder.loadByField("", "title", path);
|
||||
if (found) {
|
||||
qStdout() << "Found" << path << endl;
|
||||
} else {
|
||||
qWarning() << "Not found:" << path;
|
||||
}
|
||||
std::unique_ptr<Folder> root = Folder::root();
|
||||
children = root->children();
|
||||
}
|
||||
|
||||
qStdout() << QString("Total: %1 items").arg(children.size()) << endl;
|
||||
for (int i = 0; i < children.size(); i++) {
|
||||
qStdout() << children[i]->displayTitle() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (command == "touch") {
|
||||
QString path = args.size() ? args[0] : QString();
|
||||
if (path == "") {
|
||||
// std::vector<std::unique_ptr<Folder>> folders = Folder::all();
|
||||
// for (size_t i = 0; i < folders.size(); i++) {
|
||||
// qDebug().noquote() << folders[i].get()->value("title").toString();
|
||||
// }
|
||||
|
||||
if (path.isEmpty()) {
|
||||
qStdout() << "Please provide a path or name for the note.";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Folder>> folders = Folder::pathToFolders(path, false, errorCode);
|
||||
|
||||
if (errorCode) {
|
||||
qStdout() << "Invalid path: " << path << endl;
|
||||
} else {
|
||||
std::vector<std::unique_ptr<Folder>> folders = Folder::pathToFolders(path, true);
|
||||
for (size_t i = 0; i < folders.size(); i++) {
|
||||
qDebug() << folders[i]->value("title").toString();
|
||||
}
|
||||
QString noteTitle = Folder::pathBaseName(path);
|
||||
|
||||
Note note;
|
||||
note.setValue("parent_id", folders.size() ? folders[folders.size() - 1]->idString() : "");
|
||||
note.setValue("title", noteTitle);
|
||||
note.save();
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << "===========================================";
|
||||
qDebug() << "=========================================== END";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -209,6 +209,10 @@ bool BaseModel::trackChanges() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString BaseModel::displayTitle() const {
|
||||
return value("title").toString();
|
||||
}
|
||||
|
||||
bool BaseModel::isNew() const {
|
||||
if (isNew_ == 0) return false;
|
||||
if (isNew_ == 1) return true;
|
||||
|
@ -51,6 +51,7 @@ public:
|
||||
virtual QString primaryKey() const;
|
||||
virtual bool primaryKeyIsUuid() const;
|
||||
virtual bool trackChanges() const;
|
||||
virtual QString displayTitle() const;
|
||||
|
||||
bool isNew() const;
|
||||
|
||||
|
@ -27,6 +27,49 @@ int Folder::noteCount() const {
|
||||
return q.value(0).toInt();
|
||||
}
|
||||
|
||||
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);
|
||||
for (int tableIndex = 0; tableIndex < tables.size(); tableIndex++) {
|
||||
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;
|
||||
|
||||
@ -49,19 +92,31 @@ std::vector<std::unique_ptr<Note>> Folder::notes(const QString &orderBy, int lim
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Folder>> Folder::pathToFolders(const QString& path, bool isNotePath) {
|
||||
std::vector<std::unique_ptr<Folder>> Folder::pathToFolders(const QString& path, bool returnLast, int& errorCode) {
|
||||
std::vector<std::unique_ptr<Folder>> output;
|
||||
if (!path.length()) return output;
|
||||
|
||||
QStringList parts = path.split('/');
|
||||
QString parentId = "";
|
||||
for (int i = 0; i < parts.size(); i++) {
|
||||
QString parentId("");
|
||||
int toIndex = returnLast ? parts.size() : parts.size() - 1;
|
||||
for (int i = 0; i < toIndex; i++) {
|
||||
std::unique_ptr<Folder> folder(new Folder());
|
||||
bool ok = folder->loadByField(parentId, "title", parts[i]);
|
||||
qWarning() << "Folder does not exist" << parts[i];
|
||||
if (!ok) {
|
||||
// qWarning() << "Folder does not exist" << parts[i];
|
||||
errorCode = 1;
|
||||
return output;
|
||||
}
|
||||
output.push_back(std::move(folder));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
QString Folder::pathBaseName(const QString& path) {
|
||||
QStringList parts = path.split('/');
|
||||
return parts[parts.size() - 1];
|
||||
}
|
||||
|
||||
int Folder::noteIndexById(const QString &orderBy, const QString& id) const {
|
||||
qDebug() << "Folder::noteIndexById" << orderBy << id;
|
||||
|
||||
@ -103,4 +158,8 @@ std::vector<std::unique_ptr<Folder>> Folder::all(const QString &orderBy) {
|
||||
return output;
|
||||
}
|
||||
|
||||
QString Folder::displayTitle() const {
|
||||
return QString("%1/").arg(value("title").toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,15 +15,18 @@ public:
|
||||
|
||||
static int count();
|
||||
static std::vector<std::unique_ptr<Folder>> all(const QString& orderBy = "title");
|
||||
static std::vector<std::unique_ptr<Folder>> pathToFolders(const QString& path, bool isNotePath);
|
||||
static std::vector<std::unique_ptr<Folder>> pathToFolders(const QString& path, bool returnLast, int& errorCode);
|
||||
static QString pathBaseName(const QString& path);
|
||||
static std::unique_ptr<Folder> root();
|
||||
|
||||
//Table table() const;
|
||||
bool primaryKeyIsUuid() const;
|
||||
bool trackChanges() const;
|
||||
int noteCount() const;
|
||||
std::vector<std::unique_ptr<Note> > notes(const QString& orderBy, int limit, int offset) const;
|
||||
std::vector<std::unique_ptr<Note>> notes(const QString& orderBy, int limit, int offset = 0) const;
|
||||
std::vector<std::unique_ptr<BaseModel>> children(const QString &orderBy = QString("title"), int limit = 0, int offset = 0) const;
|
||||
int noteIndexById(const QString& orderBy, const QString &id) const;
|
||||
|
||||
QString displayTitle() const;
|
||||
|
||||
};
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#if defined __cplusplus
|
||||
|
||||
#include <memory>
|
||||
#include <limits>
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QCoreApplication>
|
||||
|
Loading…
Reference in New Issue
Block a user