~ubuntu-branches/ubuntu/trusty/xfce4-panel/trusty-proposed

« back to all changes in this revision

Viewing changes to panel/settings.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Huggins
  • Date: 2004-06-08 10:44:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040608104421-b0b77kis8o0uoi6q
Tags: upstream-4.0.5
ImportĀ upstreamĀ versionĀ 4.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  settings.c
 
2
 *  
 
3
 *  Copyright (C) 2002 Jasper Huijsmans (huysmans@users.sourceforge.net)
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include <config.h>
 
22
#endif
 
23
 
 
24
#ifdef HAVE_SYS_STAT_H
 
25
#include <sys/stat.h>
 
26
#endif
 
27
 
 
28
#include <stdio.h>
 
29
#ifdef HAVE_STDLIB_H
 
30
#include <stdlib.h>
 
31
#endif
 
32
 
 
33
#include <libxml/parser.h>
 
34
#include <libxml/tree.h>
 
35
 
 
36
#include "xfce.h"
 
37
#include "settings.h"
 
38
#include "groups.h"
 
39
#include "popup.h"
 
40
#include "item.h"
 
41
 
 
42
#define ROOT    "Xfce"
 
43
#define NS      "http://www.xfce.org/xfce4/panel/1.0"
 
44
 
 
45
gboolean disable_user_config = FALSE;
 
46
 
 
47
xmlDocPtr xmlconfig = NULL;
 
48
 
 
49
/*  Configuration
 
50
 *  -------------
 
51
*/
 
52
static gboolean
 
53
check_disable_user_config (void)
 
54
{
 
55
    const char *var = g_getenv ("XFCE_DISABLE_USER_CONFIG");
 
56
 
 
57
    return (var && !strequal (var, "0"));
 
58
}
 
59
 
 
60
/*  Reading xml
 
61
 *  -----------
 
62
*/
 
63
static xmlDocPtr
 
64
read_xml_file (void)
 
65
{
 
66
    char *rcfile;
 
67
    xmlDocPtr doc = NULL;
 
68
 
 
69
    xmlKeepBlanksDefault (0);
 
70
 
 
71
    rcfile = get_read_file (XFCERC);
 
72
 
 
73
    if (rcfile)
 
74
    {
 
75
        doc = xmlParseFile (rcfile);
 
76
        g_free (rcfile);
 
77
    }
 
78
 
 
79
    return doc;
 
80
}
 
81
 
 
82
static xmlNodePtr
 
83
get_xml_root (void)
 
84
{
 
85
    xmlNodePtr node = NULL;
 
86
 
 
87
    /* global xmlDocPtr */
 
88
    if (!xmlconfig)
 
89
        xmlconfig = read_xml_file ();
 
90
 
 
91
    if (!xmlconfig)
 
92
    {
 
93
        g_message ("%s: No config file found", PACKAGE);
 
94
        return NULL;
 
95
    }
 
96
 
 
97
    node = xmlDocGetRootElement (xmlconfig);
 
98
 
 
99
    if (!node)
 
100
    {
 
101
        g_warning ("%s: empty document: %s\n", PACKAGE, xmlconfig->name);
 
102
 
 
103
        xmlFreeDoc (xmlconfig);
 
104
        xmlconfig = NULL;
 
105
        return NULL;
 
106
    }
 
107
 
 
108
    if (!xmlStrEqual (node->name, (const xmlChar *) ROOT))
 
109
    {
 
110
        g_warning ("%s: wrong document type: %s\n", PACKAGE, xmlconfig->name);
 
111
 
 
112
        xmlFreeDoc (xmlconfig);
 
113
        xmlconfig = NULL;
 
114
        return NULL;
 
115
    }
 
116
 
 
117
    return node;
 
118
}
 
119
 
 
120
void
 
121
get_global_prefs (void)
 
122
{
 
123
    xmlNodePtr node;
 
124
 
 
125
    node = get_xml_root ();
 
126
 
 
127
    if (!node)
 
128
        return;
 
129
 
 
130
    /* Now parse the xml tree */
 
131
    for (node = node->children; node; node = node->next)
 
132
    {
 
133
        if (xmlStrEqual (node->name, (const xmlChar *) "Panel"))
 
134
        {
 
135
            panel_parse_xml (node);
 
136
 
 
137
            break;
 
138
        }
 
139
    }
 
140
 
 
141
    /* leave the xmldoc open for get_panel_config() */
 
142
}
 
143
 
 
144
void
 
145
get_panel_config (void)
 
146
{
 
147
    xmlNodePtr node;
 
148
 
 
149
    disable_user_config = check_disable_user_config ();
 
150
 
 
151
    node = get_xml_root ();
 
152
 
 
153
    if (!node)
 
154
    {
 
155
        /* create empty items */
 
156
        groups_set_from_xml (NULL);
 
157
        return;
 
158
    }
 
159
 
 
160
    /* Now parse the xml tree */
 
161
    for (node = node->children; node; node = node->next)
 
162
    {
 
163
        if (xmlStrEqual (node->name, (const xmlChar *) "Groups"))
 
164
        {
 
165
            groups_set_from_xml (node);
 
166
            break;
 
167
        }
 
168
 
 
169
        /* old format */
 
170
        if (xmlStrEqual (node->name, (const xmlChar *) "Left"))
 
171
            old_groups_set_from_xml (LEFT, node);
 
172
        else if (xmlStrEqual (node->name, (const xmlChar *) "Right"))
 
173
            old_groups_set_from_xml (RIGHT, node);
 
174
    }
 
175
 
 
176
    xmlFreeDoc (xmlconfig);
 
177
    xmlconfig = NULL;
 
178
}
 
179
 
 
180
void
 
181
write_panel_config (void)
 
182
{
 
183
    char *dir;
 
184
    char *rcfile;
 
185
    xmlNodePtr root;
 
186
    static gboolean backup = TRUE;
 
187
 
 
188
    disable_user_config = check_disable_user_config ();
 
189
 
 
190
    if (disable_user_config)
 
191
        return;
 
192
 
 
193
    rcfile = get_save_file (XFCERC);
 
194
 
 
195
    if (g_file_test (rcfile, G_FILE_TEST_EXISTS))
 
196
    {
 
197
        if (backup)
 
198
        {
 
199
            write_backup_file (rcfile);
 
200
            backup = FALSE;
 
201
        }
 
202
 
 
203
    }
 
204
    else
 
205
    {
 
206
        dir = g_path_get_dirname (rcfile);
 
207
 
 
208
        if (!g_file_test (dir, G_FILE_TEST_IS_DIR))
 
209
            mkdir (dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
 
210
 
 
211
        g_free (dir);
 
212
    }
 
213
 
 
214
    xmlconfig = xmlNewDoc ("1.0");
 
215
    xmlconfig->children = xmlNewDocRawNode (xmlconfig, NULL, ROOT, NULL);
 
216
 
 
217
    root = (xmlNodePtr) xmlconfig->children;
 
218
    xmlDocSetRootElement (xmlconfig, root);
 
219
 
 
220
    panel_write_xml (root);
 
221
 
 
222
    groups_write_xml (root);
 
223
 
 
224
    xmlSaveFormatFile (rcfile, xmlconfig, 1);
 
225
 
 
226
    xmlFreeDoc (xmlconfig);
 
227
    xmlconfig = NULL;
 
228
}