Code

rewrote the document list as a (SPDocument,use_count) map, incrementing use_count...
[inkscape.git] / src / prefs-utils.cpp
index 54c83c1e9580805d1c37ab2d0f20d9bea031273f..004626ac68b5c0b2028b2e3aad0383b610c6bc1f 100644 (file)
 # include "config.h"
 #endif
 
+#include "prefs-utils.h"
 #include "inkscape.h"
 #include "xml/repr.h"
 
 
-void
-prefs_set_int_attribute(gchar const *path, gchar const *attr, gint value)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        sp_repr_set_int(repr, attr, value);
-    }
-}
-
-gint
-prefs_get_int_attribute(gchar const *path, gchar const *attr, gint def)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        return sp_repr_get_int_attribute(repr, attr, def);
-    } else {
-        return 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)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        gint const v = sp_repr_get_int_attribute(repr, attr, def);
-        if (v >= min && v <= max) {
-            return v;
-        } else {
-            return def;
-        }
-    } else {
-        return def;
-    }
-}
-
-void
-prefs_set_double_attribute(gchar const *path, gchar const *attr, double value)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        sp_repr_set_svg_double(repr, attr, value);
-    }
-}
-
-double
-prefs_get_double_attribute(gchar const *path, gchar const *attr, double def)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        return sp_repr_get_double_attribute(repr, attr, def);
-    } else {
-        return def;
-    }
-}
-
-/**
-\brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
-*/
-double
-prefs_get_double_attribute_limited(gchar const *path, gchar const *attr, double def, double min, double max)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        double const v = sp_repr_get_double_attribute(repr, attr, def);
-        if (v >= min && v <= max) {
-            return v;
-        } else {
-            return def;
-        }
-    } else {
-        return def;
-    }
-}
-
-gchar const *
-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 NULL;
-}
-
-void
-prefs_set_string_attribute(gchar const *path, gchar const *attr, gchar const *value)
-{
-    Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
-    if (repr) {
-        repr->setAttribute(attr, value);
-    }
-}
-
 void
 prefs_set_recent_file(gchar const *uri, gchar const *name)
 {
@@ -123,26 +27,32 @@ prefs_set_recent_file(gchar const *uri, gchar const *name)
     if (uri != NULL) {
         Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
         if (recent) {
-            Inkscape::XML::Node *child = sp_repr_lookup_child(recent, "uri", uri);
-            if (child) {
-                recent->changeOrder(child, NULL);
-            } else {
-                if (recent->childCount() >= max_documents) {
-                    child = recent->firstChild();
-                    // count to the last
-                    for (unsigned i = 0; i + 2 < max_documents; ++i) {
-                        child = child->next();
-                    }
-                    // remove all after the last
-                    while (child->next()) {
-                        sp_repr_unparent(child->next());
-                    }
+            // remove excess recent files
+            if (recent->childCount() >= max_documents) {
+                Inkscape::XML::Node *child = recent->firstChild();
+                // count to the last
+                for (unsigned i = 0; child && i + 1 < max_documents; ++i) {
+                    child = child->next();
+                }
+                // remove all after the last
+                while (child) {
+                    Inkscape::XML::Node *next = child->next();
+                    sp_repr_unparent(child);
+                    child = next;
+                }
+            }
+
+            if (max_documents > 0) {
+                Inkscape::XML::Node *child = sp_repr_lookup_child(recent, "uri", uri);
+                if (child) {
+                    recent->changeOrder(child, NULL);
+                } else {
+                    child = recent->document()->createElement("document");
+                    child->setAttribute("uri", uri);
+                    recent->addChild(child, NULL);
                 }
-                child = sp_repr_new("document");
-                child->setAttribute("uri", uri);
-                recent->addChild(child, NULL);
+                child->setAttribute("name", name);
             }
-            child->setAttribute("name", name);
         }
     }
 }