From bdb01c2c068df326f5a8328ed1492ab1bea388c5 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 20 Jan 2016 19:07:07 +0100 Subject: Renamed Akonadi2 to Sink (except for documentation). --- sinksh/syntax_modules/core_syntax.cpp | 204 ++++++++++++++++++++++++++++++++++ sinksh/syntax_modules/sink_clear.cpp | 61 ++++++++++ sinksh/syntax_modules/sink_count.cpp | 83 ++++++++++++++ sinksh/syntax_modules/sink_create.cpp | 118 ++++++++++++++++++++ sinksh/syntax_modules/sink_list.cpp | 114 +++++++++++++++++++ sinksh/syntax_modules/sink_modify.cpp | 120 ++++++++++++++++++++ sinksh/syntax_modules/sink_remove.cpp | 110 ++++++++++++++++++ sinksh/syntax_modules/sink_stat.cpp | 120 ++++++++++++++++++++ sinksh/syntax_modules/sink_sync.cpp | 67 +++++++++++ 9 files changed, 997 insertions(+) create mode 100644 sinksh/syntax_modules/core_syntax.cpp create mode 100644 sinksh/syntax_modules/sink_clear.cpp create mode 100644 sinksh/syntax_modules/sink_count.cpp create mode 100644 sinksh/syntax_modules/sink_create.cpp create mode 100644 sinksh/syntax_modules/sink_list.cpp create mode 100644 sinksh/syntax_modules/sink_modify.cpp create mode 100644 sinksh/syntax_modules/sink_remove.cpp create mode 100644 sinksh/syntax_modules/sink_stat.cpp create mode 100644 sinksh/syntax_modules/sink_sync.cpp (limited to 'sinksh/syntax_modules') diff --git a/sinksh/syntax_modules/core_syntax.cpp b/sinksh/syntax_modules/core_syntax.cpp new file mode 100644 index 0000000..f5b6274 --- /dev/null +++ b/sinksh/syntax_modules/core_syntax.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include // tr() +#include +#include + +#include "state.h" +#include "syntaxtree.h" +#include "utils.h" + +namespace CoreSyntax +{ + +bool exit(const QStringList &, State &) +{ + ::exit(0); + return true; +} + +bool showHelp(const QStringList &commands, State &state) +{ + SyntaxTree::Command command = SyntaxTree::self()->match(commands); + if (commands.isEmpty()) { + state.printLine(QObject::tr("Welcome to the Sink command line tool!")); + state.printLine(QObject::tr("Top-level commands:")); + + QSet sorted; + for (auto syntax: SyntaxTree::self()->syntax()) { + sorted.insert(syntax.keyword); + } + + for (auto keyword: sorted) { + state.printLine(keyword, 1); + } + } else if (const Syntax *syntax = command.first) { + //TODO: get parent! + state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); + + if (!syntax->help.isEmpty()) { + state.print(": " + syntax->help); + } + state.printLine(); + + if (!syntax->children.isEmpty()) { + state.printLine("Sub-commands:", 1); + QSet sorted; + for (auto childSyntax: syntax->children) { + sorted.insert(childSyntax.keyword); + } + + for (auto keyword: sorted) { + state.printLine(keyword, 1); + } + } + } else { + state.printError("Unknown command: " + commands.join(" ")); + } + + return true; +} + +QStringList showHelpCompleter(const QStringList &commands, const QString &fragment, State &) +{ + QStringList items; + + for (auto syntax: SyntaxTree::self()->syntax()) { + if (syntax.keyword != QObject::tr("help") && + (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) { + items << syntax.keyword; + } + } + + qSort(items); + return items; +} + +bool setDebugLevel(const QStringList &commands, State &state) +{ + if (commands.count() != 1) { + state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count())); + return false; + } + + bool ok = false; + int level = commands[0].toUInt(&ok); + + if (!ok) { + state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0])); + return false; + } + + state.setDebugLevel(level); + return true; +} + +bool printDebugLevel(const QStringList &, State &state) +{ + state.printLine(QString::number(state.debugLevel())); + return true; +} + +bool printCommandTiming(const QStringList &, State &state) +{ + state.printLine(state.commandTiming() ? QObject::tr("on") : QObject::tr("off")); + return true; +} + +void printSyntaxBranch(State &state, const Syntax::List &list, int depth) +{ + if (list.isEmpty()) { + return; + } + + if (depth > 0) { + state.printLine("\\", depth); + } + + for (auto syntax: list) { + state.print("|-", depth); + state.printLine(syntax.keyword); + printSyntaxBranch(state, syntax.children, depth + 1); + } +} + +bool printSyntaxTree(const QStringList &, State &state) +{ + printSyntaxBranch(state, SyntaxTree::self()->syntax(), 0); + return true; +} + +bool setLoggingLevel(const QStringList &commands, State &state) +{ + if (commands.count() != 1) { + state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count())); + return false; + } + + state.setLoggingLevel(commands.at(0)); + return true; +} + +bool printLoggingLevel(const QStringList &commands, State &state) +{ + const QString level = state.loggingLevel(); + state.printLine(level); + return true; +} + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + + Syntax help("help", QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + help.completer = &CoreSyntax::showHelpCompleter; + syntax << help; + + syntax << Syntax("syntaxtree", QString(), &printSyntaxTree); + + Syntax set("set", QObject::tr("Sets settings for the session")); + set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + + Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete")); + setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); + setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); + set.children << setTiming; + + Syntax logging("logging", QObject::tr("Set the logging level to one of Trace, Log, Warning or Error"), &CoreSyntax::setLoggingLevel); + logging.completer = [](const QStringList &, const QString &fragment, State &state) -> QStringList { return Utils::filteredCompletions(QStringList() << "trace" << "log" << "warning" << "error", fragment, Qt::CaseInsensitive); }; + set.children << logging; + + syntax << set; + + Syntax get("get", QObject::tr("Gets settings for the session")); + get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); + get.children << Syntax("logging", QObject::tr("The current logging level"), &CoreSyntax::printLoggingLevel); + syntax << get; + + return syntax; +} + +REGISTER_SYNTAX(CoreSyntax) + +} // namespace CoreSyntax + diff --git a/sinksh/syntax_modules/sink_clear.cpp b/sinksh/syntax_modules/sink_clear.cpp new file mode 100644 index 0000000..d02c638 --- /dev/null +++ b/sinksh/syntax_modules/sink_clear.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include // tr() +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkClear +{ + +bool clear(const QStringList &args, State &state) +{ + for (const auto &resource : args) { + state.print(QObject::tr("Removing local cache for '%1' ...").arg(resource)); + Sink::Store::removeFromDisk(resource.toLatin1()); + state.printLine(QObject::tr("done")); + } + + return true; +} + +Syntax::List syntax() +{ + Syntax clear("clear", QObject::tr("Clears the local cache of one or more resources (be careful!)"), &SinkClear::clear); + clear.completer = &SinkshUtils::resourceCompleter; + + return Syntax::List() << clear; +} + +REGISTER_SYNTAX(SinkClear) + +} diff --git a/sinksh/syntax_modules/sink_count.cpp b/sinksh/syntax_modules/sink_count.cpp new file mode 100644 index 0000000..fde7c33 --- /dev/null +++ b/sinksh/syntax_modules/sink_count.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkCount +{ + +bool count(const QStringList &args, State &state) +{ + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Sink::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.liveQuery = false; + + auto model = SinkshUtils::loadModel(type, query); + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Sink::Store::ChildrenFetchedRole)) { + state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); + state.commandFinished(); + } + }); + + if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) { + return true; + } + + return true; +} + +Syntax::List syntax() +{ + Syntax count("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &SinkCount::count, Syntax::EventDriven); + count.completer = &SinkshUtils::typeCompleter; + + return Syntax::List() << count; +} + +REGISTER_SYNTAX(SinkCount) + +} diff --git a/sinksh/syntax_modules/sink_create.cpp b/sinksh/syntax_modules/sink_create.cpp new file mode 100644 index 0000000..cd2cd80 --- /dev/null +++ b/sinksh/syntax_modules/sink_create.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkCreate +{ + +bool create(const QStringList &allArgs, State &state) +{ + if (allArgs.isEmpty()) { + state.printError(QObject::tr("A type is required"), "sinkcreate/02"); + return false; + } + + if (allArgs.count() < 2) { + state.printError(QObject::tr("A resource ID is required to create items"), "sinkcreate/03"); + return false; + } + + auto args = allArgs; + auto type = args.takeFirst(); + auto &store = SinkshUtils::getStore(type); + Sink::ApplicationDomain::ApplicationDomainType::Ptr object; + auto resource = args.takeFirst().toLatin1(); + object = store.getObject(resource); + + auto map = SinkshUtils::keyValueMapFromArgs(args); + for (auto i = map.begin(); i != map.end(); ++i) { + object->setProperty(i.key().toLatin1(), i.value()); + } + + auto result = store.create(*object).exec(); + result.waitForFinished(); + if (result.errorCode()) { + state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()), + "akonaid_create_e" + QString::number(result.errorCode())); + } + + return true; +} + +bool resource(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A resource can not be created without a type"), "sinkcreate/01"); + return false; + } + + auto &store = SinkshUtils::getStore("resource"); + + auto resourceType = args.at(0); + Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(""); + object->setProperty("type", resourceType); + + auto map = SinkshUtils::keyValueMapFromArgs(args); + for (auto i = map.begin(); i != map.end(); ++i) { + object->setProperty(i.key().toLatin1(), i.value()); + } + + auto result = store.create(*object).exec(); + result.waitForFinished(); + if (result.errorCode()) { + state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()), + "akonaid_create_e" + QString::number(result.errorCode())); + } + + return true; +} + + +Syntax::List syntax() +{ + Syntax::List syntax; + + Syntax create("create", QObject::tr("Create items in a resource"), &SinkCreate::create); + create.children << Syntax("resource", QObject::tr("Creates a new resource"), &SinkCreate::resource); + + syntax << create; + return syntax; +} + +REGISTER_SYNTAX(SinkCreate) + +} diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp new file mode 100644 index 0000000..9712b6f --- /dev/null +++ b/sinksh/syntax_modules/sink_list.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkList +{ + +bool list(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("Please provide at least one type to list (e.g. resource, ..")); + return false; + } + + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Sink::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.liveQuery = false; + + QTime time; + time.start(); + auto model = SinkshUtils::loadModel(type, query); + if (state.debugLevel() > 0) { + state.printLine(QObject::tr("Folder type %1").arg(type)); + state.printLine(QObject::tr("Loaded model in %1 ms").arg(time.elapsed())); + } + + //qDebug() << "Listing"; + int colSize = 38; //Necessary to display a complete UUID + state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) + + QObject::tr("Identifier").leftJustified(colSize, ' ', true)); + for (int i = 0; i < model->columnCount(QModelIndex()); i++) { + state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true)); + } + state.printLine(); + + QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) { + for (int i = start; i <= end; i++) { + auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value(); + state.print(object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true)); + state.print(object->identifier().leftJustified(colSize, ' ', true)); + for (int col = 0; col < model->columnCount(QModelIndex()); col++) { + state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true)); + } + state.printLine(); + } + }); + + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Sink::Store::ChildrenFetchedRole)) { + state.commandFinished(); + } + }); + + if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) { + return true; + } + + return false; +} + +Syntax::List syntax() +{ + Syntax list("list", QObject::tr("List all resources, or the contents of one or more resources"), &SinkList::list, Syntax::EventDriven); + list.completer = &SinkshUtils::resourceOrTypeCompleter; + return Syntax::List() << list; +} + +REGISTER_SYNTAX(SinkList) + +} diff --git a/sinksh/syntax_modules/sink_modify.cpp b/sinksh/syntax_modules/sink_modify.cpp new file mode 100644 index 0000000..4d637d8 --- /dev/null +++ b/sinksh/syntax_modules/sink_modify.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkModify +{ + +bool modify(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A type is required"), "sink_modify/02"); + return false; + } + + if (args.count() < 2) { + state.printError(QObject::tr("A resource ID is required to remove items"), "sink_modify/03"); + return false; + } + + if (args.count() < 3) { + state.printError(QObject::tr("An object ID is required to remove items"), "sink_modify/03"); + return false; + } + + auto type = args[0]; + auto resourceId = args[1]; + auto identifier = args[2]; + + auto &store = SinkshUtils::getStore(type); + Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8()); + + auto map = SinkshUtils::keyValueMapFromArgs(args); + for (auto i = map.begin(); i != map.end(); ++i) { + object->setProperty(i.key().toLatin1(), i.value()); + } + + auto result = store.modify(*object).exec(); + result.waitForFinished(); + if (result.errorCode()) { + state.printError(QObject::tr("An error occurred while removing %1 from %1: %2").arg(identifier).arg(resourceId).arg(result.errorMessage()), + "akonaid__modify_e" + QString::number(result.errorCode())); + } + + return true; +} + +bool resource(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A resource can not be modified without an id"), "sink_modify/01"); + } + + auto &store = SinkshUtils::getStore("resource"); + + auto resourceId = args.at(0); + Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); + + auto map = SinkshUtils::keyValueMapFromArgs(args); + for (auto i = map.begin(); i != map.end(); ++i) { + object->setProperty(i.key().toLatin1(), i.value()); + } + + auto result = store.modify(*object).exec(); + result.waitForFinished(); + if (result.errorCode()) { + state.printError(QObject::tr("An error occurred while modifying the resource %1: %2").arg(resourceId).arg(result.errorMessage()), + "akonaid_modify_e" + QString::number(result.errorCode())); + } + + return true; +} + + +Syntax::List syntax() +{ + Syntax modify("modify", QObject::tr("Modify items in a resource"), &SinkModify::modify); + Syntax resource("resource", QObject::tr("Modify a resource"), &SinkModify::resource);//, Syntax::EventDriven); + resource.completer = &SinkshUtils::resourceOrTypeCompleter; + modify.children << resource; + + return Syntax::List() << modify; +} + +REGISTER_SYNTAX(SinkModify) + +} diff --git a/sinksh/syntax_modules/sink_remove.cpp b/sinksh/syntax_modules/sink_remove.cpp new file mode 100644 index 0000000..b374824 --- /dev/null +++ b/sinksh/syntax_modules/sink_remove.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkRemove +{ + +bool remove(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A type is required"), "sink_remove/02"); + return false; + } + + if (args.count() < 2) { + state.printError(QObject::tr("A resource ID is required to remove items"), "sink_remove/03"); + return false; + } + + if (args.count() < 3) { + state.printError(QObject::tr("An object ID is required to remove items"), "sink_remove/03"); + return false; + } + + auto type = args[0]; + auto resourceId = args[1]; + auto identifier = args[2]; + + auto &store = SinkshUtils::getStore(type); + Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8()); + + auto result = store.remove(*object).exec(); + result.waitForFinished(); + if (result.errorCode()) { + state.printError(QObject::tr("An error occurred while removing %1 from %1: %2").arg(identifier).arg(resourceId).arg(result.errorMessage()), + "akonaid_remove_e" + QString::number(result.errorCode())); + } + + return true; +} + +bool resource(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A resource can not be removed without an id"), "sink_remove/01"); + } + + auto &store = SinkshUtils::getStore("resource"); + + auto resourceId = args.at(0); + Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); + + auto result = store.remove(*object).exec(); + result.waitForFinished(); + if (result.errorCode()) { + state.printError(QObject::tr("An error occurred while removing the resource %1: %2").arg(resourceId).arg(result.errorMessage()), + "akonaid_remove_e" + QString::number(result.errorCode())); + } + + return true; +} + + +Syntax::List syntax() +{ + Syntax remove("remove", QObject::tr("Remove items in a resource"), &SinkRemove::remove); + Syntax resource("resource", QObject::tr("Removes a resource"), &SinkRemove::resource);//, Syntax::EventDriven); + resource.completer = &SinkshUtils::resourceCompleter; + remove.children << resource; + + return Syntax::List() << remove; +} + +REGISTER_SYNTAX(SinkRemove) + +} diff --git a/sinksh/syntax_modules/sink_stat.cpp b/sinksh/syntax_modules/sink_stat.cpp new file mode 100644 index 0000000..06586d9 --- /dev/null +++ b/sinksh/syntax_modules/sink_stat.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include // tr() +#include +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkStat +{ + +void statResources(const QStringList &resources, const State &state) +{ + qint64 total = 0; + for (const auto &resource : resources) { + Sink::Storage storage(Sink::storageLocation(), resource, Sink::Storage::ReadOnly); + auto transaction = storage.createTransaction(Sink::Storage::ReadOnly); + + QList databases = transaction.getDatabaseNames(); + for (const auto &databaseName : databases) { + state.printLine(QObject::tr("Database: %1").arg(QString(databaseName)), 1); + auto db = transaction.openDatabase(databaseName); + qint64 size = db.getSize() / 1024; + state.printLine(QObject::tr("Size [kb]: %1").arg(size), 1); + total += size; + } + int diskUsage = 0; + + QDir dir(Sink::storageLocation()); + for (const auto &folder : dir.entryList(QStringList() << resource + "*")) { + diskUsage += Sink::Storage(Sink::storageLocation(), folder, Sink::Storage::ReadOnly).diskUsage(); + } + auto size = diskUsage / 1024; + state.printLine(QObject::tr("Disk usage [kb]: %1").arg(size), 1); + } + + state.printLine(QObject::tr("Total [kb]: %1").arg(total)); +} + +bool statAllResources(State &state) +{ + Sink::Query query; + query.liveQuery = false; + auto model = SinkshUtils::loadModel("resource", query); + + //SUUUPER ugly, but can't think of a better way with 2 glasses of wine in me on Christmas day + static QStringList resources; + resources.clear(); + + QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) mutable { + for (int i = start; i <= end; i++) { + auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value(); + resources << object->identifier(); + } + }); + + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Sink::Store::ChildrenFetchedRole)) { + statResources(resources, state); + state.commandFinished(); + } + }); + + if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) { + return true; + } + + return false; +} + +bool stat(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + return statAllResources(state); + } + + statResources(args, state); + return false; +} + +Syntax::List syntax() +{ + Syntax state("stat", QObject::tr("Shows database usage for the resources requested"), &SinkStat::stat, Syntax::EventDriven); + state.completer = &SinkshUtils::resourceCompleter; + + return Syntax::List() << state; +} + +REGISTER_SYNTAX(SinkStat) + +} diff --git a/sinksh/syntax_modules/sink_sync.cpp b/sinksh/syntax_modules/sink_sync.cpp new file mode 100644 index 0000000..3006202 --- /dev/null +++ b/sinksh/syntax_modules/sink_sync.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include // tr() +#include + +#include "common/resource.h" +#include "common/storage.h" +#include "common/domain/event.h" +#include "common/domain/folder.h" +#include "common/resourceconfig.h" +#include "common/log.h" +#include "common/storage.h" +#include "common/definitions.h" + +#include "sinksh_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace SinkSync +{ + +bool sync(const QStringList &args, State &state) +{ + Sink::Query query; + for (const auto &res : args) { + query.resources << res.toLatin1(); + } + + QTimer::singleShot(0, [query, state]() { + Sink::Store::synchronize(query).then([state]() { + state.printLine("Synchronization complete!"); + state.commandFinished(); + }).exec(); + }); + + return true; +} + +Syntax::List syntax() +{ + Syntax sync("sync", QObject::tr("Syncronizes all resources that are listed; and empty list triggers a syncronizaton on all resources"), &SinkSync::sync, Syntax::EventDriven ); + sync.completer = &SinkshUtils::resourceCompleter; + + return Syntax::List() << sync; +} + +REGISTER_SYNTAX(SinkSync) + +} -- cgit v1.2.3