summaryrefslogtreecommitdiffstats
path: root/sinksh/syntax_modules/sink_list.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-20 19:07:07 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-20 19:07:07 +0100
commitbdb01c2c068df326f5a8328ed1492ab1bea388c5 (patch)
tree25c2ee1b29bc481b6914c244ed9ca194b1415d16 /sinksh/syntax_modules/sink_list.cpp
parent17e7ee40c9185c0505883853345fd6024c675b1a (diff)
downloadsink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.tar.gz
sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.zip
Renamed Akonadi2 to Sink
(except for documentation).
Diffstat (limited to 'sinksh/syntax_modules/sink_list.cpp')
-rw-r--r--sinksh/syntax_modules/sink_list.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp
new file mode 100644
index 0000000..9712b6f
--- /dev/null
+++ b/sinksh/syntax_modules/sink_list.cpp
@@ -0,0 +1,114 @@
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 "sinksh_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace SinkList
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() && !SinkshUtils::isValidStoreType(type)) {
53 state.printError(QObject::tr("Unknown type: %1").arg(type));
54 return false;
55 }
56
57 Sink::Query query;
58 for (const auto &res : resources) {
59 query.resources << res.toLatin1();
60 }
61 query.liveQuery = false;
62
63 QTime time;
64 time.start();
65 auto model = SinkshUtils::loadModel(type, query);
66 if (state.debugLevel() > 0) {
67 state.printLine(QObject::tr("Folder type %1").arg(type));
68 state.printLine(QObject::tr("Loaded model in %1 ms").arg(time.elapsed()));
69 }
70
71 //qDebug() << "Listing";
72 int colSize = 38; //Necessary to display a complete UUID
73 state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) +
74 QObject::tr("Identifier").leftJustified(colSize, ' ', true));
75 for (int i = 0; i < model->columnCount(QModelIndex()); i++) {
76 state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true));
77 }
78 state.printLine();
79
80 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) {
81 for (int i = start; i <= end; i++) {
82 auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>();
83 state.print(object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true));
84 state.print(object->identifier().leftJustified(colSize, ' ', true));
85 for (int col = 0; col < model->columnCount(QModelIndex()); col++) {
86 state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true));
87 }
88 state.printLine();
89 }
90 });
91
92 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
93 if (roles.contains(Sink::Store::ChildrenFetchedRole)) {
94 state.commandFinished();
95 }
96 });
97
98 if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) {
99 return true;
100 }
101
102 return false;
103}
104
105Syntax::List syntax()
106{
107 Syntax list("list", QObject::tr("List all resources, or the contents of one or more resources"), &SinkList::list, Syntax::EventDriven);
108 list.completer = &SinkshUtils::resourceOrTypeCompleter;
109 return Syntax::List() << list;
110}
111
112REGISTER_SYNTAX(SinkList)
113
114}