Code

fix leak of the arena and arenaitem
[inkscape.git] / src / prefs-utils.cpp
index a0851a3543693ffbe22d83c8aa653384192b29c7..7a950c3c3c60a54bff5dfbe76e1aa95301c5281e 100644 (file)
 #include "inkscape.h"
 #include "xml/repr.h"
 
+/**
+\brief Checks if the path exists in the preference file
+*/
+bool pref_path_exists(gchar const *path){
+    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
+    return (repr != NULL);
+}
+
+/**
+\brief returns the number of sub-prefs
+*/
+unsigned int pref_path_number_of_children(gchar const *path){
+    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
+    Inkscape::XML::Node *child_repr = sp_repr_children(repr);
+    int nb_child = 0;
+    while (child_repr) {
+        nb_child ++;
+        child_repr = sp_repr_next(child_repr);
+    }
+    return nb_child;
+}
+
+/**
+\brief creates a new preference and returns its key on success.
+*/
+gchar * create_pref(gchar const *father_path, gchar const *child){
+    Inkscape::XML::Node *father = inkscape_get_repr(INKSCAPE, father_path);
+    if (! father ) return NULL;
+    Inkscape::XML::Node *repr = father->document()->createElement("group");
+    repr->setAttribute("id", child, false);
+    father->appendChild(repr);
+    return g_strdup_printf("%s.%s", father_path,child);
+}
+
+/**
+\brief gets the nth children of a pref, starting from one (first child <=> n=1). returns NULL if out of bounds or father does not exist. Please free all that stuff after use.
+*/
+gchar  *get_pref_nth_child(gchar const *father_path, unsigned int n){
+    if (n <= 0) return NULL;
+    Inkscape::XML::Node *father = inkscape_get_repr(INKSCAPE, father_path);
+    if (! father ) return NULL;
+    Inkscape::XML::Node *child_repr = sp_repr_children(father);  
+    unsigned int index = 0;
+    while (child_repr && (++index < n)) {
+        child_repr = sp_repr_next(child_repr);
+    }
+    if (child_repr) return g_strdup_printf("%s.%s",father_path,child_repr->attribute("id"));
+    return NULL;
+}
 
 void
-prefs_set_int_attribute(gchar const *path, gchar const *attr, gint value)
+prefs_set_int_attribute(gchar const *path, gchar const *attr, long long int value)
 {
     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
     if (repr) {
@@ -27,8 +76,8 @@ prefs_set_int_attribute(gchar const *path, gchar const *attr, gint value)
     }
 }
 
-gint
-prefs_get_int_attribute(gchar const *path, gchar const *attr, gint def)
+long long int
+prefs_get_int_attribute(gchar const *path, gchar const *attr, long long int def)
 {
     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
     if (repr) {
@@ -41,12 +90,12 @@ prefs_get_int_attribute(gchar const *path, gchar const *attr, gint def)
 /**
 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
 */
-gint
-prefs_get_int_attribute_limited(gchar const *path, gchar const *attr, gint def, gint min, gint max)
+long long int
+prefs_get_int_attribute_limited(gchar const *path, gchar const *attr, long long int def, long long int min, long long int max)
 {
     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
     if (repr) {
-        gint const v = sp_repr_get_int_attribute(repr, attr, def);
+        long long int const v = sp_repr_get_int_attribute(repr, attr, def);
         if (v >= min && v <= max) {
             return v;
         } else {
@@ -101,7 +150,7 @@ prefs_get_string_attribute(gchar const *path, gchar const *attr)
 {
     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
     if (repr) {
-        return (char *) repr->attribute(attr);
+        return repr->attribute(attr);
     }
     return NULL;
 }
@@ -143,7 +192,7 @@ prefs_set_recent_file(gchar const *uri, gchar const *name)
                 if (child) {
                     recent->changeOrder(child, NULL);
                 } else {
-                    child = sp_repr_new("document");
+                    child = recent->document()->createElement("document");
                     child->setAttribute("uri", uri);
                     recent->addChild(child, NULL);
                 }