diff options
Diffstat (limited to 'akonadish/syntax_modules/akonadi_list.cpp')
-rw-r--r-- | akonadish/syntax_modules/akonadi_list.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
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 | } | ||