diff options
Diffstat (limited to 'src/bookmarks.c')
-rw-r--r-- | src/bookmarks.c | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/src/bookmarks.c b/src/bookmarks.c index b023e90a..96a2b6a0 100644 --- a/src/bookmarks.c +++ b/src/bookmarks.c | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | #include <the_Foundation/file.h> | 3 | #include <the_Foundation/file.h> |
4 | #include <the_Foundation/path.h> | 4 | #include <the_Foundation/path.h> |
5 | #include <the_Foundation/hash.h> | ||
5 | 6 | ||
6 | void init_Bookmark(iBookmark *d) { | 7 | void init_Bookmark(iBookmark *d) { |
7 | init_String(&d->url); | 8 | init_String(&d->url); |
@@ -27,25 +28,33 @@ static int cmpTimeDescending_Bookmark_(const iBookmark **a, const iBookmark **b) | |||
27 | static const char *fileName_Bookmarks_ = "bookmarks.txt"; | 28 | static const char *fileName_Bookmarks_ = "bookmarks.txt"; |
28 | 29 | ||
29 | struct Impl_Bookmarks { | 30 | struct Impl_Bookmarks { |
30 | iArray bookmarks; | 31 | int idEnum; |
32 | iHash bookmarks; | ||
31 | }; | 33 | }; |
32 | 34 | ||
33 | iDefineTypeConstruction(Bookmarks) | 35 | iDefineTypeConstruction(Bookmarks) |
34 | 36 | ||
35 | void init_Bookmarks(iBookmarks *d) { | 37 | void init_Bookmarks(iBookmarks *d) { |
36 | init_Array(&d->bookmarks, sizeof(iBookmark)); | 38 | d->idEnum = 0; |
39 | init_Hash(&d->bookmarks); | ||
37 | } | 40 | } |
38 | 41 | ||
39 | void deinit_Bookmarks(iBookmarks *d) { | 42 | void deinit_Bookmarks(iBookmarks *d) { |
40 | clear_Bookmarks(d); | 43 | clear_Bookmarks(d); |
41 | deinit_Array(&d->bookmarks); | 44 | deinit_Hash(&d->bookmarks); |
42 | } | 45 | } |
43 | 46 | ||
44 | void clear_Bookmarks(iBookmarks *d) { | 47 | void clear_Bookmarks(iBookmarks *d) { |
45 | iForEach(Array, i, &d->bookmarks) { | 48 | iForEach(Hash, i, &d->bookmarks) { |
46 | deinit_Bookmark(i.value); | 49 | delete_Bookmark((iBookmark *) i.value); |
47 | } | 50 | } |
48 | clear_Array(&d->bookmarks); | 51 | clear_Hash(&d->bookmarks); |
52 | d->idEnum = 0; | ||
53 | } | ||
54 | |||
55 | static void insert_Bookmarks_(iBookmarks *d, iBookmark *bookmark) { | ||
56 | bookmark->node.key = ++d->idEnum; | ||
57 | insert_Hash(&d->bookmarks, &bookmark->node); | ||
49 | } | 58 | } |
50 | 59 | ||
51 | void load_Bookmarks(iBookmarks *d, const char *dirPath) { | 60 | void load_Bookmarks(iBookmarks *d, const char *dirPath) { |
@@ -62,19 +71,18 @@ void load_Bookmarks(iBookmarks *d, const char *dirPath) { | |||
62 | continue; | 71 | continue; |
63 | } | 72 | } |
64 | } | 73 | } |
65 | iBookmark bm; | 74 | iBookmark *bm = new_Bookmark(); |
66 | init_Bookmark(&bm); | 75 | bm->icon = strtoul(line.start, NULL, 16); |
67 | bm.icon = strtoul(line.start, NULL, 16); | ||
68 | line.start += 9; | 76 | line.start += 9; |
69 | char *endPos; | 77 | char *endPos; |
70 | initSeconds_Time(&bm.when, strtod(line.start, &endPos)); | 78 | initSeconds_Time(&bm->when, strtod(line.start, &endPos)); |
71 | line.start = skipSpace_CStr(endPos); | 79 | line.start = skipSpace_CStr(endPos); |
72 | setRange_String(&bm.url, line); | 80 | setRange_String(&bm->url, line); |
73 | nextSplit_Rangecc(&src, "\n", &line); | 81 | nextSplit_Rangecc(&src, "\n", &line); |
74 | setRange_String(&bm.title, line); | 82 | setRange_String(&bm->title, line); |
75 | nextSplit_Rangecc(&src, "\n", &line); | 83 | nextSplit_Rangecc(&src, "\n", &line); |
76 | setRange_String(&bm.tags, line); | 84 | setRange_String(&bm->tags, line); |
77 | pushBack_Array(&d->bookmarks, &bm); | 85 | insert_Bookmarks_(d, bm); |
78 | } | 86 | } |
79 | } | 87 | } |
80 | iRelease(f); | 88 | iRelease(f); |
@@ -84,8 +92,8 @@ void save_Bookmarks(const iBookmarks *d, const char *dirPath) { | |||
84 | iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_)); | 92 | iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_)); |
85 | if (open_File(f, writeOnly_FileMode | text_FileMode)) { | 93 | if (open_File(f, writeOnly_FileMode | text_FileMode)) { |
86 | iString *str = collectNew_String(); | 94 | iString *str = collectNew_String(); |
87 | iConstForEach(Array, i, &d->bookmarks) { | 95 | iConstForEach(Hash, i, &d->bookmarks) { |
88 | const iBookmark *bm = i.value; | 96 | const iBookmark *bm = (const iBookmark *) i.value; |
89 | format_String(str, | 97 | format_String(str, |
90 | "%08x %lf %s\n%s\n%s\n", | 98 | "%08x %lf %s\n%s\n%s\n", |
91 | bm->icon, | 99 | bm->icon, |
@@ -101,22 +109,26 @@ void save_Bookmarks(const iBookmarks *d, const char *dirPath) { | |||
101 | 109 | ||
102 | void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags, | 110 | void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags, |
103 | iChar icon) { | 111 | iChar icon) { |
104 | iBookmark bm; | 112 | iBookmark *bm = new_Bookmark(); |
105 | init_Bookmark(&bm); | 113 | set_String(&bm->url, url); |
106 | set_String(&bm.url, url); | 114 | set_String(&bm->title, title); |
107 | set_String(&bm.title, title); | 115 | set_String(&bm->tags, tags); |
108 | set_String(&bm.tags, tags); | 116 | bm->icon = icon; |
109 | bm.icon = icon; | 117 | initCurrent_Time(&bm->when); |
110 | initCurrent_Time(&bm.when); | 118 | insert_Bookmarks_(d, bm); |
111 | pushBack_Array(&d->bookmarks, &bm); | 119 | } |
120 | |||
121 | void remove_Bookmarks(iBookmarks *d, uint32_t id) { | ||
122 | delete_Bookmark((iBookmark *) remove_Hash(&d->bookmarks, id)); | ||
112 | } | 123 | } |
113 | 124 | ||
114 | const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter, | 125 | const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter, |
115 | iBookmarksCompareFunc cmp) { | 126 | iBookmarksCompareFunc cmp) { |
116 | iPtrArray *list = collectNew_PtrArray(); | 127 | iPtrArray *list = collectNew_PtrArray(); |
117 | iConstForEach(Array, i, &d->bookmarks) { | 128 | iConstForEach(Hash, i, &d->bookmarks) { |
118 | if (!filter || filter(i.value)) { | 129 | const iBookmark *bm = (const iBookmark *) i.value; |
119 | pushBack_PtrArray(list, i.value); | 130 | if (!filter || filter(bm)) { |
131 | pushBack_PtrArray(list, bm); | ||
120 | } | 132 | } |
121 | } | 133 | } |
122 | if (!cmp) cmp = cmpTimeDescending_Bookmark_; | 134 | if (!cmp) cmp = cmpTimeDescending_Bookmark_; |