summaryrefslogtreecommitdiffstats
path: root/akonadish
diff options
context:
space:
mode:
Diffstat (limited to 'akonadish')
-rw-r--r--akonadish/CMakeLists.txt22
-rw-r--r--akonadish/akonadish_utils.cpp74
-rw-r--r--akonadish/akonadish_utils.h81
-rw-r--r--akonadish/main.cpp80
-rw-r--r--akonadish/repl/repl.cpp78
-rw-r--r--akonadish/repl/repl.h34
-rw-r--r--akonadish/repl/replStates.cpp171
-rw-r--r--akonadish/repl/replStates.h87
-rw-r--r--akonadish/state.cpp91
-rw-r--r--akonadish/state.h43
-rw-r--r--akonadish/syntax_modules/akonadi_count.cpp79
-rw-r--r--akonadish/syntax_modules/akonadi_count.h29
-rw-r--r--akonadish/syntax_modules/akonadi_list.cpp112
-rw-r--r--akonadish/syntax_modules/akonadi_list.h29
-rw-r--r--akonadish/syntax_modules/core_syntax.cpp139
-rw-r--r--akonadish/syntax_modules/core_syntax.h33
-rw-r--r--akonadish/syntaxtree.cpp167
-rw-r--r--akonadish/syntaxtree.h75
18 files changed, 1424 insertions, 0 deletions
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 @@
1project(akonadish)
2
3find_package(Readline REQUIRED)
4
5
6set(akonadi2_cli_SRCS
7 main.cpp
8 syntaxtree.cpp
9 syntax_modules/core_syntax.cpp
10 syntax_modules/akonadi_list.cpp
11 syntax_modules/akonadi_count.cpp
12 akonadish_utils.cpp
13 repl/repl.cpp
14 repl/replStates.cpp
15 state.cpp)
16
17include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
18
19add_executable(${PROJECT_NAME} ${akonadi2_cli_SRCS})
20target_link_libraries(${PROJECT_NAME} Qt5::Core ${Readline_LIBRARY} akonadi2common)
21install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
22
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 @@
1/*
2 * Copyright (C) 2015 Aaron Seigo <aseigo@kolabsystems.com>
3 * Copyright (C) 2015 Christian Mollekopf <mollekopf@kolabsystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "akonadish_utils.h"
22
23#include "common/clientapi.h"
24
25namespace AkonadishUtils
26{
27
28bool isValidStoreType(const QString &type)
29{
30 static const QSet<QString> types = QSet<QString>() << "folder" << "mail" << "event" << "resource";
31 return types.contains(type);
32}
33
34StoreBase &getStore(const QString &type)
35{
36 if (type == "folder") {
37 static Store<Akonadi2::ApplicationDomain::Folder> store;
38 return store;
39 } else if (type == "mail") {
40 static Store<Akonadi2::ApplicationDomain::Mail> store;
41 return store;
42 } else if (type == "event") {
43 static Store<Akonadi2::ApplicationDomain::Event> store;
44 return store;
45 } else if (type == "resource") {
46 static Store<Akonadi2::ApplicationDomain::AkonadiResource> store;
47 return store;
48 }
49
50 //TODO: reinstate the warning+assert
51 //Q_ASSERT(false);
52 //qWarning() << "Trying to get a store that doesn't exist, falling back to event";
53 static Store<Akonadi2::ApplicationDomain::Event> store;
54 return store;
55}
56
57QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query)
58{
59 if (type == "folder") {
60 query.requestedProperties << "name" << "parent";
61 } else if (type == "mail") {
62 query.requestedProperties << "subject" << "folder" << "date";
63 } else if (type == "event") {
64 query.requestedProperties << "summary";
65 } else if (type == "resource") {
66 query.requestedProperties << "type";
67 }
68 auto model = getStore(type).loadModel(query);
69 Q_ASSERT(model);
70 return model;
71}
72
73}
74
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 @@
1/*
2 * Copyright (C) 2015 Aaron Seigo <aseigo@kolabsystems.com>
3 * Copyright (C) 2015 Christian Mollekopf <mollekopf@kolabsystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#pragma once
22
23#include <QAbstractItemModel>
24#include <QSharedPointer>
25
26#include "common/query.h"
27#include "common/clientapi.h"
28
29namespace AkonadishUtils
30{
31
32class StoreBase;
33
34bool isValidStoreType(const QString &type);
35StoreBase &getStore(const QString &type);
36QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query);
37
38/**
39 * A small abstraction layer to use the akonadi store with the type available as string.
40 */
41class StoreBase {
42public:
43 virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0;
44 virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0;
45 virtual KAsync::Job<void> create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0;
46 virtual KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0;
47 virtual KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0;
48 virtual QSharedPointer<QAbstractItemModel> loadModel(const Akonadi2::Query &query) = 0;
49};
50
51template <typename T>
52class Store : public StoreBase {
53public:
54 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE {
55 return T::Ptr::create();
56 }
57
58 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE {
59 return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer<Akonadi2::ApplicationDomain::MemoryBufferAdaptor>::create());
60 }
61
62 KAsync::Job<void> create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE {
63 return Akonadi2::Store::create<T>(*static_cast<const T*>(&type));
64 }
65
66 KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE {
67 return Akonadi2::Store::modify<T>(*static_cast<const T*>(&type));
68 }
69
70 KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE {
71 return Akonadi2::Store::remove<T>(*static_cast<const T*>(&type));
72 }
73
74 QSharedPointer<QAbstractItemModel> loadModel(const Akonadi2::Query &query) Q_DECL_OVERRIDE {
75 return Akonadi2::Store::loadModel<T>(query);
76 }
77};
78
79
80}
81
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <unistd.h>
21
22#include <QCoreApplication>
23#include <QDebug>
24#include <QTextStream>
25
26#include "syntaxtree.h"
27// #include "jsonlistener.h"
28#include "repl/repl.h"
29
30/*
31 * modes of operation:
32 *
33 * 1. called with no commands: start the REPL and listen for JSON on stin
34 * 2. called with -: listen for JSON on stdin
35 * 3. called with commands: try to match to syntx
36 */
37
38int main(int argc, char *argv[])
39{
40 const bool interactive = isatty(fileno(stdin));
41 const bool startRepl = (argc == 1) && interactive;
42 //TODO: make a json command parse cause that would be awesomesauce
43 const bool startJsonListener = !startRepl &&
44 (argc == 2 && qstrcmp(argv[1], "-") == 0);
45 //qDebug() << "state at startup is" << interactive << startRepl << startJsonListener;
46
47 QCoreApplication app(argc, argv);
48 app.setApplicationName(argv[0]);
49
50 if (startRepl || startJsonListener) {
51 if (startRepl) {
52 Repl *repl = new Repl;
53 QObject::connect(repl, &QStateMachine::finished,
54 repl, &QObject::deleteLater);
55 QObject::connect(repl, &QStateMachine::finished,
56 &app, &QCoreApplication::quit);
57 }
58
59 if (startJsonListener) {
60// JsonListener listener(syntax);
61 }
62
63 return app.exec();
64 } else if (!interactive) {
65 QTextStream inputStream(stdin);
66 while (true) {
67 const QString input = inputStream.readLine();
68 if (input.isEmpty()) {
69 ::exit(0);
70 }
71
72 const QStringList commands = SyntaxTree::tokenize(input);
73 SyntaxTree::self()->run(commands);
74 }
75 } else {
76 QStringList commands = app.arguments();
77 commands.removeFirst();
78 return SyntaxTree::self()->run(commands);
79 }
80}
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "repl.h"
21
22#include <readline/history.h>
23
24#include <QDir>
25#include <QFile>
26#include <QFinalState>
27#include <QStandardPaths>
28
29#include "replStates.h"
30
31Repl::Repl(QObject *parent)
32 : QStateMachine(parent)
33{
34 // readline history setup
35 using_history();
36 read_history(commandHistoryPath().toLocal8Bit());
37
38 // create all states
39 ReadState *read = new ReadState(this);
40 UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this);
41 EvalState *eval = new EvalState(this);
42 PrintState *print = new PrintState(this);
43 QFinalState *final = new QFinalState(this);
44
45 // connect the transitions
46 read->addTransition(read, SIGNAL(command(QString)), eval);
47 read->addTransition(read, SIGNAL(exitRequested()), final);
48
49 unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval);
50 unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final);
51
52 eval->addTransition(eval, SIGNAL(completed()), read);
53 eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead);
54 eval->addTransition(eval, SIGNAL(output(QString)), print);
55
56 print->addTransition(print, SIGNAL(completed()), eval);
57
58 setInitialState(read);
59 start();
60}
61
62Repl::~Repl()
63{
64 // readline history writing
65 write_history(commandHistoryPath().toLocal8Bit());
66}
67
68QString Repl::commandHistoryPath()
69{
70 const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
71 if (!QFile::exists(path)) {
72 QDir dir;
73 dir.mkpath(path);
74 }
75 return path + "/repl_history";
76}
77
78#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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QStateMachine>
23
24class Repl : public QStateMachine
25{
26 Q_OBJECT
27
28public:
29 Repl(QObject *parent = 0);
30 ~Repl();
31
32private:
33 static QString commandHistoryPath();
34};
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "replStates.h"
21
22#include <unistd.h>
23#include <iostream>
24
25#include <readline/readline.h>
26#include <readline/history.h>
27
28#include <QDebug>
29#include <QEvent>
30#include <QStateMachine>
31
32#include "syntaxtree.h"
33
34static char *akonadi2_cli_next_tab_complete_match(const char *text, int state);
35static char ** akonadi2_cli_tab_completion(const char *text, int start, int end);
36
37ReadState::ReadState(QState *parent)
38 : QState(parent)
39{
40 rl_completion_entry_function = akonadi2_cli_next_tab_complete_match;
41 rl_attempted_completion_function = akonadi2_cli_tab_completion;
42}
43
44void ReadState::onEntry(QEvent *event)
45{
46 Q_UNUSED(event)
47 char *line = readline(prompt());
48
49 if (!line) {
50 std::cout << std::endl;
51 emit exitRequested();
52 return;
53 }
54
55 // we have actual data, so let's wait for a full line of text
56 QByteArray input(line);
57 const QString text = QString(line).simplified();
58 //qDebug() << "Line is ... " << text;
59
60 if (text.length() > 0) {
61 add_history(line);
62 }
63
64 free(line);
65 emit command(text);
66}
67
68const char *ReadState::prompt() const
69{
70 return "> ";
71}
72
73UnfinishedReadState::UnfinishedReadState(QState *parent)
74 : ReadState(parent)
75{
76}
77
78const char *UnfinishedReadState::prompt() const
79{
80 return " ";
81}
82
83EvalState::EvalState(QState *parent)
84 : QState(parent)
85{
86}
87
88void EvalState::onEntry(QEvent *event)
89{
90 QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event);
91
92 const QString command = e ? e->arguments()[0].toString() : QString();
93
94 if (command.isEmpty()) {
95 complete();
96 return;
97 }
98
99 if (command.right(1) == "\\") {
100 m_partial += " " + command.left(command.size() - 1);
101 continueInput();
102 } else {
103 m_partial += " " + command;
104 complete();
105 }
106}
107
108void EvalState::complete()
109{
110 m_partial = m_partial.simplified();
111
112 if (!m_partial.isEmpty()) {
113 //emit output("Processing ... " + command);
114 const QStringList commands = SyntaxTree::tokenize(m_partial);
115 SyntaxTree::self()->run(commands);
116 m_partial.clear();
117 }
118
119 emit completed();
120}
121
122PrintState::PrintState(QState *parent)
123 : QState(parent)
124{
125}
126
127void PrintState::onEntry(QEvent *event)
128{
129 QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event);
130
131 if (e && !e->arguments().isEmpty()) {
132 const QString command = e->arguments()[0].toString();
133 QTextStream stream(stdout);
134 stream << command << "\n";
135 }
136
137 emit completed();
138}
139
140static QStringList tab_completion_full_state;
141static bool tab_completion_at_root = false;
142
143static char **akonadi2_cli_tab_completion(const char *text, int start, int end)
144{
145 tab_completion_at_root = start == 0;
146 tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts);
147 return NULL;
148}
149
150static char *akonadi2_cli_next_tab_complete_match(const char *text, int state)
151{
152 const QString fragment(text);
153 Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, fragment);
154 //for (auto syntax: nearest) { qDebug() << "Nearest: " << syntax.keyword; }
155
156 if (nearest.isEmpty()) {
157 SyntaxTree::Command command = SyntaxTree::self()->match(tab_completion_full_state);
158 if (command.first && command.first->completer) {
159 QStringList commandCompletions = command.first->completer(tab_completion_full_state, fragment);
160 if (commandCompletions.size() > state) {
161 return qstrdup(commandCompletions[state].toUtf8());
162 }
163 }
164 } else if (nearest.size() > state) {
165 return qstrdup(nearest[state].keyword.toUtf8());
166 }
167
168 return rl_filename_completion_function(text, state);
169}
170
171#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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QState>
23
24class QSocketNotifier;
25
26class ReadState : public QState
27{
28 Q_OBJECT
29
30public:
31 ReadState(QState *parent = 0);
32
33Q_SIGNALS:
34 void command(const QString &command);
35 void exitRequested();
36
37protected:
38 void onEntry(QEvent *event);
39 virtual const char *prompt() const;
40};
41
42class UnfinishedReadState : public ReadState
43{
44 Q_OBJECT
45
46public:
47 UnfinishedReadState(QState *parent = 0);
48
49protected:
50 const char *prompt() const;
51};
52
53class EvalState : public QState
54{
55 Q_OBJECT
56
57public:
58 EvalState(QState *parent = 0);
59
60Q_SIGNALS:
61 void completed();
62 void continueInput();
63 void output(const QString &output);
64
65protected:
66 void onEntry(QEvent *event);
67
68private:
69 void complete();
70
71 QString m_partial;
72};
73
74class PrintState : public QState
75{
76 Q_OBJECT
77
78public:
79 PrintState(QState *parent = 0);
80
81Q_SIGNALS:
82 void completed();
83
84protected:
85 void onEntry(QEvent *event);
86};
87
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "state.h"
21
22#include <QDebug>
23#include <QEventLoop>
24#include <QTextStream>
25
26class State::Private
27{
28public:
29 Private()
30 : outStream(stdout)
31 {
32 }
33
34 int debugLevel = 0;
35 QEventLoop eventLoop;
36 QTextStream outStream;
37};
38
39State::State()
40 : d(new Private)
41{
42}
43
44void State::print(const QString &message, unsigned int indentationLevel) const
45{
46 for (unsigned int i = 0; i < indentationLevel; ++i) {
47 d->outStream << "\t";
48 }
49
50 d->outStream << message;
51}
52
53void State::printLine(const QString &message, unsigned int indentationLevel) const
54{
55 print(message, indentationLevel);
56 d->outStream << "\n";
57 d->outStream.flush();
58}
59
60void State::printError(const QString &errorMessage, const QString &errorCode) const
61{
62 printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage);
63}
64
65void State::setDebugLevel(unsigned int level)
66{
67 if (level < 7) {
68 d->debugLevel = level;
69 }
70}
71
72unsigned int State::debugLevel() const
73{
74 return d->debugLevel;
75}
76
77int State::commandStarted() const
78{
79 if (!d->eventLoop.isRunning()) {
80 //qDebug() << "RUNNING THE EVENT LOOP!";
81 return d->eventLoop.exec();
82 }
83
84 return 0;
85}
86
87void State::commandFinished(int returnCode) const
88{
89 d->eventLoop.exit(returnCode);
90}
91
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QTextStream>
23
24class State
25{
26public:
27 State();
28
29 void print(const QString &message, unsigned int indentationLevel = 0) const;
30 void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const;
31 void printError(const QString &errorMessage, const QString &errorCode = QString()) const;
32
33 void setDebugLevel(unsigned int level);
34 unsigned int debugLevel() const;
35
36 int commandStarted() const;
37 void commandFinished(int returnCode = 0) const;
38
39private:
40 class Private;
41 Private * const d;
42};
43
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "akonadi_count.h"
21
22#include <QCoreApplication>
23#include <QDebug>
24#include <QObject> // tr()
25#include <QModelIndex>
26#include <QTime>
27
28#include "common/resource.h"
29#include "common/storage.h"
30#include "common/domain/event.h"
31#include "common/domain/folder.h"
32#include "common/resourceconfig.h"
33#include "common/log.h"
34#include "common/storage.h"
35#include "common/definitions.h"
36
37#include "akonadish_utils.h"
38
39namespace AkonadiCount
40{
41
42Syntax::List syntax()
43{
44 Syntax::List syntax;
45 syntax << Syntax("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count <type> <resource>"), &AkonadiCount::count, Syntax::EventDriven);
46
47 return syntax;
48}
49
50bool count(const QStringList &args, State &state)
51{
52 auto resources = args;
53 auto type = !resources.isEmpty() ? resources.takeFirst() : QString();
54
55 if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) {
56 state.printError(QObject::tr("Unknown type: %1").arg(type));
57 return false;
58 }
59
60 Akonadi2::Query query;
61 for (const auto &res : resources) {
62 query.resources << res.toLatin1();
63 }
64 query.syncOnDemand = false;
65 query.processAll = false;
66 query.liveQuery = false;
67
68 auto model = AkonadishUtils::loadModel(type, query);
69 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
70 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) {
71 state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex())));
72 state.commandFinished();
73 }
74 });
75
76 return true;
77}
78
79}
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "syntaxtree.h"
23
24namespace AkonadiCount
25{
26 Syntax::List syntax();
27 bool count(const QStringList &commands, State &state);
28}
29
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "akonadi_list.h"
21
22#include <QCoreApplication>
23#include <QDebug>
24#include <QObject> // tr()
25#include <QModelIndex>
26#include <QTime>
27
28#include "common/resource.h"
29#include "common/storage.h"
30#include "common/domain/event.h"
31#include "common/domain/folder.h"
32#include "common/resourceconfig.h"
33#include "common/log.h"
34#include "common/storage.h"
35#include "common/definitions.h"
36
37#include "akonadish_utils.h"
38
39namespace AkonadiList
40{
41
42Syntax::List syntax()
43{
44 Syntax::List syntax;
45 syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven);
46
47 return syntax;
48}
49
50bool list(const QStringList &args, State &state)
51{
52 auto resources = args;
53 auto type = !resources.isEmpty() ? resources.takeFirst() : QString();
54
55 if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) {
56 state.printError(QObject::tr("Unknown type: %1").arg(type));
57 return false;
58 }
59
60 Akonadi2::Query query;
61 for (const auto &res : resources) {
62 query.resources << res.toLatin1();
63 }
64 query.syncOnDemand = false;
65 query.processAll = false;
66 query.liveQuery = false;
67
68 QTime time;
69 time.start();
70 auto model = AkonadishUtils::loadModel(type, query);
71 if (state.debugLevel() > 0) {
72 state.printLine(QObject::tr("Folder type %1").arg(type));
73 state.printLine(QObject::tr("Loaded model in %1 ms").arg(time.elapsed()));
74 }
75
76 //qDebug() << "Listing";
77 int colSize = 38; //Necessary to display a complete UUID
78 state.print(" " + QObject::tr("Column") + " ");
79 state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) +
80 QObject::tr("Identifier").leftJustified(colSize, ' ', true));
81 for (int i = 0; i < model->columnCount(QModelIndex()); i++) {
82 state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true));
83 }
84 state.printLine();
85
86 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) {
87 for (int i = start; i <= end; i++) {
88 state.print(" " + QObject::tr("Row %1").arg(QString::number(model->rowCount())).rightJustified(4, ' ') + ": ");
89 auto object = model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr>();
90 state.print(" " + object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true));
91 state.print(object->identifier().leftJustified(colSize, ' ', true));
92 for (int col = 0; col < model->columnCount(QModelIndex()); col++) {
93 state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true));
94 }
95 state.printLine();
96 }
97 });
98
99 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
100 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) {
101 state.commandFinished();
102 }
103 });
104
105 if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) {
106 return true;
107 }
108
109 return false;
110}
111
112}
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "syntaxtree.h"
23
24namespace AkonadiList
25{
26 Syntax::List syntax();
27 bool list(const QStringList &commands, State &state);
28}
29
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "core_syntax.h"
21
22#include <QDebug>
23#include <QObject> // tr()
24#include <QSet>
25#include <QTextStream>
26
27namespace CoreSyntax
28{
29
30Syntax::List syntax()
31{
32 Syntax::List syntax;
33 syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit);
34
35 Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp);
36 help.completer = &CoreSyntax::showHelpCompleter;
37 syntax << help;
38
39 Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session"));
40 set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel);
41 syntax << set;
42
43 Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session"));
44 get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel);
45 syntax << get;
46
47 return syntax;
48}
49
50bool exit(const QStringList &, State &)
51{
52 ::exit(0);
53 return true;
54}
55
56bool showHelp(const QStringList &commands, State &state)
57{
58 SyntaxTree::Command command = SyntaxTree::self()->match(commands);
59 if (commands.isEmpty()) {
60 state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!"));
61 state.printLine(QObject::tr("Top-level commands:"));
62
63 QSet<QString> sorted;
64 for (auto syntax: SyntaxTree::self()->syntax()) {
65 sorted.insert(syntax.keyword);
66 }
67
68 for (auto keyword: sorted) {
69 state.printLine(keyword, 1);
70 }
71 } else if (const Syntax *syntax = command.first) {
72 //TODO: get parent!
73 state.print(QObject::tr("Command `%1`").arg(syntax->keyword));
74
75 if (!syntax->help.isEmpty()) {
76 state.print(": " + syntax->help);
77 }
78 state.printLine();
79
80 if (!syntax->children.isEmpty()) {
81 state.printLine("Sub-commands:", 1);
82 QSet<QString> sorted;
83 for (auto childSyntax: syntax->children) {
84 sorted.insert(childSyntax.keyword);
85 }
86
87 for (auto keyword: sorted) {
88 state.printLine(keyword, 1);
89 }
90 }
91 } else {
92 state.printError("Unknown command: " + commands.join(" "));
93 }
94
95 return true;
96}
97
98QStringList showHelpCompleter(const QStringList &commands, const QString &fragment)
99{
100 QStringList items;
101
102 for (auto syntax: SyntaxTree::self()->syntax()) {
103 if (syntax.keyword != QObject::tr("help") &&
104 (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) {
105 items << syntax.keyword;
106 }
107 }
108
109 qSort(items);
110 return items;
111}
112
113bool setDebugLevel(const QStringList &commands, State &state)
114{
115 if (commands.count() != 1) {
116 state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count()));
117 return false;
118 }
119
120 bool ok = false;
121 int level = commands[0].toUInt(&ok);
122
123 if (!ok) {
124 state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0]));
125 return false;
126 }
127
128 state.setDebugLevel(level);
129 return true;
130}
131
132bool printDebugLevel(const QStringList &commands, State &state)
133{
134 state.printLine(QString::number(state.debugLevel()));
135 return true;
136}
137
138} // namespace CoreSyntax
139
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "syntaxtree.h"
23
24namespace CoreSyntax
25{
26 Syntax::List syntax();
27 bool exit(const QStringList &commands, State &state);
28 bool showHelp(const QStringList &commands, State &state);
29 QStringList showHelpCompleter(const QStringList &commands, const QString &fragment);
30 bool setDebugLevel(const QStringList &commands, State &state);
31 bool printDebugLevel(const QStringList &commands, State &state);
32}
33
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "syntaxtree.h"
21
22#include <QCoreApplication>
23#include <QDebug>
24
25// TODO: needs a proper registry; making "core" modules plugins is
26// almost certainly overkill, but this is not the way either
27#include "syntax_modules/core_syntax.h"
28#include "syntax_modules/akonadi_list.h"
29#include "syntax_modules/akonadi_count.h"
30
31SyntaxTree *SyntaxTree::s_module = 0;
32
33Syntax::Syntax()
34{
35}
36
37Syntax::Syntax(const QString &k, const QString &helpText, std::function<bool(const QStringList &, State &)> l, Interactivity inter)
38 : keyword(k),
39 help(helpText),
40 interactivity(inter),
41 lambda(l)
42{
43}
44
45SyntaxTree::SyntaxTree()
46{
47 QVector<std::function<Syntax::List()> > syntaxSyntaxTrees;
48 syntaxSyntaxTrees << &CoreSyntax::syntax
49 << &AkonadiList::syntax
50 << &AkonadiCount::syntax
51 ;
52 for (auto syntaxSyntaxTree: syntaxSyntaxTrees) {
53 m_syntax += syntaxSyntaxTree();
54 }
55}
56
57SyntaxTree *SyntaxTree::self()
58{
59 if (!s_module) {
60 s_module = new SyntaxTree;
61 }
62
63 return s_module;
64}
65
66Syntax::List SyntaxTree::syntax() const
67{
68 return m_syntax;
69}
70
71bool SyntaxTree::run(const QStringList &commands)
72{
73 Command command = match(commands);
74 if (command.first && command.first->lambda) {
75 bool rv = command.first->lambda(command.second, m_state);
76 if (rv && command.first->interactivity == Syntax::EventDriven) {
77 return m_state.commandStarted();
78 }
79
80 return rv;
81 }
82
83 return false;
84}
85
86SyntaxTree::Command SyntaxTree::match(const QStringList &commandLine) const
87{
88 if (commandLine.isEmpty()) {
89 return Command();
90 }
91
92 QStringListIterator commandLineIt(commandLine);
93
94 QVectorIterator<Syntax> syntaxIt(m_syntax);
95 const Syntax *lastFullSyntax = 0;
96 QStringList tailCommands;
97 while (commandLineIt.hasNext() && syntaxIt.hasNext()) {
98 const QString word = commandLineIt.next();
99 while (syntaxIt.hasNext()) {
100 const Syntax &syntax = syntaxIt.next();
101 if (word == syntax.keyword) {
102 lastFullSyntax = &syntax;
103 syntaxIt = syntax.children;
104 break;
105 }
106 }
107 }
108
109 if (lastFullSyntax) {
110 while (commandLineIt.hasNext()) {
111 tailCommands << commandLineIt.next();
112 }
113
114 return std::make_pair(lastFullSyntax, tailCommands);
115 }
116
117 return Command();
118}
119
120Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString &fragment) const
121{
122 Syntax::List matches;
123
124 //qDebug() << "words are" << words;
125 if (words.isEmpty()) {
126 for (const Syntax &syntax: m_syntax) {
127 if (syntax.keyword.startsWith(fragment)) {
128 matches.push_back(syntax);
129 }
130 }
131 } else {
132 QStringListIterator wordIt(words);
133 QVectorIterator<Syntax> syntaxIt(m_syntax);
134 Syntax lastFullSyntax;
135
136 while (wordIt.hasNext()) {
137 QString word = wordIt.next();
138 while (syntaxIt.hasNext()) {
139 const Syntax &syntax = syntaxIt.next();
140 if (word == syntax.keyword) {
141 lastFullSyntax = syntax;
142 syntaxIt = syntax.children;
143 }
144 }
145 }
146
147 //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last();
148 if (lastFullSyntax.keyword == words.last()) {
149 syntaxIt = lastFullSyntax.children;
150 while (syntaxIt.hasNext()) {
151 Syntax syntax = syntaxIt.next();
152 if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) {
153 matches.push_back(syntax);
154 }
155 }
156 }
157 }
158
159 return matches;
160}
161
162QStringList SyntaxTree::tokenize(const QString &text)
163{
164 //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"']
165 return text.split(" ");
166}
167
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 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include "state.h"
23
24#include <QStringList>
25#include <QVector>
26
27class Syntax
28{
29public:
30 typedef QVector<Syntax> List;
31
32 enum Interactivity {
33 NotInteractive = 0,
34 EventDriven
35 };
36
37 Syntax();
38 Syntax(const QString &keyword,
39 const QString &helpText = QString(),
40 std::function<bool(const QStringList &, State &)> lambda = std::function<bool(const QStringList &, State &)>(),
41 Interactivity interactivity = NotInteractive);
42
43 QString keyword;
44 QString help;
45 Interactivity interactivity;
46 std::function<bool(const QStringList &, State &)> lambda;
47 std::function<QStringList(const QStringList &, const QString &)> completer;
48
49 QVector<Syntax> children;
50};
51
52class SyntaxTree
53{
54public:
55
56 typedef std::pair<const Syntax *, QStringList> Command;
57
58 static SyntaxTree *self();
59
60 Syntax::List syntax() const;
61 Command match(const QStringList &commands) const;
62 Syntax::List nearestSyntax(const QStringList &words, const QString &fragment) const;
63
64 bool run(const QStringList &commands);
65
66 static QStringList tokenize(const QString &text);
67
68private:
69 SyntaxTree();
70
71 Syntax::List m_syntax;
72 State m_state;
73 static SyntaxTree *s_module;
74};
75