diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-11-27 11:13:45 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-11-27 11:13:45 +0100 |
commit | 64a18747e21b244d38c1a8d2682692f07d1e46bd (patch) | |
tree | 4ed428072f945768abdded3b179fcd0f9f9da3ba /framework | |
parent | 3b052a030cb375ffc4e5f43f6ddc914bed169ee1 (diff) | |
download | kube-64a18747e21b244d38c1a8d2682692f07d1e46bd.tar.gz kube-64a18747e21b244d38c1a8d2682692f07d1e46bd.zip |
Safeguards to avoid crashes
Diffstat (limited to 'framework')
-rw-r--r-- | framework/src/domain/composercontroller.cpp | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/framework/src/domain/composercontroller.cpp b/framework/src/domain/composercontroller.cpp index aa8dea5d..75db7346 100644 --- a/framework/src/domain/composercontroller.cpp +++ b/framework/src/domain/composercontroller.cpp | |||
@@ -102,13 +102,15 @@ static void traverse(const QStandardItemModel *model, const std::function<void(Q | |||
102 | } | 102 | } |
103 | 103 | ||
104 | template<typename T> | 104 | template<typename T> |
105 | void asyncRun(std::function<T()> run, std::function<void(T)> continuation) | 105 | void asyncRun(QObject *object, std::function<T()> run, std::function<void(T)> continuation) |
106 | { | 106 | { |
107 | auto guard = QPointer<QObject>{object}; | ||
107 | auto future = QtConcurrent::run(run); | 108 | auto future = QtConcurrent::run(run); |
108 | auto watcher = new QFutureWatcher<T>; | 109 | 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, guard]() { |
110 | QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [watcher, continuation]() { | 111 | if (guard) { |
111 | continuation(watcher->future().result()); | 112 | continuation(watcher->future().result()); |
113 | } | ||
112 | delete watcher; | 114 | delete watcher; |
113 | }); | 115 | }); |
114 | watcher->setFuture(future); | 116 | watcher->setFuture(future); |
@@ -129,7 +131,8 @@ public: | |||
129 | void add(const QString &addressee) | 131 | void add(const QString &addressee) |
130 | { | 132 | { |
131 | auto item = new QStandardItem; | 133 | auto item = new QStandardItem; |
132 | item->setData(addressee, ComposerController::AddresseeNameRole); | 134 | item->setText(addressee); |
135 | item->setData(QVariant::fromValue(addressee), ComposerController::AddresseeNameRole); | ||
133 | item->setData(false, ComposerController::KeyFoundRole); | 136 | item->setData(false, ComposerController::KeyFoundRole); |
134 | item->setData(mCryptoEnabled, ComposerController::KeyMissingRole); | 137 | item->setData(mCryptoEnabled, ComposerController::KeyMissingRole); |
135 | appendRow(QList<QStandardItem*>() << item); | 138 | appendRow(QList<QStandardItem*>() << item); |
@@ -144,7 +147,7 @@ public: | |||
144 | mb.fromUnicodeString(addressee); | 147 | mb.fromUnicodeString(addressee); |
145 | 148 | ||
146 | SinkLog() << "Searching key for: " << mb.address(); | 149 | SinkLog() << "Searching key for: " << mb.address(); |
147 | asyncRun<std::vector<GpgME::Key>>([mb] { | 150 | asyncRun<std::vector<GpgME::Key>>(this, [mb] { |
148 | auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP); | 151 | auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP); |
149 | if (keys.empty()) { | 152 | if (keys.empty()) { |
150 | //Search for key on remote server if it's missing and import | 153 | //Search for key on remote server if it's missing and import |
@@ -154,15 +157,16 @@ public: | |||
154 | } | 157 | } |
155 | return keys; | 158 | return keys; |
156 | }, | 159 | }, |
157 | //FIXME holding on to the item pointer like this is not safe | 160 | [this, addressee](const std::vector<GpgME::Key> &keys) { |
158 | [this, item](const std::vector<GpgME::Key> &keys) { | ||
159 | if (!keys.empty()) { | 161 | if (!keys.empty()) { |
160 | if (keys.size() > 1 ) { | 162 | if (keys.size() > 1 ) { |
161 | SinkWarning() << "Found more than one key, picking first one."; | 163 | SinkWarning() << "Found more than one key, picking first one."; |
162 | } | 164 | } |
163 | SinkLog() << "Found key: " << keys.front().primaryFingerprint(); | 165 | SinkLog() << "Found key: " << keys.front().primaryFingerprint(); |
164 | item->setData(true, ComposerController::KeyFoundRole); | 166 | for (auto item : findItems(addressee)) { |
165 | item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); | 167 | item->setData(true, ComposerController::KeyFoundRole); |
168 | item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); | ||
169 | } | ||
166 | } else { | 170 | } else { |
167 | SinkWarning() << "Failed to find key for recipient."; | 171 | SinkWarning() << "Failed to find key for recipient."; |
168 | } | 172 | } |
@@ -173,7 +177,7 @@ public: | |||
173 | { | 177 | { |
174 | auto root = invisibleRootItem(); | 178 | auto root = invisibleRootItem(); |
175 | for (int row = 0; row < root->rowCount(); row++) { | 179 | for (int row = 0; row < root->rowCount(); row++) { |
176 | if (root->child(row, 0)->data(ComposerController::AddresseeNameRole).toString() == addressee) { | 180 | if (root->child(row, 0)->text() == addressee) { |
177 | root->removeRow(row); | 181 | root->removeRow(row); |
178 | return; | 182 | return; |
179 | } | 183 | } |
@@ -214,7 +218,7 @@ public: | |||
214 | { | 218 | { |
215 | QStringList list; | 219 | QStringList list; |
216 | traverse(this, [&] (QStandardItem *item) { | 220 | traverse(this, [&] (QStandardItem *item) { |
217 | list << item->data(ComposerController::AddresseeNameRole).toString(); | 221 | list << item->text(); |
218 | }); | 222 | }); |
219 | return list; | 223 | return list; |
220 | } | 224 | } |
@@ -225,7 +229,7 @@ public: | |||
225 | traverse(this, [&] (QStandardItem *item) { | 229 | traverse(this, [&] (QStandardItem *item) { |
226 | item->setData(mCryptoEnabled, ComposerController::KeyMissingRole); | 230 | item->setData(mCryptoEnabled, ComposerController::KeyMissingRole); |
227 | if (mCryptoEnabled) { | 231 | if (mCryptoEnabled) { |
228 | findKey(item->data(ComposerController::AddresseeNameRole).toString(), item); | 232 | findKey(item->text(), item); |
229 | } | 233 | } |
230 | }); | 234 | }); |
231 | } | 235 | } |