summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--framework/src/domain/composercontroller.cpp62
1 files changed, 35 insertions, 27 deletions
diff --git a/framework/src/domain/composercontroller.cpp b/framework/src/domain/composercontroller.cpp
index b6079f77..aa8dea5d 100644
--- a/framework/src/domain/composercontroller.cpp
+++ b/framework/src/domain/composercontroller.cpp
@@ -101,6 +101,19 @@ static void traverse(const QStandardItemModel *model, const std::function<void(Q
101 } 101 }
102} 102}
103 103
104template<typename T>
105void asyncRun(std::function<T()> run, std::function<void(T)> continuation)
106{
107 auto future = QtConcurrent::run(run);
108 auto watcher = new QFutureWatcher<T>;
109 //FIXME holding on to the item pointer like this is not safe
110 QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [watcher, continuation]() {
111 continuation(watcher->future().result());
112 delete watcher;
113 });
114 watcher->setFuture(future);
115}
116
104class AddresseeModel : public QStandardItemModel 117class AddresseeModel : public QStandardItemModel
105{ 118{
106public: 119public:
@@ -131,34 +144,29 @@ public:
131 mb.fromUnicodeString(addressee); 144 mb.fromUnicodeString(addressee);
132 145
133 SinkLog() << "Searching key for: " << mb.address(); 146 SinkLog() << "Searching key for: " << mb.address();
134 147 asyncRun<std::vector<GpgME::Key>>([mb] {
135 auto future = QtConcurrent::run([mb] { 148 auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP);
136 auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP); 149 if (keys.empty()) {
137 if (keys.empty()) { 150 //Search for key on remote server if it's missing and import
138 //Search for key on remote server if it's missing and import 151 //TODO: this is blocking and thus blocks the UI
139 //TODO: this is blocking and thus blocks the UI 152 keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, true, MailCrypto::OPENPGP);
140 keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, true, MailCrypto::OPENPGP); 153 MailCrypto::importKeys(keys);
141 MailCrypto::importKeys(keys);
142 }
143 return keys;
144 });
145 auto watcher = new QFutureWatcher<std::vector<GpgME::Key>>;
146 //FIXME holding on to the item pointer like this is not safe
147 QObject::connect(watcher, &QFutureWatcher<std::vector<GpgME::Key>>::finished, watcher, [this, watcher, item]() {
148 auto keys = watcher->future().result();
149 delete watcher;
150 if (!keys.empty()) {
151 if (keys.size() > 1 ) {
152 SinkWarning() << "Found more than one key, picking first one.";
153 } 154 }
154 SinkLog() << "Found key: " << keys.front().primaryFingerprint(); 155 return keys;
155 item->setData(true, ComposerController::KeyFoundRole); 156 },
156 item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); 157 //FIXME holding on to the item pointer like this is not safe
157 } else { 158 [this, item](const std::vector<GpgME::Key> &keys) {
158 SinkWarning() << "Failed to find key for recipient."; 159 if (!keys.empty()) {
159 } 160 if (keys.size() > 1 ) {
160 }); 161 SinkWarning() << "Found more than one key, picking first one.";
161 watcher->setFuture(future); 162 }
163 SinkLog() << "Found key: " << keys.front().primaryFingerprint();
164 item->setData(true, ComposerController::KeyFoundRole);
165 item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole);
166 } else {
167 SinkWarning() << "Failed to find key for recipient.";
168 }
169 });
162 } 170 }
163 171
164 void remove(const QString &addressee) 172 void remove(const QString &addressee)