summaryrefslogtreecommitdiffstats
path: root/framework/domain/composercontroller.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-12-30 12:02:12 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-12-30 12:02:12 +0100
commit431c257dd29e2e3d8878db200f0de4d452bffe92 (patch)
tree167c9c38839421c6be8ad460b6113b961ba5d2b1 /framework/domain/composercontroller.cpp
parent48da7de1b6bcd87c57b4c7f60133f3f13fc4bff2 (diff)
downloadkube-431c257dd29e2e3d8878db200f0de4d452bffe92.tar.gz
kube-431c257dd29e2e3d8878db200f0de4d452bffe92.zip
Encapsulate completer and selector.
Diffstat (limited to 'framework/domain/composercontroller.cpp')
-rw-r--r--framework/domain/composercontroller.cpp88
1 files changed, 48 insertions, 40 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}