summaryrefslogtreecommitdiffstats
path: root/akonadish/syntax_modules
diff options
context:
space:
mode:
Diffstat (limited to 'akonadish/syntax_modules')
-rw-r--r--akonadish/syntax_modules/akonadi_clear.cpp61
-rw-r--r--akonadish/syntax_modules/akonadi_count.cpp81
-rw-r--r--akonadish/syntax_modules/akonadi_create.cpp118
-rw-r--r--akonadish/syntax_modules/akonadi_list.cpp119
-rw-r--r--akonadish/syntax_modules/akonadi_modify.cpp121
-rw-r--r--akonadish/syntax_modules/akonadi_remove.cpp111
-rw-r--r--akonadish/syntax_modules/akonadi_stat.cpp113
-rw-r--r--akonadish/syntax_modules/akonadi_sync.cpp69
-rw-r--r--akonadish/syntax_modules/core_syntax.cpp178
9 files changed, 971 insertions, 0 deletions
diff --git a/akonadish/syntax_modules/akonadi_clear.cpp b/akonadish/syntax_modules/akonadi_clear.cpp
new file mode 100644
index 0000000..d17fac2
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_clear.cpp
@@ -0,0 +1,61 @@
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 <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23
24#include "common/resource.h"
25#include "common/storage.h"
26#include "common/domain/event.h"
27#include "common/domain/folder.h"
28#include "common/resourceconfig.h"
29#include "common/log.h"
30#include "common/storage.h"
31#include "common/definitions.h"
32
33#include "akonadish_utils.h"
34#include "state.h"
35#include "syntaxtree.h"
36
37namespace AkonadiClear
38{
39
40bool clear(const QStringList &args, State &state)
41{
42 for (const auto &resource : args) {
43 state.print(QObject::tr("Removing local cache for '%1' ...").arg(resource));
44 Akonadi2::Store::removeFromDisk(resource.toLatin1());
45 state.printLine(QObject::tr("done"));
46 }
47
48 return true;
49}
50
51Syntax::List syntax()
52{
53 Syntax::List syntax;
54 syntax << Syntax("clear", QObject::tr("Clears the local cache of one or more resources (be careful!)"), &AkonadiClear::clear);
55
56 return syntax;
57}
58
59REGISTER_SYNTAX(AkonadiClear)
60
61}
diff --git a/akonadish/syntax_modules/akonadi_count.cpp b/akonadish/syntax_modules/akonadi_count.cpp
new file mode 100644
index 0000000..70aabc9
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_count.cpp
@@ -0,0 +1,81 @@
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 <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "akonadish_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace AkonadiCount
40{
41
42bool count(const QStringList &args, State &state)
43{
44 auto resources = args;
45 auto type = !resources.isEmpty() ? resources.takeFirst() : QString();
46
47 if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) {
48 state.printError(QObject::tr("Unknown type: %1").arg(type));
49 return false;
50 }
51
52 Akonadi2::Query query;
53 for (const auto &res : resources) {
54 query.resources << res.toLatin1();
55 }
56 query.syncOnDemand = false;
57 query.processAll = false;
58 query.liveQuery = false;
59
60 auto model = AkonadishUtils::loadModel(type, query);
61 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
62 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) {
63 state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex())));
64 state.commandFinished();
65 }
66 });
67
68 return true;
69}
70
71Syntax::List syntax()
72{
73 Syntax::List syntax;
74 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);
75
76 return syntax;
77}
78
79REGISTER_SYNTAX(AkonadiCount)
80
81}
diff --git a/akonadish/syntax_modules/akonadi_create.cpp b/akonadish/syntax_modules/akonadi_create.cpp
new file mode 100644
index 0000000..377219a
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_create.cpp
@@ -0,0 +1,118 @@
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 <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "akonadish_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace AkonadiCreate
40{
41
42bool create(const QStringList &allArgs, State &state)
43{
44 if (allArgs.isEmpty()) {
45 state.printError(QObject::tr("A type is required"), "akonadicreate/02");
46 return false;
47 }
48
49 if (allArgs.count() < 2) {
50 state.printError(QObject::tr("A resource ID is required to create items"), "akonadicreate/03");
51 return false;
52 }
53
54 auto args = allArgs;
55 auto type = args.takeFirst();
56 auto &store = AkonadishUtils::getStore(type);
57 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object;
58 auto resource = args.takeFirst().toLatin1();
59 object = store.getObject(resource);
60
61 auto map = AkonadishUtils::keyValueMapFromArgs(args);
62 for (auto i = map.begin(); i != map.end(); ++i) {
63 object->setProperty(i.key().toLatin1(), i.value());
64 }
65
66 auto result = store.create(*object).exec();
67 result.waitForFinished();
68 if (result.errorCode()) {
69 state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
70 "akonaid_create_e" + QString::number(result.errorCode()));
71 }
72
73 return true;
74}
75
76bool resource(const QStringList &args, State &state)
77{
78 if (args.isEmpty()) {
79 state.printError(QObject::tr("A resource can not be created without a type"), "akonadicreate/01");
80 return false;
81 }
82
83 auto &store = AkonadishUtils::getStore("resource");
84
85 auto resourceType = args.at(0);
86 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("");
87 object->setProperty("type", resourceType);
88
89 auto map = AkonadishUtils::keyValueMapFromArgs(args);
90 for (auto i = map.begin(); i != map.end(); ++i) {
91 object->setProperty(i.key().toLatin1(), i.value());
92 }
93
94 auto result = store.create(*object).exec();
95 result.waitForFinished();
96 if (result.errorCode()) {
97 state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
98 "akonaid_create_e" + QString::number(result.errorCode()));
99 }
100
101 return true;
102}
103
104
105Syntax::List syntax()
106{
107 Syntax::List syntax;
108
109 Syntax create("create", QObject::tr("Create items in a resource"), &AkonadiCreate::create);
110 create.children << Syntax("resource", QObject::tr("Creates a new resource"), &AkonadiCreate::resource);
111
112 syntax << create;
113 return syntax;
114}
115
116REGISTER_SYNTAX(AkonadiCreate)
117
118}
diff --git a/akonadish/syntax_modules/akonadi_list.cpp b/akonadish/syntax_modules/akonadi_list.cpp
new file mode 100644
index 0000000..807119c
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_list.cpp
@@ -0,0 +1,119 @@
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 <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "akonadish_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace AkonadiList
40{
41
42bool list(const QStringList &args, State &state)
43{
44 if (args.isEmpty()) {
45 state.printError(QObject::tr("Please provide at least one type to list (e.g. resource, .."));
46 return false;
47 }
48
49 auto resources = args;
50 auto type = !resources.isEmpty() ? resources.takeFirst() : QString();
51
52 if (!type.isEmpty() && !AkonadishUtils::isValidStoreType(type)) {
53 state.printError(QObject::tr("Unknown type: %1").arg(type));
54 return false;
55 }
56
57 Akonadi2::Query query;
58 for (const auto &res : resources) {
59 query.resources << res.toLatin1();
60 }
61 query.syncOnDemand = false;
62 query.processAll = false;
63 query.liveQuery = false;
64
65 QTime time;
66 time.start();
67 auto model = AkonadishUtils::loadModel(type, query);
68 if (state.debugLevel() > 0) {
69 state.printLine(QObject::tr("Folder type %1").arg(type));
70 state.printLine(QObject::tr("Loaded model in %1 ms").arg(time.elapsed()));
71 }
72
73 //qDebug() << "Listing";
74 int colSize = 38; //Necessary to display a complete UUID
75 state.print(" " + QObject::tr("Column") + " ");
76 state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) +
77 QObject::tr("Identifier").leftJustified(colSize, ' ', true));
78 for (int i = 0; i < model->columnCount(QModelIndex()); i++) {
79 state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true));
80 }
81 state.printLine();
82
83 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) {
84 for (int i = start; i <= end; i++) {
85 state.print(" " + QObject::tr("Row %1").arg(QString::number(model->rowCount())).rightJustified(4, ' ') + ": ");
86 auto object = model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr>();
87 state.print(" " + object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true));
88 state.print(object->identifier().leftJustified(colSize, ' ', true));
89 for (int col = 0; col < model->columnCount(QModelIndex()); col++) {
90 state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true));
91 }
92 state.printLine();
93 }
94 });
95
96 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
97 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) {
98 state.commandFinished();
99 }
100 });
101
102 if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) {
103 return true;
104 }
105
106 return false;
107}
108
109Syntax::List syntax()
110{
111 Syntax::List syntax;
112 syntax << Syntax("list", QObject::tr("List all resources, or the contents of one or more resources"), &AkonadiList::list, Syntax::EventDriven);
113
114 return syntax;
115}
116
117REGISTER_SYNTAX(AkonadiList)
118
119}
diff --git a/akonadish/syntax_modules/akonadi_modify.cpp b/akonadish/syntax_modules/akonadi_modify.cpp
new file mode 100644
index 0000000..8438301
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_modify.cpp
@@ -0,0 +1,121 @@
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 <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "akonadish_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace AkonadiModify
40{
41
42bool modify(const QStringList &args, State &state)
43{
44 if (args.isEmpty()) {
45 state.printError(QObject::tr("A type is required"), "akonadi_modify/02");
46 return false;
47 }
48
49 if (args.count() < 2) {
50 state.printError(QObject::tr("A resource ID is required to remove items"), "akonadi_modify/03");
51 return false;
52 }
53
54 if (args.count() < 3) {
55 state.printError(QObject::tr("An object ID is required to remove items"), "akonadi_modify/03");
56 return false;
57 }
58
59 auto type = args[0];
60 auto resourceId = args[1];
61 auto identifier = args[2];
62
63 auto &store = AkonadishUtils::getStore(type);
64 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8());
65
66 auto map = AkonadishUtils::keyValueMapFromArgs(args);
67 for (auto i = map.begin(); i != map.end(); ++i) {
68 object->setProperty(i.key().toLatin1(), i.value());
69 }
70
71 auto result = store.modify(*object).exec();
72 result.waitForFinished();
73 if (result.errorCode()) {
74 state.printError(QObject::tr("An error occurred while removing %1 from %1: %2").arg(identifier).arg(resourceId).arg(result.errorMessage()),
75 "akonaid__modify_e" + QString::number(result.errorCode()));
76 }
77
78 return true;
79}
80
81bool resource(const QStringList &args, State &state)
82{
83 if (args.isEmpty()) {
84 state.printError(QObject::tr("A resource can not be modified without an id"), "akonadi_modify/01");
85 }
86
87 auto &store = AkonadishUtils::getStore("resource");
88
89 auto resourceId = args.at(0);
90 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1());
91
92 auto map = AkonadishUtils::keyValueMapFromArgs(args);
93 for (auto i = map.begin(); i != map.end(); ++i) {
94 object->setProperty(i.key().toLatin1(), i.value());
95 }
96
97 auto result = store.modify(*object).exec();
98 result.waitForFinished();
99 if (result.errorCode()) {
100 state.printError(QObject::tr("An error occurred while modifying the resource %1: %2").arg(resourceId).arg(result.errorMessage()),
101 "akonaid_modify_e" + QString::number(result.errorCode()));
102 }
103
104 return true;
105}
106
107
108Syntax::List syntax()
109{
110 Syntax::List syntax;
111
112 Syntax modify("modify", QObject::tr("Modify items in a resource"), &AkonadiModify::modify);
113 modify.children << Syntax("resource", QObject::tr("Modify a resource"), &AkonadiModify::resource);//, Syntax::EventDriven);
114
115 syntax << modify;
116 return syntax;
117}
118
119REGISTER_SYNTAX(AkonadiModify)
120
121}
diff --git a/akonadish/syntax_modules/akonadi_remove.cpp b/akonadish/syntax_modules/akonadi_remove.cpp
new file mode 100644
index 0000000..bf09e2e
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_remove.cpp
@@ -0,0 +1,111 @@
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 <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "akonadish_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace AkonadiRemove
40{
41
42bool remove(const QStringList &args, State &state)
43{
44 if (args.isEmpty()) {
45 state.printError(QObject::tr("A type is required"), "akonadi_remove/02");
46 return false;
47 }
48
49 if (args.count() < 2) {
50 state.printError(QObject::tr("A resource ID is required to remove items"), "akonadi_remove/03");
51 return false;
52 }
53
54 if (args.count() < 3) {
55 state.printError(QObject::tr("An object ID is required to remove items"), "akonadi_remove/03");
56 return false;
57 }
58
59 auto type = args[0];
60 auto resourceId = args[1];
61 auto identifier = args[2];
62
63 auto &store = AkonadishUtils::getStore(type);
64 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8());
65
66 auto result = store.remove(*object).exec();
67 result.waitForFinished();
68 if (result.errorCode()) {
69 state.printError(QObject::tr("An error occurred while removing %1 from %1: %2").arg(identifier).arg(resourceId).arg(result.errorMessage()),
70 "akonaid_remove_e" + QString::number(result.errorCode()));
71 }
72
73 return true;
74}
75
76bool resource(const QStringList &args, State &state)
77{
78 if (args.isEmpty()) {
79 state.printError(QObject::tr("A resource can not be removed without an id"), "akonadi_remove/01");
80 }
81
82 auto &store = AkonadishUtils::getStore("resource");
83
84 auto resourceId = args.at(0);
85 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1());
86
87 auto result = store.remove(*object).exec();
88 result.waitForFinished();
89 if (result.errorCode()) {
90 state.printError(QObject::tr("An error occurred while removing the resource %1: %2").arg(resourceId).arg(result.errorMessage()),
91 "akonaid_remove_e" + QString::number(result.errorCode()));
92 }
93
94 return true;
95}
96
97
98Syntax::List syntax()
99{
100 Syntax::List syntax;
101
102 Syntax remove("remove", QObject::tr("Remove items in a resource"), &AkonadiRemove::remove);
103 remove.children << Syntax("resource", QObject::tr("Removes a resource"), &AkonadiRemove::resource);//, Syntax::EventDriven);
104
105 syntax << remove;
106 return syntax;
107}
108
109REGISTER_SYNTAX(AkonadiRemove)
110
111}
diff --git a/akonadish/syntax_modules/akonadi_stat.cpp b/akonadish/syntax_modules/akonadi_stat.cpp
new file mode 100644
index 0000000..149ccbd
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_stat.cpp
@@ -0,0 +1,113 @@
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 <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23
24#include "common/resource.h"
25#include "common/storage.h"
26#include "common/domain/event.h"
27#include "common/domain/folder.h"
28#include "common/resourceconfig.h"
29#include "common/log.h"
30#include "common/storage.h"
31#include "common/definitions.h"
32
33#include "akonadish_utils.h"
34#include "state.h"
35#include "syntaxtree.h"
36
37namespace AkonadiStat
38{
39
40void statResources(const QStringList &resources, const State &state)
41{
42 qint64 total = 0;
43 for (const auto &resource : resources) {
44 Akonadi2::Storage storage(Akonadi2::storageLocation(), resource, Akonadi2::Storage::ReadOnly);
45 auto transaction = storage.createTransaction(Akonadi2::Storage::ReadOnly);
46
47 QList<QByteArray> databases = transaction.getDatabaseNames();
48 for (const auto &databaseName : databases) {
49 state.printLine(QObject::tr("Database: %1").arg(QString(databaseName)), 1);
50 auto db = transaction.openDatabase(databaseName);
51 qint64 size = db.getSize() / 1024;
52 state.printLine(QObject::tr("Size [kb]: %1").arg(size), 1);
53 total += size;
54 }
55 }
56
57 state.printLine(QObject::tr("Total [kb]: %1").arg(total));
58}
59
60bool statAllResources(State &state)
61{
62 Akonadi2::Query query;
63 query.syncOnDemand = false;
64 query.processAll = false;
65 query.liveQuery = false;
66 auto model = AkonadishUtils::loadModel("resource", query);
67
68 //SUUUPER ugly, but can't think of a better way with 2 glasses of wine in me on Christmas day
69 static QStringList resources;
70 resources.clear();
71
72 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) mutable {
73 for (int i = start; i <= end; i++) {
74 auto object = model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr>();
75 resources << object->identifier();
76 }
77 });
78
79 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
80 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) {
81 statResources(resources, state);
82 state.commandFinished();
83 }
84 });
85
86 if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) {
87 return true;
88 }
89
90 return false;
91}
92
93bool stat(const QStringList &args, State &state)
94{
95 if (args.isEmpty()) {
96 return statAllResources(state);
97 }
98
99 statResources(args, state);
100 return false;
101}
102
103Syntax::List syntax()
104{
105 Syntax::List syntax;
106 syntax << Syntax("stat", QObject::tr("Shows database usage for the resources requested"), &AkonadiStat::stat, Syntax::EventDriven);
107
108 return syntax;
109}
110
111REGISTER_SYNTAX(AkonadiStat)
112
113}
diff --git a/akonadish/syntax_modules/akonadi_sync.cpp b/akonadish/syntax_modules/akonadi_sync.cpp
new file mode 100644
index 0000000..1cf097d
--- /dev/null
+++ b/akonadish/syntax_modules/akonadi_sync.cpp
@@ -0,0 +1,69 @@
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 <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23
24#include "common/resource.h"
25#include "common/storage.h"
26#include "common/domain/event.h"
27#include "common/domain/folder.h"
28#include "common/resourceconfig.h"
29#include "common/log.h"
30#include "common/storage.h"
31#include "common/definitions.h"
32
33#include "akonadish_utils.h"
34#include "state.h"
35#include "syntaxtree.h"
36
37namespace AkonadiSync
38{
39
40bool sync(const QStringList &args, State &state)
41{
42 Akonadi2::Query query;
43 for (const auto &res : args) {
44 query.resources << res.toLatin1();
45 }
46 query.syncOnDemand = true;
47 query.processAll = true;
48
49 QTimer::singleShot(0, [query, state]() {
50 Akonadi2::Store::synchronize(query).then<void>([state]() {
51 state.printLine("Synchronization complete!");
52 state.commandFinished();
53 }).exec();
54 });
55
56 return true;
57}
58
59Syntax::List syntax()
60{
61 Syntax::List syntax;
62 syntax << Syntax("sync", QObject::tr("Syncronizes all resources that are listed; and empty list triggers a syncronizaton on all resources"), &AkonadiSync::sync, Syntax::EventDriven );
63
64 return syntax;
65}
66
67REGISTER_SYNTAX(AkonadiSync)
68
69}
diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp
new file mode 100644
index 0000000..31b824a
--- /dev/null
+++ b/akonadish/syntax_modules/core_syntax.cpp
@@ -0,0 +1,178 @@
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 <QDebug>
21#include <QObject> // tr()
22#include <QSet>
23#include <QTextStream>
24
25#include "state.h"
26#include "syntaxtree.h"
27
28namespace CoreSyntax
29{
30
31bool exit(const QStringList &, State &)
32{
33 ::exit(0);
34 return true;
35}
36
37bool showHelp(const QStringList &commands, State &state)
38{
39 SyntaxTree::Command command = SyntaxTree::self()->match(commands);
40 if (commands.isEmpty()) {
41 state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!"));
42 state.printLine(QObject::tr("Top-level commands:"));
43
44 QSet<QString> sorted;
45 for (auto syntax: SyntaxTree::self()->syntax()) {
46 sorted.insert(syntax.keyword);
47 }
48
49 for (auto keyword: sorted) {
50 state.printLine(keyword, 1);
51 }
52 } else if (const Syntax *syntax = command.first) {
53 //TODO: get parent!
54 state.print(QObject::tr("Command `%1`").arg(syntax->keyword));
55
56 if (!syntax->help.isEmpty()) {
57 state.print(": " + syntax->help);
58 }
59 state.printLine();
60
61 if (!syntax->children.isEmpty()) {
62 state.printLine("Sub-commands:", 1);
63 QSet<QString> sorted;
64 for (auto childSyntax: syntax->children) {
65 sorted.insert(childSyntax.keyword);
66 }
67
68 for (auto keyword: sorted) {
69 state.printLine(keyword, 1);
70 }
71 }
72 } else {
73 state.printError("Unknown command: " + commands.join(" "));
74 }
75
76 return true;
77}
78
79QStringList showHelpCompleter(const QStringList &commands, const QString &fragment)
80{
81 QStringList items;
82
83 for (auto syntax: SyntaxTree::self()->syntax()) {
84 if (syntax.keyword != QObject::tr("help") &&
85 (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) {
86 items << syntax.keyword;
87 }
88 }
89
90 qSort(items);
91 return items;
92}
93
94bool setDebugLevel(const QStringList &commands, State &state)
95{
96 if (commands.count() != 1) {
97 state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count()));
98 return false;
99 }
100
101 bool ok = false;
102 int level = commands[0].toUInt(&ok);
103
104 if (!ok) {
105 state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0]));
106 return false;
107 }
108
109 state.setDebugLevel(level);
110 return true;
111}
112
113bool printDebugLevel(const QStringList &, State &state)
114{
115 state.printLine(QString::number(state.debugLevel()));
116 return true;
117}
118
119bool printCommandTiming(const QStringList &, State &state)
120{
121 state.printLine(state.commandTiming() ? QObject::tr("on") : QObject::tr("off"));
122 return true;
123}
124
125void printSyntaxBranch(State &state, const Syntax::List &list, int depth)
126{
127 if (list.isEmpty()) {
128 return;
129 }
130
131 if (depth > 0) {
132 state.printLine("\\", depth);
133 }
134
135 for (auto syntax: list) {
136 state.print("|-", depth);
137 state.printLine(syntax.keyword);
138 printSyntaxBranch(state, syntax.children, depth + 1);
139 }
140}
141
142bool printSyntaxTree(const QStringList &, State &state)
143{
144 printSyntaxBranch(state, SyntaxTree::self()->syntax(), 0);
145 return true;
146}
147
148Syntax::List syntax()
149{
150 Syntax::List syntax;
151 syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit);
152
153 Syntax help("help", QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp);
154 help.completer = &CoreSyntax::showHelpCompleter;
155 syntax << help;
156
157 syntax << Syntax("syntaxtree", QString(), &printSyntaxTree);
158
159 Syntax set("set", QObject::tr("Sets settings for the session"));
160 set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel);
161 Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"));
162 setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; });
163 setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; });
164 set.children << setTiming;
165 syntax << set;
166
167 Syntax get("get", QObject::tr("Gets settings for the session"));
168 get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel);
169 get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming);
170 syntax << get;
171
172 return syntax;
173}
174
175REGISTER_SYNTAX(CoreSyntax)
176
177} // namespace CoreSyntax
178