1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
/*
Copyright (C) 2007 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See the file COPYING for the full license text.
*/
/**
* This is an implementation of XBEL based on Glib and libXML2.
*
* Design Goals:
* - XbelItem is the only opaque public data structure.
* - The interface should be intuitive and familiar to Glib users.
* - There should be no public exposure of libXML2 specific code.
* - Bookmarks should actually be easily exchangeable between programs.
*
* TODO:
* - Support info > metadata, alias, added, modified, visited
* - Compatibility: The Nokia 770 *requires* metadata and folder
* - Compatibility: Kazehakase's bookmarks
* - Compatibility: Epiphany's bookmarks
* - XML Indentation
* - Export and import to other formats
**/
#include "xbel.h"
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
// Private: Create a new item of a certain type
static XbelItem* xbel_item_new(XbelItemKind kind)
{
XbelItem* item = g_new(XbelItem, 1);
item->parent = NULL;
item->kind = kind;
if(kind == XBEL_ITEM_FOLDER)
{
item->items = NULL;
item->folded = TRUE;
}
if(kind != XBEL_ITEM_SEPARATOR)
{
item->title = NULL;
item->desc = NULL;
}
if(kind == XBEL_ITEM_BOOKMARK)
item->href = g_strdup("");
return item;
}
/**
* xbel_bookmark_new:
*
* Create a new empty bookmark.
*
* Return value: a newly allocated bookmark
**/
XbelItem* xbel_bookmark_new(void)
{
return xbel_item_new(XBEL_ITEM_BOOKMARK);
}
static XbelItem* xbel_bookmark_from_xmlNodePtr(xmlNodePtr cur)
{
g_return_val_if_fail(cur != NULL, NULL);
XbelItem* bookmark = xbel_bookmark_new();
xmlChar* key = xmlGetProp(cur, (xmlChar*)"href");
xbel_bookmark_set_href(bookmark, (gchar*)key);
cur = cur->xmlChildrenNode;
while(cur != NULL)
{
if(!xmlStrcmp(cur->name, (const xmlChar*)"title"))
{
xmlChar* key = xmlNodeGetContent(cur);
bookmark->title = (gchar*)g_strstrip((gchar*)key);
}
else if(!xmlStrcmp(cur->name, (const xmlChar*)"desc"))
{
xmlChar* key = xmlNodeGetContent(cur);
bookmark->desc = (gchar*)g_strstrip((gchar*)key);
}
cur = cur->next;
}
return bookmark;
}
/**
* xbel_separator_new:
*
* Create a new separator.
*
* The returned item must be freed eventually.
*
* Return value: a newly allocated separator.
**/
XbelItem* xbel_separator_new(void)
{
return xbel_item_new(XBEL_ITEM_SEPARATOR);
}
/**
* xbel_folder_new:
*
* Create a new empty folder.
*
* The returned item must be freed eventually.
*
* Return value: a newly allocated folder.
**/
XbelItem* xbel_folder_new(void)
{
return xbel_item_new(XBEL_ITEM_FOLDER);
}
// Private: Create a folder from an xmlNodePtr
static XbelItem* xbel_folder_from_xmlNodePtr(xmlNodePtr cur)
{
g_return_val_if_fail(cur != NULL, NULL);
XbelItem* folder = xbel_folder_new();
xmlChar* key = xmlGetProp(cur, (xmlChar*)"folded");
if(key)
{
if(!g_ascii_strncasecmp((gchar*)key, "yes", 3))
folder->folded = TRUE;
else if(!g_ascii_strncasecmp((gchar*)key, "no", 2))
folder->folded = FALSE;
else
g_warning("XBEL: Unknown value for folded.");
xmlFree(key);
}
cur = cur->xmlChildrenNode;
while(cur)
{
if(!xmlStrcmp(cur->name, (const xmlChar*)"title"))
{
xmlChar* key = xmlNodeGetContent(cur);
folder->title = (gchar*)g_strstrip((gchar*)key);
}
else if(!xmlStrcmp(cur->name, (const xmlChar*)"desc"))
{
xmlChar* key = xmlNodeGetContent(cur);
folder->desc = (gchar*)g_strstrip((gchar*)key);
}
else if(!xmlStrcmp(cur->name, (const xmlChar*)"folder"))
{
XbelItem* item = xbel_folder_from_xmlNodePtr(cur);
item->parent = (struct XbelItem*)folder;
folder->items = g_list_prepend(folder->items, item);
}
else if(!xmlStrcmp(cur->name, (const xmlChar*)"bookmark"))
{
XbelItem* item = xbel_bookmark_from_xmlNodePtr(cur);
item->parent = (struct XbelItem*)folder;
folder->items = g_list_prepend(folder->items, item);
}
else if(!xmlStrcmp(cur->name, (const xmlChar*)"separator"))
{
XbelItem* item = xbel_separator_new();
item->parent = (struct XbelItem*)folder;
folder->items = g_list_prepend(folder->items, item);
}
cur = cur->next;
}
// Prepending and reversing is faster than appending
folder->items = g_list_reverse(folder->items);
return folder;
}
// Private: Loads the contents from an xmlNodePtr into a folder.
static gboolean xbel_folder_from_xmlDocPtr(XbelItem* folder, xmlDocPtr doc)
{
g_return_val_if_fail(xbel_folder_is_empty(folder), FALSE);
g_return_val_if_fail(doc != NULL, FALSE);
xmlNodePtr cur = xmlDocGetRootElement(doc);
xmlChar* version = xmlGetProp(cur, (xmlChar*)"version");
if(xmlStrcmp(version, (xmlChar*)"1.0"))
g_warning("XBEL version is not 1.0.");
xmlFree(version);
folder->title = (gchar*)xmlGetProp(cur, (xmlChar*)"title");
folder->desc = (gchar*)xmlGetProp(cur, (xmlChar*)"desc");
if((cur = xmlDocGetRootElement(doc)) == NULL)
{
// Empty document
return FALSE;
}
if(xmlStrcmp(cur->name, (const xmlChar*)"xbel"))
{
// Wrong document kind
return FALSE;
}
cur = cur->xmlChildrenNode;
while(cur != NULL)
{
XbelItem* item = NULL;
if(!xmlStrcmp(cur->name, (const xmlChar*)"folder"))
item = xbel_folder_from_xmlNodePtr(cur);
else if(!xmlStrcmp(cur->name, (const xmlChar*)"bookmark"))
item = xbel_bookmark_from_xmlNodePtr(cur);
else if(!xmlStrcmp(cur->name, (const xmlChar*)"separator"))
item = xbel_separator_new();
/*else if(!xmlStrcmp(cur->name, (const xmlChar*)"info"))
item = xbel_parse_info(xbel, cur);*/
if(item != NULL)
{
item->parent = (struct XbelItem*)folder;
folder->items = g_list_prepend(folder->items, item);
}
cur = cur->next;
}
// Prepending and reversing is faster than appending
folder->items = g_list_reverse(folder->items);
return TRUE;
}
/**
* xbel_item_free:
* @item: a valid item
*
* Free an XbelItem. If @item is a folder all of its children will also
* be freed automatically.
*
* The item must not be contained in a folder or attempting to free it will fail.
**/
void xbel_item_free(XbelItem* item)
{
g_return_if_fail(item);
g_return_if_fail(!xbel_item_get_parent(item));
if(xbel_item_is_folder(item))
{
guint n = xbel_folder_get_n_items(item);
guint i;
for(i = 0; i < n; i++)
{
XbelItem* _item = xbel_folder_get_nth_item(item, i);
_item->parent = NULL;
xbel_item_free(_item);
}
g_list_free(item->items);
}
if(item->kind != XBEL_ITEM_SEPARATOR)
{
g_free(item->title);
g_free(item->desc);
}
if(item->kind == XBEL_ITEM_BOOKMARK)
g_free(item->href);
g_free(item);
}
/**
* xbel_item_copy:
* @item: the item to copy
*
* Copy an XbelItem.
*
* The returned item must be freed eventually.
*
* Return value: a copy of @item
**/
XbelItem* xbel_item_copy(XbelItem* item)
{
g_return_val_if_fail(item, NULL);
XbelItem* copy = xbel_folder_new();
if(xbel_item_is_folder(item))
{
guint n = xbel_folder_get_n_items(item);
guint i;
for(i = 0; i < n; i++)
{
XbelItem* _item = xbel_folder_get_nth_item(item, i);
xbel_folder_append_item(copy, xbel_item_copy(_item));
}
}
if(item->kind != XBEL_ITEM_SEPARATOR)
{
xbel_item_set_title(copy, item->title);
xbel_item_set_desc(copy, item->desc);
}
if(item->kind == XBEL_ITEM_BOOKMARK)
xbel_bookmark_set_href(copy, item->href);
return copy;
}
GType xbel_item_get_type()
{
static GType type = 0;
if(!type)
type = g_pointer_type_register_static("xbel_item");
return type;
}
/**
* xbel_folder_append_item:
* @folder: a folder
* @item: the item to append
*
* Append the given item to a folder.
*
* The item is actually moved and must not be contained in another folder.
*
**/
void xbel_folder_append_item(XbelItem* folder, XbelItem* item)
{
g_return_if_fail(!xbel_item_get_parent(item));
g_return_if_fail(xbel_item_is_folder(folder));
item->parent = (struct XbelItem*)folder;
folder->items = g_list_append(folder->items, item);
}
/**
* xbel_folder_prepend_item:
* @folder: a folder
* @item: the item to prepend
*
* Prepend the given item to a folder.
*
* The item is actually moved and must not be contained in another folder.
*
**/
void xbel_folder_prepend_item(XbelItem* folder, XbelItem* item)
{
g_return_if_fail(!xbel_item_get_parent(item));
g_return_if_fail(xbel_item_is_folder(folder));
item->parent = (struct XbelItem*)folder;
folder->items = g_list_prepend(folder->items, item);
}
/**
* xbel_folder_remove_item:
* @folder: a folder
* @item: the item to remove
*
* Remove the given @item from a @folder.
**/
void xbel_folder_remove_item(XbelItem* folder, XbelItem* item)
{
g_return_if_fail(item);
g_return_if_fail(xbel_item_get_parent(folder) != item);
item->parent = NULL;
// Fortunately we know that items are unique
folder->items = g_list_remove(folder->items, item);
}
/**
* xbel_folder_get_n_items:
* @folder: a folder
*
* Retrieve the number of items contained in the given @folder.
*
* Return value: number of items
**/
guint xbel_folder_get_n_items(XbelItem* folder)
{
g_return_val_if_fail(xbel_item_is_folder(folder), 0);
return g_list_length(folder->items);
}
/**
* xbel_folder_get_nth_item:
* @folder: a folder
* @n: the index of an item
*
* Retrieve an item contained in the given @folder by its index.
*
* Return value: the item at the given index or %NULL
**/
XbelItem* xbel_folder_get_nth_item(XbelItem* folder, guint n)
{
g_return_val_if_fail(xbel_item_is_folder(folder), NULL);
return (XbelItem*)g_list_nth_data(folder->items, n);
}
/**
* xbel_folder_is_empty:
* @folder: A folder.
*
* Determines wether or not a folder contains no items. This is significantly
* faster than xbel_folder_get_n_items for this particular purpose.
*
* Return value: Wether the given @folder is folded.
**/
gboolean xbel_folder_is_empty(XbelItem* folder)
{
return !xbel_folder_get_nth_item(folder, 0);
}
/**
* xbel_folder_get_folded:
* @folder: A folder.
*
* Determines wether or not a folder is folded. If a folder is not folded
* it should not be exposed in a user interface.
*
* New folders are folded by default.
*
* Return value: Wether the given @folder is folded.
**/
gboolean xbel_folder_get_folded(XbelItem* folder)
{
g_return_val_if_fail(xbel_item_is_folder(folder), TRUE);
return folder->folded;
}
/**
* xbel_item_get_kind:
* @item: A item.
*
* Determines the kind of an item.
*
* Return value: The kind of the given @item.
**/
XbelItemKind xbel_item_get_kind(XbelItem* item)
{
return item->kind;
}
/**
* xbel_item_get_parent:
* @item: A valid item.
*
* Retrieves the parent folder of an item.
*
* Return value: The parent folder of the given @item or %NULL.
**/
XbelItem* xbel_item_get_parent(XbelItem* item)
{
g_return_val_if_fail(item, NULL);
return (XbelItem*)item->parent;
}
/**
* xbel_item_get_title:
* @item: A valid item.
*
* Retrieves the title of an item.
*
* Return value: The title of the given @item or %NULL.
**/
G_CONST_RETURN gchar* xbel_item_get_title(XbelItem* item)
{
g_return_val_if_fail(!xbel_item_is_separator(item), NULL);
return item->title;
}
/**
* xbel_item_get_desc:
* @item: A valid item.
*
* Retrieves the description of an item.
*
* Return value: The description of the @item or %NULL.
**/
G_CONST_RETURN gchar* xbel_item_get_desc(XbelItem* item)
{
g_return_val_if_fail(!xbel_item_is_separator(item), NULL);
return item->desc;
}
/**
* xbel_bookmark_get_href:
* @bookmark: A bookmark.
*
* Retrieves the uri of a bookmark. The value is guaranteed to not be %NULL.
*
* Return value: The uri of the @bookmark.
**/
G_CONST_RETURN gchar* xbel_bookmark_get_href(XbelItem* bookmark)
{
g_return_val_if_fail(xbel_item_is_bookmark(bookmark), NULL);
return bookmark->href;
}
/**
* xbel_item_is_bookmark:
* @item: A valid item.
*
* Determines wether or not an item is a bookmark.
*
* Return value: %TRUE if the @item is a bookmark.
**/
gboolean xbel_item_is_bookmark(XbelItem* item)
{
g_return_val_if_fail(item, FALSE);
return item->kind == XBEL_ITEM_BOOKMARK;
}
/**
* xbel_item_is_separator:
* @item: A valid item.
*
* Determines wether or not an item is a separator.
*
* Return value: %TRUE if the @item is a separator.
**/
gboolean xbel_item_is_separator(XbelItem* item)
{
g_return_val_if_fail(item, FALSE);
return item->kind == XBEL_ITEM_SEPARATOR;
}
/**
* xbel_item_is_folder:
* @item: A valid item.
*
* Determines wether or not an item is a folder.
*
* Return value: %TRUE if the item is a folder.
**/
gboolean xbel_item_is_folder(XbelItem* item)
{
g_return_val_if_fail(item, FALSE);
return item->kind == XBEL_ITEM_FOLDER;
}
/**
* xbel_folder_set_folded:
* @folder: A folder.
* @folded: TRUE if the folder is folded.
*
* Sets the foldedness of the @folder.
**/
void xbel_folder_set_folded(XbelItem* folder, gboolean folded)
{
g_return_if_fail(xbel_item_is_folder(folder));
folder->folded = folded;
}
/**
* xbel_item_set_title:
* @item: A valid item.
* @title: A string to use for the title.
*
* Sets the title of the @item.
**/
void xbel_item_set_title(XbelItem* item, const gchar* title)
{
g_return_if_fail(!xbel_item_is_separator(item));
g_free(item->title);
item->title = g_strdup(title);
}
/**
* xbel_item_set_desc:
* @item: A valid item.
* @title: A string to use for the description.
*
* Sets the description of the @item.
**/
void xbel_item_set_desc(XbelItem* item, const gchar* desc)
{
g_return_if_fail(!xbel_item_is_separator(item));
g_free(item->desc);
item->desc = g_strdup(desc);
}
/**
* xbel_bookmark_set_href:
* @bookmark: A bookmark.
* @href: A string containing a valid uri.
*
* Sets the uri of the bookmark.
*
* The uri must not be %NULL.
*
* This uri is not currently validated in any way. This may change in the future.
**/
void xbel_bookmark_set_href(XbelItem* bookmark, const gchar* href)
{
g_return_if_fail(xbel_item_is_bookmark(bookmark));
g_return_if_fail(href);
g_free(bookmark->href);
bookmark->href = g_strdup(href);
}
gboolean xbel_folder_from_data(XbelItem* folder, const gchar* data, GError** error)
{
g_return_val_if_fail(xbel_folder_is_empty(folder), FALSE);
g_return_val_if_fail(data, FALSE);
xmlDocPtr doc;
if((doc = xmlParseMemory(data, strlen(data))) == NULL)
{
// No valid xml or broken encoding
*error = g_error_new(XBEL_ERROR, XBEL_ERROR_READ
, "Malformed document.");
return FALSE;
}
if(!xbel_folder_from_xmlDocPtr(folder, doc))
{
// Parsing failed
xmlFreeDoc(doc);
*error = g_error_new(XBEL_ERROR, XBEL_ERROR_READ
, "Malformed document.");
return FALSE;
}
xmlFreeDoc(doc);
return TRUE;
}
/**
* xbel_folder_from_file:
* @folder: An empty folder.
* @file: A relative path to a file.
* @error: return location for a GError or %NULL
*
* Tries to load @file.
*
* Return value: %TRUE on success or %FALSE when an error occured.
**/
gboolean xbel_folder_from_file(XbelItem* folder, const gchar* file, GError** error)
{
g_return_val_if_fail(xbel_folder_is_empty(folder), FALSE);
g_return_val_if_fail(file, FALSE);
if(!g_file_test(file, G_FILE_TEST_EXISTS))
{
// File doesn't exist
*error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_NOENT
, "File not found.");
return FALSE;
}
xmlDocPtr doc;
if((doc = xmlParseFile(file)) == NULL)
{
// No valid xml or broken encoding
*error = g_error_new(XBEL_ERROR, XBEL_ERROR_READ
, "Malformed document.");
return FALSE;
}
if(!xbel_folder_from_xmlDocPtr(folder, doc))
{
// Parsing failed
xmlFreeDoc(doc);
*error = g_error_new(XBEL_ERROR, XBEL_ERROR_READ
, "Malformed document.");
return FALSE;
}
xmlFreeDoc(doc);
return TRUE;
}
/**
* xbel_folder_from_data_dirs:
* @folder: An empty folder.
* @file: A relative path to a file.
* @full_path: return location for the full path of the file or %NULL
* @error: return location for a GError or %NULL
*
* Tries to load @file from the user data dir or any of the system data dirs.
*
* Return value: %TRUE on success or %FALSE when an error occured.
**/
gboolean xbel_folder_from_data_dirs(XbelItem* folder, const gchar* file
, gchar** full_path, GError** error)
{
g_return_val_if_fail(xbel_folder_is_empty(folder), FALSE);
g_return_val_if_fail(file, FALSE);
// FIXME: Essentially unimplemented
*error = g_error_new(XBEL_ERROR, XBEL_ERROR_READ
, "Malformed document.");
return FALSE;
}
static gchar* xbel_xml_element(const gchar* name, const gchar* value)
{
if(!value)
return g_strdup("");
gchar* valueEscaped = g_markup_escape_text(value, -1);
gchar* XML = g_strdup_printf("<%s>%s</%s>\n", name, valueEscaped, name);
g_free(valueEscaped);
return XML;
}
static gchar* xbel_item_to_data(XbelItem* item)
{
g_return_val_if_fail(item, NULL);
gchar* XML = NULL;
switch(xbel_item_get_kind(item))
{
case XBEL_ITEM_FOLDER:
{
GString* _XML = g_string_new(NULL);
guint n = xbel_folder_get_n_items(item);
guint i;
for(i = 0; i < n; i++)
{
XbelItem* _item = xbel_folder_get_nth_item(item, i);
gchar* itemXML = xbel_item_to_data(_item);
g_string_append(_XML, itemXML);
g_free(itemXML);
}
gchar* folded = item->folded ? NULL : g_strdup_printf(" folded=\"no\"");
gchar* title = xbel_xml_element("title", item->title);
gchar* desc = xbel_xml_element("desc", item->desc);
XML = g_strdup_printf("<folder%s>\n%s%s%s</folder>\n"
, folded ? folded : ""
, title
, desc
, g_string_free(_XML, FALSE));
g_free(folded);
g_free(title);
g_free(desc);
break;
}
case XBEL_ITEM_BOOKMARK:
{
gchar* hrefEscaped = g_markup_escape_text(item->href, -1);
gchar* href = g_strdup_printf(" href=\"%s\"", hrefEscaped);
g_free(hrefEscaped);
gchar* title = xbel_xml_element("title", item->title);
gchar* desc = xbel_xml_element("desc", item->desc);
XML = g_strdup_printf("<bookmark%s>\n%s%s%s</bookmark>\n"
, href
, title
, desc
, "");
g_free(href);
g_free(title);
g_free(desc);
break;
}
case XBEL_ITEM_SEPARATOR:
XML = g_strdup("<separator/>\n");
break;
default:
g_warning("XBEL: Unknown item kind");
}
return XML;
}
/**
* xbel_folder_to_data:
* @folder: A folder.
* @length: return location for the length of the created string or %NULL
* @error: return location for a GError or %NULL
*
* Retrieve the contents of @folder as a string.
*
* Return value: %TRUE on success or %FALSE when an error occured.
**/
gchar* xbel_folder_to_data(XbelItem* folder, gsize* length, GError** error)
{
g_return_val_if_fail(xbel_item_is_folder(folder), FALSE);
// FIXME: length is never filled
GString* innerXML = g_string_new(NULL);
guint n = xbel_folder_get_n_items(folder);
guint i;
for(i = 0; i < n; i++)
{
gchar* sItem = xbel_item_to_data(xbel_folder_get_nth_item(folder, i));
g_string_append(innerXML, sItem);
g_free(sItem);
}
gchar* title = xbel_xml_element("title", folder->title);
gchar* desc = xbel_xml_element("desc", folder->desc);
gchar* outerXML;
outerXML = g_strdup_printf("%s%s<xbel version=\"1.0\">\n%s%s%s</xbel>\n"
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
, "<!DOCTYPE xbel PUBLIC \"+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML\" \"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd\">\n"
, title
, desc
, g_string_free(innerXML, FALSE));
g_free(title);
g_free(desc);
return outerXML;
}
/**
* xbel_folder_to_file:
* @folder: A folder.
* @file: The destination filename.
* @error: return location for a GError or %NULL
*
* Write the contents of @folder to the specified file, creating it if necessary.
*
* Return value: %TRUE on success or %FALSE when an error occured.
**/
gboolean xbel_folder_to_file(XbelItem* folder, const gchar* file, GError** error)
{
g_return_val_if_fail(file, FALSE);
gchar* data;
if(!(data = xbel_folder_to_data(folder, NULL, error)))
return FALSE;
FILE* fp;
if(!(fp = fopen(file, "w")))
{
*error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_ACCES
, "Writing failed.");
return FALSE;
}
fputs(data, fp);
fclose(fp);
g_free(data);
return TRUE;
}
|