diff options
Diffstat (limited to 'akonadish/syntax_modules')
-rw-r--r-- | akonadish/syntax_modules/akonadi_count.cpp | 79 | ||||
-rw-r--r-- | akonadish/syntax_modules/akonadi_count.h | 29 | ||||
-rw-r--r-- | akonadish/syntax_modules/akonadi_list.cpp | 112 | ||||
-rw-r--r-- | akonadish/syntax_modules/akonadi_list.h | 29 | ||||
-rw-r--r-- | akonadish/syntax_modules/core_syntax.cpp | 139 | ||||
-rw-r--r-- | akonadish/syntax_modules/core_syntax.h | 33 |
6 files changed, 421 insertions, 0 deletions
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 | |||
39 | namespace AkonadiCount | ||
40 | { | ||
41 | |||
42 | Syntax::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 | |||
50 | bool 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 | |||
24 | namespace 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 | |||
39 | namespace AkonadiList | ||
40 | { | ||
41 | |||
42 | Syntax::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 | |||
50 | bool 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 | |||
24 | namespace 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 | |||
27 | namespace CoreSyntax | ||
28 | { | ||
29 | |||
30 | Syntax::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 | |||
50 | bool exit(const QStringList &, State &) | ||
51 | { | ||
52 | ::exit(0); | ||
53 | return true; | ||
54 | } | ||
55 | |||
56 | bool 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 | |||
98 | QStringList 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 | |||
113 | bool 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 | |||
132 | bool 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 | |||
24 | namespace 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 | |||