summaryrefslogtreecommitdiff
path: root/src/bookmarks.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-09-06 22:45:15 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-09-06 22:45:15 +0300
commita364d9456dfdfd8181904fca6308e9c36eefd10a (patch)
treef355ded227cf52053784b991f5d8441a5502e447 /src/bookmarks.c
parent52a1652536e4e27751ac121009f85113e72afe7d (diff)
LookupWidget: Keyboard focus and cursor
Diffstat (limited to 'src/bookmarks.c')
-rw-r--r--src/bookmarks.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/bookmarks.c b/src/bookmarks.c
index eff0146d..6a5eb296 100644
--- a/src/bookmarks.c
+++ b/src/bookmarks.c
@@ -23,8 +23,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
23#include "bookmarks.h" 23#include "bookmarks.h"
24 24
25#include <the_Foundation/file.h> 25#include <the_Foundation/file.h>
26#include <the_Foundation/path.h>
27#include <the_Foundation/hash.h> 26#include <the_Foundation/hash.h>
27#include <the_Foundation/mutex.h>
28#include <the_Foundation/path.h>
28 29
29void init_Bookmark(iBookmark *d) { 30void init_Bookmark(iBookmark *d) {
30 init_String(&d->url); 31 init_String(&d->url);
@@ -50,13 +51,15 @@ static int cmpTimeDescending_Bookmark_(const iBookmark **a, const iBookmark **b)
50static const char *fileName_Bookmarks_ = "bookmarks.txt"; 51static const char *fileName_Bookmarks_ = "bookmarks.txt";
51 52
52struct Impl_Bookmarks { 53struct Impl_Bookmarks {
53 int idEnum; 54 iMutex *mtx;
54 iHash bookmarks; 55 int idEnum;
56 iHash bookmarks;
55}; 57};
56 58
57iDefineTypeConstruction(Bookmarks) 59iDefineTypeConstruction(Bookmarks)
58 60
59void init_Bookmarks(iBookmarks *d) { 61void init_Bookmarks(iBookmarks *d) {
62 d->mtx = new_Mutex();
60 d->idEnum = 0; 63 d->idEnum = 0;
61 init_Hash(&d->bookmarks); 64 init_Hash(&d->bookmarks);
62} 65}
@@ -64,19 +67,24 @@ void init_Bookmarks(iBookmarks *d) {
64void deinit_Bookmarks(iBookmarks *d) { 67void deinit_Bookmarks(iBookmarks *d) {
65 clear_Bookmarks(d); 68 clear_Bookmarks(d);
66 deinit_Hash(&d->bookmarks); 69 deinit_Hash(&d->bookmarks);
70 delete_Mutex(d->mtx);
67} 71}
68 72
69void clear_Bookmarks(iBookmarks *d) { 73void clear_Bookmarks(iBookmarks *d) {
74 lock_Mutex(d->mtx);
70 iForEach(Hash, i, &d->bookmarks) { 75 iForEach(Hash, i, &d->bookmarks) {
71 delete_Bookmark((iBookmark *) i.value); 76 delete_Bookmark((iBookmark *) i.value);
72 } 77 }
73 clear_Hash(&d->bookmarks); 78 clear_Hash(&d->bookmarks);
74 d->idEnum = 0; 79 d->idEnum = 0;
80 unlock_Mutex(d->mtx);
75} 81}
76 82
77static void insert_Bookmarks_(iBookmarks *d, iBookmark *bookmark) { 83static void insert_Bookmarks_(iBookmarks *d, iBookmark *bookmark) {
84 lock_Mutex(d->mtx);
78 bookmark->node.key = ++d->idEnum; 85 bookmark->node.key = ++d->idEnum;
79 insert_Hash(&d->bookmarks, &bookmark->node); 86 insert_Hash(&d->bookmarks, &bookmark->node);
87 unlock_Mutex(d->mtx);
80} 88}
81 89
82void load_Bookmarks(iBookmarks *d, const char *dirPath) { 90void load_Bookmarks(iBookmarks *d, const char *dirPath) {
@@ -111,6 +119,7 @@ void load_Bookmarks(iBookmarks *d, const char *dirPath) {
111} 119}
112 120
113void save_Bookmarks(const iBookmarks *d, const char *dirPath) { 121void save_Bookmarks(const iBookmarks *d, const char *dirPath) {
122 lock_Mutex(d->mtx);
114 iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_)); 123 iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_));
115 if (open_File(f, writeOnly_FileMode | text_FileMode)) { 124 if (open_File(f, writeOnly_FileMode | text_FileMode)) {
116 iString *str = collectNew_String(); 125 iString *str = collectNew_String();
@@ -127,10 +136,12 @@ void save_Bookmarks(const iBookmarks *d, const char *dirPath) {
127 } 136 }
128 } 137 }
129 iRelease(f); 138 iRelease(f);
139 unlock_Mutex(d->mtx);
130} 140}
131 141
132void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags, 142void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags,
133 iChar icon) { 143 iChar icon) {
144 lock_Mutex(d->mtx);
134 iBookmark *bm = new_Bookmark(); 145 iBookmark *bm = new_Bookmark();
135 set_String(&bm->url, url); 146 set_String(&bm->url, url);
136 set_String(&bm->title, title); 147 set_String(&bm->title, title);
@@ -138,30 +149,34 @@ void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, cons
138 bm->icon = icon; 149 bm->icon = icon;
139 initCurrent_Time(&bm->when); 150 initCurrent_Time(&bm->when);
140 insert_Bookmarks_(d, bm); 151 insert_Bookmarks_(d, bm);
152 unlock_Mutex(d->mtx);
141} 153}
142 154
143iBool remove_Bookmarks(iBookmarks *d, uint32_t id) { 155iBool remove_Bookmarks(iBookmarks *d, uint32_t id) {
156 lock_Mutex(d->mtx);
144 iBookmark *bm = (iBookmark *) remove_Hash(&d->bookmarks, id); 157 iBookmark *bm = (iBookmark *) remove_Hash(&d->bookmarks, id);
145 if (bm) { 158 if (bm) {
146 delete_Bookmark(bm); 159 delete_Bookmark(bm);
147 return iTrue;
148 } 160 }
149 return iFalse; 161 unlock_Mutex(d->mtx);
162 return bm != NULL;
150} 163}
151 164
152iBookmark *get_Bookmarks(iBookmarks *d, uint32_t id) { 165iBookmark *get_Bookmarks(iBookmarks *d, uint32_t id) {
153 return (iBookmark *) value_Hash(&d->bookmarks, id); 166 return (iBookmark *) value_Hash(&d->bookmarks, id);
154} 167}
155 168
156const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter, 169const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksCompareFunc cmp,
157 iBookmarksCompareFunc cmp) { 170 iBookmarksFilterFunc filter, void *context) {
171 lock_Mutex(d->mtx);
158 iPtrArray *list = collectNew_PtrArray(); 172 iPtrArray *list = collectNew_PtrArray();
159 iConstForEach(Hash, i, &d->bookmarks) { 173 iConstForEach(Hash, i, &d->bookmarks) {
160 const iBookmark *bm = (const iBookmark *) i.value; 174 const iBookmark *bm = (const iBookmark *) i.value;
161 if (!filter || filter(bm)) { 175 if (!filter || filter(context, bm)) {
162 pushBack_PtrArray(list, bm); 176 pushBack_PtrArray(list, bm);
163 } 177 }
164 } 178 }
179 unlock_Mutex(d->mtx);
165 if (!cmp) cmp = cmpTimeDescending_Bookmark_; 180 if (!cmp) cmp = cmpTimeDescending_Bookmark_;
166 sort_Array(list, (int (*)(const void *, const void *)) cmp); 181 sort_Array(list, (int (*)(const void *, const void *)) cmp);
167 return list; 182 return list;