diff options
-rw-r--r-- | framework/mail/CMakeLists.txt | 1 | ||||
-rw-r--r-- | framework/mail/mailplugin.cpp | 2 | ||||
-rw-r--r-- | framework/mail/retriever.cpp | 55 | ||||
-rw-r--r-- | framework/mail/retriever.h | 55 |
4 files changed, 113 insertions, 0 deletions
diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt index 54a031cc..29fda0e4 100644 --- a/framework/mail/CMakeLists.txt +++ b/framework/mail/CMakeLists.txt | |||
@@ -13,6 +13,7 @@ set(mailplugin_SRCS | |||
13 | composer.cpp | 13 | composer.cpp |
14 | messageparser.cpp | 14 | messageparser.cpp |
15 | mailtransport.cpp | 15 | mailtransport.cpp |
16 | retriever.cpp | ||
16 | ) | 17 | ) |
17 | add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") | 18 | add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") |
18 | 19 | ||
diff --git a/framework/mail/mailplugin.cpp b/framework/mail/mailplugin.cpp index 750c6f8e..2d19a437 100644 --- a/framework/mail/mailplugin.cpp +++ b/framework/mail/mailplugin.cpp | |||
@@ -24,6 +24,7 @@ | |||
24 | #include "folderlistmodel.h" | 24 | #include "folderlistmodel.h" |
25 | #include "composer.h" | 25 | #include "composer.h" |
26 | #include "messageparser.h" | 26 | #include "messageparser.h" |
27 | #include "retriever.h" | ||
27 | 28 | ||
28 | #include <QtQml> | 29 | #include <QtQml> |
29 | 30 | ||
@@ -35,4 +36,5 @@ void MailPlugin::registerTypes (const char *uri) | |||
35 | qmlRegisterType<MailListModel>(uri, 1, 0, "MailListModel"); | 36 | qmlRegisterType<MailListModel>(uri, 1, 0, "MailListModel"); |
36 | qmlRegisterType<Composer>(uri, 1, 0, "Composer"); | 37 | qmlRegisterType<Composer>(uri, 1, 0, "Composer"); |
37 | qmlRegisterType<MessageParser>(uri, 1, 0, "MessageParser"); | 38 | qmlRegisterType<MessageParser>(uri, 1, 0, "MessageParser"); |
39 | qmlRegisterType<Retriever>(uri, 1, 0, "Retriever"); | ||
38 | } | 40 | } |
diff --git a/framework/mail/retriever.cpp b/framework/mail/retriever.cpp new file mode 100644 index 00000000..b8e29523 --- /dev/null +++ b/framework/mail/retriever.cpp | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsystems.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #include "retriever.h" | ||
22 | |||
23 | Retriever::Retriever(QObject *parent) : QObject(parent) | ||
24 | { | ||
25 | } | ||
26 | |||
27 | QAbstractItemModel* Retriever::model() const | ||
28 | { | ||
29 | return mModel; | ||
30 | } | ||
31 | |||
32 | void Retriever::setModel(QAbstractItemModel* model) | ||
33 | { | ||
34 | mValue = QVariant(); | ||
35 | mModel = model; | ||
36 | connect(model, &QAbstractItemModel::rowsInserted, this, &Retriever::onRowsInserted); | ||
37 | connect(model, &QAbstractItemModel::modelReset, this, &Retriever::onModelReset); | ||
38 | if (model->rowCount(QModelIndex())) { | ||
39 | mValue = model->index(0, 0, QModelIndex()).data(mModel->roleNames().key(mPropertyName.toLatin1())); | ||
40 | emit valueChanged(); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | void Retriever::onRowsInserted(const QModelIndex &parent, int first, int last) | ||
45 | { | ||
46 | if (!mValue.isValid()) { | ||
47 | mValue = mModel->index(0, 0, QModelIndex()).data(mModel->roleNames().key(mPropertyName.toLatin1())); | ||
48 | emit valueChanged(); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | void Retriever::onModelReset() | ||
53 | { | ||
54 | mValue = QVariant(); | ||
55 | } | ||
diff --git a/framework/mail/retriever.h b/framework/mail/retriever.h new file mode 100644 index 00000000..e454532d --- /dev/null +++ b/framework/mail/retriever.h | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsystems.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #pragma once | ||
21 | |||
22 | #include <QObject> | ||
23 | #include <QAbstractItemModel> | ||
24 | #include <QVariant> | ||
25 | |||
26 | /** | ||
27 | * A wrapper for a QAbstractItemModel to retrieve a value from a single index via property binding | ||
28 | * | ||
29 | * Assign a model that retrieves the index, set the property your interested in, and propery-bind "value". | ||
30 | */ | ||
31 | class Retriever : public QObject | ||
32 | { | ||
33 | Q_OBJECT | ||
34 | Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel) | ||
35 | Q_PROPERTY(QString propertyName MEMBER mPropertyName) | ||
36 | Q_PROPERTY(QVariant value MEMBER mValue NOTIFY valueChanged) | ||
37 | |||
38 | public: | ||
39 | explicit Retriever(QObject *parent = Q_NULLPTR); | ||
40 | |||
41 | QAbstractItemModel* model() const; | ||
42 | void setModel(QAbstractItemModel* model); | ||
43 | |||
44 | signals: | ||
45 | void valueChanged(); | ||
46 | |||
47 | private slots: | ||
48 | void onRowsInserted(const QModelIndex &parent, int first, int last); | ||
49 | void onModelReset(); | ||
50 | |||
51 | private: | ||
52 | QString mPropertyName; | ||
53 | QVariant mValue; | ||
54 | QAbstractItemModel *mModel; | ||
55 | }; | ||