summaryrefslogtreecommitdiff
path: root/src/ui/lookupwidget.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-09-07 23:06:07 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-09-07 23:06:07 +0300
commit6145c27ad68f3f49bdc8e5c621ef7209edf81545 (patch)
treef721ccb1377209e0b4525521338ad3244380790b /src/ui/lookupwidget.c
parent320791c629e988f4634590aadf1bee5ee53d1785 (diff)
LookupWidget: Finding identities
Diffstat (limited to 'src/ui/lookupwidget.c')
-rw-r--r--src/ui/lookupwidget.c54
1 files changed, 47 insertions, 7 deletions
diff --git a/src/ui/lookupwidget.c b/src/ui/lookupwidget.c
index 5fcbaacd..dbe734bf 100644
--- a/src/ui/lookupwidget.c
+++ b/src/ui/lookupwidget.c
@@ -21,17 +21,19 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 21SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22 22
23#include "lookupwidget.h" 23#include "lookupwidget.h"
24
25#include "app.h"
26#include "bookmarks.h"
27#include "command.h"
24#include "documentwidget.h" 28#include "documentwidget.h"
25#include "lookup.h" 29#include "gmcerts.h"
26#include "listwidget.h" 30#include "gmutil.h"
31#include "history.h"
27#include "inputwidget.h" 32#include "inputwidget.h"
33#include "listwidget.h"
34#include "lookup.h"
28#include "util.h" 35#include "util.h"
29#include "command.h"
30#include "bookmarks.h"
31#include "history.h"
32#include "visited.h" 36#include "visited.h"
33#include "gmutil.h"
34#include "app.h"
35 37
36#include <the_Foundation/mutex.h> 38#include <the_Foundation/mutex.h>
37#include <the_Foundation/thread.h> 39#include <the_Foundation/thread.h>
@@ -156,6 +158,14 @@ static float bookmarkRelevance_LookupJob_(const iLookupJob *d, const iBookmark *
156 return h + iMax(p, t) + 2 * g; /* extra weight for tags */ 158 return h + iMax(p, t) + 2 * g; /* extra weight for tags */
157} 159}
158 160
161static float identityRelevance_LookupJob_(const iLookupJob *d, const iGmIdentity *identity) {
162 iString *cn = subject_TlsCertificate(identity->cert);
163 const float c = scoreMatch_(d->term, range_String(cn));
164 const float n = scoreMatch_(d->term, range_String(&identity->notes));
165 delete_String(cn);
166 return c + 2 * n; /* extra weight for notes */
167}
168
159static float visitedRelevance_LookupJob_(const iLookupJob *d, const iVisitedUrl *vis) { 169static float visitedRelevance_LookupJob_(const iLookupJob *d, const iVisitedUrl *vis) {
160 iUrl parts; 170 iUrl parts;
161 init_Url(&parts, &vis->url); 171 init_Url(&parts, &vis->url);
@@ -169,6 +179,10 @@ static iBool matchBookmark_LookupJob_(void *context, const iBookmark *bm) {
169 return bookmarkRelevance_LookupJob_(context, bm) > 0; 179 return bookmarkRelevance_LookupJob_(context, bm) > 0;
170} 180}
171 181
182static iBool matchIdentity_LookupJob_(void *context, const iGmIdentity *identity) {
183 return identityRelevance_LookupJob_(context, identity) > 0;
184}
185
172static void searchBookmarks_LookupJob_(iLookupJob *d) { 186static void searchBookmarks_LookupJob_(iLookupJob *d) {
173 /* Note: Called in a background thread. */ 187 /* Note: Called in a background thread. */
174 /* TODO: Thread safety! What if a bookmark gets deleted while its being accessed here? */ 188 /* TODO: Thread safety! What if a bookmark gets deleted while its being accessed here? */
@@ -230,6 +244,25 @@ static void searchHistory_LookupJob_(iLookupJob *d) {
230 } 244 }
231} 245}
232 246
247static void searchIdentities_LookupJob_(iLookupJob *d) {
248 /* Note: Called in a background thread. */
249 iConstForEach(PtrArray, i, listIdentities_GmCerts(certs_App(), matchIdentity_LookupJob_, d)) {
250 const iGmIdentity *identity = i.ptr;
251 iLookupResult *res = new_LookupResult();
252 res->type = identity_LookupResultType;
253 res->relevance = identityRelevance_LookupJob_(d, identity);
254 appendChar_String(&res->label, identity->icon);
255 appendChar_String(&res->label, ' ');
256 iString *cn = subject_TlsCertificate(identity->cert);
257 append_String(&res->label, cn);
258 delete_String(cn);
259 set_String(&res->meta,
260 collect_String(
261 hexEncode_Block(collect_Block(fingerprint_TlsCertificate(identity->cert)))));
262 pushBack_PtrArray(&d->results, res);
263 }
264}
265
233static iThreadResult worker_LookupWidget_(iThread *thread) { 266static iThreadResult worker_LookupWidget_(iThread *thread) {
234 iLookupWidget *d = userData_Thread(thread); 267 iLookupWidget *d = userData_Thread(thread);
235 printf("[LookupWidget] worker is running\n"); fflush(stdout); 268 printf("[LookupWidget] worker is running\n"); fflush(stdout);
@@ -271,6 +304,7 @@ static iThreadResult worker_LookupWidget_(iThread *thread) {
271 if (termLen >= 3) { 304 if (termLen >= 3) {
272 searchHistory_LookupJob_(job); 305 searchHistory_LookupJob_(job);
273 } 306 }
307 searchIdentities_LookupJob_(job);
274 } 308 }
275 /* Submit the result. */ 309 /* Submit the result. */
276 lock_Mutex(d->mtx); 310 lock_Mutex(d->mtx);
@@ -433,6 +467,12 @@ static void presentResults_LookupWidget_(iLookupWidget *d) {
433 format_String(&item->command, "open url:%s", cstr_String(&res->url)); 467 format_String(&item->command, "open url:%s", cstr_String(&res->url));
434 break; 468 break;
435 } 469 }
470 case identity_LookupResultType: {
471 item->fg = uiText_ColorId;
472 item->font = uiContent_FontId;
473 format_String(&item->text, "%s", cstr_String(&res->label));
474 break;
475 }
436 } 476 }
437 addItem_ListWidget(d->list, item); 477 addItem_ListWidget(d->list, item);
438 iRelease(item); 478 iRelease(item);