1 #ifdef COPYRIGHT_INFORMATION
2 #include "gplv3.h"
3 #endif
4 /*
5  *
6  * (c) Edscott Wilson Garcia 2001-2011.
7  *
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program;
22  */
23 
24 
25 static void
start_element(GMarkupParseContext * context,const gchar * element_name,const gchar ** attribute_names,const gchar ** attribute_values,gpointer user_data,GError ** error)26 start_element (GMarkupParseContext * context,
27                const gchar * element_name,
28                const gchar ** attribute_names,
29 	       const gchar ** attribute_values,
30 	       gpointer user_data,
31 	       GError ** error)
32 {
33     const gchar *name = NULL;
34     const gchar *icon = NULL;
35     NOOP ("icon-module, start -> %s\n",element_name);
36     /* here we should have the type and icon attributes set,
37      * but they are not :-(
38      * */
39     if(strcmp (element_name, "mime-type")) return;
40     if(attribute_names) {
41         if(attribute_names[0] && attribute_values[0]){
42             name = attribute_values[0];
43 	}
44         if(attribute_names[1] && attribute_values[1]){
45 	    icon = attribute_values[1];
46 	}
47         if(name && icon) {
48 	    gchar *hash_key=rfm_get_hash_key(name, 0);
49             g_hash_table_replace (basename_hash, hash_key, g_strdup(icon));
50             NOOP(stderr, "0x%x: hashing %s=%s (%s)\n",
51 		    GPOINTER_TO_INT(g_thread_self()), name, icon, hash_key);
52         } else if(name) {
53             NOOP("icon-module, no icon defined for %s\n", name);
54         }
55     }
56     return;
57 }
58 
59 static void
glib_parser(const gchar * mimefile)60 glib_parser (const gchar * mimefile) {
61     FILE *f;
62     size_t l;
63     gchar line[81];
64     GMarkupParseContext *context;
65     GError *error = NULL;
66     GMarkupParser parser = {
67         start_element,
68         NULL,
69         NULL,                   /*text_fun, */
70         NULL,
71         NULL
72     };
73     gpointer user_data = NULL;
74 
75     NOOP("glib_parser(icon-module): parsing %s\n", mimefile);
76     context = g_markup_parse_context_new (&parser, 0, user_data, NULL);
77     f = fopen (mimefile, "r");
78     if(!f) {
79         DBG ("cannot open %s\n", mimefile);
80         return;
81     }
82     while(!feof (f) && (l = fread (line, 1, 80, f)) != 0) {
83         line[l] = 0;
84         g_markup_parse_context_parse (context, line, l, &error);
85     }
86     fclose (f);
87 
88     g_markup_parse_context_free (context);
89 }
90 
91 static gboolean
create_icon_hash(const gchar * mimefile)92 create_icon_hash (const gchar * mimefile) {
93 
94     NOOP("icon-module, create_icon_hash...\n");
95     if(!mimefile) {
96         return FALSE;
97     }
98     rfm_rw_lock_writer_lock(&basename_lock);
99     if(basename_hash) {
100 	return TRUE;
101     }
102 
103     basename_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
104     glib_parser (mimefile);
105     rfm_rw_lock_writer_unlock(&basename_lock);
106     NOOP("icon-module, basename_hash created!\n");
107     return TRUE;
108 }
109 
110 /************************************************************************/
111