5e01b1903c135503bcefe24bf02b19424b5eed6f
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"
20 /**
21 \brief Checks if the path exists in the preference file
22 */
23 bool pref_path_exists(gchar const *path){
24 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
25 return (repr != NULL);
26 }
27 void
28 prefs_set_int_attribute(gchar const *path, gchar const *attr, long long int value)
29 {
30 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
31 if (repr) {
32 sp_repr_set_int(repr, attr, value);
33 }
34 }
36 long long int
37 prefs_get_int_attribute(gchar const *path, gchar const *attr, long long int def)
38 {
39 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
40 if (repr) {
41 return sp_repr_get_int_attribute(repr, attr, def);
42 } else {
43 return def;
44 }
45 }
47 /**
48 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
49 */
50 long long int
51 prefs_get_int_attribute_limited(gchar const *path, gchar const *attr, long long int def, long long int min, long long int max)
52 {
53 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
54 if (repr) {
55 long long int const v = sp_repr_get_int_attribute(repr, attr, def);
56 if (v >= min && v <= max) {
57 return v;
58 } else {
59 return def;
60 }
61 } else {
62 return def;
63 }
64 }
66 void
67 prefs_set_double_attribute(gchar const *path, gchar const *attr, double value)
68 {
69 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
70 if (repr) {
71 sp_repr_set_svg_double(repr, attr, value);
72 }
73 }
75 double
76 prefs_get_double_attribute(gchar const *path, gchar const *attr, double def)
77 {
78 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
79 if (repr) {
80 return sp_repr_get_double_attribute(repr, attr, def);
81 } else {
82 return def;
83 }
84 }
86 /**
87 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
88 */
89 double
90 prefs_get_double_attribute_limited(gchar const *path, gchar const *attr, double def, double min, double max)
91 {
92 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
93 if (repr) {
94 double const v = sp_repr_get_double_attribute(repr, attr, def);
95 if (v >= min && v <= max) {
96 return v;
97 } else {
98 return def;
99 }
100 } else {
101 return def;
102 }
103 }
105 gchar const *
106 prefs_get_string_attribute(gchar const *path, gchar const *attr)
107 {
108 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
109 if (repr) {
110 return (char *) repr->attribute(attr);
111 }
112 return NULL;
113 }
115 void
116 prefs_set_string_attribute(gchar const *path, gchar const *attr, gchar const *value)
117 {
118 Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
119 if (repr) {
120 repr->setAttribute(attr, value);
121 }
122 }
124 void
125 prefs_set_recent_file(gchar const *uri, gchar const *name)
126 {
127 unsigned const max_documents = prefs_get_int_attribute("options.maxrecentdocuments", "value", 20);
129 if (uri != NULL) {
130 Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
131 if (recent) {
132 // remove excess recent files
133 if (recent->childCount() >= max_documents) {
134 Inkscape::XML::Node *child = recent->firstChild();
135 // count to the last
136 for (unsigned i = 0; child && i + 1 < max_documents; ++i) {
137 child = child->next();
138 }
139 // remove all after the last
140 while (child) {
141 Inkscape::XML::Node *next = child->next();
142 sp_repr_unparent(child);
143 child = next;
144 }
145 }
147 if (max_documents > 0) {
148 Inkscape::XML::Node *child = sp_repr_lookup_child(recent, "uri", uri);
149 if (child) {
150 recent->changeOrder(child, NULL);
151 } else {
152 child = recent->document()->createElement("document");
153 child->setAttribute("uri", uri);
154 recent->addChild(child, NULL);
155 }
156 child->setAttribute("name", name);
157 }
158 }
159 }
160 }
162 gchar const **
163 prefs_get_recent_files()
164 {
165 Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
166 if (recent) {
167 unsigned const docs = recent->childCount();
168 gchar const **datalst = (gchar const **) g_malloc(sizeof(gchar *) * ((docs * 2) + 1));
170 gint i;
171 Inkscape::XML::Node *child;
172 for (i = 0, child = recent->firstChild();
173 child != NULL;
174 child = child->next(), i += 2)
175 {
176 gchar const *uri = child->attribute("uri");
177 gchar const *name = child->attribute("name");
178 datalst[i] = uri;
179 datalst[i + 1] = name;
180 }
182 datalst[i] = NULL;
183 return datalst;
184 }
186 return NULL;
187 }
189 /*
190 Local Variables:
191 mode:c++
192 c-file-style:"stroustrup"
193 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
194 indent-tabs-mode:nil
195 fill-column:99
196 End:
197 */
198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :