Turn KatzeXbelItem into a gobject

This commit is contained in:
Christian Dywan 2008-06-24 21:47:41 +02:00
parent 282368ff81
commit 8600c2591d
2 changed files with 153 additions and 172 deletions

View file

@ -28,48 +28,90 @@
#include "katze-utils.h" #include "katze-utils.h"
struct _KatzeXbelItemPrivate struct _KatzeXbelItem
{ {
GObject parent_instance;
guint refs; guint refs;
KatzeXbelItemKind kind; KatzeXbelItemKind kind;
KatzeXbelItem* parent; KatzeXbelItem* parent;
GList* items; // folder GList* items;
gboolean folded; // foolder gboolean folded;
gchar* title; // !separator gchar* title;
gchar* desc; // folder and bookmark gchar* desc;
gchar* href; // bookmark gchar* href;
//time_t added; // !separator };
//time_t modfied; // bookmark
//time_t visited; // bookmark
} ;
#define KATZE_XBEL_ITEM_GET_PRIVATE(item) \ G_DEFINE_TYPE (KatzeXbelItem, katze_xbel_item, G_TYPE_OBJECT)
item->priv
// Private: Create a new item of a certain type static void
katze_xbel_item_finalize (GObject* object);
static void
katze_xbel_item_class_init (KatzeXbelItemClass* class)
{
GObjectClass* gobject_class = G_OBJECT_CLASS (class);
gobject_class->finalize = katze_xbel_item_finalize;
}
static void
katze_xbel_item_init (KatzeXbelItem* item)
{
item->parent = NULL;
}
static void
katze_xbel_item_finalize (GObject* object)
{
KatzeXbelItem* item = KATZE_XBEL_ITEM (object);
if (item->parent)
katze_xbel_folder_remove_item (item->parent, item);
if (item->kind == KATZE_XBEL_ITEM_KIND_FOLDER)
{
guint n = katze_xbel_folder_get_n_items (item);
guint i;
for (i = 0; i < n; i++)
{
KatzeXbelItem* _item = katze_xbel_folder_get_nth_item (item, i);
_item->parent = NULL;
katze_xbel_item_unref (_item);
}
g_list_free (item->items);
}
if (item->kind != KATZE_XBEL_ITEM_KIND_SEPARATOR)
{
g_free (item->title);
g_free (item->desc);
}
if (item->kind == KATZE_XBEL_ITEM_KIND_BOOKMARK)
g_free (item->href);
G_OBJECT_CLASS (katze_xbel_item_parent_class)->finalize (object);
}
/* Create a new item of a certain type */
static KatzeXbelItem* static KatzeXbelItem*
katze_xbel_item_new (KatzeXbelItemKind kind) katze_xbel_item_new (KatzeXbelItemKind kind)
{ {
KatzeXbelItem* item = g_new (KatzeXbelItem, 1); KatzeXbelItem* item = g_object_new (KATZE_TYPE_XBEL_ITEM, NULL);
KATZE_XBEL_ITEM_GET_PRIVATE (item) = g_new (KatzeXbelItemPrivate, 1);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); item->kind = kind;
priv->refs = 1;
priv->parent = NULL;
priv->kind = kind;
if (kind == KATZE_XBEL_ITEM_KIND_FOLDER) if (kind == KATZE_XBEL_ITEM_KIND_FOLDER)
{ {
priv->items = NULL; item->items = NULL;
priv->folded = TRUE; item->folded = TRUE;
} }
if (kind != KATZE_XBEL_ITEM_KIND_SEPARATOR) if (kind != KATZE_XBEL_ITEM_KIND_SEPARATOR)
{ {
priv->title = NULL; item->title = NULL;
priv->desc = NULL; item->desc = NULL;
} }
if (kind == KATZE_XBEL_ITEM_KIND_BOOKMARK) if (kind == KATZE_XBEL_ITEM_KIND_BOOKMARK)
priv->href = g_strdup (""); item->href = g_strdup ("");
return item; return item;
} }
@ -142,22 +184,21 @@ katze_xbel_folder_new (void)
return katze_xbel_item_new (KATZE_XBEL_ITEM_KIND_FOLDER); return katze_xbel_item_new (KATZE_XBEL_ITEM_KIND_FOLDER);
} }
// Private: Create a folder from an xmlNodePtr /* Create a folder from an xmlNodePtr */
static KatzeXbelItem* static KatzeXbelItem*
katze_xbel_folder_from_xmlNodePtr (xmlNodePtr cur) katze_xbel_folder_from_xmlNodePtr (xmlNodePtr cur)
{ {
g_return_val_if_fail (cur, NULL); g_return_val_if_fail (cur, NULL);
KatzeXbelItem* folder = katze_xbel_folder_new (); KatzeXbelItem* folder = katze_xbel_folder_new ();
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder);
xmlChar* key = xmlGetProp (cur, (xmlChar*)"folded"); xmlChar* key = xmlGetProp (cur, (xmlChar*)"folded");
if (key) if (key)
{ {
if (!g_ascii_strncasecmp ((gchar*)key, "yes", 3)) if (!g_ascii_strncasecmp ((gchar*)key, "yes", 3))
priv->folded = TRUE; folder->folded = TRUE;
else if (!g_ascii_strncasecmp ((gchar*)key, "no", 2)) else if (!g_ascii_strncasecmp ((gchar*)key, "no", 2))
priv->folded = FALSE; folder->folded = FALSE;
else else
g_warning ("XBEL: Unknown value for folded."); g_warning ("XBEL: Unknown value for folded.");
xmlFree (key); xmlFree (key);
@ -168,39 +209,39 @@ katze_xbel_folder_from_xmlNodePtr (xmlNodePtr cur)
if (!xmlStrcmp (cur->name, (const xmlChar*)"title")) if (!xmlStrcmp (cur->name, (const xmlChar*)"title"))
{ {
xmlChar* key = xmlNodeGetContent (cur); xmlChar* key = xmlNodeGetContent (cur);
katze_assign (priv->title, g_strstrip ((gchar*)key)); katze_assign (folder->title, g_strstrip ((gchar*)key));
} }
else if (!xmlStrcmp (cur->name, (const xmlChar*)"desc")) else if (!xmlStrcmp (cur->name, (const xmlChar*)"desc"))
{ {
xmlChar* key = xmlNodeGetContent (cur); xmlChar* key = xmlNodeGetContent (cur);
katze_assign (priv->desc, g_strstrip ((gchar*)key)); katze_assign (folder->desc, g_strstrip ((gchar*)key));
} }
else if (!xmlStrcmp (cur->name, (const xmlChar*)"folder")) else if (!xmlStrcmp (cur->name, (const xmlChar*)"folder"))
{ {
KatzeXbelItem* item = katze_xbel_folder_from_xmlNodePtr (cur); KatzeXbelItem* item = katze_xbel_folder_from_xmlNodePtr (cur);
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = folder; item->parent = folder;
priv->items = g_list_prepend (priv->items, item); folder->items = g_list_prepend (folder->items, item);
} }
else if (!xmlStrcmp (cur->name, (const xmlChar*)"bookmark")) else if (!xmlStrcmp (cur->name, (const xmlChar*)"bookmark"))
{ {
KatzeXbelItem* item = katze_xbel_bookmark_from_xmlNodePtr (cur); KatzeXbelItem* item = katze_xbel_bookmark_from_xmlNodePtr (cur);
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = folder; item->parent = folder;
priv->items = g_list_prepend (priv->items, item); folder->items = g_list_prepend (folder->items, item);
} }
else if (!xmlStrcmp (cur->name, (const xmlChar*)"separator")) else if (!xmlStrcmp (cur->name, (const xmlChar*)"separator"))
{ {
KatzeXbelItem* item = katze_xbel_separator_new (); KatzeXbelItem* item = katze_xbel_separator_new ();
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = folder; item->parent = folder;
priv->items = g_list_prepend (priv->items, item); folder->items = g_list_prepend (folder->items, item);
} }
cur = cur->next; cur = cur->next;
} }
// Prepending and reversing is faster than appending /* Prepending and reversing is faster than appending */
priv->items = g_list_reverse (priv->items); folder->items = g_list_reverse (folder->items);
return folder; return folder;
} }
// Private: Loads the contents from an xmlNodePtr into a folder. /* Loads the contents from an xmlNodePtr into a folder. */
static gboolean static gboolean
katze_xbel_folder_from_xmlDocPtr (KatzeXbelItem* folder, katze_xbel_folder_from_xmlDocPtr (KatzeXbelItem* folder,
xmlDocPtr doc) xmlDocPtr doc)
@ -208,24 +249,22 @@ katze_xbel_folder_from_xmlDocPtr (KatzeXbelItem* folder,
g_return_val_if_fail (katze_xbel_folder_is_empty (folder), FALSE); g_return_val_if_fail (katze_xbel_folder_is_empty (folder), FALSE);
g_return_val_if_fail (doc, FALSE); g_return_val_if_fail (doc, FALSE);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder);
xmlNodePtr cur = xmlDocGetRootElement (doc); xmlNodePtr cur = xmlDocGetRootElement (doc);
xmlChar* version = xmlGetProp (cur, (xmlChar*)"version"); xmlChar* version = xmlGetProp (cur, (xmlChar*)"version");
if (xmlStrcmp (version, (xmlChar*)"1.0")) if (xmlStrcmp (version, (xmlChar*)"1.0"))
g_warning ("XBEL version is not 1.0."); g_warning ("XBEL version is not 1.0.");
xmlFree (version); xmlFree (version);
katze_assign (priv->title, (gchar*)xmlGetProp (cur, (xmlChar*)"title")); katze_assign (folder->title, (gchar*)xmlGetProp (cur, (xmlChar*)"title"));
katze_assign (priv->desc, (gchar*)xmlGetProp (cur, (xmlChar*)"desc")); katze_assign (folder->desc, (gchar*)xmlGetProp (cur, (xmlChar*)"desc"));
if ((cur = xmlDocGetRootElement (doc)) == NULL) if ((cur = xmlDocGetRootElement (doc)) == NULL)
{ {
// Empty document /* Empty document */
return FALSE; return FALSE;
} }
if (xmlStrcmp (cur->name, (const xmlChar*)"xbel")) if (xmlStrcmp (cur->name, (const xmlChar*)"xbel"))
{ {
// Wrong document kind /* Wrong document kind */
return FALSE; return FALSE;
} }
cur = cur->xmlChildrenNode; cur = cur->xmlChildrenNode;
@ -242,13 +281,13 @@ katze_xbel_folder_from_xmlDocPtr (KatzeXbelItem* folder,
item = katze_xbel_parse_info (xbel, cur);*/ item = katze_xbel_parse_info (xbel, cur);*/
if (item) if (item)
{ {
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = folder; item->parent = folder;
priv->items = g_list_prepend (priv->items, item); folder->items = g_list_prepend (folder->items, item);
} }
cur = cur->next; cur = cur->next;
} }
// Prepending and reversing is faster than appending /* Prepending and reversing is faster than appending */
priv->items = g_list_reverse (priv->items); folder->items = g_list_reverse (folder->items);
return TRUE; return TRUE;
} }
@ -265,10 +304,9 @@ katze_xbel_folder_from_xmlDocPtr (KatzeXbelItem* folder,
void void
katze_xbel_item_ref (KatzeXbelItem* item) katze_xbel_item_ref (KatzeXbelItem* item)
{ {
g_return_if_fail (item); g_return_if_fail (KATZE_IS_XBEL_ITEM (item));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); g_object_ref (item);
priv->refs++;
} }
/** /**
@ -285,36 +323,9 @@ katze_xbel_item_ref (KatzeXbelItem* item)
void void
katze_xbel_item_unref (KatzeXbelItem* item) katze_xbel_item_unref (KatzeXbelItem* item)
{ {
g_return_if_fail (item); g_return_if_fail (KATZE_IS_XBEL_ITEM (item));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); g_object_unref (item);
priv->refs--;
if (priv->refs)
return;
if (priv->parent)
katze_xbel_folder_remove_item (priv->parent, item);
if (priv->kind == KATZE_XBEL_ITEM_KIND_FOLDER)
{
guint n = katze_xbel_folder_get_n_items (item);
guint i;
for (i = 0; i < n; i++)
{
KatzeXbelItem* _item = katze_xbel_folder_get_nth_item (item, i);
KATZE_XBEL_ITEM_GET_PRIVATE (_item)->parent = NULL;
katze_xbel_item_unref (_item);
}
g_list_free (priv->items);
}
if (priv->kind != KATZE_XBEL_ITEM_KIND_SEPARATOR)
{
g_free (priv->title);
g_free (priv->desc);
}
if (priv->kind == KATZE_XBEL_ITEM_KIND_BOOKMARK)
g_free (priv->href);
g_free (item);
} }
/** /**
@ -330,10 +341,9 @@ katze_xbel_item_unref (KatzeXbelItem* item)
KatzeXbelItem* KatzeXbelItem*
katze_xbel_item_copy (KatzeXbelItem* item) katze_xbel_item_copy (KatzeXbelItem* item)
{ {
g_return_val_if_fail (item, NULL); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); KatzeXbelItem* copy = katze_xbel_item_new (item->kind);
KatzeXbelItem* copy = katze_xbel_item_new (priv->kind);
if (katze_xbel_item_is_folder (item)) if (katze_xbel_item_is_folder (item))
{ {
@ -345,25 +355,16 @@ katze_xbel_item_copy (KatzeXbelItem* item)
katze_xbel_folder_append_item (copy, katze_xbel_item_copy (_item)); katze_xbel_folder_append_item (copy, katze_xbel_item_copy (_item));
} }
} }
if (priv->kind != KATZE_XBEL_ITEM_KIND_SEPARATOR) if (item->kind != KATZE_XBEL_ITEM_KIND_SEPARATOR)
{ {
katze_xbel_item_set_title (copy, priv->title); katze_xbel_item_set_title (copy, item->title);
katze_xbel_item_set_desc (copy, priv->desc); katze_xbel_item_set_desc (copy, item->desc);
} }
if (priv->kind == KATZE_XBEL_ITEM_KIND_BOOKMARK) if (item->kind == KATZE_XBEL_ITEM_KIND_BOOKMARK)
katze_xbel_bookmark_set_href (copy, priv->href); katze_xbel_bookmark_set_href (copy, item->href);
return copy; return copy;
} }
GType
katze_xbel_item_get_type (void)
{
static GType type = 0;
if (!type)
type = g_pointer_type_register_static ("katze_xbel_item");
return type;
}
/** /**
* katze_xbel_folder_append_item: * katze_xbel_folder_append_item:
* @folder: a folder * @folder: a folder
@ -381,10 +382,9 @@ katze_xbel_folder_append_item (KatzeXbelItem* folder,
g_return_if_fail (!katze_xbel_item_get_parent (item)); g_return_if_fail (!katze_xbel_item_get_parent (item));
g_return_if_fail (katze_xbel_item_is_folder (folder)); g_return_if_fail (katze_xbel_item_is_folder (folder));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); folder->items = g_list_append (folder->items, item);
priv->items = g_list_append (priv->items, item);
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = folder; item->parent = folder;
} }
/** /**
@ -404,10 +404,9 @@ katze_xbel_folder_prepend_item (KatzeXbelItem* folder,
g_return_if_fail (!katze_xbel_item_get_parent (item)); g_return_if_fail (!katze_xbel_item_get_parent (item));
g_return_if_fail (katze_xbel_item_is_folder (folder)); g_return_if_fail (katze_xbel_item_is_folder (folder));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); folder->items = g_list_prepend (folder->items, item);
priv->items = g_list_prepend (priv->items, item);
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = folder; item->parent = folder;
} }
/** /**
@ -421,14 +420,13 @@ void
katze_xbel_folder_remove_item (KatzeXbelItem* folder, katze_xbel_folder_remove_item (KatzeXbelItem* folder,
KatzeXbelItem* item) KatzeXbelItem* item)
{ {
g_return_if_fail (item); g_return_if_fail (KATZE_IS_XBEL_ITEM (item));
g_return_if_fail (katze_xbel_item_get_parent(folder) != item); g_return_if_fail (katze_xbel_item_get_parent(folder) != item);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); /* Fortunately we know that items are unique */
// Fortunately we know that items are unique folder->items = g_list_remove (folder->items, item);
priv->items = g_list_remove (priv->items, item);
KATZE_XBEL_ITEM_GET_PRIVATE (item)->parent = NULL; item->parent = NULL;
} }
/** /**
@ -444,8 +442,7 @@ katze_xbel_folder_get_n_items (KatzeXbelItem* folder)
{ {
g_return_val_if_fail (katze_xbel_item_is_folder (folder), 0); g_return_val_if_fail (katze_xbel_item_is_folder (folder), 0);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); return g_list_length (folder->items);
return g_list_length (priv->items);
} }
/** /**
@ -463,8 +460,7 @@ katze_xbel_folder_get_nth_item (KatzeXbelItem* folder,
{ {
g_return_val_if_fail (katze_xbel_item_is_folder(folder), NULL); g_return_val_if_fail (katze_xbel_item_is_folder(folder), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); return (KatzeXbelItem*) g_list_nth_data (folder->items, n);
return (KatzeXbelItem*) g_list_nth_data (priv->items, n);
} }
/** /**
@ -498,8 +494,7 @@ katze_xbel_folder_get_folded (KatzeXbelItem* folder)
{ {
g_return_val_if_fail (katze_xbel_item_is_folder (folder), TRUE); g_return_val_if_fail (katze_xbel_item_is_folder (folder), TRUE);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); return folder->folded;
return priv->folded;
} }
/** /**
@ -513,8 +508,9 @@ katze_xbel_folder_get_folded (KatzeXbelItem* folder)
KatzeXbelItemKind KatzeXbelItemKind
katze_xbel_item_get_kind (KatzeXbelItem* item) katze_xbel_item_get_kind (KatzeXbelItem* item)
{ {
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), KATZE_XBEL_ITEM_KIND_BOOKMARK);
return priv->kind;
return item->kind;
} }
/** /**
@ -528,10 +524,9 @@ katze_xbel_item_get_kind (KatzeXbelItem* item)
KatzeXbelItem* KatzeXbelItem*
katze_xbel_item_get_parent (KatzeXbelItem* item) katze_xbel_item_get_parent (KatzeXbelItem* item)
{ {
g_return_val_if_fail (item, NULL); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); return item->parent;
return priv->parent;
} }
/** /**
@ -547,8 +542,7 @@ katze_xbel_item_get_title (KatzeXbelItem* item)
{ {
g_return_val_if_fail (!katze_xbel_item_is_separator (item), NULL); g_return_val_if_fail (!katze_xbel_item_is_separator (item), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); return item->title;
return priv->title;
} }
/** /**
@ -564,8 +558,7 @@ katze_xbel_item_get_desc (KatzeXbelItem* item)
{ {
g_return_val_if_fail (!katze_xbel_item_is_separator (item), NULL); g_return_val_if_fail (!katze_xbel_item_is_separator (item), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); return item->desc;
return priv->desc;
} }
/** /**
@ -581,8 +574,7 @@ katze_xbel_bookmark_get_href (KatzeXbelItem* bookmark)
{ {
g_return_val_if_fail (katze_xbel_item_is_bookmark (bookmark), NULL); g_return_val_if_fail (katze_xbel_item_is_bookmark (bookmark), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (bookmark); return bookmark->href;
return priv->href;
} }
/** /**
@ -596,10 +588,9 @@ katze_xbel_bookmark_get_href (KatzeXbelItem* bookmark)
gboolean gboolean
katze_xbel_item_is_bookmark (KatzeXbelItem* item) katze_xbel_item_is_bookmark (KatzeXbelItem* item)
{ {
g_return_val_if_fail (item, FALSE); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), FALSE);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); return item->kind == KATZE_XBEL_ITEM_KIND_BOOKMARK;
return priv->kind == KATZE_XBEL_ITEM_KIND_BOOKMARK;
} }
/** /**
@ -612,10 +603,9 @@ katze_xbel_item_is_bookmark (KatzeXbelItem* item)
**/ **/
gboolean katze_xbel_item_is_separator (KatzeXbelItem* item) gboolean katze_xbel_item_is_separator (KatzeXbelItem* item)
{ {
g_return_val_if_fail (item, FALSE); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), FALSE);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); return item->kind == KATZE_XBEL_ITEM_KIND_SEPARATOR;
return priv->kind == KATZE_XBEL_ITEM_KIND_SEPARATOR;
} }
/** /**
@ -629,10 +619,9 @@ gboolean katze_xbel_item_is_separator (KatzeXbelItem* item)
gboolean gboolean
katze_xbel_item_is_folder (KatzeXbelItem* item) katze_xbel_item_is_folder (KatzeXbelItem* item)
{ {
g_return_val_if_fail (item, FALSE); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), FALSE);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); return item->kind == KATZE_XBEL_ITEM_KIND_FOLDER;
return priv->kind == KATZE_XBEL_ITEM_KIND_FOLDER;
} }
/** /**
@ -648,8 +637,7 @@ katze_xbel_folder_set_folded (KatzeXbelItem* folder,
{ {
g_return_if_fail (katze_xbel_item_is_folder (folder)); g_return_if_fail (katze_xbel_item_is_folder (folder));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder); folder->folded = folded;
priv->folded = folded;
} }
/** /**
@ -665,8 +653,7 @@ katze_xbel_item_set_title (KatzeXbelItem* item,
{ {
g_return_if_fail (!katze_xbel_item_is_separator (item)); g_return_if_fail (!katze_xbel_item_is_separator (item));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); katze_assign (item->title, g_strdup (title));
katze_assign (priv->title, g_strdup (title));
} }
/** /**
@ -682,8 +669,7 @@ katze_xbel_item_set_desc (KatzeXbelItem* item,
{ {
g_return_if_fail (!katze_xbel_item_is_separator (item)); g_return_if_fail (!katze_xbel_item_is_separator (item));
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item); katze_assign (item->desc, g_strdup (desc));
katze_assign (priv->desc, g_strdup (desc));
} }
/** /**
@ -704,8 +690,7 @@ katze_xbel_bookmark_set_href (KatzeXbelItem* bookmark,
g_return_if_fail (katze_xbel_item_is_bookmark (bookmark)); g_return_if_fail (katze_xbel_item_is_bookmark (bookmark));
g_return_if_fail (href); g_return_if_fail (href);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (bookmark); katze_assign (bookmark->href, g_strdup (href));
katze_assign (priv->href, g_strdup (href));
} }
gboolean gboolean
@ -718,14 +703,14 @@ katze_xbel_folder_from_data (KatzeXbelItem* folder,
xmlDocPtr doc; xmlDocPtr doc;
if((doc = xmlParseMemory (data, strlen (data))) == NULL) if((doc = xmlParseMemory (data, strlen (data))) == NULL)
{ {
// No valid xml or broken encoding /* No valid xml or broken encoding */
*error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ, *error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ,
_("Malformed document.")); _("Malformed document."));
return FALSE; return FALSE;
} }
if (!katze_xbel_folder_from_xmlDocPtr (folder, doc)) if (!katze_xbel_folder_from_xmlDocPtr (folder, doc))
{ {
// Parsing failed /* Parsing failed */
xmlFreeDoc(doc); xmlFreeDoc(doc);
*error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ, *error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ,
_("Malformed document.")); _("Malformed document."));
@ -754,7 +739,7 @@ katze_xbel_folder_from_file (KatzeXbelItem* folder,
g_return_val_if_fail (file, FALSE); g_return_val_if_fail (file, FALSE);
if (!g_file_test (file, G_FILE_TEST_EXISTS)) if (!g_file_test (file, G_FILE_TEST_EXISTS))
{ {
// File doesn't exist /* File doesn't exist */
*error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT, *error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT,
_("File not found.")); _("File not found."));
return FALSE; return FALSE;
@ -762,14 +747,14 @@ katze_xbel_folder_from_file (KatzeXbelItem* folder,
xmlDocPtr doc; xmlDocPtr doc;
if ((doc = xmlParseFile (file)) == NULL) if ((doc = xmlParseFile (file)) == NULL)
{ {
// No valid xml or broken encoding /* No valid xml or broken encoding */
*error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ, *error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ,
_("Malformed document.")); _("Malformed document."));
return FALSE; return FALSE;
} }
if (!katze_xbel_folder_from_xmlDocPtr (folder, doc)) if (!katze_xbel_folder_from_xmlDocPtr (folder, doc))
{ {
// Parsing failed /* Parsing failed */
xmlFreeDoc (doc); xmlFreeDoc (doc);
*error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ, *error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ,
_("Malformed document.")); _("Malformed document."));
@ -798,7 +783,7 @@ katze_xbel_folder_from_data_dirs (KatzeXbelItem* folder,
{ {
g_return_val_if_fail (katze_xbel_folder_is_empty (folder), FALSE); g_return_val_if_fail (katze_xbel_folder_is_empty (folder), FALSE);
g_return_val_if_fail (file, FALSE); g_return_val_if_fail (file, FALSE);
// FIXME: Essentially unimplemented /* FIXME: Essentially unimplemented */
*error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ, *error = g_error_new_literal (KATZE_XBEL_ERROR, KATZE_XBEL_ERROR_READ,
_("Malformed document.")); _("Malformed document."));
@ -821,12 +806,10 @@ katze_xbel_xml_element (const gchar* name,
static gchar* static gchar*
katze_xbel_item_to_data (KatzeXbelItem* item) katze_xbel_item_to_data (KatzeXbelItem* item)
{ {
g_return_val_if_fail (item, NULL); g_return_val_if_fail (KATZE_IS_XBEL_ITEM (item), NULL);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (item);
gchar* markup = NULL; gchar* markup = NULL;
switch (priv->kind) switch (item->kind)
{ {
case KATZE_XBEL_ITEM_KIND_FOLDER: case KATZE_XBEL_ITEM_KIND_FOLDER:
{ {
@ -840,9 +823,9 @@ katze_xbel_item_to_data (KatzeXbelItem* item)
g_string_append (_markup, item_markup); g_string_append (_markup, item_markup);
g_free (item_markup); g_free (item_markup);
} }
gchar* folded = priv->folded ? NULL : g_strdup_printf (" folded=\"no\""); gchar* folded = item->folded ? NULL : g_strdup_printf (" folded=\"no\"");
gchar* title = katze_xbel_xml_element ("title", priv->title); gchar* title = katze_xbel_xml_element ("title", item->title);
gchar* desc = katze_xbel_xml_element ("desc", priv->desc); gchar* desc = katze_xbel_xml_element ("desc", item->desc);
markup = g_strdup_printf ("<folder%s>\n%s%s%s</folder>\n", markup = g_strdup_printf ("<folder%s>\n%s%s%s</folder>\n",
folded ? folded : "", folded ? folded : "",
title, desc, title, desc,
@ -854,11 +837,11 @@ katze_xbel_item_to_data (KatzeXbelItem* item)
} }
case KATZE_XBEL_ITEM_KIND_BOOKMARK: case KATZE_XBEL_ITEM_KIND_BOOKMARK:
{ {
gchar* href_escaped = g_markup_escape_text (priv->href, -1); gchar* href_escaped = g_markup_escape_text (item->href, -1);
gchar* href = g_strdup_printf (" href=\"%s\"", href_escaped); gchar* href = g_strdup_printf (" href=\"%s\"", href_escaped);
g_free (href_escaped); g_free (href_escaped);
gchar* title = katze_xbel_xml_element ("title", priv->title); gchar* title = katze_xbel_xml_element ("title", item->title);
gchar* desc = katze_xbel_xml_element ("desc", priv->desc); gchar* desc = katze_xbel_xml_element ("desc", item->desc);
markup = g_strdup_printf ("<bookmark%s>\n%s%s%s</bookmark>\n", markup = g_strdup_printf ("<bookmark%s>\n%s%s%s</bookmark>\n",
href, href,
title, desc, title, desc,
@ -894,8 +877,6 @@ katze_xbel_folder_to_data (KatzeXbelItem* folder,
{ {
g_return_val_if_fail (katze_xbel_item_is_folder (folder), FALSE); g_return_val_if_fail (katze_xbel_item_is_folder (folder), FALSE);
KatzeXbelItemPrivate* priv = KATZE_XBEL_ITEM_GET_PRIVATE (folder);
GString* inner_markup = g_string_new (NULL); GString* inner_markup = g_string_new (NULL);
guint n = katze_xbel_folder_get_n_items (folder); guint n = katze_xbel_folder_get_n_items (folder);
guint i; guint i;
@ -906,8 +887,8 @@ katze_xbel_folder_to_data (KatzeXbelItem* folder,
g_string_append (inner_markup, sItem); g_string_append (inner_markup, sItem);
g_free (sItem); g_free (sItem);
} }
gchar* title = katze_xbel_xml_element ("title", priv->title); gchar* title = katze_xbel_xml_element ("title", folder->title);
gchar* desc = katze_xbel_xml_element ("desc", priv->desc); gchar* desc = katze_xbel_xml_element ("desc", folder->desc);
gchar* outer_markup; gchar* outer_markup;
outer_markup = g_strdup_printf ( outer_markup = g_strdup_printf (
"%s%s<xbel version=\"1.0\">\n%s%s%s</xbel>\n", "%s%s<xbel version=\"1.0\">\n%s%s%s</xbel>\n",
@ -941,7 +922,9 @@ katze_xbel_folder_to_file (KatzeXbelItem* folder,
const gchar* file, const gchar* file,
GError** error) GError** error)
{ {
g_return_val_if_fail (KATZE_IS_XBEL_ITEM (folder), FALSE);
g_return_val_if_fail (file, FALSE); g_return_val_if_fail (file, FALSE);
gchar* data; gchar* data;
if (!(data = katze_xbel_folder_to_data (folder, NULL, error))) if (!(data = katze_xbel_folder_to_data (folder, NULL, error)))
return FALSE; return FALSE;

View file

@ -18,19 +18,17 @@ G_BEGIN_DECLS
#define KATZE_TYPE_XBEL_ITEM \ #define KATZE_TYPE_XBEL_ITEM \
(katze_xbel_item_get_type ()) (katze_xbel_item_get_type ())
#define KATZE_XBEL_ITEM(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), KATZE_TYPE_XBEL_ITEM, KatzeXbelItem))
#define KATZE_IS_XBEL_ITEM(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), KATZE_TYPE_XBEL_ITEM))
typedef struct _KatzeXbelItem KatzeXbelItem; typedef struct _KatzeXbelItem KatzeXbelItem;
typedef struct _KatzeXbelItemPrivate KatzeXbelItemPrivate; typedef struct _KatzeXbelItemClass KatzeXbelItemClass;
struct _KatzeXbelItem struct _KatzeXbelItemClass
{ {
KatzeXbelItemPrivate* priv; GObjectClass parent_class;
/* Padding for future expansion */
void (*_katze_reserved1) (void);
void (*_katze_reserved2) (void);
void (*_katze_reserved3) (void);
void (*_katze_reserved4) (void);
}; };
#define KATZE_XBEL_ERROR g_quark_from_string("KATZE_XBEL_ERROR") #define KATZE_XBEL_ERROR g_quark_from_string("KATZE_XBEL_ERROR")