Skip to content

Commit

Permalink
toolbar-editor: Add methods to load from GResource
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Persch authored and raveit65 committed Mar 22, 2018
1 parent 6b84d6f commit e0f26b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
60 changes: 60 additions & 0 deletions cut-n-paste/toolbar-editor/egg-toolbars-model.c
Expand Up @@ -607,6 +607,36 @@ egg_toolbars_model_load_toolbars (EggToolbarsModel *model,
return TRUE;
}

gboolean
egg_toolbars_model_load_toolbars_from_resource (EggToolbarsModel *model,
const char *path)
{
xmlDocPtr doc;
xmlNodePtr root;
GBytes *bytes;
GError *error = NULL;
const guint8 *data;
gsize data_len;

g_return_val_if_fail (EGG_IS_TOOLBARS_MODEL (model), FALSE);

bytes = g_resources_lookup_data (path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
g_assert_no_error (error);

data = g_bytes_get_data (bytes, &data_len);
doc = xmlParseMemory ((const char *) data, data_len);
if (doc == NULL)
g_error ("Failed to load XML data from resource %s", path);

root = xmlDocGetRootElement (doc);
parse_toolbars (model, root->children);

xmlFreeDoc (doc);
g_bytes_unref (bytes);

return TRUE;
}

static void
parse_available_list (EggToolbarsModel *model,
xmlNodePtr child)
Expand Down Expand Up @@ -671,6 +701,36 @@ egg_toolbars_model_load_names (EggToolbarsModel *model,
return TRUE;
}

gboolean
egg_toolbars_model_load_names_from_resource (EggToolbarsModel *model,
const char *path)
{
xmlDocPtr doc;
xmlNodePtr root;
GBytes *bytes;
GError *error = NULL;
const guint8 *data;
gsize data_len;

g_return_val_if_fail (EGG_IS_TOOLBARS_MODEL (model), FALSE);

bytes = g_resources_lookup_data (path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
g_assert_no_error (error);

data = g_bytes_get_data (bytes, &data_len);
doc = xmlParseMemory ((const char *) data, data_len);
if (doc == NULL)
g_error ("Failed to load XML data from resource %s", path);

root = xmlDocGetRootElement (doc);
parse_names (model, root->children);

xmlFreeDoc (doc);
g_bytes_unref (bytes);

return TRUE;
}

static void
egg_toolbars_model_class_init (EggToolbarsModelClass *klass)
{
Expand Down
13 changes: 9 additions & 4 deletions cut-n-paste/toolbar-editor/egg-toolbars-model.h
Expand Up @@ -23,6 +23,7 @@

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <gdk/gdk.h>

G_BEGIN_DECLS
Expand Down Expand Up @@ -114,12 +115,16 @@ GType egg_tb_model_flags_get_type (void);
GType egg_toolbars_model_get_type (void);
EggToolbarsModel *egg_toolbars_model_new (void);
gboolean egg_toolbars_model_load_names (EggToolbarsModel *model,
const char *xml_file);
const char *xml_file);
gboolean egg_toolbars_model_load_toolbars (EggToolbarsModel *model,
const char *xml_file);
const char *xml_file);
void egg_toolbars_model_save_toolbars (EggToolbarsModel *model,
const char *xml_file,
const char *version);
const char *xml_file,
const char *version);
gboolean egg_toolbars_model_load_names_from_resource (EggToolbarsModel *model,
const char *path);
gboolean egg_toolbars_model_load_toolbars_from_resource (EggToolbarsModel *model,
const char *path);

/* Functions for manipulating the types of portable data this toolbar understands. */
GList * egg_toolbars_model_get_types (EggToolbarsModel *model);
Expand Down

0 comments on commit e0f26b9

Please sign in to comment.