summaryrefslogtreecommitdiffstats
path: root/examples/client/main.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-21 09:57:33 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-21 09:57:33 +0200
commit98a057260f51b8af2cf3f933119e08590cc0639b (patch)
tree56a27c8aeafac9a16f97fff86d7b4cca24380ee2 /examples/client/main.cpp
parent830de76043696c591009d763bffcd38d5039b0d2 (diff)
downloadsink-98a057260f51b8af2cf3f933119e08590cc0639b.tar.gz
sink-98a057260f51b8af2cf3f933119e08590cc0639b.zip
Moved the ListModelResult to a separate file
Diffstat (limited to 'examples/client/main.cpp')
-rw-r--r--examples/client/main.cpp84
1 files changed, 2 insertions, 82 deletions
diff --git a/examples/client/main.cpp b/examples/client/main.cpp
index e96ddb6..269f1aa 100644
--- a/examples/client/main.cpp
+++ b/examples/client/main.cpp
@@ -20,12 +20,10 @@
20#include <QApplication> 20#include <QApplication>
21#include <QCommandLineParser> 21#include <QCommandLineParser>
22#include <QCommandLineOption> 22#include <QCommandLineOption>
23#include <QAbstractListModel>
24 23
25#include "common/clientapi.h" 24#include "common/clientapi.h"
26#include "common/resultprovider.h"
27#include "common/resource.h" 25#include "common/resource.h"
28#include "common/synclistresult.h" 26#include "common/listmodelresult.h"
29#include "common/storage.h" 27#include "common/storage.h"
30#include "common/domain/event.h" 28#include "common/domain/event.h"
31#include "console.h" 29#include "console.h"
@@ -37,10 +35,6 @@
37#include <QPushButton> 35#include <QPushButton>
38#include <QItemSelectionModel> 36#include <QItemSelectionModel>
39 37
40enum Roles {
41 DomainObjectRole = Qt::UserRole + 1
42};
43
44template <typename T> 38template <typename T>
45class View : public QWidget 39class View : public QWidget
46{ 40{
@@ -85,80 +79,6 @@ public:
85 79
86}; 80};
87 81
88template<class T>
89class AkonadiListModel : public QAbstractListModel
90{
91public:
92 AkonadiListModel(const QSharedPointer<Akonadi2::ResultEmitter<T> > &emitter, const QByteArray &property)
93 :QAbstractListModel(),
94 mEmitter(emitter),
95 mProperty(property)
96 {
97 emitter->onAdded([this, property](const T &value) {
98 const auto keys = mEntities.keys();
99 int index = 0;
100 for (; index < keys.size(); index++) {
101 if (value->identifier() < keys.at(index)) {
102 break;
103 }
104 }
105 beginInsertRows(QModelIndex(), index, index);
106 mEntities.insert(value->identifier(), value);
107 endInsertRows();
108 });
109 emitter->onModified([this, property](const T &value) {
110 mEntities.remove(value->identifier());
111 mEntities.insert(value->identifier(), value);
112 //FIXME
113 // emit dataChanged();
114 });
115 emitter->onRemoved([this, property](const T &value) {
116 auto index = mEntities.keys().indexOf(value->identifier());
117 beginRemoveRows(QModelIndex(), index, index);
118 mEntities.remove(value->identifier());
119 endRemoveRows();
120 });
121 emitter->onInitialResultSetComplete([this]() {
122 });
123 emitter->onComplete([this]() {
124 // qDebug() << "COMPLETE";
125 mEmitter.clear();
126 });
127 emitter->onClear([this]() {
128 // qDebug() << "CLEAR";
129 beginResetModel();
130 mEntities.clear();
131 endResetModel();
132 });
133 }
134
135 int rowCount(const QModelIndex &parent = QModelIndex()) const
136 {
137 return mEntities.size();
138 }
139
140 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
141 {
142 if (index.row() >= mEntities.size()) {
143 qWarning() << "Out of bounds access";
144 return QVariant();
145 }
146 if (role == Qt::DisplayRole) {
147 auto entity = mEntities.value(mEntities.keys().at(index.row()));
148 return entity->getProperty(mProperty).toString() + entity->identifier();
149 }
150 if (role == DomainObjectRole) {
151 return QVariant::fromValue(mEntities.value(mEntities.keys().at(index.row())));
152 }
153 return QVariant();
154 }
155
156private:
157 QSharedPointer<Akonadi2::ResultEmitter<T> > mEmitter;
158 QMap<QByteArray, T> mEntities;
159 QByteArray mProperty;
160};
161
162int main(int argc, char *argv[]) 82int main(int argc, char *argv[])
163{ 83{
164 QApplication app(argc, argv); 84 QApplication app(argc, argv);
@@ -201,7 +121,7 @@ int main(int argc, char *argv[])
201 query.processAll = false; 121 query.processAll = false;
202 query.liveQuery = true; 122 query.liveQuery = true;
203 123
204 auto model = QSharedPointer<AkonadiListModel<Akonadi2::ApplicationDomain::Event::Ptr> >::create(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query), "summary"); 124 auto model = QSharedPointer<ListModelResult<Akonadi2::ApplicationDomain::Event::Ptr> >::create(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query), "summary");
205 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data()); 125 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data());
206 126
207 return app.exec(); 127 return app.exec();