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 --- 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 +++++++++++++ 18 files changed, 1424 insertions(+) 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 (limited to 'akonadish') 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