summaryrefslogtreecommitdiffstats
path: root/framework
diff options
context:
space:
mode:
Diffstat (limited to 'framework')
-rw-r--r--framework/domain/composercontroller.cpp88
-rw-r--r--framework/domain/composercontroller.h67
2 files changed, 102 insertions, 53 deletions
diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp
index 7129e342..57d386c6 100644
--- a/framework/domain/composercontroller.cpp
+++ b/framework/domain/composercontroller.cpp
@@ -47,35 +47,65 @@ ComposerController::ComposerController(QObject *parent) : QObject(parent)
47 QQmlEngine::setObjectOwnership(&mContext, QQmlEngine::CppOwnership); 47 QQmlEngine::setObjectOwnership(&mContext, QQmlEngine::CppOwnership);
48} 48}
49 49
50QString ComposerController::recepientSearchString() const
51{
52 return QString();
53}
54 50
55Kube::Context* ComposerController::mailContext() 51Kube::Context* ComposerController::mailContext()
56{ 52{
57 return &mContext; 53 return &mContext;
58} 54}
59 55
60void ComposerController::setRecepientSearchString(const QString &s) 56class RecipientCompleter : public Completer {
61{ 57public:
62 if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) { 58 RecipientCompleter() : Completer(new RecipientAutocompletionModel)
63 model->setFilter(s); 59 {
64 } 60 }
65}
66 61
67QAbstractItemModel *ComposerController::identityModel() const 62 void setSearchString(const QString &s) {
63 static_cast<RecipientAutocompletionModel*>(model())->setFilter(s);
64 Completer::setSearchString(s);
65 }
66};
67
68Completer *ComposerController::recipientCompleter() const
68{ 69{
69 static auto model = new IdentitiesModel(); 70 static auto selector = new RecipientCompleter();
70 QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); 71 QQmlEngine::setObjectOwnership(selector, QQmlEngine::CppOwnership);
71 return model; 72 return selector;
72} 73}
73 74
74QAbstractItemModel *ComposerController::recepientAutocompletionModel() const 75class IdentitySelector : public Selector {
76public:
77 IdentitySelector(ComposerContext &context) : Selector(new IdentitiesModel), mContext(context)
78 {
79 }
80
81 void setCurrent(const QModelIndex &index) Q_DECL_OVERRIDE
82 {
83 if (index.isValid()) {
84 auto currentAccountId = index.data(IdentitiesModel::AccountId).toByteArray();
85
86 KMime::Types::Mailbox mb;
87 mb.setName(index.data(IdentitiesModel::Username).toString());
88 mb.setAddress(index.data(IdentitiesModel::Address).toString().toUtf8());
89 SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId;
90
91 mContext.setProperty("identity", QVariant::fromValue(mb));
92 mContext.setProperty("accountId", QVariant::fromValue(currentAccountId));
93 } else {
94 SinkWarning() << "No valid identity for index: " << index;
95 mContext.setProperty("identity", QVariant{});
96 mContext.setProperty("accountId", QVariant{});
97 }
98
99 }
100private:
101 ComposerContext &mContext;
102};
103
104Selector *ComposerController::identitySelector() const
75{ 105{
76 static auto model = new RecipientAutocompletionModel(); 106 static auto selector = new IdentitySelector(*const_cast<ComposerContext*>(&mContext));
77 QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); 107 QQmlEngine::setObjectOwnership(selector, QQmlEngine::CppOwnership);
78 return model; 108 return selector;
79} 109}
80 110
81void ComposerController::setMessage(const KMime::Message::Ptr &msg) 111void ComposerController::setMessage(const KMime::Message::Ptr &msg)
@@ -113,7 +143,7 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft)
113 143
114void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName) 144void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName)
115{ 145{
116 if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) { 146 if (auto model = static_cast<RecipientAutocompletionModel*>(recipientCompleter()->model())) {
117 model->addEntry(addrSpec, displayName); 147 model->addEntry(addrSpec, displayName);
118 } 148 }
119} 149}
@@ -194,25 +224,3 @@ Kube::Action* ComposerController::sendAction()
194 })); 224 }));
195 return action; 225 return action;
196} 226}
197
198void ComposerController::setCurrentIdentityIndex(int index)
199{
200 m_currentAccountIndex = index;
201 auto currentIndex = identityModel()->index(m_currentAccountIndex, 0);
202 if (currentIndex.isValid()) {
203 auto currentAccountId = currentIndex.data(IdentitiesModel::AccountId).toByteArray();
204 KMime::Types::Mailbox mb;
205 mb.setName(currentIndex.data(IdentitiesModel::Username).toString());
206 mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8());
207 SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId;
208 mContext.setProperty("identity", QVariant::fromValue(mb));
209 mContext.setProperty("accountId", QVariant::fromValue(currentAccountId));
210 } else {
211 SinkWarning() << "No valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount();
212 }
213}
214
215int ComposerController::currentIdentityIndex() const
216{
217 return m_currentAccountIndex;
218}
diff --git a/framework/domain/composercontroller.h b/framework/domain/composercontroller.h
index 11da517e..3e701ed1 100644
--- a/framework/domain/composercontroller.h
+++ b/framework/domain/composercontroller.h
@@ -23,6 +23,7 @@
23#include <QString> 23#include <QString>
24#include <QStringList> 24#include <QStringList>
25#include <QVariant> 25#include <QVariant>
26#include <QQmlEngine>
26#include <QAbstractItemModel> 27#include <QAbstractItemModel>
27#include <sink/applicationdomaintype.h> 28#include <sink/applicationdomaintype.h>
28 29
@@ -43,15 +44,62 @@ class ComposerContext : public Kube::Context {
43 KUBE_CONTEXT_PROPERTY(QString, Body, body) 44 KUBE_CONTEXT_PROPERTY(QString, Body, body)
44}; 45};
45 46
47class Completer : public QObject {
48 Q_OBJECT
49 Q_PROPERTY (QAbstractItemModel* model READ model CONSTANT)
50 Q_PROPERTY (QString searchString WRITE setSearchString READ searchString)
51
52public:
53 Completer(QAbstractItemModel *model) : mModel{model}
54 {
55 QQmlEngine::setObjectOwnership(mModel, QQmlEngine::CppOwnership);
56 }
57 QAbstractItemModel *model() { return mModel; }
58 virtual void setSearchString(const QString &s) { mSearchString = s; }
59 QString searchString() const { return mSearchString; }
60
61private:
62 QAbstractItemModel *mModel = nullptr;
63 QString mSearchString;
64};
65
66/**
67 * Exposes a model and maintains a current index selection.
68 */
69class Selector : public QObject {
70 Q_OBJECT
71 Q_PROPERTY (int currentIndex READ currentIndex WRITE setCurrentIndex)
72 Q_PROPERTY (QAbstractItemModel* model READ model CONSTANT)
73
74public:
75 Selector(QAbstractItemModel *model) : mModel{model}
76 {
77 QQmlEngine::setObjectOwnership(mModel, QQmlEngine::CppOwnership);
78 }
79
80 virtual QAbstractItemModel *model() { return mModel; }
81
82 void setCurrentIndex(int i) {
83 mCurrentIndex = i;
84 Q_ASSERT(mModel);
85 setCurrent(mModel->index(mCurrentIndex, 0));
86 }
87
88 int currentIndex() { return mCurrentIndex; }
89
90 virtual void setCurrent(const QModelIndex &) = 0;
91private:
92 QAbstractItemModel *mModel = nullptr;
93 int mCurrentIndex = 0;
94};
95
46class ComposerController : public QObject 96class ComposerController : public QObject
47{ 97{
48 Q_OBJECT 98 Q_OBJECT
49 Q_PROPERTY (Kube::Context* mailContext READ mailContext CONSTANT) 99 Q_PROPERTY (Kube::Context* mailContext READ mailContext CONSTANT)
50 Q_PROPERTY (int currentIdentityIndex READ currentIdentityIndex WRITE setCurrentIdentityIndex)
51 100
52 Q_PROPERTY (QString recepientSearchString READ recepientSearchString WRITE setRecepientSearchString) 101 Q_PROPERTY (Completer* recipientCompleter READ recipientCompleter CONSTANT)
53 Q_PROPERTY (QAbstractItemModel* recepientAutocompletionModel READ recepientAutocompletionModel CONSTANT) 102 Q_PROPERTY (Selector* identitySelector READ identitySelector CONSTANT)
54 Q_PROPERTY (QAbstractItemModel* identityModel READ identityModel CONSTANT)
55 103
56 Q_PROPERTY (Kube::Action* sendAction READ sendAction) 104 Q_PROPERTY (Kube::Action* sendAction READ sendAction)
57 Q_PROPERTY (Kube::Action* saveAsDraftAction READ saveAsDraftAction) 105 Q_PROPERTY (Kube::Action* saveAsDraftAction READ saveAsDraftAction)
@@ -61,20 +109,14 @@ public:
61 109
62 Kube::Context* mailContext(); 110 Kube::Context* mailContext();
63 111
64 QString recepientSearchString() const; 112 Completer *recipientCompleter() const;
65 void setRecepientSearchString(const QString &body); 113 Selector *identitySelector() const;
66
67 QAbstractItemModel *identityModel() const;
68 QAbstractItemModel *recepientAutocompletionModel() const;
69 114
70 Q_INVOKABLE void loadMessage(const QVariant &draft, bool loadAsDraft); 115 Q_INVOKABLE void loadMessage(const QVariant &draft, bool loadAsDraft);
71 116
72 Kube::Action* sendAction(); 117 Kube::Action* sendAction();
73 Kube::Action* saveAsDraftAction(); 118 Kube::Action* saveAsDraftAction();
74 119
75 void setCurrentIdentityIndex(int index);
76 int currentIdentityIndex() const;
77
78public slots: 120public slots:
79 void clear(); 121 void clear();
80 122
@@ -86,6 +128,5 @@ private:
86 void recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName); 128 void recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName);
87 void setMessage(const QSharedPointer<KMime::Message> &msg); 129 void setMessage(const QSharedPointer<KMime::Message> &msg);
88 130
89 int m_currentAccountIndex = -1;
90 ComposerContext mContext; 131 ComposerContext mContext;
91}; 132};