From 255d73af197faf8437343abc10bd98cca2057a1e Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 10:47:32 +0100 Subject: initial import of the new cli app --- CMakeLists.txt | 3 + akonadi2_cli/CMakeLists.txt | 16 +++ akonadi2_cli/main.cpp | 71 ++++++++++++++ akonadi2_cli/module.cpp | 195 +++++++++++++++++++++++++++++++++++++ akonadi2_cli/module.h | 61 ++++++++++++ akonadi2_cli/modules/exit/exit.cpp | 39 ++++++++ akonadi2_cli/modules/exit/exit.h | 37 +++++++ akonadi2_cli/modules/help/help.cpp | 80 +++++++++++++++ akonadi2_cli/modules/help/help.h | 37 +++++++ akonadi2_cli/repl/repl.cpp | 78 +++++++++++++++ akonadi2_cli/repl/repl.h | 34 +++++++ akonadi2_cli/repl/replStates.cpp | 155 +++++++++++++++++++++++++++++ akonadi2_cli/repl/replStates.h | 85 ++++++++++++++++ akonadi2_cli/state.cpp | 28 ++++++ akonadi2_cli/state.h | 29 ++++++ cmake/modules/FindReadline.cmake | 47 +++++++++ 16 files changed, 995 insertions(+) create mode 100644 akonadi2_cli/CMakeLists.txt create mode 100644 akonadi2_cli/main.cpp create mode 100644 akonadi2_cli/module.cpp create mode 100644 akonadi2_cli/module.h create mode 100644 akonadi2_cli/modules/exit/exit.cpp create mode 100644 akonadi2_cli/modules/exit/exit.h create mode 100644 akonadi2_cli/modules/help/help.cpp create mode 100644 akonadi2_cli/modules/help/help.h create mode 100644 akonadi2_cli/repl/repl.cpp create mode 100644 akonadi2_cli/repl/repl.h create mode 100644 akonadi2_cli/repl/replStates.cpp create mode 100644 akonadi2_cli/repl/replStates.h create mode 100644 akonadi2_cli/state.cpp create mode 100644 akonadi2_cli/state.h create mode 100644 cmake/modules/FindReadline.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c480ddd..d92f830 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,4 +57,7 @@ add_subdirectory(examples) # some tests add_subdirectory(tests) +# cli +add_subdirectory(akonadi2_cli) + feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/akonadi2_cli/CMakeLists.txt b/akonadi2_cli/CMakeLists.txt new file mode 100644 index 0000000..b5bdb46 --- /dev/null +++ b/akonadi2_cli/CMakeLists.txt @@ -0,0 +1,16 @@ +project(akonadi2_cli) + +find_package(Readline REQUIRED) + + +set(akonadi2_SRCS + main.cpp + module.cpp + modules/exit/exit.cpp + modules/help/help.cpp + repl/repl.cpp + repl/replStates.cpp + state.cpp) + +add_executable(${PROJECT_NAME} ${akonadi2_SRCS}) + diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp new file mode 100644 index 0000000..1a036d0 --- /dev/null +++ b/akonadi2_cli/main.cpp @@ -0,0 +1,71 @@ +/* + * 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 + +#include "module.h" +// #include "jsonlistener.h" +#include "repl/repl.h" + +/* + * modes of operation: + * + * 1. called with no commands: start the REPL and listen for JSON on stin + * 2. called with -: listen for JSON on stdin + * 3. called with commands: try to match to syntx + */ + +int main(int argc, char *argv[]) +{ + // load all modules + Module::loadModules(); + + const bool interactive = isatty(fileno(stdin)); + const bool startRepl = (argc == 1) && interactive; + //TODO: make a json command parse cause that would be awesomesauce + const bool startJsonListener = !startRepl && + (argc == 2 && qstrcmp(argv[1], "-") == 0); + qDebug() << "state at startup is" << interactive << startRepl << startJsonListener; + + QCoreApplication app(argc, argv); + app.setApplicationName("funq"); + + if (startRepl || startJsonListener) { + if (startRepl) { + Repl *repl = new Repl; + QObject::connect(repl, &QStateMachine::finished, + repl, &QObject::deleteLater); + QObject::connect(repl, &QStateMachine::finished, + &app, &QCoreApplication::quit); + } + + if (startJsonListener) { +// JsonListener listener(syntax); + } + + return app.exec(); + } + + QStringList commands = app.arguments(); + commands.removeFirst(); + return Module::run(commands); +} diff --git a/akonadi2_cli/module.cpp b/akonadi2_cli/module.cpp new file mode 100644 index 0000000..0b83d98 --- /dev/null +++ b/akonadi2_cli/module.cpp @@ -0,0 +1,195 @@ +/* + * 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 "module.h" + +#include +#include + +// TODO: needs a proper registry; making "core" modules plugins is +// almost certainly overkill, but this is not the way either +#include "modules/exit/exit.h" +#include "modules/help/help.h" + +QList Module::s_modules; +State Module::s_state; + +Module::Syntax::Syntax() +{ +} + +Module::Syntax::Syntax(const QString &k, std::function l, const QString &helpText, bool e) + : keyword(k), + lambda(l), + help(helpText), + eventDriven(e) +{ +} + +Module::Module() +{ +} + +void Module::loadModules() +{ + addModule(CLI::Exit()); + addModule(CLI::Help()); +} + +void Module::addModule(const Module &module) +{ + s_modules.append(module); +} + +QList Module::modules() +{ + return s_modules; +} + +Module::Command Module::match(const QStringList &commands) +{ + Command command; + for (const Module &module: s_modules) { + command = module.matches(commands); + if (command.first) { + break; + } + } + + return command; +} + +Module::Syntax Module::syntax() const +{ + return m_syntax; +} + +void Module::setSyntax(const Syntax &syntax) +{ + m_syntax = syntax; +} + +bool Module::run(const QStringList &commands) +{ + Command command = match(commands); + if (command.first && command.first->lambda) { + bool rv = command.first->lambda(command.second, s_state); + if (rv && command.first->eventDriven) { + return QCoreApplication::instance()->exec(); + } + + return rv; + } + + return false; +} + +Module::Command Module::matches(const QStringList &commandLine) const +{ + if (commandLine.isEmpty()) { + return Command(); + } + + QStringListIterator commandLineIt(commandLine); + + if (commandLineIt.next() != m_syntax.keyword) { + return Command(); + } + + QListIterator syntaxIt(m_syntax.children); + const Syntax *syntax = &m_syntax; + QStringList tailCommands; + while (commandLineIt.hasNext() && syntaxIt.hasNext()) { + const QString word = commandLineIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &child = syntaxIt.next(); + if (word == child.keyword) { + syntax = &child; + syntaxIt = child.children; + } + } + + if (!syntaxIt.hasNext()) { + tailCommands << word; + break; + } + } + + if (syntax && syntax->lambda) { + while (commandLineIt.hasNext()) { + tailCommands << commandLineIt.next(); + } + + return std::make_pair(syntax, tailCommands); + } + + return Command(); +} + +QVector Module::nearestSyntax(const QStringList &words, const QString &fragment) +{ + QVector matches; + + //qDebug() << "words are" << words; + if (words.isEmpty()) { + for (const Module &module: s_modules) { + if (module.syntax().keyword.startsWith(fragment)) { + matches.push_back(module.syntax()); + } + } + } else { + QStringListIterator wordIt(words); + QString word = wordIt.next(); + Syntax lastFullSyntax; + + for (const Module &module: s_modules) { + if (module.syntax().keyword == word) { + lastFullSyntax = module.syntax(); + QListIterator syntaxIt(module.syntax().children); + while (wordIt.hasNext()) { + word = wordIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &child = syntaxIt.next(); + if (word == child.keyword) { + lastFullSyntax = child; + syntaxIt = child.children; + } + } + } + + break; + } + } + + //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); + if (lastFullSyntax.keyword == words.last()) { + QListIterator syntaxIt(lastFullSyntax.children); + while (syntaxIt.hasNext()) { + Syntax syntax = syntaxIt.next(); + if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { + matches.push_back(syntax); + } + } + } + } + + return matches; +} + + diff --git a/akonadi2_cli/module.h b/akonadi2_cli/module.h new file mode 100644 index 0000000..4f426b8 --- /dev/null +++ b/akonadi2_cli/module.h @@ -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. + */ + +#pragma once + +#include "state.h" + +#include +#include + +class Module +{ +public: + struct Syntax + { + Syntax(); + Syntax(const QString &keyword, std::function lambda = std::function(), const QString &helpText = QString(), bool eventDriven = false); + QString keyword; + std::function lambda; + QList children; + QString help; + bool eventDriven; + }; + + typedef std::pair Command; + + static void addModule(const Module &module); + static QList modules(); + static Command match(const QStringList &commands); + static bool run(const QStringList &commands); + static void loadModules(); + static QVectornearestSyntax(const QStringList &words, const QString &fragment); + + Module(); + Module::Syntax syntax() const; + void setSyntax(const Syntax &syntax); + +private: + Command matches(const QStringList &commands) const; + + Syntax m_syntax; + static QList s_modules; + static State s_state; +}; + diff --git a/akonadi2_cli/modules/exit/exit.cpp b/akonadi2_cli/modules/exit/exit.cpp new file mode 100644 index 0000000..64828be --- /dev/null +++ b/akonadi2_cli/modules/exit/exit.cpp @@ -0,0 +1,39 @@ +/* + * 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 "exit.h" + +#include + +namespace CLI +{ + +Exit::Exit() +{ + setSyntax(Syntax("exit", &Exit::exit, QObject::tr("Exits the application. Ctrl-d also works!"))); +} + +bool Exit::exit(const QStringList &, State &) +{ + ::exit(0); + return true; +} + +} // namespace CLI + diff --git a/akonadi2_cli/modules/exit/exit.h b/akonadi2_cli/modules/exit/exit.h new file mode 100644 index 0000000..5ed4174 --- /dev/null +++ b/akonadi2_cli/modules/exit/exit.h @@ -0,0 +1,37 @@ +/* + * 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. + */ + +#pragma once + +#include "module.h" + +namespace CLI +{ + +class Exit : public Module +{ +public: + Exit(); + +private: + static bool exit(const QStringList &commands, State &state); +}; + +} // namespace CLI + diff --git a/akonadi2_cli/modules/help/help.cpp b/akonadi2_cli/modules/help/help.cpp new file mode 100644 index 0000000..aaff6fb --- /dev/null +++ b/akonadi2_cli/modules/help/help.cpp @@ -0,0 +1,80 @@ +/* + * 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 "help.h" + +#include +#include +#include + +#include "module.h" + +namespace CLI +{ + +Help::Help() +{ + Syntax topLevel = Syntax(QObject::tr("help"), &Help::showHelp, QObject::tr("Print command information: help [command]")); + setSyntax(topLevel); +} + +bool Help::showHelp(const QStringList &commands, State &) +{ + Module::Command command = Module::match(commands); + QTextStream stream(stdout); + if (commands.isEmpty()) { + stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; + stream << QObject::tr("Top-level commands:") << "\n"; + QSet sorted; + for (auto module: Module::modules()) { + sorted.insert(module.syntax().keyword); + } + + for (auto keyword: sorted) { + stream << "\t" << keyword << "\n"; + } + } else if (const Syntax *syntax = command.first) { + //TODO: get parent! + stream << QObject::tr("Command `%1`").arg(syntax->keyword); + + if (!syntax->help.isEmpty()) { + stream << ": " << syntax->help; + } + stream << "\n"; + + if (!syntax->children.isEmpty()) { + stream << "\tSub-commands:\n"; + QSet sorted; + for (auto childSyntax: syntax->children) { + sorted.insert(childSyntax.keyword); + } + + for (auto keyword: sorted) { + stream << "\t" << keyword << "\n"; + } + } + } else { + stream << "Unknown command: " << commands.join(" ") << "\n"; + } + + return true; +} + +} // namespace CLI + diff --git a/akonadi2_cli/modules/help/help.h b/akonadi2_cli/modules/help/help.h new file mode 100644 index 0000000..df1cfc2 --- /dev/null +++ b/akonadi2_cli/modules/help/help.h @@ -0,0 +1,37 @@ +/* + * 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. + */ + +#pragma once + +#include "module.h" + +namespace CLI +{ + +class Help : public Module +{ +public: + Help(); + +private: + static bool showHelp(const QStringList &commands, State &state); +}; + +} // namespace CLI + diff --git a/akonadi2_cli/repl/repl.cpp b/akonadi2_cli/repl/repl.cpp new file mode 100644 index 0000000..395661e --- /dev/null +++ b/akonadi2_cli/repl/repl.cpp @@ -0,0 +1,78 @@ +/* + * 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 "repl.h" + +#include + +#include +#include +#include +#include + +#include "replStates.h" + +Repl::Repl(QObject *parent) + : QStateMachine(parent) +{ + // readline history setup + using_history(); + read_history(commandHistoryPath().toLocal8Bit()); + + // create all states + ReadState *read = new ReadState(this); + UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this); + EvalState *eval = new EvalState(this); + PrintState *print = new PrintState(this); + QFinalState *final = new QFinalState(this); + + // connect the transitions + read->addTransition(read, SIGNAL(command(QString)), eval); + read->addTransition(read, SIGNAL(exitRequested()), final); + + unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval); + unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final); + + eval->addTransition(eval, SIGNAL(completed()), read); + eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead); + eval->addTransition(eval, SIGNAL(output(QString)), print); + + print->addTransition(print, SIGNAL(completed()), eval); + + setInitialState(read); + start(); +} + +Repl::~Repl() +{ + // readline history writing + write_history(commandHistoryPath().toLocal8Bit()); +} + +QString Repl::commandHistoryPath() +{ + const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); + if (!QFile::exists(path)) { + QDir dir; + dir.mkpath(path); + } + return path + "/repl_history"; +} + +#include "moc_repl.cpp" diff --git a/akonadi2_cli/repl/repl.h b/akonadi2_cli/repl/repl.h new file mode 100644 index 0000000..b76c66b --- /dev/null +++ b/akonadi2_cli/repl/repl.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#pragma once + +#include + +class Repl : public QStateMachine +{ + Q_OBJECT + +public: + Repl(QObject *parent = 0); + ~Repl(); + +private: + static QString commandHistoryPath(); +}; diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp new file mode 100644 index 0000000..e87dd5f --- /dev/null +++ b/akonadi2_cli/repl/replStates.cpp @@ -0,0 +1,155 @@ +/* + * 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 "replStates.h" + +#include +#include + +#include +#include + +#include +#include +#include + +#include "module.h" + +static char *akonadi2_cli_next_tab_complete_match(const char *text, int state); +static char ** akonadi2_cli_tab_completion(const char *text, int start, int end); + +ReadState::ReadState(QState *parent) + : QState(parent) +{ + rl_completion_entry_function = akonadi2_cli_next_tab_complete_match; + rl_attempted_completion_function = akonadi2_cli_tab_completion; +} + +void ReadState::onEntry(QEvent *event) +{ + Q_UNUSED(event) + char *line = readline(prompt()); + + if (!line) { + std::cout << std::endl; + emit exitRequested(); + return; + } + + // we have actual data, so let's wait for a full line of text + QByteArray input(line); + const QString text = QString(line).simplified(); + //qDebug() << "Line is ... " << text; + + if (text.length() > 0) { + add_history(line); + } + + free(line); + emit command(text); +} + +const char *ReadState::prompt() const +{ + return "> "; +} + +UnfinishedReadState::UnfinishedReadState(QState *parent) + : ReadState(parent) +{ +} + +const char *UnfinishedReadState::prompt() const +{ + return " "; +} + +EvalState::EvalState(QState *parent) + : QState(parent), + m_complete(false) +{ +} + +void EvalState::onEntry(QEvent *event) +{ + QStateMachine::SignalEvent *e = dynamic_cast(event); + + if (!e || e->arguments().isEmpty()) { + if (m_complete) { + emit completed(); + } else { + emit continueInput(); + } + return; + } + + //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] + const QString command = e->arguments()[0].toString(); + + if (!command.isEmpty()) { + m_complete = command.right(1) != "\\"; + if (m_complete) { + //emit output("Processing ... " + command); + const QStringList commands = command.split(" "); + Module::run(commands); + emit completed(); + } + } +} + +PrintState::PrintState(QState *parent) + : QState(parent) +{ +} + +void PrintState::onEntry(QEvent *event) +{ + QStateMachine::SignalEvent *e = dynamic_cast(event); + + if (e && !e->arguments().isEmpty()) { + const QString command = e->arguments()[0].toString(); + QTextStream stream(stdout); + stream << command << "\n"; + } + + emit completed(); +} + +static QStringList tab_completion_full_state; +static bool tab_completion_at_root = false; + +static char **akonadi2_cli_tab_completion(const char *text, int start, int end) +{ + tab_completion_at_root = start == 0; + tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts); + return NULL; +} + +static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) +{ + QVector nearest = Module::nearestSyntax(tab_completion_full_state, QString(text)); + + if (nearest.size() > state) { + return qstrdup(nearest[state].keyword.toUtf8()); + } + + return rl_filename_completion_function(text, state); +} + +#include "moc_replStates.cpp" diff --git a/akonadi2_cli/repl/replStates.h b/akonadi2_cli/repl/replStates.h new file mode 100644 index 0000000..a019a37 --- /dev/null +++ b/akonadi2_cli/repl/replStates.h @@ -0,0 +1,85 @@ +/* + * 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. + */ + +#pragma once + +#include + +class QSocketNotifier; + +class ReadState : public QState +{ + Q_OBJECT + +public: + ReadState(QState *parent = 0); + +Q_SIGNALS: + void command(const QString &command); + void exitRequested(); + +protected: + void onEntry(QEvent *event); + virtual const char *prompt() const; +}; + +class UnfinishedReadState : public ReadState +{ + Q_OBJECT + +public: + UnfinishedReadState(QState *parent = 0); + +protected: + const char *prompt() const; +}; + +class EvalState : public QState +{ + Q_OBJECT + +public: + EvalState(QState *parent = 0); + +Q_SIGNALS: + void completed(); + void continueInput(); + void output(const QString &output); + +protected: + void onEntry(QEvent *event); + +private: + bool m_complete; +}; + +class PrintState : public QState +{ + Q_OBJECT + +public: + PrintState(QState *parent = 0); + +Q_SIGNALS: + void completed(); + +protected: + void onEntry(QEvent *event); +}; + diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp new file mode 100644 index 0000000..bb21e0e --- /dev/null +++ b/akonadi2_cli/state.cpp @@ -0,0 +1,28 @@ +/* + * 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 "state.h" + +#include +#include + +State::State() +{ +} + diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h new file mode 100644 index 0000000..10b2318 --- /dev/null +++ b/akonadi2_cli/state.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#pragma once + +#include + +class State +{ +public: + State(); +}; + diff --git a/cmake/modules/FindReadline.cmake b/cmake/modules/FindReadline.cmake new file mode 100644 index 0000000..883ad3f --- /dev/null +++ b/cmake/modules/FindReadline.cmake @@ -0,0 +1,47 @@ +# - Try to find readline include dirs and libraries +# +# Usage of this module as follows: +# +# find_package(Readline) +# +# Variables used by this module, they can change the default behaviour and need +# to be set before calling find_package: +# +# Readline_ROOT_DIR Set this variable to the root installation of +# readline if the module has problems finding the +# proper installation path. +# +# Variables defined by this module: +# +# READLINE_FOUND System has readline, include and lib dirs found +# Readline_INCLUDE_DIR The readline include directories. +# Readline_LIBRARY The readline library. + +find_path(Readline_ROOT_DIR + NAMES include/readline/readline.h + ) + +find_path(Readline_INCLUDE_DIR + NAMES readline/readline.h + HINTS ${Readline_ROOT_DIR}/include + ) + +find_library(Readline_LIBRARY + NAMES readline + HINTS ${Readline_ROOT_DIR}/lib + ) + +if(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) + set(READLINE_FOUND TRUE) +else(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) + FIND_LIBRARY(Readline_LIBRARY NAMES readline) + include(FindPackageHandleStandardArgs) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(Readline DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY ) + MARK_AS_ADVANCED(Readline_INCLUDE_DIR Readline_LIBRARY) +endif(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) + +mark_as_advanced( + Readline_ROOT_DIR + Readline_INCLUDE_DIR + Readline_LIBRARY + ) -- cgit v1.2.3 From 071f4ef0122a8bfceeda9a10b41e85ad9a34a28d Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 11:43:37 +0100 Subject: vastly simplify by getting rid of Module as a base class just a move slightly more towards functional --- akonadi2_cli/CMakeLists.txt | 11 ++-- akonadi2_cli/main.cpp | 5 +- akonadi2_cli/module.cpp | 116 +++++++++++++---------------------- akonadi2_cli/module.h | 41 ++++++++----- akonadi2_cli/modules/core_syntax.cpp | 85 +++++++++++++++++++++++++ akonadi2_cli/modules/core_syntax.h | 30 +++++++++ akonadi2_cli/modules/exit/exit.cpp | 39 ------------ akonadi2_cli/modules/exit/exit.h | 37 ----------- akonadi2_cli/modules/help/help.cpp | 80 ------------------------ akonadi2_cli/modules/help/help.h | 37 ----------- akonadi2_cli/repl/replStates.cpp | 4 +- 11 files changed, 191 insertions(+), 294 deletions(-) create mode 100644 akonadi2_cli/modules/core_syntax.cpp create mode 100644 akonadi2_cli/modules/core_syntax.h delete mode 100644 akonadi2_cli/modules/exit/exit.cpp delete mode 100644 akonadi2_cli/modules/exit/exit.h delete mode 100644 akonadi2_cli/modules/help/help.cpp delete mode 100644 akonadi2_cli/modules/help/help.h diff --git a/akonadi2_cli/CMakeLists.txt b/akonadi2_cli/CMakeLists.txt index b5bdb46..a07140e 100644 --- a/akonadi2_cli/CMakeLists.txt +++ b/akonadi2_cli/CMakeLists.txt @@ -3,14 +3,17 @@ project(akonadi2_cli) find_package(Readline REQUIRED) -set(akonadi2_SRCS +set(akonadi2_cli_SRCS main.cpp module.cpp - modules/exit/exit.cpp - modules/help/help.cpp + modules/core_syntax.cpp repl/repl.cpp repl/replStates.cpp state.cpp) -add_executable(${PROJECT_NAME} ${akonadi2_SRCS}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) + +add_executable(${PROJECT_NAME} ${akonadi2_cli_SRCS}) +target_link_libraries(${PROJECT_NAME} Qt5::Core ${Readline_LIBRARY}) +install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp index 1a036d0..d23e070 100644 --- a/akonadi2_cli/main.cpp +++ b/akonadi2_cli/main.cpp @@ -36,9 +36,6 @@ int main(int argc, char *argv[]) { - // load all modules - Module::loadModules(); - const bool interactive = isatty(fileno(stdin)); const bool startRepl = (argc == 1) && interactive; //TODO: make a json command parse cause that would be awesomesauce @@ -67,5 +64,5 @@ int main(int argc, char *argv[]) QStringList commands = app.arguments(); commands.removeFirst(); - return Module::run(commands); + return Module::self()->run(commands); } diff --git a/akonadi2_cli/module.cpp b/akonadi2_cli/module.cpp index 0b83d98..dfc58c8 100644 --- a/akonadi2_cli/module.cpp +++ b/akonadi2_cli/module.cpp @@ -24,83 +24,61 @@ // TODO: needs a proper registry; making "core" modules plugins is // almost certainly overkill, but this is not the way either -#include "modules/exit/exit.h" -#include "modules/help/help.h" +#include "modules/core_syntax.h" -QList Module::s_modules; -State Module::s_state; +Module *Module::s_module = 0; Module::Syntax::Syntax() { } -Module::Syntax::Syntax(const QString &k, std::function l, const QString &helpText, bool e) +Module::Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) : keyword(k), - lambda(l), help(helpText), - eventDriven(e) + interactivity(inter), + lambda(l) { } Module::Module() { + QVector > syntaxModules; + syntaxModules << &CoreSyntax::syntax; + for (auto syntaxModule: syntaxModules) { + m_syntax += syntaxModule(); + } } -void Module::loadModules() -{ - addModule(CLI::Exit()); - addModule(CLI::Help()); -} - -void Module::addModule(const Module &module) -{ - s_modules.append(module); -} - -QList Module::modules() -{ - return s_modules; -} - -Module::Command Module::match(const QStringList &commands) +Module *Module::self() { - Command command; - for (const Module &module: s_modules) { - command = module.matches(commands); - if (command.first) { - break; - } + if (!s_module) { + s_module = new Module; } - return command; + return s_module; } -Module::Syntax Module::syntax() const +Module::SyntaxList Module::syntax() const { return m_syntax; } -void Module::setSyntax(const Syntax &syntax) -{ - m_syntax = syntax; -} - bool Module::run(const QStringList &commands) { Command command = match(commands); if (command.first && command.first->lambda) { - bool rv = command.first->lambda(command.second, s_state); - if (rv && command.first->eventDriven) { + command.first->lambda(command.second, m_state); + if (command.first->interactivity == Syntax::EventDriven) { return QCoreApplication::instance()->exec(); } - return rv; + return true; } return false; } -Module::Command Module::matches(const QStringList &commandLine) const +Module::Command Module::match(const QStringList &commandLine) const { if (commandLine.isEmpty()) { return Command(); @@ -108,20 +86,16 @@ Module::Command Module::matches(const QStringList &commandLine) const QStringListIterator commandLineIt(commandLine); - if (commandLineIt.next() != m_syntax.keyword) { - return Command(); - } - - QListIterator syntaxIt(m_syntax.children); - const Syntax *syntax = &m_syntax; + QVectorIterator syntaxIt(m_syntax); + const Syntax *lastFullSyntax = 0; QStringList tailCommands; while (commandLineIt.hasNext() && syntaxIt.hasNext()) { const QString word = commandLineIt.next(); while (syntaxIt.hasNext()) { - const Syntax &child = syntaxIt.next(); - if (word == child.keyword) { - syntax = &child; - syntaxIt = child.children; + const Syntax &syntax = syntaxIt.next(); + if (word == syntax.keyword) { + lastFullSyntax = &syntax; + syntaxIt = syntax.children; } } @@ -131,55 +105,47 @@ Module::Command Module::matches(const QStringList &commandLine) const } } - if (syntax && syntax->lambda) { + if (lastFullSyntax && lastFullSyntax->lambda) { while (commandLineIt.hasNext()) { tailCommands << commandLineIt.next(); } - return std::make_pair(syntax, tailCommands); + return std::make_pair(lastFullSyntax, tailCommands); } return Command(); } -QVector Module::nearestSyntax(const QStringList &words, const QString &fragment) +Module::SyntaxList Module::nearestSyntax(const QStringList &words, const QString &fragment) const { - QVector matches; + SyntaxList matches; //qDebug() << "words are" << words; if (words.isEmpty()) { - for (const Module &module: s_modules) { - if (module.syntax().keyword.startsWith(fragment)) { - matches.push_back(module.syntax()); + for (const Syntax &syntax: m_syntax) { + if (syntax.keyword.startsWith(fragment)) { + matches.push_back(syntax); } } } else { QStringListIterator wordIt(words); - QString word = wordIt.next(); + QVectorIterator syntaxIt(m_syntax); Syntax lastFullSyntax; - for (const Module &module: s_modules) { - if (module.syntax().keyword == word) { - lastFullSyntax = module.syntax(); - QListIterator syntaxIt(module.syntax().children); - while (wordIt.hasNext()) { - word = wordIt.next(); - while (syntaxIt.hasNext()) { - const Syntax &child = syntaxIt.next(); - if (word == child.keyword) { - lastFullSyntax = child; - syntaxIt = child.children; - } - } + while (wordIt.hasNext()) { + QString word = wordIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &syntax = syntaxIt.next(); + if (word == syntax.keyword) { + lastFullSyntax = syntax; + syntaxIt = syntax.children; } - - break; } } //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); if (lastFullSyntax.keyword == words.last()) { - QListIterator syntaxIt(lastFullSyntax.children); + syntaxIt = lastFullSyntax.children; while (syntaxIt.hasNext()) { Syntax syntax = syntaxIt.next(); if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { diff --git a/akonadi2_cli/module.h b/akonadi2_cli/module.h index 4f426b8..d2745d0 100644 --- a/akonadi2_cli/module.h +++ b/akonadi2_cli/module.h @@ -29,33 +29,42 @@ class Module public: struct Syntax { + enum Interactivity { + NotInteractive = 0, + EventDriven + }; + Syntax(); - Syntax(const QString &keyword, std::function lambda = std::function(), const QString &helpText = QString(), bool eventDriven = false); + Syntax(const QString &keyword, + const QString &helpText = QString(), + std::function lambda = std::function(), + Interactivity interactivity = NotInteractive); + QString keyword; - std::function lambda; - QList children; QString help; - bool eventDriven; + Interactivity interactivity; + std::function lambda; + + QVector children; }; typedef std::pair Command; + typedef QVector SyntaxList; - static void addModule(const Module &module); - static QList modules(); - static Command match(const QStringList &commands); - static bool run(const QStringList &commands); - static void loadModules(); - static QVectornearestSyntax(const QStringList &words, const QString &fragment); + static Module *self(); - Module(); - Module::Syntax syntax() const; - void setSyntax(const Syntax &syntax); + SyntaxList syntax() const; + Command match(const QStringList &commands) const; + SyntaxList nearestSyntax(const QStringList &words, const QString &fragment) const; + + bool run(const QStringList &commands); private: + Module(); Command matches(const QStringList &commands) const; - Syntax m_syntax; - static QList s_modules; - static State s_state; + SyntaxList m_syntax; + State m_state; + static Module *s_module; }; diff --git a/akonadi2_cli/modules/core_syntax.cpp b/akonadi2_cli/modules/core_syntax.cpp new file mode 100644 index 0000000..8324c31 --- /dev/null +++ b/akonadi2_cli/modules/core_syntax.cpp @@ -0,0 +1,85 @@ +/* + * 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 "core_syntax.h" + +#include // tr() +#include +#include + +namespace CoreSyntax +{ + +Module::SyntaxList syntax() +{ + Module::SyntaxList syntax; + syntax << Module::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + syntax << Module::Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + return syntax; +} + +bool exit(const QStringList &, State &) +{ + ::exit(0); + return true; +} + +bool showHelp(const QStringList &commands, State &) +{ + Module::Command command = Module::self()->match(commands); + QTextStream stream(stdout); + if (commands.isEmpty()) { + stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; + stream << QObject::tr("Top-level commands:") << "\n"; + QSet sorted; + for (auto syntax: Module::self()->syntax()) { + sorted.insert(syntax.keyword); + } + + for (auto keyword: sorted) { + stream << "\t" << keyword << "\n"; + } + } else if (const Module::Syntax *syntax = command.first) { + //TODO: get parent! + stream << QObject::tr("Command `%1`").arg(syntax->keyword); + + if (!syntax->help.isEmpty()) { + stream << ": " << syntax->help; + } + stream << "\n"; + + if (!syntax->children.isEmpty()) { + stream << "\tSub-commands:\n"; + QSet sorted; + for (auto childSyntax: syntax->children) { + sorted.insert(childSyntax.keyword); + } + + for (auto keyword: sorted) { + stream << "\t" << keyword << "\n"; + } + } + } else { + stream << "Unknown command: " << commands.join(" ") << "\n"; + } + return true; +} + +} // namespace CoreSyntax + diff --git a/akonadi2_cli/modules/core_syntax.h b/akonadi2_cli/modules/core_syntax.h new file mode 100644 index 0000000..beb8528 --- /dev/null +++ b/akonadi2_cli/modules/core_syntax.h @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#pragma once + +#include "module.h" + +namespace CoreSyntax +{ + Module::SyntaxList syntax(); + bool exit(const QStringList &commands, State &state); + bool showHelp(const QStringList &commands, State &); +} + diff --git a/akonadi2_cli/modules/exit/exit.cpp b/akonadi2_cli/modules/exit/exit.cpp deleted file mode 100644 index 64828be..0000000 --- a/akonadi2_cli/modules/exit/exit.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 "exit.h" - -#include - -namespace CLI -{ - -Exit::Exit() -{ - setSyntax(Syntax("exit", &Exit::exit, QObject::tr("Exits the application. Ctrl-d also works!"))); -} - -bool Exit::exit(const QStringList &, State &) -{ - ::exit(0); - return true; -} - -} // namespace CLI - diff --git a/akonadi2_cli/modules/exit/exit.h b/akonadi2_cli/modules/exit/exit.h deleted file mode 100644 index 5ed4174..0000000 --- a/akonadi2_cli/modules/exit/exit.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "module.h" - -namespace CLI -{ - -class Exit : public Module -{ -public: - Exit(); - -private: - static bool exit(const QStringList &commands, State &state); -}; - -} // namespace CLI - diff --git a/akonadi2_cli/modules/help/help.cpp b/akonadi2_cli/modules/help/help.cpp deleted file mode 100644 index aaff6fb..0000000 --- a/akonadi2_cli/modules/help/help.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 "help.h" - -#include -#include -#include - -#include "module.h" - -namespace CLI -{ - -Help::Help() -{ - Syntax topLevel = Syntax(QObject::tr("help"), &Help::showHelp, QObject::tr("Print command information: help [command]")); - setSyntax(topLevel); -} - -bool Help::showHelp(const QStringList &commands, State &) -{ - Module::Command command = Module::match(commands); - QTextStream stream(stdout); - if (commands.isEmpty()) { - stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; - stream << QObject::tr("Top-level commands:") << "\n"; - QSet sorted; - for (auto module: Module::modules()) { - sorted.insert(module.syntax().keyword); - } - - for (auto keyword: sorted) { - stream << "\t" << keyword << "\n"; - } - } else if (const Syntax *syntax = command.first) { - //TODO: get parent! - stream << QObject::tr("Command `%1`").arg(syntax->keyword); - - if (!syntax->help.isEmpty()) { - stream << ": " << syntax->help; - } - stream << "\n"; - - if (!syntax->children.isEmpty()) { - stream << "\tSub-commands:\n"; - QSet sorted; - for (auto childSyntax: syntax->children) { - sorted.insert(childSyntax.keyword); - } - - for (auto keyword: sorted) { - stream << "\t" << keyword << "\n"; - } - } - } else { - stream << "Unknown command: " << commands.join(" ") << "\n"; - } - - return true; -} - -} // namespace CLI - diff --git a/akonadi2_cli/modules/help/help.h b/akonadi2_cli/modules/help/help.h deleted file mode 100644 index df1cfc2..0000000 --- a/akonadi2_cli/modules/help/help.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "module.h" - -namespace CLI -{ - -class Help : public Module -{ -public: - Help(); - -private: - static bool showHelp(const QStringList &commands, State &state); -}; - -} // namespace CLI - diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp index e87dd5f..efa1353 100644 --- a/akonadi2_cli/repl/replStates.cpp +++ b/akonadi2_cli/repl/replStates.cpp @@ -107,7 +107,7 @@ void EvalState::onEntry(QEvent *event) if (m_complete) { //emit output("Processing ... " + command); const QStringList commands = command.split(" "); - Module::run(commands); + Module::self()->run(commands); emit completed(); } } @@ -143,7 +143,7 @@ static char **akonadi2_cli_tab_completion(const char *text, int start, int end) static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) { - QVector nearest = Module::nearestSyntax(tab_completion_full_state, QString(text)); + QVector nearest = Module::self()->nearestSyntax(tab_completion_full_state, QString(text)); if (nearest.size() > state) { return qstrdup(nearest[state].keyword.toUtf8()); -- cgit v1.2.3 From b0d831910d0615dea94bec77dc46c8c2e415545c Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 11:48:59 +0100 Subject: shove output through state --- akonadi2_cli/state.cpp | 17 ++++++++++++++++- akonadi2_cli/state.h | 9 ++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp index bb21e0e..80a2d3a 100644 --- a/akonadi2_cli/state.cpp +++ b/akonadi2_cli/state.cpp @@ -20,9 +20,24 @@ #include "state.h" #include -#include +#include State::State() + : m_outStream(stdout) { } +void State::print(const QString &message) +{ + m_outStream << message; +} + +void State::printLine(const QString &message) +{ + m_outStream << message << "\n"; +} + +void State::printError(const QString &error, int code) +{ + m_outStream << "ERROR " << code << ": " << error << "\n"; +} diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h index 10b2318..4fd2935 100644 --- a/akonadi2_cli/state.h +++ b/akonadi2_cli/state.h @@ -19,11 +19,18 @@ #pragma once -#include +#include class State { public: State(); + + void print(const QString &string); + void printLine(const QString &string); + void printError(const QString &string, int errorCode = 0); + +private: + QTextStream m_outStream; }; -- cgit v1.2.3 From 6f720eaf111826a1588b37870bf1af638b1420a0 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 11:59:45 +0100 Subject: no longer need to keep this; it's always the top level item --- akonadi2_cli/module.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/akonadi2_cli/module.cpp b/akonadi2_cli/module.cpp index dfc58c8..125caaa 100644 --- a/akonadi2_cli/module.cpp +++ b/akonadi2_cli/module.cpp @@ -100,7 +100,6 @@ Module::Command Module::match(const QStringList &commandLine) const } if (!syntaxIt.hasNext()) { - tailCommands << word; break; } } -- cgit v1.2.3 From a4d438b7f5780e865c824f6e7f892bb3f0ee2b61 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 12:00:00 +0100 Subject: help now uses State to push out output --- akonadi2_cli/modules/core_syntax.cpp | 23 ++++++++++++----------- akonadi2_cli/state.cpp | 16 +++++++++++----- akonadi2_cli/state.h | 6 +++--- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/akonadi2_cli/modules/core_syntax.cpp b/akonadi2_cli/modules/core_syntax.cpp index 8324c31..944abbe 100644 --- a/akonadi2_cli/modules/core_syntax.cpp +++ b/akonadi2_cli/modules/core_syntax.cpp @@ -40,44 +40,45 @@ bool exit(const QStringList &, State &) return true; } -bool showHelp(const QStringList &commands, State &) +bool showHelp(const QStringList &commands, State &state) { Module::Command command = Module::self()->match(commands); - QTextStream stream(stdout); if (commands.isEmpty()) { - stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; - stream << QObject::tr("Top-level commands:") << "\n"; + state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!")); + state.printLine(QObject::tr("Top-level commands:")); + QSet sorted; for (auto syntax: Module::self()->syntax()) { sorted.insert(syntax.keyword); } for (auto keyword: sorted) { - stream << "\t" << keyword << "\n"; + state.printLine(keyword, 1); } } else if (const Module::Syntax *syntax = command.first) { //TODO: get parent! - stream << QObject::tr("Command `%1`").arg(syntax->keyword); + state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); if (!syntax->help.isEmpty()) { - stream << ": " << syntax->help; + state.print(": " + syntax->help); } - stream << "\n"; + state.printLine(); if (!syntax->children.isEmpty()) { - stream << "\tSub-commands:\n"; + state.printLine("Sub-commands:", 1); QSet sorted; for (auto childSyntax: syntax->children) { sorted.insert(childSyntax.keyword); } for (auto keyword: sorted) { - stream << "\t" << keyword << "\n"; + state.printLine(keyword, 1); } } } else { - stream << "Unknown command: " << commands.join(" ") << "\n"; + state.printError("Unknown command: " + commands.join(" ")); } + return true; } diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp index 80a2d3a..9beba7e 100644 --- a/akonadi2_cli/state.cpp +++ b/akonadi2_cli/state.cpp @@ -27,17 +27,23 @@ State::State() { } -void State::print(const QString &message) +void State::print(const QString &message, unsigned int indentationLevel) { + for (unsigned int i = 0; i < indentationLevel; ++i) { + m_outStream << "\t"; + } + m_outStream << message; } -void State::printLine(const QString &message) +void State::printLine(const QString &message, unsigned int indentationLevel) { - m_outStream << message << "\n"; + print(message, indentationLevel); + m_outStream << "\n"; + m_outStream.flush(); } -void State::printError(const QString &error, int code) +void State::printError(const QString &errorMessage, const QString &errorCode) { - m_outStream << "ERROR " << code << ": " << error << "\n"; + printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); } diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h index 4fd2935..a343e51 100644 --- a/akonadi2_cli/state.h +++ b/akonadi2_cli/state.h @@ -26,9 +26,9 @@ class State public: State(); - void print(const QString &string); - void printLine(const QString &string); - void printError(const QString &string, int errorCode = 0); + void print(const QString &message, unsigned int indentationLevel = 0); + void printLine(const QString &message = QString(), unsigned int indentationLevel = 0); + void printError(const QString &errorMessage, const QString &errorCode = QString()); private: QTextStream m_outStream; -- cgit v1.2.3 From afba02ad5fc52acf84a18f67f38b6b75267a31c0 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 16:40:08 +0100 Subject: stub in where command line tokenization will live --- akonadi2_cli/module.cpp | 5 +++++ akonadi2_cli/module.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/akonadi2_cli/module.cpp b/akonadi2_cli/module.cpp index 125caaa..5fd68b4 100644 --- a/akonadi2_cli/module.cpp +++ b/akonadi2_cli/module.cpp @@ -157,4 +157,9 @@ Module::SyntaxList Module::nearestSyntax(const QStringList &words, const QString return matches; } +QStringList Module::tokenize(const QString &text) +{ + //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] + return text.split(" "); +} diff --git a/akonadi2_cli/module.h b/akonadi2_cli/module.h index d2745d0..1149f4f 100644 --- a/akonadi2_cli/module.h +++ b/akonadi2_cli/module.h @@ -59,6 +59,8 @@ public: bool run(const QStringList &commands); + static QStringList tokenize(const QString &text); + private: Module(); Command matches(const QStringList &commands) const; -- cgit v1.2.3 From 8c78033ca7e59c44eb2886a3f8fe89c5b22ad114 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 16:40:17 +0100 Subject: handle multiline, use Module::tokenize --- akonadi2_cli/repl/replStates.cpp | 44 +++++++++++++++++++++++----------------- akonadi2_cli/repl/replStates.h | 4 +++- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp index efa1353..314cca8 100644 --- a/akonadi2_cli/repl/replStates.cpp +++ b/akonadi2_cli/repl/replStates.cpp @@ -81,8 +81,7 @@ const char *UnfinishedReadState::prompt() const } EvalState::EvalState(QState *parent) - : QState(parent), - m_complete(false) + : QState(parent) { } @@ -90,29 +89,36 @@ void EvalState::onEntry(QEvent *event) { QStateMachine::SignalEvent *e = dynamic_cast(event); - if (!e || e->arguments().isEmpty()) { - if (m_complete) { - emit completed(); - } else { - emit continueInput(); - } + const QString command = e ? e->arguments()[0].toString() : QString(); + + if (command.isEmpty()) { + complete(); return; } - //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] - const QString command = e->arguments()[0].toString(); - - if (!command.isEmpty()) { - m_complete = command.right(1) != "\\"; - if (m_complete) { - //emit output("Processing ... " + command); - const QStringList commands = command.split(" "); - Module::self()->run(commands); - emit completed(); - } + if (command.right(1) == "\\") { + m_partial += " " + command.left(command.size() - 1); + continueInput(); + } else { + m_partial += " " + command; + complete(); } } +void EvalState::complete() +{ + m_partial = m_partial.simplified(); + + if (!m_partial.isEmpty()) { + //emit output("Processing ... " + command); + const QStringList commands = Module::tokenize(m_partial); + Module::self()->run(commands); + m_partial.clear(); + } + + emit completed(); +} + PrintState::PrintState(QState *parent) : QState(parent) { diff --git a/akonadi2_cli/repl/replStates.h b/akonadi2_cli/repl/replStates.h index a019a37..a0d3f90 100644 --- a/akonadi2_cli/repl/replStates.h +++ b/akonadi2_cli/repl/replStates.h @@ -66,7 +66,9 @@ protected: void onEntry(QEvent *event); private: - bool m_complete; + void complete(); + + QString m_partial; }; class PrintState : public QState -- cgit v1.2.3 From 7e96145d27329ca3870f23d5b161785d10c9faf5 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 16:43:45 +0100 Subject: Module -> SyntaxTree --- akonadi2_cli/CMakeLists.txt | 4 +- akonadi2_cli/main.cpp | 4 +- akonadi2_cli/module.cpp | 165 ---------------------------- akonadi2_cli/module.h | 72 ------------ akonadi2_cli/modules/core_syntax.cpp | 86 --------------- akonadi2_cli/modules/core_syntax.h | 30 ----- akonadi2_cli/repl/replStates.cpp | 8 +- akonadi2_cli/syntax_modules/core_syntax.cpp | 86 +++++++++++++++ akonadi2_cli/syntax_modules/core_syntax.h | 30 +++++ akonadi2_cli/syntaxtree.cpp | 165 ++++++++++++++++++++++++++++ akonadi2_cli/syntaxtree.h | 72 ++++++++++++ 11 files changed, 361 insertions(+), 361 deletions(-) delete mode 100644 akonadi2_cli/module.cpp delete mode 100644 akonadi2_cli/module.h delete mode 100644 akonadi2_cli/modules/core_syntax.cpp delete mode 100644 akonadi2_cli/modules/core_syntax.h create mode 100644 akonadi2_cli/syntax_modules/core_syntax.cpp create mode 100644 akonadi2_cli/syntax_modules/core_syntax.h create mode 100644 akonadi2_cli/syntaxtree.cpp create mode 100644 akonadi2_cli/syntaxtree.h diff --git a/akonadi2_cli/CMakeLists.txt b/akonadi2_cli/CMakeLists.txt index a07140e..a061ebb 100644 --- a/akonadi2_cli/CMakeLists.txt +++ b/akonadi2_cli/CMakeLists.txt @@ -5,8 +5,8 @@ find_package(Readline REQUIRED) set(akonadi2_cli_SRCS main.cpp - module.cpp - modules/core_syntax.cpp + syntaxtree.cpp + syntax_modules/core_syntax.cpp repl/repl.cpp repl/replStates.cpp state.cpp) diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp index d23e070..f7b7b9f 100644 --- a/akonadi2_cli/main.cpp +++ b/akonadi2_cli/main.cpp @@ -22,7 +22,7 @@ #include #include -#include "module.h" +#include "syntaxtree.h" // #include "jsonlistener.h" #include "repl/repl.h" @@ -64,5 +64,5 @@ int main(int argc, char *argv[]) QStringList commands = app.arguments(); commands.removeFirst(); - return Module::self()->run(commands); + return SyntaxTree::self()->run(commands); } diff --git a/akonadi2_cli/module.cpp b/akonadi2_cli/module.cpp deleted file mode 100644 index 5fd68b4..0000000 --- a/akonadi2_cli/module.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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 "module.h" - -#include -#include - -// TODO: needs a proper registry; making "core" modules plugins is -// almost certainly overkill, but this is not the way either -#include "modules/core_syntax.h" - -Module *Module::s_module = 0; - -Module::Syntax::Syntax() -{ -} - -Module::Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) - : keyword(k), - help(helpText), - interactivity(inter), - lambda(l) -{ -} - -Module::Module() -{ - QVector > syntaxModules; - syntaxModules << &CoreSyntax::syntax; - for (auto syntaxModule: syntaxModules) { - m_syntax += syntaxModule(); - } -} - -Module *Module::self() -{ - if (!s_module) { - s_module = new Module; - } - - return s_module; -} - -Module::SyntaxList Module::syntax() const -{ - return m_syntax; -} - -bool Module::run(const QStringList &commands) -{ - Command command = match(commands); - if (command.first && command.first->lambda) { - command.first->lambda(command.second, m_state); - if (command.first->interactivity == Syntax::EventDriven) { - return QCoreApplication::instance()->exec(); - } - - return true; - } - - return false; -} - -Module::Command Module::match(const QStringList &commandLine) const -{ - if (commandLine.isEmpty()) { - return Command(); - } - - QStringListIterator commandLineIt(commandLine); - - QVectorIterator syntaxIt(m_syntax); - const Syntax *lastFullSyntax = 0; - QStringList tailCommands; - while (commandLineIt.hasNext() && syntaxIt.hasNext()) { - const QString word = commandLineIt.next(); - while (syntaxIt.hasNext()) { - const Syntax &syntax = syntaxIt.next(); - if (word == syntax.keyword) { - lastFullSyntax = &syntax; - syntaxIt = syntax.children; - } - } - - if (!syntaxIt.hasNext()) { - break; - } - } - - if (lastFullSyntax && lastFullSyntax->lambda) { - while (commandLineIt.hasNext()) { - tailCommands << commandLineIt.next(); - } - - return std::make_pair(lastFullSyntax, tailCommands); - } - - return Command(); -} - -Module::SyntaxList Module::nearestSyntax(const QStringList &words, const QString &fragment) const -{ - SyntaxList matches; - - //qDebug() << "words are" << words; - if (words.isEmpty()) { - for (const Syntax &syntax: m_syntax) { - if (syntax.keyword.startsWith(fragment)) { - matches.push_back(syntax); - } - } - } else { - QStringListIterator wordIt(words); - QVectorIterator syntaxIt(m_syntax); - Syntax lastFullSyntax; - - while (wordIt.hasNext()) { - QString word = wordIt.next(); - while (syntaxIt.hasNext()) { - const Syntax &syntax = syntaxIt.next(); - if (word == syntax.keyword) { - lastFullSyntax = syntax; - syntaxIt = syntax.children; - } - } - } - - //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); - if (lastFullSyntax.keyword == words.last()) { - syntaxIt = lastFullSyntax.children; - while (syntaxIt.hasNext()) { - Syntax syntax = syntaxIt.next(); - if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { - matches.push_back(syntax); - } - } - } - } - - return matches; -} - -QStringList Module::tokenize(const QString &text) -{ - //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] - return text.split(" "); -} - diff --git a/akonadi2_cli/module.h b/akonadi2_cli/module.h deleted file mode 100644 index 1149f4f..0000000 --- a/akonadi2_cli/module.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "state.h" - -#include -#include - -class Module -{ -public: - struct Syntax - { - enum Interactivity { - NotInteractive = 0, - EventDriven - }; - - Syntax(); - Syntax(const QString &keyword, - const QString &helpText = QString(), - std::function lambda = std::function(), - Interactivity interactivity = NotInteractive); - - QString keyword; - QString help; - Interactivity interactivity; - std::function lambda; - - QVector children; - }; - - typedef std::pair Command; - typedef QVector SyntaxList; - - static Module *self(); - - SyntaxList syntax() const; - Command match(const QStringList &commands) const; - SyntaxList nearestSyntax(const QStringList &words, const QString &fragment) const; - - bool run(const QStringList &commands); - - static QStringList tokenize(const QString &text); - -private: - Module(); - Command matches(const QStringList &commands) const; - - SyntaxList m_syntax; - State m_state; - static Module *s_module; -}; - diff --git a/akonadi2_cli/modules/core_syntax.cpp b/akonadi2_cli/modules/core_syntax.cpp deleted file mode 100644 index 944abbe..0000000 --- a/akonadi2_cli/modules/core_syntax.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 "core_syntax.h" - -#include // tr() -#include -#include - -namespace CoreSyntax -{ - -Module::SyntaxList syntax() -{ - Module::SyntaxList syntax; - syntax << Module::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - syntax << Module::Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); - return syntax; -} - -bool exit(const QStringList &, State &) -{ - ::exit(0); - return true; -} - -bool showHelp(const QStringList &commands, State &state) -{ - Module::Command command = Module::self()->match(commands); - if (commands.isEmpty()) { - state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!")); - state.printLine(QObject::tr("Top-level commands:")); - - QSet sorted; - for (auto syntax: Module::self()->syntax()) { - sorted.insert(syntax.keyword); - } - - for (auto keyword: sorted) { - state.printLine(keyword, 1); - } - } else if (const Module::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; -} - -} // namespace CoreSyntax - diff --git a/akonadi2_cli/modules/core_syntax.h b/akonadi2_cli/modules/core_syntax.h deleted file mode 100644 index beb8528..0000000 --- a/akonadi2_cli/modules/core_syntax.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "module.h" - -namespace CoreSyntax -{ - Module::SyntaxList syntax(); - bool exit(const QStringList &commands, State &state); - bool showHelp(const QStringList &commands, State &); -} - diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp index 314cca8..0273aa2 100644 --- a/akonadi2_cli/repl/replStates.cpp +++ b/akonadi2_cli/repl/replStates.cpp @@ -29,7 +29,7 @@ #include #include -#include "module.h" +#include "syntaxtree.h" static char *akonadi2_cli_next_tab_complete_match(const char *text, int state); static char ** akonadi2_cli_tab_completion(const char *text, int start, int end); @@ -111,8 +111,8 @@ void EvalState::complete() if (!m_partial.isEmpty()) { //emit output("Processing ... " + command); - const QStringList commands = Module::tokenize(m_partial); - Module::self()->run(commands); + const QStringList commands = SyntaxTree::tokenize(m_partial); + SyntaxTree::self()->run(commands); m_partial.clear(); } @@ -149,7 +149,7 @@ static char **akonadi2_cli_tab_completion(const char *text, int start, int end) static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) { - QVector nearest = Module::self()->nearestSyntax(tab_completion_full_state, QString(text)); + QVector nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, QString(text)); if (nearest.size() > state) { return qstrdup(nearest[state].keyword.toUtf8()); diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp new file mode 100644 index 0000000..f71c1d6 --- /dev/null +++ b/akonadi2_cli/syntax_modules/core_syntax.cpp @@ -0,0 +1,86 @@ +/* + * 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 "core_syntax.h" + +#include // tr() +#include +#include + +namespace CoreSyntax +{ + +SyntaxTree::SyntaxList syntax() +{ + SyntaxTree::SyntaxList syntax; + syntax << SyntaxTree::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + syntax << SyntaxTree::Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + return syntax; +} + +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 Akonadi2 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 SyntaxTree::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; +} + +} // namespace CoreSyntax + diff --git a/akonadi2_cli/syntax_modules/core_syntax.h b/akonadi2_cli/syntax_modules/core_syntax.h new file mode 100644 index 0000000..0db6661 --- /dev/null +++ b/akonadi2_cli/syntax_modules/core_syntax.h @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace CoreSyntax +{ + SyntaxTree::SyntaxList syntax(); + bool exit(const QStringList &commands, State &state); + bool showHelp(const QStringList &commands, State &); +} + diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp new file mode 100644 index 0000000..ea017db --- /dev/null +++ b/akonadi2_cli/syntaxtree.cpp @@ -0,0 +1,165 @@ +/* + * 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 "syntaxtree.h" + +#include +#include + +// TODO: needs a proper registry; making "core" modules plugins is +// almost certainly overkill, but this is not the way either +#include "syntax_modules/core_syntax.h" + +SyntaxTree *SyntaxTree::s_module = 0; + +SyntaxTree::Syntax::Syntax() +{ +} + +SyntaxTree::Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) + : keyword(k), + help(helpText), + interactivity(inter), + lambda(l) +{ +} + +SyntaxTree::SyntaxTree() +{ + QVector > syntaxSyntaxTrees; + syntaxSyntaxTrees << &CoreSyntax::syntax; + for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { + m_syntax += syntaxSyntaxTree(); + } +} + +SyntaxTree *SyntaxTree::self() +{ + if (!s_module) { + s_module = new SyntaxTree; + } + + return s_module; +} + +SyntaxTree::SyntaxList SyntaxTree::syntax() const +{ + return m_syntax; +} + +bool SyntaxTree::run(const QStringList &commands) +{ + Command command = match(commands); + if (command.first && command.first->lambda) { + command.first->lambda(command.second, m_state); + if (command.first->interactivity == Syntax::EventDriven) { + return QCoreApplication::instance()->exec(); + } + + return true; + } + + return false; +} + +SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const +{ + if (commandLine.isEmpty()) { + return Command(); + } + + QStringListIterator commandLineIt(commandLine); + + QVectorIterator syntaxIt(m_syntax); + const Syntax *lastFullSyntax = 0; + QStringList tailCommands; + while (commandLineIt.hasNext() && syntaxIt.hasNext()) { + const QString word = commandLineIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &syntax = syntaxIt.next(); + if (word == syntax.keyword) { + lastFullSyntax = &syntax; + syntaxIt = syntax.children; + } + } + + if (!syntaxIt.hasNext()) { + break; + } + } + + if (lastFullSyntax && lastFullSyntax->lambda) { + while (commandLineIt.hasNext()) { + tailCommands << commandLineIt.next(); + } + + return std::make_pair(lastFullSyntax, tailCommands); + } + + return Command(); +} + +SyntaxTree::SyntaxList SyntaxTree::nearestSyntax(const QStringList &words, const QString &fragment) const +{ + SyntaxList matches; + + //qDebug() << "words are" << words; + if (words.isEmpty()) { + for (const Syntax &syntax: m_syntax) { + if (syntax.keyword.startsWith(fragment)) { + matches.push_back(syntax); + } + } + } else { + QStringListIterator wordIt(words); + QVectorIterator syntaxIt(m_syntax); + Syntax lastFullSyntax; + + while (wordIt.hasNext()) { + QString word = wordIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &syntax = syntaxIt.next(); + if (word == syntax.keyword) { + lastFullSyntax = syntax; + syntaxIt = syntax.children; + } + } + } + + //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); + if (lastFullSyntax.keyword == words.last()) { + syntaxIt = lastFullSyntax.children; + while (syntaxIt.hasNext()) { + Syntax syntax = syntaxIt.next(); + if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { + matches.push_back(syntax); + } + } + } + } + + return matches; +} + +QStringList SyntaxTree::tokenize(const QString &text) +{ + //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] + return text.split(" "); +} + diff --git a/akonadi2_cli/syntaxtree.h b/akonadi2_cli/syntaxtree.h new file mode 100644 index 0000000..54b867f --- /dev/null +++ b/akonadi2_cli/syntaxtree.h @@ -0,0 +1,72 @@ +/* + * 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. + */ + +#pragma once + +#include "state.h" + +#include +#include + +class SyntaxTree +{ +public: + struct Syntax + { + enum Interactivity { + NotInteractive = 0, + EventDriven + }; + + Syntax(); + Syntax(const QString &keyword, + const QString &helpText = QString(), + std::function lambda = std::function(), + Interactivity interactivity = NotInteractive); + + QString keyword; + QString help; + Interactivity interactivity; + std::function lambda; + + QVector children; + }; + + typedef std::pair Command; + typedef QVector SyntaxList; + + static SyntaxTree *self(); + + SyntaxList syntax() const; + Command match(const QStringList &commands) const; + SyntaxList nearestSyntax(const QStringList &words, const QString &fragment) const; + + bool run(const QStringList &commands); + + static QStringList tokenize(const QString &text); + +private: + SyntaxTree(); + Command matches(const QStringList &commands) const; + + SyntaxList m_syntax; + State m_state; + static SyntaxTree *s_module; +}; + -- cgit v1.2.3 From df040bb75653245960cfbab3c1a28baee53fd3c4 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 17:02:20 +0100 Subject: refactor breakage: move the break to where it belongs --- akonadi2_cli/syntaxtree.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index ea017db..fe1acfb 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -96,12 +96,9 @@ SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const if (word == syntax.keyword) { lastFullSyntax = &syntax; syntaxIt = syntax.children; + break; } } - - if (!syntaxIt.hasNext()) { - break; - } } if (lastFullSyntax && lastFullSyntax->lambda) { -- cgit v1.2.3 From 72f52fb148380feb018faa9a94a8ad54bb93635f Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 17:02:56 +0100 Subject: debug level --- akonadi2_cli/state.cpp | 13 +++++++++++++ akonadi2_cli/state.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp index 9beba7e..08934a8 100644 --- a/akonadi2_cli/state.cpp +++ b/akonadi2_cli/state.cpp @@ -47,3 +47,16 @@ void State::printError(const QString &errorMessage, const QString &errorCode) { printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); } + +void State::setDebugLevel(unsigned int level) +{ + if (level < 7) { + m_debugLevel = level; + } +} + +unsigned int State::debugLevel() const +{ + return m_debugLevel; +} + diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h index a343e51..2f13166 100644 --- a/akonadi2_cli/state.h +++ b/akonadi2_cli/state.h @@ -30,7 +30,11 @@ public: void printLine(const QString &message = QString(), unsigned int indentationLevel = 0); void printError(const QString &errorMessage, const QString &errorCode = QString()); + void setDebugLevel(unsigned int level); + unsigned int debugLevel() const; + private: + int m_debugLevel = 0; QTextStream m_outStream; }; -- cgit v1.2.3 From e995699ad88e6c6f1980d11c45a544c79993ccb5 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 17:02:59 +0100 Subject: set/get syntax, currently just for debug level --- akonadi2_cli/syntax_modules/core_syntax.cpp | 35 +++++++++++++++++++++++++++++ akonadi2_cli/syntax_modules/core_syntax.h | 4 +++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp index f71c1d6..f9cd622 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.cpp +++ b/akonadi2_cli/syntax_modules/core_syntax.cpp @@ -19,6 +19,7 @@ #include "core_syntax.h" +#include #include // tr() #include #include @@ -31,6 +32,15 @@ SyntaxTree::SyntaxList syntax() SyntaxTree::SyntaxList syntax; syntax << SyntaxTree::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); syntax << SyntaxTree::Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + + SyntaxTree::Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); + set.children << SyntaxTree::Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + syntax << set; + + SyntaxTree::Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); + get.children << SyntaxTree::Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + syntax << get; + return syntax; } @@ -82,5 +92,30 @@ bool showHelp(const QStringList &commands, State &state) return true; } +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 &commands, State &state) +{ + state.printLine(QString::number(state.debugLevel())); + return true; +} + } // namespace CoreSyntax diff --git a/akonadi2_cli/syntax_modules/core_syntax.h b/akonadi2_cli/syntax_modules/core_syntax.h index 0db6661..4afd69d 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.h +++ b/akonadi2_cli/syntax_modules/core_syntax.h @@ -25,6 +25,8 @@ namespace CoreSyntax { SyntaxTree::SyntaxList syntax(); bool exit(const QStringList &commands, State &state); - bool showHelp(const QStringList &commands, State &); + bool showHelp(const QStringList &commands, State &state); + bool setDebugLevel(const QStringList &commands, State &state); + bool printDebugLevel(const QStringList &commands, State &state); } -- cgit v1.2.3 From 391e112e999075abf0ddb8abf1b13b8b9f6f3601 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:05:32 +0100 Subject: ssh --- akonadi2_cli/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp index f7b7b9f..a2b25a9 100644 --- a/akonadi2_cli/main.cpp +++ b/akonadi2_cli/main.cpp @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) //TODO: make a json command parse cause that would be awesomesauce const bool startJsonListener = !startRepl && (argc == 2 && qstrcmp(argv[1], "-") == 0); - qDebug() << "state at startup is" << interactive << startRepl << startJsonListener; + //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener; QCoreApplication app(argc, argv); app.setApplicationName("funq"); -- cgit v1.2.3 From 01526599a191d2e31a836a9d12f089f564c646e6 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:05:36 +0100 Subject: take the name we were started with --- akonadi2_cli/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp index a2b25a9..91936c3 100644 --- a/akonadi2_cli/main.cpp +++ b/akonadi2_cli/main.cpp @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener; QCoreApplication app(argc, argv); - app.setApplicationName("funq"); + app.setApplicationName(argv[0]); if (startRepl || startJsonListener) { if (startRepl) { -- cgit v1.2.3 From 00a4b8b6a90539f42334a489e8e44a3d0e5a246c Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:21:23 +0100 Subject: some useful common utilities --- akonadi2_cli/akonadish_utils.cpp | 74 ++++++++++++++++++++++++++++++++++++ akonadi2_cli/akonadish_utils.h | 81 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 akonadi2_cli/akonadish_utils.cpp create mode 100644 akonadi2_cli/akonadish_utils.h diff --git a/akonadi2_cli/akonadish_utils.cpp b/akonadi2_cli/akonadish_utils.cpp new file mode 100644 index 0000000..bfb72ca --- /dev/null +++ b/akonadi2_cli/akonadish_utils.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2015 Aaron Seigo + * Copyright (C) 2015 Christian Mollekopf + * + * 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 "akonadish_utils.h" + +#include "common/clientapi.h" + +namespace AkonadishUtils +{ + +bool isValidStoreType(const QString &type) +{ + static const QSet types = QSet() << "folder" << "mail" << "event" << "resource"; + return types.contains(type); +} + +StoreBase &getStore(const QString &type) +{ + if (type == "folder") { + static Store store; + return store; + } else if (type == "mail") { + static Store store; + return store; + } else if (type == "event") { + static Store store; + return store; + } else if (type == "resource") { + static Store store; + return store; + } + + //TODO: reinstate the warning+assert + //Q_ASSERT(false); + //qWarning() << "Trying to get a store that doesn't exist, falling back to event"; + static Store store; + return store; +} + +QSharedPointer loadModel(const QString &type, Akonadi2::Query query) +{ + if (type == "folder") { + query.requestedProperties << "name" << "parent"; + } else if (type == "mail") { + query.requestedProperties << "subject" << "folder" << "date"; + } else if (type == "event") { + query.requestedProperties << "summary"; + } else if (type == "resource") { + query.requestedProperties << "type"; + } + auto model = getStore(type).loadModel(query); + Q_ASSERT(model); + return model; +} + +} + diff --git a/akonadi2_cli/akonadish_utils.h b/akonadi2_cli/akonadish_utils.h new file mode 100644 index 0000000..c15162f --- /dev/null +++ b/akonadi2_cli/akonadish_utils.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2015 Aaron Seigo + * Copyright (C) 2015 Christian Mollekopf + * + * 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. + */ + +#pragma once + +#include +#include + +#include "common/query.h" +#include "common/clientapi.h" + +namespace AkonadishUtils +{ + +class StoreBase; + +bool isValidStoreType(const QString &type); +StoreBase &getStore(const QString &type); +QSharedPointer loadModel(const QString &type, Akonadi2::Query query); + +/** + * A small abstraction layer to use the akonadi store with the type available as string. + */ +class StoreBase { +public: + virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0; + virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0; + virtual KAsync::Job create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; + virtual KAsync::Job modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; + virtual KAsync::Job remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; + virtual QSharedPointer loadModel(const Akonadi2::Query &query) = 0; +}; + +template +class Store : public StoreBase { +public: + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE { + return T::Ptr::create(); + } + + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE { + return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer::create()); + } + + KAsync::Job create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { + return Akonadi2::Store::create(*static_cast(&type)); + } + + KAsync::Job modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { + return Akonadi2::Store::modify(*static_cast(&type)); + } + + KAsync::Job remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { + return Akonadi2::Store::remove(*static_cast(&type)); + } + + QSharedPointer loadModel(const Akonadi2::Query &query) Q_DECL_OVERRIDE { + return Akonadi2::Store::loadModel(query); + } +}; + + +} + -- cgit v1.2.3 From 34210d830a2075562bb18c7803bedca7540a2512 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:21:46 +0100 Subject: listing --- akonadi2_cli/syntax_modules/akonadi_list.cpp | 112 +++++++++++++++++++++++++++ akonadi2_cli/syntax_modules/akonadi_list.h | 29 +++++++ 2 files changed, 141 insertions(+) create mode 100644 akonadi2_cli/syntax_modules/akonadi_list.cpp create mode 100644 akonadi2_cli/syntax_modules/akonadi_list.h diff --git a/akonadi2_cli/syntax_modules/akonadi_list.cpp b/akonadi2_cli/syntax_modules/akonadi_list.cpp new file mode 100644 index 0000000..26b1f1e --- /dev/null +++ b/akonadi2_cli/syntax_modules/akonadi_list.cpp @@ -0,0 +1,112 @@ +/* + * 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 "akonadi_list.h" + +#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 "akonadish_utils.h" + +namespace AkonadiList +{ + +SyntaxTree::SyntaxList syntax() +{ + SyntaxTree::SyntaxList syntax; + syntax << SyntaxTree::Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, SyntaxTree::Syntax::EventDriven); + + return syntax; +} + +bool list(const QStringList &args, State &state) +{ + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Akonadi2::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + + QTime time; + time.start(); + auto model = AkonadishUtils::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("Column") + " "); + 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++) { + state.print(" " + QObject::tr("Row %1").arg(QString::number(model->rowCount())).rightJustified(4, ' ') + ": "); + auto object = model->data(model->index(i, 0, index), Akonadi2::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(Akonadi2::Store::ChildrenFetchedRole)) { + state.commandFinished(); + } + }); + + if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) { + return true; + } + + return false; +} + +} diff --git a/akonadi2_cli/syntax_modules/akonadi_list.h b/akonadi2_cli/syntax_modules/akonadi_list.h new file mode 100644 index 0000000..f2a123d --- /dev/null +++ b/akonadi2_cli/syntax_modules/akonadi_list.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace AkonadiList +{ + SyntaxTree::SyntaxList syntax(); + bool list(const QStringList &commands, State &state); +} + -- cgit v1.2.3 From 25a17e16901590a9400deef9e2feb5423df84b59 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:22:00 +0100 Subject: setart of akonadi integration --- akonadi2_cli/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/akonadi2_cli/CMakeLists.txt b/akonadi2_cli/CMakeLists.txt index a061ebb..669ad52 100644 --- a/akonadi2_cli/CMakeLists.txt +++ b/akonadi2_cli/CMakeLists.txt @@ -7,6 +7,8 @@ set(akonadi2_cli_SRCS main.cpp syntaxtree.cpp syntax_modules/core_syntax.cpp + syntax_modules/akonadi_list.cpp + akonadish_utils.cpp repl/repl.cpp repl/replStates.cpp state.cpp) @@ -14,6 +16,6 @@ set(akonadi2_cli_SRCS include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) add_executable(${PROJECT_NAME} ${akonadi2_cli_SRCS}) -target_link_libraries(${PROJECT_NAME} Qt5::Core ${Readline_LIBRARY}) +target_link_libraries(${PROJECT_NAME} Qt5::Core ${Readline_LIBRARY} akonadi2common) install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) -- cgit v1.2.3 From 109fa78cd929b134e8fa826ec5c64b09dc88c3b7 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:22:15 +0100 Subject: get around const'ness with a dptr and introduce an event loop --- akonadi2_cli/state.cpp | 49 +++++++++++++++++++++++++++++++++++++++---------- akonadi2_cli/state.h | 13 ++++++++----- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp index 08934a8..257da6b 100644 --- a/akonadi2_cli/state.cpp +++ b/akonadi2_cli/state.cpp @@ -20,30 +20,44 @@ #include "state.h" #include +#include #include +class State::Private +{ +public: + Private() + : outStream(stdout) + { + } + + int debugLevel = 0; + QEventLoop eventLoop; + QTextStream outStream; +}; + State::State() - : m_outStream(stdout) + : d(new Private) { } -void State::print(const QString &message, unsigned int indentationLevel) +void State::print(const QString &message, unsigned int indentationLevel) const { for (unsigned int i = 0; i < indentationLevel; ++i) { - m_outStream << "\t"; + d->outStream << "\t"; } - m_outStream << message; + d->outStream << message; } -void State::printLine(const QString &message, unsigned int indentationLevel) +void State::printLine(const QString &message, unsigned int indentationLevel) const { print(message, indentationLevel); - m_outStream << "\n"; - m_outStream.flush(); + d->outStream << "\n"; + d->outStream.flush(); } -void State::printError(const QString &errorMessage, const QString &errorCode) +void State::printError(const QString &errorMessage, const QString &errorCode) const { printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); } @@ -51,12 +65,27 @@ void State::printError(const QString &errorMessage, const QString &errorCode) void State::setDebugLevel(unsigned int level) { if (level < 7) { - m_debugLevel = level; + d->debugLevel = level; } } unsigned int State::debugLevel() const { - return m_debugLevel; + return d->debugLevel; +} + +int State::commandStarted() const +{ + if (!d->eventLoop.isRunning()) { + qDebug() << "RUNNING THE EVENT LOOP!"; + return d->eventLoop.exec(); + } + + return 0; +} + +void State::commandFinished(int returnCode) const +{ + d->eventLoop.exit(returnCode); } diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h index 2f13166..eb07f56 100644 --- a/akonadi2_cli/state.h +++ b/akonadi2_cli/state.h @@ -26,15 +26,18 @@ class State public: State(); - void print(const QString &message, unsigned int indentationLevel = 0); - void printLine(const QString &message = QString(), unsigned int indentationLevel = 0); - void printError(const QString &errorMessage, const QString &errorCode = QString()); + void print(const QString &message, unsigned int indentationLevel = 0) const; + void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const; + void printError(const QString &errorMessage, const QString &errorCode = QString()) const; void setDebugLevel(unsigned int level); unsigned int debugLevel() const; + int commandStarted() const; + void commandFinished(int returnCode = 0) const; + private: - int m_debugLevel = 0; - QTextStream m_outStream; + class Private; + Private * const d; }; -- cgit v1.2.3 From 0a882e76dde2795f2f74ca28f775a74ac7177f54 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:23:10 +0100 Subject: start the loop via State and only if the command succeeded in setting up --- akonadi2_cli/syntaxtree.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index fe1acfb..e87b291 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -67,12 +67,12 @@ bool SyntaxTree::run(const QStringList &commands) { Command command = match(commands); if (command.first && command.first->lambda) { - command.first->lambda(command.second, m_state); - if (command.first->interactivity == Syntax::EventDriven) { - return QCoreApplication::instance()->exec(); + bool rv = command.first->lambda(command.second, m_state); + if (rv && command.first->interactivity == Syntax::EventDriven) { + return m_state.commandStarted(); } - return true; + return rv; } return false; -- cgit v1.2.3 From 26c510ace968af7d4c82c1f192bc8cdcb3505910 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:23:50 +0100 Subject: akonadi list --- akonadi2_cli/syntaxtree.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index e87b291..5dd4c54 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -25,6 +25,7 @@ // TODO: needs a proper registry; making "core" modules plugins is // almost certainly overkill, but this is not the way either #include "syntax_modules/core_syntax.h" +#include "syntax_modules/akonadi_list.h" SyntaxTree *SyntaxTree::s_module = 0; @@ -43,7 +44,9 @@ SyntaxTree::Syntax::Syntax(const QString &k, const QString &helpText, std::funct SyntaxTree::SyntaxTree() { QVector > syntaxSyntaxTrees; - syntaxSyntaxTrees << &CoreSyntax::syntax; + syntaxSyntaxTrees << &CoreSyntax::syntax + << &AkonadiList::syntax + ; for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { m_syntax += syntaxSyntaxTree(); } -- cgit v1.2.3 From 5e9c1172ca196bdc8bcdc842e54efc5a74908fff Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:31:25 +0100 Subject: move Syntax out of SyntraxTree --- akonadi2_cli/repl/replStates.cpp | 2 +- akonadi2_cli/syntax_modules/akonadi_list.cpp | 2 +- akonadi2_cli/syntax_modules/core_syntax.cpp | 14 ++++----- akonadi2_cli/syntaxtree.cpp | 4 +-- akonadi2_cli/syntaxtree.h | 44 +++++++++++++++------------- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp index 0273aa2..0179779 100644 --- a/akonadi2_cli/repl/replStates.cpp +++ b/akonadi2_cli/repl/replStates.cpp @@ -149,7 +149,7 @@ static char **akonadi2_cli_tab_completion(const char *text, int start, int end) static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) { - QVector nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, QString(text)); + SyntaxTree::SyntaxList nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, QString(text)); if (nearest.size() > state) { return qstrdup(nearest[state].keyword.toUtf8()); diff --git a/akonadi2_cli/syntax_modules/akonadi_list.cpp b/akonadi2_cli/syntax_modules/akonadi_list.cpp index 26b1f1e..62824ee 100644 --- a/akonadi2_cli/syntax_modules/akonadi_list.cpp +++ b/akonadi2_cli/syntax_modules/akonadi_list.cpp @@ -42,7 +42,7 @@ namespace AkonadiList SyntaxTree::SyntaxList syntax() { SyntaxTree::SyntaxList syntax; - syntax << SyntaxTree::Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, SyntaxTree::Syntax::EventDriven); + syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven); return syntax; } diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp index f9cd622..1231e29 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.cpp +++ b/akonadi2_cli/syntax_modules/core_syntax.cpp @@ -30,15 +30,15 @@ namespace CoreSyntax SyntaxTree::SyntaxList syntax() { SyntaxTree::SyntaxList syntax; - syntax << SyntaxTree::Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - syntax << SyntaxTree::Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + syntax << Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); - SyntaxTree::Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); - set.children << SyntaxTree::Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); + set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); syntax << set; - SyntaxTree::Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << SyntaxTree::Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); + get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); syntax << get; return syntax; @@ -65,7 +65,7 @@ bool showHelp(const QStringList &commands, State &state) for (auto keyword: sorted) { state.printLine(keyword, 1); } - } else if (const SyntaxTree::Syntax *syntax = command.first) { + } else if (const Syntax *syntax = command.first) { //TODO: get parent! state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index 5dd4c54..a4f5d40 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -29,11 +29,11 @@ SyntaxTree *SyntaxTree::s_module = 0; -SyntaxTree::Syntax::Syntax() +Syntax::Syntax() { } -SyntaxTree::Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) +Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) : keyword(k), help(helpText), interactivity(inter), diff --git a/akonadi2_cli/syntaxtree.h b/akonadi2_cli/syntaxtree.h index 54b867f..7282955 100644 --- a/akonadi2_cli/syntaxtree.h +++ b/akonadi2_cli/syntaxtree.h @@ -24,32 +24,34 @@ #include #include -class SyntaxTree +class Syntax { public: - struct Syntax - { - enum Interactivity { - NotInteractive = 0, - EventDriven - }; - - Syntax(); - Syntax(const QString &keyword, - const QString &helpText = QString(), - std::function lambda = std::function(), - Interactivity interactivity = NotInteractive); - - QString keyword; - QString help; - Interactivity interactivity; - std::function lambda; - - QVector children; + enum Interactivity { + NotInteractive = 0, + EventDriven }; + Syntax(); + Syntax(const QString &keyword, + const QString &helpText = QString(), + std::function lambda = std::function(), + Interactivity interactivity = NotInteractive); + + QString keyword; + QString help; + Interactivity interactivity; + std::function lambda; + + QVector children; +}; + +class SyntaxTree +{ +public: + typedef std::pair Command; - typedef QVector SyntaxList; + typedef QVector SyntaxList; static SyntaxTree *self(); -- cgit v1.2.3 From ce6f9b1eb4320a08b059cfb4f9a81f8d99653b1a Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:33:42 +0100 Subject: SyntaxTree::SyntaxList -> Syntax::List --- akonadi2_cli/repl/replStates.cpp | 2 +- akonadi2_cli/syntax_modules/akonadi_list.cpp | 4 ++-- akonadi2_cli/syntax_modules/akonadi_list.h | 2 +- akonadi2_cli/syntax_modules/core_syntax.cpp | 4 ++-- akonadi2_cli/syntax_modules/core_syntax.h | 2 +- akonadi2_cli/syntaxtree.cpp | 8 ++++---- akonadi2_cli/syntaxtree.h | 9 +++++---- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp index 0179779..ceb95ed 100644 --- a/akonadi2_cli/repl/replStates.cpp +++ b/akonadi2_cli/repl/replStates.cpp @@ -149,7 +149,7 @@ static char **akonadi2_cli_tab_completion(const char *text, int start, int end) static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) { - SyntaxTree::SyntaxList nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, QString(text)); + Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, QString(text)); if (nearest.size() > state) { return qstrdup(nearest[state].keyword.toUtf8()); diff --git a/akonadi2_cli/syntax_modules/akonadi_list.cpp b/akonadi2_cli/syntax_modules/akonadi_list.cpp index 62824ee..6abc853 100644 --- a/akonadi2_cli/syntax_modules/akonadi_list.cpp +++ b/akonadi2_cli/syntax_modules/akonadi_list.cpp @@ -39,9 +39,9 @@ namespace AkonadiList { -SyntaxTree::SyntaxList syntax() +Syntax::List syntax() { - SyntaxTree::SyntaxList syntax; + Syntax::List syntax; syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven); return syntax; diff --git a/akonadi2_cli/syntax_modules/akonadi_list.h b/akonadi2_cli/syntax_modules/akonadi_list.h index f2a123d..61effc5 100644 --- a/akonadi2_cli/syntax_modules/akonadi_list.h +++ b/akonadi2_cli/syntax_modules/akonadi_list.h @@ -23,7 +23,7 @@ namespace AkonadiList { - SyntaxTree::SyntaxList syntax(); + Syntax::List syntax(); bool list(const QStringList &commands, State &state); } diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp index 1231e29..407fcbf 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.cpp +++ b/akonadi2_cli/syntax_modules/core_syntax.cpp @@ -27,9 +27,9 @@ namespace CoreSyntax { -SyntaxTree::SyntaxList syntax() +Syntax::List syntax() { - SyntaxTree::SyntaxList syntax; + Syntax::List syntax; syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); syntax << Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); diff --git a/akonadi2_cli/syntax_modules/core_syntax.h b/akonadi2_cli/syntax_modules/core_syntax.h index 4afd69d..521cb8e 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.h +++ b/akonadi2_cli/syntax_modules/core_syntax.h @@ -23,7 +23,7 @@ namespace CoreSyntax { - SyntaxTree::SyntaxList syntax(); + Syntax::List syntax(); bool exit(const QStringList &commands, State &state); bool showHelp(const QStringList &commands, State &state); bool setDebugLevel(const QStringList &commands, State &state); diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index a4f5d40..968802d 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -43,7 +43,7 @@ Syntax::Syntax(const QString &k, const QString &helpText, std::function > syntaxSyntaxTrees; + QVector > syntaxSyntaxTrees; syntaxSyntaxTrees << &CoreSyntax::syntax << &AkonadiList::syntax ; @@ -61,7 +61,7 @@ SyntaxTree *SyntaxTree::self() return s_module; } -SyntaxTree::SyntaxList SyntaxTree::syntax() const +Syntax::List SyntaxTree::syntax() const { return m_syntax; } @@ -115,9 +115,9 @@ SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const return Command(); } -SyntaxTree::SyntaxList SyntaxTree::nearestSyntax(const QStringList &words, const QString &fragment) const +Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString &fragment) const { - SyntaxList matches; + Syntax::List matches; //qDebug() << "words are" << words; if (words.isEmpty()) { diff --git a/akonadi2_cli/syntaxtree.h b/akonadi2_cli/syntaxtree.h index 7282955..c7df104 100644 --- a/akonadi2_cli/syntaxtree.h +++ b/akonadi2_cli/syntaxtree.h @@ -27,6 +27,8 @@ class Syntax { public: + typedef QVector List; + enum Interactivity { NotInteractive = 0, EventDriven @@ -51,13 +53,12 @@ class SyntaxTree public: typedef std::pair Command; - typedef QVector SyntaxList; static SyntaxTree *self(); - SyntaxList syntax() const; + Syntax::List syntax() const; Command match(const QStringList &commands) const; - SyntaxList nearestSyntax(const QStringList &words, const QString &fragment) const; + Syntax::List nearestSyntax(const QStringList &words, const QString &fragment) const; bool run(const QStringList &commands); @@ -67,7 +68,7 @@ private: SyntaxTree(); Command matches(const QStringList &commands) const; - SyntaxList m_syntax; + Syntax::List m_syntax; State m_state; static SyntaxTree *s_module; }; -- cgit v1.2.3 From 9e02fbcefabb75c4ea16d7c28fd562100251fdfc Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:45:51 +0100 Subject: deadcode --- akonadi2_cli/syntaxtree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akonadi2_cli/syntaxtree.h b/akonadi2_cli/syntaxtree.h index c7df104..77f52af 100644 --- a/akonadi2_cli/syntaxtree.h +++ b/akonadi2_cli/syntaxtree.h @@ -44,6 +44,7 @@ public: QString help; Interactivity interactivity; std::function lambda; + std::function completer; QVector children; }; @@ -66,7 +67,6 @@ public: private: SyntaxTree(); - Command matches(const QStringList &commands) const; Syntax::List m_syntax; State m_state; -- cgit v1.2.3 From a6d04e09a8476773c08eca37acaea95e4f83e5f2 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:52:44 +0100 Subject: return the command even if the syntax doesn't have an exec lambda we check it elsewhere, and this is useful for autocomplete use --- akonadi2_cli/syntaxtree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index 968802d..cebfc4b 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -104,7 +104,7 @@ SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const } } - if (lastFullSyntax && lastFullSyntax->lambda) { + if (lastFullSyntax) { while (commandLineIt.hasNext()) { tailCommands << commandLineIt.next(); } -- cgit v1.2.3 From 42d772acc88a2c57b5761b67918ae7f502a9a7f9 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:53:11 +0100 Subject: allow commands to do their own completions --- akonadi2_cli/repl/replStates.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp index ceb95ed..62888d0 100644 --- a/akonadi2_cli/repl/replStates.cpp +++ b/akonadi2_cli/repl/replStates.cpp @@ -149,9 +149,19 @@ static char **akonadi2_cli_tab_completion(const char *text, int start, int end) static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) { - Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, QString(text)); - - if (nearest.size() > state) { + const QString fragment(text); + Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, fragment); + //for (auto syntax: nearest) { qDebug() << "Nearest: " << syntax.keyword; } + + if (nearest.isEmpty()) { + SyntaxTree::Command command = SyntaxTree::self()->match(tab_completion_full_state); + if (command.first && command.first->completer) { + QStringList commandCompletions = command.first->completer(tab_completion_full_state, fragment); + if (commandCompletions.size() > state) { + return qstrdup(commandCompletions[state].toUtf8()); + } + } + } else if (nearest.size() > state) { return qstrdup(nearest[state].keyword.toUtf8()); } -- cgit v1.2.3 From a9eb4e0c25ff4153a57a142d3ac28b8c5fc1a13d Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:53:41 +0100 Subject: help can now autocomplete what it shows help for --- akonadi2_cli/syntax_modules/core_syntax.cpp | 19 ++++++++++++++++++- akonadi2_cli/syntax_modules/core_syntax.h | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp index 407fcbf..ca5ac4c 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.cpp +++ b/akonadi2_cli/syntax_modules/core_syntax.cpp @@ -31,7 +31,10 @@ Syntax::List syntax() { Syntax::List syntax; syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - syntax << Syntax(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + + Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + help.completer = &CoreSyntax::showHelpCompleter; + syntax << help; Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); @@ -92,6 +95,20 @@ bool showHelp(const QStringList &commands, State &state) return true; } +QStringList showHelpCompleter(const QStringList &commands, const QString &fragment) +{ + QStringList items; + + for (auto syntax: SyntaxTree::self()->syntax()) { + if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { + items << syntax.keyword; + } + } + + qSort(items); + return items; +} + bool setDebugLevel(const QStringList &commands, State &state) { if (commands.count() != 1) { diff --git a/akonadi2_cli/syntax_modules/core_syntax.h b/akonadi2_cli/syntax_modules/core_syntax.h index 521cb8e..89187e5 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.h +++ b/akonadi2_cli/syntax_modules/core_syntax.h @@ -26,6 +26,7 @@ namespace CoreSyntax Syntax::List syntax(); bool exit(const QStringList &commands, State &state); bool showHelp(const QStringList &commands, State &state); + QStringList showHelpCompleter(const QStringList &commands, const QString &fragment); bool setDebugLevel(const QStringList &commands, State &state); bool printDebugLevel(const QStringList &commands, State &state); } -- cgit v1.2.3 From d0cd6658f72f2e509764d49fefbed7dd7fa9f48d Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 20:54:44 +0100 Subject: don't include help in the autocomplete list; it's a bit banal --- akonadi2_cli/syntax_modules/core_syntax.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp index ca5ac4c..393a0a5 100644 --- a/akonadi2_cli/syntax_modules/core_syntax.cpp +++ b/akonadi2_cli/syntax_modules/core_syntax.cpp @@ -100,7 +100,8 @@ QStringList showHelpCompleter(const QStringList &commands, const QString &fragme QStringList items; for (auto syntax: SyntaxTree::self()->syntax()) { - if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { + if (syntax.keyword != QObject::tr("help") && + (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) { items << syntax.keyword; } } -- cgit v1.2.3 From a89e9766adac31f700ef47f982583baaf5ce9309 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:08:04 +0100 Subject: shush --- akonadi2_cli/state.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp index 257da6b..84bce13 100644 --- a/akonadi2_cli/state.cpp +++ b/akonadi2_cli/state.cpp @@ -77,7 +77,7 @@ unsigned int State::debugLevel() const int State::commandStarted() const { if (!d->eventLoop.isRunning()) { - qDebug() << "RUNNING THE EVENT LOOP!"; + //qDebug() << "RUNNING THE EVENT LOOP!"; return d->eventLoop.exec(); } -- cgit v1.2.3 From a638c410ad83f8d8fe236c1500beaf63d69cbac6 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:10:03 +0100 Subject: add count command --- akonadi2_cli/CMakeLists.txt | 3 +- akonadi2_cli/syntax_modules/akonadi_count.cpp | 79 +++++++++++++++++++++++++++ akonadi2_cli/syntax_modules/akonadi_count.h | 29 ++++++++++ akonadi2_cli/syntaxtree.cpp | 2 + 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 akonadi2_cli/syntax_modules/akonadi_count.cpp create mode 100644 akonadi2_cli/syntax_modules/akonadi_count.h diff --git a/akonadi2_cli/CMakeLists.txt b/akonadi2_cli/CMakeLists.txt index 669ad52..9d0e7a5 100644 --- a/akonadi2_cli/CMakeLists.txt +++ b/akonadi2_cli/CMakeLists.txt @@ -1,4 +1,4 @@ -project(akonadi2_cli) +project(akonadish) find_package(Readline REQUIRED) @@ -8,6 +8,7 @@ set(akonadi2_cli_SRCS syntaxtree.cpp syntax_modules/core_syntax.cpp syntax_modules/akonadi_list.cpp + syntax_modules/akonadi_count.cpp akonadish_utils.cpp repl/repl.cpp repl/replStates.cpp diff --git a/akonadi2_cli/syntax_modules/akonadi_count.cpp b/akonadi2_cli/syntax_modules/akonadi_count.cpp new file mode 100644 index 0000000..40ad693 --- /dev/null +++ b/akonadi2_cli/syntax_modules/akonadi_count.cpp @@ -0,0 +1,79 @@ +/* + * 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 "akonadi_count.h" + +#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 "akonadish_utils.h" + +namespace AkonadiCount +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &AkonadiCount::count, Syntax::EventDriven); + + return syntax; +} + +bool count(const QStringList &args, State &state) +{ + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Akonadi2::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + + auto model = AkonadishUtils::loadModel(type, query); + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { + state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); + state.commandFinished(); + } + }); + + return true; +} + +} diff --git a/akonadi2_cli/syntax_modules/akonadi_count.h b/akonadi2_cli/syntax_modules/akonadi_count.h new file mode 100644 index 0000000..c592c4c --- /dev/null +++ b/akonadi2_cli/syntax_modules/akonadi_count.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace AkonadiCount +{ + Syntax::List syntax(); + bool count(const QStringList &commands, State &state); +} + diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp index cebfc4b..0cd3e3f 100644 --- a/akonadi2_cli/syntaxtree.cpp +++ b/akonadi2_cli/syntaxtree.cpp @@ -26,6 +26,7 @@ // almost certainly overkill, but this is not the way either #include "syntax_modules/core_syntax.h" #include "syntax_modules/akonadi_list.h" +#include "syntax_modules/akonadi_count.h" SyntaxTree *SyntaxTree::s_module = 0; @@ -46,6 +47,7 @@ SyntaxTree::SyntaxTree() QVector > syntaxSyntaxTrees; syntaxSyntaxTrees << &CoreSyntax::syntax << &AkonadiList::syntax + << &AkonadiCount::syntax ; for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { m_syntax += syntaxSyntaxTree(); -- cgit v1.2.3 From 3e701d9387cf4ea5107893ef9d1018900aa73d74 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:19:15 +0100 Subject: piping now works --- akonadi2_cli/main.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp index 91936c3..695fb82 100644 --- a/akonadi2_cli/main.cpp +++ b/akonadi2_cli/main.cpp @@ -21,6 +21,7 @@ #include #include +#include #include "syntaxtree.h" // #include "jsonlistener.h" @@ -60,9 +61,20 @@ int main(int argc, char *argv[]) } return app.exec(); - } + } else if (!interactive) { + QTextStream inputStream(stdin); + while (true) { + const QString input = inputStream.readLine(); + if (input.isEmpty()) { + ::exit(0); + } - QStringList commands = app.arguments(); - commands.removeFirst(); - return SyntaxTree::self()->run(commands); + const QStringList commands = SyntaxTree::tokenize(input); + SyntaxTree::self()->run(commands); + } + } else { + QStringList commands = app.arguments(); + commands.removeFirst(); + return SyntaxTree::self()->run(commands); + } } -- cgit v1.2.3 From 8b07718cb47dca6240a70e9aea57b88121cc956b Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:25:09 +0100 Subject: akonadi2_cli -> akonadish --- CMakeLists.txt | 2 +- akonadi2_cli/CMakeLists.txt | 22 ---- akonadi2_cli/akonadish_utils.cpp | 74 ----------- akonadi2_cli/akonadish_utils.h | 81 ------------ akonadi2_cli/main.cpp | 80 ------------ akonadi2_cli/repl/repl.cpp | 78 ------------ akonadi2_cli/repl/repl.h | 34 ----- akonadi2_cli/repl/replStates.cpp | 171 -------------------------- akonadi2_cli/repl/replStates.h | 87 ------------- akonadi2_cli/state.cpp | 91 -------------- akonadi2_cli/state.h | 43 ------- akonadi2_cli/syntax_modules/akonadi_count.cpp | 79 ------------ akonadi2_cli/syntax_modules/akonadi_count.h | 29 ----- akonadi2_cli/syntax_modules/akonadi_list.cpp | 112 ----------------- akonadi2_cli/syntax_modules/akonadi_list.h | 29 ----- akonadi2_cli/syntax_modules/core_syntax.cpp | 139 --------------------- akonadi2_cli/syntax_modules/core_syntax.h | 33 ----- akonadi2_cli/syntaxtree.cpp | 167 ------------------------- akonadi2_cli/syntaxtree.h | 75 ----------- akonadish/CMakeLists.txt | 22 ++++ akonadish/akonadish_utils.cpp | 74 +++++++++++ akonadish/akonadish_utils.h | 81 ++++++++++++ akonadish/main.cpp | 80 ++++++++++++ akonadish/repl/repl.cpp | 78 ++++++++++++ akonadish/repl/repl.h | 34 +++++ akonadish/repl/replStates.cpp | 171 ++++++++++++++++++++++++++ akonadish/repl/replStates.h | 87 +++++++++++++ akonadish/state.cpp | 91 ++++++++++++++ akonadish/state.h | 43 +++++++ akonadish/syntax_modules/akonadi_count.cpp | 79 ++++++++++++ akonadish/syntax_modules/akonadi_count.h | 29 +++++ akonadish/syntax_modules/akonadi_list.cpp | 112 +++++++++++++++++ akonadish/syntax_modules/akonadi_list.h | 29 +++++ akonadish/syntax_modules/core_syntax.cpp | 139 +++++++++++++++++++++ akonadish/syntax_modules/core_syntax.h | 33 +++++ akonadish/syntaxtree.cpp | 167 +++++++++++++++++++++++++ akonadish/syntaxtree.h | 75 +++++++++++ 37 files changed, 1425 insertions(+), 1425 deletions(-) delete mode 100644 akonadi2_cli/CMakeLists.txt delete mode 100644 akonadi2_cli/akonadish_utils.cpp delete mode 100644 akonadi2_cli/akonadish_utils.h delete mode 100644 akonadi2_cli/main.cpp delete mode 100644 akonadi2_cli/repl/repl.cpp delete mode 100644 akonadi2_cli/repl/repl.h delete mode 100644 akonadi2_cli/repl/replStates.cpp delete mode 100644 akonadi2_cli/repl/replStates.h delete mode 100644 akonadi2_cli/state.cpp delete mode 100644 akonadi2_cli/state.h delete mode 100644 akonadi2_cli/syntax_modules/akonadi_count.cpp delete mode 100644 akonadi2_cli/syntax_modules/akonadi_count.h delete mode 100644 akonadi2_cli/syntax_modules/akonadi_list.cpp delete mode 100644 akonadi2_cli/syntax_modules/akonadi_list.h delete mode 100644 akonadi2_cli/syntax_modules/core_syntax.cpp delete mode 100644 akonadi2_cli/syntax_modules/core_syntax.h delete mode 100644 akonadi2_cli/syntaxtree.cpp delete mode 100644 akonadi2_cli/syntaxtree.h create mode 100644 akonadish/CMakeLists.txt create mode 100644 akonadish/akonadish_utils.cpp create mode 100644 akonadish/akonadish_utils.h create mode 100644 akonadish/main.cpp create mode 100644 akonadish/repl/repl.cpp create mode 100644 akonadish/repl/repl.h create mode 100644 akonadish/repl/replStates.cpp create mode 100644 akonadish/repl/replStates.h create mode 100644 akonadish/state.cpp create mode 100644 akonadish/state.h create mode 100644 akonadish/syntax_modules/akonadi_count.cpp create mode 100644 akonadish/syntax_modules/akonadi_count.h create mode 100644 akonadish/syntax_modules/akonadi_list.cpp create mode 100644 akonadish/syntax_modules/akonadi_list.h create mode 100644 akonadish/syntax_modules/core_syntax.cpp create mode 100644 akonadish/syntax_modules/core_syntax.h create mode 100644 akonadish/syntaxtree.cpp create mode 100644 akonadish/syntaxtree.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d92f830..a827a10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,6 @@ add_subdirectory(examples) add_subdirectory(tests) # cli -add_subdirectory(akonadi2_cli) +add_subdirectory(akonadish) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/akonadi2_cli/CMakeLists.txt b/akonadi2_cli/CMakeLists.txt deleted file mode 100644 index 9d0e7a5..0000000 --- a/akonadi2_cli/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -project(akonadish) - -find_package(Readline REQUIRED) - - -set(akonadi2_cli_SRCS - main.cpp - syntaxtree.cpp - syntax_modules/core_syntax.cpp - syntax_modules/akonadi_list.cpp - syntax_modules/akonadi_count.cpp - akonadish_utils.cpp - repl/repl.cpp - repl/replStates.cpp - state.cpp) - -include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) - -add_executable(${PROJECT_NAME} ${akonadi2_cli_SRCS}) -target_link_libraries(${PROJECT_NAME} Qt5::Core ${Readline_LIBRARY} akonadi2common) -install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) - diff --git a/akonadi2_cli/akonadish_utils.cpp b/akonadi2_cli/akonadish_utils.cpp deleted file mode 100644 index bfb72ca..0000000 --- a/akonadi2_cli/akonadish_utils.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2015 Aaron Seigo - * Copyright (C) 2015 Christian Mollekopf - * - * 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 "akonadish_utils.h" - -#include "common/clientapi.h" - -namespace AkonadishUtils -{ - -bool isValidStoreType(const QString &type) -{ - static const QSet types = QSet() << "folder" << "mail" << "event" << "resource"; - return types.contains(type); -} - -StoreBase &getStore(const QString &type) -{ - if (type == "folder") { - static Store store; - return store; - } else if (type == "mail") { - static Store store; - return store; - } else if (type == "event") { - static Store store; - return store; - } else if (type == "resource") { - static Store store; - return store; - } - - //TODO: reinstate the warning+assert - //Q_ASSERT(false); - //qWarning() << "Trying to get a store that doesn't exist, falling back to event"; - static Store store; - return store; -} - -QSharedPointer loadModel(const QString &type, Akonadi2::Query query) -{ - if (type == "folder") { - query.requestedProperties << "name" << "parent"; - } else if (type == "mail") { - query.requestedProperties << "subject" << "folder" << "date"; - } else if (type == "event") { - query.requestedProperties << "summary"; - } else if (type == "resource") { - query.requestedProperties << "type"; - } - auto model = getStore(type).loadModel(query); - Q_ASSERT(model); - return model; -} - -} - diff --git a/akonadi2_cli/akonadish_utils.h b/akonadi2_cli/akonadish_utils.h deleted file mode 100644 index c15162f..0000000 --- a/akonadi2_cli/akonadish_utils.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2015 Aaron Seigo - * Copyright (C) 2015 Christian Mollekopf - * - * 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. - */ - -#pragma once - -#include -#include - -#include "common/query.h" -#include "common/clientapi.h" - -namespace AkonadishUtils -{ - -class StoreBase; - -bool isValidStoreType(const QString &type); -StoreBase &getStore(const QString &type); -QSharedPointer loadModel(const QString &type, Akonadi2::Query query); - -/** - * A small abstraction layer to use the akonadi store with the type available as string. - */ -class StoreBase { -public: - virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0; - virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0; - virtual KAsync::Job create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; - virtual KAsync::Job modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; - virtual KAsync::Job remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; - virtual QSharedPointer loadModel(const Akonadi2::Query &query) = 0; -}; - -template -class Store : public StoreBase { -public: - Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE { - return T::Ptr::create(); - } - - Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE { - return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer::create()); - } - - KAsync::Job create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { - return Akonadi2::Store::create(*static_cast(&type)); - } - - KAsync::Job modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { - return Akonadi2::Store::modify(*static_cast(&type)); - } - - KAsync::Job remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { - return Akonadi2::Store::remove(*static_cast(&type)); - } - - QSharedPointer loadModel(const Akonadi2::Query &query) Q_DECL_OVERRIDE { - return Akonadi2::Store::loadModel(query); - } -}; - - -} - diff --git a/akonadi2_cli/main.cpp b/akonadi2_cli/main.cpp deleted file mode 100644 index 695fb82..0000000 --- a/akonadi2_cli/main.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 -#include - -#include "syntaxtree.h" -// #include "jsonlistener.h" -#include "repl/repl.h" - -/* - * modes of operation: - * - * 1. called with no commands: start the REPL and listen for JSON on stin - * 2. called with -: listen for JSON on stdin - * 3. called with commands: try to match to syntx - */ - -int main(int argc, char *argv[]) -{ - const bool interactive = isatty(fileno(stdin)); - const bool startRepl = (argc == 1) && interactive; - //TODO: make a json command parse cause that would be awesomesauce - const bool startJsonListener = !startRepl && - (argc == 2 && qstrcmp(argv[1], "-") == 0); - //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener; - - QCoreApplication app(argc, argv); - app.setApplicationName(argv[0]); - - if (startRepl || startJsonListener) { - if (startRepl) { - Repl *repl = new Repl; - QObject::connect(repl, &QStateMachine::finished, - repl, &QObject::deleteLater); - QObject::connect(repl, &QStateMachine::finished, - &app, &QCoreApplication::quit); - } - - if (startJsonListener) { -// JsonListener listener(syntax); - } - - return app.exec(); - } else if (!interactive) { - QTextStream inputStream(stdin); - while (true) { - const QString input = inputStream.readLine(); - if (input.isEmpty()) { - ::exit(0); - } - - const QStringList commands = SyntaxTree::tokenize(input); - SyntaxTree::self()->run(commands); - } - } else { - QStringList commands = app.arguments(); - commands.removeFirst(); - return SyntaxTree::self()->run(commands); - } -} diff --git a/akonadi2_cli/repl/repl.cpp b/akonadi2_cli/repl/repl.cpp deleted file mode 100644 index 395661e..0000000 --- a/akonadi2_cli/repl/repl.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 "repl.h" - -#include - -#include -#include -#include -#include - -#include "replStates.h" - -Repl::Repl(QObject *parent) - : QStateMachine(parent) -{ - // readline history setup - using_history(); - read_history(commandHistoryPath().toLocal8Bit()); - - // create all states - ReadState *read = new ReadState(this); - UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this); - EvalState *eval = new EvalState(this); - PrintState *print = new PrintState(this); - QFinalState *final = new QFinalState(this); - - // connect the transitions - read->addTransition(read, SIGNAL(command(QString)), eval); - read->addTransition(read, SIGNAL(exitRequested()), final); - - unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval); - unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final); - - eval->addTransition(eval, SIGNAL(completed()), read); - eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead); - eval->addTransition(eval, SIGNAL(output(QString)), print); - - print->addTransition(print, SIGNAL(completed()), eval); - - setInitialState(read); - start(); -} - -Repl::~Repl() -{ - // readline history writing - write_history(commandHistoryPath().toLocal8Bit()); -} - -QString Repl::commandHistoryPath() -{ - const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); - if (!QFile::exists(path)) { - QDir dir; - dir.mkpath(path); - } - return path + "/repl_history"; -} - -#include "moc_repl.cpp" diff --git a/akonadi2_cli/repl/repl.h b/akonadi2_cli/repl/repl.h deleted file mode 100644 index b76c66b..0000000 --- a/akonadi2_cli/repl/repl.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include - -class Repl : public QStateMachine -{ - Q_OBJECT - -public: - Repl(QObject *parent = 0); - ~Repl(); - -private: - static QString commandHistoryPath(); -}; diff --git a/akonadi2_cli/repl/replStates.cpp b/akonadi2_cli/repl/replStates.cpp deleted file mode 100644 index 62888d0..0000000 --- a/akonadi2_cli/repl/replStates.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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 "replStates.h" - -#include -#include - -#include -#include - -#include -#include -#include - -#include "syntaxtree.h" - -static char *akonadi2_cli_next_tab_complete_match(const char *text, int state); -static char ** akonadi2_cli_tab_completion(const char *text, int start, int end); - -ReadState::ReadState(QState *parent) - : QState(parent) -{ - rl_completion_entry_function = akonadi2_cli_next_tab_complete_match; - rl_attempted_completion_function = akonadi2_cli_tab_completion; -} - -void ReadState::onEntry(QEvent *event) -{ - Q_UNUSED(event) - char *line = readline(prompt()); - - if (!line) { - std::cout << std::endl; - emit exitRequested(); - return; - } - - // we have actual data, so let's wait for a full line of text - QByteArray input(line); - const QString text = QString(line).simplified(); - //qDebug() << "Line is ... " << text; - - if (text.length() > 0) { - add_history(line); - } - - free(line); - emit command(text); -} - -const char *ReadState::prompt() const -{ - return "> "; -} - -UnfinishedReadState::UnfinishedReadState(QState *parent) - : ReadState(parent) -{ -} - -const char *UnfinishedReadState::prompt() const -{ - return " "; -} - -EvalState::EvalState(QState *parent) - : QState(parent) -{ -} - -void EvalState::onEntry(QEvent *event) -{ - QStateMachine::SignalEvent *e = dynamic_cast(event); - - const QString command = e ? e->arguments()[0].toString() : QString(); - - if (command.isEmpty()) { - complete(); - return; - } - - if (command.right(1) == "\\") { - m_partial += " " + command.left(command.size() - 1); - continueInput(); - } else { - m_partial += " " + command; - complete(); - } -} - -void EvalState::complete() -{ - m_partial = m_partial.simplified(); - - if (!m_partial.isEmpty()) { - //emit output("Processing ... " + command); - const QStringList commands = SyntaxTree::tokenize(m_partial); - SyntaxTree::self()->run(commands); - m_partial.clear(); - } - - emit completed(); -} - -PrintState::PrintState(QState *parent) - : QState(parent) -{ -} - -void PrintState::onEntry(QEvent *event) -{ - QStateMachine::SignalEvent *e = dynamic_cast(event); - - if (e && !e->arguments().isEmpty()) { - const QString command = e->arguments()[0].toString(); - QTextStream stream(stdout); - stream << command << "\n"; - } - - emit completed(); -} - -static QStringList tab_completion_full_state; -static bool tab_completion_at_root = false; - -static char **akonadi2_cli_tab_completion(const char *text, int start, int end) -{ - tab_completion_at_root = start == 0; - tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts); - return NULL; -} - -static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) -{ - const QString fragment(text); - Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, fragment); - //for (auto syntax: nearest) { qDebug() << "Nearest: " << syntax.keyword; } - - if (nearest.isEmpty()) { - SyntaxTree::Command command = SyntaxTree::self()->match(tab_completion_full_state); - if (command.first && command.first->completer) { - QStringList commandCompletions = command.first->completer(tab_completion_full_state, fragment); - if (commandCompletions.size() > state) { - return qstrdup(commandCompletions[state].toUtf8()); - } - } - } else if (nearest.size() > state) { - return qstrdup(nearest[state].keyword.toUtf8()); - } - - return rl_filename_completion_function(text, state); -} - -#include "moc_replStates.cpp" diff --git a/akonadi2_cli/repl/replStates.h b/akonadi2_cli/repl/replStates.h deleted file mode 100644 index a0d3f90..0000000 --- a/akonadi2_cli/repl/replStates.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include - -class QSocketNotifier; - -class ReadState : public QState -{ - Q_OBJECT - -public: - ReadState(QState *parent = 0); - -Q_SIGNALS: - void command(const QString &command); - void exitRequested(); - -protected: - void onEntry(QEvent *event); - virtual const char *prompt() const; -}; - -class UnfinishedReadState : public ReadState -{ - Q_OBJECT - -public: - UnfinishedReadState(QState *parent = 0); - -protected: - const char *prompt() const; -}; - -class EvalState : public QState -{ - Q_OBJECT - -public: - EvalState(QState *parent = 0); - -Q_SIGNALS: - void completed(); - void continueInput(); - void output(const QString &output); - -protected: - void onEntry(QEvent *event); - -private: - void complete(); - - QString m_partial; -}; - -class PrintState : public QState -{ - Q_OBJECT - -public: - PrintState(QState *parent = 0); - -Q_SIGNALS: - void completed(); - -protected: - void onEntry(QEvent *event); -}; - diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp deleted file mode 100644 index 84bce13..0000000 --- a/akonadi2_cli/state.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 "state.h" - -#include -#include -#include - -class State::Private -{ -public: - Private() - : outStream(stdout) - { - } - - int debugLevel = 0; - QEventLoop eventLoop; - QTextStream outStream; -}; - -State::State() - : d(new Private) -{ -} - -void State::print(const QString &message, unsigned int indentationLevel) const -{ - for (unsigned int i = 0; i < indentationLevel; ++i) { - d->outStream << "\t"; - } - - d->outStream << message; -} - -void State::printLine(const QString &message, unsigned int indentationLevel) const -{ - print(message, indentationLevel); - d->outStream << "\n"; - d->outStream.flush(); -} - -void State::printError(const QString &errorMessage, const QString &errorCode) const -{ - printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); -} - -void State::setDebugLevel(unsigned int level) -{ - if (level < 7) { - d->debugLevel = level; - } -} - -unsigned int State::debugLevel() const -{ - return d->debugLevel; -} - -int State::commandStarted() const -{ - if (!d->eventLoop.isRunning()) { - //qDebug() << "RUNNING THE EVENT LOOP!"; - return d->eventLoop.exec(); - } - - return 0; -} - -void State::commandFinished(int returnCode) const -{ - d->eventLoop.exit(returnCode); -} - diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h deleted file mode 100644 index eb07f56..0000000 --- a/akonadi2_cli/state.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include - -class State -{ -public: - State(); - - void print(const QString &message, unsigned int indentationLevel = 0) const; - void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const; - void printError(const QString &errorMessage, const QString &errorCode = QString()) const; - - void setDebugLevel(unsigned int level); - unsigned int debugLevel() const; - - int commandStarted() const; - void commandFinished(int returnCode = 0) const; - -private: - class Private; - Private * const d; -}; - diff --git a/akonadi2_cli/syntax_modules/akonadi_count.cpp b/akonadi2_cli/syntax_modules/akonadi_count.cpp deleted file mode 100644 index 40ad693..0000000 --- a/akonadi2_cli/syntax_modules/akonadi_count.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 "akonadi_count.h" - -#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 "akonadish_utils.h" - -namespace AkonadiCount -{ - -Syntax::List syntax() -{ - Syntax::List syntax; - syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &AkonadiCount::count, Syntax::EventDriven); - - return syntax; -} - -bool count(const QStringList &args, State &state) -{ - auto resources = args; - auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); - - if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { - state.printError(QObject::tr("Unknown type: %1").arg(type)); - return false; - } - - Akonadi2::Query query; - for (const auto &res : resources) { - query.resources << res.toLatin1(); - } - query.syncOnDemand = false; - query.processAll = false; - query.liveQuery = false; - - auto model = AkonadishUtils::loadModel(type, query); - QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { - if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { - state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); - state.commandFinished(); - } - }); - - return true; -} - -} diff --git a/akonadi2_cli/syntax_modules/akonadi_count.h b/akonadi2_cli/syntax_modules/akonadi_count.h deleted file mode 100644 index c592c4c..0000000 --- a/akonadi2_cli/syntax_modules/akonadi_count.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace AkonadiCount -{ - Syntax::List syntax(); - bool count(const QStringList &commands, State &state); -} - diff --git a/akonadi2_cli/syntax_modules/akonadi_list.cpp b/akonadi2_cli/syntax_modules/akonadi_list.cpp deleted file mode 100644 index 6abc853..0000000 --- a/akonadi2_cli/syntax_modules/akonadi_list.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 "akonadi_list.h" - -#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 "akonadish_utils.h" - -namespace AkonadiList -{ - -Syntax::List syntax() -{ - Syntax::List syntax; - syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven); - - return syntax; -} - -bool list(const QStringList &args, State &state) -{ - auto resources = args; - auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); - - if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { - state.printError(QObject::tr("Unknown type: %1").arg(type)); - return false; - } - - Akonadi2::Query query; - for (const auto &res : resources) { - query.resources << res.toLatin1(); - } - query.syncOnDemand = false; - query.processAll = false; - query.liveQuery = false; - - QTime time; - time.start(); - auto model = AkonadishUtils::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("Column") + " "); - 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++) { - state.print(" " + QObject::tr("Row %1").arg(QString::number(model->rowCount())).rightJustified(4, ' ') + ": "); - auto object = model->data(model->index(i, 0, index), Akonadi2::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(Akonadi2::Store::ChildrenFetchedRole)) { - state.commandFinished(); - } - }); - - if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) { - return true; - } - - return false; -} - -} diff --git a/akonadi2_cli/syntax_modules/akonadi_list.h b/akonadi2_cli/syntax_modules/akonadi_list.h deleted file mode 100644 index 61effc5..0000000 --- a/akonadi2_cli/syntax_modules/akonadi_list.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace AkonadiList -{ - Syntax::List syntax(); - bool list(const QStringList &commands, State &state); -} - diff --git a/akonadi2_cli/syntax_modules/core_syntax.cpp b/akonadi2_cli/syntax_modules/core_syntax.cpp deleted file mode 100644 index 393a0a5..0000000 --- a/akonadi2_cli/syntax_modules/core_syntax.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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 "core_syntax.h" - -#include -#include // tr() -#include -#include - -namespace CoreSyntax -{ - -Syntax::List syntax() -{ - Syntax::List syntax; - syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - - Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); - help.completer = &CoreSyntax::showHelpCompleter; - syntax << help; - - Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); - set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); - syntax << set; - - Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); - syntax << get; - - return syntax; -} - -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 Akonadi2 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) -{ - 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 &commands, State &state) -{ - state.printLine(QString::number(state.debugLevel())); - return true; -} - -} // namespace CoreSyntax - diff --git a/akonadi2_cli/syntax_modules/core_syntax.h b/akonadi2_cli/syntax_modules/core_syntax.h deleted file mode 100644 index 89187e5..0000000 --- a/akonadi2_cli/syntax_modules/core_syntax.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace CoreSyntax -{ - Syntax::List syntax(); - bool exit(const QStringList &commands, State &state); - bool showHelp(const QStringList &commands, State &state); - QStringList showHelpCompleter(const QStringList &commands, const QString &fragment); - bool setDebugLevel(const QStringList &commands, State &state); - bool printDebugLevel(const QStringList &commands, State &state); -} - diff --git a/akonadi2_cli/syntaxtree.cpp b/akonadi2_cli/syntaxtree.cpp deleted file mode 100644 index 0cd3e3f..0000000 --- a/akonadi2_cli/syntaxtree.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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 "syntaxtree.h" - -#include -#include - -// TODO: needs a proper registry; making "core" modules plugins is -// almost certainly overkill, but this is not the way either -#include "syntax_modules/core_syntax.h" -#include "syntax_modules/akonadi_list.h" -#include "syntax_modules/akonadi_count.h" - -SyntaxTree *SyntaxTree::s_module = 0; - -Syntax::Syntax() -{ -} - -Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) - : keyword(k), - help(helpText), - interactivity(inter), - lambda(l) -{ -} - -SyntaxTree::SyntaxTree() -{ - QVector > syntaxSyntaxTrees; - syntaxSyntaxTrees << &CoreSyntax::syntax - << &AkonadiList::syntax - << &AkonadiCount::syntax - ; - for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { - m_syntax += syntaxSyntaxTree(); - } -} - -SyntaxTree *SyntaxTree::self() -{ - if (!s_module) { - s_module = new SyntaxTree; - } - - return s_module; -} - -Syntax::List SyntaxTree::syntax() const -{ - return m_syntax; -} - -bool SyntaxTree::run(const QStringList &commands) -{ - Command command = match(commands); - if (command.first && command.first->lambda) { - bool rv = command.first->lambda(command.second, m_state); - if (rv && command.first->interactivity == Syntax::EventDriven) { - return m_state.commandStarted(); - } - - return rv; - } - - return false; -} - -SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const -{ - if (commandLine.isEmpty()) { - return Command(); - } - - QStringListIterator commandLineIt(commandLine); - - QVectorIterator syntaxIt(m_syntax); - const Syntax *lastFullSyntax = 0; - QStringList tailCommands; - while (commandLineIt.hasNext() && syntaxIt.hasNext()) { - const QString word = commandLineIt.next(); - while (syntaxIt.hasNext()) { - const Syntax &syntax = syntaxIt.next(); - if (word == syntax.keyword) { - lastFullSyntax = &syntax; - syntaxIt = syntax.children; - break; - } - } - } - - if (lastFullSyntax) { - while (commandLineIt.hasNext()) { - tailCommands << commandLineIt.next(); - } - - return std::make_pair(lastFullSyntax, tailCommands); - } - - return Command(); -} - -Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString &fragment) const -{ - Syntax::List matches; - - //qDebug() << "words are" << words; - if (words.isEmpty()) { - for (const Syntax &syntax: m_syntax) { - if (syntax.keyword.startsWith(fragment)) { - matches.push_back(syntax); - } - } - } else { - QStringListIterator wordIt(words); - QVectorIterator syntaxIt(m_syntax); - Syntax lastFullSyntax; - - while (wordIt.hasNext()) { - QString word = wordIt.next(); - while (syntaxIt.hasNext()) { - const Syntax &syntax = syntaxIt.next(); - if (word == syntax.keyword) { - lastFullSyntax = syntax; - syntaxIt = syntax.children; - } - } - } - - //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); - if (lastFullSyntax.keyword == words.last()) { - syntaxIt = lastFullSyntax.children; - while (syntaxIt.hasNext()) { - Syntax syntax = syntaxIt.next(); - if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { - matches.push_back(syntax); - } - } - } - } - - return matches; -} - -QStringList SyntaxTree::tokenize(const QString &text) -{ - //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] - return text.split(" "); -} - diff --git a/akonadi2_cli/syntaxtree.h b/akonadi2_cli/syntaxtree.h deleted file mode 100644 index 77f52af..0000000 --- a/akonadi2_cli/syntaxtree.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "state.h" - -#include -#include - -class Syntax -{ -public: - typedef QVector List; - - enum Interactivity { - NotInteractive = 0, - EventDriven - }; - - Syntax(); - Syntax(const QString &keyword, - const QString &helpText = QString(), - std::function lambda = std::function(), - Interactivity interactivity = NotInteractive); - - QString keyword; - QString help; - Interactivity interactivity; - std::function lambda; - std::function completer; - - QVector children; -}; - -class SyntaxTree -{ -public: - - typedef std::pair Command; - - static SyntaxTree *self(); - - Syntax::List syntax() const; - Command match(const QStringList &commands) const; - Syntax::List nearestSyntax(const QStringList &words, const QString &fragment) const; - - bool run(const QStringList &commands); - - static QStringList tokenize(const QString &text); - -private: - SyntaxTree(); - - Syntax::List m_syntax; - State m_state; - static SyntaxTree *s_module; -}; - diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt new file mode 100644 index 0000000..9d0e7a5 --- /dev/null +++ b/akonadish/CMakeLists.txt @@ -0,0 +1,22 @@ +project(akonadish) + +find_package(Readline REQUIRED) + + +set(akonadi2_cli_SRCS + main.cpp + syntaxtree.cpp + syntax_modules/core_syntax.cpp + syntax_modules/akonadi_list.cpp + syntax_modules/akonadi_count.cpp + akonadish_utils.cpp + repl/repl.cpp + repl/replStates.cpp + state.cpp) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) + +add_executable(${PROJECT_NAME} ${akonadi2_cli_SRCS}) +target_link_libraries(${PROJECT_NAME} Qt5::Core ${Readline_LIBRARY} akonadi2common) +install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) + diff --git a/akonadish/akonadish_utils.cpp b/akonadish/akonadish_utils.cpp new file mode 100644 index 0000000..bfb72ca --- /dev/null +++ b/akonadish/akonadish_utils.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2015 Aaron Seigo + * Copyright (C) 2015 Christian Mollekopf + * + * 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 "akonadish_utils.h" + +#include "common/clientapi.h" + +namespace AkonadishUtils +{ + +bool isValidStoreType(const QString &type) +{ + static const QSet types = QSet() << "folder" << "mail" << "event" << "resource"; + return types.contains(type); +} + +StoreBase &getStore(const QString &type) +{ + if (type == "folder") { + static Store store; + return store; + } else if (type == "mail") { + static Store store; + return store; + } else if (type == "event") { + static Store store; + return store; + } else if (type == "resource") { + static Store store; + return store; + } + + //TODO: reinstate the warning+assert + //Q_ASSERT(false); + //qWarning() << "Trying to get a store that doesn't exist, falling back to event"; + static Store store; + return store; +} + +QSharedPointer loadModel(const QString &type, Akonadi2::Query query) +{ + if (type == "folder") { + query.requestedProperties << "name" << "parent"; + } else if (type == "mail") { + query.requestedProperties << "subject" << "folder" << "date"; + } else if (type == "event") { + query.requestedProperties << "summary"; + } else if (type == "resource") { + query.requestedProperties << "type"; + } + auto model = getStore(type).loadModel(query); + Q_ASSERT(model); + return model; +} + +} + diff --git a/akonadish/akonadish_utils.h b/akonadish/akonadish_utils.h new file mode 100644 index 0000000..c15162f --- /dev/null +++ b/akonadish/akonadish_utils.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2015 Aaron Seigo + * Copyright (C) 2015 Christian Mollekopf + * + * 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. + */ + +#pragma once + +#include +#include + +#include "common/query.h" +#include "common/clientapi.h" + +namespace AkonadishUtils +{ + +class StoreBase; + +bool isValidStoreType(const QString &type); +StoreBase &getStore(const QString &type); +QSharedPointer loadModel(const QString &type, Akonadi2::Query query); + +/** + * A small abstraction layer to use the akonadi store with the type available as string. + */ +class StoreBase { +public: + virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0; + virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0; + virtual KAsync::Job create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; + virtual KAsync::Job modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; + virtual KAsync::Job remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; + virtual QSharedPointer loadModel(const Akonadi2::Query &query) = 0; +}; + +template +class Store : public StoreBase { +public: + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE { + return T::Ptr::create(); + } + + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE { + return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer::create()); + } + + KAsync::Job create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { + return Akonadi2::Store::create(*static_cast(&type)); + } + + KAsync::Job modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { + return Akonadi2::Store::modify(*static_cast(&type)); + } + + KAsync::Job remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { + return Akonadi2::Store::remove(*static_cast(&type)); + } + + QSharedPointer loadModel(const Akonadi2::Query &query) Q_DECL_OVERRIDE { + return Akonadi2::Store::loadModel(query); + } +}; + + +} + diff --git a/akonadish/main.cpp b/akonadish/main.cpp new file mode 100644 index 0000000..695fb82 --- /dev/null +++ b/akonadish/main.cpp @@ -0,0 +1,80 @@ +/* + * 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 +#include + +#include "syntaxtree.h" +// #include "jsonlistener.h" +#include "repl/repl.h" + +/* + * modes of operation: + * + * 1. called with no commands: start the REPL and listen for JSON on stin + * 2. called with -: listen for JSON on stdin + * 3. called with commands: try to match to syntx + */ + +int main(int argc, char *argv[]) +{ + const bool interactive = isatty(fileno(stdin)); + const bool startRepl = (argc == 1) && interactive; + //TODO: make a json command parse cause that would be awesomesauce + const bool startJsonListener = !startRepl && + (argc == 2 && qstrcmp(argv[1], "-") == 0); + //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener; + + QCoreApplication app(argc, argv); + app.setApplicationName(argv[0]); + + if (startRepl || startJsonListener) { + if (startRepl) { + Repl *repl = new Repl; + QObject::connect(repl, &QStateMachine::finished, + repl, &QObject::deleteLater); + QObject::connect(repl, &QStateMachine::finished, + &app, &QCoreApplication::quit); + } + + if (startJsonListener) { +// JsonListener listener(syntax); + } + + return app.exec(); + } else if (!interactive) { + QTextStream inputStream(stdin); + while (true) { + const QString input = inputStream.readLine(); + if (input.isEmpty()) { + ::exit(0); + } + + const QStringList commands = SyntaxTree::tokenize(input); + SyntaxTree::self()->run(commands); + } + } else { + QStringList commands = app.arguments(); + commands.removeFirst(); + return SyntaxTree::self()->run(commands); + } +} diff --git a/akonadish/repl/repl.cpp b/akonadish/repl/repl.cpp new file mode 100644 index 0000000..395661e --- /dev/null +++ b/akonadish/repl/repl.cpp @@ -0,0 +1,78 @@ +/* + * 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 "repl.h" + +#include + +#include +#include +#include +#include + +#include "replStates.h" + +Repl::Repl(QObject *parent) + : QStateMachine(parent) +{ + // readline history setup + using_history(); + read_history(commandHistoryPath().toLocal8Bit()); + + // create all states + ReadState *read = new ReadState(this); + UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this); + EvalState *eval = new EvalState(this); + PrintState *print = new PrintState(this); + QFinalState *final = new QFinalState(this); + + // connect the transitions + read->addTransition(read, SIGNAL(command(QString)), eval); + read->addTransition(read, SIGNAL(exitRequested()), final); + + unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval); + unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final); + + eval->addTransition(eval, SIGNAL(completed()), read); + eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead); + eval->addTransition(eval, SIGNAL(output(QString)), print); + + print->addTransition(print, SIGNAL(completed()), eval); + + setInitialState(read); + start(); +} + +Repl::~Repl() +{ + // readline history writing + write_history(commandHistoryPath().toLocal8Bit()); +} + +QString Repl::commandHistoryPath() +{ + const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); + if (!QFile::exists(path)) { + QDir dir; + dir.mkpath(path); + } + return path + "/repl_history"; +} + +#include "moc_repl.cpp" diff --git a/akonadish/repl/repl.h b/akonadish/repl/repl.h new file mode 100644 index 0000000..b76c66b --- /dev/null +++ b/akonadish/repl/repl.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#pragma once + +#include + +class Repl : public QStateMachine +{ + Q_OBJECT + +public: + Repl(QObject *parent = 0); + ~Repl(); + +private: + static QString commandHistoryPath(); +}; diff --git a/akonadish/repl/replStates.cpp b/akonadish/repl/replStates.cpp new file mode 100644 index 0000000..62888d0 --- /dev/null +++ b/akonadish/repl/replStates.cpp @@ -0,0 +1,171 @@ +/* + * 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 "replStates.h" + +#include +#include + +#include +#include + +#include +#include +#include + +#include "syntaxtree.h" + +static char *akonadi2_cli_next_tab_complete_match(const char *text, int state); +static char ** akonadi2_cli_tab_completion(const char *text, int start, int end); + +ReadState::ReadState(QState *parent) + : QState(parent) +{ + rl_completion_entry_function = akonadi2_cli_next_tab_complete_match; + rl_attempted_completion_function = akonadi2_cli_tab_completion; +} + +void ReadState::onEntry(QEvent *event) +{ + Q_UNUSED(event) + char *line = readline(prompt()); + + if (!line) { + std::cout << std::endl; + emit exitRequested(); + return; + } + + // we have actual data, so let's wait for a full line of text + QByteArray input(line); + const QString text = QString(line).simplified(); + //qDebug() << "Line is ... " << text; + + if (text.length() > 0) { + add_history(line); + } + + free(line); + emit command(text); +} + +const char *ReadState::prompt() const +{ + return "> "; +} + +UnfinishedReadState::UnfinishedReadState(QState *parent) + : ReadState(parent) +{ +} + +const char *UnfinishedReadState::prompt() const +{ + return " "; +} + +EvalState::EvalState(QState *parent) + : QState(parent) +{ +} + +void EvalState::onEntry(QEvent *event) +{ + QStateMachine::SignalEvent *e = dynamic_cast(event); + + const QString command = e ? e->arguments()[0].toString() : QString(); + + if (command.isEmpty()) { + complete(); + return; + } + + if (command.right(1) == "\\") { + m_partial += " " + command.left(command.size() - 1); + continueInput(); + } else { + m_partial += " " + command; + complete(); + } +} + +void EvalState::complete() +{ + m_partial = m_partial.simplified(); + + if (!m_partial.isEmpty()) { + //emit output("Processing ... " + command); + const QStringList commands = SyntaxTree::tokenize(m_partial); + SyntaxTree::self()->run(commands); + m_partial.clear(); + } + + emit completed(); +} + +PrintState::PrintState(QState *parent) + : QState(parent) +{ +} + +void PrintState::onEntry(QEvent *event) +{ + QStateMachine::SignalEvent *e = dynamic_cast(event); + + if (e && !e->arguments().isEmpty()) { + const QString command = e->arguments()[0].toString(); + QTextStream stream(stdout); + stream << command << "\n"; + } + + emit completed(); +} + +static QStringList tab_completion_full_state; +static bool tab_completion_at_root = false; + +static char **akonadi2_cli_tab_completion(const char *text, int start, int end) +{ + tab_completion_at_root = start == 0; + tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts); + return NULL; +} + +static char *akonadi2_cli_next_tab_complete_match(const char *text, int state) +{ + const QString fragment(text); + Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, fragment); + //for (auto syntax: nearest) { qDebug() << "Nearest: " << syntax.keyword; } + + if (nearest.isEmpty()) { + SyntaxTree::Command command = SyntaxTree::self()->match(tab_completion_full_state); + if (command.first && command.first->completer) { + QStringList commandCompletions = command.first->completer(tab_completion_full_state, fragment); + if (commandCompletions.size() > state) { + return qstrdup(commandCompletions[state].toUtf8()); + } + } + } else if (nearest.size() > state) { + return qstrdup(nearest[state].keyword.toUtf8()); + } + + return rl_filename_completion_function(text, state); +} + +#include "moc_replStates.cpp" diff --git a/akonadish/repl/replStates.h b/akonadish/repl/replStates.h new file mode 100644 index 0000000..a0d3f90 --- /dev/null +++ b/akonadish/repl/replStates.h @@ -0,0 +1,87 @@ +/* + * 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. + */ + +#pragma once + +#include + +class QSocketNotifier; + +class ReadState : public QState +{ + Q_OBJECT + +public: + ReadState(QState *parent = 0); + +Q_SIGNALS: + void command(const QString &command); + void exitRequested(); + +protected: + void onEntry(QEvent *event); + virtual const char *prompt() const; +}; + +class UnfinishedReadState : public ReadState +{ + Q_OBJECT + +public: + UnfinishedReadState(QState *parent = 0); + +protected: + const char *prompt() const; +}; + +class EvalState : public QState +{ + Q_OBJECT + +public: + EvalState(QState *parent = 0); + +Q_SIGNALS: + void completed(); + void continueInput(); + void output(const QString &output); + +protected: + void onEntry(QEvent *event); + +private: + void complete(); + + QString m_partial; +}; + +class PrintState : public QState +{ + Q_OBJECT + +public: + PrintState(QState *parent = 0); + +Q_SIGNALS: + void completed(); + +protected: + void onEntry(QEvent *event); +}; + diff --git a/akonadish/state.cpp b/akonadish/state.cpp new file mode 100644 index 0000000..84bce13 --- /dev/null +++ b/akonadish/state.cpp @@ -0,0 +1,91 @@ +/* + * 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 "state.h" + +#include +#include +#include + +class State::Private +{ +public: + Private() + : outStream(stdout) + { + } + + int debugLevel = 0; + QEventLoop eventLoop; + QTextStream outStream; +}; + +State::State() + : d(new Private) +{ +} + +void State::print(const QString &message, unsigned int indentationLevel) const +{ + for (unsigned int i = 0; i < indentationLevel; ++i) { + d->outStream << "\t"; + } + + d->outStream << message; +} + +void State::printLine(const QString &message, unsigned int indentationLevel) const +{ + print(message, indentationLevel); + d->outStream << "\n"; + d->outStream.flush(); +} + +void State::printError(const QString &errorMessage, const QString &errorCode) const +{ + printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); +} + +void State::setDebugLevel(unsigned int level) +{ + if (level < 7) { + d->debugLevel = level; + } +} + +unsigned int State::debugLevel() const +{ + return d->debugLevel; +} + +int State::commandStarted() const +{ + if (!d->eventLoop.isRunning()) { + //qDebug() << "RUNNING THE EVENT LOOP!"; + return d->eventLoop.exec(); + } + + return 0; +} + +void State::commandFinished(int returnCode) const +{ + d->eventLoop.exit(returnCode); +} + diff --git a/akonadish/state.h b/akonadish/state.h new file mode 100644 index 0000000..eb07f56 --- /dev/null +++ b/akonadish/state.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#pragma once + +#include + +class State +{ +public: + State(); + + void print(const QString &message, unsigned int indentationLevel = 0) const; + void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const; + void printError(const QString &errorMessage, const QString &errorCode = QString()) const; + + void setDebugLevel(unsigned int level); + unsigned int debugLevel() const; + + int commandStarted() const; + void commandFinished(int returnCode = 0) const; + +private: + class Private; + Private * const d; +}; + diff --git a/akonadish/syntax_modules/akonadi_count.cpp b/akonadish/syntax_modules/akonadi_count.cpp new file mode 100644 index 0000000..40ad693 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_count.cpp @@ -0,0 +1,79 @@ +/* + * 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 "akonadi_count.h" + +#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 "akonadish_utils.h" + +namespace AkonadiCount +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &AkonadiCount::count, Syntax::EventDriven); + + return syntax; +} + +bool count(const QStringList &args, State &state) +{ + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Akonadi2::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + + auto model = AkonadishUtils::loadModel(type, query); + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { + state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); + state.commandFinished(); + } + }); + + return true; +} + +} diff --git a/akonadish/syntax_modules/akonadi_count.h b/akonadish/syntax_modules/akonadi_count.h new file mode 100644 index 0000000..c592c4c --- /dev/null +++ b/akonadish/syntax_modules/akonadi_count.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace AkonadiCount +{ + Syntax::List syntax(); + bool count(const QStringList &commands, State &state); +} + diff --git a/akonadish/syntax_modules/akonadi_list.cpp b/akonadish/syntax_modules/akonadi_list.cpp new file mode 100644 index 0000000..6abc853 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_list.cpp @@ -0,0 +1,112 @@ +/* + * 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 "akonadi_list.h" + +#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 "akonadish_utils.h" + +namespace AkonadiList +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven); + + return syntax; +} + +bool list(const QStringList &args, State &state) +{ + auto resources = args; + auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); + + if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) { + state.printError(QObject::tr("Unknown type: %1").arg(type)); + return false; + } + + Akonadi2::Query query; + for (const auto &res : resources) { + query.resources << res.toLatin1(); + } + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + + QTime time; + time.start(); + auto model = AkonadishUtils::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("Column") + " "); + 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++) { + state.print(" " + QObject::tr("Row %1").arg(QString::number(model->rowCount())).rightJustified(4, ' ') + ": "); + auto object = model->data(model->index(i, 0, index), Akonadi2::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(Akonadi2::Store::ChildrenFetchedRole)) { + state.commandFinished(); + } + }); + + if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) { + return true; + } + + return false; +} + +} diff --git a/akonadish/syntax_modules/akonadi_list.h b/akonadish/syntax_modules/akonadi_list.h new file mode 100644 index 0000000..61effc5 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_list.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace AkonadiList +{ + Syntax::List syntax(); + bool list(const QStringList &commands, State &state); +} + diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp new file mode 100644 index 0000000..393a0a5 --- /dev/null +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -0,0 +1,139 @@ +/* + * 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 "core_syntax.h" + +#include +#include // tr() +#include +#include + +namespace CoreSyntax +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + + Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + help.completer = &CoreSyntax::showHelpCompleter; + syntax << help; + + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); + set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + syntax << set; + + Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); + get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + syntax << get; + + return syntax; +} + +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 Akonadi2 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) +{ + 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 &commands, State &state) +{ + state.printLine(QString::number(state.debugLevel())); + return true; +} + +} // namespace CoreSyntax + diff --git a/akonadish/syntax_modules/core_syntax.h b/akonadish/syntax_modules/core_syntax.h new file mode 100644 index 0000000..89187e5 --- /dev/null +++ b/akonadish/syntax_modules/core_syntax.h @@ -0,0 +1,33 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace CoreSyntax +{ + Syntax::List syntax(); + bool exit(const QStringList &commands, State &state); + bool showHelp(const QStringList &commands, State &state); + QStringList showHelpCompleter(const QStringList &commands, const QString &fragment); + bool setDebugLevel(const QStringList &commands, State &state); + bool printDebugLevel(const QStringList &commands, State &state); +} + diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp new file mode 100644 index 0000000..0cd3e3f --- /dev/null +++ b/akonadish/syntaxtree.cpp @@ -0,0 +1,167 @@ +/* + * 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 "syntaxtree.h" + +#include +#include + +// TODO: needs a proper registry; making "core" modules plugins is +// almost certainly overkill, but this is not the way either +#include "syntax_modules/core_syntax.h" +#include "syntax_modules/akonadi_list.h" +#include "syntax_modules/akonadi_count.h" + +SyntaxTree *SyntaxTree::s_module = 0; + +Syntax::Syntax() +{ +} + +Syntax::Syntax(const QString &k, const QString &helpText, std::function l, Interactivity inter) + : keyword(k), + help(helpText), + interactivity(inter), + lambda(l) +{ +} + +SyntaxTree::SyntaxTree() +{ + QVector > syntaxSyntaxTrees; + syntaxSyntaxTrees << &CoreSyntax::syntax + << &AkonadiList::syntax + << &AkonadiCount::syntax + ; + for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { + m_syntax += syntaxSyntaxTree(); + } +} + +SyntaxTree *SyntaxTree::self() +{ + if (!s_module) { + s_module = new SyntaxTree; + } + + return s_module; +} + +Syntax::List SyntaxTree::syntax() const +{ + return m_syntax; +} + +bool SyntaxTree::run(const QStringList &commands) +{ + Command command = match(commands); + if (command.first && command.first->lambda) { + bool rv = command.first->lambda(command.second, m_state); + if (rv && command.first->interactivity == Syntax::EventDriven) { + return m_state.commandStarted(); + } + + return rv; + } + + return false; +} + +SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const +{ + if (commandLine.isEmpty()) { + return Command(); + } + + QStringListIterator commandLineIt(commandLine); + + QVectorIterator syntaxIt(m_syntax); + const Syntax *lastFullSyntax = 0; + QStringList tailCommands; + while (commandLineIt.hasNext() && syntaxIt.hasNext()) { + const QString word = commandLineIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &syntax = syntaxIt.next(); + if (word == syntax.keyword) { + lastFullSyntax = &syntax; + syntaxIt = syntax.children; + break; + } + } + } + + if (lastFullSyntax) { + while (commandLineIt.hasNext()) { + tailCommands << commandLineIt.next(); + } + + return std::make_pair(lastFullSyntax, tailCommands); + } + + return Command(); +} + +Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString &fragment) const +{ + Syntax::List matches; + + //qDebug() << "words are" << words; + if (words.isEmpty()) { + for (const Syntax &syntax: m_syntax) { + if (syntax.keyword.startsWith(fragment)) { + matches.push_back(syntax); + } + } + } else { + QStringListIterator wordIt(words); + QVectorIterator syntaxIt(m_syntax); + Syntax lastFullSyntax; + + while (wordIt.hasNext()) { + QString word = wordIt.next(); + while (syntaxIt.hasNext()) { + const Syntax &syntax = syntaxIt.next(); + if (word == syntax.keyword) { + lastFullSyntax = syntax; + syntaxIt = syntax.children; + } + } + } + + //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); + if (lastFullSyntax.keyword == words.last()) { + syntaxIt = lastFullSyntax.children; + while (syntaxIt.hasNext()) { + Syntax syntax = syntaxIt.next(); + if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { + matches.push_back(syntax); + } + } + } + } + + return matches; +} + +QStringList SyntaxTree::tokenize(const QString &text) +{ + //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] + return text.split(" "); +} + diff --git a/akonadish/syntaxtree.h b/akonadish/syntaxtree.h new file mode 100644 index 0000000..77f52af --- /dev/null +++ b/akonadish/syntaxtree.h @@ -0,0 +1,75 @@ +/* + * 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. + */ + +#pragma once + +#include "state.h" + +#include +#include + +class Syntax +{ +public: + typedef QVector List; + + enum Interactivity { + NotInteractive = 0, + EventDriven + }; + + Syntax(); + Syntax(const QString &keyword, + const QString &helpText = QString(), + std::function lambda = std::function(), + Interactivity interactivity = NotInteractive); + + QString keyword; + QString help; + Interactivity interactivity; + std::function lambda; + std::function completer; + + QVector children; +}; + +class SyntaxTree +{ +public: + + typedef std::pair Command; + + static SyntaxTree *self(); + + Syntax::List syntax() const; + Command match(const QStringList &commands) const; + Syntax::List nearestSyntax(const QStringList &words, const QString &fragment) const; + + bool run(const QStringList &commands); + + static QStringList tokenize(const QString &text); + +private: + SyntaxTree(); + + Syntax::List m_syntax; + State m_state; + static SyntaxTree *s_module; +}; + -- cgit v1.2.3 From 0efcf4febf4d6d6cdb3218674999b6de1155a4d9 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:39:07 +0100 Subject: shush --- akonadish/state.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/akonadish/state.cpp b/akonadish/state.cpp index 84bce13..968c0ac 100644 --- a/akonadish/state.cpp +++ b/akonadish/state.cpp @@ -77,7 +77,6 @@ unsigned int State::debugLevel() const int State::commandStarted() const { if (!d->eventLoop.isRunning()) { - //qDebug() << "RUNNING THE EVENT LOOP!"; return d->eventLoop.exec(); } -- cgit v1.2.3 From 17ea319fa8cbdf36396a4a5a68853857ca6ac51f Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:39:14 +0100 Subject: sync command --- akonadish/CMakeLists.txt | 1 + akonadish/syntax_modules/akonadi_sync.cpp | 67 +++++++++++++++++++++++++++++++ akonadish/syntax_modules/akonadi_sync.h | 29 +++++++++++++ akonadish/syntaxtree.cpp | 2 + 4 files changed, 99 insertions(+) create mode 100644 akonadish/syntax_modules/akonadi_sync.cpp create mode 100644 akonadish/syntax_modules/akonadi_sync.h diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt index 9d0e7a5..39a059f 100644 --- a/akonadish/CMakeLists.txt +++ b/akonadish/CMakeLists.txt @@ -9,6 +9,7 @@ set(akonadi2_cli_SRCS syntax_modules/core_syntax.cpp syntax_modules/akonadi_list.cpp syntax_modules/akonadi_count.cpp + syntax_modules/akonadi_sync.cpp akonadish_utils.cpp repl/repl.cpp repl/replStates.cpp diff --git a/akonadish/syntax_modules/akonadi_sync.cpp b/akonadish/syntax_modules/akonadi_sync.cpp new file mode 100644 index 0000000..990fdf6 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_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 "akonadi_sync.h" + +#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 "akonadish_utils.h" + +namespace AkonadiSync +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("sync", QObject::tr("Syncronizes all resources that are listed; and empty list triggers a syncronizaton on all resources"), &AkonadiSync::sync, Syntax::EventDriven ); + + return syntax; +} + +bool sync(const QStringList &args, State &state) +{ + Akonadi2::Query query; + for (const auto &res : args) { + query.resources << res.toLatin1(); + } + query.syncOnDemand = true; + query.processAll = true; + + QTimer::singleShot(0, [query, state]() { + Akonadi2::Store::synchronize(query).then([state]() { + state.printLine("Synchronization complete!"); + state.commandFinished(); + }).exec(); + }); + + return true; +} + +} diff --git a/akonadish/syntax_modules/akonadi_sync.h b/akonadish/syntax_modules/akonadi_sync.h new file mode 100644 index 0000000..62f7424 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_sync.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#pragma once + +#include "syntaxtree.h" + +namespace AkonadiSync +{ + Syntax::List syntax(); + bool sync(const QStringList &commands, State &state); +} + diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index 0cd3e3f..2d7c127 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -27,6 +27,7 @@ #include "syntax_modules/core_syntax.h" #include "syntax_modules/akonadi_list.h" #include "syntax_modules/akonadi_count.h" +#include "syntax_modules/akonadi_sync.h" SyntaxTree *SyntaxTree::s_module = 0; @@ -48,6 +49,7 @@ SyntaxTree::SyntaxTree() syntaxSyntaxTrees << &CoreSyntax::syntax << &AkonadiList::syntax << &AkonadiCount::syntax + << &AkonadiSync::syntax ; for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { m_syntax += syntaxSyntaxTree(); -- cgit v1.2.3 From 35f0ddf67c629ce9efaa1ba893afcb2921a251a2 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 24 Dec 2015 10:22:07 +0100 Subject: REGISTER_SYNTAX for automagic adding of syntax a fun abuse of static initialization and std::function --- akonadish/syntax_modules/akonadi_count.cpp | 2 ++ akonadish/syntax_modules/akonadi_list.cpp | 2 ++ akonadish/syntax_modules/akonadi_sync.cpp | 2 ++ akonadish/syntax_modules/core_syntax.cpp | 2 ++ akonadish/syntaxtree.cpp | 15 ++++++--------- akonadish/syntaxtree.h | 2 ++ 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/akonadish/syntax_modules/akonadi_count.cpp b/akonadish/syntax_modules/akonadi_count.cpp index 40ad693..e54ac22 100644 --- a/akonadish/syntax_modules/akonadi_count.cpp +++ b/akonadish/syntax_modules/akonadi_count.cpp @@ -39,6 +39,8 @@ namespace AkonadiCount { +REGISTER_SYNTAX(AkonadiCount) + Syntax::List syntax() { Syntax::List syntax; diff --git a/akonadish/syntax_modules/akonadi_list.cpp b/akonadish/syntax_modules/akonadi_list.cpp index 6abc853..25ebbca 100644 --- a/akonadish/syntax_modules/akonadi_list.cpp +++ b/akonadish/syntax_modules/akonadi_list.cpp @@ -39,6 +39,8 @@ namespace AkonadiList { +REGISTER_SYNTAX(AkonadiList) + Syntax::List syntax() { Syntax::List syntax; diff --git a/akonadish/syntax_modules/akonadi_sync.cpp b/akonadish/syntax_modules/akonadi_sync.cpp index 990fdf6..e9388d2 100644 --- a/akonadish/syntax_modules/akonadi_sync.cpp +++ b/akonadish/syntax_modules/akonadi_sync.cpp @@ -37,6 +37,8 @@ namespace AkonadiSync { +REGISTER_SYNTAX(AkonadiSync) + Syntax::List syntax() { Syntax::List syntax; diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 393a0a5..8fb1448 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -27,6 +27,8 @@ namespace CoreSyntax { +REGISTER_SYNTAX(CoreSyntax) + Syntax::List syntax() { Syntax::List syntax; diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index 2d7c127..cd7348e 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -45,15 +45,12 @@ Syntax::Syntax(const QString &k, const QString &helpText, std::function > syntaxSyntaxTrees; - syntaxSyntaxTrees << &CoreSyntax::syntax - << &AkonadiList::syntax - << &AkonadiCount::syntax - << &AkonadiSync::syntax - ; - for (auto syntaxSyntaxTree: syntaxSyntaxTrees) { - m_syntax += syntaxSyntaxTree(); - } +} + +int SyntaxTree::registerSyntax(std::function f) +{ + m_syntax += f(); + return m_syntax.size(); } SyntaxTree *SyntaxTree::self() diff --git a/akonadish/syntaxtree.h b/akonadish/syntaxtree.h index 77f52af..6fdf2b9 100644 --- a/akonadish/syntaxtree.h +++ b/akonadish/syntaxtree.h @@ -57,6 +57,7 @@ public: static SyntaxTree *self(); + int registerSyntax(std::function f); Syntax::List syntax() const; Command match(const QStringList &commands) const; Syntax::List nearestSyntax(const QStringList &words, const QString &fragment) const; @@ -73,3 +74,4 @@ private: static SyntaxTree *s_module; }; +#define REGISTER_SYNTAX(name) static const int theTrickFor##name = SyntaxTree::self()->registerSyntax(&name::syntax); -- cgit v1.2.3 From f5d4b7dc12dbb3b38b70bcbe94298972208d64f5 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 24 Dec 2015 10:36:52 +0100 Subject: minor ws fix --- akonadish/syntaxtree.h | 1 - 1 file changed, 1 deletion(-) diff --git a/akonadish/syntaxtree.h b/akonadish/syntaxtree.h index 6fdf2b9..3421fc3 100644 --- a/akonadish/syntaxtree.h +++ b/akonadish/syntaxtree.h @@ -52,7 +52,6 @@ public: class SyntaxTree { public: - typedef std::pair Command; static SyntaxTree *self(); -- cgit v1.2.3 From b352f61f136f21854b3da5b76d49fe45cbb2d3fd Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 24 Dec 2015 10:36:56 +0100 Subject: if not being run interactively, then use the main app loop QEventLoop requires QCoreApplication is running; so when we don't have one running the whole app, just start/stop the core app on demand (from/for commands). --- akonadish/main.cpp | 1 + akonadish/state.cpp | 31 +++++++++++++++++++++++++++---- akonadish/state.h | 2 ++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/akonadish/main.cpp b/akonadish/main.cpp index 695fb82..bd85fb4 100644 --- a/akonadish/main.cpp +++ b/akonadish/main.cpp @@ -60,6 +60,7 @@ int main(int argc, char *argv[]) // JsonListener listener(syntax); } + State::setHasEventLoop(true); return app.exec(); } else if (!interactive) { QTextStream inputStream(stdin); diff --git a/akonadish/state.cpp b/akonadish/state.cpp index 968c0ac..c9f9ee3 100644 --- a/akonadish/state.cpp +++ b/akonadish/state.cpp @@ -19,10 +19,13 @@ #include "state.h" +#include #include #include #include +static bool s_hasEventLoop = false; + class State::Private { public: @@ -31,8 +34,17 @@ public: { } + QEventLoop *eventLoop() + { + if (!event) { + event = new QEventLoop; + } + + return event; + } + int debugLevel = 0; - QEventLoop eventLoop; + QEventLoop *event = 0; QTextStream outStream; }; @@ -76,8 +88,10 @@ unsigned int State::debugLevel() const int State::commandStarted() const { - if (!d->eventLoop.isRunning()) { - return d->eventLoop.exec(); + if (!s_hasEventLoop) { + return QCoreApplication::exec(); + } else if (!d->eventLoop()->isRunning()) { + return d->eventLoop()->exec(); } return 0; @@ -85,6 +99,15 @@ int State::commandStarted() const void State::commandFinished(int returnCode) const { - d->eventLoop.exit(returnCode); + if (!s_hasEventLoop) { + QCoreApplication::exit(returnCode); + } else { + d->eventLoop()->exit(returnCode); + } +} + +void State::setHasEventLoop(bool evented) +{ + s_hasEventLoop = evented; } diff --git a/akonadish/state.h b/akonadish/state.h index eb07f56..1ba86dd 100644 --- a/akonadish/state.h +++ b/akonadish/state.h @@ -36,6 +36,8 @@ public: int commandStarted() const; void commandFinished(int returnCode = 0) const; + static void setHasEventLoop(bool evented); + private: class Private; Private * const d; -- cgit v1.2.3 From 8b397cec29460e2cb53b9be02b4e53da9cce60c9 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 24 Dec 2015 16:23:46 +0100 Subject: bye-bye header files the function-centric approach simply does not require them! --- akonadish/syntax_modules/akonadi_count.cpp | 24 +++++++-------- akonadish/syntax_modules/akonadi_count.h | 29 ------------------ akonadish/syntax_modules/akonadi_list.cpp | 24 +++++++-------- akonadish/syntax_modules/akonadi_list.h | 29 ------------------ akonadish/syntax_modules/akonadi_sync.cpp | 24 +++++++-------- akonadish/syntax_modules/akonadi_sync.h | 29 ------------------ akonadish/syntax_modules/core_syntax.cpp | 49 +++++++++++++++--------------- akonadish/syntax_modules/core_syntax.h | 33 -------------------- akonadish/syntaxtree.cpp | 7 ----- 9 files changed, 61 insertions(+), 187 deletions(-) delete mode 100644 akonadish/syntax_modules/akonadi_count.h delete mode 100644 akonadish/syntax_modules/akonadi_list.h delete mode 100644 akonadish/syntax_modules/akonadi_sync.h delete mode 100644 akonadish/syntax_modules/core_syntax.h diff --git a/akonadish/syntax_modules/akonadi_count.cpp b/akonadish/syntax_modules/akonadi_count.cpp index e54ac22..70aabc9 100644 --- a/akonadish/syntax_modules/akonadi_count.cpp +++ b/akonadish/syntax_modules/akonadi_count.cpp @@ -17,8 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "akonadi_count.h" - #include #include #include // tr() @@ -35,20 +33,12 @@ #include "common/definitions.h" #include "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" namespace AkonadiCount { -REGISTER_SYNTAX(AkonadiCount) - -Syntax::List syntax() -{ - Syntax::List syntax; - syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &AkonadiCount::count, Syntax::EventDriven); - - return syntax; -} - bool count(const QStringList &args, State &state) { auto resources = args; @@ -78,4 +68,14 @@ bool count(const QStringList &args, State &state) return true; } +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count "), &AkonadiCount::count, Syntax::EventDriven); + + return syntax; +} + +REGISTER_SYNTAX(AkonadiCount) + } diff --git a/akonadish/syntax_modules/akonadi_count.h b/akonadish/syntax_modules/akonadi_count.h deleted file mode 100644 index c592c4c..0000000 --- a/akonadish/syntax_modules/akonadi_count.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace AkonadiCount -{ - Syntax::List syntax(); - bool count(const QStringList &commands, State &state); -} - diff --git a/akonadish/syntax_modules/akonadi_list.cpp b/akonadish/syntax_modules/akonadi_list.cpp index 25ebbca..25ccabf 100644 --- a/akonadish/syntax_modules/akonadi_list.cpp +++ b/akonadish/syntax_modules/akonadi_list.cpp @@ -17,8 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "akonadi_list.h" - #include #include #include // tr() @@ -35,20 +33,12 @@ #include "common/definitions.h" #include "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" namespace AkonadiList { -REGISTER_SYNTAX(AkonadiList) - -Syntax::List syntax() -{ - Syntax::List syntax; - syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven); - - return syntax; -} - bool list(const QStringList &args, State &state) { auto resources = args; @@ -111,4 +101,14 @@ bool list(const QStringList &args, State &state) return false; } +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven); + + return syntax; +} + +REGISTER_SYNTAX(AkonadiList) + } diff --git a/akonadish/syntax_modules/akonadi_list.h b/akonadish/syntax_modules/akonadi_list.h deleted file mode 100644 index 61effc5..0000000 --- a/akonadish/syntax_modules/akonadi_list.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace AkonadiList -{ - Syntax::List syntax(); - bool list(const QStringList &commands, State &state); -} - diff --git a/akonadish/syntax_modules/akonadi_sync.cpp b/akonadish/syntax_modules/akonadi_sync.cpp index e9388d2..1cf097d 100644 --- a/akonadish/syntax_modules/akonadi_sync.cpp +++ b/akonadish/syntax_modules/akonadi_sync.cpp @@ -17,8 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "akonadi_sync.h" - #include #include // tr() #include @@ -33,20 +31,12 @@ #include "common/definitions.h" #include "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" namespace AkonadiSync { -REGISTER_SYNTAX(AkonadiSync) - -Syntax::List syntax() -{ - Syntax::List syntax; - syntax << Syntax("sync", QObject::tr("Syncronizes all resources that are listed; and empty list triggers a syncronizaton on all resources"), &AkonadiSync::sync, Syntax::EventDriven ); - - return syntax; -} - bool sync(const QStringList &args, State &state) { Akonadi2::Query query; @@ -66,4 +56,14 @@ bool sync(const QStringList &args, State &state) return true; } +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("sync", QObject::tr("Syncronizes all resources that are listed; and empty list triggers a syncronizaton on all resources"), &AkonadiSync::sync, Syntax::EventDriven ); + + return syntax; +} + +REGISTER_SYNTAX(AkonadiSync) + } diff --git a/akonadish/syntax_modules/akonadi_sync.h b/akonadish/syntax_modules/akonadi_sync.h deleted file mode 100644 index 62f7424..0000000 --- a/akonadish/syntax_modules/akonadi_sync.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace AkonadiSync -{ - Syntax::List syntax(); - bool sync(const QStringList &commands, State &state); -} - diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 8fb1448..a0cd4c6 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -17,37 +17,16 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "core_syntax.h" - #include #include // tr() #include #include -namespace CoreSyntax -{ - -REGISTER_SYNTAX(CoreSyntax) +#include "state.h" +#include "syntaxtree.h" -Syntax::List syntax() +namespace CoreSyntax { - Syntax::List syntax; - syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - - Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); - help.completer = &CoreSyntax::showHelpCompleter; - syntax << help; - - Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); - set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); - syntax << set; - - Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); - syntax << get; - - return syntax; -} bool exit(const QStringList &, State &) { @@ -137,5 +116,27 @@ bool printDebugLevel(const QStringList &commands, State &state) return true; } +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + + Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + help.completer = &CoreSyntax::showHelpCompleter; + syntax << help; + + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); + set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + syntax << set; + + Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); + get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + syntax << get; + + return syntax; +} + +REGISTER_SYNTAX(CoreSyntax) + } // namespace CoreSyntax diff --git a/akonadish/syntax_modules/core_syntax.h b/akonadish/syntax_modules/core_syntax.h deleted file mode 100644 index 89187e5..0000000 --- a/akonadish/syntax_modules/core_syntax.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include "syntaxtree.h" - -namespace CoreSyntax -{ - Syntax::List syntax(); - bool exit(const QStringList &commands, State &state); - bool showHelp(const QStringList &commands, State &state); - QStringList showHelpCompleter(const QStringList &commands, const QString &fragment); - bool setDebugLevel(const QStringList &commands, State &state); - bool printDebugLevel(const QStringList &commands, State &state); -} - diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index cd7348e..36a7f41 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -22,13 +22,6 @@ #include #include -// TODO: needs a proper registry; making "core" modules plugins is -// almost certainly overkill, but this is not the way either -#include "syntax_modules/core_syntax.h" -#include "syntax_modules/akonadi_list.h" -#include "syntax_modules/akonadi_count.h" -#include "syntax_modules/akonadi_sync.h" - SyntaxTree *SyntaxTree::s_module = 0; Syntax::Syntax() -- cgit v1.2.3 From 7211e9e3bab79ae5644cebe38996a028fcaabc8a Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:30:05 +0100 Subject: const& --- akonadish/syntaxtree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index 36a7f41..b571904 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -126,7 +126,7 @@ Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString & Syntax lastFullSyntax; while (wordIt.hasNext()) { - QString word = wordIt.next(); + const QString &word = wordIt.next(); while (syntaxIt.hasNext()) { const Syntax &syntax = syntaxIt.next(); if (word == syntax.keyword) { -- cgit v1.2.3 From 7890daed900f177bf1f1327b5dde754b7dfe6aaf Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:30:16 +0100 Subject: missing break, preventing proper traversal down the syntax tree --- akonadish/syntaxtree.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index b571904..899fc61 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -132,6 +132,7 @@ Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString & if (word == syntax.keyword) { lastFullSyntax = syntax; syntaxIt = syntax.children; + break; } } } -- cgit v1.2.3 From f52da78a2da39974f27faf34915adc6dcb80b4c8 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:30:55 +0100 Subject: command timing setting --- akonadish/state.cpp | 12 ++++++++++++ akonadish/state.h | 3 +++ 2 files changed, 15 insertions(+) diff --git a/akonadish/state.cpp b/akonadish/state.cpp index c9f9ee3..f3f5975 100644 --- a/akonadish/state.cpp +++ b/akonadish/state.cpp @@ -45,6 +45,7 @@ public: int debugLevel = 0; QEventLoop *event = 0; + bool timing = false; QTextStream outStream; }; @@ -111,3 +112,14 @@ void State::setHasEventLoop(bool evented) s_hasEventLoop = evented; } +void State::setCommandTiming(bool time) +{ + d->timing = time; +} + +bool State::commandTiming() const +{ + return d->timing; +} + + diff --git a/akonadish/state.h b/akonadish/state.h index 1ba86dd..9c1ab6f 100644 --- a/akonadish/state.h +++ b/akonadish/state.h @@ -33,6 +33,9 @@ public: void setDebugLevel(unsigned int level); unsigned int debugLevel() const; + void setCommandTiming(bool); + bool commandTiming() const; + int commandStarted() const; void commandFinished(int returnCode = 0) const; -- cgit v1.2.3 From ad442fb49e5d4271a5f2276eb73d9d15b1e8755f Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:31:24 +0100 Subject: support command timing output --- akonadish/syntaxtree.cpp | 13 ++++++++----- akonadish/syntaxtree.h | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index 899fc61..dbbff01 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -62,16 +62,19 @@ Syntax::List SyntaxTree::syntax() const bool SyntaxTree::run(const QStringList &commands) { + bool success = false; + m_timeElapsed.start(); Command command = match(commands); if (command.first && command.first->lambda) { - bool rv = command.first->lambda(command.second, m_state); - if (rv && command.first->interactivity == Syntax::EventDriven) { - return m_state.commandStarted(); + success = command.first->lambda(command.second, m_state); + if (success && command.first->interactivity == Syntax::EventDriven) { + success = m_state.commandStarted(); } - - return rv; } + if (m_state.commandTiming()) { + m_state.printLine(QObject::tr("Time elapsed: %1").arg(m_timeElapsed.elapsed())); + } return false; } diff --git a/akonadish/syntaxtree.h b/akonadish/syntaxtree.h index 3421fc3..5ee915a 100644 --- a/akonadish/syntaxtree.h +++ b/akonadish/syntaxtree.h @@ -22,6 +22,7 @@ #include "state.h" #include +#include #include class Syntax @@ -70,6 +71,7 @@ private: Syntax::List m_syntax; State m_state; + QTime m_timeElapsed; static SyntaxTree *s_module; }; -- cgit v1.2.3 From 908dc9b172a08bfa219a834e15916e9fcfc3c4ec Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:31:30 +0100 Subject: syntax to turn timing on/off loving the lambdas :) --- akonadish/syntax_modules/core_syntax.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index a0cd4c6..9cd6a6a 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -110,12 +110,18 @@ bool setDebugLevel(const QStringList &commands, State &state) return true; } -bool printDebugLevel(const QStringList &commands, State &state) +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; +} + Syntax::List syntax() { Syntax::List syntax; @@ -127,10 +133,15 @@ Syntax::List syntax() Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + Syntax setTiming = Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete")); + setTiming.children << Syntax(QObject::tr("on"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); + setTiming.children << Syntax(QObject::tr("off"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); + set.children << setTiming; syntax << set; Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + get.children << Syntax(QObject::tr("debug"), QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + get.children << Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); syntax << get; return syntax; -- cgit v1.2.3 From 1231996ec2668e330ead79a16edf85ded1e07e48 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:47:02 +0100 Subject: print the syntax tree --- akonadish/syntax_modules/core_syntax.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 9cd6a6a..8f3219f 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -122,6 +122,29 @@ bool printCommandTiming(const QStringList &, State &state) 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; +} + Syntax::List syntax() { Syntax::List syntax; @@ -131,6 +154,8 @@ Syntax::List syntax() help.completer = &CoreSyntax::showHelpCompleter; syntax << help; + syntax << Syntax("syntaxtree", QString(), &printSyntaxTree); + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); Syntax setTiming = Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete")); -- cgit v1.2.3 From 540864148b9bccc4b00bb3ba504190d608489413 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 11:01:01 +0100 Subject: don't translate the commands; let them be scriptable universally --- akonadish/syntax_modules/core_syntax.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 8f3219f..31b824a 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -150,23 +150,23 @@ Syntax::List syntax() Syntax::List syntax; syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + 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(QObject::tr("set"), QObject::tr("Sets settings for the session")); - set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); - Syntax setTiming = Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete")); - setTiming.children << Syntax(QObject::tr("on"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); - setTiming.children << Syntax(QObject::tr("off"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); + 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 << set; - Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); - get.children << Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); + 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); syntax << get; return syntax; -- cgit v1.2.3 From ee3600d7f6a4e31e35dad5d0029d8b1e52d660ad Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 11:05:55 +0100 Subject: error on unknown / malformed commands --- akonadish/syntaxtree.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index dbbff01..4188e5f 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -65,11 +65,24 @@ bool SyntaxTree::run(const QStringList &commands) bool success = false; m_timeElapsed.start(); Command command = match(commands); - if (command.first && command.first->lambda) { - success = command.first->lambda(command.second, m_state); - if (success && command.first->interactivity == Syntax::EventDriven) { - success = m_state.commandStarted(); + if (command.first) { + if (command.first->lambda) { + success = command.first->lambda(command.second, m_state); + if (success && command.first->interactivity == Syntax::EventDriven) { + success = m_state.commandStarted(); + } + } else if (command.first->children.isEmpty()) { + m_state.printError(QObject::tr("Broken command... sorry :("), "st_broken"); + } else { + QStringList keywordList; + for (auto syntax: command.first->children) { + keywordList << syntax.keyword; + } + const QString keywords = keywordList.join(" " ); + m_state.printError(QObject::tr("Command requires additional arguments, one of: %1").arg(keywords)); } + } else { + m_state.printError(QObject::tr("Unknown command"), "st_unknown"); } if (m_state.commandTiming()) { -- cgit v1.2.3 From 5722805a3f6aa144c5b47dc47f16cd21c057bbd6 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 11:21:23 +0100 Subject: proper tokenization of input --- akonadish/syntaxtree.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index 4188e5f..495ad22 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp @@ -170,7 +170,47 @@ Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString & QStringList SyntaxTree::tokenize(const QString &text) { - //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] - return text.split(" "); + //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"']a + static const QVector quoters = QVector() << '"' << '\''; + QStringList tokens; + QString acc; + QChar closer; + for (int i = 0; i < text.size(); ++i) { + const QChar c = text.at(i); + if (c == '\\') { + ++i; + if (i < text.size()) { + acc.append(text.at(i)); + } + } else if (!closer.isNull()) { + if (c == closer) { + acc = acc.trimmed(); + if (!acc.isEmpty()) { + tokens << acc; + } + acc.clear(); + closer = QChar(); + } else { + acc.append(c); + } + } else if (c.isSpace()) { + acc = acc.trimmed(); + if (!acc.isEmpty()) { + tokens << acc; + } + acc.clear(); + } else if (quoters.contains(c)) { + closer = c; + } else { + acc.append(c); + } + } + + acc = acc.trimmed(); + if (!acc.isEmpty()) { + tokens << acc; + } + + return tokens; } -- cgit v1.2.3 From c1eb4d6f95962c36e5e3994a0e6578cbff03cf49 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 11:32:51 +0100 Subject: clear command --- akonadish/CMakeLists.txt | 1 + akonadish/syntax_modules/akonadi_clear.cpp | 61 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 akonadish/syntax_modules/akonadi_clear.cpp diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt index 39a059f..e00d25a 100644 --- a/akonadish/CMakeLists.txt +++ b/akonadish/CMakeLists.txt @@ -8,6 +8,7 @@ set(akonadi2_cli_SRCS syntaxtree.cpp syntax_modules/core_syntax.cpp syntax_modules/akonadi_list.cpp + syntax_modules/akonadi_clear.cpp syntax_modules/akonadi_count.cpp syntax_modules/akonadi_sync.cpp akonadish_utils.cpp diff --git a/akonadish/syntax_modules/akonadi_clear.cpp b/akonadish/syntax_modules/akonadi_clear.cpp new file mode 100644 index 0000000..d17fac2 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_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 "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace AkonadiClear +{ + +bool clear(const QStringList &args, State &state) +{ + for (const auto &resource : args) { + state.print(QObject::tr("Removing local cache for '%1' ...").arg(resource)); + Akonadi2::Store::removeFromDisk(resource.toLatin1()); + state.printLine(QObject::tr("done")); + } + + return true; +} + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("clear", QObject::tr("Clears the local cache of one or more resources (be careful!)"), &AkonadiClear::clear); + + return syntax; +} + +REGISTER_SYNTAX(AkonadiClear) + +} -- cgit v1.2.3 From a90342d5fb92092e1d1715eae7f66f33d5b4a66c Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 18:27:28 +0100 Subject: key/value map maker straight from akonadi2_client .. needs improving --- akonadish/akonadish_utils.cpp | 12 ++++++++++++ akonadish/akonadish_utils.h | 1 + 2 files changed, 13 insertions(+) diff --git a/akonadish/akonadish_utils.cpp b/akonadish/akonadish_utils.cpp index bfb72ca..ffbdcb3 100644 --- a/akonadish/akonadish_utils.cpp +++ b/akonadish/akonadish_utils.cpp @@ -70,5 +70,17 @@ QSharedPointer loadModel(const QString &type, Akonadi2::Quer return model; } +QMap keyValueMapFromArgs(const QStringList &args) +{ + //TODO: this is not the most clever of algorithms. preserved during the port of commands + // from akonadi2_client ... we can probably do better, however ;) + QMap map; + for (int i = 0; i + 2 <= args.size(); i += 2) { + map.insert(args.at(i), args.at(i + 1)); + } + + return map; +} + } diff --git a/akonadish/akonadish_utils.h b/akonadish/akonadish_utils.h index c15162f..17b8ec7 100644 --- a/akonadish/akonadish_utils.h +++ b/akonadish/akonadish_utils.h @@ -34,6 +34,7 @@ class StoreBase; bool isValidStoreType(const QString &type); StoreBase &getStore(const QString &type); QSharedPointer loadModel(const QString &type, Akonadi2::Query query); +QMap keyValueMapFromArgs(const QStringList &args); /** * A small abstraction layer to use the akonadi store with the type available as string. -- cgit v1.2.3 From 02ebb2bd3c9a5d4fe224c239b2ea10e7db12ebc6 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 18:27:55 +0100 Subject: create resource --- akonadish/CMakeLists.txt | 1 + akonadish/syntax_modules/akonadi_create.cpp | 107 ++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 akonadish/syntax_modules/akonadi_create.cpp diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt index e00d25a..03a8898 100644 --- a/akonadish/CMakeLists.txt +++ b/akonadish/CMakeLists.txt @@ -10,6 +10,7 @@ set(akonadi2_cli_SRCS syntax_modules/akonadi_list.cpp syntax_modules/akonadi_clear.cpp syntax_modules/akonadi_count.cpp + syntax_modules/akonadi_create.cpp syntax_modules/akonadi_sync.cpp akonadish_utils.cpp repl/repl.cpp diff --git a/akonadish/syntax_modules/akonadi_create.cpp b/akonadish/syntax_modules/akonadi_create.cpp new file mode 100644 index 0000000..025cd58 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_create.cpp @@ -0,0 +1,107 @@ +/* + * 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 "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace AkonadiCreate +{ + + /* +{ + auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + auto &store = getStore(type); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object; + if (type == "resource") { + auto resourceType = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject(""); + object->setProperty("type", resourceType); + } else { + auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); + object = store.getObject(resource); + } + auto map = consumeMap(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()) { + std::cout << "An error occurred while creating the entity: " << result.errorMessage().toStdString(); + } +} +*/ +bool resource(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A resource can not be created without a type"), "akonadicreate/01"); + } + + auto &store = AkonadishUtils::getStore("resource"); + + auto resourceType = args.at(0); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(""); + object->setProperty("type", resourceType); + + auto map = AkonadishUtils::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_" + QString::number(result.errorCode())); + } + + return true; +} + + +Syntax::List syntax() +{ + Syntax::List syntax; + + Syntax create("create");//, QString(), &AkonadiCreate::resource, Syntax::EventDriven); + create.children << Syntax("resource", QObject::tr("Creates a new resource"), &AkonadiCreate::resource);//, Syntax::EventDriven); + + syntax << create; + return syntax; +} + +REGISTER_SYNTAX(AkonadiCreate) + +} -- cgit v1.2.3 From 045c47b877cd6b996eb17b91963d5e25b6707a53 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 19:44:49 +0100 Subject: error out when nothing useful is provided would be nicer to autocomplete? --- akonadish/syntax_modules/akonadi_list.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/akonadish/syntax_modules/akonadi_list.cpp b/akonadish/syntax_modules/akonadi_list.cpp index 25ccabf..807119c 100644 --- a/akonadish/syntax_modules/akonadi_list.cpp +++ b/akonadish/syntax_modules/akonadi_list.cpp @@ -41,6 +41,11 @@ namespace AkonadiList 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(); -- cgit v1.2.3 From 5333d2560ecce9795382800fb84117da1a56d8c4 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 19:45:04 +0100 Subject: stat --- akonadish/CMakeLists.txt | 1 + akonadish/syntax_modules/akonadi_stat.cpp | 113 ++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 akonadish/syntax_modules/akonadi_stat.cpp diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt index 03a8898..0aad28d 100644 --- a/akonadish/CMakeLists.txt +++ b/akonadish/CMakeLists.txt @@ -11,6 +11,7 @@ set(akonadi2_cli_SRCS syntax_modules/akonadi_clear.cpp syntax_modules/akonadi_count.cpp syntax_modules/akonadi_create.cpp + syntax_modules/akonadi_stat.cpp syntax_modules/akonadi_sync.cpp akonadish_utils.cpp repl/repl.cpp diff --git a/akonadish/syntax_modules/akonadi_stat.cpp b/akonadish/syntax_modules/akonadi_stat.cpp new file mode 100644 index 0000000..149ccbd --- /dev/null +++ b/akonadish/syntax_modules/akonadi_stat.cpp @@ -0,0 +1,113 @@ +/* + * 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 "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace AkonadiStat +{ + +void statResources(const QStringList &resources, const State &state) +{ + qint64 total = 0; + for (const auto &resource : resources) { + Akonadi2::Storage storage(Akonadi2::storageLocation(), resource, Akonadi2::Storage::ReadOnly); + auto transaction = storage.createTransaction(Akonadi2::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; + } + } + + state.printLine(QObject::tr("Total [kb]: %1").arg(total)); +} + +bool statAllResources(State &state) +{ + Akonadi2::Query query; + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + auto model = AkonadishUtils::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), Akonadi2::Store::DomainObjectBaseRole).value(); + resources << object->identifier(); + } + }); + + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { + statResources(resources, state); + state.commandFinished(); + } + }); + + if (!model->data(QModelIndex(), Akonadi2::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::List syntax; + syntax << Syntax("stat", QObject::tr("Shows database usage for the resources requested"), &AkonadiStat::stat, Syntax::EventDriven); + + return syntax; +} + +REGISTER_SYNTAX(AkonadiStat) + +} -- cgit v1.2.3 From 55e5d310fb1cf1c85359bbb6f3d64120da9b226b Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 19:51:34 +0100 Subject: remove resource --- akonadish/CMakeLists.txt | 1 + akonadish/syntax_modules/akonadi_remove.cpp | 82 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 akonadish/syntax_modules/akonadi_remove.cpp diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt index 0aad28d..e5ad667 100644 --- a/akonadish/CMakeLists.txt +++ b/akonadish/CMakeLists.txt @@ -11,6 +11,7 @@ set(akonadi2_cli_SRCS syntax_modules/akonadi_clear.cpp syntax_modules/akonadi_count.cpp syntax_modules/akonadi_create.cpp + syntax_modules/akonadi_remove.cpp syntax_modules/akonadi_stat.cpp syntax_modules/akonadi_sync.cpp akonadish_utils.cpp diff --git a/akonadish/syntax_modules/akonadi_remove.cpp b/akonadish/syntax_modules/akonadi_remove.cpp new file mode 100644 index 0000000..f58fa7d --- /dev/null +++ b/akonadish/syntax_modules/akonadi_remove.cpp @@ -0,0 +1,82 @@ +/* + * 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 "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace AkonadiRemove +{ + +bool resource(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A resource can not be removed without an id"), "akonadi_remove/01"); + } + + auto &store = AkonadishUtils::getStore("resource"); + + auto resourceId = args.at(0); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); + + auto map = AkonadishUtils::keyValueMapFromArgs(args); + for (auto i = map.begin(); i != map.end(); ++i) { + object->setProperty(i.key().toLatin1(), i.value()); + } + + 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_create_" + QString::number(result.errorCode())); + } + + return true; +} + + +Syntax::List syntax() +{ + Syntax::List syntax; + + Syntax create("remove"); + create.children << Syntax("resource", QObject::tr("Removes a resource"), &AkonadiRemove::resource);//, Syntax::EventDriven); + + syntax << create; + return syntax; +} + +REGISTER_SYNTAX(AkonadiRemove) + +} -- cgit v1.2.3 From 6517af4a8cb6223893ddf805ae5193f715d661a6 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 19:55:21 +0100 Subject: add my little list of things to contemplate --- akonadish/TODO | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 akonadish/TODO diff --git a/akonadish/TODO b/akonadish/TODO new file mode 100644 index 0000000..81187e3 --- /dev/null +++ b/akonadish/TODO @@ -0,0 +1,10 @@ +* commands + * create (non-resource types) + * modify + * remove (non-resource types) +* provide a setting to turn on/off user interaction during commands (e.g. deletion confirmations) + * add a "ask the user a question" helper in State +* key/value syntax objects +* json objects! (set json on; ...) +* make the shell generic and have it load a plugin matching the name of argv[0] for syntax + -- cgit v1.2.3 From 8648ce60eda891404231d7e484de5c2a2a3efd35 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 19:55:25 +0100 Subject: unneeded --- akonadish/syntax_modules/akonadi_remove.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/akonadish/syntax_modules/akonadi_remove.cpp b/akonadish/syntax_modules/akonadi_remove.cpp index f58fa7d..01e4ead 100644 --- a/akonadish/syntax_modules/akonadi_remove.cpp +++ b/akonadish/syntax_modules/akonadi_remove.cpp @@ -50,11 +50,6 @@ bool resource(const QStringList &args, State &state) auto resourceId = args.at(0); Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); - auto map = AkonadishUtils::keyValueMapFromArgs(args); - for (auto i = map.begin(); i != map.end(); ++i) { - object->setProperty(i.key().toLatin1(), i.value()); - } - auto result = store.remove(*object).exec(); result.waitForFinished(); if (result.errorCode()) { -- cgit v1.2.3 From 7e548fbe071d3978a9659cedeb5fbbc183985bc3 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 20:03:49 +0100 Subject: crete other items --- akonadish/syntax_modules/akonadi_create.cpp | 45 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/akonadish/syntax_modules/akonadi_create.cpp b/akonadish/syntax_modules/akonadi_create.cpp index 025cd58..377219a 100644 --- a/akonadish/syntax_modules/akonadi_create.cpp +++ b/akonadish/syntax_modules/akonadi_create.cpp @@ -39,34 +39,45 @@ namespace AkonadiCreate { - /* +bool create(const QStringList &allArgs, State &state) { - auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - auto &store = getStore(type); - Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object; - if (type == "resource") { - auto resourceType = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - object = store.getObject(""); - object->setProperty("type", resourceType); - } else { - auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); - object = store.getObject(resource); + if (allArgs.isEmpty()) { + state.printError(QObject::tr("A type is required"), "akonadicreate/02"); + return false; + } + + if (allArgs.count() < 2) { + state.printError(QObject::tr("A resource ID is required to create items"), "akonadicreate/03"); + return false; } - auto map = consumeMap(args); + + auto args = allArgs; + auto type = args.takeFirst(); + auto &store = AkonadishUtils::getStore(type); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object; + auto resource = args.takeFirst().toLatin1(); + object = store.getObject(resource); + + auto map = AkonadishUtils::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()) { - std::cout << "An error occurred while creating the entity: " << result.errorMessage().toStdString(); + 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"), "akonadicreate/01"); + return false; } auto &store = AkonadishUtils::getStore("resource"); @@ -84,7 +95,7 @@ bool resource(const QStringList &args, State &state) result.waitForFinished(); if (result.errorCode()) { state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()), - "akonaid_create_" + QString::number(result.errorCode())); + "akonaid_create_e" + QString::number(result.errorCode())); } return true; @@ -95,8 +106,8 @@ Syntax::List syntax() { Syntax::List syntax; - Syntax create("create");//, QString(), &AkonadiCreate::resource, Syntax::EventDriven); - create.children << Syntax("resource", QObject::tr("Creates a new resource"), &AkonadiCreate::resource);//, Syntax::EventDriven); + Syntax create("create", QObject::tr("Create items in a resource"), &AkonadiCreate::create); + create.children << Syntax("resource", QObject::tr("Creates a new resource"), &AkonadiCreate::resource); syntax << create; return syntax; -- cgit v1.2.3 From 23f0de019b76cc3552fa7b03a7ff343b6f1c1b2d Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 20:11:00 +0100 Subject: update --- akonadish/TODO | 2 -- 1 file changed, 2 deletions(-) diff --git a/akonadish/TODO b/akonadish/TODO index 81187e3..985bf21 100644 --- a/akonadish/TODO +++ b/akonadish/TODO @@ -1,7 +1,5 @@ * commands - * create (non-resource types) * modify - * remove (non-resource types) * provide a setting to turn on/off user interaction during commands (e.g. deletion confirmations) * add a "ask the user a question" helper in State * key/value syntax objects -- cgit v1.2.3 From 47f175743ada27a031c9496a2bd1890c74464b27 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 20:11:02 +0100 Subject: remove non-resources --- akonadish/syntax_modules/akonadi_remove.cpp | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/akonadish/syntax_modules/akonadi_remove.cpp b/akonadish/syntax_modules/akonadi_remove.cpp index 01e4ead..5e11af3 100644 --- a/akonadish/syntax_modules/akonadi_remove.cpp +++ b/akonadish/syntax_modules/akonadi_remove.cpp @@ -39,6 +39,40 @@ namespace AkonadiRemove { +bool remove(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A type is required"), "akonadicreate/02"); + return false; + } + + if (args.count() < 2) { + state.printError(QObject::tr("A resource ID is required to remove items"), "akonadicreate/03"); + return false; + } + + if (args.count() < 3) { + state.printError(QObject::tr("An object ID is required to remove items"), "akonadicreate/03"); + return false; + } + + auto type = args[0]; + auto resourceId = args[1]; + auto identifier = args[2]; + + auto &store = AkonadishUtils::getStore(type); + Akonadi2::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_create_" + QString::number(result.errorCode())); + } + + return true; +} + bool resource(const QStringList &args, State &state) { if (args.isEmpty()) { -- cgit v1.2.3 From 26db899f5a12d5ba960e778ce904e279e97481d8 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 21:15:47 +0100 Subject: a little welcome banner --- akonadish/repl/repl.cpp | 13 +++++++++++++ akonadish/repl/repl.h | 1 + 2 files changed, 14 insertions(+) diff --git a/akonadish/repl/repl.cpp b/akonadish/repl/repl.cpp index 395661e..499a4af 100644 --- a/akonadish/repl/repl.cpp +++ b/akonadish/repl/repl.cpp @@ -25,8 +25,10 @@ #include #include #include +#include #include "replStates.h" +#include "syntaxtree.h" Repl::Repl(QObject *parent) : QStateMachine(parent) @@ -56,6 +58,7 @@ Repl::Repl(QObject *parent) print->addTransition(print, SIGNAL(completed()), eval); setInitialState(read); + printWelcomeBanner(); start(); } @@ -65,13 +68,23 @@ Repl::~Repl() write_history(commandHistoryPath().toLocal8Bit()); } +void Repl::printWelcomeBanner() +{ + QTextStream out(stdout); + out << QObject::tr("Welcome to the Akonadi2 interative shell!\n"); + out << QObject::tr("Type `help` for information on the available commands.\n"); + out.flush(); +} + QString Repl::commandHistoryPath() { const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); + if (!QFile::exists(path)) { QDir dir; dir.mkpath(path); } + return path + "/repl_history"; } diff --git a/akonadish/repl/repl.h b/akonadish/repl/repl.h index b76c66b..d8d2533 100644 --- a/akonadish/repl/repl.h +++ b/akonadish/repl/repl.h @@ -30,5 +30,6 @@ public: ~Repl(); private: + static void printWelcomeBanner(); static QString commandHistoryPath(); }; -- cgit v1.2.3 From 2a88528b15312e011a6acc0e92ecbd4766103a23 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 22:45:23 +0100 Subject: cleanup --- akonadish/syntax_modules/akonadi_remove.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/akonadish/syntax_modules/akonadi_remove.cpp b/akonadish/syntax_modules/akonadi_remove.cpp index 5e11af3..bf09e2e 100644 --- a/akonadish/syntax_modules/akonadi_remove.cpp +++ b/akonadish/syntax_modules/akonadi_remove.cpp @@ -42,17 +42,17 @@ namespace AkonadiRemove bool remove(const QStringList &args, State &state) { if (args.isEmpty()) { - state.printError(QObject::tr("A type is required"), "akonadicreate/02"); + state.printError(QObject::tr("A type is required"), "akonadi_remove/02"); return false; } if (args.count() < 2) { - state.printError(QObject::tr("A resource ID is required to remove items"), "akonadicreate/03"); + state.printError(QObject::tr("A resource ID is required to remove items"), "akonadi_remove/03"); return false; } if (args.count() < 3) { - state.printError(QObject::tr("An object ID is required to remove items"), "akonadicreate/03"); + state.printError(QObject::tr("An object ID is required to remove items"), "akonadi_remove/03"); return false; } @@ -67,7 +67,7 @@ bool remove(const QStringList &args, State &state) 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_create_" + QString::number(result.errorCode())); + "akonaid_remove_e" + QString::number(result.errorCode())); } return true; @@ -88,7 +88,7 @@ bool resource(const QStringList &args, State &state) result.waitForFinished(); if (result.errorCode()) { state.printError(QObject::tr("An error occurred while removing the resource %1: %2").arg(resourceId).arg(result.errorMessage()), - "akonaid_create_" + QString::number(result.errorCode())); + "akonaid_remove_e" + QString::number(result.errorCode())); } return true; @@ -99,10 +99,10 @@ Syntax::List syntax() { Syntax::List syntax; - Syntax create("remove"); - create.children << Syntax("resource", QObject::tr("Removes a resource"), &AkonadiRemove::resource);//, Syntax::EventDriven); + Syntax remove("remove", QObject::tr("Remove items in a resource"), &AkonadiRemove::remove); + remove.children << Syntax("resource", QObject::tr("Removes a resource"), &AkonadiRemove::resource);//, Syntax::EventDriven); - syntax << create; + syntax << remove; return syntax; } -- cgit v1.2.3 From 0489b7f975881aa445a635dc97a7b8f32c3d6319 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 22:46:43 +0100 Subject: update --- akonadish/TODO | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/akonadish/TODO b/akonadish/TODO index 985bf21..469dbf2 100644 --- a/akonadish/TODO +++ b/akonadish/TODO @@ -1,5 +1,6 @@ * commands - * modify + * improve modify/remove/create to dynamically add syntax items for each type + * improve modify/remove/create to autocomplete resource names * provide a setting to turn on/off user interaction during commands (e.g. deletion confirmations) * add a "ask the user a question" helper in State * key/value syntax objects -- cgit v1.2.3 From c8fda874bb4625217b6f5cb70228d891c2a419bb Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 22:46:48 +0100 Subject: modify --- akonadish/CMakeLists.txt | 1 + akonadish/syntax_modules/akonadi_modify.cpp | 121 ++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 akonadish/syntax_modules/akonadi_modify.cpp diff --git a/akonadish/CMakeLists.txt b/akonadish/CMakeLists.txt index e5ad667..6761a32 100644 --- a/akonadish/CMakeLists.txt +++ b/akonadish/CMakeLists.txt @@ -11,6 +11,7 @@ set(akonadi2_cli_SRCS syntax_modules/akonadi_clear.cpp syntax_modules/akonadi_count.cpp syntax_modules/akonadi_create.cpp + syntax_modules/akonadi_modify.cpp syntax_modules/akonadi_remove.cpp syntax_modules/akonadi_stat.cpp syntax_modules/akonadi_sync.cpp diff --git a/akonadish/syntax_modules/akonadi_modify.cpp b/akonadish/syntax_modules/akonadi_modify.cpp new file mode 100644 index 0000000..8438301 --- /dev/null +++ b/akonadish/syntax_modules/akonadi_modify.cpp @@ -0,0 +1,121 @@ +/* + * 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 "akonadish_utils.h" +#include "state.h" +#include "syntaxtree.h" + +namespace AkonadiModify +{ + +bool modify(const QStringList &args, State &state) +{ + if (args.isEmpty()) { + state.printError(QObject::tr("A type is required"), "akonadi_modify/02"); + return false; + } + + if (args.count() < 2) { + state.printError(QObject::tr("A resource ID is required to remove items"), "akonadi_modify/03"); + return false; + } + + if (args.count() < 3) { + state.printError(QObject::tr("An object ID is required to remove items"), "akonadi_modify/03"); + return false; + } + + auto type = args[0]; + auto resourceId = args[1]; + auto identifier = args[2]; + + auto &store = AkonadishUtils::getStore(type); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8()); + + auto map = AkonadishUtils::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"), "akonadi_modify/01"); + } + + auto &store = AkonadishUtils::getStore("resource"); + + auto resourceId = args.at(0); + Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); + + auto map = AkonadishUtils::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::List syntax; + + Syntax modify("modify", QObject::tr("Modify items in a resource"), &AkonadiModify::modify); + modify.children << Syntax("resource", QObject::tr("Modify a resource"), &AkonadiModify::resource);//, Syntax::EventDriven); + + syntax << modify; + return syntax; +} + +REGISTER_SYNTAX(AkonadiModify) + +} -- cgit v1.2.3 From 67a19008d078a067ceb3424c00553c33b918970e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 26 Dec 2015 19:18:49 +0100 Subject: Fixed build --- akonadish/syntaxtree.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/akonadish/syntaxtree.h b/akonadish/syntaxtree.h index 5ee915a..884a10d 100644 --- a/akonadish/syntaxtree.h +++ b/akonadish/syntaxtree.h @@ -25,6 +25,8 @@ #include #include +#include + class Syntax { public: -- cgit v1.2.3