11
#include <libxml/parser.h>
12
#include <libxml/tree.h>
14
// for copyfile and rcopy
20
// Modified from Advanced Programming in the Unix Environment.
21
// A generic error(const char*, ...) would be nice.
22
void copyfile(const char* from, const char* to) {
26
const int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
30
if ((fdin = open(from, O_RDONLY)) < 0) {
31
printf("can't open %s for reading\n", from);
35
if ((fdout = open(to, O_RDWR | O_CREAT | O_TRUNC, mode)) < 0) {
36
printf("can't creat %s for writing\n", to);
40
if (fstat(fdin, &statbuf) < 0) { /* need size of input file */
45
/* set size of output file */
46
if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1) {
50
if (write(fdout, "", 1) != 1) {
55
if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED,
56
fdin, 0)) == MAP_FAILED) {
57
puts("mmap error for input");
61
if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
62
MAP_SHARED, fdout, 0)) == MAP_FAILED) {
63
puts("mmap error for output");
67
memcpy(dst, src, statbuf.st_size); /* does the file copy */
70
void rcopy(const char* from, const char* to) {
77
printf("could not open dir, %s\n", from);
83
while((de = readdir(d)) != NULL) {
84
if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
87
asprintf(&fpn, "%s/%s", from, de->d_name);
88
asprintf(&tpn, "%s/%s", to, de->d_name);
90
if(de->d_type == DT_REG) {
92
} else if(de->d_type == DT_DIR) {
93
mkdir(tpn, 0755); // I think we can axe this. See above.
102
/* taken from partconf/partconf.c */
103
void makedirs(const char *dir)
106
char *dirtmp, *basedir;
108
if ((d = opendir(dir)) != NULL) {
112
if (mkdir(dir, 0755) < 0) {
113
dirtmp = strdup(dir);
114
basedir = dirname(dirtmp);
121
void create_file(const char* file) {
126
if((fp = fopen(file, "r")) != NULL) {
132
filename = dirname(tmp);
136
creat(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
138
void add_wallpaper (const char* path) {
140
xmlNode* root, *wallpaper, *ptr = NULL;
142
const int opts = XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_RECOVER;
145
name = basename(name);
147
asprintf(&file, "%s/home/%s/.gnome2/backgrounds.xml", to_location, to_user);
149
doc = xmlReadFile(file, NULL, opts);
150
root = xmlDocGetRootElement(doc);
154
root = xmlNewNode(NULL, (xmlChar*) "wallpapers");
155
xmlDocSetRootElement(doc, root);
157
wallpaper = root->children;
158
while(wallpaper != NULL) {
159
ptr = wallpaper->children;
161
if((xmlStrcmp(ptr->name, (xmlChar*) "filename") == 0) &&
162
(xmlStrcmp(ptr->children->content, (xmlChar*) path) == 0)) {
163
return; // Already set.
167
wallpaper = wallpaper->next;
172
wallpaper = xmlNewChild(root, NULL, (xmlChar*) "wallpaper", NULL);
173
xmlNewProp(wallpaper, (xmlChar*) "deleted", (xmlChar*) "false");
174
xmlNewTextChild(wallpaper, NULL, (xmlChar*) "name", (xmlChar*) name);
175
xmlNewTextChild(wallpaper, NULL, (xmlChar*) "filename", (xmlChar*) path);
176
xmlNewTextChild(wallpaper, NULL, (xmlChar*) "options", (xmlChar*) "scaled");
179
xmlSaveFormatFile(file, doc, 1);
182
// yikes, get rid of this.
183
void makegconfdirs(const char *dir)
186
char *dirtmp, *basedir, *gconf;
188
if ((d = opendir(dir)) != NULL) {
192
if (mkdir(dir, 0755) < 0) {
193
dirtmp = strdup(dir);
194
basedir = dirname(dirtmp);
195
makegconfdirs(basedir);
201
dirtmp = strdup(dir);
202
basedir = basename(dirtmp);
203
if(strcmp(basedir,".gconf") != 0) {
204
asprintf(&gconf, "%s/%%gconf.xml", dir);
205
creat(gconf, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
212
void set_gconf_key (const char* path, const char* name, gconf_type type, const char* value) {
215
xmlNode* root, *entry = NULL;
216
const int opts = XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_RECOVER;
218
asprintf(&gconf, "%s/home/%s/.gconf/%s", to_location, to_user, path);
219
asprintf(&file, "%s/%%gconf.xml", gconf);
221
makegconfdirs(gconf);
225
doc = xmlReadFile(file, NULL, opts);
226
root = xmlDocGetRootElement(doc);
228
root = xmlNewNode(NULL, (xmlChar*) "gconf");
229
xmlDocSetRootElement(doc, root);
230
entry = xmlNewChild(root, NULL, (xmlChar*) "entry", NULL);
231
xmlNewProp(entry, (xmlChar*) "name", (xmlChar*) name);
233
entry = root->children;
234
while(entry != NULL) {
235
if(xmlStrcmp(xmlGetProp(entry, (xmlChar*) "name"),(xmlChar*) name) == 0)
240
entry = xmlNewChild(root, NULL, (xmlChar*) "entry", NULL);
241
xmlNewProp(entry, (xmlChar*) "name", (xmlChar*) name);
246
if(type == GCONF_STRING) {
248
xmlSetProp(entry, (xmlChar*) "type", (xmlChar*) "string");
249
if(entry->children == NULL)
250
xmlNewTextChild(entry, NULL, (xmlChar*) "stringvalue", (xmlChar*) value);
252
xmlNodeSetContent(entry->children, (xmlChar*) value);
254
} else if (type == GCONF_BOOLEAN) {
256
xmlSetProp(entry, (xmlChar*) "type", (xmlChar*) "bool");
257
xmlSetProp(entry, (xmlChar*) "value", (xmlChar*) value);
259
} else if (type == GCONF_LIST_STRING) {
260
xmlNode* li, *sv = NULL;
261
xmlSetProp(entry, (xmlChar*) "type", (xmlChar*) "list");
262
xmlSetProp(entry, (xmlChar*) "ltype", (xmlChar*) "string");
264
if(entry->children == NULL)
265
li = xmlNewChild(entry, NULL, (xmlChar*) "li", NULL);
267
li = entry->children;
269
xmlSetProp(li, (xmlChar*) "type", (xmlChar*) "string");
273
if(xmlStrcmp(sv->children->content, (xmlChar*) value) == 0)
277
xmlNewTextChild(li, NULL, (xmlChar*) "stringvalue", (xmlChar*) value);
280
xmlSaveFormatFile(file, doc, 1);