Code

Merging from trunk
[inkscape.git] / src / prefs-utils.cpp
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 "prefs-utils.h"
18 #include "inkscape.h"
19 #include "xml/repr.h"
22 void
23 prefs_set_recent_file(gchar const *uri, gchar const *name)
24 {
25     unsigned const max_documents = prefs_get_int_attribute("options.maxrecentdocuments", "value", 20);
27     if (uri != NULL) {
28         Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
29         if (recent) {
30             // remove excess recent files
31             if (recent->childCount() >= max_documents) {
32                 Inkscape::XML::Node *child = recent->firstChild();
33                 // count to the last
34                 for (unsigned i = 0; child && i + 1 < max_documents; ++i) {
35                     child = child->next();
36                 }
37                 // remove all after the last
38                 while (child) {
39                     Inkscape::XML::Node *next = child->next();
40                     sp_repr_unparent(child);
41                     child = next;
42                 }
43             }
45             if (max_documents > 0) {
46                 Inkscape::XML::Node *child = sp_repr_lookup_child(recent, "uri", uri);
47                 if (child) {
48                     recent->changeOrder(child, NULL);
49                 } else {
50                     child = recent->document()->createElement("document");
51                     child->setAttribute("uri", uri);
52                     recent->addChild(child, NULL);
53                 }
54                 child->setAttribute("name", name);
55             }
56         }
57     }
58 }
60 gchar const **
61 prefs_get_recent_files()
62 {
63     Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
64     if (recent) {
65         unsigned const docs = recent->childCount();
66         gchar const **datalst = (gchar const **) g_malloc(sizeof(gchar *) * ((docs * 2) + 1));
68         gint i;
69         Inkscape::XML::Node *child;
70         for (i = 0, child = recent->firstChild();
71              child != NULL;
72              child = child->next(), i += 2)
73         {
74             gchar const *uri = child->attribute("uri");
75             gchar const *name = child->attribute("name");
76             datalst[i]     = uri;
77             datalst[i + 1] = name;
78         }
80         datalst[i] = NULL;
81         return datalst;
82     }
84     return NULL;
85 }
87 /*
88   Local Variables:
89   mode:c++
90   c-file-style:"stroustrup"
91   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
92   indent-tabs-mode:nil
93   fill-column:99
94   End:
95 */
96 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :