diff options
-rw-r--r-- | components/package/contents/ui/FocusComposer.qml | 16 | ||||
-rw-r--r-- | framework/domain/composercontroller.cpp | 88 | ||||
-rw-r--r-- | framework/domain/composercontroller.h | 67 |
3 files changed, 110 insertions, 61 deletions
diff --git a/components/package/contents/ui/FocusComposer.qml b/components/package/contents/ui/FocusComposer.qml index 51d1cc0d..07fb08b6 100644 --- a/components/package/contents/ui/FocusComposer.qml +++ b/components/package/contents/ui/FocusComposer.qml | |||
@@ -96,9 +96,9 @@ Controls2.Popup { | |||
96 | mailcontext.to = text; | 96 | mailcontext.to = text; |
97 | } | 97 | } |
98 | 98 | ||
99 | model: composer.recepientAutocompletionModel | 99 | model: composer.recipientCompleter.model |
100 | onSearchTermChanged: { | 100 | onSearchTermChanged: { |
101 | composer.recepientSearchString = searchTerm | 101 | composer.recipientCompleter.searchString = searchTerm |
102 | } | 102 | } |
103 | } | 103 | } |
104 | 104 | ||
@@ -122,9 +122,9 @@ Controls2.Popup { | |||
122 | mailcontext.cc = text; | 122 | mailcontext.cc = text; |
123 | } | 123 | } |
124 | 124 | ||
125 | model: composer.recepientAutocompletionModel | 125 | model: composer.recipientCompleter.model |
126 | onSearchTermChanged: { | 126 | onSearchTermChanged: { |
127 | composer.recepientSearchString = searchTerm | 127 | composer.recipientCompleter.searchString = searchTerm |
128 | } | 128 | } |
129 | } | 129 | } |
130 | 130 | ||
@@ -147,9 +147,9 @@ Controls2.Popup { | |||
147 | mailcontext.bcc = text; | 147 | mailcontext.bcc = text; |
148 | } | 148 | } |
149 | 149 | ||
150 | model: composer.recepientAutocompletionModel | 150 | model: composer.recipientCompleter.model |
151 | onSearchTermChanged: { | 151 | onSearchTermChanged: { |
152 | composer.recepientSearchString = searchTerm | 152 | composer.recipientCompleter.searchString = searchTerm |
153 | } | 153 | } |
154 | } | 154 | } |
155 | 155 | ||
@@ -161,13 +161,13 @@ Controls2.Popup { | |||
161 | 161 | ||
162 | Controls2.ComboBox { | 162 | Controls2.ComboBox { |
163 | id: identityCombo | 163 | id: identityCombo |
164 | model: composer.identityModel | 164 | model: composer.identitySelector.model |
165 | textRole: "displayName" | 165 | textRole: "displayName" |
166 | 166 | ||
167 | Layout.fillWidth: true | 167 | Layout.fillWidth: true |
168 | 168 | ||
169 | onCurrentIndexChanged: { | 169 | onCurrentIndexChanged: { |
170 | composer.currentIdentityIndex = currentIndex | 170 | composer.identitySelector.currentIndex = currentIndex |
171 | } | 171 | } |
172 | } | 172 | } |
173 | 173 | ||
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 | ||
50 | QString ComposerController::recepientSearchString() const | ||
51 | { | ||
52 | return QString(); | ||
53 | } | ||
54 | 50 | ||
55 | Kube::Context* ComposerController::mailContext() | 51 | Kube::Context* ComposerController::mailContext() |
56 | { | 52 | { |
57 | return &mContext; | 53 | return &mContext; |
58 | } | 54 | } |
59 | 55 | ||
60 | void ComposerController::setRecepientSearchString(const QString &s) | 56 | class RecipientCompleter : public Completer { |
61 | { | 57 | public: |
62 | if (auto model = static_cast<RecipientAutocompletionModel*>(recepientAutocompletionModel())) { | 58 | RecipientCompleter() : Completer(new RecipientAutocompletionModel) |
63 | model->setFilter(s); | 59 | { |
64 | } | 60 | } |
65 | } | ||
66 | 61 | ||
67 | QAbstractItemModel *ComposerController::identityModel() const | 62 | void setSearchString(const QString &s) { |
63 | static_cast<RecipientAutocompletionModel*>(model())->setFilter(s); | ||
64 | Completer::setSearchString(s); | ||
65 | } | ||
66 | }; | ||
67 | |||
68 | Completer *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 | ||
74 | QAbstractItemModel *ComposerController::recepientAutocompletionModel() const | 75 | class IdentitySelector : public Selector { |
76 | public: | ||
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 | } | ||
100 | private: | ||
101 | ComposerContext &mContext; | ||
102 | }; | ||
103 | |||
104 | Selector *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 | ||
81 | void ComposerController::setMessage(const KMime::Message::Ptr &msg) | 111 | void ComposerController::setMessage(const KMime::Message::Ptr &msg) |
@@ -113,7 +143,7 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) | |||
113 | 143 | ||
114 | void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName) | 144 | void 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 | |||
198 | void 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 | |||
215 | int 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 | ||
47 | class Completer : public QObject { | ||
48 | Q_OBJECT | ||
49 | Q_PROPERTY (QAbstractItemModel* model READ model CONSTANT) | ||
50 | Q_PROPERTY (QString searchString WRITE setSearchString READ searchString) | ||
51 | |||
52 | public: | ||
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 | |||
61 | private: | ||
62 | QAbstractItemModel *mModel = nullptr; | ||
63 | QString mSearchString; | ||
64 | }; | ||
65 | |||
66 | /** | ||
67 | * Exposes a model and maintains a current index selection. | ||
68 | */ | ||
69 | class Selector : public QObject { | ||
70 | Q_OBJECT | ||
71 | Q_PROPERTY (int currentIndex READ currentIndex WRITE setCurrentIndex) | ||
72 | Q_PROPERTY (QAbstractItemModel* model READ model CONSTANT) | ||
73 | |||
74 | public: | ||
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; | ||
91 | private: | ||
92 | QAbstractItemModel *mModel = nullptr; | ||
93 | int mCurrentIndex = 0; | ||
94 | }; | ||
95 | |||
46 | class ComposerController : public QObject | 96 | class 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 | |||
78 | public slots: | 120 | public 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 | }; |