1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* Utility functions for reading and setting preferences
*
* Authors:
* bulia byak <bulia@dr.com>
*
* Copyright (C) 2003 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#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");
sp_repr_set_attr(repr, "id", child);
father->appendChild(repr);
return g_strdup_printf("%s.%s", father_path,child);
}
/**
\brief gets the list of children from a pref. Please free all that stuff after use.
*/
bool get_pref_children(gchar const *father_path, GSList ** children){
Inkscape::XML::Node *father = inkscape_get_repr(INKSCAPE, father_path);
if (! father ) return false;
Inkscape::XML::Node *child_repr = sp_repr_children(father);
while (child_repr) {
*children = g_slist_prepend(*children, g_strdup_printf("%s.%s",father_path,child_repr->attribute("id")));
child_repr = sp_repr_next(child_repr);
}
}
/**
\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, long long int value)
{
Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
if (repr) {
sp_repr_set_int(repr, attr, value);
}
}
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) {
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
*/
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) {
long long int 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)
{
unsigned const max_documents = prefs_get_int_attribute("options.maxrecentdocuments", "value", 20);
if (uri != NULL) {
Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
if (recent) {
// 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->setAttribute("name", name);
}
}
}
}
gchar const **
prefs_get_recent_files()
{
Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
if (recent) {
unsigned const docs = recent->childCount();
gchar const **datalst = (gchar const **) g_malloc(sizeof(gchar *) * ((docs * 2) + 1));
gint i;
Inkscape::XML::Node *child;
for (i = 0, child = recent->firstChild();
child != NULL;
child = child->next(), i += 2)
{
gchar const *uri = child->attribute("uri");
gchar const *name = child->attribute("name");
datalst[i] = uri;
datalst[i + 1] = name;
}
datalst[i] = NULL;
return datalst;
}
return NULL;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|