677f8d7169bd9b2227723088089f5ba979b49bdf
[livewp] / applet / src / livewp-exthemes.c
1 /* vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24 /*******************************************************************************/
25 #include "livewp-exthemes.h"
26 /*******************************************************************************/
27
28 GHashTable  * 
29 parse_theme(gchar *file){
30     GHashTable  *hash;
31     xmlDoc *doc = NULL;
32     xmlNode *root = NULL, *first_child, *node;
33     doc = xmlReadFile(file, NULL, 0);
34     if (!doc)
35         return NULL;
36     root = xmlDocGetRootElement(doc);
37     if (!root)
38         return NULL;
39     first_child = root->children;
40     hash = g_hash_table_new(g_str_hash, g_str_equal);
41     for (node = first_child; node; node = node->next){
42         //if (node-type == XML_ELEMENT_NODE && xmlStrcmp(node->name, "param")){
43           //  child = 
44         //}
45         if (node->type == XML_ELEMENT_NODE){ 
46             //fprintf(stderr, "%s => %s\n", node->name, xmlNodeGetContent(node));
47             g_hash_table_insert(hash, g_strdup((gchar*)node->name), g_strdup((gchar*)xmlNodeGetContent(node)));
48         }
49     }
50     if (!(g_hash_table_lookup(hash, "category")))
51         g_hash_table_insert(hash, g_strdup("category"), g_strdup("Unknown"));  
52     if (!(g_hash_table_lookup(hash, "name")))
53         g_hash_table_insert(hash, g_strdup("name"), g_strdup("Unknown"));
54
55     xmlFreeDoc(doc);
56     xmlCleanupParser();
57     return hash;
58 }
59
60 /*******************************************************************************/
61 gint compar (gpointer a, gpointer b){
62     return strcmp( (g_hash_table_lookup(a, "name")), (g_hash_table_lookup(b, "name")) );
63 }
64
65
66 GSList *
67 get_list_exthemes(void){
68     Dirent  *dp;
69     DIR     *dir_fd;
70     GSList *store = NULL;
71     GHashTable *hash = NULL;
72     
73     dir_fd = opendir(EXTHEME_PATH);
74     if(dir_fd){
75
76         while((dp = readdir(dir_fd))){
77             
78             if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
79                 continue;
80             /* TO DO read only *.xml files */
81             if(dp->d_type == DT_REG || dp->d_type == DT_LNK){
82                 hash = parse_theme(g_strdup_printf("%s%s", EXTHEME_PATH, dp->d_name));
83                 if (hash)
84                     store = g_slist_append(store, hash);
85             }
86         }
87         closedir(dir_fd);
88     }
89     store = g_slist_sort(store, (GCompareFunc)compar);
90     return store;
91 }