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

186 lines
4.4 KiB
C++
Raw Normal View History

2017-02-03 21:44:51 +00:00
#include <stable.h>
2017-01-27 20:26:46 +00:00
#include "cliapplication.h"
#include "constants.h"
#include "database.h"
#include "paths.h"
#include "uuid.h"
#include "settings.h"
2017-01-31 20:19:03 +00:00
#include "models/folder.h"
#include <signal.h>
2017-01-27 20:26:46 +00:00
namespace jop {
2017-01-31 20:19:03 +00:00
StdoutHandler::StdoutHandler() : QTextStream(stdout) {}
2017-01-27 20:26:46 +00:00
CliApplication::CliApplication(int &argc, char **argv) : QCoreApplication(argc, argv) {
// This is linked to where the QSettings will be saved. In other words,
// if these values are changed, the settings will be reset and saved
// somewhere else.
QCoreApplication::setOrganizationName(jop::ORG_NAME);
QCoreApplication::setOrganizationDomain(jop::ORG_DOMAIN);
QCoreApplication::setApplicationName(jop::APP_NAME);
qInfo() << "Config dir:" << paths::configDir();
qInfo() << "Database file:" << paths::databaseFile();
qInfo() << "SSL:" << QSslSocket::sslLibraryBuildVersionString() << QSslSocket::sslLibraryVersionNumber();
jop::db().initialize(paths::databaseFile());
Settings::initialize();
Settings settings;
if (!settings.contains("clientId")) {
// Client ID should be unique per instance of a program
settings.setValue("clientId", uuid::createUuid());
}
}
2017-02-07 20:11:08 +00:00
CliApplication::~CliApplication() {
jop::db().close();
}
2017-01-31 20:19:03 +00:00
// void CliApplication::processCommand(const Command& command) {
// qDebug() << "Command" << command.name();
// qDebug() << "Flags" << command.flags();
// qDebug() << "Args" << command.args();
// // if (command == "mkdir") {
// // //Folder folder;
// // //folder.setValue("title", args[
// // }
// }
2017-01-27 20:26:46 +00:00
int CliApplication::exec() {
2017-01-31 20:19:03 +00:00
qDebug() << "===========================================";
// QProcess* process = new QProcess();
// qint64* processId = new qint64();
// process->startDetached("/usr/bin/vim", QStringList(), QString(), processId);
// while (kill(*processId, 0) == 0) {}
// delete processId;
QString command = "help";
QStringList args = arguments();
if (args.size() >= 2) {
command = args[1];
args.erase(args.begin() + 1);
}
QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
// mkdir "new_folder"
// ls
// ls new_folder
// touch new_folder/new_note
if (command == "mkdir") {
parser.addPositionalArgument("path", "Folder path.");
} else if (command == "ls") {
parser.addPositionalArgument("path", "Folder path.");
} else if (command == "touch") {
parser.addPositionalArgument("path", "Note path.");
} else {
qDebug().noquote() << parser.helpText();
return 1;
}
parser.process(args);
args = parser.positionalArguments();
2017-02-06 21:18:03 +00:00
int errorCode = 0;
2017-01-31 20:19:03 +00:00
if (command == "mkdir") {
2017-02-06 21:18:03 +00:00
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;
}
2017-01-31 20:19:03 +00:00
Folder folder;
2017-02-06 21:18:03 +00:00
folder.setValue("parent_id", folders.size() ? folders[folders.size() - 1]->idString() : "");
folder.setValue("title", Folder::pathBaseName(path));
2017-01-31 20:19:03 +00:00
folder.save();
}
if (command == "ls") {
QString path = args.size() ? args[0] : QString();
2017-02-06 21:18:03 +00:00
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();
2017-01-31 20:19:03 +00:00
} else {
2017-02-06 21:18:03 +00:00
std::unique_ptr<Folder> root = Folder::root();
children = root->children();
}
qStdout() << QString("Total: %1 items").arg(children.size()) << endl;
2017-02-07 19:42:35 +00:00
for (size_t i = 0; i < children.size(); i++) {
2017-02-06 21:18:03 +00:00
qStdout() << children[i]->displayTitle() << endl;
2017-01-31 20:19:03 +00:00
}
}
if (command == "touch") {
2017-02-03 20:47:45 +00:00
QString path = args.size() ? args[0] : QString();
2017-02-06 21:18:03 +00:00
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;
2017-02-03 20:47:45 +00:00
} else {
2017-02-06 21:18:03 +00:00
QString noteTitle = Folder::pathBaseName(path);
Note note;
note.setValue("parent_id", folders.size() ? folders[folders.size() - 1]->idString() : "");
note.setValue("title", noteTitle);
note.save();
2017-02-03 20:47:45 +00:00
}
2017-01-31 20:19:03 +00:00
}
2017-02-06 21:18:03 +00:00
qDebug() << "=========================================== END";
2017-01-31 20:19:03 +00:00
2017-01-27 20:26:46 +00:00
return 0;
}
}