summaryrefslogtreecommitdiffstats
path: root/akonadi2_cli/module.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'akonadi2_cli/module.cpp')
-rw-r--r--akonadi2_cli/module.cpp195
1 files changed, 195 insertions, 0 deletions
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 @@
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 "module.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 "modules/exit/exit.h"
28#include "modules/help/help.h"
29
30QList<Module> Module::s_modules;
31State Module::s_state;
32
33Module::Syntax::Syntax()
34{
35}
36
37Module::Syntax::Syntax(const QString &k, std::function<bool(const QStringList &, State &)> l, const QString &helpText, bool e)
38 : keyword(k),
39 lambda(l),
40 help(helpText),
41 eventDriven(e)
42{
43}
44
45Module::Module()
46{
47}
48
49void Module::loadModules()
50{
51 addModule(CLI::Exit());
52 addModule(CLI::Help());
53}
54
55void Module::addModule(const Module &module)
56{
57 s_modules.append(module);
58}
59
60QList<Module> Module::modules()
61{
62 return s_modules;
63}
64
65Module::Command Module::match(const QStringList &commands)
66{
67 Command command;
68 for (const Module &module: s_modules) {
69 command = module.matches(commands);
70 if (command.first) {
71 break;
72 }
73 }
74
75 return command;
76}
77
78Module::Syntax Module::syntax() const
79{
80 return m_syntax;
81}
82
83void Module::setSyntax(const Syntax &syntax)
84{
85 m_syntax = syntax;
86}
87
88bool Module::run(const QStringList &commands)
89{
90 Command command = match(commands);
91 if (command.first && command.first->lambda) {
92 bool rv = command.first->lambda(command.second, s_state);
93 if (rv && command.first->eventDriven) {
94 return QCoreApplication::instance()->exec();
95 }
96
97 return rv;
98 }
99
100 return false;
101}
102
103Module::Command Module::matches(const QStringList &commandLine) const
104{
105 if (commandLine.isEmpty()) {
106 return Command();
107 }
108
109 QStringListIterator commandLineIt(commandLine);
110
111 if (commandLineIt.next() != m_syntax.keyword) {
112 return Command();
113 }
114
115 QListIterator<Syntax> syntaxIt(m_syntax.children);
116 const Syntax *syntax = &m_syntax;
117 QStringList tailCommands;
118 while (commandLineIt.hasNext() && syntaxIt.hasNext()) {
119 const QString word = commandLineIt.next();
120 while (syntaxIt.hasNext()) {
121 const Syntax &child = syntaxIt.next();
122 if (word == child.keyword) {
123 syntax = &child;
124 syntaxIt = child.children;
125 }
126 }
127
128 if (!syntaxIt.hasNext()) {
129 tailCommands << word;
130 break;
131 }
132 }
133
134 if (syntax && syntax->lambda) {
135 while (commandLineIt.hasNext()) {
136 tailCommands << commandLineIt.next();
137 }
138
139 return std::make_pair(syntax, tailCommands);
140 }
141
142 return Command();
143}
144
145QVector<Module::Syntax> Module::nearestSyntax(const QStringList &words, const QString &fragment)
146{
147 QVector<Module::Syntax> matches;
148
149 //qDebug() << "words are" << words;
150 if (words.isEmpty()) {
151 for (const Module &module: s_modules) {
152 if (module.syntax().keyword.startsWith(fragment)) {
153 matches.push_back(module.syntax());
154 }
155 }
156 } else {
157 QStringListIterator wordIt(words);
158 QString word = wordIt.next();
159 Syntax lastFullSyntax;
160
161 for (const Module &module: s_modules) {
162 if (module.syntax().keyword == word) {
163 lastFullSyntax = module.syntax();
164 QListIterator<Syntax> syntaxIt(module.syntax().children);
165 while (wordIt.hasNext()) {
166 word = wordIt.next();
167 while (syntaxIt.hasNext()) {
168 const Syntax &child = syntaxIt.next();
169 if (word == child.keyword) {
170 lastFullSyntax = child;
171 syntaxIt = child.children;
172 }
173 }
174 }
175
176 break;
177 }
178 }
179
180 //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last();
181 if (lastFullSyntax.keyword == words.last()) {
182 QListIterator<Syntax> syntaxIt(lastFullSyntax.children);
183 while (syntaxIt.hasNext()) {
184 Syntax syntax = syntaxIt.next();
185 if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) {
186 matches.push_back(syntax);
187 }
188 }
189 }
190 }
191
192 return matches;
193}
194
195