adds support for tag colors

Sun, 17 Jun 2018 09:50:05 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 17 Jun 2018 09:50:05 +0200
changeset 423
84e6c407b431
parent 422
1429fb385513
child 424
f851ba530957

adds support for tag colors

dav/tags.c file | annotate | diff | comparison | revisions
--- a/dav/tags.c	Sun Jun 17 07:49:25 2018 +0200
+++ b/dav/tags.c	Sun Jun 17 09:50:05 2018 +0200
@@ -69,8 +69,25 @@
             sstr_t line = sstrtrim(sstrn((char*)buf + line_start, i - line_start));
             if(line.length > 0) {
                 DavTag *tag = calloc(1, sizeof(DavTag));
-                tag->name = sstrdup(line).ptr;
-                tag->color = NULL;
+                sstr_t color = sstrchr(line, '#');
+                if(color.length>0) {
+                    sstr_t name = line;
+                    name.length = (int)(color.ptr-line.ptr);
+                    if(name.length != 0) {
+                        tag->name = sstrdup(name).ptr;
+                        color.ptr++;
+                        color.length--;
+                        if(color.length > 0) {
+                            tag->color = sstrdup(color).ptr;
+                        }
+                    } else {
+                        free(tag);
+                    }
+                } else {
+                    tag->name = sstrdup(line).ptr;
+                    tag->color = NULL;
+                }
+                
                 tags = ucx_list_append(tags, tag);
             }
             line_start = i+1;
@@ -88,7 +105,11 @@
     UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
     UCX_FOREACH(elm, tags) {
         DavTag *tag = elm->data;
-        ucx_bprintf(buf, "%s\n", tag->name);
+        if(tag->color) {
+            ucx_bprintf(buf, "%s#%s\n", tag->name, tag->color);
+        } else {
+            ucx_bprintf(buf, "%s\n", tag->name);
+        }
     }
     return buf;
 }
@@ -133,6 +154,7 @@
 
 static DavTag* parse_xml_dav_tag(DavXmlNode *node) {
     char *name = NULL;
+    char *color = NULL;
     
     DavXmlNode *c = node->children;
     while(c) {
@@ -146,7 +168,12 @@
                             name = value;
                         }
                     }
-                    // TODO: color, ...
+                    if(!strcmp(c->name, "color")) {
+                        char *value = dav_xml_getstring(c->children);
+                        if(value) {
+                            color = value;
+                        }
+                    }
                 }
             }
         }
@@ -157,7 +184,7 @@
     if(name) {
         tag = malloc(sizeof(DavTag));
         tag->name = strdup(name);
-        tag->color = NULL;
+        tag->color = color ? strdup(color) : NULL;
     }
     return tag;
 } 
@@ -189,8 +216,11 @@
         
         DavXmlNode *tagelm = dav_xml_createnode(DAV_NS, "tag");
         DavXmlNode *tagname = dav_xml_createnode_with_text(DAV_NS, "name", tag->name);
-        // TODO: color
         tagelm->children = tagname;
+        if(tag->color) {
+            DavXmlNode *tagcolor = dav_xml_createnode_with_text(DAV_NS, "color", tag->color);
+            tagname->next = tagcolor;
+        }
         
         if(lasttag) {
             lasttag->next = tagelm;

mercurial