diff options
Diffstat (limited to 'src/bookmarks.c')
-rw-r--r-- | src/bookmarks.c | 31 |
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 | ||
29 | void init_Bookmark(iBookmark *d) { | 30 | void 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) | |||
50 | static const char *fileName_Bookmarks_ = "bookmarks.txt"; | 51 | static const char *fileName_Bookmarks_ = "bookmarks.txt"; |
51 | 52 | ||
52 | struct Impl_Bookmarks { | 53 | struct Impl_Bookmarks { |
53 | int idEnum; | 54 | iMutex *mtx; |
54 | iHash bookmarks; | 55 | int idEnum; |
56 | iHash bookmarks; | ||
55 | }; | 57 | }; |
56 | 58 | ||
57 | iDefineTypeConstruction(Bookmarks) | 59 | iDefineTypeConstruction(Bookmarks) |
58 | 60 | ||
59 | void init_Bookmarks(iBookmarks *d) { | 61 | void 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) { | |||
64 | void deinit_Bookmarks(iBookmarks *d) { | 67 | void 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 | ||
69 | void clear_Bookmarks(iBookmarks *d) { | 73 | void 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 | ||
77 | static void insert_Bookmarks_(iBookmarks *d, iBookmark *bookmark) { | 83 | static 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 | ||
82 | void load_Bookmarks(iBookmarks *d, const char *dirPath) { | 90 | void load_Bookmarks(iBookmarks *d, const char *dirPath) { |
@@ -111,6 +119,7 @@ void load_Bookmarks(iBookmarks *d, const char *dirPath) { | |||
111 | } | 119 | } |
112 | 120 | ||
113 | void save_Bookmarks(const iBookmarks *d, const char *dirPath) { | 121 | void 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 | ||
132 | void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags, | 142 | void 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 | ||
143 | iBool remove_Bookmarks(iBookmarks *d, uint32_t id) { | 155 | iBool 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 | ||
152 | iBookmark *get_Bookmarks(iBookmarks *d, uint32_t id) { | 165 | iBookmark *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 | ||
156 | const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter, | 169 | const 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; |