dav/config.c

changeset 194
1950f483d3c4
parent 185
cd42cccee550
child 198
44054c452de1
--- a/dav/config.c	Sat Feb 27 17:23:36 2016 +0100
+++ b/dav/config.c	Sat Feb 27 19:39:55 2016 +0100
@@ -477,3 +477,50 @@
     return https_proxy;
 }
 
+
+int add_repository(Repository *repo) {
+    char *file = util_concat_path(ENV_HOME, ".dav/config.xml");
+    xmlDoc *doc = xmlReadFile(file, NULL, 0);
+    if(!doc) {
+        free(file);
+        fprintf(stderr, "Cannot load config.xml\n");
+        return 1;
+    }
+    
+    xmlNode *root = xmlDocGetRootElement(doc);
+    
+    xmlNode *repoNode = xmlNewNode(NULL, BAD_CAST "repository");
+    xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t");
+    xmlNewTextChild(repoNode, NULL, BAD_CAST "name", BAD_CAST repo->name);
+    xmlNodeAddContent(repoNode, BAD_CAST "\n\t\t");
+    xmlNewTextChild(repoNode, NULL, BAD_CAST "url", BAD_CAST repo->url);
+    xmlNodeAddContent(repoNode, BAD_CAST "\n");
+    if(repo->user) {
+        xmlNodeAddContent(repoNode, BAD_CAST "\t\t");
+        xmlNewChild(repoNode, NULL, BAD_CAST "user", BAD_CAST repo->user);
+        xmlNodeAddContent(repoNode, BAD_CAST "\n");
+        if(repo->password) {
+            char *pwenc = util_base64encode(
+                    repo->password,
+                    strlen(repo->password));
+            xmlNodeAddContent(repoNode, BAD_CAST "\t\t");
+            xmlNewTextChild(repoNode, NULL, BAD_CAST "password", BAD_CAST pwenc);
+            free(pwenc);
+            xmlNodeAddContent(repoNode, BAD_CAST "\n");
+        }
+    }
+    xmlNodeAddContent(repoNode, BAD_CAST "\t");
+    
+    xmlNodeAddContent(root, "\n\t");
+    xmlAddChild(root, repoNode);
+    xmlNodeAddContent(root, BAD_CAST "\n");
+    
+    int ret = 0;
+    if(xmlSaveFormatFileEnc(file, doc, "UTF-8", 1) == -1) {
+        ret = 1;
+    }
+    xmlFreeDoc(doc);
+    free(file);
+    
+    return ret;
+}

mercurial