/src/gstreamer/subprojects/glib-2.86.3/glib/gbookmarkfile.c
Line | Count | Source |
1 | | /* gbookmarkfile.c: parsing and building desktop bookmarks |
2 | | * |
3 | | * Copyright (C) 2005-2006 Emmanuele Bassi |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | |
23 | | #include "gbookmarkfile.h" |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | #include <errno.h> |
29 | | #include <fcntl.h> |
30 | | #include <locale.h> |
31 | | #include <time.h> |
32 | | #include <stdarg.h> |
33 | | |
34 | | #include "gconvert.h" |
35 | | #include "gdataset.h" |
36 | | #include "gdatetime.h" |
37 | | #include "gerror.h" |
38 | | #include "gfileutils.h" |
39 | | #include "ghash.h" |
40 | | #include "glibintl.h" |
41 | | #include "glist.h" |
42 | | #include "gmain.h" |
43 | | #include "gmarkup.h" |
44 | | #include "gmem.h" |
45 | | #include "gmessages.h" |
46 | | #include "gshell.h" |
47 | | #include "gslice.h" |
48 | | #include "gstdio.h" |
49 | | #include "gstring.h" |
50 | | #include "gstrfuncs.h" |
51 | | #include "gtimer.h" |
52 | | #include "gutils.h" |
53 | | |
54 | | |
55 | | /* XBEL 1.0 standard entities */ |
56 | 0 | #define XBEL_VERSION "1.0" |
57 | | #define XBEL_DTD_NICK "xbel" |
58 | | #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \ |
59 | | "Exchange Language 1.0//EN//XML" |
60 | | |
61 | | #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd" |
62 | | |
63 | 0 | #define XBEL_ROOT_ELEMENT "xbel" |
64 | | #define XBEL_FOLDER_ELEMENT "folder" /* unused */ |
65 | 0 | #define XBEL_BOOKMARK_ELEMENT "bookmark" |
66 | | #define XBEL_ALIAS_ELEMENT "alias" /* unused */ |
67 | | #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */ |
68 | 0 | #define XBEL_TITLE_ELEMENT "title" |
69 | 0 | #define XBEL_DESC_ELEMENT "desc" |
70 | 0 | #define XBEL_INFO_ELEMENT "info" |
71 | 0 | #define XBEL_METADATA_ELEMENT "metadata" |
72 | | |
73 | | #define XBEL_VERSION_ATTRIBUTE "version" |
74 | | #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */ |
75 | | #define XBEL_OWNER_ATTRIBUTE "owner" |
76 | | #define XBEL_ADDED_ATTRIBUTE "added" |
77 | | #define XBEL_VISITED_ATTRIBUTE "visited" |
78 | | #define XBEL_MODIFIED_ATTRIBUTE "modified" |
79 | | #define XBEL_ID_ATTRIBUTE "id" |
80 | 0 | #define XBEL_HREF_ATTRIBUTE "href" |
81 | | #define XBEL_REF_ATTRIBUTE "ref" /* unused */ |
82 | | |
83 | | #define XBEL_YES_VALUE "yes" |
84 | | #define XBEL_NO_VALUE "no" |
85 | | |
86 | | /* Desktop bookmark spec entities */ |
87 | 0 | #define BOOKMARK_METADATA_OWNER "http://freedesktop.org" |
88 | | |
89 | | #define BOOKMARK_NAMESPACE_NAME "bookmark" |
90 | | #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks" |
91 | | |
92 | 0 | #define BOOKMARK_GROUPS_ELEMENT "groups" |
93 | 0 | #define BOOKMARK_GROUP_ELEMENT "group" |
94 | 0 | #define BOOKMARK_APPLICATIONS_ELEMENT "applications" |
95 | 0 | #define BOOKMARK_APPLICATION_ELEMENT "application" |
96 | 0 | #define BOOKMARK_ICON_ELEMENT "icon" |
97 | | #define BOOKMARK_PRIVATE_ELEMENT "private" |
98 | | |
99 | 0 | #define BOOKMARK_NAME_ATTRIBUTE "name" |
100 | 0 | #define BOOKMARK_EXEC_ATTRIBUTE "exec" |
101 | | #define BOOKMARK_COUNT_ATTRIBUTE "count" |
102 | | #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */ |
103 | | #define BOOKMARK_MODIFIED_ATTRIBUTE "modified" |
104 | 0 | #define BOOKMARK_HREF_ATTRIBUTE "href" |
105 | | #define BOOKMARK_TYPE_ATTRIBUTE "type" |
106 | | |
107 | | /* Shared MIME Info entities */ |
108 | | #define MIME_NAMESPACE_NAME "mime" |
109 | | #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info" |
110 | 0 | #define MIME_TYPE_ELEMENT "mime-type" |
111 | | #define MIME_TYPE_ATTRIBUTE "type" |
112 | | |
113 | | |
114 | | typedef struct _BookmarkAppInfo BookmarkAppInfo; |
115 | | typedef struct _BookmarkMetadata BookmarkMetadata; |
116 | | typedef struct _BookmarkItem BookmarkItem; |
117 | | typedef struct _ParseData ParseData; |
118 | | |
119 | | struct _BookmarkAppInfo |
120 | | { |
121 | | gchar *name; |
122 | | gchar *exec; |
123 | | |
124 | | guint count; |
125 | | |
126 | | GDateTime *stamp; /* (owned) */ |
127 | | }; |
128 | | |
129 | | struct _BookmarkMetadata |
130 | | { |
131 | | gchar *mime_type; |
132 | | |
133 | | GList *groups; |
134 | | |
135 | | GList *applications; |
136 | | GHashTable *apps_by_name; |
137 | | |
138 | | gchar *icon_href; |
139 | | gchar *icon_mime; |
140 | | |
141 | | guint is_private : 1; |
142 | | }; |
143 | | |
144 | | struct _BookmarkItem |
145 | | { |
146 | | gchar *uri; |
147 | | |
148 | | gchar *title; |
149 | | gchar *description; |
150 | | |
151 | | GDateTime *added; /* (owned) */ |
152 | | GDateTime *modified; /* (owned) */ |
153 | | GDateTime *visited; /* (owned) */ |
154 | | |
155 | | BookmarkMetadata *metadata; |
156 | | }; |
157 | | |
158 | | struct _GBookmarkFile |
159 | | { |
160 | | gchar *title; |
161 | | gchar *description; |
162 | | |
163 | | /* we store our items in a list and keep a copy inside |
164 | | * a hash table for faster lookup performances |
165 | | */ |
166 | | GList *items; |
167 | | GHashTable *items_by_uri; |
168 | | }; |
169 | | |
170 | | /* parser state machine */ |
171 | | typedef enum |
172 | | { |
173 | | STATE_STARTED = 0, |
174 | | |
175 | | STATE_ROOT, |
176 | | STATE_BOOKMARK, |
177 | | STATE_TITLE, |
178 | | STATE_DESC, |
179 | | STATE_INFO, |
180 | | STATE_METADATA, |
181 | | STATE_APPLICATIONS, |
182 | | STATE_APPLICATION, |
183 | | STATE_GROUPS, |
184 | | STATE_GROUP, |
185 | | STATE_MIME, |
186 | | STATE_ICON, |
187 | | |
188 | | STATE_FINISHED |
189 | | } ParserState; |
190 | | |
191 | | static void g_bookmark_file_init (GBookmarkFile *bookmark); |
192 | | static void g_bookmark_file_clear (GBookmarkFile *bookmark); |
193 | | static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark, |
194 | | const gchar *buffer, |
195 | | gsize length, |
196 | | GError **error); |
197 | | static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark, |
198 | | gsize *length, |
199 | | GError **error); |
200 | | static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark, |
201 | | const gchar *uri); |
202 | | static void g_bookmark_file_add_item (GBookmarkFile *bookmark, |
203 | | BookmarkItem *item, |
204 | | GError **error); |
205 | | |
206 | | static gboolean timestamp_from_iso8601 (const gchar *iso_date, |
207 | | GDateTime **out_date_time, |
208 | | GError **error); |
209 | | |
210 | | /******************************** |
211 | | * BookmarkAppInfo * |
212 | | * * |
213 | | * Application metadata storage * |
214 | | ********************************/ |
215 | | static BookmarkAppInfo * |
216 | | bookmark_app_info_new (const gchar *name) |
217 | 0 | { |
218 | 0 | BookmarkAppInfo *retval; |
219 | |
|
220 | 0 | g_warn_if_fail (name != NULL); |
221 | |
|
222 | 0 | retval = g_slice_new (BookmarkAppInfo); |
223 | |
|
224 | 0 | retval->name = g_strdup (name); |
225 | 0 | retval->exec = NULL; |
226 | 0 | retval->count = 0; |
227 | 0 | retval->stamp = NULL; |
228 | |
|
229 | 0 | return retval; |
230 | 0 | } |
231 | | |
232 | | static void |
233 | | bookmark_app_info_free (BookmarkAppInfo *app_info) |
234 | 0 | { |
235 | 0 | if (!app_info) |
236 | 0 | return; |
237 | | |
238 | 0 | g_free (app_info->name); |
239 | 0 | g_free (app_info->exec); |
240 | 0 | g_clear_pointer (&app_info->stamp, g_date_time_unref); |
241 | |
|
242 | 0 | g_slice_free (BookmarkAppInfo, app_info); |
243 | 0 | } |
244 | | |
245 | | static BookmarkAppInfo * |
246 | | bookmark_app_info_copy (BookmarkAppInfo *app_info) |
247 | 0 | { |
248 | 0 | BookmarkAppInfo *copy; |
249 | |
|
250 | 0 | if (!app_info) |
251 | 0 | return NULL; |
252 | | |
253 | 0 | copy = bookmark_app_info_new (app_info->name); |
254 | 0 | copy->count = app_info->count; |
255 | 0 | copy->exec = g_strdup (app_info->exec); |
256 | |
|
257 | 0 | if (app_info->stamp) |
258 | 0 | copy->stamp = g_date_time_ref (app_info->stamp); |
259 | |
|
260 | 0 | return copy; |
261 | 0 | } |
262 | | |
263 | | static gchar * |
264 | | bookmark_app_info_dump (BookmarkAppInfo *app_info) |
265 | 0 | { |
266 | 0 | gchar *retval; |
267 | 0 | gchar *name, *exec, *modified, *count; |
268 | |
|
269 | 0 | g_warn_if_fail (app_info != NULL); |
270 | |
|
271 | 0 | if (app_info->count == 0) |
272 | 0 | return NULL; |
273 | | |
274 | 0 | name = g_markup_escape_text (app_info->name, -1); |
275 | 0 | exec = g_markup_escape_text (app_info->exec, -1); |
276 | 0 | count = g_strdup_printf ("%u", app_info->count); |
277 | |
|
278 | 0 | if (app_info->stamp) |
279 | 0 | { |
280 | 0 | char *tmp; |
281 | |
|
282 | 0 | tmp = g_date_time_format_iso8601 (app_info->stamp); |
283 | 0 | modified = g_strconcat (" " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", tmp, "\"", |
284 | 0 | NULL); |
285 | 0 | g_free (tmp); |
286 | 0 | } |
287 | 0 | else |
288 | 0 | { |
289 | 0 | modified = g_strdup (""); |
290 | 0 | } |
291 | |
|
292 | 0 | retval = g_strconcat (" " |
293 | 0 | "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT |
294 | 0 | " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\"" |
295 | 0 | " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\"", |
296 | 0 | modified, |
297 | 0 | " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n", |
298 | 0 | NULL); |
299 | |
|
300 | 0 | g_free (name); |
301 | 0 | g_free (exec); |
302 | 0 | g_free (modified); |
303 | 0 | g_free (count); |
304 | |
|
305 | 0 | return retval; |
306 | 0 | } |
307 | | |
308 | | |
309 | | /*********************** |
310 | | * BookmarkMetadata * |
311 | | * * |
312 | | * Metadata storage * |
313 | | ***********************/ |
314 | | static BookmarkMetadata * |
315 | | bookmark_metadata_new (void) |
316 | 0 | { |
317 | 0 | BookmarkMetadata *retval; |
318 | |
|
319 | 0 | retval = g_slice_new (BookmarkMetadata); |
320 | |
|
321 | 0 | retval->mime_type = NULL; |
322 | |
|
323 | 0 | retval->groups = NULL; |
324 | |
|
325 | 0 | retval->applications = NULL; |
326 | 0 | retval->apps_by_name = g_hash_table_new_full (g_str_hash, |
327 | 0 | g_str_equal, |
328 | 0 | NULL, |
329 | 0 | NULL); |
330 | |
|
331 | 0 | retval->is_private = FALSE; |
332 | |
|
333 | 0 | retval->icon_href = NULL; |
334 | 0 | retval->icon_mime = NULL; |
335 | |
|
336 | 0 | return retval; |
337 | 0 | } |
338 | | |
339 | | static void |
340 | | bookmark_metadata_free (BookmarkMetadata *metadata) |
341 | 0 | { |
342 | 0 | if (!metadata) |
343 | 0 | return; |
344 | | |
345 | 0 | g_free (metadata->mime_type); |
346 | |
|
347 | 0 | g_list_free_full (metadata->groups, g_free); |
348 | 0 | g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free); |
349 | |
|
350 | 0 | g_hash_table_destroy (metadata->apps_by_name); |
351 | |
|
352 | 0 | g_free (metadata->icon_href); |
353 | 0 | g_free (metadata->icon_mime); |
354 | |
|
355 | 0 | g_slice_free (BookmarkMetadata, metadata); |
356 | 0 | } |
357 | | |
358 | | static BookmarkMetadata * |
359 | | bookmark_metadata_copy (BookmarkMetadata *metadata) |
360 | 0 | { |
361 | 0 | BookmarkMetadata *copy; |
362 | 0 | GList *l; |
363 | |
|
364 | 0 | if (!metadata) |
365 | 0 | return NULL; |
366 | | |
367 | 0 | copy = bookmark_metadata_new (); |
368 | 0 | copy->is_private = metadata->is_private; |
369 | 0 | copy->mime_type = g_strdup (metadata->mime_type); |
370 | 0 | copy->icon_href = g_strdup (metadata->icon_href); |
371 | 0 | copy->icon_mime = g_strdup (metadata->icon_mime); |
372 | |
|
373 | 0 | copy->groups = g_list_copy_deep (metadata->groups, (GCopyFunc) g_strdup, NULL); |
374 | 0 | copy->applications = |
375 | 0 | g_list_copy_deep (metadata->applications, (GCopyFunc) bookmark_app_info_copy, NULL); |
376 | |
|
377 | 0 | for (l = copy->applications; l; l = l->next) |
378 | 0 | { |
379 | 0 | BookmarkAppInfo *app_info = l->data; |
380 | 0 | g_hash_table_insert (copy->apps_by_name, app_info->name, app_info); |
381 | 0 | } |
382 | |
|
383 | 0 | g_assert (g_hash_table_size (copy->apps_by_name) == |
384 | 0 | g_hash_table_size (metadata->apps_by_name)); |
385 | | |
386 | 0 | return copy; |
387 | 0 | } |
388 | | |
389 | | static gchar * |
390 | | bookmark_metadata_dump (BookmarkMetadata *metadata) |
391 | 0 | { |
392 | 0 | GString *retval; |
393 | 0 | gchar *buffer; |
394 | |
|
395 | 0 | if (!metadata->applications) |
396 | 0 | return NULL; |
397 | | |
398 | 0 | retval = g_string_sized_new (1024); |
399 | | |
400 | | /* metadata container */ |
401 | 0 | g_string_append (retval, |
402 | 0 | " " |
403 | 0 | "<" XBEL_METADATA_ELEMENT |
404 | 0 | " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER |
405 | 0 | "\">\n"); |
406 | | |
407 | | /* mime type */ |
408 | 0 | if (metadata->mime_type) { |
409 | 0 | buffer = g_strconcat (" " |
410 | 0 | "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " " |
411 | 0 | MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n", |
412 | 0 | NULL); |
413 | 0 | g_string_append (retval, buffer); |
414 | 0 | g_free (buffer); |
415 | 0 | } |
416 | |
|
417 | 0 | if (metadata->groups) |
418 | 0 | { |
419 | 0 | GList *l; |
420 | | |
421 | | /* open groups container */ |
422 | 0 | g_string_append (retval, |
423 | 0 | " " |
424 | 0 | "<" BOOKMARK_NAMESPACE_NAME |
425 | 0 | ":" BOOKMARK_GROUPS_ELEMENT ">\n"); |
426 | |
|
427 | 0 | for (l = g_list_last (metadata->groups); l != NULL; l = l->prev) |
428 | 0 | { |
429 | 0 | gchar *group_name; |
430 | |
|
431 | 0 | group_name = g_markup_escape_text ((gchar *) l->data, -1); |
432 | 0 | buffer = g_strconcat (" " |
433 | 0 | "<" BOOKMARK_NAMESPACE_NAME |
434 | 0 | ":" BOOKMARK_GROUP_ELEMENT ">", |
435 | 0 | group_name, |
436 | 0 | "</" BOOKMARK_NAMESPACE_NAME |
437 | 0 | ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL); |
438 | 0 | g_string_append (retval, buffer); |
439 | |
|
440 | 0 | g_free (buffer); |
441 | 0 | g_free (group_name); |
442 | 0 | } |
443 | | |
444 | | /* close groups container */ |
445 | 0 | g_string_append (retval, |
446 | 0 | " " |
447 | 0 | "</" BOOKMARK_NAMESPACE_NAME |
448 | 0 | ":" BOOKMARK_GROUPS_ELEMENT ">\n"); |
449 | 0 | } |
450 | |
|
451 | 0 | if (metadata->applications) |
452 | 0 | { |
453 | 0 | GList *l; |
454 | | |
455 | | /* open applications container */ |
456 | 0 | g_string_append (retval, |
457 | 0 | " " |
458 | 0 | "<" BOOKMARK_NAMESPACE_NAME |
459 | 0 | ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n"); |
460 | |
|
461 | 0 | for (l = g_list_last (metadata->applications); l != NULL; l = l->prev) |
462 | 0 | { |
463 | 0 | BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data; |
464 | 0 | gchar *app_data; |
465 | |
|
466 | 0 | g_warn_if_fail (app_info != NULL); |
467 | |
|
468 | 0 | app_data = bookmark_app_info_dump (app_info); |
469 | |
|
470 | 0 | if (app_data) |
471 | 0 | { |
472 | 0 | retval = g_string_append (retval, app_data); |
473 | |
|
474 | 0 | g_free (app_data); |
475 | 0 | } |
476 | 0 | } |
477 | | |
478 | | /* close applications container */ |
479 | 0 | g_string_append (retval, |
480 | 0 | " " |
481 | 0 | "</" BOOKMARK_NAMESPACE_NAME |
482 | 0 | ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n"); |
483 | 0 | } |
484 | | |
485 | | /* icon */ |
486 | 0 | if (metadata->icon_href) |
487 | 0 | { |
488 | 0 | gchar *href, *mime; |
489 | |
|
490 | 0 | if (!metadata->icon_mime) |
491 | 0 | metadata->icon_mime = g_strdup ("application/octet-stream"); |
492 | |
|
493 | 0 | href = g_markup_escape_text (metadata->icon_href, -1); |
494 | 0 | mime = g_markup_escape_text (metadata->icon_mime, -1); |
495 | |
|
496 | 0 | buffer = g_strconcat (" " |
497 | 0 | "<" BOOKMARK_NAMESPACE_NAME |
498 | 0 | ":" BOOKMARK_ICON_ELEMENT |
499 | 0 | " " BOOKMARK_HREF_ATTRIBUTE "=\"", |
500 | 0 | href, |
501 | 0 | "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", mime, "\"/>\n", NULL); |
502 | 0 | g_string_append (retval, buffer); |
503 | |
|
504 | 0 | g_free (buffer); |
505 | 0 | g_free (mime); |
506 | 0 | g_free (href); |
507 | 0 | } |
508 | | |
509 | | /* private hint */ |
510 | 0 | if (metadata->is_private) |
511 | 0 | g_string_append (retval, |
512 | 0 | " " |
513 | 0 | "<" BOOKMARK_NAMESPACE_NAME |
514 | 0 | ":" BOOKMARK_PRIVATE_ELEMENT "/>\n"); |
515 | | |
516 | | /* close metadata container */ |
517 | 0 | g_string_append (retval, |
518 | 0 | " " |
519 | 0 | "</" XBEL_METADATA_ELEMENT ">\n"); |
520 | |
|
521 | 0 | return g_string_free (retval, FALSE); |
522 | 0 | } |
523 | | |
524 | | /****************************************************** |
525 | | * BookmarkItem * |
526 | | * * |
527 | | * Storage for a single bookmark item inside the list * |
528 | | ******************************************************/ |
529 | | static BookmarkItem * |
530 | | bookmark_item_new (const gchar *uri) |
531 | 0 | { |
532 | 0 | BookmarkItem *item; |
533 | |
|
534 | 0 | g_warn_if_fail (uri != NULL); |
535 | |
|
536 | 0 | item = g_slice_new (BookmarkItem); |
537 | 0 | item->uri = g_strdup (uri); |
538 | |
|
539 | 0 | item->title = NULL; |
540 | 0 | item->description = NULL; |
541 | |
|
542 | 0 | item->added = NULL; |
543 | 0 | item->modified = NULL; |
544 | 0 | item->visited = NULL; |
545 | |
|
546 | 0 | item->metadata = NULL; |
547 | |
|
548 | 0 | return item; |
549 | 0 | } |
550 | | |
551 | | static void |
552 | | bookmark_item_free (BookmarkItem *item) |
553 | 0 | { |
554 | 0 | if (!item) |
555 | 0 | return; |
556 | | |
557 | 0 | g_free (item->uri); |
558 | 0 | g_free (item->title); |
559 | 0 | g_free (item->description); |
560 | |
|
561 | 0 | if (item->metadata) |
562 | 0 | bookmark_metadata_free (item->metadata); |
563 | |
|
564 | 0 | g_clear_pointer (&item->added, g_date_time_unref); |
565 | 0 | g_clear_pointer (&item->modified, g_date_time_unref); |
566 | 0 | g_clear_pointer (&item->visited, g_date_time_unref); |
567 | |
|
568 | 0 | g_slice_free (BookmarkItem, item); |
569 | 0 | } |
570 | | |
571 | | static BookmarkItem * |
572 | | bookmark_item_copy (BookmarkItem *item) |
573 | 0 | { |
574 | 0 | BookmarkItem* copy; |
575 | |
|
576 | 0 | if (!item) |
577 | 0 | return NULL; |
578 | | |
579 | 0 | copy = bookmark_item_new (item->uri); |
580 | |
|
581 | 0 | copy->title = g_strdup (item->title); |
582 | 0 | copy->description = g_strdup (item->description); |
583 | |
|
584 | 0 | copy->metadata = bookmark_metadata_copy (item->metadata); |
585 | |
|
586 | 0 | if (item->added) |
587 | 0 | copy->added = g_date_time_ref (item->added); |
588 | 0 | if (item->modified) |
589 | 0 | copy->modified = g_date_time_ref (item->modified); |
590 | 0 | if (item->visited) |
591 | 0 | copy->visited = g_date_time_ref (item->visited); |
592 | |
|
593 | 0 | return copy; |
594 | 0 | } |
595 | | |
596 | | static void |
597 | | bookmark_item_touch_modified (BookmarkItem *item) |
598 | 0 | { |
599 | 0 | g_clear_pointer (&item->modified, g_date_time_unref); |
600 | 0 | item->modified = g_date_time_new_now_utc (); |
601 | 0 | } |
602 | | |
603 | | static gchar * |
604 | | bookmark_item_dump (BookmarkItem *item) |
605 | 0 | { |
606 | 0 | GString *retval; |
607 | 0 | gchar *escaped_uri; |
608 | | |
609 | | /* at this point, we must have at least a registered application; if we don't |
610 | | * we don't screw up the bookmark file, and just skip this item |
611 | | */ |
612 | 0 | if (!item->metadata || !item->metadata->applications) |
613 | 0 | { |
614 | 0 | g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri); |
615 | 0 | return NULL; |
616 | 0 | } |
617 | | |
618 | 0 | retval = g_string_sized_new (4096); |
619 | |
|
620 | 0 | g_string_append (retval, " <" XBEL_BOOKMARK_ELEMENT " "); |
621 | |
|
622 | 0 | escaped_uri = g_markup_escape_text (item->uri, -1); |
623 | |
|
624 | 0 | g_string_append (retval, XBEL_HREF_ATTRIBUTE "=\""); |
625 | 0 | g_string_append (retval, escaped_uri); |
626 | 0 | g_string_append (retval , "\" "); |
627 | |
|
628 | 0 | g_free (escaped_uri); |
629 | |
|
630 | 0 | if (item->added) |
631 | 0 | { |
632 | 0 | char *added; |
633 | |
|
634 | 0 | added = g_date_time_format_iso8601 (item->added); |
635 | 0 | g_string_append (retval, XBEL_ADDED_ATTRIBUTE "=\""); |
636 | 0 | g_string_append (retval, added); |
637 | 0 | g_string_append (retval, "\" "); |
638 | 0 | g_free (added); |
639 | 0 | } |
640 | |
|
641 | 0 | if (item->modified) |
642 | 0 | { |
643 | 0 | char *modified; |
644 | |
|
645 | 0 | modified = g_date_time_format_iso8601 (item->modified); |
646 | 0 | g_string_append (retval, XBEL_MODIFIED_ATTRIBUTE "=\""); |
647 | 0 | g_string_append (retval, modified); |
648 | 0 | g_string_append (retval, "\" "); |
649 | 0 | g_free (modified); |
650 | 0 | } |
651 | |
|
652 | 0 | if (item->visited) |
653 | 0 | { |
654 | 0 | char *visited; |
655 | |
|
656 | 0 | visited = g_date_time_format_iso8601 (item->visited); |
657 | 0 | g_string_append (retval, XBEL_VISITED_ATTRIBUTE "=\""); |
658 | 0 | g_string_append (retval, visited); |
659 | 0 | g_string_append (retval, "\" "); |
660 | 0 | g_free (visited); |
661 | 0 | } |
662 | |
|
663 | 0 | if (retval->str[retval->len - 1] == ' ') |
664 | 0 | g_string_truncate (retval, retval->len - 1); |
665 | 0 | g_string_append (retval, ">\n"); |
666 | |
|
667 | 0 | if (item->title) |
668 | 0 | { |
669 | 0 | gchar *escaped_title; |
670 | |
|
671 | 0 | escaped_title = g_markup_escape_text (item->title, -1); |
672 | 0 | g_string_append (retval, " " "<" XBEL_TITLE_ELEMENT ">"); |
673 | 0 | g_string_append (retval, escaped_title); |
674 | 0 | g_string_append (retval, "</" XBEL_TITLE_ELEMENT ">\n"); |
675 | |
|
676 | 0 | g_free (escaped_title); |
677 | 0 | } |
678 | |
|
679 | 0 | if (item->description) |
680 | 0 | { |
681 | 0 | gchar *escaped_desc; |
682 | |
|
683 | 0 | escaped_desc = g_markup_escape_text (item->description, -1); |
684 | 0 | g_string_append (retval, " " "<" XBEL_DESC_ELEMENT ">"); |
685 | 0 | g_string_append (retval, escaped_desc); |
686 | 0 | g_string_append (retval, "</" XBEL_DESC_ELEMENT ">\n"); |
687 | |
|
688 | 0 | g_free (escaped_desc); |
689 | 0 | } |
690 | |
|
691 | 0 | if (item->metadata) |
692 | 0 | { |
693 | 0 | gchar *metadata; |
694 | |
|
695 | 0 | metadata = bookmark_metadata_dump (item->metadata); |
696 | 0 | if (metadata) |
697 | 0 | { |
698 | 0 | g_string_append (retval, " " "<" XBEL_INFO_ELEMENT ">\n"); |
699 | 0 | g_string_append (retval, metadata); |
700 | 0 | g_string_append (retval, " " "</" XBEL_INFO_ELEMENT ">\n"); |
701 | |
|
702 | 0 | g_free (metadata); |
703 | 0 | } |
704 | 0 | } |
705 | |
|
706 | 0 | g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n"); |
707 | |
|
708 | 0 | return g_string_free (retval, FALSE); |
709 | 0 | } |
710 | | |
711 | | static BookmarkAppInfo * |
712 | | bookmark_item_lookup_app_info (BookmarkItem *item, |
713 | | const gchar *app_name) |
714 | 0 | { |
715 | 0 | g_warn_if_fail (item != NULL && app_name != NULL); |
716 | |
|
717 | 0 | if (!item->metadata) |
718 | 0 | return NULL; |
719 | | |
720 | 0 | return g_hash_table_lookup (item->metadata->apps_by_name, app_name); |
721 | 0 | } |
722 | | |
723 | | /************************* |
724 | | * GBookmarkFile * |
725 | | *************************/ |
726 | | |
727 | | static void |
728 | | g_bookmark_file_init (GBookmarkFile *bookmark) |
729 | 0 | { |
730 | 0 | bookmark->title = NULL; |
731 | 0 | bookmark->description = NULL; |
732 | |
|
733 | 0 | bookmark->items = NULL; |
734 | 0 | bookmark->items_by_uri = g_hash_table_new_full (g_str_hash, |
735 | 0 | g_str_equal, |
736 | 0 | NULL, |
737 | 0 | NULL); |
738 | 0 | } |
739 | | |
740 | | static void |
741 | | g_bookmark_file_clear (GBookmarkFile *bookmark) |
742 | 0 | { |
743 | 0 | g_free (bookmark->title); |
744 | 0 | g_free (bookmark->description); |
745 | |
|
746 | 0 | g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free); |
747 | 0 | bookmark->items = NULL; |
748 | |
|
749 | 0 | g_clear_pointer (&bookmark->items_by_uri, g_hash_table_unref); |
750 | 0 | } |
751 | | |
752 | | struct _ParseData |
753 | | { |
754 | | ParserState state; |
755 | | |
756 | | GHashTable *namespaces; |
757 | | |
758 | | GBookmarkFile *bookmark_file; |
759 | | BookmarkItem *current_item; |
760 | | }; |
761 | | |
762 | | static ParseData * |
763 | | parse_data_new (void) |
764 | 0 | { |
765 | 0 | ParseData *retval; |
766 | |
|
767 | 0 | retval = g_new (ParseData, 1); |
768 | |
|
769 | 0 | retval->state = STATE_STARTED; |
770 | 0 | retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal, |
771 | 0 | (GDestroyNotify) g_free, |
772 | 0 | (GDestroyNotify) g_free); |
773 | 0 | retval->bookmark_file = NULL; |
774 | 0 | retval->current_item = NULL; |
775 | |
|
776 | 0 | return retval; |
777 | 0 | } |
778 | | |
779 | | static void |
780 | | parse_data_free (ParseData *parse_data) |
781 | 0 | { |
782 | 0 | g_hash_table_destroy (parse_data->namespaces); |
783 | |
|
784 | 0 | g_free (parse_data); |
785 | 0 | } |
786 | | |
787 | 0 | #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a)))) |
788 | | |
789 | | static void |
790 | | parse_bookmark_element (GMarkupParseContext *context, |
791 | | ParseData *parse_data, |
792 | | const gchar **attribute_names, |
793 | | const gchar **attribute_values, |
794 | | GError **error) |
795 | 0 | { |
796 | 0 | const gchar *uri, *added, *modified, *visited; |
797 | 0 | const gchar *attr; |
798 | 0 | gint i; |
799 | 0 | BookmarkItem *item; |
800 | 0 | GError *add_error; |
801 | |
|
802 | 0 | g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK)); |
803 | |
|
804 | 0 | i = 0; |
805 | 0 | uri = added = modified = visited = NULL; |
806 | 0 | for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i]) |
807 | 0 | { |
808 | 0 | if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE)) |
809 | 0 | uri = attribute_values[i]; |
810 | 0 | else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE)) |
811 | 0 | added = attribute_values[i]; |
812 | 0 | else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE)) |
813 | 0 | modified = attribute_values[i]; |
814 | 0 | else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE)) |
815 | 0 | visited = attribute_values[i]; |
816 | 0 | else |
817 | 0 | { |
818 | | /* bookmark is defined by the XBEL spec, so we need |
819 | | * to error out if the element has different or |
820 | | * missing attributes |
821 | | */ |
822 | 0 | g_set_error (error, G_MARKUP_ERROR, |
823 | 0 | G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, |
824 | 0 | _("Unexpected attribute “%s” for element “%s”"), |
825 | 0 | attr, |
826 | 0 | XBEL_BOOKMARK_ELEMENT); |
827 | 0 | return; |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | 0 | if (!uri) |
832 | 0 | { |
833 | 0 | g_set_error (error, G_MARKUP_ERROR, |
834 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
835 | 0 | _("Attribute “%s” of element “%s” not found"), |
836 | 0 | XBEL_HREF_ATTRIBUTE, |
837 | 0 | XBEL_BOOKMARK_ELEMENT); |
838 | 0 | return; |
839 | 0 | } |
840 | | |
841 | 0 | g_warn_if_fail (parse_data->current_item == NULL); |
842 | |
|
843 | 0 | item = bookmark_item_new (uri); |
844 | |
|
845 | 0 | if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error)) |
846 | 0 | { |
847 | 0 | bookmark_item_free (item); |
848 | 0 | return; |
849 | 0 | } |
850 | | |
851 | 0 | if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error)) |
852 | 0 | { |
853 | 0 | bookmark_item_free (item); |
854 | 0 | return; |
855 | 0 | } |
856 | | |
857 | 0 | if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error)) |
858 | 0 | { |
859 | 0 | bookmark_item_free (item); |
860 | 0 | return; |
861 | 0 | } |
862 | | |
863 | 0 | add_error = NULL; |
864 | 0 | g_bookmark_file_add_item (parse_data->bookmark_file, |
865 | 0 | item, |
866 | 0 | &add_error); |
867 | 0 | if (add_error) |
868 | 0 | { |
869 | 0 | bookmark_item_free (item); |
870 | |
|
871 | 0 | g_propagate_error (error, add_error); |
872 | |
|
873 | 0 | return; |
874 | 0 | } |
875 | | |
876 | 0 | parse_data->current_item = item; |
877 | 0 | } |
878 | | |
879 | | static void |
880 | | parse_application_element (GMarkupParseContext *context, |
881 | | ParseData *parse_data, |
882 | | const gchar **attribute_names, |
883 | | const gchar **attribute_values, |
884 | | GError **error) |
885 | 0 | { |
886 | 0 | const gchar *name, *exec, *count, *stamp, *modified; |
887 | 0 | const gchar *attr; |
888 | 0 | gint i; |
889 | 0 | BookmarkItem *item; |
890 | 0 | BookmarkAppInfo *ai; |
891 | |
|
892 | 0 | g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION)); |
893 | |
|
894 | 0 | i = 0; |
895 | 0 | name = exec = count = stamp = modified = NULL; |
896 | 0 | for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i]) |
897 | 0 | { |
898 | 0 | if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE)) |
899 | 0 | name = attribute_values[i]; |
900 | 0 | else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE)) |
901 | 0 | exec = attribute_values[i]; |
902 | 0 | else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE)) |
903 | 0 | count = attribute_values[i]; |
904 | 0 | else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE)) |
905 | 0 | stamp = attribute_values[i]; |
906 | 0 | else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE)) |
907 | 0 | modified = attribute_values[i]; |
908 | 0 | } |
909 | | |
910 | | /* the "name" and "exec" attributes are mandatory */ |
911 | 0 | if (!name) |
912 | 0 | { |
913 | 0 | g_set_error (error, G_MARKUP_ERROR, |
914 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
915 | 0 | _("Attribute “%s” of element “%s” not found"), |
916 | 0 | BOOKMARK_NAME_ATTRIBUTE, |
917 | 0 | BOOKMARK_APPLICATION_ELEMENT); |
918 | 0 | return; |
919 | 0 | } |
920 | | |
921 | 0 | if (!exec) |
922 | 0 | { |
923 | 0 | g_set_error (error, G_MARKUP_ERROR, |
924 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
925 | 0 | _("Attribute “%s” of element “%s” not found"), |
926 | 0 | BOOKMARK_EXEC_ATTRIBUTE, |
927 | 0 | BOOKMARK_APPLICATION_ELEMENT); |
928 | 0 | return; |
929 | 0 | } |
930 | | |
931 | 0 | g_warn_if_fail (parse_data->current_item != NULL); |
932 | 0 | item = parse_data->current_item; |
933 | |
|
934 | 0 | ai = bookmark_item_lookup_app_info (item, name); |
935 | 0 | if (!ai) |
936 | 0 | { |
937 | 0 | ai = bookmark_app_info_new (name); |
938 | |
|
939 | 0 | if (!item->metadata) |
940 | 0 | item->metadata = bookmark_metadata_new (); |
941 | |
|
942 | 0 | item->metadata->applications = g_list_prepend (item->metadata->applications, ai); |
943 | 0 | g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai); |
944 | 0 | } |
945 | |
|
946 | 0 | g_free (ai->exec); |
947 | 0 | ai->exec = g_strdup (exec); |
948 | |
|
949 | 0 | if (count) |
950 | 0 | ai->count = atoi (count); |
951 | 0 | else |
952 | 0 | ai->count = 1; |
953 | |
|
954 | 0 | g_clear_pointer (&ai->stamp, g_date_time_unref); |
955 | 0 | if (modified != NULL) |
956 | 0 | { |
957 | 0 | if (!timestamp_from_iso8601 (modified, &ai->stamp, error)) |
958 | 0 | return; |
959 | 0 | } |
960 | 0 | else |
961 | 0 | { |
962 | | /* the timestamp attribute has been deprecated but we still parse |
963 | | * it for backward compatibility |
964 | | */ |
965 | 0 | if (stamp) |
966 | 0 | ai->stamp = g_date_time_new_from_unix_utc (atol (stamp)); |
967 | 0 | else |
968 | 0 | ai->stamp = g_date_time_new_now_utc (); |
969 | 0 | } |
970 | 0 | } |
971 | | |
972 | | static void |
973 | | parse_mime_type_element (GMarkupParseContext *context, |
974 | | ParseData *parse_data, |
975 | | const gchar **attribute_names, |
976 | | const gchar **attribute_values, |
977 | | GError **error) |
978 | 0 | { |
979 | 0 | const gchar *type; |
980 | 0 | const gchar *attr; |
981 | 0 | gint i; |
982 | 0 | BookmarkItem *item; |
983 | |
|
984 | 0 | g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME)); |
985 | |
|
986 | 0 | i = 0; |
987 | 0 | type = NULL; |
988 | 0 | for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i]) |
989 | 0 | { |
990 | 0 | if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE)) |
991 | 0 | type = attribute_values[i]; |
992 | 0 | } |
993 | |
|
994 | 0 | if (!type) |
995 | 0 | type = "application/octet-stream"; |
996 | |
|
997 | 0 | g_warn_if_fail (parse_data->current_item != NULL); |
998 | 0 | item = parse_data->current_item; |
999 | |
|
1000 | 0 | if (!item->metadata) |
1001 | 0 | item->metadata = bookmark_metadata_new (); |
1002 | |
|
1003 | 0 | g_free (item->metadata->mime_type); |
1004 | 0 | item->metadata->mime_type = g_strdup (type); |
1005 | 0 | } |
1006 | | |
1007 | | static void |
1008 | | parse_icon_element (GMarkupParseContext *context, |
1009 | | ParseData *parse_data, |
1010 | | const gchar **attribute_names, |
1011 | | const gchar **attribute_values, |
1012 | | GError **error) |
1013 | 0 | { |
1014 | 0 | const gchar *href; |
1015 | 0 | const gchar *type; |
1016 | 0 | const gchar *attr; |
1017 | 0 | gint i; |
1018 | 0 | BookmarkItem *item; |
1019 | |
|
1020 | 0 | g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON)); |
1021 | |
|
1022 | 0 | i = 0; |
1023 | 0 | href = NULL; |
1024 | 0 | type = NULL; |
1025 | 0 | for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i]) |
1026 | 0 | { |
1027 | 0 | if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE)) |
1028 | 0 | href = attribute_values[i]; |
1029 | 0 | else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE)) |
1030 | 0 | type = attribute_values[i]; |
1031 | 0 | } |
1032 | | |
1033 | | /* the "href" attribute is mandatory */ |
1034 | 0 | if (!href) |
1035 | 0 | { |
1036 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1037 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1038 | 0 | _("Attribute “%s” of element “%s” not found"), |
1039 | 0 | BOOKMARK_HREF_ATTRIBUTE, |
1040 | 0 | BOOKMARK_ICON_ELEMENT); |
1041 | 0 | return; |
1042 | 0 | } |
1043 | | |
1044 | 0 | if (!type) |
1045 | 0 | type = "application/octet-stream"; |
1046 | |
|
1047 | 0 | g_warn_if_fail (parse_data->current_item != NULL); |
1048 | 0 | item = parse_data->current_item; |
1049 | |
|
1050 | 0 | if (!item->metadata) |
1051 | 0 | item->metadata = bookmark_metadata_new (); |
1052 | |
|
1053 | 0 | g_free (item->metadata->icon_href); |
1054 | 0 | g_free (item->metadata->icon_mime); |
1055 | 0 | item->metadata->icon_href = g_strdup (href); |
1056 | 0 | item->metadata->icon_mime = g_strdup (type); |
1057 | 0 | } |
1058 | | |
1059 | | /* scans through the attributes of an element for the "xmlns" pragma, and |
1060 | | * adds any resulting namespace declaration to a per-parser hashtable, using |
1061 | | * the namespace name as a key for the namespace URI; if no key was found, |
1062 | | * the namespace is considered as default, and stored under the "default" key. |
1063 | | * |
1064 | | * FIXME: this works on the assumption that the generator of the XBEL file |
1065 | | * is either this code or is smart enough to place the namespace declarations |
1066 | | * inside the main root node or inside the metadata node and does not redefine |
1067 | | * a namespace inside an inner node; this does *not* conform to the |
1068 | | * XML-NS standard, although is a close approximation. In order to make this |
1069 | | * conformant to the XML-NS specification we should use a per-element |
1070 | | * namespace table inside GMarkup and ask it to resolve the namespaces for us. |
1071 | | */ |
1072 | | static void |
1073 | | map_namespace_to_name (ParseData *parse_data, |
1074 | | const gchar **attribute_names, |
1075 | | const gchar **attribute_values) |
1076 | 0 | { |
1077 | 0 | const gchar *attr; |
1078 | 0 | gint i; |
1079 | |
|
1080 | 0 | g_warn_if_fail (parse_data != NULL); |
1081 | |
|
1082 | 0 | if (!attribute_names || !attribute_names[0]) |
1083 | 0 | return; |
1084 | | |
1085 | 0 | i = 0; |
1086 | 0 | for (attr = attribute_names[i]; attr; attr = attribute_names[++i]) |
1087 | 0 | { |
1088 | 0 | if (g_str_has_prefix (attr, "xmlns")) |
1089 | 0 | { |
1090 | 0 | gchar *namespace_name, *namespace_uri; |
1091 | 0 | gchar *p; |
1092 | |
|
1093 | 0 | p = g_utf8_strchr (attr, -1, ':'); |
1094 | 0 | if (p) |
1095 | 0 | p = g_utf8_next_char (p); |
1096 | 0 | else |
1097 | 0 | p = "default"; |
1098 | |
|
1099 | 0 | namespace_name = g_strdup (p); |
1100 | 0 | namespace_uri = g_strdup (attribute_values[i]); |
1101 | |
|
1102 | 0 | g_hash_table_replace (parse_data->namespaces, |
1103 | 0 | namespace_name, |
1104 | 0 | namespace_uri); |
1105 | 0 | } |
1106 | 0 | } |
1107 | 0 | } |
1108 | | |
1109 | | /* checks whether @element_full is equal to @element. |
1110 | | * |
1111 | | * if @namespace is set, it tries to resolve the namespace to a known URI, |
1112 | | * and if found is prepended to the element name, from which is separated |
1113 | | * using the character specified in the @sep parameter. |
1114 | | */ |
1115 | | static gboolean |
1116 | | is_element_full (ParseData *parse_data, |
1117 | | const gchar *element_full, |
1118 | | const gchar *namespace, |
1119 | | const gchar *element, |
1120 | | const gchar sep) |
1121 | 0 | { |
1122 | 0 | gchar *ns_uri, *ns_name; |
1123 | 0 | const gchar *p, *element_name; |
1124 | 0 | gboolean retval; |
1125 | |
|
1126 | 0 | g_warn_if_fail (parse_data != NULL); |
1127 | 0 | g_warn_if_fail (element_full != NULL); |
1128 | |
|
1129 | 0 | if (!element) |
1130 | 0 | return FALSE; |
1131 | | |
1132 | | /* no namespace requested: dumb element compare */ |
1133 | 0 | if (!namespace) |
1134 | 0 | return (0 == strcmp (element_full, element)); |
1135 | | |
1136 | | /* search for namespace separator; if none found, assume we are under the |
1137 | | * default namespace, and set ns_name to our "default" marker; if no default |
1138 | | * namespace has been set, just do a plain comparison between @full_element |
1139 | | * and @element. |
1140 | | */ |
1141 | 0 | p = g_utf8_strchr (element_full, -1, ':'); |
1142 | 0 | if (p) |
1143 | 0 | { |
1144 | 0 | ns_name = g_strndup (element_full, p - element_full); |
1145 | 0 | element_name = g_utf8_next_char (p); |
1146 | 0 | } |
1147 | 0 | else |
1148 | 0 | { |
1149 | 0 | ns_name = g_strdup ("default"); |
1150 | 0 | element_name = element_full; |
1151 | 0 | } |
1152 | |
|
1153 | 0 | ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name); |
1154 | 0 | if (!ns_uri) |
1155 | 0 | { |
1156 | | /* no default namespace found */ |
1157 | 0 | g_free (ns_name); |
1158 | |
|
1159 | 0 | return (0 == strcmp (element_full, element)); |
1160 | 0 | } |
1161 | | |
1162 | 0 | retval = (0 == strcmp (ns_uri, namespace) && |
1163 | 0 | 0 == strcmp (element_name, element)); |
1164 | |
|
1165 | 0 | g_free (ns_name); |
1166 | |
|
1167 | 0 | return retval; |
1168 | 0 | } |
1169 | | |
1170 | 0 | #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0')) |
1171 | 0 | #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|')) |
1172 | | |
1173 | | static const gchar * |
1174 | | parser_state_to_element_name (ParserState state) |
1175 | 0 | { |
1176 | 0 | switch (state) |
1177 | 0 | { |
1178 | 0 | case STATE_STARTED: |
1179 | 0 | case STATE_FINISHED: |
1180 | 0 | return "(top-level)"; |
1181 | 0 | case STATE_ROOT: |
1182 | 0 | return XBEL_ROOT_ELEMENT; |
1183 | 0 | case STATE_BOOKMARK: |
1184 | 0 | return XBEL_BOOKMARK_ELEMENT; |
1185 | 0 | case STATE_TITLE: |
1186 | 0 | return XBEL_TITLE_ELEMENT; |
1187 | 0 | case STATE_DESC: |
1188 | 0 | return XBEL_DESC_ELEMENT; |
1189 | 0 | case STATE_INFO: |
1190 | 0 | return XBEL_INFO_ELEMENT; |
1191 | 0 | case STATE_METADATA: |
1192 | 0 | return XBEL_METADATA_ELEMENT; |
1193 | 0 | case STATE_APPLICATIONS: |
1194 | 0 | return BOOKMARK_APPLICATIONS_ELEMENT; |
1195 | 0 | case STATE_APPLICATION: |
1196 | 0 | return BOOKMARK_APPLICATION_ELEMENT; |
1197 | 0 | case STATE_GROUPS: |
1198 | 0 | return BOOKMARK_GROUPS_ELEMENT; |
1199 | 0 | case STATE_GROUP: |
1200 | 0 | return BOOKMARK_GROUP_ELEMENT; |
1201 | 0 | case STATE_MIME: |
1202 | 0 | return MIME_TYPE_ELEMENT; |
1203 | 0 | case STATE_ICON: |
1204 | 0 | return BOOKMARK_ICON_ELEMENT; |
1205 | 0 | default: |
1206 | 0 | g_assert_not_reached (); |
1207 | 0 | } |
1208 | 0 | } |
1209 | | |
1210 | | static void |
1211 | | start_element_raw_cb (GMarkupParseContext *context, |
1212 | | const gchar *element_name, |
1213 | | const gchar **attribute_names, |
1214 | | const gchar **attribute_values, |
1215 | | gpointer user_data, |
1216 | | GError **error) |
1217 | 0 | { |
1218 | 0 | ParseData *parse_data = (ParseData *) user_data; |
1219 | | |
1220 | | /* we must check for namespace declarations first |
1221 | | * |
1222 | | * XXX - we could speed up things by checking for namespace declarations |
1223 | | * only on the root node, where they usually are; this would probably break |
1224 | | * on streams not produced by us or by "smart" generators |
1225 | | */ |
1226 | 0 | map_namespace_to_name (parse_data, attribute_names, attribute_values); |
1227 | |
|
1228 | 0 | switch (parse_data->state) |
1229 | 0 | { |
1230 | 0 | case STATE_STARTED: |
1231 | 0 | if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT)) |
1232 | 0 | { |
1233 | 0 | const gchar *attr; |
1234 | 0 | gint i; |
1235 | |
|
1236 | 0 | i = 0; |
1237 | 0 | for (attr = attribute_names[i]; attr; attr = attribute_names[++i]) |
1238 | 0 | { |
1239 | 0 | if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) && |
1240 | 0 | (0 == strcmp (attribute_values[i], XBEL_VERSION))) |
1241 | 0 | parse_data->state = STATE_ROOT; |
1242 | 0 | } |
1243 | 0 | } |
1244 | 0 | else |
1245 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1246 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1247 | 0 | _("Unexpected tag “%s”, tag “%s” expected"), |
1248 | 0 | element_name, XBEL_ROOT_ELEMENT); |
1249 | 0 | break; |
1250 | 0 | case STATE_ROOT: |
1251 | 0 | if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) |
1252 | 0 | parse_data->state = STATE_TITLE; |
1253 | 0 | else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)) |
1254 | 0 | parse_data->state = STATE_DESC; |
1255 | 0 | else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT)) |
1256 | 0 | { |
1257 | 0 | GError *inner_error = NULL; |
1258 | |
|
1259 | 0 | parse_data->state = STATE_BOOKMARK; |
1260 | |
|
1261 | 0 | parse_bookmark_element (context, |
1262 | 0 | parse_data, |
1263 | 0 | attribute_names, |
1264 | 0 | attribute_values, |
1265 | 0 | &inner_error); |
1266 | 0 | if (inner_error) |
1267 | 0 | g_propagate_error (error, inner_error); |
1268 | 0 | } |
1269 | 0 | else |
1270 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1271 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1272 | 0 | _("Unexpected tag “%s” inside “%s”"), |
1273 | 0 | element_name, |
1274 | 0 | XBEL_ROOT_ELEMENT); |
1275 | 0 | break; |
1276 | 0 | case STATE_BOOKMARK: |
1277 | 0 | if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) |
1278 | 0 | parse_data->state = STATE_TITLE; |
1279 | 0 | else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)) |
1280 | 0 | parse_data->state = STATE_DESC; |
1281 | 0 | else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) |
1282 | 0 | parse_data->state = STATE_INFO; |
1283 | 0 | else |
1284 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1285 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1286 | 0 | _("Unexpected tag “%s” inside “%s”"), |
1287 | 0 | element_name, |
1288 | 0 | XBEL_BOOKMARK_ELEMENT); |
1289 | 0 | break; |
1290 | 0 | case STATE_INFO: |
1291 | 0 | if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT)) |
1292 | 0 | { |
1293 | 0 | const gchar *attr; |
1294 | 0 | gint i; |
1295 | |
|
1296 | 0 | i = 0; |
1297 | 0 | for (attr = attribute_names[i]; attr; attr = attribute_names[++i]) |
1298 | 0 | { |
1299 | 0 | if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) && |
1300 | 0 | (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER))) |
1301 | 0 | { |
1302 | 0 | parse_data->state = STATE_METADATA; |
1303 | |
|
1304 | 0 | if (!parse_data->current_item->metadata) |
1305 | 0 | parse_data->current_item->metadata = bookmark_metadata_new (); |
1306 | 0 | } |
1307 | 0 | } |
1308 | 0 | } |
1309 | 0 | else |
1310 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1311 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1312 | 0 | _("Unexpected tag “%s”, tag “%s” expected"), |
1313 | 0 | element_name, |
1314 | 0 | XBEL_METADATA_ELEMENT); |
1315 | 0 | break; |
1316 | 0 | case STATE_METADATA: |
1317 | 0 | if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) |
1318 | 0 | parse_data->state = STATE_APPLICATIONS; |
1319 | 0 | else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) |
1320 | 0 | parse_data->state = STATE_GROUPS; |
1321 | 0 | else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) |
1322 | 0 | parse_data->current_item->metadata->is_private = TRUE; |
1323 | 0 | else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) |
1324 | 0 | { |
1325 | 0 | GError *inner_error = NULL; |
1326 | |
|
1327 | 0 | parse_data->state = STATE_ICON; |
1328 | |
|
1329 | 0 | parse_icon_element (context, |
1330 | 0 | parse_data, |
1331 | 0 | attribute_names, |
1332 | 0 | attribute_values, |
1333 | 0 | &inner_error); |
1334 | 0 | if (inner_error) |
1335 | 0 | g_propagate_error (error, inner_error); |
1336 | 0 | } |
1337 | 0 | else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)) |
1338 | 0 | { |
1339 | 0 | GError *inner_error = NULL; |
1340 | |
|
1341 | 0 | parse_data->state = STATE_MIME; |
1342 | |
|
1343 | 0 | parse_mime_type_element (context, |
1344 | 0 | parse_data, |
1345 | 0 | attribute_names, |
1346 | 0 | attribute_values, |
1347 | 0 | &inner_error); |
1348 | 0 | if (inner_error) |
1349 | 0 | g_propagate_error (error, inner_error); |
1350 | 0 | } |
1351 | 0 | else |
1352 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1353 | 0 | G_MARKUP_ERROR_UNKNOWN_ELEMENT, |
1354 | 0 | _("Unexpected tag “%s” inside “%s”"), |
1355 | 0 | element_name, |
1356 | 0 | XBEL_METADATA_ELEMENT); |
1357 | 0 | break; |
1358 | 0 | case STATE_APPLICATIONS: |
1359 | 0 | if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT)) |
1360 | 0 | { |
1361 | 0 | GError *inner_error = NULL; |
1362 | |
|
1363 | 0 | parse_data->state = STATE_APPLICATION; |
1364 | |
|
1365 | 0 | parse_application_element (context, |
1366 | 0 | parse_data, |
1367 | 0 | attribute_names, |
1368 | 0 | attribute_values, |
1369 | 0 | &inner_error); |
1370 | 0 | if (inner_error) |
1371 | 0 | g_propagate_error (error, inner_error); |
1372 | 0 | } |
1373 | 0 | else |
1374 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1375 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1376 | 0 | _("Unexpected tag “%s”, tag “%s” expected"), |
1377 | 0 | element_name, |
1378 | 0 | BOOKMARK_APPLICATION_ELEMENT); |
1379 | 0 | break; |
1380 | 0 | case STATE_GROUPS: |
1381 | 0 | if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT)) |
1382 | 0 | parse_data->state = STATE_GROUP; |
1383 | 0 | else |
1384 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1385 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1386 | 0 | _("Unexpected tag “%s”, tag “%s” expected"), |
1387 | 0 | element_name, |
1388 | 0 | BOOKMARK_GROUP_ELEMENT); |
1389 | 0 | break; |
1390 | | |
1391 | 0 | case STATE_TITLE: |
1392 | 0 | case STATE_DESC: |
1393 | 0 | case STATE_APPLICATION: |
1394 | 0 | case STATE_GROUP: |
1395 | 0 | case STATE_MIME: |
1396 | 0 | case STATE_ICON: |
1397 | 0 | case STATE_FINISHED: |
1398 | 0 | g_set_error (error, G_MARKUP_ERROR, |
1399 | 0 | G_MARKUP_ERROR_INVALID_CONTENT, |
1400 | 0 | _("Unexpected tag “%s” inside “%s”"), |
1401 | 0 | element_name, |
1402 | 0 | parser_state_to_element_name (parse_data->state)); |
1403 | 0 | break; |
1404 | | |
1405 | 0 | default: |
1406 | 0 | g_assert_not_reached (); |
1407 | 0 | break; |
1408 | 0 | } |
1409 | 0 | } |
1410 | | |
1411 | | static void |
1412 | | end_element_raw_cb (GMarkupParseContext *context, |
1413 | | const gchar *element_name, |
1414 | | gpointer user_data, |
1415 | | GError **error) |
1416 | 0 | { |
1417 | 0 | ParseData *parse_data = (ParseData *) user_data; |
1418 | |
|
1419 | 0 | if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT)) |
1420 | 0 | parse_data->state = STATE_FINISHED; |
1421 | 0 | else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT)) |
1422 | 0 | { |
1423 | 0 | parse_data->current_item = NULL; |
1424 | |
|
1425 | 0 | parse_data->state = STATE_ROOT; |
1426 | 0 | } |
1427 | 0 | else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) || |
1428 | 0 | (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) || |
1429 | 0 | (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))) |
1430 | 0 | { |
1431 | 0 | if (parse_data->current_item) |
1432 | 0 | parse_data->state = STATE_BOOKMARK; |
1433 | 0 | else |
1434 | 0 | parse_data->state = STATE_ROOT; |
1435 | 0 | } |
1436 | 0 | else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT)) |
1437 | 0 | parse_data->state = STATE_INFO; |
1438 | 0 | else if (IS_ELEMENT_NS (parse_data, element_name, |
1439 | 0 | BOOKMARK_NAMESPACE_URI, |
1440 | 0 | BOOKMARK_APPLICATION_ELEMENT)) |
1441 | 0 | parse_data->state = STATE_APPLICATIONS; |
1442 | 0 | else if (IS_ELEMENT_NS (parse_data, element_name, |
1443 | 0 | BOOKMARK_NAMESPACE_URI, |
1444 | 0 | BOOKMARK_GROUP_ELEMENT)) |
1445 | 0 | parse_data->state = STATE_GROUPS; |
1446 | 0 | else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) || |
1447 | 0 | (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) || |
1448 | 0 | (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) || |
1449 | 0 | (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) || |
1450 | 0 | (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))) |
1451 | 0 | parse_data->state = STATE_METADATA; |
1452 | 0 | } |
1453 | | |
1454 | | static void |
1455 | | text_raw_cb (GMarkupParseContext *context, |
1456 | | const gchar *text, |
1457 | | gsize length, |
1458 | | gpointer user_data, |
1459 | | GError **error) |
1460 | 0 | { |
1461 | 0 | ParseData *parse_data = (ParseData *) user_data; |
1462 | 0 | gchar *payload; |
1463 | |
|
1464 | 0 | payload = g_strndup (text, length); |
1465 | |
|
1466 | 0 | switch (parse_data->state) |
1467 | 0 | { |
1468 | 0 | case STATE_TITLE: |
1469 | 0 | if (parse_data->current_item) |
1470 | 0 | { |
1471 | 0 | g_free (parse_data->current_item->title); |
1472 | 0 | parse_data->current_item->title = g_strdup (payload); |
1473 | 0 | } |
1474 | 0 | else |
1475 | 0 | { |
1476 | 0 | g_free (parse_data->bookmark_file->title); |
1477 | 0 | parse_data->bookmark_file->title = g_strdup (payload); |
1478 | 0 | } |
1479 | 0 | break; |
1480 | 0 | case STATE_DESC: |
1481 | 0 | if (parse_data->current_item) |
1482 | 0 | { |
1483 | 0 | g_free (parse_data->current_item->description); |
1484 | 0 | parse_data->current_item->description = g_strdup (payload); |
1485 | 0 | } |
1486 | 0 | else |
1487 | 0 | { |
1488 | 0 | g_free (parse_data->bookmark_file->description); |
1489 | 0 | parse_data->bookmark_file->description = g_strdup (payload); |
1490 | 0 | } |
1491 | 0 | break; |
1492 | 0 | case STATE_GROUP: |
1493 | 0 | { |
1494 | 0 | GList *groups; |
1495 | |
|
1496 | 0 | g_warn_if_fail (parse_data->current_item != NULL); |
1497 | |
|
1498 | 0 | if (!parse_data->current_item->metadata) |
1499 | 0 | parse_data->current_item->metadata = bookmark_metadata_new (); |
1500 | |
|
1501 | 0 | groups = parse_data->current_item->metadata->groups; |
1502 | 0 | parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload)); |
1503 | 0 | } |
1504 | 0 | break; |
1505 | 0 | case STATE_ROOT: |
1506 | 0 | case STATE_BOOKMARK: |
1507 | 0 | case STATE_INFO: |
1508 | 0 | case STATE_METADATA: |
1509 | 0 | case STATE_APPLICATIONS: |
1510 | 0 | case STATE_APPLICATION: |
1511 | 0 | case STATE_GROUPS: |
1512 | 0 | case STATE_MIME: |
1513 | 0 | case STATE_ICON: |
1514 | 0 | break; |
1515 | 0 | default: |
1516 | 0 | g_warn_if_reached (); |
1517 | 0 | break; |
1518 | 0 | } |
1519 | | |
1520 | 0 | g_free (payload); |
1521 | 0 | } |
1522 | | |
1523 | | static const GMarkupParser markup_parser = |
1524 | | { |
1525 | | start_element_raw_cb, /* start_element */ |
1526 | | end_element_raw_cb, /* end_element */ |
1527 | | text_raw_cb, /* text */ |
1528 | | NULL, /* passthrough */ |
1529 | | NULL |
1530 | | }; |
1531 | | |
1532 | | static gboolean |
1533 | | g_bookmark_file_parse (GBookmarkFile *bookmark, |
1534 | | const gchar *buffer, |
1535 | | gsize length, |
1536 | | GError **error) |
1537 | 0 | { |
1538 | 0 | GMarkupParseContext *context; |
1539 | 0 | ParseData *parse_data; |
1540 | 0 | GError *parse_error, *end_error; |
1541 | 0 | gboolean retval; |
1542 | |
|
1543 | 0 | g_warn_if_fail (bookmark != NULL); |
1544 | |
|
1545 | 0 | if (!buffer) |
1546 | 0 | return FALSE; |
1547 | | |
1548 | 0 | parse_error = NULL; |
1549 | 0 | end_error = NULL; |
1550 | |
|
1551 | 0 | if (length == (gsize) -1) |
1552 | 0 | length = strlen (buffer); |
1553 | |
|
1554 | 0 | parse_data = parse_data_new (); |
1555 | 0 | parse_data->bookmark_file = bookmark; |
1556 | |
|
1557 | 0 | context = g_markup_parse_context_new (&markup_parser, |
1558 | 0 | G_MARKUP_DEFAULT_FLAGS, |
1559 | 0 | parse_data, |
1560 | 0 | (GDestroyNotify) parse_data_free); |
1561 | |
|
1562 | 0 | retval = g_markup_parse_context_parse (context, |
1563 | 0 | buffer, |
1564 | 0 | length, |
1565 | 0 | &parse_error); |
1566 | 0 | if (!retval) |
1567 | 0 | g_propagate_error (error, parse_error); |
1568 | 0 | else |
1569 | 0 | { |
1570 | 0 | retval = g_markup_parse_context_end_parse (context, &end_error); |
1571 | 0 | if (!retval) |
1572 | 0 | g_propagate_error (error, end_error); |
1573 | 0 | } |
1574 | |
|
1575 | 0 | g_markup_parse_context_free (context); |
1576 | |
|
1577 | 0 | return retval; |
1578 | 0 | } |
1579 | | |
1580 | | static gchar * |
1581 | | g_bookmark_file_dump (GBookmarkFile *bookmark, |
1582 | | gsize *length, |
1583 | | GError **error) |
1584 | 0 | { |
1585 | 0 | GString *retval; |
1586 | 0 | gchar *buffer; |
1587 | 0 | GList *l; |
1588 | |
|
1589 | 0 | retval = g_string_sized_new (4096); |
1590 | |
|
1591 | 0 | g_string_append (retval, |
1592 | 0 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
1593 | | #if 0 |
1594 | | /* XXX - do we really need the doctype? */ |
1595 | | "<!DOCTYPE " XBEL_DTD_NICK "\n" |
1596 | | " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n" |
1597 | | " \"" XBEL_DTD_URI "\">\n" |
1598 | | #endif |
1599 | 0 | "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n" |
1600 | 0 | " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n" |
1601 | 0 | " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>"); |
1602 | |
|
1603 | 0 | if (bookmark->title) |
1604 | 0 | { |
1605 | 0 | gchar *escaped_title; |
1606 | |
|
1607 | 0 | escaped_title = g_markup_escape_text (bookmark->title, -1); |
1608 | |
|
1609 | 0 | buffer = g_strconcat (" " |
1610 | 0 | "<" XBEL_TITLE_ELEMENT ">", |
1611 | 0 | escaped_title, |
1612 | 0 | "</" XBEL_TITLE_ELEMENT ">\n", NULL); |
1613 | |
|
1614 | 0 | g_string_append (retval, buffer); |
1615 | |
|
1616 | 0 | g_free (buffer); |
1617 | 0 | g_free (escaped_title); |
1618 | 0 | } |
1619 | |
|
1620 | 0 | if (bookmark->description) |
1621 | 0 | { |
1622 | 0 | gchar *escaped_desc; |
1623 | |
|
1624 | 0 | escaped_desc = g_markup_escape_text (bookmark->description, -1); |
1625 | |
|
1626 | 0 | buffer = g_strconcat (" " |
1627 | 0 | "<" XBEL_DESC_ELEMENT ">", |
1628 | 0 | escaped_desc, |
1629 | 0 | "</" XBEL_DESC_ELEMENT ">\n", NULL); |
1630 | 0 | g_string_append (retval, buffer); |
1631 | |
|
1632 | 0 | g_free (buffer); |
1633 | 0 | g_free (escaped_desc); |
1634 | 0 | } |
1635 | |
|
1636 | 0 | if (!bookmark->items) |
1637 | 0 | goto out; |
1638 | 0 | else |
1639 | 0 | retval = g_string_append (retval, "\n"); |
1640 | | |
1641 | | /* the items are stored in reverse order */ |
1642 | 0 | for (l = g_list_last (bookmark->items); |
1643 | 0 | l != NULL; |
1644 | 0 | l = l->prev) |
1645 | 0 | { |
1646 | 0 | BookmarkItem *item = (BookmarkItem *) l->data; |
1647 | 0 | gchar *item_dump; |
1648 | |
|
1649 | 0 | item_dump = bookmark_item_dump (item); |
1650 | 0 | if (!item_dump) |
1651 | 0 | continue; |
1652 | | |
1653 | 0 | retval = g_string_append (retval, item_dump); |
1654 | |
|
1655 | 0 | g_free (item_dump); |
1656 | 0 | } |
1657 | |
|
1658 | 0 | out: |
1659 | 0 | g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">"); |
1660 | |
|
1661 | 0 | if (length) |
1662 | 0 | *length = retval->len; |
1663 | |
|
1664 | 0 | return g_string_free (retval, FALSE); |
1665 | 0 | } |
1666 | | |
1667 | | /************** |
1668 | | * Misc * |
1669 | | **************/ |
1670 | | |
1671 | | static gboolean |
1672 | | timestamp_from_iso8601 (const gchar *iso_date, |
1673 | | GDateTime **out_date_time, |
1674 | | GError **error) |
1675 | 0 | { |
1676 | 0 | GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL); |
1677 | 0 | if (dt == NULL) |
1678 | 0 | { |
1679 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ, |
1680 | 0 | _("Invalid date/time ‘%s’ in bookmark file"), iso_date); |
1681 | 0 | return FALSE; |
1682 | 0 | } |
1683 | | |
1684 | 0 | *out_date_time = g_steal_pointer (&dt); |
1685 | 0 | return TRUE; |
1686 | 0 | } |
1687 | | |
1688 | | G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error) |
1689 | | |
1690 | | /******************** |
1691 | | * Public API * |
1692 | | ********************/ |
1693 | | |
1694 | | /** |
1695 | | * g_bookmark_file_new: (constructor) |
1696 | | * |
1697 | | * Creates a new empty #GBookmarkFile object. |
1698 | | * |
1699 | | * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data() |
1700 | | * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark |
1701 | | * file. |
1702 | | * |
1703 | | * Returns: an empty #GBookmarkFile |
1704 | | * |
1705 | | * Since: 2.12 |
1706 | | */ |
1707 | | GBookmarkFile * |
1708 | | g_bookmark_file_new (void) |
1709 | 0 | { |
1710 | 0 | GBookmarkFile *bookmark; |
1711 | |
|
1712 | 0 | bookmark = g_new (GBookmarkFile, 1); |
1713 | |
|
1714 | 0 | g_bookmark_file_init (bookmark); |
1715 | |
|
1716 | 0 | return bookmark; |
1717 | 0 | } |
1718 | | |
1719 | | /** |
1720 | | * g_bookmark_file_copy: |
1721 | | * @bookmark: A #GBookmarkFile |
1722 | | * |
1723 | | * Deeply copies a @bookmark #GBookmarkFile object to a new one. |
1724 | | * |
1725 | | * Returns: (transfer full): the copy of @bookmark. Use |
1726 | | * g_bookmark_free() when finished using it. |
1727 | | * |
1728 | | * Since: 2.76 |
1729 | | */ |
1730 | | GBookmarkFile * |
1731 | | g_bookmark_file_copy (GBookmarkFile *bookmark) |
1732 | 0 | { |
1733 | 0 | GBookmarkFile *copy; |
1734 | 0 | GList *l; |
1735 | |
|
1736 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
1737 | | |
1738 | 0 | copy = g_bookmark_file_new (); |
1739 | 0 | copy->title = g_strdup (bookmark->title); |
1740 | 0 | copy->description = g_strdup (bookmark->description); |
1741 | 0 | copy->items = g_list_copy_deep (bookmark->items, (GCopyFunc) bookmark_item_copy, NULL); |
1742 | |
|
1743 | 0 | for (l = copy->items; l; l = l->next) |
1744 | 0 | { |
1745 | 0 | BookmarkItem *item = l->data; |
1746 | 0 | g_hash_table_insert (copy->items_by_uri, item->uri, item); |
1747 | 0 | } |
1748 | |
|
1749 | 0 | g_assert (g_hash_table_size (copy->items_by_uri) == |
1750 | 0 | g_hash_table_size (bookmark->items_by_uri)); |
1751 | | |
1752 | 0 | return copy; |
1753 | 0 | } |
1754 | | |
1755 | | /** |
1756 | | * g_bookmark_file_free: |
1757 | | * @bookmark: a #GBookmarkFile |
1758 | | * |
1759 | | * Frees a #GBookmarkFile. |
1760 | | * |
1761 | | * Since: 2.12 |
1762 | | */ |
1763 | | void |
1764 | | g_bookmark_file_free (GBookmarkFile *bookmark) |
1765 | 0 | { |
1766 | 0 | if (!bookmark) |
1767 | 0 | return; |
1768 | | |
1769 | 0 | g_bookmark_file_clear (bookmark); |
1770 | |
|
1771 | 0 | g_free (bookmark); |
1772 | 0 | } |
1773 | | |
1774 | | /** |
1775 | | * g_bookmark_file_load_from_data: |
1776 | | * @bookmark: an empty #GBookmarkFile struct |
1777 | | * @data: (array length=length) (element-type guint8): desktop bookmarks |
1778 | | * loaded in memory |
1779 | | * @length: the length of @data in bytes |
1780 | | * @error: return location for a #GError, or %NULL |
1781 | | * |
1782 | | * Loads a bookmark file from memory into an empty #GBookmarkFile |
1783 | | * structure. If the object cannot be created then @error is set to a |
1784 | | * #GBookmarkFileError. |
1785 | | * |
1786 | | * Returns: %TRUE if a desktop bookmark could be loaded. |
1787 | | * |
1788 | | * Since: 2.12 |
1789 | | */ |
1790 | | gboolean |
1791 | | g_bookmark_file_load_from_data (GBookmarkFile *bookmark, |
1792 | | const gchar *data, |
1793 | | gsize length, |
1794 | | GError **error) |
1795 | 0 | { |
1796 | 0 | GError *parse_error; |
1797 | 0 | gboolean retval; |
1798 | |
|
1799 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
1800 | | |
1801 | 0 | if (length == (gsize) -1) |
1802 | 0 | length = strlen (data); |
1803 | |
|
1804 | 0 | if (bookmark->items) |
1805 | 0 | { |
1806 | 0 | g_bookmark_file_clear (bookmark); |
1807 | 0 | g_bookmark_file_init (bookmark); |
1808 | 0 | } |
1809 | |
|
1810 | 0 | parse_error = NULL; |
1811 | 0 | retval = g_bookmark_file_parse (bookmark, data, length, &parse_error); |
1812 | |
|
1813 | 0 | if (!retval) |
1814 | 0 | g_propagate_error (error, parse_error); |
1815 | |
|
1816 | 0 | return retval; |
1817 | 0 | } |
1818 | | |
1819 | | /** |
1820 | | * g_bookmark_file_load_from_file: |
1821 | | * @bookmark: an empty #GBookmarkFile struct |
1822 | | * @filename: (type filename): the path of a filename to load, in the |
1823 | | * GLib file name encoding |
1824 | | * @error: return location for a #GError, or %NULL |
1825 | | * |
1826 | | * Loads a desktop bookmark file into an empty #GBookmarkFile structure. |
1827 | | * If the file could not be loaded then @error is set to either a #GFileError |
1828 | | * or #GBookmarkFileError. |
1829 | | * |
1830 | | * Returns: %TRUE if a desktop bookmark file could be loaded |
1831 | | * |
1832 | | * Since: 2.12 |
1833 | | */ |
1834 | | gboolean |
1835 | | g_bookmark_file_load_from_file (GBookmarkFile *bookmark, |
1836 | | const gchar *filename, |
1837 | | GError **error) |
1838 | 0 | { |
1839 | 0 | gboolean ret = FALSE; |
1840 | 0 | gchar *buffer = NULL; |
1841 | 0 | gsize len; |
1842 | |
|
1843 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
1844 | 0 | g_return_val_if_fail (filename != NULL, FALSE); |
1845 | | |
1846 | 0 | if (!g_file_get_contents (filename, &buffer, &len, error)) |
1847 | 0 | goto out; |
1848 | | |
1849 | 0 | if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error)) |
1850 | 0 | goto out; |
1851 | | |
1852 | 0 | ret = TRUE; |
1853 | 0 | out: |
1854 | 0 | g_free (buffer); |
1855 | 0 | return ret; |
1856 | 0 | } |
1857 | | |
1858 | | |
1859 | | /* Iterates through all the directories in *dirs trying to |
1860 | | * find file. When it successfully locates file, returns a |
1861 | | * string its absolute path. It also leaves the unchecked |
1862 | | * directories in *dirs. You should free the returned string |
1863 | | * |
1864 | | * Adapted from gkeyfile.c |
1865 | | */ |
1866 | | static gchar * |
1867 | | find_file_in_data_dirs (const gchar *file, |
1868 | | gchar ***dirs, |
1869 | | GError **error) |
1870 | 0 | { |
1871 | 0 | gchar **data_dirs, *data_dir, *path; |
1872 | |
|
1873 | 0 | path = NULL; |
1874 | |
|
1875 | 0 | if (dirs == NULL) |
1876 | 0 | return NULL; |
1877 | | |
1878 | 0 | data_dirs = *dirs; |
1879 | 0 | path = NULL; |
1880 | 0 | while (data_dirs && (data_dir = *data_dirs) && !path) |
1881 | 0 | { |
1882 | 0 | gchar *candidate_file, *sub_dir; |
1883 | |
|
1884 | 0 | candidate_file = (gchar *) file; |
1885 | 0 | sub_dir = g_strdup (""); |
1886 | 0 | while (candidate_file != NULL && !path) |
1887 | 0 | { |
1888 | 0 | gchar *p; |
1889 | |
|
1890 | 0 | path = g_build_filename (data_dir, sub_dir, |
1891 | 0 | candidate_file, NULL); |
1892 | |
|
1893 | 0 | candidate_file = strchr (candidate_file, '-'); |
1894 | |
|
1895 | 0 | if (candidate_file == NULL) |
1896 | 0 | break; |
1897 | | |
1898 | 0 | candidate_file++; |
1899 | |
|
1900 | 0 | g_free (sub_dir); |
1901 | 0 | sub_dir = g_strndup (file, candidate_file - file - 1); |
1902 | |
|
1903 | 0 | for (p = sub_dir; *p != '\0'; p++) |
1904 | 0 | { |
1905 | 0 | if (*p == '-') |
1906 | 0 | *p = G_DIR_SEPARATOR; |
1907 | 0 | } |
1908 | 0 | } |
1909 | 0 | g_free (sub_dir); |
1910 | 0 | data_dirs++; |
1911 | 0 | } |
1912 | |
|
1913 | 0 | *dirs = data_dirs; |
1914 | |
|
1915 | 0 | if (!path) |
1916 | 0 | { |
1917 | 0 | g_set_error_literal (error, G_BOOKMARK_FILE_ERROR, |
1918 | 0 | G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND, |
1919 | 0 | _("No valid bookmark file found in data dirs")); |
1920 | |
|
1921 | 0 | return NULL; |
1922 | 0 | } |
1923 | | |
1924 | 0 | return path; |
1925 | 0 | } |
1926 | | |
1927 | | |
1928 | | /** |
1929 | | * g_bookmark_file_load_from_data_dirs: |
1930 | | * @bookmark: a #GBookmarkFile |
1931 | | * @file: (type filename): a relative path to a filename to open and parse |
1932 | | * @full_path: (out) (optional) (type filename): return location for a string |
1933 | | * containing the full path of the file, or %NULL |
1934 | | * @error: return location for a #GError, or %NULL |
1935 | | * |
1936 | | * This function looks for a desktop bookmark file named @file in the |
1937 | | * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), |
1938 | | * loads the file into @bookmark and returns the file's full path in |
1939 | | * @full_path. If the file could not be loaded then @error is |
1940 | | * set to either a #GFileError or #GBookmarkFileError. |
1941 | | * |
1942 | | * Returns: %TRUE if a key file could be loaded, %FALSE otherwise |
1943 | | * |
1944 | | * Since: 2.12 |
1945 | | */ |
1946 | | gboolean |
1947 | | g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark, |
1948 | | const gchar *file, |
1949 | | gchar **full_path, |
1950 | | GError **error) |
1951 | 0 | { |
1952 | 0 | GError *file_error = NULL; |
1953 | 0 | gchar **all_data_dirs, **data_dirs; |
1954 | 0 | const gchar *user_data_dir; |
1955 | 0 | const gchar * const * system_data_dirs; |
1956 | 0 | gsize i, j; |
1957 | 0 | gchar *output_path; |
1958 | 0 | gboolean found_file; |
1959 | |
|
1960 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
1961 | 0 | g_return_val_if_fail (!g_path_is_absolute (file), FALSE); |
1962 | | |
1963 | 0 | user_data_dir = g_get_user_data_dir (); |
1964 | 0 | system_data_dirs = g_get_system_data_dirs (); |
1965 | 0 | all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2); |
1966 | |
|
1967 | 0 | i = 0; |
1968 | 0 | all_data_dirs[i++] = g_strdup (user_data_dir); |
1969 | |
|
1970 | 0 | j = 0; |
1971 | 0 | while (system_data_dirs[j] != NULL) |
1972 | 0 | all_data_dirs[i++] = g_strdup (system_data_dirs[j++]); |
1973 | |
|
1974 | 0 | found_file = FALSE; |
1975 | 0 | data_dirs = all_data_dirs; |
1976 | 0 | output_path = NULL; |
1977 | 0 | while (*data_dirs != NULL && !found_file) |
1978 | 0 | { |
1979 | 0 | g_free (output_path); |
1980 | |
|
1981 | 0 | output_path = find_file_in_data_dirs (file, &data_dirs, &file_error); |
1982 | |
|
1983 | 0 | if (file_error) |
1984 | 0 | { |
1985 | 0 | g_propagate_error (error, file_error); |
1986 | 0 | break; |
1987 | 0 | } |
1988 | | |
1989 | 0 | found_file = g_bookmark_file_load_from_file (bookmark, |
1990 | 0 | output_path, |
1991 | 0 | &file_error); |
1992 | 0 | if (file_error) |
1993 | 0 | { |
1994 | 0 | g_propagate_error (error, file_error); |
1995 | 0 | break; |
1996 | 0 | } |
1997 | 0 | } |
1998 | |
|
1999 | 0 | if (found_file && full_path) |
2000 | 0 | *full_path = output_path; |
2001 | 0 | else |
2002 | 0 | g_free (output_path); |
2003 | |
|
2004 | 0 | g_strfreev (all_data_dirs); |
2005 | |
|
2006 | 0 | return found_file; |
2007 | 0 | } |
2008 | | |
2009 | | |
2010 | | /** |
2011 | | * g_bookmark_file_to_data: |
2012 | | * @bookmark: a #GBookmarkFile |
2013 | | * @length: (out) (optional): return location for the length of the returned string, or %NULL |
2014 | | * @error: return location for a #GError, or %NULL |
2015 | | * |
2016 | | * This function outputs @bookmark as a string. |
2017 | | * |
2018 | | * Returns: (transfer full) (array length=length) (element-type guint8): |
2019 | | * a newly allocated string holding the contents of the #GBookmarkFile |
2020 | | * |
2021 | | * Since: 2.12 |
2022 | | */ |
2023 | | gchar * |
2024 | | g_bookmark_file_to_data (GBookmarkFile *bookmark, |
2025 | | gsize *length, |
2026 | | GError **error) |
2027 | 0 | { |
2028 | 0 | GError *write_error = NULL; |
2029 | 0 | gchar *retval; |
2030 | |
|
2031 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2032 | | |
2033 | 0 | retval = g_bookmark_file_dump (bookmark, length, &write_error); |
2034 | 0 | if (write_error) |
2035 | 0 | { |
2036 | 0 | g_propagate_error (error, write_error); |
2037 | |
|
2038 | 0 | return NULL; |
2039 | 0 | } |
2040 | | |
2041 | 0 | return retval; |
2042 | 0 | } |
2043 | | |
2044 | | /** |
2045 | | * g_bookmark_file_to_file: |
2046 | | * @bookmark: a #GBookmarkFile |
2047 | | * @filename: (type filename): path of the output file |
2048 | | * @error: return location for a #GError, or %NULL |
2049 | | * |
2050 | | * This function outputs @bookmark into a file. The write process is |
2051 | | * guaranteed to be atomic by using g_file_set_contents() internally. |
2052 | | * |
2053 | | * Returns: %TRUE if the file was successfully written. |
2054 | | * |
2055 | | * Since: 2.12 |
2056 | | */ |
2057 | | gboolean |
2058 | | g_bookmark_file_to_file (GBookmarkFile *bookmark, |
2059 | | const gchar *filename, |
2060 | | GError **error) |
2061 | 0 | { |
2062 | 0 | gchar *data; |
2063 | 0 | GError *data_error, *write_error; |
2064 | 0 | gsize len; |
2065 | 0 | gboolean retval; |
2066 | |
|
2067 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
2068 | 0 | g_return_val_if_fail (filename != NULL, FALSE); |
2069 | | |
2070 | 0 | data_error = NULL; |
2071 | 0 | data = g_bookmark_file_to_data (bookmark, &len, &data_error); |
2072 | 0 | if (data_error) |
2073 | 0 | { |
2074 | 0 | g_propagate_error (error, data_error); |
2075 | |
|
2076 | 0 | return FALSE; |
2077 | 0 | } |
2078 | | |
2079 | 0 | write_error = NULL; |
2080 | 0 | g_file_set_contents (filename, data, len, &write_error); |
2081 | 0 | if (write_error) |
2082 | 0 | { |
2083 | 0 | g_propagate_error (error, write_error); |
2084 | |
|
2085 | 0 | retval = FALSE; |
2086 | 0 | } |
2087 | 0 | else |
2088 | 0 | retval = TRUE; |
2089 | |
|
2090 | 0 | g_free (data); |
2091 | |
|
2092 | 0 | return retval; |
2093 | 0 | } |
2094 | | |
2095 | | static BookmarkItem * |
2096 | | g_bookmark_file_lookup_item (GBookmarkFile *bookmark, |
2097 | | const gchar *uri) |
2098 | 0 | { |
2099 | 0 | g_warn_if_fail (bookmark != NULL && uri != NULL); |
2100 | |
|
2101 | 0 | return g_hash_table_lookup (bookmark->items_by_uri, uri); |
2102 | 0 | } |
2103 | | |
2104 | | /* this function adds a new item to the list */ |
2105 | | static void |
2106 | | g_bookmark_file_add_item (GBookmarkFile *bookmark, |
2107 | | BookmarkItem *item, |
2108 | | GError **error) |
2109 | 0 | { |
2110 | 0 | g_warn_if_fail (bookmark != NULL); |
2111 | 0 | g_warn_if_fail (item != NULL); |
2112 | | |
2113 | | /* this should never happen; and if it does, then we are |
2114 | | * screwing up something big time. |
2115 | | */ |
2116 | 0 | if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri))) |
2117 | 0 | { |
2118 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2119 | 0 | G_BOOKMARK_FILE_ERROR_INVALID_URI, |
2120 | 0 | _("A bookmark for URI “%s” already exists"), |
2121 | 0 | item->uri); |
2122 | 0 | return; |
2123 | 0 | } |
2124 | | |
2125 | 0 | bookmark->items = g_list_prepend (bookmark->items, item); |
2126 | |
|
2127 | 0 | g_hash_table_replace (bookmark->items_by_uri, |
2128 | 0 | item->uri, |
2129 | 0 | item); |
2130 | |
|
2131 | 0 | if (item->added == NULL) |
2132 | 0 | item->added = g_date_time_new_now_utc (); |
2133 | |
|
2134 | 0 | if (item->modified == NULL) |
2135 | 0 | item->modified = g_date_time_new_now_utc (); |
2136 | |
|
2137 | 0 | if (item->visited == NULL) |
2138 | 0 | item->visited = g_date_time_new_now_utc (); |
2139 | 0 | } |
2140 | | |
2141 | | /** |
2142 | | * g_bookmark_file_remove_item: |
2143 | | * @bookmark: a #GBookmarkFile |
2144 | | * @uri: a valid URI |
2145 | | * @error: return location for a #GError, or %NULL |
2146 | | * |
2147 | | * Removes the bookmark for @uri from the bookmark file @bookmark. |
2148 | | * |
2149 | | * Returns: %TRUE if the bookmark was removed successfully. |
2150 | | * |
2151 | | * Since: 2.12 |
2152 | | */ |
2153 | | gboolean |
2154 | | g_bookmark_file_remove_item (GBookmarkFile *bookmark, |
2155 | | const gchar *uri, |
2156 | | GError **error) |
2157 | 0 | { |
2158 | 0 | BookmarkItem *item; |
2159 | |
|
2160 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
2161 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
2162 | | |
2163 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2164 | |
|
2165 | 0 | if (!item) |
2166 | 0 | { |
2167 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2168 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2169 | 0 | _("No bookmark found for URI “%s”"), |
2170 | 0 | uri); |
2171 | 0 | return FALSE; |
2172 | 0 | } |
2173 | | |
2174 | 0 | bookmark->items = g_list_remove (bookmark->items, item); |
2175 | 0 | g_hash_table_remove (bookmark->items_by_uri, item->uri); |
2176 | |
|
2177 | 0 | bookmark_item_free (item); |
2178 | |
|
2179 | 0 | return TRUE; |
2180 | 0 | } |
2181 | | |
2182 | | /** |
2183 | | * g_bookmark_file_has_item: |
2184 | | * @bookmark: a #GBookmarkFile |
2185 | | * @uri: a valid URI |
2186 | | * |
2187 | | * Looks whether the desktop bookmark has an item with its URI set to @uri. |
2188 | | * |
2189 | | * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise |
2190 | | * |
2191 | | * Since: 2.12 |
2192 | | */ |
2193 | | gboolean |
2194 | | g_bookmark_file_has_item (GBookmarkFile *bookmark, |
2195 | | const gchar *uri) |
2196 | 0 | { |
2197 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
2198 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
2199 | | |
2200 | 0 | return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri)); |
2201 | 0 | } |
2202 | | |
2203 | | /** |
2204 | | * g_bookmark_file_get_uris: |
2205 | | * @bookmark: a #GBookmarkFile |
2206 | | * @length: (out) (optional): return location for the number of returned URIs, or %NULL |
2207 | | * |
2208 | | * Returns all URIs of the bookmarks in the bookmark file @bookmark. |
2209 | | * The array of returned URIs will be %NULL-terminated, so @length may |
2210 | | * optionally be %NULL. |
2211 | | * |
2212 | | * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings. |
2213 | | * Use g_strfreev() to free it. |
2214 | | * |
2215 | | * Since: 2.12 |
2216 | | */ |
2217 | | gchar ** |
2218 | | g_bookmark_file_get_uris (GBookmarkFile *bookmark, |
2219 | | gsize *length) |
2220 | 0 | { |
2221 | 0 | GList *l; |
2222 | 0 | gchar **uris; |
2223 | 0 | gsize i, n_items; |
2224 | |
|
2225 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2226 | | |
2227 | 0 | n_items = g_list_length (bookmark->items); |
2228 | 0 | uris = g_new0 (gchar *, n_items + 1); |
2229 | | |
2230 | | /* the items are stored in reverse order, so we walk the list backward */ |
2231 | 0 | for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev) |
2232 | 0 | { |
2233 | 0 | BookmarkItem *item = (BookmarkItem *) l->data; |
2234 | |
|
2235 | 0 | g_warn_if_fail (item != NULL); |
2236 | |
|
2237 | 0 | uris[i++] = g_strdup (item->uri); |
2238 | 0 | } |
2239 | 0 | uris[i] = NULL; |
2240 | |
|
2241 | 0 | if (length) |
2242 | 0 | *length = i; |
2243 | |
|
2244 | 0 | return uris; |
2245 | 0 | } |
2246 | | |
2247 | | /** |
2248 | | * g_bookmark_file_set_title: |
2249 | | * @bookmark: a #GBookmarkFile |
2250 | | * @uri: (nullable): a valid URI or %NULL |
2251 | | * @title: a UTF-8 encoded string |
2252 | | * |
2253 | | * Sets @title as the title of the bookmark for @uri inside the |
2254 | | * bookmark file @bookmark. |
2255 | | * |
2256 | | * If @uri is %NULL, the title of @bookmark is set. |
2257 | | * |
2258 | | * If a bookmark for @uri cannot be found then it is created. |
2259 | | * |
2260 | | * Since: 2.12 |
2261 | | */ |
2262 | | void |
2263 | | g_bookmark_file_set_title (GBookmarkFile *bookmark, |
2264 | | const gchar *uri, |
2265 | | const gchar *title) |
2266 | 0 | { |
2267 | 0 | g_return_if_fail (bookmark != NULL); |
2268 | | |
2269 | 0 | if (!uri) |
2270 | 0 | { |
2271 | 0 | g_free (bookmark->title); |
2272 | 0 | bookmark->title = g_strdup (title); |
2273 | 0 | } |
2274 | 0 | else |
2275 | 0 | { |
2276 | 0 | BookmarkItem *item; |
2277 | |
|
2278 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2279 | 0 | if (!item) |
2280 | 0 | { |
2281 | 0 | item = bookmark_item_new (uri); |
2282 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2283 | 0 | } |
2284 | |
|
2285 | 0 | g_free (item->title); |
2286 | 0 | item->title = g_strdup (title); |
2287 | |
|
2288 | 0 | bookmark_item_touch_modified (item); |
2289 | 0 | } |
2290 | 0 | } |
2291 | | |
2292 | | /** |
2293 | | * g_bookmark_file_get_title: |
2294 | | * @bookmark: a #GBookmarkFile |
2295 | | * @uri: (nullable): a valid URI or %NULL |
2296 | | * @error: return location for a #GError, or %NULL |
2297 | | * |
2298 | | * Returns the title of the bookmark for @uri. |
2299 | | * |
2300 | | * If @uri is %NULL, the title of @bookmark is returned. |
2301 | | * |
2302 | | * In the event the URI cannot be found, %NULL is returned and |
2303 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2304 | | * |
2305 | | * Returns: (transfer full): a newly allocated string or %NULL if the specified |
2306 | | * URI cannot be found. |
2307 | | * |
2308 | | * Since: 2.12 |
2309 | | */ |
2310 | | gchar * |
2311 | | g_bookmark_file_get_title (GBookmarkFile *bookmark, |
2312 | | const gchar *uri, |
2313 | | GError **error) |
2314 | 0 | { |
2315 | 0 | BookmarkItem *item; |
2316 | |
|
2317 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2318 | | |
2319 | 0 | if (!uri) |
2320 | 0 | return g_strdup (bookmark->title); |
2321 | | |
2322 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2323 | 0 | if (!item) |
2324 | 0 | { |
2325 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2326 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2327 | 0 | _("No bookmark found for URI “%s”"), |
2328 | 0 | uri); |
2329 | 0 | return NULL; |
2330 | 0 | } |
2331 | | |
2332 | 0 | return g_strdup (item->title); |
2333 | 0 | } |
2334 | | |
2335 | | /** |
2336 | | * g_bookmark_file_set_description: |
2337 | | * @bookmark: a #GBookmarkFile |
2338 | | * @uri: (nullable): a valid URI or %NULL |
2339 | | * @description: a string |
2340 | | * |
2341 | | * Sets @description as the description of the bookmark for @uri. |
2342 | | * |
2343 | | * If @uri is %NULL, the description of @bookmark is set. |
2344 | | * |
2345 | | * If a bookmark for @uri cannot be found then it is created. |
2346 | | * |
2347 | | * Since: 2.12 |
2348 | | */ |
2349 | | void |
2350 | | g_bookmark_file_set_description (GBookmarkFile *bookmark, |
2351 | | const gchar *uri, |
2352 | | const gchar *description) |
2353 | 0 | { |
2354 | 0 | g_return_if_fail (bookmark != NULL); |
2355 | | |
2356 | 0 | if (!uri) |
2357 | 0 | { |
2358 | 0 | g_free (bookmark->description); |
2359 | 0 | bookmark->description = g_strdup (description); |
2360 | 0 | } |
2361 | 0 | else |
2362 | 0 | { |
2363 | 0 | BookmarkItem *item; |
2364 | |
|
2365 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2366 | 0 | if (!item) |
2367 | 0 | { |
2368 | 0 | item = bookmark_item_new (uri); |
2369 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2370 | 0 | } |
2371 | |
|
2372 | 0 | g_free (item->description); |
2373 | 0 | item->description = g_strdup (description); |
2374 | |
|
2375 | 0 | bookmark_item_touch_modified (item); |
2376 | 0 | } |
2377 | 0 | } |
2378 | | |
2379 | | /** |
2380 | | * g_bookmark_file_get_description: |
2381 | | * @bookmark: a #GBookmarkFile |
2382 | | * @uri: a valid URI |
2383 | | * @error: return location for a #GError, or %NULL |
2384 | | * |
2385 | | * Retrieves the description of the bookmark for @uri. |
2386 | | * |
2387 | | * In the event the URI cannot be found, %NULL is returned and |
2388 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2389 | | * |
2390 | | * Returns: (transfer full): a newly allocated string or %NULL if the specified |
2391 | | * URI cannot be found. |
2392 | | * |
2393 | | * Since: 2.12 |
2394 | | */ |
2395 | | gchar * |
2396 | | g_bookmark_file_get_description (GBookmarkFile *bookmark, |
2397 | | const gchar *uri, |
2398 | | GError **error) |
2399 | 0 | { |
2400 | 0 | BookmarkItem *item; |
2401 | |
|
2402 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2403 | | |
2404 | 0 | if (!uri) |
2405 | 0 | return g_strdup (bookmark->description); |
2406 | | |
2407 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2408 | 0 | if (!item) |
2409 | 0 | { |
2410 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2411 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2412 | 0 | _("No bookmark found for URI “%s”"), |
2413 | 0 | uri); |
2414 | 0 | return NULL; |
2415 | 0 | } |
2416 | | |
2417 | 0 | return g_strdup (item->description); |
2418 | 0 | } |
2419 | | |
2420 | | /** |
2421 | | * g_bookmark_file_set_mime_type: |
2422 | | * @bookmark: a #GBookmarkFile |
2423 | | * @uri: a valid URI |
2424 | | * @mime_type: a MIME type |
2425 | | * |
2426 | | * Sets @mime_type as the MIME type of the bookmark for @uri. |
2427 | | * |
2428 | | * If a bookmark for @uri cannot be found then it is created. |
2429 | | * |
2430 | | * Since: 2.12 |
2431 | | */ |
2432 | | void |
2433 | | g_bookmark_file_set_mime_type (GBookmarkFile *bookmark, |
2434 | | const gchar *uri, |
2435 | | const gchar *mime_type) |
2436 | 0 | { |
2437 | 0 | BookmarkItem *item; |
2438 | |
|
2439 | 0 | g_return_if_fail (bookmark != NULL); |
2440 | 0 | g_return_if_fail (uri != NULL); |
2441 | 0 | g_return_if_fail (mime_type != NULL); |
2442 | | |
2443 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2444 | 0 | if (!item) |
2445 | 0 | { |
2446 | 0 | item = bookmark_item_new (uri); |
2447 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2448 | 0 | } |
2449 | |
|
2450 | 0 | if (!item->metadata) |
2451 | 0 | item->metadata = bookmark_metadata_new (); |
2452 | |
|
2453 | 0 | g_free (item->metadata->mime_type); |
2454 | |
|
2455 | 0 | item->metadata->mime_type = g_strdup (mime_type); |
2456 | 0 | bookmark_item_touch_modified (item); |
2457 | 0 | } |
2458 | | |
2459 | | /** |
2460 | | * g_bookmark_file_get_mime_type: |
2461 | | * @bookmark: a #GBookmarkFile |
2462 | | * @uri: a valid URI |
2463 | | * @error: return location for a #GError, or %NULL |
2464 | | * |
2465 | | * Retrieves the MIME type of the resource pointed by @uri. |
2466 | | * |
2467 | | * In the event the URI cannot be found, %NULL is returned and |
2468 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the |
2469 | | * event that the MIME type cannot be found, %NULL is returned and |
2470 | | * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE. |
2471 | | * |
2472 | | * Returns: (transfer full): a newly allocated string or %NULL if the specified |
2473 | | * URI cannot be found. |
2474 | | * |
2475 | | * Since: 2.12 |
2476 | | */ |
2477 | | gchar * |
2478 | | g_bookmark_file_get_mime_type (GBookmarkFile *bookmark, |
2479 | | const gchar *uri, |
2480 | | GError **error) |
2481 | 0 | { |
2482 | 0 | BookmarkItem *item; |
2483 | |
|
2484 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2485 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
2486 | | |
2487 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2488 | 0 | if (!item) |
2489 | 0 | { |
2490 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2491 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2492 | 0 | _("No bookmark found for URI “%s”"), |
2493 | 0 | uri); |
2494 | 0 | return NULL; |
2495 | 0 | } |
2496 | | |
2497 | 0 | if (!item->metadata) |
2498 | 0 | { |
2499 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2500 | 0 | G_BOOKMARK_FILE_ERROR_INVALID_VALUE, |
2501 | 0 | _("No MIME type defined in the bookmark for URI “%s”"), |
2502 | 0 | uri); |
2503 | 0 | return NULL; |
2504 | 0 | } |
2505 | | |
2506 | 0 | return g_strdup (item->metadata->mime_type); |
2507 | 0 | } |
2508 | | |
2509 | | /** |
2510 | | * g_bookmark_file_set_is_private: |
2511 | | * @bookmark: a #GBookmarkFile |
2512 | | * @uri: a valid URI |
2513 | | * @is_private: %TRUE if the bookmark should be marked as private |
2514 | | * |
2515 | | * Sets the private flag of the bookmark for @uri. |
2516 | | * |
2517 | | * If a bookmark for @uri cannot be found then it is created. |
2518 | | * |
2519 | | * Since: 2.12 |
2520 | | */ |
2521 | | void |
2522 | | g_bookmark_file_set_is_private (GBookmarkFile *bookmark, |
2523 | | const gchar *uri, |
2524 | | gboolean is_private) |
2525 | 0 | { |
2526 | 0 | BookmarkItem *item; |
2527 | |
|
2528 | 0 | g_return_if_fail (bookmark != NULL); |
2529 | 0 | g_return_if_fail (uri != NULL); |
2530 | | |
2531 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2532 | 0 | if (!item) |
2533 | 0 | { |
2534 | 0 | item = bookmark_item_new (uri); |
2535 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2536 | 0 | } |
2537 | |
|
2538 | 0 | if (!item->metadata) |
2539 | 0 | item->metadata = bookmark_metadata_new (); |
2540 | |
|
2541 | 0 | item->metadata->is_private = (is_private == TRUE); |
2542 | 0 | bookmark_item_touch_modified (item); |
2543 | 0 | } |
2544 | | |
2545 | | /** |
2546 | | * g_bookmark_file_get_is_private: |
2547 | | * @bookmark: a #GBookmarkFile |
2548 | | * @uri: a valid URI |
2549 | | * @error: return location for a #GError, or %NULL |
2550 | | * |
2551 | | * Gets whether the private flag of the bookmark for @uri is set. |
2552 | | * |
2553 | | * In the event the URI cannot be found, %FALSE is returned and |
2554 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the |
2555 | | * event that the private flag cannot be found, %FALSE is returned and |
2556 | | * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE. |
2557 | | * |
2558 | | * Returns: %TRUE if the private flag is set, %FALSE otherwise. |
2559 | | * |
2560 | | * Since: 2.12 |
2561 | | */ |
2562 | | gboolean |
2563 | | g_bookmark_file_get_is_private (GBookmarkFile *bookmark, |
2564 | | const gchar *uri, |
2565 | | GError **error) |
2566 | 0 | { |
2567 | 0 | BookmarkItem *item; |
2568 | |
|
2569 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
2570 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
2571 | | |
2572 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2573 | 0 | if (!item) |
2574 | 0 | { |
2575 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2576 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2577 | 0 | _("No bookmark found for URI “%s”"), |
2578 | 0 | uri); |
2579 | 0 | return FALSE; |
2580 | 0 | } |
2581 | | |
2582 | 0 | if (!item->metadata) |
2583 | 0 | { |
2584 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2585 | 0 | G_BOOKMARK_FILE_ERROR_INVALID_VALUE, |
2586 | 0 | _("No private flag has been defined in bookmark for URI “%s”"), |
2587 | 0 | uri); |
2588 | 0 | return FALSE; |
2589 | 0 | } |
2590 | | |
2591 | 0 | return item->metadata->is_private; |
2592 | 0 | } |
2593 | | |
2594 | | /** |
2595 | | * g_bookmark_file_set_added: |
2596 | | * @bookmark: a #GBookmarkFile |
2597 | | * @uri: a valid URI |
2598 | | * @added: a timestamp or -1 to use the current time |
2599 | | * |
2600 | | * Sets the time the bookmark for @uri was added into @bookmark. |
2601 | | * |
2602 | | * If no bookmark for @uri is found then it is created. |
2603 | | * |
2604 | | * Since: 2.12 |
2605 | | * Deprecated: 2.66: Use g_bookmark_file_set_added_date_time() instead, as |
2606 | | * `time_t` is deprecated due to the year 2038 problem. |
2607 | | */ |
2608 | | void |
2609 | | g_bookmark_file_set_added (GBookmarkFile *bookmark, |
2610 | | const gchar *uri, |
2611 | | time_t added) |
2612 | 0 | { |
2613 | 0 | GDateTime *added_dt = (added != (time_t) -1) ? g_date_time_new_from_unix_utc (added) : g_date_time_new_now_utc (); |
2614 | 0 | g_bookmark_file_set_added_date_time (bookmark, uri, added_dt); |
2615 | 0 | g_date_time_unref (added_dt); |
2616 | 0 | } |
2617 | | |
2618 | | /** |
2619 | | * g_bookmark_file_set_added_date_time: |
2620 | | * @bookmark: a #GBookmarkFile |
2621 | | * @uri: a valid URI |
2622 | | * @added: a #GDateTime |
2623 | | * |
2624 | | * Sets the time the bookmark for @uri was added into @bookmark. |
2625 | | * |
2626 | | * If no bookmark for @uri is found then it is created. |
2627 | | * |
2628 | | * Since: 2.66 |
2629 | | */ |
2630 | | void |
2631 | | g_bookmark_file_set_added_date_time (GBookmarkFile *bookmark, |
2632 | | const char *uri, |
2633 | | GDateTime *added) |
2634 | 0 | { |
2635 | 0 | BookmarkItem *item; |
2636 | |
|
2637 | 0 | g_return_if_fail (bookmark != NULL); |
2638 | 0 | g_return_if_fail (uri != NULL); |
2639 | 0 | g_return_if_fail (added != NULL); |
2640 | | |
2641 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2642 | 0 | if (!item) |
2643 | 0 | { |
2644 | 0 | item = bookmark_item_new (uri); |
2645 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2646 | 0 | } |
2647 | |
|
2648 | 0 | g_clear_pointer (&item->added, g_date_time_unref); |
2649 | 0 | item->added = g_date_time_ref (added); |
2650 | 0 | g_clear_pointer (&item->modified, g_date_time_unref); |
2651 | 0 | item->modified = g_date_time_ref (added); |
2652 | 0 | } |
2653 | | |
2654 | | /** |
2655 | | * g_bookmark_file_get_added: |
2656 | | * @bookmark: a #GBookmarkFile |
2657 | | * @uri: a valid URI |
2658 | | * @error: return location for a #GError, or %NULL |
2659 | | * |
2660 | | * Gets the time the bookmark for @uri was added to @bookmark |
2661 | | * |
2662 | | * In the event the URI cannot be found, -1 is returned and |
2663 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2664 | | * |
2665 | | * Returns: a timestamp |
2666 | | * |
2667 | | * Since: 2.12 |
2668 | | * Deprecated: 2.66: Use g_bookmark_file_get_added_date_time() instead, as |
2669 | | * `time_t` is deprecated due to the year 2038 problem. |
2670 | | */ |
2671 | | time_t |
2672 | | g_bookmark_file_get_added (GBookmarkFile *bookmark, |
2673 | | const gchar *uri, |
2674 | | GError **error) |
2675 | 0 | { |
2676 | 0 | GDateTime *added = g_bookmark_file_get_added_date_time (bookmark, uri, error); |
2677 | 0 | return (added != NULL) ? g_date_time_to_unix (added) : (time_t) -1; |
2678 | 0 | } |
2679 | | |
2680 | | /** |
2681 | | * g_bookmark_file_get_added_date_time: |
2682 | | * @bookmark: a #GBookmarkFile |
2683 | | * @uri: a valid URI |
2684 | | * @error: return location for a #GError, or %NULL |
2685 | | * |
2686 | | * Gets the time the bookmark for @uri was added to @bookmark |
2687 | | * |
2688 | | * In the event the URI cannot be found, %NULL is returned and |
2689 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2690 | | * |
2691 | | * Returns: (transfer none): a #GDateTime |
2692 | | * |
2693 | | * Since: 2.66 |
2694 | | */ |
2695 | | GDateTime * |
2696 | | g_bookmark_file_get_added_date_time (GBookmarkFile *bookmark, |
2697 | | const char *uri, |
2698 | | GError **error) |
2699 | 0 | { |
2700 | 0 | BookmarkItem *item; |
2701 | |
|
2702 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2703 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
2704 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2705 | | |
2706 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2707 | 0 | if (!item) |
2708 | 0 | { |
2709 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2710 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2711 | 0 | _("No bookmark found for URI “%s”"), |
2712 | 0 | uri); |
2713 | 0 | return NULL; |
2714 | 0 | } |
2715 | | |
2716 | 0 | return item->added; |
2717 | 0 | } |
2718 | | |
2719 | | /** |
2720 | | * g_bookmark_file_set_modified: |
2721 | | * @bookmark: a #GBookmarkFile |
2722 | | * @uri: a valid URI |
2723 | | * @modified: a timestamp or -1 to use the current time |
2724 | | * |
2725 | | * Sets the last time the bookmark for @uri was last modified. |
2726 | | * |
2727 | | * If no bookmark for @uri is found then it is created. |
2728 | | * |
2729 | | * The "modified" time should only be set when the bookmark's meta-data |
2730 | | * was actually changed. Every function of #GBookmarkFile that |
2731 | | * modifies a bookmark also changes the modification time, except for |
2732 | | * g_bookmark_file_set_visited_date_time(). |
2733 | | * |
2734 | | * Since: 2.12 |
2735 | | * Deprecated: 2.66: Use g_bookmark_file_set_modified_date_time() instead, as |
2736 | | * `time_t` is deprecated due to the year 2038 problem. |
2737 | | */ |
2738 | | void |
2739 | | g_bookmark_file_set_modified (GBookmarkFile *bookmark, |
2740 | | const gchar *uri, |
2741 | | time_t modified) |
2742 | 0 | { |
2743 | 0 | GDateTime *modified_dt = (modified != (time_t) -1) ? g_date_time_new_from_unix_utc (modified) : g_date_time_new_now_utc (); |
2744 | 0 | g_bookmark_file_set_modified_date_time (bookmark, uri, modified_dt); |
2745 | 0 | g_date_time_unref (modified_dt); |
2746 | 0 | } |
2747 | | |
2748 | | /** |
2749 | | * g_bookmark_file_set_modified_date_time: |
2750 | | * @bookmark: a #GBookmarkFile |
2751 | | * @uri: a valid URI |
2752 | | * @modified: a #GDateTime |
2753 | | * |
2754 | | * Sets the last time the bookmark for @uri was last modified. |
2755 | | * |
2756 | | * If no bookmark for @uri is found then it is created. |
2757 | | * |
2758 | | * The "modified" time should only be set when the bookmark's meta-data |
2759 | | * was actually changed. Every function of #GBookmarkFile that |
2760 | | * modifies a bookmark also changes the modification time, except for |
2761 | | * g_bookmark_file_set_visited_date_time(). |
2762 | | * |
2763 | | * Since: 2.66 |
2764 | | */ |
2765 | | void |
2766 | | g_bookmark_file_set_modified_date_time (GBookmarkFile *bookmark, |
2767 | | const char *uri, |
2768 | | GDateTime *modified) |
2769 | 0 | { |
2770 | 0 | BookmarkItem *item; |
2771 | |
|
2772 | 0 | g_return_if_fail (bookmark != NULL); |
2773 | 0 | g_return_if_fail (uri != NULL); |
2774 | 0 | g_return_if_fail (modified != NULL); |
2775 | | |
2776 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2777 | 0 | if (!item) |
2778 | 0 | { |
2779 | 0 | item = bookmark_item_new (uri); |
2780 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2781 | 0 | } |
2782 | |
|
2783 | 0 | g_clear_pointer (&item->modified, g_date_time_unref); |
2784 | 0 | item->modified = g_date_time_ref (modified); |
2785 | 0 | } |
2786 | | |
2787 | | /** |
2788 | | * g_bookmark_file_get_modified: |
2789 | | * @bookmark: a #GBookmarkFile |
2790 | | * @uri: a valid URI |
2791 | | * @error: return location for a #GError, or %NULL |
2792 | | * |
2793 | | * Gets the time when the bookmark for @uri was last modified. |
2794 | | * |
2795 | | * In the event the URI cannot be found, -1 is returned and |
2796 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2797 | | * |
2798 | | * Returns: a timestamp |
2799 | | * |
2800 | | * Since: 2.12 |
2801 | | * Deprecated: 2.66: Use g_bookmark_file_get_modified_date_time() instead, as |
2802 | | * `time_t` is deprecated due to the year 2038 problem. |
2803 | | */ |
2804 | | time_t |
2805 | | g_bookmark_file_get_modified (GBookmarkFile *bookmark, |
2806 | | const gchar *uri, |
2807 | | GError **error) |
2808 | 0 | { |
2809 | 0 | GDateTime *modified = g_bookmark_file_get_modified_date_time (bookmark, uri, error); |
2810 | 0 | return (modified != NULL) ? g_date_time_to_unix (modified) : (time_t) -1; |
2811 | 0 | } |
2812 | | |
2813 | | /** |
2814 | | * g_bookmark_file_get_modified_date_time: |
2815 | | * @bookmark: a #GBookmarkFile |
2816 | | * @uri: a valid URI |
2817 | | * @error: return location for a #GError, or %NULL |
2818 | | * |
2819 | | * Gets the time when the bookmark for @uri was last modified. |
2820 | | * |
2821 | | * In the event the URI cannot be found, %NULL is returned and |
2822 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2823 | | * |
2824 | | * Returns: (transfer none): a #GDateTime |
2825 | | * |
2826 | | * Since: 2.66 |
2827 | | */ |
2828 | | GDateTime * |
2829 | | g_bookmark_file_get_modified_date_time (GBookmarkFile *bookmark, |
2830 | | const char *uri, |
2831 | | GError **error) |
2832 | 0 | { |
2833 | 0 | BookmarkItem *item; |
2834 | |
|
2835 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2836 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
2837 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2838 | | |
2839 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2840 | 0 | if (!item) |
2841 | 0 | { |
2842 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2843 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2844 | 0 | _("No bookmark found for URI “%s”"), |
2845 | 0 | uri); |
2846 | 0 | return NULL; |
2847 | 0 | } |
2848 | | |
2849 | 0 | return item->modified; |
2850 | 0 | } |
2851 | | |
2852 | | /** |
2853 | | * g_bookmark_file_set_visited: |
2854 | | * @bookmark: a #GBookmarkFile |
2855 | | * @uri: a valid URI |
2856 | | * @visited: a timestamp or -1 to use the current time |
2857 | | * |
2858 | | * Sets the time the bookmark for @uri was last visited. |
2859 | | * |
2860 | | * If no bookmark for @uri is found then it is created. |
2861 | | * |
2862 | | * The "visited" time should only be set if the bookmark was launched, |
2863 | | * either using the command line retrieved by g_bookmark_file_get_application_info() |
2864 | | * or by the default application for the bookmark's MIME type, retrieved |
2865 | | * using g_bookmark_file_get_mime_type(). Changing the "visited" time |
2866 | | * does not affect the "modified" time. |
2867 | | * |
2868 | | * Since: 2.12 |
2869 | | * Deprecated: 2.66: Use g_bookmark_file_set_visited_date_time() instead, as |
2870 | | * `time_t` is deprecated due to the year 2038 problem. |
2871 | | */ |
2872 | | void |
2873 | | g_bookmark_file_set_visited (GBookmarkFile *bookmark, |
2874 | | const gchar *uri, |
2875 | | time_t visited) |
2876 | 0 | { |
2877 | 0 | GDateTime *visited_dt = (visited != (time_t) -1) ? g_date_time_new_from_unix_utc (visited) : g_date_time_new_now_utc (); |
2878 | 0 | g_bookmark_file_set_visited_date_time (bookmark, uri, visited_dt); |
2879 | 0 | g_date_time_unref (visited_dt); |
2880 | 0 | } |
2881 | | |
2882 | | /** |
2883 | | * g_bookmark_file_set_visited_date_time: |
2884 | | * @bookmark: a #GBookmarkFile |
2885 | | * @uri: a valid URI |
2886 | | * @visited: a #GDateTime |
2887 | | * |
2888 | | * Sets the time the bookmark for @uri was last visited. |
2889 | | * |
2890 | | * If no bookmark for @uri is found then it is created. |
2891 | | * |
2892 | | * The "visited" time should only be set if the bookmark was launched, |
2893 | | * either using the command line retrieved by g_bookmark_file_get_application_info() |
2894 | | * or by the default application for the bookmark's MIME type, retrieved |
2895 | | * using g_bookmark_file_get_mime_type(). Changing the "visited" time |
2896 | | * does not affect the "modified" time. |
2897 | | * |
2898 | | * Since: 2.66 |
2899 | | */ |
2900 | | void |
2901 | | g_bookmark_file_set_visited_date_time (GBookmarkFile *bookmark, |
2902 | | const char *uri, |
2903 | | GDateTime *visited) |
2904 | 0 | { |
2905 | 0 | BookmarkItem *item; |
2906 | |
|
2907 | 0 | g_return_if_fail (bookmark != NULL); |
2908 | 0 | g_return_if_fail (uri != NULL); |
2909 | 0 | g_return_if_fail (visited != NULL); |
2910 | | |
2911 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2912 | 0 | if (!item) |
2913 | 0 | { |
2914 | 0 | item = bookmark_item_new (uri); |
2915 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
2916 | 0 | } |
2917 | |
|
2918 | 0 | g_clear_pointer (&item->visited, g_date_time_unref); |
2919 | 0 | item->visited = g_date_time_ref (visited); |
2920 | 0 | } |
2921 | | |
2922 | | /** |
2923 | | * g_bookmark_file_get_visited: |
2924 | | * @bookmark: a #GBookmarkFile |
2925 | | * @uri: a valid URI |
2926 | | * @error: return location for a #GError, or %NULL |
2927 | | * |
2928 | | * Gets the time the bookmark for @uri was last visited. |
2929 | | * |
2930 | | * In the event the URI cannot be found, -1 is returned and |
2931 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2932 | | * |
2933 | | * Returns: a timestamp. |
2934 | | * |
2935 | | * Since: 2.12 |
2936 | | * Deprecated: 2.66: Use g_bookmark_file_get_visited_date_time() instead, as |
2937 | | * `time_t` is deprecated due to the year 2038 problem. |
2938 | | */ |
2939 | | time_t |
2940 | | g_bookmark_file_get_visited (GBookmarkFile *bookmark, |
2941 | | const gchar *uri, |
2942 | | GError **error) |
2943 | 0 | { |
2944 | 0 | GDateTime *visited = g_bookmark_file_get_visited_date_time (bookmark, uri, error); |
2945 | 0 | return (visited != NULL) ? g_date_time_to_unix (visited) : (time_t) -1; |
2946 | 0 | } |
2947 | | |
2948 | | /** |
2949 | | * g_bookmark_file_get_visited_date_time: |
2950 | | * @bookmark: a #GBookmarkFile |
2951 | | * @uri: a valid URI |
2952 | | * @error: return location for a #GError, or %NULL |
2953 | | * |
2954 | | * Gets the time the bookmark for @uri was last visited. |
2955 | | * |
2956 | | * In the event the URI cannot be found, %NULL is returned and |
2957 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2958 | | * |
2959 | | * Returns: (transfer none): a #GDateTime |
2960 | | * |
2961 | | * Since: 2.66 |
2962 | | */ |
2963 | | GDateTime * |
2964 | | g_bookmark_file_get_visited_date_time (GBookmarkFile *bookmark, |
2965 | | const char *uri, |
2966 | | GError **error) |
2967 | 0 | { |
2968 | 0 | BookmarkItem *item; |
2969 | |
|
2970 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
2971 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
2972 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2973 | | |
2974 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
2975 | 0 | if (!item) |
2976 | 0 | { |
2977 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
2978 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
2979 | 0 | _("No bookmark found for URI “%s”"), |
2980 | 0 | uri); |
2981 | 0 | return NULL; |
2982 | 0 | } |
2983 | | |
2984 | 0 | return item->visited; |
2985 | 0 | } |
2986 | | |
2987 | | /** |
2988 | | * g_bookmark_file_has_group: |
2989 | | * @bookmark: a #GBookmarkFile |
2990 | | * @uri: a valid URI |
2991 | | * @group: the group name to be searched |
2992 | | * @error: return location for a #GError, or %NULL |
2993 | | * |
2994 | | * Checks whether @group appears in the list of groups to which |
2995 | | * the bookmark for @uri belongs to. |
2996 | | * |
2997 | | * In the event the URI cannot be found, %FALSE is returned and |
2998 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
2999 | | * |
3000 | | * Returns: %TRUE if @group was found. |
3001 | | * |
3002 | | * Since: 2.12 |
3003 | | */ |
3004 | | gboolean |
3005 | | g_bookmark_file_has_group (GBookmarkFile *bookmark, |
3006 | | const gchar *uri, |
3007 | | const gchar *group, |
3008 | | GError **error) |
3009 | 0 | { |
3010 | 0 | BookmarkItem *item; |
3011 | 0 | GList *l; |
3012 | |
|
3013 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3014 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
3015 | | |
3016 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3017 | 0 | if (!item) |
3018 | 0 | { |
3019 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3020 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3021 | 0 | _("No bookmark found for URI “%s”"), |
3022 | 0 | uri); |
3023 | 0 | return FALSE; |
3024 | 0 | } |
3025 | | |
3026 | 0 | if (!item->metadata) |
3027 | 0 | return FALSE; |
3028 | | |
3029 | 0 | for (l = item->metadata->groups; l != NULL; l = l->next) |
3030 | 0 | { |
3031 | 0 | if (strcmp (l->data, group) == 0) |
3032 | 0 | return TRUE; |
3033 | 0 | } |
3034 | | |
3035 | 0 | return FALSE; |
3036 | |
|
3037 | 0 | } |
3038 | | |
3039 | | /** |
3040 | | * g_bookmark_file_add_group: |
3041 | | * @bookmark: a #GBookmarkFile |
3042 | | * @uri: a valid URI |
3043 | | * @group: the group name to be added |
3044 | | * |
3045 | | * Adds @group to the list of groups to which the bookmark for @uri |
3046 | | * belongs to. |
3047 | | * |
3048 | | * If no bookmark for @uri is found then it is created. |
3049 | | * |
3050 | | * Since: 2.12 |
3051 | | */ |
3052 | | void |
3053 | | g_bookmark_file_add_group (GBookmarkFile *bookmark, |
3054 | | const gchar *uri, |
3055 | | const gchar *group) |
3056 | 0 | { |
3057 | 0 | BookmarkItem *item; |
3058 | |
|
3059 | 0 | g_return_if_fail (bookmark != NULL); |
3060 | 0 | g_return_if_fail (uri != NULL); |
3061 | 0 | g_return_if_fail (group != NULL && group[0] != '\0'); |
3062 | | |
3063 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3064 | 0 | if (!item) |
3065 | 0 | { |
3066 | 0 | item = bookmark_item_new (uri); |
3067 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
3068 | 0 | } |
3069 | |
|
3070 | 0 | if (!item->metadata) |
3071 | 0 | item->metadata = bookmark_metadata_new (); |
3072 | |
|
3073 | 0 | if (!g_bookmark_file_has_group (bookmark, uri, group, NULL)) |
3074 | 0 | { |
3075 | 0 | item->metadata->groups = g_list_prepend (item->metadata->groups, |
3076 | 0 | g_strdup (group)); |
3077 | |
|
3078 | 0 | bookmark_item_touch_modified (item); |
3079 | 0 | } |
3080 | 0 | } |
3081 | | |
3082 | | /** |
3083 | | * g_bookmark_file_remove_group: |
3084 | | * @bookmark: a #GBookmarkFile |
3085 | | * @uri: a valid URI |
3086 | | * @group: the group name to be removed |
3087 | | * @error: return location for a #GError, or %NULL |
3088 | | * |
3089 | | * Removes @group from the list of groups to which the bookmark |
3090 | | * for @uri belongs to. |
3091 | | * |
3092 | | * In the event the URI cannot be found, %FALSE is returned and |
3093 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
3094 | | * In the event no group was defined, %FALSE is returned and |
3095 | | * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE. |
3096 | | * |
3097 | | * Returns: %TRUE if @group was successfully removed. |
3098 | | * |
3099 | | * Since: 2.12 |
3100 | | */ |
3101 | | gboolean |
3102 | | g_bookmark_file_remove_group (GBookmarkFile *bookmark, |
3103 | | const gchar *uri, |
3104 | | const gchar *group, |
3105 | | GError **error) |
3106 | 0 | { |
3107 | 0 | BookmarkItem *item; |
3108 | 0 | GList *l; |
3109 | |
|
3110 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3111 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
3112 | | |
3113 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3114 | 0 | if (!item) |
3115 | 0 | { |
3116 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3117 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3118 | 0 | _("No bookmark found for URI “%s”"), |
3119 | 0 | uri); |
3120 | 0 | return FALSE; |
3121 | 0 | } |
3122 | | |
3123 | 0 | if (!item->metadata) |
3124 | 0 | { |
3125 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3126 | 0 | G_BOOKMARK_FILE_ERROR_INVALID_VALUE, |
3127 | 0 | _("No groups set in bookmark for URI “%s”"), |
3128 | 0 | uri); |
3129 | 0 | return FALSE; |
3130 | 0 | } |
3131 | | |
3132 | 0 | for (l = item->metadata->groups; l != NULL; l = l->next) |
3133 | 0 | { |
3134 | 0 | if (strcmp (l->data, group) == 0) |
3135 | 0 | { |
3136 | 0 | item->metadata->groups = g_list_remove_link (item->metadata->groups, l); |
3137 | 0 | g_free (l->data); |
3138 | 0 | g_list_free_1 (l); |
3139 | |
|
3140 | 0 | bookmark_item_touch_modified (item); |
3141 | |
|
3142 | 0 | return TRUE; |
3143 | 0 | } |
3144 | 0 | } |
3145 | | |
3146 | 0 | return FALSE; |
3147 | 0 | } |
3148 | | |
3149 | | /** |
3150 | | * g_bookmark_file_set_groups: |
3151 | | * @bookmark: a #GBookmarkFile |
3152 | | * @uri: an item's URI |
3153 | | * @groups: (nullable) (array length=length) (element-type utf8): an array of |
3154 | | * group names, or %NULL to remove all groups |
3155 | | * @length: number of group name values in @groups |
3156 | | * |
3157 | | * Sets a list of group names for the item with URI @uri. Each previously |
3158 | | * set group name list is removed. |
3159 | | * |
3160 | | * If @uri cannot be found then an item for it is created. |
3161 | | * |
3162 | | * Since: 2.12 |
3163 | | */ |
3164 | | void |
3165 | | g_bookmark_file_set_groups (GBookmarkFile *bookmark, |
3166 | | const gchar *uri, |
3167 | | const gchar **groups, |
3168 | | gsize length) |
3169 | 0 | { |
3170 | 0 | BookmarkItem *item; |
3171 | 0 | gsize i; |
3172 | |
|
3173 | 0 | g_return_if_fail (bookmark != NULL); |
3174 | 0 | g_return_if_fail (uri != NULL); |
3175 | 0 | g_return_if_fail (groups != NULL); |
3176 | | |
3177 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3178 | 0 | if (!item) |
3179 | 0 | { |
3180 | 0 | item = bookmark_item_new (uri); |
3181 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
3182 | 0 | } |
3183 | |
|
3184 | 0 | if (!item->metadata) |
3185 | 0 | item->metadata = bookmark_metadata_new (); |
3186 | |
|
3187 | 0 | g_list_free_full (item->metadata->groups, g_free); |
3188 | 0 | item->metadata->groups = NULL; |
3189 | |
|
3190 | 0 | if (groups) |
3191 | 0 | { |
3192 | 0 | for (i = 0; i < length && groups[i] != NULL; i++) |
3193 | 0 | item->metadata->groups = g_list_append (item->metadata->groups, |
3194 | 0 | g_strdup (groups[i])); |
3195 | 0 | } |
3196 | |
|
3197 | 0 | bookmark_item_touch_modified (item); |
3198 | 0 | } |
3199 | | |
3200 | | /** |
3201 | | * g_bookmark_file_get_groups: |
3202 | | * @bookmark: a #GBookmarkFile |
3203 | | * @uri: a valid URI |
3204 | | * @length: (out) (optional): return location for the length of the returned string, or %NULL |
3205 | | * @error: return location for a #GError, or %NULL |
3206 | | * |
3207 | | * Retrieves the list of group names of the bookmark for @uri. |
3208 | | * |
3209 | | * In the event the URI cannot be found, %NULL is returned and |
3210 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
3211 | | * |
3212 | | * The returned array is %NULL terminated, so @length may optionally |
3213 | | * be %NULL. |
3214 | | * |
3215 | | * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names. |
3216 | | * Use g_strfreev() to free it. |
3217 | | * |
3218 | | * Since: 2.12 |
3219 | | */ |
3220 | | gchar ** |
3221 | | g_bookmark_file_get_groups (GBookmarkFile *bookmark, |
3222 | | const gchar *uri, |
3223 | | gsize *length, |
3224 | | GError **error) |
3225 | 0 | { |
3226 | 0 | BookmarkItem *item; |
3227 | 0 | GList *l; |
3228 | 0 | gsize len, i; |
3229 | 0 | gchar **retval; |
3230 | |
|
3231 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
3232 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
3233 | | |
3234 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3235 | 0 | if (!item) |
3236 | 0 | { |
3237 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3238 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3239 | 0 | _("No bookmark found for URI “%s”"), |
3240 | 0 | uri); |
3241 | 0 | return NULL; |
3242 | 0 | } |
3243 | | |
3244 | 0 | if (!item->metadata) |
3245 | 0 | { |
3246 | 0 | if (length) |
3247 | 0 | *length = 0; |
3248 | |
|
3249 | 0 | return NULL; |
3250 | 0 | } |
3251 | | |
3252 | 0 | len = g_list_length (item->metadata->groups); |
3253 | 0 | retval = g_new0 (gchar *, len + 1); |
3254 | 0 | for (l = g_list_last (item->metadata->groups), i = 0; |
3255 | 0 | l != NULL; |
3256 | 0 | l = l->prev) |
3257 | 0 | { |
3258 | 0 | gchar *group_name = (gchar *) l->data; |
3259 | |
|
3260 | 0 | g_warn_if_fail (group_name != NULL); |
3261 | |
|
3262 | 0 | retval[i++] = g_strdup (group_name); |
3263 | 0 | } |
3264 | 0 | retval[i] = NULL; |
3265 | |
|
3266 | 0 | if (length) |
3267 | 0 | *length = len; |
3268 | |
|
3269 | 0 | return retval; |
3270 | 0 | } |
3271 | | |
3272 | | /** |
3273 | | * g_bookmark_file_add_application: |
3274 | | * @bookmark: a #GBookmarkFile |
3275 | | * @uri: a valid URI |
3276 | | * @name: (nullable): the name of the application registering the bookmark |
3277 | | * or %NULL |
3278 | | * @exec: (nullable): command line to be used to launch the bookmark or %NULL |
3279 | | * |
3280 | | * Adds the application with @name and @exec to the list of |
3281 | | * applications that have registered a bookmark for @uri into |
3282 | | * @bookmark. |
3283 | | * |
3284 | | * Every bookmark inside a #GBookmarkFile must have at least an |
3285 | | * application registered. Each application must provide a name, a |
3286 | | * command line useful for launching the bookmark, the number of times |
3287 | | * the bookmark has been registered by the application and the last |
3288 | | * time the application registered this bookmark. |
3289 | | * |
3290 | | * If @name is %NULL, the name of the application will be the |
3291 | | * same returned by g_get_application_name(); if @exec is %NULL, the |
3292 | | * command line will be a composition of the program name as |
3293 | | * returned by g_get_prgname() and the "\%u" modifier, which will be |
3294 | | * expanded to the bookmark's URI. |
3295 | | * |
3296 | | * This function will automatically take care of updating the |
3297 | | * registrations count and timestamping in case an application |
3298 | | * with the same @name had already registered a bookmark for |
3299 | | * @uri inside @bookmark. |
3300 | | * |
3301 | | * If no bookmark for @uri is found, one is created. |
3302 | | * |
3303 | | * Since: 2.12 |
3304 | | */ |
3305 | | void |
3306 | | g_bookmark_file_add_application (GBookmarkFile *bookmark, |
3307 | | const gchar *uri, |
3308 | | const gchar *name, |
3309 | | const gchar *exec) |
3310 | 0 | { |
3311 | 0 | BookmarkItem *item; |
3312 | 0 | gchar *app_name, *app_exec; |
3313 | 0 | GDateTime *stamp; |
3314 | |
|
3315 | 0 | g_return_if_fail (bookmark != NULL); |
3316 | 0 | g_return_if_fail (uri != NULL); |
3317 | | |
3318 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3319 | 0 | if (!item) |
3320 | 0 | { |
3321 | 0 | item = bookmark_item_new (uri); |
3322 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
3323 | 0 | } |
3324 | |
|
3325 | 0 | if (name && name[0] != '\0') |
3326 | 0 | app_name = g_strdup (name); |
3327 | 0 | else |
3328 | 0 | app_name = g_strdup (g_get_application_name ()); |
3329 | |
|
3330 | 0 | if (exec && exec[0] != '\0') |
3331 | 0 | app_exec = g_strdup (exec); |
3332 | 0 | else |
3333 | 0 | app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL); |
3334 | |
|
3335 | 0 | stamp = g_date_time_new_now_utc (); |
3336 | |
|
3337 | 0 | g_bookmark_file_set_application_info (bookmark, uri, |
3338 | 0 | app_name, |
3339 | 0 | app_exec, |
3340 | 0 | -1, |
3341 | 0 | stamp, |
3342 | 0 | NULL); |
3343 | |
|
3344 | 0 | g_date_time_unref (stamp); |
3345 | 0 | g_free (app_exec); |
3346 | 0 | g_free (app_name); |
3347 | 0 | } |
3348 | | |
3349 | | /** |
3350 | | * g_bookmark_file_remove_application: |
3351 | | * @bookmark: a #GBookmarkFile |
3352 | | * @uri: a valid URI |
3353 | | * @name: the name of the application |
3354 | | * @error: return location for a #GError or %NULL |
3355 | | * |
3356 | | * Removes application registered with @name from the list of applications |
3357 | | * that have registered a bookmark for @uri inside @bookmark. |
3358 | | * |
3359 | | * In the event the URI cannot be found, %FALSE is returned and |
3360 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
3361 | | * In the event that no application with name @app_name has registered |
3362 | | * a bookmark for @uri, %FALSE is returned and error is set to |
3363 | | * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. |
3364 | | * |
3365 | | * Returns: %TRUE if the application was successfully removed. |
3366 | | * |
3367 | | * Since: 2.12 |
3368 | | */ |
3369 | | gboolean |
3370 | | g_bookmark_file_remove_application (GBookmarkFile *bookmark, |
3371 | | const gchar *uri, |
3372 | | const gchar *name, |
3373 | | GError **error) |
3374 | 0 | { |
3375 | 0 | GError *set_error; |
3376 | 0 | gboolean retval; |
3377 | |
|
3378 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3379 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
3380 | 0 | g_return_val_if_fail (name != NULL, FALSE); |
3381 | | |
3382 | 0 | set_error = NULL; |
3383 | 0 | retval = g_bookmark_file_set_application_info (bookmark, uri, |
3384 | 0 | name, |
3385 | 0 | "", |
3386 | 0 | 0, |
3387 | 0 | NULL, |
3388 | 0 | &set_error); |
3389 | 0 | if (set_error) |
3390 | 0 | { |
3391 | 0 | g_propagate_error (error, set_error); |
3392 | |
|
3393 | 0 | return FALSE; |
3394 | 0 | } |
3395 | | |
3396 | 0 | return retval; |
3397 | 0 | } |
3398 | | |
3399 | | /** |
3400 | | * g_bookmark_file_has_application: |
3401 | | * @bookmark: a #GBookmarkFile |
3402 | | * @uri: a valid URI |
3403 | | * @name: the name of the application |
3404 | | * @error: return location for a #GError or %NULL |
3405 | | * |
3406 | | * Checks whether the bookmark for @uri inside @bookmark has been |
3407 | | * registered by application @name. |
3408 | | * |
3409 | | * In the event the URI cannot be found, %FALSE is returned and |
3410 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
3411 | | * |
3412 | | * Returns: %TRUE if the application @name was found |
3413 | | * |
3414 | | * Since: 2.12 |
3415 | | */ |
3416 | | gboolean |
3417 | | g_bookmark_file_has_application (GBookmarkFile *bookmark, |
3418 | | const gchar *uri, |
3419 | | const gchar *name, |
3420 | | GError **error) |
3421 | 0 | { |
3422 | 0 | BookmarkItem *item; |
3423 | |
|
3424 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3425 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
3426 | 0 | g_return_val_if_fail (name != NULL, FALSE); |
3427 | | |
3428 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3429 | 0 | if (!item) |
3430 | 0 | { |
3431 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3432 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3433 | 0 | _("No bookmark found for URI “%s”"), |
3434 | 0 | uri); |
3435 | 0 | return FALSE; |
3436 | 0 | } |
3437 | | |
3438 | 0 | return (NULL != bookmark_item_lookup_app_info (item, name)); |
3439 | 0 | } |
3440 | | |
3441 | | /** |
3442 | | * g_bookmark_file_set_app_info: |
3443 | | * @bookmark: a #GBookmarkFile |
3444 | | * @uri: a valid URI |
3445 | | * @name: an application's name |
3446 | | * @exec: an application's command line |
3447 | | * @count: the number of registrations done for this application |
3448 | | * @stamp: the time of the last registration for this application |
3449 | | * @error: return location for a #GError or %NULL |
3450 | | * |
3451 | | * Sets the meta-data of application @name inside the list of |
3452 | | * applications that have registered a bookmark for @uri inside |
3453 | | * @bookmark. |
3454 | | * |
3455 | | * You should rarely use this function; use g_bookmark_file_add_application() |
3456 | | * and g_bookmark_file_remove_application() instead. |
3457 | | * |
3458 | | * @name can be any UTF-8 encoded string used to identify an |
3459 | | * application. |
3460 | | * @exec can have one of these two modifiers: "\%f", which will |
3461 | | * be expanded as the local file name retrieved from the bookmark's |
3462 | | * URI; "\%u", which will be expanded as the bookmark's URI. |
3463 | | * The expansion is done automatically when retrieving the stored |
3464 | | * command line using the g_bookmark_file_get_application_info() function. |
3465 | | * @count is the number of times the application has registered the |
3466 | | * bookmark; if is < 0, the current registration count will be increased |
3467 | | * by one, if is 0, the application with @name will be removed from |
3468 | | * the list of registered applications. |
3469 | | * @stamp is the Unix time of the last registration; if it is -1, the |
3470 | | * current time will be used. |
3471 | | * |
3472 | | * If you try to remove an application by setting its registration count to |
3473 | | * zero, and no bookmark for @uri is found, %FALSE is returned and |
3474 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly, |
3475 | | * in the event that no application @name has registered a bookmark |
3476 | | * for @uri, %FALSE is returned and error is set to |
3477 | | * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark |
3478 | | * for @uri is found, one is created. |
3479 | | * |
3480 | | * Returns: %TRUE if the application's meta-data was successfully |
3481 | | * changed. |
3482 | | * |
3483 | | * Since: 2.12 |
3484 | | * Deprecated: 2.66: Use g_bookmark_file_set_application_info() instead, as |
3485 | | * `time_t` is deprecated due to the year 2038 problem. |
3486 | | */ |
3487 | | gboolean |
3488 | | g_bookmark_file_set_app_info (GBookmarkFile *bookmark, |
3489 | | const gchar *uri, |
3490 | | const gchar *name, |
3491 | | const gchar *exec, |
3492 | | gint count, |
3493 | | time_t stamp, |
3494 | | GError **error) |
3495 | 0 | { |
3496 | 0 | GDateTime *stamp_dt = (stamp != (time_t) -1) ? g_date_time_new_from_unix_utc (stamp) : g_date_time_new_now_utc (); |
3497 | 0 | gboolean retval; |
3498 | 0 | retval = g_bookmark_file_set_application_info (bookmark, uri, name, exec, count, |
3499 | 0 | stamp_dt, error); |
3500 | 0 | g_date_time_unref (stamp_dt); |
3501 | 0 | return retval; |
3502 | 0 | } |
3503 | | |
3504 | | /** |
3505 | | * g_bookmark_file_set_application_info: |
3506 | | * @bookmark: a #GBookmarkFile |
3507 | | * @uri: a valid URI |
3508 | | * @name: an application's name |
3509 | | * @exec: an application's command line |
3510 | | * @count: the number of registrations done for this application |
3511 | | * @stamp: (nullable): the time of the last registration for this application, |
3512 | | * which may be %NULL if @count is 0 |
3513 | | * @error: return location for a #GError or %NULL |
3514 | | * |
3515 | | * Sets the meta-data of application @name inside the list of |
3516 | | * applications that have registered a bookmark for @uri inside |
3517 | | * @bookmark. |
3518 | | * |
3519 | | * You should rarely use this function; use g_bookmark_file_add_application() |
3520 | | * and g_bookmark_file_remove_application() instead. |
3521 | | * |
3522 | | * @name can be any UTF-8 encoded string used to identify an |
3523 | | * application. |
3524 | | * @exec can have one of these two modifiers: "\%f", which will |
3525 | | * be expanded as the local file name retrieved from the bookmark's |
3526 | | * URI; "\%u", which will be expanded as the bookmark's URI. |
3527 | | * The expansion is done automatically when retrieving the stored |
3528 | | * command line using the g_bookmark_file_get_application_info() function. |
3529 | | * @count is the number of times the application has registered the |
3530 | | * bookmark; if is < 0, the current registration count will be increased |
3531 | | * by one, if is 0, the application with @name will be removed from |
3532 | | * the list of registered applications. |
3533 | | * @stamp is the Unix time of the last registration. |
3534 | | * |
3535 | | * If you try to remove an application by setting its registration count to |
3536 | | * zero, and no bookmark for @uri is found, %FALSE is returned and |
3537 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly, |
3538 | | * in the event that no application @name has registered a bookmark |
3539 | | * for @uri, %FALSE is returned and error is set to |
3540 | | * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark |
3541 | | * for @uri is found, one is created. |
3542 | | * |
3543 | | * Returns: %TRUE if the application's meta-data was successfully |
3544 | | * changed. |
3545 | | * |
3546 | | * Since: 2.66 |
3547 | | */ |
3548 | | gboolean |
3549 | | g_bookmark_file_set_application_info (GBookmarkFile *bookmark, |
3550 | | const char *uri, |
3551 | | const char *name, |
3552 | | const char *exec, |
3553 | | int count, |
3554 | | GDateTime *stamp, |
3555 | | GError **error) |
3556 | 0 | { |
3557 | 0 | BookmarkItem *item; |
3558 | 0 | BookmarkAppInfo *ai; |
3559 | |
|
3560 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3561 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
3562 | 0 | g_return_val_if_fail (name != NULL, FALSE); |
3563 | 0 | g_return_val_if_fail (exec != NULL, FALSE); |
3564 | 0 | g_return_val_if_fail (count == 0 || stamp != NULL, FALSE); |
3565 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
3566 | | |
3567 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3568 | 0 | if (!item) |
3569 | 0 | { |
3570 | 0 | if (count == 0) |
3571 | 0 | { |
3572 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3573 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3574 | 0 | _("No bookmark found for URI “%s”"), |
3575 | 0 | uri); |
3576 | 0 | return FALSE; |
3577 | 0 | } |
3578 | 0 | else |
3579 | 0 | { |
3580 | 0 | item = bookmark_item_new (uri); |
3581 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
3582 | 0 | } |
3583 | 0 | } |
3584 | | |
3585 | 0 | if (!item->metadata) |
3586 | 0 | item->metadata = bookmark_metadata_new (); |
3587 | |
|
3588 | 0 | ai = bookmark_item_lookup_app_info (item, name); |
3589 | 0 | if (!ai) |
3590 | 0 | { |
3591 | 0 | if (count == 0) |
3592 | 0 | { |
3593 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3594 | 0 | G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED, |
3595 | 0 | _("No application with name “%s” registered a bookmark for “%s”"), |
3596 | 0 | name, |
3597 | 0 | uri); |
3598 | 0 | return FALSE; |
3599 | 0 | } |
3600 | 0 | else |
3601 | 0 | { |
3602 | 0 | ai = bookmark_app_info_new (name); |
3603 | |
|
3604 | 0 | item->metadata->applications = g_list_prepend (item->metadata->applications, ai); |
3605 | 0 | g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai); |
3606 | 0 | } |
3607 | 0 | } |
3608 | | |
3609 | 0 | if (count == 0) |
3610 | 0 | { |
3611 | 0 | item->metadata->applications = g_list_remove (item->metadata->applications, ai); |
3612 | 0 | g_hash_table_remove (item->metadata->apps_by_name, ai->name); |
3613 | 0 | bookmark_app_info_free (ai); |
3614 | |
|
3615 | 0 | bookmark_item_touch_modified (item); |
3616 | |
|
3617 | 0 | return TRUE; |
3618 | 0 | } |
3619 | 0 | else if (count > 0) |
3620 | 0 | ai->count = count; |
3621 | 0 | else |
3622 | 0 | ai->count += 1; |
3623 | | |
3624 | 0 | g_clear_pointer (&ai->stamp, g_date_time_unref); |
3625 | 0 | ai->stamp = g_date_time_ref (stamp); |
3626 | |
|
3627 | 0 | if (exec && exec[0] != '\0') |
3628 | 0 | { |
3629 | 0 | g_free (ai->exec); |
3630 | 0 | ai->exec = g_shell_quote (exec); |
3631 | 0 | } |
3632 | |
|
3633 | 0 | bookmark_item_touch_modified (item); |
3634 | |
|
3635 | 0 | return TRUE; |
3636 | 0 | } |
3637 | | |
3638 | | /* expands the application's command line */ |
3639 | | static gchar * |
3640 | | expand_exec_line (const gchar *exec_fmt, |
3641 | | const gchar *uri) |
3642 | 0 | { |
3643 | 0 | GString *exec; |
3644 | 0 | gchar ch; |
3645 | |
|
3646 | 0 | exec = g_string_sized_new (512); |
3647 | 0 | while ((ch = *exec_fmt++) != '\0') |
3648 | 0 | { |
3649 | 0 | if (ch != '%') |
3650 | 0 | { |
3651 | 0 | exec = g_string_append_c (exec, ch); |
3652 | 0 | continue; |
3653 | 0 | } |
3654 | | |
3655 | 0 | ch = *exec_fmt++; |
3656 | 0 | switch (ch) |
3657 | 0 | { |
3658 | 0 | case '\0': |
3659 | 0 | goto out; |
3660 | 0 | case 'U': |
3661 | 0 | case 'u': |
3662 | 0 | g_string_append (exec, uri); |
3663 | 0 | break; |
3664 | 0 | case 'F': |
3665 | 0 | case 'f': |
3666 | 0 | { |
3667 | 0 | gchar *file = g_filename_from_uri (uri, NULL, NULL); |
3668 | 0 | if (file) |
3669 | 0 | { |
3670 | 0 | g_string_append (exec, file); |
3671 | 0 | g_free (file); |
3672 | 0 | } |
3673 | 0 | else |
3674 | 0 | { |
3675 | 0 | g_string_free (exec, TRUE); |
3676 | 0 | return NULL; |
3677 | 0 | } |
3678 | 0 | } |
3679 | 0 | break; |
3680 | 0 | case '%': |
3681 | 0 | default: |
3682 | 0 | exec = g_string_append_c (exec, ch); |
3683 | 0 | break; |
3684 | 0 | } |
3685 | 0 | } |
3686 | | |
3687 | 0 | out: |
3688 | 0 | return g_string_free (exec, FALSE); |
3689 | 0 | } |
3690 | | |
3691 | | /** |
3692 | | * g_bookmark_file_get_app_info: |
3693 | | * @bookmark: a #GBookmarkFile |
3694 | | * @uri: a valid URI |
3695 | | * @name: an application's name |
3696 | | * @exec: (out) (optional): return location for the command line of the application, or %NULL |
3697 | | * @count: (out) (optional): return location for the registration count, or %NULL |
3698 | | * @stamp: (out) (optional): return location for the last registration time, or %NULL |
3699 | | * @error: return location for a #GError, or %NULL |
3700 | | * |
3701 | | * Gets the registration information of @app_name for the bookmark for |
3702 | | * @uri. See g_bookmark_file_set_application_info() for more information about |
3703 | | * the returned data. |
3704 | | * |
3705 | | * The string returned in @app_exec must be freed. |
3706 | | * |
3707 | | * In the event the URI cannot be found, %FALSE is returned and |
3708 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the |
3709 | | * event that no application with name @app_name has registered a bookmark |
3710 | | * for @uri, %FALSE is returned and error is set to |
3711 | | * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting |
3712 | | * the command line fails, an error of the %G_SHELL_ERROR domain is |
3713 | | * set and %FALSE is returned. |
3714 | | * |
3715 | | * Returns: %TRUE on success. |
3716 | | * |
3717 | | * Since: 2.12 |
3718 | | * Deprecated: 2.66: Use g_bookmark_file_get_application_info() instead, as |
3719 | | * `time_t` is deprecated due to the year 2038 problem. |
3720 | | */ |
3721 | | gboolean |
3722 | | g_bookmark_file_get_app_info (GBookmarkFile *bookmark, |
3723 | | const gchar *uri, |
3724 | | const gchar *name, |
3725 | | gchar **exec, |
3726 | | guint *count, |
3727 | | time_t *stamp, |
3728 | | GError **error) |
3729 | 0 | { |
3730 | 0 | GDateTime *stamp_dt = NULL; |
3731 | 0 | gboolean retval; |
3732 | |
|
3733 | 0 | retval = g_bookmark_file_get_application_info (bookmark, uri, name, exec, count, &stamp_dt, error); |
3734 | 0 | if (!retval) |
3735 | 0 | return FALSE; |
3736 | | |
3737 | 0 | if (stamp != NULL) |
3738 | 0 | *stamp = g_date_time_to_unix (stamp_dt); |
3739 | |
|
3740 | 0 | return TRUE; |
3741 | 0 | } |
3742 | | |
3743 | | /** |
3744 | | * g_bookmark_file_get_application_info: |
3745 | | * @bookmark: a #GBookmarkFile |
3746 | | * @uri: a valid URI |
3747 | | * @name: an application's name |
3748 | | * @exec: (out) (optional): return location for the command line of the application, or %NULL |
3749 | | * @count: (out) (optional): return location for the registration count, or %NULL |
3750 | | * @stamp: (out) (optional) (transfer none): return location for the last registration time, or %NULL |
3751 | | * @error: return location for a #GError, or %NULL |
3752 | | * |
3753 | | * Gets the registration information of @app_name for the bookmark for |
3754 | | * @uri. See g_bookmark_file_set_application_info() for more information about |
3755 | | * the returned data. |
3756 | | * |
3757 | | * The string returned in @app_exec must be freed. |
3758 | | * |
3759 | | * In the event the URI cannot be found, %FALSE is returned and |
3760 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the |
3761 | | * event that no application with name @app_name has registered a bookmark |
3762 | | * for @uri, %FALSE is returned and error is set to |
3763 | | * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting |
3764 | | * the command line fails, an error of the %G_SHELL_ERROR domain is |
3765 | | * set and %FALSE is returned. |
3766 | | * |
3767 | | * Returns: %TRUE on success. |
3768 | | * |
3769 | | * Since: 2.66 |
3770 | | */ |
3771 | | gboolean |
3772 | | g_bookmark_file_get_application_info (GBookmarkFile *bookmark, |
3773 | | const char *uri, |
3774 | | const char *name, |
3775 | | char **exec, |
3776 | | unsigned int *count, |
3777 | | GDateTime **stamp, |
3778 | | GError **error) |
3779 | 0 | { |
3780 | 0 | BookmarkItem *item; |
3781 | 0 | BookmarkAppInfo *ai; |
3782 | |
|
3783 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3784 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
3785 | 0 | g_return_val_if_fail (name != NULL, FALSE); |
3786 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
3787 | | |
3788 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3789 | 0 | if (!item) |
3790 | 0 | { |
3791 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3792 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3793 | 0 | _("No bookmark found for URI “%s”"), |
3794 | 0 | uri); |
3795 | 0 | return FALSE; |
3796 | 0 | } |
3797 | | |
3798 | 0 | ai = bookmark_item_lookup_app_info (item, name); |
3799 | 0 | if (!ai) |
3800 | 0 | { |
3801 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3802 | 0 | G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED, |
3803 | 0 | _("No application with name “%s” registered a bookmark for “%s”"), |
3804 | 0 | name, |
3805 | 0 | uri); |
3806 | 0 | return FALSE; |
3807 | 0 | } |
3808 | | |
3809 | 0 | if (exec) |
3810 | 0 | { |
3811 | 0 | GError *unquote_error = NULL; |
3812 | 0 | gchar *command_line; |
3813 | |
|
3814 | 0 | command_line = g_shell_unquote (ai->exec, &unquote_error); |
3815 | 0 | if (unquote_error) |
3816 | 0 | { |
3817 | 0 | g_propagate_error (error, unquote_error); |
3818 | 0 | return FALSE; |
3819 | 0 | } |
3820 | | |
3821 | 0 | *exec = expand_exec_line (command_line, uri); |
3822 | 0 | if (!*exec) |
3823 | 0 | { |
3824 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3825 | 0 | G_BOOKMARK_FILE_ERROR_INVALID_URI, |
3826 | 0 | _("Failed to expand exec line “%s” with URI “%s”"), |
3827 | 0 | ai->exec, uri); |
3828 | 0 | g_free (command_line); |
3829 | |
|
3830 | 0 | return FALSE; |
3831 | 0 | } |
3832 | 0 | else |
3833 | 0 | g_free (command_line); |
3834 | 0 | } |
3835 | | |
3836 | 0 | if (count) |
3837 | 0 | *count = ai->count; |
3838 | |
|
3839 | 0 | if (stamp) |
3840 | 0 | *stamp = ai->stamp; |
3841 | |
|
3842 | 0 | return TRUE; |
3843 | 0 | } |
3844 | | |
3845 | | /** |
3846 | | * g_bookmark_file_get_applications: |
3847 | | * @bookmark: a #GBookmarkFile |
3848 | | * @uri: a valid URI |
3849 | | * @length: (out) (optional): return location of the length of the returned list, or %NULL |
3850 | | * @error: return location for a #GError, or %NULL |
3851 | | * |
3852 | | * Retrieves the names of the applications that have registered the |
3853 | | * bookmark for @uri. |
3854 | | * |
3855 | | * In the event the URI cannot be found, %NULL is returned and |
3856 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
3857 | | * |
3858 | | * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings. |
3859 | | * Use g_strfreev() to free it. |
3860 | | * |
3861 | | * Since: 2.12 |
3862 | | */ |
3863 | | gchar ** |
3864 | | g_bookmark_file_get_applications (GBookmarkFile *bookmark, |
3865 | | const gchar *uri, |
3866 | | gsize *length, |
3867 | | GError **error) |
3868 | 0 | { |
3869 | 0 | BookmarkItem *item; |
3870 | 0 | GList *l; |
3871 | 0 | gchar **apps; |
3872 | 0 | gsize i, n_apps; |
3873 | |
|
3874 | 0 | g_return_val_if_fail (bookmark != NULL, NULL); |
3875 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
3876 | | |
3877 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
3878 | 0 | if (!item) |
3879 | 0 | { |
3880 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3881 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3882 | 0 | _("No bookmark found for URI “%s”"), |
3883 | 0 | uri); |
3884 | 0 | return NULL; |
3885 | 0 | } |
3886 | | |
3887 | 0 | if (!item->metadata) |
3888 | 0 | { |
3889 | 0 | if (length) |
3890 | 0 | *length = 0; |
3891 | |
|
3892 | 0 | return NULL; |
3893 | 0 | } |
3894 | | |
3895 | 0 | n_apps = g_list_length (item->metadata->applications); |
3896 | 0 | apps = g_new0 (gchar *, n_apps + 1); |
3897 | |
|
3898 | 0 | for (l = g_list_last (item->metadata->applications), i = 0; |
3899 | 0 | l != NULL; |
3900 | 0 | l = l->prev) |
3901 | 0 | { |
3902 | 0 | BookmarkAppInfo *ai; |
3903 | |
|
3904 | 0 | ai = (BookmarkAppInfo *) l->data; |
3905 | |
|
3906 | 0 | g_warn_if_fail (ai != NULL); |
3907 | 0 | g_warn_if_fail (ai->name != NULL); |
3908 | |
|
3909 | 0 | apps[i++] = g_strdup (ai->name); |
3910 | 0 | } |
3911 | 0 | apps[i] = NULL; |
3912 | |
|
3913 | 0 | if (length) |
3914 | 0 | *length = i; |
3915 | |
|
3916 | 0 | return apps; |
3917 | 0 | } |
3918 | | |
3919 | | /** |
3920 | | * g_bookmark_file_get_size: |
3921 | | * @bookmark: a #GBookmarkFile |
3922 | | * |
3923 | | * Gets the number of bookmarks inside @bookmark. |
3924 | | * |
3925 | | * Returns: the number of bookmarks |
3926 | | * |
3927 | | * Since: 2.12 |
3928 | | */ |
3929 | | gint |
3930 | | g_bookmark_file_get_size (GBookmarkFile *bookmark) |
3931 | 0 | { |
3932 | 0 | g_return_val_if_fail (bookmark != NULL, 0); |
3933 | | |
3934 | 0 | return g_list_length (bookmark->items); |
3935 | 0 | } |
3936 | | |
3937 | | /** |
3938 | | * g_bookmark_file_move_item: |
3939 | | * @bookmark: a #GBookmarkFile |
3940 | | * @old_uri: a valid URI |
3941 | | * @new_uri: (nullable): a valid URI, or %NULL |
3942 | | * @error: return location for a #GError or %NULL |
3943 | | * |
3944 | | * Changes the URI of a bookmark item from @old_uri to @new_uri. Any |
3945 | | * existing bookmark for @new_uri will be overwritten. If @new_uri is |
3946 | | * %NULL, then the bookmark is removed. |
3947 | | * |
3948 | | * In the event the URI cannot be found, %FALSE is returned and |
3949 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
3950 | | * |
3951 | | * Returns: %TRUE if the URI was successfully changed |
3952 | | * |
3953 | | * Since: 2.12 |
3954 | | */ |
3955 | | gboolean |
3956 | | g_bookmark_file_move_item (GBookmarkFile *bookmark, |
3957 | | const gchar *old_uri, |
3958 | | const gchar *new_uri, |
3959 | | GError **error) |
3960 | 0 | { |
3961 | 0 | BookmarkItem *item; |
3962 | |
|
3963 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
3964 | 0 | g_return_val_if_fail (old_uri != NULL, FALSE); |
3965 | | |
3966 | 0 | item = g_bookmark_file_lookup_item (bookmark, old_uri); |
3967 | 0 | if (!item) |
3968 | 0 | { |
3969 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
3970 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
3971 | 0 | _("No bookmark found for URI “%s”"), |
3972 | 0 | old_uri); |
3973 | 0 | return FALSE; |
3974 | 0 | } |
3975 | | |
3976 | 0 | if (new_uri && new_uri[0] != '\0') |
3977 | 0 | { |
3978 | 0 | if (g_strcmp0 (old_uri, new_uri) == 0) |
3979 | 0 | return TRUE; |
3980 | | |
3981 | 0 | if (g_bookmark_file_has_item (bookmark, new_uri)) |
3982 | 0 | { |
3983 | 0 | if (!g_bookmark_file_remove_item (bookmark, new_uri, error)) |
3984 | 0 | return FALSE; |
3985 | 0 | } |
3986 | | |
3987 | 0 | g_hash_table_steal (bookmark->items_by_uri, item->uri); |
3988 | |
|
3989 | 0 | g_free (item->uri); |
3990 | 0 | item->uri = g_strdup (new_uri); |
3991 | 0 | bookmark_item_touch_modified (item); |
3992 | |
|
3993 | 0 | g_hash_table_replace (bookmark->items_by_uri, item->uri, item); |
3994 | |
|
3995 | 0 | return TRUE; |
3996 | 0 | } |
3997 | 0 | else |
3998 | 0 | { |
3999 | 0 | if (!g_bookmark_file_remove_item (bookmark, old_uri, error)) |
4000 | 0 | return FALSE; |
4001 | | |
4002 | 0 | return TRUE; |
4003 | 0 | } |
4004 | 0 | } |
4005 | | |
4006 | | /** |
4007 | | * g_bookmark_file_set_icon: |
4008 | | * @bookmark: a #GBookmarkFile |
4009 | | * @uri: a valid URI |
4010 | | * @href: (nullable): the URI of the icon for the bookmark, or %NULL |
4011 | | * @mime_type: the MIME type of the icon for the bookmark |
4012 | | * |
4013 | | * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets |
4014 | | * the currently set icon. @href can either be a full URL for the icon |
4015 | | * file or the icon name following the Icon Naming specification. |
4016 | | * |
4017 | | * If no bookmark for @uri is found one is created. |
4018 | | * |
4019 | | * Since: 2.12 |
4020 | | */ |
4021 | | void |
4022 | | g_bookmark_file_set_icon (GBookmarkFile *bookmark, |
4023 | | const gchar *uri, |
4024 | | const gchar *href, |
4025 | | const gchar *mime_type) |
4026 | 0 | { |
4027 | 0 | BookmarkItem *item; |
4028 | |
|
4029 | 0 | g_return_if_fail (bookmark != NULL); |
4030 | 0 | g_return_if_fail (uri != NULL); |
4031 | | |
4032 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
4033 | 0 | if (!item) |
4034 | 0 | { |
4035 | 0 | item = bookmark_item_new (uri); |
4036 | 0 | g_bookmark_file_add_item (bookmark, item, NULL); |
4037 | 0 | } |
4038 | |
|
4039 | 0 | if (!item->metadata) |
4040 | 0 | item->metadata = bookmark_metadata_new (); |
4041 | |
|
4042 | 0 | g_free (item->metadata->icon_href); |
4043 | 0 | g_free (item->metadata->icon_mime); |
4044 | |
|
4045 | 0 | item->metadata->icon_href = g_strdup (href); |
4046 | |
|
4047 | 0 | if (mime_type && mime_type[0] != '\0') |
4048 | 0 | item->metadata->icon_mime = g_strdup (mime_type); |
4049 | 0 | else |
4050 | 0 | item->metadata->icon_mime = g_strdup ("application/octet-stream"); |
4051 | |
|
4052 | 0 | bookmark_item_touch_modified (item); |
4053 | 0 | } |
4054 | | |
4055 | | /** |
4056 | | * g_bookmark_file_get_icon: |
4057 | | * @bookmark: a #GBookmarkFile |
4058 | | * @uri: a valid URI |
4059 | | * @href: (out) (optional): return location for the icon's location or %NULL |
4060 | | * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL |
4061 | | * @error: return location for a #GError or %NULL |
4062 | | * |
4063 | | * Gets the icon of the bookmark for @uri. |
4064 | | * |
4065 | | * In the event the URI cannot be found, %FALSE is returned and |
4066 | | * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. |
4067 | | * |
4068 | | * Returns: %TRUE if the icon for the bookmark for the URI was found. |
4069 | | * You should free the returned strings. |
4070 | | * |
4071 | | * Since: 2.12 |
4072 | | */ |
4073 | | gboolean |
4074 | | g_bookmark_file_get_icon (GBookmarkFile *bookmark, |
4075 | | const gchar *uri, |
4076 | | gchar **href, |
4077 | | gchar **mime_type, |
4078 | | GError **error) |
4079 | 0 | { |
4080 | 0 | BookmarkItem *item; |
4081 | |
|
4082 | 0 | g_return_val_if_fail (bookmark != NULL, FALSE); |
4083 | 0 | g_return_val_if_fail (uri != NULL, FALSE); |
4084 | | |
4085 | 0 | item = g_bookmark_file_lookup_item (bookmark, uri); |
4086 | 0 | if (!item) |
4087 | 0 | { |
4088 | 0 | g_set_error (error, G_BOOKMARK_FILE_ERROR, |
4089 | 0 | G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND, |
4090 | 0 | _("No bookmark found for URI “%s”"), |
4091 | 0 | uri); |
4092 | 0 | return FALSE; |
4093 | 0 | } |
4094 | | |
4095 | 0 | if ((!item->metadata) || (!item->metadata->icon_href)) |
4096 | 0 | return FALSE; |
4097 | | |
4098 | 0 | if (href) |
4099 | 0 | *href = g_strdup (item->metadata->icon_href); |
4100 | |
|
4101 | 0 | if (mime_type) |
4102 | 0 | *mime_type = g_strdup (item->metadata->icon_mime); |
4103 | |
|
4104 | 0 | return TRUE; |
4105 | 0 | } |