1 /*
2 * Utility functions for reading and setting preferences
3 *
4 * Authors:
5 * bulia byak <bulia@dr.com>
6 *
7 * Copyright (C) 2003 authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
17 #include "inkscape.h"
18 #include "xml/repr.h"
21 void
22 prefs_set_int_attribute(gchar const *path, gchar const *attr, long long int value)
23 {
24 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
25 if (repr) {
26 sp_repr_set_int(repr, attr, value);
27 }
28 }
30 long long int
31 prefs_get_int_attribute(gchar const *path, gchar const *attr, long long int def)
32 {
33 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
34 if (repr) {
35 return sp_repr_get_int_attribute(repr, attr, def);
36 } else {
37 return def;
38 }
39 }
41 /**
42 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
43 */
44 long long int
45 prefs_get_int_attribute_limited(gchar const *path, gchar const *attr, long long int def, long long int min, long long int max)
46 {
47 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
48 if (repr) {
49 long long int const v = sp_repr_get_int_attribute(repr, attr, def);
50 if (v >= min && v <= max) {
51 return v;
52 } else {
53 return def;
54 }
55 } else {
56 return def;
57 }
58 }
60 void
61 prefs_set_double_attribute(gchar const *path, gchar const *attr, double value)
62 {
63 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
64 if (repr) {
65 sp_repr_set_svg_double(repr, attr, value);
66 }
67 }
69 double
70 prefs_get_double_attribute(gchar const *path, gchar const *attr, double def)
71 {
72 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
73 if (repr) {
74 return sp_repr_get_double_attribute(repr, attr, def);
75 } else {
76 return def;
77 }
78 }
80 /**
81 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
82 */
83 double
84 prefs_get_double_attribute_limited(gchar const *path, gchar const *attr, double def, double min, double max)
85 {
86 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
87 if (repr) {
88 double const v = sp_repr_get_double_attribute(repr, attr, def);
89 if (v >= min && v <= max) {
90 return v;
91 } else {
92 return def;
93 }
94 } else {
95 return def;
96 }
97 }
99 gchar const *
100 prefs_get_string_attribute(gchar const *path, gchar const *attr)
101 {
102 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
103 if (repr) {
104 return (char *) repr->attribute(attr);
105 }
106 return NULL;
107 }
109 void
110 prefs_set_string_attribute(gchar const *path, gchar const *attr, gchar const *value)
111 {
112 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
113 if (repr) {
114 repr->setAttribute(attr, value);
115 }
116 }
118 void
119 prefs_set_recent_file(gchar const *uri, gchar const *name)
120 {
121 unsigned const max_documents = prefs_get_int_attribute("options.maxrecentdocuments", "value", 20);
123 if (uri != NULL) {
124 Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
125 if (recent) {
126 // remove excess recent files
127 if (recent->childCount() >= max_documents) {
128 Inkscape::XML::Node *child = recent->firstChild();
129 // count to the last
130 for (unsigned i = 0; child && i + 1 < max_documents; ++i) {
131 child = child->next();
132 }
133 // remove all after the last
134 while (child) {
135 Inkscape::XML::Node *next = child->next();
136 sp_repr_unparent(child);
137 child = next;
138 }
139 }
141 if (max_documents > 0) {
142 Inkscape::XML::Node *child = sp_repr_lookup_child(recent, "uri", uri);
143 if (child) {
144 recent->changeOrder(child, NULL);
145 } else {
146 child = sp_repr_new("document");
147 child->setAttribute("uri", uri);
148 recent->addChild(child, NULL);
149 }
150 child->setAttribute("name", name);
151 }
152 }
153 }
154 }
156 gchar const **
157 prefs_get_recent_files()
158 {
159 Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
160 if (recent) {
161 unsigned const docs = recent->childCount();
162 gchar const **datalst = (gchar const **) g_malloc(sizeof(gchar *) * ((docs * 2) + 1));
164 gint i;
165 Inkscape::XML::Node *child;
166 for (i = 0, child = recent->firstChild();
167 child != NULL;
168 child = child->next(), i += 2)
169 {
170 gchar const *uri = child->attribute("uri");
171 gchar const *name = child->attribute("name");
172 datalst[i] = uri;
173 datalst[i + 1] = name;
174 }
176 datalst[i] = NULL;
177 return datalst;
178 }
180 return NULL;
181 }
183 /*
184 Local Variables:
185 mode:c++
186 c-file-style:"stroustrup"
187 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
188 indent-tabs-mode:nil
189 fill-column:99
190 End:
191 */
192 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :