changed license to GPL v2
[livewp] / applet / src / livewp-exthemes.c
index 958631f..677f8d7 100644 (file)
@@ -7,8 +7,8 @@
  *       for the code
  * 
  * This software is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1 of
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2 of
  * the License, or (at your option) any later version.
  * 
  * This software is distributed in the hope that it will be useful, but
 GHashTable  * 
 parse_theme(gchar *file){
     GHashTable  *hash;
-    xmlDoc *doc;
-    xmlNode *root, *first_child, *node;
+    xmlDoc *doc = NULL;
+    xmlNode *root = NULL, *first_child, *node;
     doc = xmlReadFile(file, NULL, 0);
+    if (!doc)
+        return NULL;
     root = xmlDocGetRootElement(doc);
+    if (!root)
+        return NULL;
     first_child = root->children;
     hash = g_hash_table_new(g_str_hash, g_str_equal);
     for (node = first_child; node; node = node->next){
@@ -40,21 +44,31 @@ parse_theme(gchar *file){
         //}
         if (node->type == XML_ELEMENT_NODE){ 
             //fprintf(stderr, "%s => %s\n", node->name, xmlNodeGetContent(node));
-            g_hash_table_insert(hash, g_strdup(node->name), g_strdup(xmlNodeGetContent(node)));
+            g_hash_table_insert(hash, g_strdup((gchar*)node->name), g_strdup((gchar*)xmlNodeGetContent(node)));
         }
     }
     if (!(g_hash_table_lookup(hash, "category")))
-        g_hash_table_insert(hash, g_strdup("category"), g_strdup("Unknown"));
+        g_hash_table_insert(hash, g_strdup("category"), g_strdup("Unknown"));  
+    if (!(g_hash_table_lookup(hash, "name")))
+        g_hash_table_insert(hash, g_strdup("name"), g_strdup("Unknown"));
+
     xmlFreeDoc(doc);
     xmlCleanupParser();
     return hash;
 }
 
+/*******************************************************************************/
+gint compar (gpointer a, gpointer b){
+    return strcmp( (g_hash_table_lookup(a, "name")), (g_hash_table_lookup(b, "name")) );
+}
+
+
 GSList *
 get_list_exthemes(void){
     Dirent  *dp;
     DIR     *dir_fd;
     GSList *store = NULL;
+    GHashTable *hash = NULL;
     
     dir_fd = opendir(EXTHEME_PATH);
     if(dir_fd){
@@ -65,10 +79,13 @@ get_list_exthemes(void){
                 continue;
             /* TO DO read only *.xml files */
             if(dp->d_type == DT_REG || dp->d_type == DT_LNK){
-                store = g_slist_append(store, parse_theme(g_strdup_printf("%s%s", EXTHEME_PATH, dp->d_name)));
+                hash = parse_theme(g_strdup_printf("%s%s", EXTHEME_PATH, dp->d_name));
+                if (hash)
+                    store = g_slist_append(store, hash);
             }
         }
         closedir(dir_fd);
     }
+    store = g_slist_sort(store, (GCompareFunc)compar);
     return store;
 }