Branch data Line data Source code
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 : : #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 : : #define XBEL_ROOT_ELEMENT "xbel"
64 : : #define XBEL_FOLDER_ELEMENT "folder" /* unused */
65 : : #define XBEL_BOOKMARK_ELEMENT "bookmark"
66 : : #define XBEL_ALIAS_ELEMENT "alias" /* unused */
67 : : #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
68 : : #define XBEL_TITLE_ELEMENT "title"
69 : : #define XBEL_DESC_ELEMENT "desc"
70 : : #define XBEL_INFO_ELEMENT "info"
71 : : #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 : : #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 : : #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 : : #define BOOKMARK_GROUPS_ELEMENT "groups"
93 : : #define BOOKMARK_GROUP_ELEMENT "group"
94 : : #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
95 : : #define BOOKMARK_APPLICATION_ELEMENT "application"
96 : : #define BOOKMARK_ICON_ELEMENT "icon"
97 : : #define BOOKMARK_PRIVATE_ELEMENT "private"
98 : :
99 : : #define BOOKMARK_NAME_ATTRIBUTE "name"
100 : : #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 : : #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 : : #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 : 66 : bookmark_app_info_new (const gchar *name)
217 : : {
218 : : BookmarkAppInfo *retval;
219 : :
220 : 66 : g_warn_if_fail (name != NULL);
221 : :
222 : 66 : retval = g_slice_new (BookmarkAppInfo);
223 : :
224 : 66 : retval->name = g_strdup (name);
225 : 66 : retval->exec = NULL;
226 : 66 : retval->count = 0;
227 : 66 : retval->stamp = NULL;
228 : :
229 : 66 : return retval;
230 : : }
231 : :
232 : : static void
233 : 66 : bookmark_app_info_free (BookmarkAppInfo *app_info)
234 : : {
235 : 66 : if (!app_info)
236 : 0 : return;
237 : :
238 : 66 : g_free (app_info->name);
239 : 66 : g_free (app_info->exec);
240 : 66 : g_clear_pointer (&app_info->stamp, g_date_time_unref);
241 : :
242 : 66 : g_slice_free (BookmarkAppInfo, app_info);
243 : : }
244 : :
245 : : static BookmarkAppInfo *
246 : 17 : bookmark_app_info_copy (BookmarkAppInfo *app_info)
247 : : {
248 : : BookmarkAppInfo *copy;
249 : :
250 : 17 : if (!app_info)
251 : 0 : return NULL;
252 : :
253 : 17 : copy = bookmark_app_info_new (app_info->name);
254 : 17 : copy->count = app_info->count;
255 : 17 : copy->exec = g_strdup (app_info->exec);
256 : :
257 : 17 : if (app_info->stamp)
258 : 16 : copy->stamp = g_date_time_ref (app_info->stamp);
259 : :
260 : 17 : return copy;
261 : : }
262 : :
263 : : static gchar *
264 : 48 : bookmark_app_info_dump (BookmarkAppInfo *app_info)
265 : : {
266 : : gchar *retval;
267 : : gchar *name, *exec, *modified, *count;
268 : :
269 : 48 : g_warn_if_fail (app_info != NULL);
270 : :
271 : 48 : if (app_info->count == 0)
272 : 0 : return NULL;
273 : :
274 : 48 : name = g_markup_escape_text (app_info->name, -1);
275 : 48 : exec = g_markup_escape_text (app_info->exec, -1);
276 : 48 : count = g_strdup_printf ("%u", app_info->count);
277 : :
278 : 48 : if (app_info->stamp)
279 : : {
280 : : char *tmp;
281 : :
282 : 46 : tmp = g_date_time_format_iso8601 (app_info->stamp);
283 : 46 : modified = g_strconcat (" " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", tmp, "\"",
284 : : NULL);
285 : 46 : g_free (tmp);
286 : : }
287 : : else
288 : : {
289 : 2 : modified = g_strdup ("");
290 : : }
291 : :
292 : 48 : retval = g_strconcat (" "
293 : : "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
294 : : " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
295 : : " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\"",
296 : : modified,
297 : : " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
298 : : NULL);
299 : :
300 : 48 : g_free (name);
301 : 48 : g_free (exec);
302 : 48 : g_free (modified);
303 : 48 : g_free (count);
304 : :
305 : 48 : return retval;
306 : : }
307 : :
308 : :
309 : : /***********************
310 : : * BookmarkMetadata *
311 : : * *
312 : : * Metadata storage *
313 : : ***********************/
314 : : static BookmarkMetadata *
315 : 80 : bookmark_metadata_new (void)
316 : : {
317 : : BookmarkMetadata *retval;
318 : :
319 : 80 : retval = g_slice_new (BookmarkMetadata);
320 : :
321 : 80 : retval->mime_type = NULL;
322 : :
323 : 80 : retval->groups = NULL;
324 : :
325 : 80 : retval->applications = NULL;
326 : 80 : retval->apps_by_name = g_hash_table_new_full (g_str_hash,
327 : : g_str_equal,
328 : : NULL,
329 : : NULL);
330 : :
331 : 80 : retval->is_private = FALSE;
332 : :
333 : 80 : retval->icon_href = NULL;
334 : 80 : retval->icon_mime = NULL;
335 : :
336 : 80 : return retval;
337 : : }
338 : :
339 : : static void
340 : 80 : bookmark_metadata_free (BookmarkMetadata *metadata)
341 : : {
342 : 80 : if (!metadata)
343 : 0 : return;
344 : :
345 : 80 : g_free (metadata->mime_type);
346 : :
347 : 80 : g_list_free_full (metadata->groups, g_free);
348 : 80 : g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
349 : :
350 : 80 : g_hash_table_destroy (metadata->apps_by_name);
351 : :
352 : 80 : g_free (metadata->icon_href);
353 : 80 : g_free (metadata->icon_mime);
354 : :
355 : 80 : g_slice_free (BookmarkMetadata, metadata);
356 : : }
357 : :
358 : : static BookmarkMetadata *
359 : 24 : bookmark_metadata_copy (BookmarkMetadata *metadata)
360 : : {
361 : : BookmarkMetadata *copy;
362 : : GList *l;
363 : :
364 : 24 : if (!metadata)
365 : 3 : return NULL;
366 : :
367 : 21 : copy = bookmark_metadata_new ();
368 : 21 : copy->is_private = metadata->is_private;
369 : 21 : copy->mime_type = g_strdup (metadata->mime_type);
370 : 21 : copy->icon_href = g_strdup (metadata->icon_href);
371 : 21 : copy->icon_mime = g_strdup (metadata->icon_mime);
372 : :
373 : 21 : copy->groups = g_list_copy_deep (metadata->groups, (GCopyFunc) g_strdup, NULL);
374 : 21 : copy->applications =
375 : 21 : g_list_copy_deep (metadata->applications, (GCopyFunc) bookmark_app_info_copy, NULL);
376 : :
377 : 38 : for (l = copy->applications; l; l = l->next)
378 : : {
379 : 17 : BookmarkAppInfo *app_info = l->data;
380 : 17 : g_hash_table_insert (copy->apps_by_name, app_info->name, app_info);
381 : : }
382 : :
383 : 21 : g_assert (g_hash_table_size (copy->apps_by_name) ==
384 : : g_hash_table_size (metadata->apps_by_name));
385 : :
386 : 21 : return copy;
387 : : }
388 : :
389 : : static gchar *
390 : 48 : bookmark_metadata_dump (BookmarkMetadata *metadata)
391 : : {
392 : : GString *retval;
393 : : gchar *buffer;
394 : :
395 : 48 : if (!metadata->applications)
396 : 0 : return NULL;
397 : :
398 : 48 : retval = g_string_sized_new (1024);
399 : :
400 : : /* metadata container */
401 : 48 : g_string_append (retval,
402 : : " "
403 : : "<" XBEL_METADATA_ELEMENT
404 : : " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
405 : : "\">\n");
406 : :
407 : : /* mime type */
408 : 48 : if (metadata->mime_type) {
409 : 44 : buffer = g_strconcat (" "
410 : : "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
411 : : MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
412 : : NULL);
413 : : g_string_append (retval, buffer);
414 : 44 : g_free (buffer);
415 : : }
416 : :
417 : 48 : if (metadata->groups)
418 : : {
419 : : GList *l;
420 : :
421 : : /* open groups container */
422 : 11 : g_string_append (retval,
423 : : " "
424 : : "<" BOOKMARK_NAMESPACE_NAME
425 : : ":" BOOKMARK_GROUPS_ELEMENT ">\n");
426 : :
427 : 29 : for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
428 : : {
429 : : gchar *group_name;
430 : :
431 : 18 : group_name = g_markup_escape_text ((gchar *) l->data, -1);
432 : 18 : buffer = g_strconcat (" "
433 : : "<" BOOKMARK_NAMESPACE_NAME
434 : : ":" BOOKMARK_GROUP_ELEMENT ">",
435 : : group_name,
436 : : "</" BOOKMARK_NAMESPACE_NAME
437 : : ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
438 : : g_string_append (retval, buffer);
439 : :
440 : 18 : g_free (buffer);
441 : 18 : g_free (group_name);
442 : : }
443 : :
444 : : /* close groups container */
445 : 22 : g_string_append (retval,
446 : : " "
447 : : "</" BOOKMARK_NAMESPACE_NAME
448 : : ":" BOOKMARK_GROUPS_ELEMENT ">\n");
449 : : }
450 : :
451 : 48 : if (metadata->applications)
452 : : {
453 : : GList *l;
454 : :
455 : : /* open applications container */
456 : 48 : g_string_append (retval,
457 : : " "
458 : : "<" BOOKMARK_NAMESPACE_NAME
459 : : ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
460 : :
461 : 96 : for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
462 : : {
463 : 48 : BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
464 : : gchar *app_data;
465 : :
466 : 48 : g_warn_if_fail (app_info != NULL);
467 : :
468 : 48 : app_data = bookmark_app_info_dump (app_info);
469 : :
470 : 48 : if (app_data)
471 : : {
472 : 48 : retval = g_string_append (retval, app_data);
473 : :
474 : 48 : g_free (app_data);
475 : : }
476 : : }
477 : :
478 : : /* close applications container */
479 : 96 : g_string_append (retval,
480 : : " "
481 : : "</" BOOKMARK_NAMESPACE_NAME
482 : : ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
483 : : }
484 : :
485 : : /* icon */
486 : 48 : if (metadata->icon_href)
487 : : {
488 : : gchar *href, *mime;
489 : :
490 : 19 : if (!metadata->icon_mime)
491 : 0 : metadata->icon_mime = g_strdup ("application/octet-stream");
492 : :
493 : 19 : href = g_markup_escape_text (metadata->icon_href, -1);
494 : 19 : mime = g_markup_escape_text (metadata->icon_mime, -1);
495 : :
496 : 19 : buffer = g_strconcat (" "
497 : : "<" BOOKMARK_NAMESPACE_NAME
498 : : ":" BOOKMARK_ICON_ELEMENT
499 : : " " BOOKMARK_HREF_ATTRIBUTE "=\"",
500 : : href,
501 : : "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", mime, "\"/>\n", NULL);
502 : : g_string_append (retval, buffer);
503 : :
504 : 19 : g_free (buffer);
505 : 19 : g_free (mime);
506 : 19 : g_free (href);
507 : : }
508 : :
509 : : /* private hint */
510 : 48 : if (metadata->is_private)
511 : 24 : g_string_append (retval,
512 : : " "
513 : : "<" BOOKMARK_NAMESPACE_NAME
514 : : ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
515 : :
516 : : /* close metadata container */
517 : 48 : g_string_append (retval,
518 : : " "
519 : : "</" XBEL_METADATA_ELEMENT ">\n");
520 : :
521 : 48 : return g_string_free (retval, FALSE);
522 : : }
523 : :
524 : : /******************************************************
525 : : * BookmarkItem *
526 : : * *
527 : : * Storage for a single bookmark item inside the list *
528 : : ******************************************************/
529 : : static BookmarkItem *
530 : 95 : bookmark_item_new (const gchar *uri)
531 : : {
532 : : BookmarkItem *item;
533 : :
534 : 95 : g_warn_if_fail (uri != NULL);
535 : :
536 : 95 : item = g_slice_new (BookmarkItem);
537 : 95 : item->uri = g_strdup (uri);
538 : :
539 : 95 : item->title = NULL;
540 : 95 : item->description = NULL;
541 : :
542 : 95 : item->added = NULL;
543 : 95 : item->modified = NULL;
544 : 95 : item->visited = NULL;
545 : :
546 : 95 : item->metadata = NULL;
547 : :
548 : 95 : return item;
549 : : }
550 : :
551 : : static void
552 : 95 : bookmark_item_free (BookmarkItem *item)
553 : : {
554 : 95 : if (!item)
555 : 0 : return;
556 : :
557 : 95 : g_free (item->uri);
558 : 95 : g_free (item->title);
559 : 95 : g_free (item->description);
560 : :
561 : 95 : if (item->metadata)
562 : 80 : bookmark_metadata_free (item->metadata);
563 : :
564 : 95 : g_clear_pointer (&item->added, g_date_time_unref);
565 : 95 : g_clear_pointer (&item->modified, g_date_time_unref);
566 : 95 : g_clear_pointer (&item->visited, g_date_time_unref);
567 : :
568 : 95 : g_slice_free (BookmarkItem, item);
569 : : }
570 : :
571 : : static BookmarkItem *
572 : 24 : bookmark_item_copy (BookmarkItem *item)
573 : : {
574 : : BookmarkItem* copy;
575 : :
576 : 24 : if (!item)
577 : 0 : return NULL;
578 : :
579 : 24 : copy = bookmark_item_new (item->uri);
580 : :
581 : 24 : copy->title = g_strdup (item->title);
582 : 24 : copy->description = g_strdup (item->description);
583 : :
584 : 24 : copy->metadata = bookmark_metadata_copy (item->metadata);
585 : :
586 : 24 : if (item->added)
587 : 24 : copy->added = g_date_time_ref (item->added);
588 : 24 : if (item->modified)
589 : 24 : copy->modified = g_date_time_ref (item->modified);
590 : 24 : if (item->visited)
591 : 24 : copy->visited = g_date_time_ref (item->visited);
592 : :
593 : 24 : return copy;
594 : : }
595 : :
596 : : static void
597 : 116 : bookmark_item_touch_modified (BookmarkItem *item)
598 : : {
599 : 116 : g_clear_pointer (&item->modified, g_date_time_unref);
600 : 116 : item->modified = g_date_time_new_now_utc ();
601 : 116 : }
602 : :
603 : : static gchar *
604 : 62 : bookmark_item_dump (BookmarkItem *item)
605 : : {
606 : : GString *retval;
607 : : 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 : 62 : if (!item->metadata || !item->metadata->applications)
613 : : {
614 : 14 : g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
615 : 14 : return NULL;
616 : : }
617 : :
618 : 48 : retval = g_string_sized_new (4096);
619 : :
620 : 48 : g_string_append (retval, " <" XBEL_BOOKMARK_ELEMENT " ");
621 : :
622 : 48 : escaped_uri = g_markup_escape_text (item->uri, -1);
623 : :
624 : 96 : g_string_append (retval, XBEL_HREF_ATTRIBUTE "=\"");
625 : : g_string_append (retval, escaped_uri);
626 : 48 : g_string_append (retval , "\" ");
627 : :
628 : 48 : g_free (escaped_uri);
629 : :
630 : 48 : if (item->added)
631 : : {
632 : : char *added;
633 : :
634 : 48 : added = g_date_time_format_iso8601 (item->added);
635 : 96 : g_string_append (retval, XBEL_ADDED_ATTRIBUTE "=\"");
636 : : g_string_append (retval, added);
637 : 48 : g_string_append (retval, "\" ");
638 : 48 : g_free (added);
639 : : }
640 : :
641 : 48 : if (item->modified)
642 : : {
643 : : char *modified;
644 : :
645 : 48 : modified = g_date_time_format_iso8601 (item->modified);
646 : 96 : g_string_append (retval, XBEL_MODIFIED_ATTRIBUTE "=\"");
647 : : g_string_append (retval, modified);
648 : 48 : g_string_append (retval, "\" ");
649 : 48 : g_free (modified);
650 : : }
651 : :
652 : 48 : if (item->visited)
653 : : {
654 : : char *visited;
655 : :
656 : 48 : visited = g_date_time_format_iso8601 (item->visited);
657 : 96 : g_string_append (retval, XBEL_VISITED_ATTRIBUTE "=\"");
658 : : g_string_append (retval, visited);
659 : 48 : g_string_append (retval, "\" ");
660 : 48 : g_free (visited);
661 : : }
662 : :
663 : 48 : if (retval->str[retval->len - 1] == ' ')
664 : 48 : g_string_truncate (retval, retval->len - 1);
665 : 48 : g_string_append (retval, ">\n");
666 : :
667 : 48 : if (item->title)
668 : : {
669 : : gchar *escaped_title;
670 : :
671 : 19 : escaped_title = g_markup_escape_text (item->title, -1);
672 : 38 : g_string_append (retval, " " "<" XBEL_TITLE_ELEMENT ">");
673 : : g_string_append (retval, escaped_title);
674 : 19 : g_string_append (retval, "</" XBEL_TITLE_ELEMENT ">\n");
675 : :
676 : 19 : g_free (escaped_title);
677 : : }
678 : :
679 : 48 : if (item->description)
680 : : {
681 : : gchar *escaped_desc;
682 : :
683 : 18 : escaped_desc = g_markup_escape_text (item->description, -1);
684 : 36 : g_string_append (retval, " " "<" XBEL_DESC_ELEMENT ">");
685 : : g_string_append (retval, escaped_desc);
686 : 18 : g_string_append (retval, "</" XBEL_DESC_ELEMENT ">\n");
687 : :
688 : 18 : g_free (escaped_desc);
689 : : }
690 : :
691 : 48 : if (item->metadata)
692 : : {
693 : : gchar *metadata;
694 : :
695 : 48 : metadata = bookmark_metadata_dump (item->metadata);
696 : 48 : if (metadata)
697 : : {
698 : 96 : g_string_append (retval, " " "<" XBEL_INFO_ELEMENT ">\n");
699 : : g_string_append (retval, metadata);
700 : 48 : g_string_append (retval, " " "</" XBEL_INFO_ELEMENT ">\n");
701 : :
702 : 48 : g_free (metadata);
703 : : }
704 : : }
705 : :
706 : 48 : g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
707 : :
708 : 48 : return g_string_free (retval, FALSE);
709 : : }
710 : :
711 : : static BookmarkAppInfo *
712 : 106 : bookmark_item_lookup_app_info (BookmarkItem *item,
713 : : const gchar *app_name)
714 : : {
715 : 106 : g_warn_if_fail (item != NULL && app_name != NULL);
716 : :
717 : 106 : if (!item->metadata)
718 : 0 : return NULL;
719 : :
720 : 106 : return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
721 : : }
722 : :
723 : : /*************************
724 : : * GBookmarkFile *
725 : : *************************/
726 : :
727 : : static void
728 : 148 : g_bookmark_file_init (GBookmarkFile *bookmark)
729 : : {
730 : 148 : bookmark->title = NULL;
731 : 148 : bookmark->description = NULL;
732 : :
733 : 148 : bookmark->items = NULL;
734 : 148 : bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
735 : : g_str_equal,
736 : : NULL,
737 : : NULL);
738 : 148 : }
739 : :
740 : : static void
741 : 148 : g_bookmark_file_clear (GBookmarkFile *bookmark)
742 : : {
743 : 148 : g_free (bookmark->title);
744 : 148 : g_free (bookmark->description);
745 : :
746 : 148 : g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
747 : 148 : bookmark->items = NULL;
748 : :
749 : 148 : g_clear_pointer (&bookmark->items_by_uri, g_hash_table_unref);
750 : 148 : }
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 : 96 : parse_data_new (void)
764 : : {
765 : : ParseData *retval;
766 : :
767 : 96 : retval = g_new (ParseData, 1);
768 : :
769 : 96 : retval->state = STATE_STARTED;
770 : 96 : retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
771 : : (GDestroyNotify) g_free,
772 : : (GDestroyNotify) g_free);
773 : 96 : retval->bookmark_file = NULL;
774 : 96 : retval->current_item = NULL;
775 : :
776 : 96 : return retval;
777 : : }
778 : :
779 : : static void
780 : 96 : parse_data_free (ParseData *parse_data)
781 : : {
782 : 96 : g_hash_table_destroy (parse_data->namespaces);
783 : :
784 : 96 : g_free (parse_data);
785 : 96 : }
786 : :
787 : : #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
788 : :
789 : : static void
790 : 46 : parse_bookmark_element (GMarkupParseContext *context,
791 : : ParseData *parse_data,
792 : : const gchar **attribute_names,
793 : : const gchar **attribute_values,
794 : : GError **error)
795 : : {
796 : : const gchar *uri, *added, *modified, *visited;
797 : : const gchar *attr;
798 : : gint i;
799 : : BookmarkItem *item;
800 : : GError *add_error;
801 : :
802 : 46 : g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
803 : :
804 : 46 : i = 0;
805 : 46 : uri = added = modified = visited = NULL;
806 : 194 : for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
807 : : {
808 : 150 : if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
809 : 44 : uri = attribute_values[i];
810 : 106 : else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
811 : 36 : added = attribute_values[i];
812 : 70 : else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
813 : 34 : modified = attribute_values[i];
814 : 36 : else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
815 : 34 : visited = attribute_values[i];
816 : : else
817 : : {
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 : 2 : g_set_error (error, G_MARKUP_ERROR,
823 : : G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
824 : : _("Unexpected attribute “%s” for element “%s”"),
825 : : attr,
826 : : XBEL_BOOKMARK_ELEMENT);
827 : 6 : return;
828 : : }
829 : : }
830 : :
831 : 44 : if (!uri)
832 : : {
833 : 2 : g_set_error (error, G_MARKUP_ERROR,
834 : : G_MARKUP_ERROR_INVALID_CONTENT,
835 : : _("Attribute “%s” of element “%s” not found"),
836 : : XBEL_HREF_ATTRIBUTE,
837 : : XBEL_BOOKMARK_ELEMENT);
838 : 2 : return;
839 : : }
840 : :
841 : 42 : g_warn_if_fail (parse_data->current_item == NULL);
842 : :
843 : 42 : item = bookmark_item_new (uri);
844 : :
845 : 42 : if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error))
846 : : {
847 : 2 : bookmark_item_free (item);
848 : 2 : return;
849 : : }
850 : :
851 : 40 : if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error))
852 : : {
853 : 0 : bookmark_item_free (item);
854 : 0 : return;
855 : : }
856 : :
857 : 40 : if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error))
858 : : {
859 : 0 : bookmark_item_free (item);
860 : 0 : return;
861 : : }
862 : :
863 : 40 : add_error = NULL;
864 : 40 : g_bookmark_file_add_item (parse_data->bookmark_file,
865 : : item,
866 : : &add_error);
867 : 40 : if (add_error)
868 : : {
869 : 0 : bookmark_item_free (item);
870 : :
871 : 0 : g_propagate_error (error, add_error);
872 : :
873 : 0 : return;
874 : : }
875 : :
876 : 40 : parse_data->current_item = item;
877 : : }
878 : :
879 : : static void
880 : 32 : parse_application_element (GMarkupParseContext *context,
881 : : ParseData *parse_data,
882 : : const gchar **attribute_names,
883 : : const gchar **attribute_values,
884 : : GError **error)
885 : : {
886 : : const gchar *name, *exec, *count, *stamp, *modified;
887 : : const gchar *attr;
888 : : gint i;
889 : : BookmarkItem *item;
890 : : BookmarkAppInfo *ai;
891 : :
892 : 32 : g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
893 : :
894 : 32 : i = 0;
895 : 32 : name = exec = count = stamp = modified = NULL;
896 : 140 : for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
897 : : {
898 : 108 : if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
899 : 28 : name = attribute_values[i];
900 : 80 : else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
901 : 30 : exec = attribute_values[i];
902 : 50 : else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
903 : 24 : count = attribute_values[i];
904 : 26 : else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
905 : 18 : stamp = attribute_values[i];
906 : 8 : else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
907 : 8 : modified = attribute_values[i];
908 : : }
909 : :
910 : : /* the "name" and "exec" attributes are mandatory */
911 : 32 : if (!name)
912 : : {
913 : 4 : g_set_error (error, G_MARKUP_ERROR,
914 : : G_MARKUP_ERROR_INVALID_CONTENT,
915 : : _("Attribute “%s” of element “%s” not found"),
916 : : BOOKMARK_NAME_ATTRIBUTE,
917 : : BOOKMARK_APPLICATION_ELEMENT);
918 : 4 : return;
919 : : }
920 : :
921 : 28 : if (!exec)
922 : : {
923 : 0 : g_set_error (error, G_MARKUP_ERROR,
924 : : G_MARKUP_ERROR_INVALID_CONTENT,
925 : : _("Attribute “%s” of element “%s” not found"),
926 : : BOOKMARK_EXEC_ATTRIBUTE,
927 : : BOOKMARK_APPLICATION_ELEMENT);
928 : 0 : return;
929 : : }
930 : :
931 : 28 : g_warn_if_fail (parse_data->current_item != NULL);
932 : 28 : item = parse_data->current_item;
933 : :
934 : 28 : ai = bookmark_item_lookup_app_info (item, name);
935 : 28 : if (!ai)
936 : : {
937 : 26 : ai = bookmark_app_info_new (name);
938 : :
939 : 26 : if (!item->metadata)
940 : 0 : item->metadata = bookmark_metadata_new ();
941 : :
942 : 26 : item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
943 : 26 : g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
944 : : }
945 : :
946 : 28 : g_free (ai->exec);
947 : 28 : ai->exec = g_strdup (exec);
948 : :
949 : 28 : if (count)
950 : 22 : ai->count = atoi (count);
951 : : else
952 : 6 : ai->count = 1;
953 : :
954 : 28 : g_clear_pointer (&ai->stamp, g_date_time_unref);
955 : 28 : if (modified != NULL)
956 : : {
957 : 8 : if (!timestamp_from_iso8601 (modified, &ai->stamp, error))
958 : 0 : return;
959 : : }
960 : : else
961 : : {
962 : : /* the timestamp attribute has been deprecated but we still parse
963 : : * it for backward compatibility
964 : : */
965 : 20 : if (stamp)
966 : 16 : ai->stamp = g_date_time_new_from_unix_utc (atol (stamp));
967 : : else
968 : 4 : ai->stamp = g_date_time_new_now_utc ();
969 : : }
970 : : }
971 : :
972 : : static void
973 : 28 : parse_mime_type_element (GMarkupParseContext *context,
974 : : ParseData *parse_data,
975 : : const gchar **attribute_names,
976 : : const gchar **attribute_values,
977 : : GError **error)
978 : : {
979 : : const gchar *type;
980 : : const gchar *attr;
981 : : gint i;
982 : : BookmarkItem *item;
983 : :
984 : 28 : g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
985 : :
986 : 28 : i = 0;
987 : 28 : type = NULL;
988 : 52 : for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
989 : : {
990 : 24 : if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
991 : 24 : type = attribute_values[i];
992 : : }
993 : :
994 : 28 : if (!type)
995 : 4 : type = "application/octet-stream";
996 : :
997 : 28 : g_warn_if_fail (parse_data->current_item != NULL);
998 : 28 : item = parse_data->current_item;
999 : :
1000 : 28 : if (!item->metadata)
1001 : 0 : item->metadata = bookmark_metadata_new ();
1002 : :
1003 : 28 : g_free (item->metadata->mime_type);
1004 : 28 : item->metadata->mime_type = g_strdup (type);
1005 : 28 : }
1006 : :
1007 : : static void
1008 : 5 : parse_icon_element (GMarkupParseContext *context,
1009 : : ParseData *parse_data,
1010 : : const gchar **attribute_names,
1011 : : const gchar **attribute_values,
1012 : : GError **error)
1013 : : {
1014 : : const gchar *href;
1015 : : const gchar *type;
1016 : : const gchar *attr;
1017 : : gint i;
1018 : : BookmarkItem *item;
1019 : :
1020 : 5 : g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
1021 : :
1022 : 5 : i = 0;
1023 : 5 : href = NULL;
1024 : 5 : type = NULL;
1025 : 13 : for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
1026 : : {
1027 : 8 : if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
1028 : 3 : href = attribute_values[i];
1029 : 5 : else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
1030 : 5 : type = attribute_values[i];
1031 : : }
1032 : :
1033 : : /* the "href" attribute is mandatory */
1034 : 5 : if (!href)
1035 : : {
1036 : 2 : g_set_error (error, G_MARKUP_ERROR,
1037 : : G_MARKUP_ERROR_INVALID_CONTENT,
1038 : : _("Attribute “%s” of element “%s” not found"),
1039 : : BOOKMARK_HREF_ATTRIBUTE,
1040 : : BOOKMARK_ICON_ELEMENT);
1041 : 2 : return;
1042 : : }
1043 : :
1044 : 3 : if (!type)
1045 : 0 : type = "application/octet-stream";
1046 : :
1047 : 3 : g_warn_if_fail (parse_data->current_item != NULL);
1048 : 3 : item = parse_data->current_item;
1049 : :
1050 : 3 : if (!item->metadata)
1051 : 0 : item->metadata = bookmark_metadata_new ();
1052 : :
1053 : 3 : g_free (item->metadata->icon_href);
1054 : 3 : g_free (item->metadata->icon_mime);
1055 : 3 : item->metadata->icon_href = g_strdup (href);
1056 : 3 : item->metadata->icon_mime = g_strdup (type);
1057 : : }
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 : 449 : map_namespace_to_name (ParseData *parse_data,
1074 : : const gchar **attribute_names,
1075 : : const gchar **attribute_values)
1076 : : {
1077 : : const gchar *attr;
1078 : : gint i;
1079 : :
1080 : 449 : g_warn_if_fail (parse_data != NULL);
1081 : :
1082 : 449 : if (!attribute_names || !attribute_names[0])
1083 : 226 : return;
1084 : :
1085 : 223 : i = 0;
1086 : 739 : for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1087 : : {
1088 : 516 : if (g_str_has_prefix (attr, "xmlns"))
1089 : : {
1090 : : gchar *namespace_name, *namespace_uri;
1091 : : gchar *p;
1092 : :
1093 : 104 : p = g_utf8_strchr (attr, -1, ':');
1094 : 104 : if (p)
1095 : 104 : p = g_utf8_next_char (p);
1096 : : else
1097 : 0 : p = "default";
1098 : :
1099 : 104 : namespace_name = g_strdup (p);
1100 : 104 : namespace_uri = g_strdup (attribute_values[i]);
1101 : :
1102 : 104 : g_hash_table_replace (parse_data->namespaces,
1103 : : namespace_name,
1104 : : namespace_uri);
1105 : : }
1106 : : }
1107 : : }
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 : 2173 : is_element_full (ParseData *parse_data,
1117 : : const gchar *element_full,
1118 : : const gchar *namespace,
1119 : : const gchar *element,
1120 : : const gchar sep)
1121 : : {
1122 : : gchar *ns_uri, *ns_name;
1123 : : const gchar *p, *element_name;
1124 : : gboolean retval;
1125 : :
1126 : 2173 : g_warn_if_fail (parse_data != NULL);
1127 : 2173 : g_warn_if_fail (element_full != NULL);
1128 : :
1129 : 2173 : if (!element)
1130 : 0 : return FALSE;
1131 : :
1132 : : /* no namespace requested: dumb element compare */
1133 : 2173 : if (!namespace)
1134 : 1559 : 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 : 614 : p = g_utf8_strchr (element_full, -1, ':');
1142 : 614 : if (p)
1143 : : {
1144 : 520 : ns_name = g_strndup (element_full, p - element_full);
1145 : 520 : element_name = g_utf8_next_char (p);
1146 : : }
1147 : : else
1148 : : {
1149 : 94 : ns_name = g_strdup ("default");
1150 : 94 : element_name = element_full;
1151 : : }
1152 : :
1153 : 614 : ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1154 : 614 : if (!ns_uri)
1155 : : {
1156 : : /* no default namespace found */
1157 : 94 : g_free (ns_name);
1158 : :
1159 : 94 : return (0 == strcmp (element_full, element));
1160 : : }
1161 : :
1162 : 818 : retval = (0 == strcmp (ns_uri, namespace) &&
1163 : 298 : 0 == strcmp (element_name, element));
1164 : :
1165 : 520 : g_free (ns_name);
1166 : :
1167 : 520 : return retval;
1168 : : }
1169 : :
1170 : : #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1171 : : #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1172 : :
1173 : : static const gchar *
1174 : 22 : parser_state_to_element_name (ParserState state)
1175 : : {
1176 : 22 : switch (state)
1177 : : {
1178 : 2 : case STATE_STARTED:
1179 : : case STATE_FINISHED:
1180 : 2 : 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 : 20 : case STATE_TITLE:
1186 : 20 : 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 : : g_assert_not_reached ();
1207 : : }
1208 : : }
1209 : :
1210 : : static void
1211 : 449 : 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 : : {
1218 : 449 : 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 : 449 : map_namespace_to_name (parse_data, attribute_names, attribute_values);
1227 : :
1228 : 449 : switch (parse_data->state)
1229 : : {
1230 : 72 : case STATE_STARTED:
1231 : 72 : if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1232 : : {
1233 : : const gchar *attr;
1234 : : gint i;
1235 : :
1236 : 70 : i = 0;
1237 : 246 : for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1238 : : {
1239 : 176 : if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1240 : 68 : (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1241 : 68 : parse_data->state = STATE_ROOT;
1242 : : }
1243 : : }
1244 : : else
1245 : 2 : g_set_error (error, G_MARKUP_ERROR,
1246 : : G_MARKUP_ERROR_INVALID_CONTENT,
1247 : : _("Unexpected tag “%s”, tag “%s” expected"),
1248 : : element_name, XBEL_ROOT_ELEMENT);
1249 : 72 : break;
1250 : 144 : case STATE_ROOT:
1251 : 144 : if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1252 : 59 : parse_data->state = STATE_TITLE;
1253 : 85 : else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1254 : 39 : parse_data->state = STATE_DESC;
1255 : 46 : else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1256 : : {
1257 : 46 : GError *inner_error = NULL;
1258 : :
1259 : 46 : parse_data->state = STATE_BOOKMARK;
1260 : :
1261 : 46 : parse_bookmark_element (context,
1262 : : parse_data,
1263 : : attribute_names,
1264 : : attribute_values,
1265 : : &inner_error);
1266 : 46 : if (inner_error)
1267 : 6 : g_propagate_error (error, inner_error);
1268 : : }
1269 : : else
1270 : 0 : g_set_error (error, G_MARKUP_ERROR,
1271 : : G_MARKUP_ERROR_INVALID_CONTENT,
1272 : : _("Unexpected tag “%s” inside “%s”"),
1273 : : element_name,
1274 : : XBEL_ROOT_ELEMENT);
1275 : 144 : break;
1276 : 45 : case STATE_BOOKMARK:
1277 : 45 : if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1278 : 5 : parse_data->state = STATE_TITLE;
1279 : 40 : else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1280 : 2 : parse_data->state = STATE_DESC;
1281 : 38 : else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1282 : 36 : parse_data->state = STATE_INFO;
1283 : : else
1284 : 2 : g_set_error (error, G_MARKUP_ERROR,
1285 : : G_MARKUP_ERROR_INVALID_CONTENT,
1286 : : _("Unexpected tag “%s” inside “%s”"),
1287 : : element_name,
1288 : : XBEL_BOOKMARK_ELEMENT);
1289 : 45 : break;
1290 : 40 : case STATE_INFO:
1291 : 40 : if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1292 : : {
1293 : : const gchar *attr;
1294 : : gint i;
1295 : :
1296 : 36 : i = 0;
1297 : 70 : for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1298 : : {
1299 : 34 : if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1300 : 34 : (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1301 : : {
1302 : 34 : parse_data->state = STATE_METADATA;
1303 : :
1304 : 34 : if (!parse_data->current_item->metadata)
1305 : 34 : parse_data->current_item->metadata = bookmark_metadata_new ();
1306 : : }
1307 : : }
1308 : : }
1309 : : else
1310 : 4 : g_set_error (error, G_MARKUP_ERROR,
1311 : : G_MARKUP_ERROR_INVALID_CONTENT,
1312 : : _("Unexpected tag “%s”, tag “%s” expected"),
1313 : : element_name,
1314 : : XBEL_METADATA_ELEMENT);
1315 : 40 : break;
1316 : 78 : case STATE_METADATA:
1317 : 78 : if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1318 : 30 : parse_data->state = STATE_APPLICATIONS;
1319 : 48 : else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1320 : 11 : parse_data->state = STATE_GROUPS;
1321 : 37 : 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 : 37 : else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1324 : : {
1325 : 5 : GError *inner_error = NULL;
1326 : :
1327 : 5 : parse_data->state = STATE_ICON;
1328 : :
1329 : 5 : parse_icon_element (context,
1330 : : parse_data,
1331 : : attribute_names,
1332 : : attribute_values,
1333 : : &inner_error);
1334 : 5 : if (inner_error)
1335 : 2 : g_propagate_error (error, inner_error);
1336 : : }
1337 : 32 : else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1338 : : {
1339 : 28 : GError *inner_error = NULL;
1340 : :
1341 : 28 : parse_data->state = STATE_MIME;
1342 : :
1343 : 28 : parse_mime_type_element (context,
1344 : : parse_data,
1345 : : attribute_names,
1346 : : attribute_values,
1347 : : &inner_error);
1348 : 28 : if (inner_error)
1349 : 0 : g_propagate_error (error, inner_error);
1350 : : }
1351 : : else
1352 : 4 : g_set_error (error, G_MARKUP_ERROR,
1353 : : G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1354 : : _("Unexpected tag “%s” inside “%s”"),
1355 : : element_name,
1356 : : XBEL_METADATA_ELEMENT);
1357 : 78 : break;
1358 : 32 : case STATE_APPLICATIONS:
1359 : 32 : if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1360 : : {
1361 : 32 : GError *inner_error = NULL;
1362 : :
1363 : 32 : parse_data->state = STATE_APPLICATION;
1364 : :
1365 : 32 : parse_application_element (context,
1366 : : parse_data,
1367 : : attribute_names,
1368 : : attribute_values,
1369 : : &inner_error);
1370 : 32 : if (inner_error)
1371 : 4 : g_propagate_error (error, inner_error);
1372 : : }
1373 : : else
1374 : 0 : g_set_error (error, G_MARKUP_ERROR,
1375 : : G_MARKUP_ERROR_INVALID_CONTENT,
1376 : : _("Unexpected tag “%s”, tag “%s” expected"),
1377 : : element_name,
1378 : : BOOKMARK_APPLICATION_ELEMENT);
1379 : 32 : break;
1380 : 16 : case STATE_GROUPS:
1381 : 16 : if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1382 : 14 : parse_data->state = STATE_GROUP;
1383 : : else
1384 : 2 : g_set_error (error, G_MARKUP_ERROR,
1385 : : G_MARKUP_ERROR_INVALID_CONTENT,
1386 : : _("Unexpected tag “%s”, tag “%s” expected"),
1387 : : element_name,
1388 : : BOOKMARK_GROUP_ELEMENT);
1389 : 16 : break;
1390 : :
1391 : 22 : case STATE_TITLE:
1392 : : case STATE_DESC:
1393 : : case STATE_APPLICATION:
1394 : : case STATE_GROUP:
1395 : : case STATE_MIME:
1396 : : case STATE_ICON:
1397 : : case STATE_FINISHED:
1398 : 22 : g_set_error (error, G_MARKUP_ERROR,
1399 : : G_MARKUP_ERROR_INVALID_CONTENT,
1400 : : _("Unexpected tag “%s” inside “%s”"),
1401 : : element_name,
1402 : : parser_state_to_element_name (parse_data->state));
1403 : 22 : break;
1404 : :
1405 : 0 : default:
1406 : : g_assert_not_reached ();
1407 : : break;
1408 : : }
1409 : 449 : }
1410 : :
1411 : : static void
1412 : 217 : end_element_raw_cb (GMarkupParseContext *context,
1413 : : const gchar *element_name,
1414 : : gpointer user_data,
1415 : : GError **error)
1416 : : {
1417 : 217 : ParseData *parse_data = (ParseData *) user_data;
1418 : :
1419 : 217 : if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1420 : 12 : parse_data->state = STATE_FINISHED;
1421 : 205 : else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1422 : : {
1423 : 10 : parse_data->current_item = NULL;
1424 : :
1425 : 10 : parse_data->state = STATE_ROOT;
1426 : : }
1427 : 380 : else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1428 : 328 : (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1429 : 143 : (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1430 : : {
1431 : 91 : if (parse_data->current_item)
1432 : 15 : parse_data->state = STATE_BOOKMARK;
1433 : : else
1434 : 76 : parse_data->state = STATE_ROOT;
1435 : : }
1436 : 104 : else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1437 : 12 : parse_data->state = STATE_INFO;
1438 : 92 : else if (IS_ELEMENT_NS (parse_data, element_name,
1439 : : BOOKMARK_NAMESPACE_URI,
1440 : : BOOKMARK_APPLICATION_ELEMENT))
1441 : 24 : parse_data->state = STATE_APPLICATIONS;
1442 : 68 : else if (IS_ELEMENT_NS (parse_data, element_name,
1443 : : BOOKMARK_NAMESPACE_URI,
1444 : : BOOKMARK_GROUP_ELEMENT))
1445 : 12 : parse_data->state = STATE_GROUPS;
1446 : 90 : else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1447 : 63 : (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1448 : 58 : (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1449 : 55 : (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1450 : 26 : (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1451 : 56 : parse_data->state = STATE_METADATA;
1452 : 217 : }
1453 : :
1454 : : static void
1455 : 541 : text_raw_cb (GMarkupParseContext *context,
1456 : : const gchar *text,
1457 : : gsize length,
1458 : : gpointer user_data,
1459 : : GError **error)
1460 : : {
1461 : 541 : ParseData *parse_data = (ParseData *) user_data;
1462 : : gchar *payload;
1463 : :
1464 : 541 : payload = g_strndup (text, length);
1465 : :
1466 : 541 : switch (parse_data->state)
1467 : : {
1468 : 62 : case STATE_TITLE:
1469 : 62 : if (parse_data->current_item)
1470 : : {
1471 : 3 : g_free (parse_data->current_item->title);
1472 : 6 : parse_data->current_item->title = g_strdup (payload);
1473 : : }
1474 : : else
1475 : : {
1476 : 59 : g_free (parse_data->bookmark_file->title);
1477 : 118 : parse_data->bookmark_file->title = g_strdup (payload);
1478 : : }
1479 : 62 : break;
1480 : 39 : case STATE_DESC:
1481 : 39 : if (parse_data->current_item)
1482 : : {
1483 : 2 : g_free (parse_data->current_item->description);
1484 : 4 : parse_data->current_item->description = g_strdup (payload);
1485 : : }
1486 : : else
1487 : : {
1488 : 37 : g_free (parse_data->bookmark_file->description);
1489 : 74 : parse_data->bookmark_file->description = g_strdup (payload);
1490 : : }
1491 : 39 : break;
1492 : 14 : case STATE_GROUP:
1493 : : {
1494 : : GList *groups;
1495 : :
1496 : 14 : g_warn_if_fail (parse_data->current_item != NULL);
1497 : :
1498 : 14 : if (!parse_data->current_item->metadata)
1499 : 0 : parse_data->current_item->metadata = bookmark_metadata_new ();
1500 : :
1501 : 14 : groups = parse_data->current_item->metadata->groups;
1502 : 14 : parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1503 : : }
1504 : 14 : break;
1505 : 426 : case STATE_ROOT:
1506 : : case STATE_BOOKMARK:
1507 : : case STATE_INFO:
1508 : : case STATE_METADATA:
1509 : : case STATE_APPLICATIONS:
1510 : : case STATE_APPLICATION:
1511 : : case STATE_GROUPS:
1512 : : case STATE_MIME:
1513 : : case STATE_ICON:
1514 : 426 : break;
1515 : 0 : default:
1516 : 0 : g_warn_if_reached ();
1517 : 0 : break;
1518 : : }
1519 : :
1520 : 541 : g_free (payload);
1521 : 541 : }
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 : 96 : g_bookmark_file_parse (GBookmarkFile *bookmark,
1534 : : const gchar *buffer,
1535 : : gsize length,
1536 : : GError **error)
1537 : : {
1538 : : GMarkupParseContext *context;
1539 : : ParseData *parse_data;
1540 : : GError *parse_error, *end_error;
1541 : : gboolean retval;
1542 : :
1543 : 96 : g_warn_if_fail (bookmark != NULL);
1544 : :
1545 : 96 : if (!buffer)
1546 : 0 : return FALSE;
1547 : :
1548 : 96 : parse_error = NULL;
1549 : 96 : end_error = NULL;
1550 : :
1551 : 96 : if (length == (gsize) -1)
1552 : 0 : length = strlen (buffer);
1553 : :
1554 : 96 : parse_data = parse_data_new ();
1555 : 96 : parse_data->bookmark_file = bookmark;
1556 : :
1557 : 96 : context = g_markup_parse_context_new (&markup_parser,
1558 : : G_MARKUP_DEFAULT_FLAGS,
1559 : : parse_data,
1560 : : (GDestroyNotify) parse_data_free);
1561 : :
1562 : 96 : retval = g_markup_parse_context_parse (context,
1563 : : buffer,
1564 : : length,
1565 : : &parse_error);
1566 : 96 : if (!retval)
1567 : 58 : g_propagate_error (error, parse_error);
1568 : : else
1569 : : {
1570 : 38 : retval = g_markup_parse_context_end_parse (context, &end_error);
1571 : 38 : if (!retval)
1572 : 28 : g_propagate_error (error, end_error);
1573 : : }
1574 : :
1575 : 96 : g_markup_parse_context_free (context);
1576 : :
1577 : 96 : return retval;
1578 : : }
1579 : :
1580 : : static gchar *
1581 : 106 : g_bookmark_file_dump (GBookmarkFile *bookmark,
1582 : : gsize *length,
1583 : : GError **error)
1584 : : {
1585 : : GString *retval;
1586 : : gchar *buffer;
1587 : : GList *l;
1588 : :
1589 : 106 : retval = g_string_sized_new (4096);
1590 : :
1591 : 106 : g_string_append (retval,
1592 : : "<?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 : : "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1600 : : " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1601 : : " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1602 : :
1603 : 106 : if (bookmark->title)
1604 : : {
1605 : : gchar *escaped_title;
1606 : :
1607 : 69 : escaped_title = g_markup_escape_text (bookmark->title, -1);
1608 : :
1609 : 69 : buffer = g_strconcat (" "
1610 : : "<" XBEL_TITLE_ELEMENT ">",
1611 : : escaped_title,
1612 : : "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1613 : :
1614 : : g_string_append (retval, buffer);
1615 : :
1616 : 69 : g_free (buffer);
1617 : 69 : g_free (escaped_title);
1618 : : }
1619 : :
1620 : 106 : if (bookmark->description)
1621 : : {
1622 : : gchar *escaped_desc;
1623 : :
1624 : 47 : escaped_desc = g_markup_escape_text (bookmark->description, -1);
1625 : :
1626 : 47 : buffer = g_strconcat (" "
1627 : : "<" XBEL_DESC_ELEMENT ">",
1628 : : escaped_desc,
1629 : : "</" XBEL_DESC_ELEMENT ">\n", NULL);
1630 : : g_string_append (retval, buffer);
1631 : :
1632 : 47 : g_free (buffer);
1633 : 47 : g_free (escaped_desc);
1634 : : }
1635 : :
1636 : 106 : if (!bookmark->items)
1637 : 56 : goto out;
1638 : : else
1639 : 50 : retval = g_string_append (retval, "\n");
1640 : :
1641 : : /* the items are stored in reverse order */
1642 : 50 : for (l = g_list_last (bookmark->items);
1643 : 112 : l != NULL;
1644 : 62 : l = l->prev)
1645 : : {
1646 : 62 : BookmarkItem *item = (BookmarkItem *) l->data;
1647 : : gchar *item_dump;
1648 : :
1649 : 62 : item_dump = bookmark_item_dump (item);
1650 : 62 : if (!item_dump)
1651 : 14 : continue;
1652 : :
1653 : 48 : retval = g_string_append (retval, item_dump);
1654 : :
1655 : 48 : g_free (item_dump);
1656 : : }
1657 : :
1658 : 50 : out:
1659 : 106 : g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1660 : :
1661 : 106 : if (length)
1662 : 103 : *length = retval->len;
1663 : :
1664 : 106 : return g_string_free (retval, FALSE);
1665 : : }
1666 : :
1667 : : /**************
1668 : : * Misc *
1669 : : **************/
1670 : :
1671 : : static gboolean
1672 : 112 : timestamp_from_iso8601 (const gchar *iso_date,
1673 : : GDateTime **out_date_time,
1674 : : GError **error)
1675 : : {
1676 : 112 : GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL);
1677 : 112 : if (dt == NULL)
1678 : : {
1679 : 2 : g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ,
1680 : : _("Invalid date/time ‘%s’ in bookmark file"), iso_date);
1681 : 2 : return FALSE;
1682 : : }
1683 : :
1684 : 110 : *out_date_time = g_steal_pointer (&dt);
1685 : 110 : return TRUE;
1686 : : }
1687 : :
1688 : 189 : 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 : 146 : g_bookmark_file_new (void)
1709 : : {
1710 : : GBookmarkFile *bookmark;
1711 : :
1712 : 146 : bookmark = g_new (GBookmarkFile, 1);
1713 : :
1714 : 146 : g_bookmark_file_init (bookmark);
1715 : :
1716 : 146 : return bookmark;
1717 : : }
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 : 48 : g_bookmark_file_copy (GBookmarkFile *bookmark)
1732 : : {
1733 : : GBookmarkFile *copy;
1734 : : GList *l;
1735 : :
1736 : 48 : g_return_val_if_fail (bookmark != NULL, NULL);
1737 : :
1738 : 48 : copy = g_bookmark_file_new ();
1739 : 48 : copy->title = g_strdup (bookmark->title);
1740 : 48 : copy->description = g_strdup (bookmark->description);
1741 : 48 : copy->items = g_list_copy_deep (bookmark->items, (GCopyFunc) bookmark_item_copy, NULL);
1742 : :
1743 : 72 : for (l = copy->items; l; l = l->next)
1744 : : {
1745 : 24 : BookmarkItem *item = l->data;
1746 : 24 : g_hash_table_insert (copy->items_by_uri, item->uri, item);
1747 : : }
1748 : :
1749 : 48 : g_assert (g_hash_table_size (copy->items_by_uri) ==
1750 : : g_hash_table_size (bookmark->items_by_uri));
1751 : :
1752 : 48 : return copy;
1753 : : }
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 : 147 : g_bookmark_file_free (GBookmarkFile *bookmark)
1765 : : {
1766 : 147 : if (!bookmark)
1767 : 1 : return;
1768 : :
1769 : 146 : g_bookmark_file_clear (bookmark);
1770 : :
1771 : 146 : g_free (bookmark);
1772 : : }
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 : 97 : g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1792 : : const gchar *data,
1793 : : gsize length,
1794 : : GError **error)
1795 : : {
1796 : : GError *parse_error;
1797 : : gboolean retval;
1798 : :
1799 : 97 : g_return_val_if_fail (bookmark != NULL, FALSE);
1800 : :
1801 : 96 : if (length == (gsize) -1)
1802 : 1 : length = strlen (data);
1803 : :
1804 : 96 : if (bookmark->items)
1805 : : {
1806 : 2 : g_bookmark_file_clear (bookmark);
1807 : 2 : g_bookmark_file_init (bookmark);
1808 : : }
1809 : :
1810 : 96 : parse_error = NULL;
1811 : 96 : retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1812 : :
1813 : 96 : if (!retval)
1814 : 86 : g_propagate_error (error, parse_error);
1815 : :
1816 : 96 : return retval;
1817 : : }
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 : 99 : g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1836 : : const gchar *filename,
1837 : : GError **error)
1838 : : {
1839 : 99 : gboolean ret = FALSE;
1840 : 99 : gchar *buffer = NULL;
1841 : : gsize len;
1842 : :
1843 : 99 : g_return_val_if_fail (bookmark != NULL, FALSE);
1844 : 98 : g_return_val_if_fail (filename != NULL, FALSE);
1845 : :
1846 : 97 : if (!g_file_get_contents (filename, &buffer, &len, error))
1847 : 3 : goto out;
1848 : :
1849 : 94 : if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1850 : 84 : goto out;
1851 : :
1852 : 10 : ret = TRUE;
1853 : 97 : out:
1854 : 97 : g_free (buffer);
1855 : 97 : return ret;
1856 : : }
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 : 5 : find_file_in_data_dirs (const gchar *file,
1868 : : gchar ***dirs,
1869 : : GError **error)
1870 : : {
1871 : : gchar **data_dirs, *data_dir, *path;
1872 : :
1873 : 5 : path = NULL;
1874 : :
1875 : 5 : if (dirs == NULL)
1876 : 0 : return NULL;
1877 : :
1878 : 5 : data_dirs = *dirs;
1879 : 5 : path = NULL;
1880 : 14 : while (data_dirs && (data_dir = *data_dirs) && !path)
1881 : : {
1882 : : gchar *candidate_file, *sub_dir;
1883 : :
1884 : 9 : candidate_file = (gchar *) file;
1885 : 9 : sub_dir = g_strdup ("");
1886 : 10 : while (candidate_file != NULL && !path)
1887 : : {
1888 : : gchar *p;
1889 : :
1890 : 3 : path = g_build_filename (data_dir, sub_dir,
1891 : : candidate_file, NULL);
1892 : :
1893 : 3 : candidate_file = strchr (candidate_file, '-');
1894 : :
1895 : 3 : if (candidate_file == NULL)
1896 : 2 : break;
1897 : :
1898 : 1 : candidate_file++;
1899 : :
1900 : 1 : g_free (sub_dir);
1901 : 1 : sub_dir = g_strndup (file, candidate_file - file - 1);
1902 : :
1903 : 3 : for (p = sub_dir; *p != '\0'; p++)
1904 : : {
1905 : 2 : if (*p == '-')
1906 : 0 : *p = G_DIR_SEPARATOR;
1907 : : }
1908 : : }
1909 : 9 : g_free (sub_dir);
1910 : 9 : data_dirs++;
1911 : : }
1912 : :
1913 : 5 : *dirs = data_dirs;
1914 : :
1915 : 5 : if (!path)
1916 : : {
1917 : 2 : g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1918 : : G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1919 : : _("No valid bookmark file found in data dirs"));
1920 : :
1921 : 2 : return NULL;
1922 : : }
1923 : :
1924 : 3 : return path;
1925 : : }
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 : 7 : g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1948 : : const gchar *file,
1949 : : gchar **full_path,
1950 : : GError **error)
1951 : : {
1952 : 7 : GError *file_error = NULL;
1953 : : gchar **all_data_dirs, **data_dirs;
1954 : : const gchar *user_data_dir;
1955 : : const gchar * const * system_data_dirs;
1956 : : gsize i, j;
1957 : : gchar *output_path;
1958 : : gboolean found_file;
1959 : :
1960 : 7 : g_return_val_if_fail (bookmark != NULL, FALSE);
1961 : 5 : g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1962 : :
1963 : 5 : user_data_dir = g_get_user_data_dir ();
1964 : 5 : system_data_dirs = g_get_system_data_dirs ();
1965 : 5 : all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1966 : :
1967 : 5 : i = 0;
1968 : 5 : all_data_dirs[i++] = g_strdup (user_data_dir);
1969 : :
1970 : 5 : j = 0;
1971 : 15 : while (system_data_dirs[j] != NULL)
1972 : 20 : all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1973 : :
1974 : 5 : found_file = FALSE;
1975 : 5 : data_dirs = all_data_dirs;
1976 : 5 : output_path = NULL;
1977 : 5 : while (*data_dirs != NULL && !found_file)
1978 : : {
1979 : 5 : g_free (output_path);
1980 : :
1981 : 5 : output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1982 : :
1983 : 5 : if (file_error)
1984 : : {
1985 : 2 : g_propagate_error (error, file_error);
1986 : 2 : break;
1987 : : }
1988 : :
1989 : 3 : found_file = g_bookmark_file_load_from_file (bookmark,
1990 : : output_path,
1991 : : &file_error);
1992 : 3 : if (file_error)
1993 : : {
1994 : 3 : g_propagate_error (error, file_error);
1995 : 3 : break;
1996 : : }
1997 : : }
1998 : :
1999 : 5 : if (found_file && full_path)
2000 : 0 : *full_path = output_path;
2001 : : else
2002 : 5 : g_free (output_path);
2003 : :
2004 : 5 : g_strfreev (all_data_dirs);
2005 : :
2006 : 5 : return found_file;
2007 : : }
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 : 107 : g_bookmark_file_to_data (GBookmarkFile *bookmark,
2025 : : gsize *length,
2026 : : GError **error)
2027 : : {
2028 : 107 : GError *write_error = NULL;
2029 : : gchar *retval;
2030 : :
2031 : 107 : g_return_val_if_fail (bookmark != NULL, NULL);
2032 : :
2033 : 106 : retval = g_bookmark_file_dump (bookmark, length, &write_error);
2034 : 106 : if (write_error)
2035 : : {
2036 : 0 : g_propagate_error (error, write_error);
2037 : :
2038 : 0 : return NULL;
2039 : : }
2040 : :
2041 : 106 : return retval;
2042 : : }
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 : 7 : g_bookmark_file_to_file (GBookmarkFile *bookmark,
2059 : : const gchar *filename,
2060 : : GError **error)
2061 : : {
2062 : : gchar *data;
2063 : : GError *data_error, *write_error;
2064 : : gsize len;
2065 : : gboolean retval;
2066 : :
2067 : 7 : g_return_val_if_fail (bookmark != NULL, FALSE);
2068 : 5 : g_return_val_if_fail (filename != NULL, FALSE);
2069 : :
2070 : 3 : data_error = NULL;
2071 : 3 : data = g_bookmark_file_to_data (bookmark, &len, &data_error);
2072 : 3 : if (data_error)
2073 : : {
2074 : 0 : g_propagate_error (error, data_error);
2075 : :
2076 : 0 : return FALSE;
2077 : : }
2078 : :
2079 : 3 : write_error = NULL;
2080 : 3 : g_file_set_contents (filename, data, len, &write_error);
2081 : 3 : if (write_error)
2082 : : {
2083 : 0 : g_propagate_error (error, write_error);
2084 : :
2085 : 0 : retval = FALSE;
2086 : : }
2087 : : else
2088 : 3 : retval = TRUE;
2089 : :
2090 : 3 : g_free (data);
2091 : :
2092 : 3 : return retval;
2093 : : }
2094 : :
2095 : : static BookmarkItem *
2096 : 467 : g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
2097 : : const gchar *uri)
2098 : : {
2099 : 467 : g_warn_if_fail (bookmark != NULL && uri != NULL);
2100 : :
2101 : 467 : return g_hash_table_lookup (bookmark->items_by_uri, uri);
2102 : : }
2103 : :
2104 : : /* this function adds a new item to the list */
2105 : : static void
2106 : 69 : g_bookmark_file_add_item (GBookmarkFile *bookmark,
2107 : : BookmarkItem *item,
2108 : : GError **error)
2109 : : {
2110 : 69 : g_warn_if_fail (bookmark != NULL);
2111 : 69 : 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 : 69 : if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2117 : : {
2118 : 0 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2119 : : G_BOOKMARK_FILE_ERROR_INVALID_URI,
2120 : : _("A bookmark for URI “%s” already exists"),
2121 : : item->uri);
2122 : 0 : return;
2123 : : }
2124 : :
2125 : 69 : bookmark->items = g_list_prepend (bookmark->items, item);
2126 : :
2127 : 69 : g_hash_table_replace (bookmark->items_by_uri,
2128 : 69 : item->uri,
2129 : : item);
2130 : :
2131 : 69 : if (item->added == NULL)
2132 : 35 : item->added = g_date_time_new_now_utc ();
2133 : :
2134 : 69 : if (item->modified == NULL)
2135 : 35 : item->modified = g_date_time_new_now_utc ();
2136 : :
2137 : 69 : if (item->visited == NULL)
2138 : 35 : item->visited = g_date_time_new_now_utc ();
2139 : : }
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 : 22 : g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2155 : : const gchar *uri,
2156 : : GError **error)
2157 : : {
2158 : : BookmarkItem *item;
2159 : :
2160 : 22 : g_return_val_if_fail (bookmark != NULL, FALSE);
2161 : 21 : g_return_val_if_fail (uri != NULL, FALSE);
2162 : :
2163 : 20 : item = g_bookmark_file_lookup_item (bookmark, uri);
2164 : :
2165 : 20 : if (!item)
2166 : : {
2167 : 9 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2168 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2169 : : _("No bookmark found for URI “%s”"),
2170 : : uri);
2171 : 9 : return FALSE;
2172 : : }
2173 : :
2174 : 11 : bookmark->items = g_list_remove (bookmark->items, item);
2175 : 11 : g_hash_table_remove (bookmark->items_by_uri, item->uri);
2176 : :
2177 : 11 : bookmark_item_free (item);
2178 : :
2179 : 11 : return TRUE;
2180 : : }
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 : 78 : g_bookmark_file_has_item (GBookmarkFile *bookmark,
2195 : : const gchar *uri)
2196 : : {
2197 : 78 : g_return_val_if_fail (bookmark != NULL, FALSE);
2198 : 77 : g_return_val_if_fail (uri != NULL, FALSE);
2199 : :
2200 : 76 : return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2201 : : }
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 : 5 : g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2219 : : gsize *length)
2220 : : {
2221 : : GList *l;
2222 : : gchar **uris;
2223 : : gsize i, n_items;
2224 : :
2225 : 5 : g_return_val_if_fail (bookmark != NULL, NULL);
2226 : :
2227 : 4 : n_items = g_list_length (bookmark->items);
2228 : 4 : uris = g_new0 (gchar *, n_items + 1);
2229 : :
2230 : : /* the items are stored in reverse order, so we walk the list backward */
2231 : 7 : for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2232 : : {
2233 : 3 : BookmarkItem *item = (BookmarkItem *) l->data;
2234 : :
2235 : 3 : g_warn_if_fail (item != NULL);
2236 : :
2237 : 6 : uris[i++] = g_strdup (item->uri);
2238 : : }
2239 : 4 : uris[i] = NULL;
2240 : :
2241 : 4 : if (length)
2242 : 4 : *length = i;
2243 : :
2244 : 4 : return uris;
2245 : : }
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 : 20 : g_bookmark_file_set_title (GBookmarkFile *bookmark,
2264 : : const gchar *uri,
2265 : : const gchar *title)
2266 : : {
2267 : 20 : g_return_if_fail (bookmark != NULL);
2268 : :
2269 : 19 : if (!uri)
2270 : : {
2271 : 9 : g_free (bookmark->title);
2272 : 9 : bookmark->title = g_strdup (title);
2273 : : }
2274 : : else
2275 : : {
2276 : : BookmarkItem *item;
2277 : :
2278 : 10 : item = g_bookmark_file_lookup_item (bookmark, uri);
2279 : 10 : if (!item)
2280 : : {
2281 : 10 : item = bookmark_item_new (uri);
2282 : 10 : g_bookmark_file_add_item (bookmark, item, NULL);
2283 : : }
2284 : :
2285 : 10 : g_free (item->title);
2286 : 10 : item->title = g_strdup (title);
2287 : :
2288 : 10 : bookmark_item_touch_modified (item);
2289 : : }
2290 : : }
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 : 21 : g_bookmark_file_get_title (GBookmarkFile *bookmark,
2312 : : const gchar *uri,
2313 : : GError **error)
2314 : : {
2315 : : BookmarkItem *item;
2316 : :
2317 : 21 : g_return_val_if_fail (bookmark != NULL, NULL);
2318 : :
2319 : 20 : if (!uri)
2320 : 18 : return g_strdup (bookmark->title);
2321 : :
2322 : 11 : item = g_bookmark_file_lookup_item (bookmark, uri);
2323 : 11 : if (!item)
2324 : : {
2325 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2326 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2327 : : _("No bookmark found for URI “%s”"),
2328 : : uri);
2329 : 1 : return NULL;
2330 : : }
2331 : :
2332 : 20 : return g_strdup (item->title);
2333 : : }
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 : 20 : g_bookmark_file_set_description (GBookmarkFile *bookmark,
2351 : : const gchar *uri,
2352 : : const gchar *description)
2353 : : {
2354 : 20 : g_return_if_fail (bookmark != NULL);
2355 : :
2356 : 19 : if (!uri)
2357 : : {
2358 : 9 : g_free (bookmark->description);
2359 : 9 : bookmark->description = g_strdup (description);
2360 : : }
2361 : : else
2362 : : {
2363 : : BookmarkItem *item;
2364 : :
2365 : 10 : item = g_bookmark_file_lookup_item (bookmark, uri);
2366 : 10 : if (!item)
2367 : : {
2368 : 1 : item = bookmark_item_new (uri);
2369 : 1 : g_bookmark_file_add_item (bookmark, item, NULL);
2370 : : }
2371 : :
2372 : 10 : g_free (item->description);
2373 : 10 : item->description = g_strdup (description);
2374 : :
2375 : 10 : bookmark_item_touch_modified (item);
2376 : : }
2377 : : }
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 : 29 : g_bookmark_file_get_description (GBookmarkFile *bookmark,
2397 : : const gchar *uri,
2398 : : GError **error)
2399 : : {
2400 : : BookmarkItem *item;
2401 : :
2402 : 29 : g_return_val_if_fail (bookmark != NULL, NULL);
2403 : :
2404 : 28 : if (!uri)
2405 : 18 : return g_strdup (bookmark->description);
2406 : :
2407 : 19 : item = g_bookmark_file_lookup_item (bookmark, uri);
2408 : 19 : if (!item)
2409 : : {
2410 : 9 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2411 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2412 : : _("No bookmark found for URI “%s”"),
2413 : : uri);
2414 : 9 : return NULL;
2415 : : }
2416 : :
2417 : 20 : return g_strdup (item->description);
2418 : : }
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 : 14 : g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2434 : : const gchar *uri,
2435 : : const gchar *mime_type)
2436 : : {
2437 : : BookmarkItem *item;
2438 : :
2439 : 14 : g_return_if_fail (bookmark != NULL);
2440 : 13 : g_return_if_fail (uri != NULL);
2441 : 12 : g_return_if_fail (mime_type != NULL);
2442 : :
2443 : 11 : item = g_bookmark_file_lookup_item (bookmark, uri);
2444 : 11 : if (!item)
2445 : : {
2446 : 1 : item = bookmark_item_new (uri);
2447 : 1 : g_bookmark_file_add_item (bookmark, item, NULL);
2448 : : }
2449 : :
2450 : 11 : if (!item->metadata)
2451 : 2 : item->metadata = bookmark_metadata_new ();
2452 : :
2453 : 11 : g_free (item->metadata->mime_type);
2454 : :
2455 : 11 : item->metadata->mime_type = g_strdup (mime_type);
2456 : 11 : bookmark_item_touch_modified (item);
2457 : : }
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 : 11 : g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2479 : : const gchar *uri,
2480 : : GError **error)
2481 : : {
2482 : : BookmarkItem *item;
2483 : :
2484 : 11 : g_return_val_if_fail (bookmark != NULL, NULL);
2485 : 10 : g_return_val_if_fail (uri != NULL, NULL);
2486 : :
2487 : 9 : item = g_bookmark_file_lookup_item (bookmark, uri);
2488 : 9 : if (!item)
2489 : : {
2490 : 3 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2491 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2492 : : _("No bookmark found for URI “%s”"),
2493 : : uri);
2494 : 3 : return NULL;
2495 : : }
2496 : :
2497 : 6 : if (!item->metadata)
2498 : : {
2499 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2500 : : G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2501 : : _("No MIME type defined in the bookmark for URI “%s”"),
2502 : : uri);
2503 : 1 : return NULL;
2504 : : }
2505 : :
2506 : 10 : return g_strdup (item->metadata->mime_type);
2507 : : }
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 : 12 : g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2523 : : const gchar *uri,
2524 : : gboolean is_private)
2525 : : {
2526 : : BookmarkItem *item;
2527 : :
2528 : 12 : g_return_if_fail (bookmark != NULL);
2529 : 11 : g_return_if_fail (uri != NULL);
2530 : :
2531 : 10 : item = g_bookmark_file_lookup_item (bookmark, uri);
2532 : 10 : if (!item)
2533 : : {
2534 : 1 : item = bookmark_item_new (uri);
2535 : 1 : g_bookmark_file_add_item (bookmark, item, NULL);
2536 : : }
2537 : :
2538 : 10 : if (!item->metadata)
2539 : 10 : item->metadata = bookmark_metadata_new ();
2540 : :
2541 : 10 : item->metadata->is_private = (is_private == TRUE);
2542 : 10 : bookmark_item_touch_modified (item);
2543 : : }
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 : 22 : g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2564 : : const gchar *uri,
2565 : : GError **error)
2566 : : {
2567 : : BookmarkItem *item;
2568 : :
2569 : 22 : g_return_val_if_fail (bookmark != NULL, FALSE);
2570 : 21 : g_return_val_if_fail (uri != NULL, FALSE);
2571 : :
2572 : 20 : item = g_bookmark_file_lookup_item (bookmark, uri);
2573 : 20 : if (!item)
2574 : : {
2575 : 9 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2576 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2577 : : _("No bookmark found for URI “%s”"),
2578 : : uri);
2579 : 9 : return FALSE;
2580 : : }
2581 : :
2582 : 11 : if (!item->metadata)
2583 : : {
2584 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2585 : : G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2586 : : _("No private flag has been defined in bookmark for URI “%s”"),
2587 : : uri);
2588 : 1 : return FALSE;
2589 : : }
2590 : :
2591 : 10 : return item->metadata->is_private;
2592 : : }
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 : 2 : g_bookmark_file_set_added (GBookmarkFile *bookmark,
2610 : : const gchar *uri,
2611 : : time_t added)
2612 : : {
2613 : 2 : GDateTime *added_dt = (added != (time_t) -1) ? g_date_time_new_from_unix_utc (added) : g_date_time_new_now_utc ();
2614 : 2 : g_bookmark_file_set_added_date_time (bookmark, uri, added_dt);
2615 : 2 : g_date_time_unref (added_dt);
2616 : 2 : }
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 : 15 : g_bookmark_file_set_added_date_time (GBookmarkFile *bookmark,
2632 : : const char *uri,
2633 : : GDateTime *added)
2634 : : {
2635 : : BookmarkItem *item;
2636 : :
2637 : 15 : g_return_if_fail (bookmark != NULL);
2638 : 14 : g_return_if_fail (uri != NULL);
2639 : 13 : g_return_if_fail (added != NULL);
2640 : :
2641 : 12 : item = g_bookmark_file_lookup_item (bookmark, uri);
2642 : 12 : if (!item)
2643 : : {
2644 : 2 : item = bookmark_item_new (uri);
2645 : 2 : g_bookmark_file_add_item (bookmark, item, NULL);
2646 : : }
2647 : :
2648 : 12 : g_clear_pointer (&item->added, g_date_time_unref);
2649 : 12 : item->added = g_date_time_ref (added);
2650 : 12 : g_clear_pointer (&item->modified, g_date_time_unref);
2651 : 12 : item->modified = g_date_time_ref (added);
2652 : : }
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 : 3 : g_bookmark_file_get_added (GBookmarkFile *bookmark,
2673 : : const gchar *uri,
2674 : : GError **error)
2675 : : {
2676 : 3 : GDateTime *added = g_bookmark_file_get_added_date_time (bookmark, uri, error);
2677 : 3 : return (added != NULL) ? g_date_time_to_unix (added) : (time_t) -1;
2678 : : }
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 : 26 : g_bookmark_file_get_added_date_time (GBookmarkFile *bookmark,
2697 : : const char *uri,
2698 : : GError **error)
2699 : : {
2700 : : BookmarkItem *item;
2701 : :
2702 : 26 : g_return_val_if_fail (bookmark != NULL, NULL);
2703 : 25 : g_return_val_if_fail (uri != NULL, NULL);
2704 : 24 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2705 : :
2706 : 24 : item = g_bookmark_file_lookup_item (bookmark, uri);
2707 : 24 : if (!item)
2708 : : {
2709 : 12 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2710 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2711 : : _("No bookmark found for URI “%s”"),
2712 : : uri);
2713 : 12 : return NULL;
2714 : : }
2715 : :
2716 : 12 : return item->added;
2717 : : }
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 : 2 : g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2740 : : const gchar *uri,
2741 : : time_t modified)
2742 : : {
2743 : 2 : GDateTime *modified_dt = (modified != (time_t) -1) ? g_date_time_new_from_unix_utc (modified) : g_date_time_new_now_utc ();
2744 : 2 : g_bookmark_file_set_modified_date_time (bookmark, uri, modified_dt);
2745 : 2 : g_date_time_unref (modified_dt);
2746 : 2 : }
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 : 15 : g_bookmark_file_set_modified_date_time (GBookmarkFile *bookmark,
2767 : : const char *uri,
2768 : : GDateTime *modified)
2769 : : {
2770 : : BookmarkItem *item;
2771 : :
2772 : 15 : g_return_if_fail (bookmark != NULL);
2773 : 14 : g_return_if_fail (uri != NULL);
2774 : 13 : g_return_if_fail (modified != NULL);
2775 : :
2776 : 12 : item = g_bookmark_file_lookup_item (bookmark, uri);
2777 : 12 : if (!item)
2778 : : {
2779 : 1 : item = bookmark_item_new (uri);
2780 : 1 : g_bookmark_file_add_item (bookmark, item, NULL);
2781 : : }
2782 : :
2783 : 12 : g_clear_pointer (&item->modified, g_date_time_unref);
2784 : 12 : item->modified = g_date_time_ref (modified);
2785 : : }
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 : 3 : g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2806 : : const gchar *uri,
2807 : : GError **error)
2808 : : {
2809 : 3 : GDateTime *modified = g_bookmark_file_get_modified_date_time (bookmark, uri, error);
2810 : 3 : return (modified != NULL) ? g_date_time_to_unix (modified) : (time_t) -1;
2811 : : }
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 : 35 : g_bookmark_file_get_modified_date_time (GBookmarkFile *bookmark,
2830 : : const char *uri,
2831 : : GError **error)
2832 : : {
2833 : : BookmarkItem *item;
2834 : :
2835 : 35 : g_return_val_if_fail (bookmark != NULL, NULL);
2836 : 34 : g_return_val_if_fail (uri != NULL, NULL);
2837 : 33 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2838 : :
2839 : 33 : item = g_bookmark_file_lookup_item (bookmark, uri);
2840 : 33 : if (!item)
2841 : : {
2842 : 12 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2843 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2844 : : _("No bookmark found for URI “%s”"),
2845 : : uri);
2846 : 12 : return NULL;
2847 : : }
2848 : :
2849 : 21 : return item->modified;
2850 : : }
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 : 2 : g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2874 : : const gchar *uri,
2875 : : time_t visited)
2876 : : {
2877 : 2 : GDateTime *visited_dt = (visited != (time_t) -1) ? g_date_time_new_from_unix_utc (visited) : g_date_time_new_now_utc ();
2878 : 2 : g_bookmark_file_set_visited_date_time (bookmark, uri, visited_dt);
2879 : 2 : g_date_time_unref (visited_dt);
2880 : 2 : }
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 : 15 : g_bookmark_file_set_visited_date_time (GBookmarkFile *bookmark,
2902 : : const char *uri,
2903 : : GDateTime *visited)
2904 : : {
2905 : : BookmarkItem *item;
2906 : :
2907 : 15 : g_return_if_fail (bookmark != NULL);
2908 : 14 : g_return_if_fail (uri != NULL);
2909 : 13 : g_return_if_fail (visited != NULL);
2910 : :
2911 : 12 : item = g_bookmark_file_lookup_item (bookmark, uri);
2912 : 12 : if (!item)
2913 : : {
2914 : 1 : item = bookmark_item_new (uri);
2915 : 1 : g_bookmark_file_add_item (bookmark, item, NULL);
2916 : : }
2917 : :
2918 : 12 : g_clear_pointer (&item->visited, g_date_time_unref);
2919 : 12 : item->visited = g_date_time_ref (visited);
2920 : : }
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 : 3 : g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2941 : : const gchar *uri,
2942 : : GError **error)
2943 : : {
2944 : 3 : GDateTime *visited = g_bookmark_file_get_visited_date_time (bookmark, uri, error);
2945 : 3 : return (visited != NULL) ? g_date_time_to_unix (visited) : (time_t) -1;
2946 : : }
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 : 26 : g_bookmark_file_get_visited_date_time (GBookmarkFile *bookmark,
2965 : : const char *uri,
2966 : : GError **error)
2967 : : {
2968 : : BookmarkItem *item;
2969 : :
2970 : 26 : g_return_val_if_fail (bookmark != NULL, NULL);
2971 : 25 : g_return_val_if_fail (uri != NULL, NULL);
2972 : 24 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2973 : :
2974 : 24 : item = g_bookmark_file_lookup_item (bookmark, uri);
2975 : 24 : if (!item)
2976 : : {
2977 : 12 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
2978 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2979 : : _("No bookmark found for URI “%s”"),
2980 : : uri);
2981 : 12 : return NULL;
2982 : : }
2983 : :
2984 : 12 : return item->visited;
2985 : : }
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 : 38 : g_bookmark_file_has_group (GBookmarkFile *bookmark,
3006 : : const gchar *uri,
3007 : : const gchar *group,
3008 : : GError **error)
3009 : : {
3010 : : BookmarkItem *item;
3011 : : GList *l;
3012 : :
3013 : 38 : g_return_val_if_fail (bookmark != NULL, FALSE);
3014 : 37 : g_return_val_if_fail (uri != NULL, FALSE);
3015 : :
3016 : 36 : item = g_bookmark_file_lookup_item (bookmark, uri);
3017 : 36 : if (!item)
3018 : : {
3019 : 9 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3020 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3021 : : _("No bookmark found for URI “%s”"),
3022 : : uri);
3023 : 9 : return FALSE;
3024 : : }
3025 : :
3026 : 27 : if (!item->metadata)
3027 : 0 : return FALSE;
3028 : :
3029 : 36 : for (l = item->metadata->groups; l != NULL; l = l->next)
3030 : : {
3031 : 18 : if (strcmp (l->data, group) == 0)
3032 : 9 : return TRUE;
3033 : : }
3034 : :
3035 : 18 : return FALSE;
3036 : :
3037 : : }
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 : 13 : g_bookmark_file_add_group (GBookmarkFile *bookmark,
3054 : : const gchar *uri,
3055 : : const gchar *group)
3056 : : {
3057 : : BookmarkItem *item;
3058 : :
3059 : 13 : g_return_if_fail (bookmark != NULL);
3060 : 12 : g_return_if_fail (uri != NULL);
3061 : 11 : g_return_if_fail (group != NULL && group[0] != '\0');
3062 : :
3063 : 9 : item = g_bookmark_file_lookup_item (bookmark, uri);
3064 : 9 : if (!item)
3065 : : {
3066 : 9 : item = bookmark_item_new (uri);
3067 : 9 : g_bookmark_file_add_item (bookmark, item, NULL);
3068 : : }
3069 : :
3070 : 9 : if (!item->metadata)
3071 : 9 : item->metadata = bookmark_metadata_new ();
3072 : :
3073 : 9 : if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
3074 : : {
3075 : 18 : item->metadata->groups = g_list_prepend (item->metadata->groups,
3076 : 9 : g_strdup (group));
3077 : :
3078 : 9 : bookmark_item_touch_modified (item);
3079 : : }
3080 : : }
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 : 13 : g_bookmark_file_remove_group (GBookmarkFile *bookmark,
3103 : : const gchar *uri,
3104 : : const gchar *group,
3105 : : GError **error)
3106 : : {
3107 : : BookmarkItem *item;
3108 : : GList *l;
3109 : :
3110 : 13 : g_return_val_if_fail (bookmark != NULL, FALSE);
3111 : 12 : g_return_val_if_fail (uri != NULL, FALSE);
3112 : :
3113 : 11 : item = g_bookmark_file_lookup_item (bookmark, uri);
3114 : 11 : if (!item)
3115 : : {
3116 : 2 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3117 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3118 : : _("No bookmark found for URI “%s”"),
3119 : : uri);
3120 : 2 : return FALSE;
3121 : : }
3122 : :
3123 : 9 : if (!item->metadata)
3124 : : {
3125 : 0 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3126 : : G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
3127 : : _("No groups set in bookmark for URI “%s”"),
3128 : : uri);
3129 : 0 : return FALSE;
3130 : : }
3131 : :
3132 : 9 : for (l = item->metadata->groups; l != NULL; l = l->next)
3133 : : {
3134 : 9 : if (strcmp (l->data, group) == 0)
3135 : : {
3136 : 9 : item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
3137 : 9 : g_free (l->data);
3138 : 9 : g_list_free_1 (l);
3139 : :
3140 : 9 : bookmark_item_touch_modified (item);
3141 : :
3142 : 9 : return TRUE;
3143 : : }
3144 : : }
3145 : :
3146 : 0 : return FALSE;
3147 : : }
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 : 12 : g_bookmark_file_set_groups (GBookmarkFile *bookmark,
3166 : : const gchar *uri,
3167 : : const gchar **groups,
3168 : : gsize length)
3169 : : {
3170 : : BookmarkItem *item;
3171 : : gsize i;
3172 : :
3173 : 12 : g_return_if_fail (bookmark != NULL);
3174 : 11 : g_return_if_fail (uri != NULL);
3175 : 10 : g_return_if_fail (groups != NULL);
3176 : :
3177 : 9 : item = g_bookmark_file_lookup_item (bookmark, uri);
3178 : 9 : if (!item)
3179 : : {
3180 : 0 : item = bookmark_item_new (uri);
3181 : 0 : g_bookmark_file_add_item (bookmark, item, NULL);
3182 : : }
3183 : :
3184 : 9 : if (!item->metadata)
3185 : 0 : item->metadata = bookmark_metadata_new ();
3186 : :
3187 : 9 : g_list_free_full (item->metadata->groups, g_free);
3188 : 9 : item->metadata->groups = NULL;
3189 : :
3190 : 9 : if (groups)
3191 : : {
3192 : 27 : for (i = 0; i < length && groups[i] != NULL; i++)
3193 : 18 : item->metadata->groups = g_list_append (item->metadata->groups,
3194 : 36 : g_strdup (groups[i]));
3195 : : }
3196 : :
3197 : 9 : bookmark_item_touch_modified (item);
3198 : : }
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 : 22 : g_bookmark_file_get_groups (GBookmarkFile *bookmark,
3222 : : const gchar *uri,
3223 : : gsize *length,
3224 : : GError **error)
3225 : : {
3226 : : BookmarkItem *item;
3227 : : GList *l;
3228 : : gsize len, i;
3229 : : gchar **retval;
3230 : :
3231 : 22 : g_return_val_if_fail (bookmark != NULL, NULL);
3232 : 21 : g_return_val_if_fail (uri != NULL, NULL);
3233 : :
3234 : 20 : item = g_bookmark_file_lookup_item (bookmark, uri);
3235 : 20 : if (!item)
3236 : : {
3237 : 2 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3238 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3239 : : _("No bookmark found for URI “%s”"),
3240 : : uri);
3241 : 2 : return NULL;
3242 : : }
3243 : :
3244 : 18 : if (!item->metadata)
3245 : : {
3246 : 0 : if (length)
3247 : 0 : *length = 0;
3248 : :
3249 : 0 : return NULL;
3250 : : }
3251 : :
3252 : 18 : len = g_list_length (item->metadata->groups);
3253 : 18 : retval = g_new0 (gchar *, len + 1);
3254 : 18 : for (l = g_list_last (item->metadata->groups), i = 0;
3255 : 36 : l != NULL;
3256 : 18 : l = l->prev)
3257 : : {
3258 : 18 : gchar *group_name = (gchar *) l->data;
3259 : :
3260 : 18 : g_warn_if_fail (group_name != NULL);
3261 : :
3262 : 36 : retval[i++] = g_strdup (group_name);
3263 : : }
3264 : 18 : retval[i] = NULL;
3265 : :
3266 : 18 : if (length)
3267 : 9 : *length = len;
3268 : :
3269 : 18 : return retval;
3270 : : }
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 : 25 : g_bookmark_file_add_application (GBookmarkFile *bookmark,
3307 : : const gchar *uri,
3308 : : const gchar *name,
3309 : : const gchar *exec)
3310 : : {
3311 : : BookmarkItem *item;
3312 : : gchar *app_name, *app_exec;
3313 : : GDateTime *stamp;
3314 : :
3315 : 25 : g_return_if_fail (bookmark != NULL);
3316 : 24 : g_return_if_fail (uri != NULL);
3317 : :
3318 : 23 : item = g_bookmark_file_lookup_item (bookmark, uri);
3319 : 23 : if (!item)
3320 : : {
3321 : 2 : item = bookmark_item_new (uri);
3322 : 2 : g_bookmark_file_add_item (bookmark, item, NULL);
3323 : : }
3324 : :
3325 : 23 : if (name && name[0] != '\0')
3326 : 21 : app_name = g_strdup (name);
3327 : : else
3328 : 4 : app_name = g_strdup (g_get_application_name ());
3329 : :
3330 : 23 : if (exec && exec[0] != '\0')
3331 : 20 : app_exec = g_strdup (exec);
3332 : : else
3333 : 3 : app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3334 : :
3335 : 23 : stamp = g_date_time_new_now_utc ();
3336 : :
3337 : 23 : g_bookmark_file_set_application_info (bookmark, uri,
3338 : : app_name,
3339 : : app_exec,
3340 : : -1,
3341 : : stamp,
3342 : : NULL);
3343 : :
3344 : 23 : g_date_time_unref (stamp);
3345 : 23 : g_free (app_exec);
3346 : 23 : g_free (app_name);
3347 : : }
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 : 14 : g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3371 : : const gchar *uri,
3372 : : const gchar *name,
3373 : : GError **error)
3374 : : {
3375 : : GError *set_error;
3376 : : gboolean retval;
3377 : :
3378 : 14 : g_return_val_if_fail (bookmark != NULL, FALSE);
3379 : 13 : g_return_val_if_fail (uri != NULL, FALSE);
3380 : 12 : g_return_val_if_fail (name != NULL, FALSE);
3381 : :
3382 : 11 : set_error = NULL;
3383 : 11 : retval = g_bookmark_file_set_application_info (bookmark, uri,
3384 : : name,
3385 : : "",
3386 : : 0,
3387 : : NULL,
3388 : : &set_error);
3389 : 11 : if (set_error)
3390 : : {
3391 : 1 : g_propagate_error (error, set_error);
3392 : :
3393 : 1 : return FALSE;
3394 : : }
3395 : :
3396 : 10 : return retval;
3397 : : }
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 : 22 : g_bookmark_file_has_application (GBookmarkFile *bookmark,
3418 : : const gchar *uri,
3419 : : const gchar *name,
3420 : : GError **error)
3421 : : {
3422 : : BookmarkItem *item;
3423 : :
3424 : 22 : g_return_val_if_fail (bookmark != NULL, FALSE);
3425 : 21 : g_return_val_if_fail (uri != NULL, FALSE);
3426 : 20 : g_return_val_if_fail (name != NULL, FALSE);
3427 : :
3428 : 19 : item = g_bookmark_file_lookup_item (bookmark, uri);
3429 : 19 : if (!item)
3430 : : {
3431 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3432 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3433 : : _("No bookmark found for URI “%s”"),
3434 : : uri);
3435 : 1 : return FALSE;
3436 : : }
3437 : :
3438 : 18 : return (NULL != bookmark_item_lookup_app_info (item, name));
3439 : : }
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 : 2 : 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 : : {
3496 : 2 : GDateTime *stamp_dt = (stamp != (time_t) -1) ? g_date_time_new_from_unix_utc (stamp) : g_date_time_new_now_utc ();
3497 : : gboolean retval;
3498 : 2 : retval = g_bookmark_file_set_application_info (bookmark, uri, name, exec, count,
3499 : : stamp_dt, error);
3500 : 2 : g_date_time_unref (stamp_dt);
3501 : 2 : return retval;
3502 : : }
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 : 41 : 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 : : {
3557 : : BookmarkItem *item;
3558 : : BookmarkAppInfo *ai;
3559 : :
3560 : 41 : g_return_val_if_fail (bookmark != NULL, FALSE);
3561 : 40 : g_return_val_if_fail (uri != NULL, FALSE);
3562 : 39 : g_return_val_if_fail (name != NULL, FALSE);
3563 : 38 : g_return_val_if_fail (exec != NULL, FALSE);
3564 : 37 : g_return_val_if_fail (count == 0 || stamp != NULL, FALSE);
3565 : 36 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3566 : :
3567 : 36 : item = g_bookmark_file_lookup_item (bookmark, uri);
3568 : 36 : if (!item)
3569 : : {
3570 : 0 : if (count == 0)
3571 : : {
3572 : 0 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3573 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3574 : : _("No bookmark found for URI “%s”"),
3575 : : uri);
3576 : 0 : return FALSE;
3577 : : }
3578 : : else
3579 : : {
3580 : 0 : item = bookmark_item_new (uri);
3581 : 0 : g_bookmark_file_add_item (bookmark, item, NULL);
3582 : : }
3583 : : }
3584 : :
3585 : 36 : if (!item->metadata)
3586 : 3 : item->metadata = bookmark_metadata_new ();
3587 : :
3588 : 36 : ai = bookmark_item_lookup_app_info (item, name);
3589 : 36 : if (!ai)
3590 : : {
3591 : 24 : if (count == 0)
3592 : : {
3593 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3594 : : G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3595 : : _("No application with name “%s” registered a bookmark for “%s”"),
3596 : : name,
3597 : : uri);
3598 : 1 : return FALSE;
3599 : : }
3600 : : else
3601 : : {
3602 : 23 : ai = bookmark_app_info_new (name);
3603 : :
3604 : 23 : item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3605 : 23 : g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3606 : : }
3607 : : }
3608 : :
3609 : 35 : if (count == 0)
3610 : : {
3611 : 10 : item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3612 : 10 : g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3613 : 10 : bookmark_app_info_free (ai);
3614 : :
3615 : 10 : bookmark_item_touch_modified (item);
3616 : :
3617 : 10 : return TRUE;
3618 : : }
3619 : 25 : else if (count > 0)
3620 : 2 : ai->count = count;
3621 : : else
3622 : 23 : ai->count += 1;
3623 : :
3624 : 25 : g_clear_pointer (&ai->stamp, g_date_time_unref);
3625 : 25 : ai->stamp = g_date_time_ref (stamp);
3626 : :
3627 : 25 : if (exec && exec[0] != '\0')
3628 : : {
3629 : 25 : g_free (ai->exec);
3630 : 25 : ai->exec = g_shell_quote (exec);
3631 : : }
3632 : :
3633 : 25 : bookmark_item_touch_modified (item);
3634 : :
3635 : 25 : return TRUE;
3636 : : }
3637 : :
3638 : : /* expands the application's command line */
3639 : : static gchar *
3640 : 10 : expand_exec_line (const gchar *exec_fmt,
3641 : : const gchar *uri)
3642 : : {
3643 : : GString *exec;
3644 : : gchar ch;
3645 : :
3646 : 10 : exec = g_string_sized_new (512);
3647 : 232 : while ((ch = *exec_fmt++) != '\0')
3648 : : {
3649 : 222 : if (ch != '%')
3650 : : {
3651 : 212 : exec = g_string_append_c (exec, ch);
3652 : 212 : continue;
3653 : : }
3654 : :
3655 : 10 : ch = *exec_fmt++;
3656 : 10 : switch (ch)
3657 : : {
3658 : 0 : case '\0':
3659 : 0 : goto out;
3660 : 1 : case 'U':
3661 : : case 'u':
3662 : : g_string_append (exec, uri);
3663 : 1 : break;
3664 : 9 : case 'F':
3665 : : case 'f':
3666 : : {
3667 : 9 : gchar *file = g_filename_from_uri (uri, NULL, NULL);
3668 : 9 : if (file)
3669 : : {
3670 : : g_string_append (exec, file);
3671 : 9 : g_free (file);
3672 : : }
3673 : : else
3674 : : {
3675 : 0 : g_string_free (exec, TRUE);
3676 : 0 : return NULL;
3677 : : }
3678 : : }
3679 : 9 : break;
3680 : 0 : case '%':
3681 : : default:
3682 : 0 : exec = g_string_append_c (exec, ch);
3683 : 0 : break;
3684 : : }
3685 : : }
3686 : :
3687 : 10 : out:
3688 : 10 : return g_string_free (exec, FALSE);
3689 : : }
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 : 4 : 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 : : {
3730 : 4 : GDateTime *stamp_dt = NULL;
3731 : : gboolean retval;
3732 : :
3733 : 4 : retval = g_bookmark_file_get_application_info (bookmark, uri, name, exec, count, &stamp_dt, error);
3734 : 4 : if (!retval)
3735 : 1 : return FALSE;
3736 : :
3737 : 3 : if (stamp != NULL)
3738 : 2 : *stamp = g_date_time_to_unix (stamp_dt);
3739 : :
3740 : 3 : return TRUE;
3741 : : }
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 : 28 : 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 : : {
3780 : : BookmarkItem *item;
3781 : : BookmarkAppInfo *ai;
3782 : :
3783 : 28 : g_return_val_if_fail (bookmark != NULL, FALSE);
3784 : 27 : g_return_val_if_fail (uri != NULL, FALSE);
3785 : 26 : g_return_val_if_fail (name != NULL, FALSE);
3786 : 25 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3787 : :
3788 : 25 : item = g_bookmark_file_lookup_item (bookmark, uri);
3789 : 25 : if (!item)
3790 : : {
3791 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3792 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3793 : : _("No bookmark found for URI “%s”"),
3794 : : uri);
3795 : 1 : return FALSE;
3796 : : }
3797 : :
3798 : 24 : ai = bookmark_item_lookup_app_info (item, name);
3799 : 24 : if (!ai)
3800 : : {
3801 : 11 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3802 : : G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3803 : : _("No application with name “%s” registered a bookmark for “%s”"),
3804 : : name,
3805 : : uri);
3806 : 11 : return FALSE;
3807 : : }
3808 : :
3809 : 13 : if (exec)
3810 : : {
3811 : 10 : GError *unquote_error = NULL;
3812 : : gchar *command_line;
3813 : :
3814 : 10 : command_line = g_shell_unquote (ai->exec, &unquote_error);
3815 : 10 : if (unquote_error)
3816 : : {
3817 : 0 : g_propagate_error (error, unquote_error);
3818 : 0 : return FALSE;
3819 : : }
3820 : :
3821 : 10 : *exec = expand_exec_line (command_line, uri);
3822 : 10 : if (!*exec)
3823 : : {
3824 : 0 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3825 : : G_BOOKMARK_FILE_ERROR_INVALID_URI,
3826 : : _("Failed to expand exec line “%s” with URI “%s”"),
3827 : : ai->exec, uri);
3828 : 0 : g_free (command_line);
3829 : :
3830 : 0 : return FALSE;
3831 : : }
3832 : : else
3833 : 10 : g_free (command_line);
3834 : : }
3835 : :
3836 : 13 : if (count)
3837 : 10 : *count = ai->count;
3838 : :
3839 : 13 : if (stamp)
3840 : 13 : *stamp = ai->stamp;
3841 : :
3842 : 13 : return TRUE;
3843 : : }
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 : 11 : g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3865 : : const gchar *uri,
3866 : : gsize *length,
3867 : : GError **error)
3868 : : {
3869 : : BookmarkItem *item;
3870 : : GList *l;
3871 : : gchar **apps;
3872 : : gsize i, n_apps;
3873 : :
3874 : 11 : g_return_val_if_fail (bookmark != NULL, NULL);
3875 : 10 : g_return_val_if_fail (uri != NULL, NULL);
3876 : :
3877 : 9 : item = g_bookmark_file_lookup_item (bookmark, uri);
3878 : 9 : if (!item)
3879 : : {
3880 : 0 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3881 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3882 : : _("No bookmark found for URI “%s”"),
3883 : : uri);
3884 : 0 : return NULL;
3885 : : }
3886 : :
3887 : 9 : if (!item->metadata)
3888 : : {
3889 : 0 : if (length)
3890 : 0 : *length = 0;
3891 : :
3892 : 0 : return NULL;
3893 : : }
3894 : :
3895 : 9 : n_apps = g_list_length (item->metadata->applications);
3896 : 9 : apps = g_new0 (gchar *, n_apps + 1);
3897 : :
3898 : 9 : for (l = g_list_last (item->metadata->applications), i = 0;
3899 : 18 : l != NULL;
3900 : 9 : l = l->prev)
3901 : : {
3902 : : BookmarkAppInfo *ai;
3903 : :
3904 : 9 : ai = (BookmarkAppInfo *) l->data;
3905 : :
3906 : 9 : g_warn_if_fail (ai != NULL);
3907 : 9 : g_warn_if_fail (ai->name != NULL);
3908 : :
3909 : 18 : apps[i++] = g_strdup (ai->name);
3910 : : }
3911 : 9 : apps[i] = NULL;
3912 : :
3913 : 9 : if (length)
3914 : 9 : *length = i;
3915 : :
3916 : 9 : return apps;
3917 : : }
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 : 4 : g_bookmark_file_get_size (GBookmarkFile *bookmark)
3931 : : {
3932 : 4 : g_return_val_if_fail (bookmark != NULL, 0);
3933 : :
3934 : 3 : return g_list_length (bookmark->items);
3935 : : }
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 : 9 : g_bookmark_file_move_item (GBookmarkFile *bookmark,
3957 : : const gchar *old_uri,
3958 : : const gchar *new_uri,
3959 : : GError **error)
3960 : : {
3961 : : BookmarkItem *item;
3962 : :
3963 : 9 : g_return_val_if_fail (bookmark != NULL, FALSE);
3964 : 8 : g_return_val_if_fail (old_uri != NULL, FALSE);
3965 : :
3966 : 7 : item = g_bookmark_file_lookup_item (bookmark, old_uri);
3967 : 7 : if (!item)
3968 : : {
3969 : 3 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
3970 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3971 : : _("No bookmark found for URI “%s”"),
3972 : : old_uri);
3973 : 3 : return FALSE;
3974 : : }
3975 : :
3976 : 4 : if (new_uri && new_uri[0] != '\0')
3977 : : {
3978 : 2 : if (g_strcmp0 (old_uri, new_uri) == 0)
3979 : 1 : return TRUE;
3980 : :
3981 : 1 : if (g_bookmark_file_has_item (bookmark, new_uri))
3982 : : {
3983 : 0 : if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3984 : 0 : return FALSE;
3985 : : }
3986 : :
3987 : 1 : g_hash_table_steal (bookmark->items_by_uri, item->uri);
3988 : :
3989 : 1 : g_free (item->uri);
3990 : 1 : item->uri = g_strdup (new_uri);
3991 : 1 : bookmark_item_touch_modified (item);
3992 : :
3993 : 1 : g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3994 : :
3995 : 1 : return TRUE;
3996 : : }
3997 : : else
3998 : : {
3999 : 2 : if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
4000 : 0 : return FALSE;
4001 : :
4002 : 2 : return TRUE;
4003 : : }
4004 : : }
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 : 14 : g_bookmark_file_set_icon (GBookmarkFile *bookmark,
4023 : : const gchar *uri,
4024 : : const gchar *href,
4025 : : const gchar *mime_type)
4026 : : {
4027 : : BookmarkItem *item;
4028 : :
4029 : 14 : g_return_if_fail (bookmark != NULL);
4030 : 13 : g_return_if_fail (uri != NULL);
4031 : :
4032 : 12 : item = g_bookmark_file_lookup_item (bookmark, uri);
4033 : 12 : if (!item)
4034 : : {
4035 : 1 : item = bookmark_item_new (uri);
4036 : 1 : g_bookmark_file_add_item (bookmark, item, NULL);
4037 : : }
4038 : :
4039 : 12 : if (!item->metadata)
4040 : 1 : item->metadata = bookmark_metadata_new ();
4041 : :
4042 : 12 : g_free (item->metadata->icon_href);
4043 : 12 : g_free (item->metadata->icon_mime);
4044 : :
4045 : 12 : item->metadata->icon_href = g_strdup (href);
4046 : :
4047 : 12 : if (mime_type && mime_type[0] != '\0')
4048 : 22 : item->metadata->icon_mime = g_strdup (mime_type);
4049 : : else
4050 : 2 : item->metadata->icon_mime = g_strdup ("application/octet-stream");
4051 : :
4052 : 12 : bookmark_item_touch_modified (item);
4053 : : }
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 : 16 : g_bookmark_file_get_icon (GBookmarkFile *bookmark,
4075 : : const gchar *uri,
4076 : : gchar **href,
4077 : : gchar **mime_type,
4078 : : GError **error)
4079 : : {
4080 : : BookmarkItem *item;
4081 : :
4082 : 16 : g_return_val_if_fail (bookmark != NULL, FALSE);
4083 : 15 : g_return_val_if_fail (uri != NULL, FALSE);
4084 : :
4085 : 14 : item = g_bookmark_file_lookup_item (bookmark, uri);
4086 : 14 : if (!item)
4087 : : {
4088 : 1 : g_set_error (error, G_BOOKMARK_FILE_ERROR,
4089 : : G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
4090 : : _("No bookmark found for URI “%s”"),
4091 : : uri);
4092 : 1 : return FALSE;
4093 : : }
4094 : :
4095 : 13 : if ((!item->metadata) || (!item->metadata->icon_href))
4096 : 2 : return FALSE;
4097 : :
4098 : 11 : if (href)
4099 : 22 : *href = g_strdup (item->metadata->icon_href);
4100 : :
4101 : 11 : if (mime_type)
4102 : 20 : *mime_type = g_strdup (item->metadata->icon_mime);
4103 : :
4104 : 11 : return TRUE;
4105 : : }
|