summaryrefslogtreecommitdiffstats
path: root/src/prefs-utils.cpp
blob: 004626ac68b5c0b2028b2e3aad0383b610c6bc1f (plain)
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
/*
 * 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 "prefs-utils.h"
#include "inkscape.h"
#include "xml/repr.h"


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 :