4 typedef struct _E_Fm2_Mime_Handler_Tuple E_Fm2_Mime_Handler_Tuple;
5 struct _E_Fm2_Mime_Handler_Tuple
11 /* local subsystem functions */
12 static Eina_Bool _e_fm2_mime_handler_glob_match_foreach(const Eina_Hash *hash __UNUSED__, const void *key, void *data, void *fdata);
13 static Eina_Bool _e_fm_mime_icon_foreach(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata);
15 static Eina_Hash *icon_map = NULL;
16 static Eina_Hash *_mime_handlers = NULL;
17 static Eina_Hash *_glob_handlers = NULL;
19 /* externally accessible functions */
21 e_fm_mime_filename_get(const char *fname)
23 return efreet_mime_globs_type_get(fname);
28 * "THUMB" == generate a thumb
29 * "e/icons/fileman/mime/..." == theme icon
30 * "/path/to/file....edj" = explicit icon edje file
31 * "/path/to/file..." = explicit image file to use
34 e_fm_mime_icon_get(const char *mime)
36 char buf[4096], buf2[4096], *val;
38 E_Config_Mime_Icon *mi;
41 /* 0.0 clean out hash cache once it has more than 512 entries in it */
42 if (eina_hash_population(icon_map) > 512) e_fm_mime_icon_cache_flush();
44 /* 0. look in mapping cache */
45 val = eina_hash_find(icon_map, mime);
48 eina_strlcpy(buf2, mime, sizeof(buf2));
49 val = strchr(buf2, '/');
52 /* 1. look up in mapping to file or thumb (thumb has flag)*/
53 EINA_LIST_FOREACH(e_config->mime_icons, l, mi)
55 if (e_util_glob_match(mi->mime, mime))
57 eina_strlcpy(buf, mi->icon, sizeof(buf));
62 /* 2. look up in ~/.e/e/icons */
63 len = e_user_dir_snprintf(buf, sizeof(buf), "icons/%s.edj", mime);
64 if (len >= sizeof(buf))
65 goto try_e_icon_generic;
67 if (ecore_file_exists(buf)) goto ok;
68 memcpy(buf + len - (sizeof("edj") - 1), "svg", sizeof("svg"));
69 if (ecore_file_exists(buf)) goto ok;
70 memcpy(buf + len - (sizeof("edj") - 1), "png", sizeof("png"));
71 if (ecore_file_exists(buf)) goto ok;
74 len = e_user_dir_snprintf(buf, sizeof(buf), "icons/%s.edj", buf2);
75 if (len >= sizeof(buf))
78 if (ecore_file_exists(buf)) goto ok;
79 memcpy(buf + len - (sizeof("edj") - 1), "svg", sizeof("svg"));
80 if (ecore_file_exists(buf)) goto ok;
81 memcpy(buf + len - (sizeof("edj") - 1), "png", sizeof("png"));
82 if (ecore_file_exists(buf)) goto ok;
84 /* 3. look up icon in theme */
86 memcpy(buf, "e/icons/fileman/mime/", sizeof("e/icons/fileman/mime/") - 1);
87 eina_strlcpy(buf + sizeof("e/icons/fileman/mime/") - 1, mime,
88 sizeof(buf) - (sizeof("e/icons/fileman/mime/") - 1));
89 val = (char *)e_theme_edje_file_get("base/theme/fileman", buf);
90 if ((val) && (e_util_edje_collection_exists(val, buf))) goto ok;
92 eina_strlcpy(buf + sizeof("e/icons/fileman/mime/") - 1, buf2,
93 sizeof(buf) - (sizeof("e/icons/fileman/mime/") - 1));
94 val = (char *)e_theme_edje_file_get("base/theme/fileman", buf);
95 if ((val) && (e_util_edje_collection_exists(val, buf))) goto ok;
97 /* 4. look up icon in PREFIX/share/enlightent/data/icons */
98 len = e_prefix_data_snprintf(buf, sizeof(buf), "data/icons/%s.edj", mime);
99 if (len >= sizeof(buf))
100 goto try_efreet_icon_generic;
102 if (ecore_file_exists(buf)) goto ok;
103 memcpy(buf + len - (sizeof("edj") - 1), "svg", sizeof("svg"));
104 if (ecore_file_exists(buf)) goto ok;
105 memcpy(buf + len - (sizeof("edj") - 1), "png", sizeof("png"));
106 if (ecore_file_exists(buf)) goto ok;
108 try_efreet_icon_generic:
109 len = e_prefix_data_snprintf(buf, sizeof(buf), "data/icons/%s.edj", buf2);
110 if (len >= sizeof(buf))
111 goto try_efreet_icon_generic;
113 if (ecore_file_exists(buf)) goto ok;
114 memcpy(buf + len - (sizeof("edj") - 1), "svg", sizeof("svg"));
115 if (ecore_file_exists(buf)) goto ok;
116 memcpy(buf + len - (sizeof("edj") - 1), "png", sizeof("png"));
117 if (ecore_file_exists(buf)) goto ok;
122 val = (char *)eina_stringshare_add(buf);
123 if (!icon_map) icon_map = eina_hash_string_superfast_new(NULL);
124 eina_hash_add(icon_map, mime, val);
129 e_fm_mime_icon_cache_flush(void)
131 Eina_List *freelist = NULL;
133 eina_hash_foreach(icon_map, _e_fm_mime_icon_foreach, &freelist);
134 E_FREE_LIST(freelist, eina_stringshare_del);
135 eina_hash_free(icon_map);
139 /* create (allocate), set properties, and return a new mime handler */
140 EAPI E_Fm2_Mime_Handler *e_fm2_mime_handler_new(const char *label, const char *icon_group, void (*action_func) (void *data, Evas_Object *obj, const char *path), void *action_data, int (test_func) (void *data, Evas_Object *obj, const char *path), void *test_data)
142 E_Fm2_Mime_Handler *handler;
144 if ((!label) || (!action_func)) return NULL;
146 handler = E_NEW(E_Fm2_Mime_Handler, 1);
147 if (!handler) return NULL;
149 handler->label = eina_stringshare_add(label);
150 handler->icon_group = icon_group ? eina_stringshare_add(icon_group) : NULL;
151 handler->action_func = action_func;
152 handler->action_data = action_data;
153 handler->test_func = test_func;
154 handler->test_data = test_data;
160 e_fm2_mime_handler_free(E_Fm2_Mime_Handler *handler)
162 if (!handler) return;
164 eina_stringshare_del(handler->label);
165 if (handler->icon_group) eina_stringshare_del(handler->icon_group);
169 /* associate a certain mime type with a handler */
171 e_fm2_mime_handler_mime_add(E_Fm2_Mime_Handler *handler, const char *mime)
173 Eina_List *handlers = NULL;
175 if ((!handler) || (!mime)) return 0;
177 /* if there's an entry for this mime already, then append to its list */
178 if ((handlers = eina_hash_find(_mime_handlers, mime)))
180 handlers = eina_list_append(handlers, handler);
181 eina_hash_modify(_mime_handlers, mime, handlers);
185 /* no previous entry for this mime, lets add one */
186 handlers = eina_list_append(handlers, handler);
187 if (!_mime_handlers) _mime_handlers = eina_hash_string_superfast_new(NULL);
188 eina_hash_add(_mime_handlers, mime, handlers);
194 /* associate a certain glob with a handler */
196 e_fm2_mime_handler_glob_add(E_Fm2_Mime_Handler *handler, const char *glob_)
198 Eina_List *handlers = NULL;
200 if ((!handler) || (!glob_)) return 0;
202 /* if there's an entry for this glob already, then append to its list */
203 if ((handlers = eina_hash_find(_glob_handlers, glob_)))
205 handlers = eina_list_append(handlers, handler);
206 eina_hash_modify(_glob_handlers, glob_, handlers);
210 /* no previous entry for this glob, lets add one */
211 handlers = eina_list_append(handlers, handler);
212 if (!_glob_handlers) _glob_handlers = eina_hash_string_superfast_new(NULL);
213 eina_hash_add(_glob_handlers, glob_, handlers);
219 /* delete a certain handler for a certain mime */
221 e_fm2_mime_handler_mime_del(E_Fm2_Mime_Handler *handler, const char *mime)
223 Eina_List *handlers = NULL;
225 if ((!handler) || (!mime)) return;
227 /* if there's an entry for this mime already, then remove from list */
228 if ((handlers = eina_hash_find(_mime_handlers, mime)))
230 handlers = eina_list_remove(handlers, handler);
232 eina_hash_modify(_mime_handlers, mime, handlers);
235 eina_hash_del(_mime_handlers, mime, handlers);
236 if (!eina_hash_population(_mime_handlers))
238 eina_hash_free(_mime_handlers);
239 _mime_handlers = NULL;
245 /* delete a certain handler for a certain glob */
247 e_fm2_mime_handler_glob_del(E_Fm2_Mime_Handler *handler, const char *glob_)
249 Eina_List *handlers = NULL;
251 if ((!handler) || (!glob_)) return;
253 /* if there's an entry for this glob already, then remove from list */
254 if ((handlers = eina_hash_find(_glob_handlers, glob_)))
256 handlers = eina_list_remove(handlers, handler);
258 eina_hash_modify(_glob_handlers, glob_, handlers);
261 eina_hash_del(_glob_handlers, glob_, handlers);
262 if (!eina_hash_population(_glob_handlers))
264 eina_hash_free(_glob_handlers);
265 _glob_handlers = NULL;
271 EAPI const Eina_List *
272 e_fm2_mime_handler_mime_handlers_get(const char *mime)
274 if ((!mime) || (!_mime_handlers)) return NULL;
275 return eina_hash_find(_mime_handlers, mime);
278 /* get the list of glob handlers for a glob.
279 NOTE: the list should be free()'ed */
281 e_fm2_mime_handler_glob_handlers_get(const char *glob_)
283 E_Fm2_Mime_Handler_Tuple *tuple = NULL;
284 Eina_List *handlers = NULL;
286 if ((!glob_) || (!_glob_handlers)) return NULL;
288 tuple = E_NEW(E_Fm2_Mime_Handler_Tuple, 1);
291 eina_hash_foreach(_glob_handlers, _e_fm2_mime_handler_glob_match_foreach, tuple);
292 handlers = tuple->list;
297 /* call a certain handler */
299 e_fm2_mime_handler_call(E_Fm2_Mime_Handler *handler, Evas_Object *obj, const char *path)
301 if ((!handler) || (!obj) || (!path) || (!handler->action_func))
304 if (handler->test_func)
306 if (handler->test_func(handler->test_data, obj, path))
308 handler->action_func(handler->action_data, obj, path);
315 handler->action_func(handler->action_data, obj, path);
319 /* call all handlers related to a certain mime */
321 e_fm2_mime_handler_mime_handlers_call_all(Evas_Object *obj, const char *path, const char *mime)
323 const Eina_List *l, *handlers;
324 E_Fm2_Mime_Handler *handler = NULL;
326 if ((!obj) || (!path) || (!mime)) return;
328 handlers = e_fm2_mime_handler_mime_handlers_get(mime);
329 if (!handlers) return;
331 EINA_LIST_FOREACH(handlers, l, handler)
333 if (!handler) continue;
335 e_fm2_mime_handler_call(handler, obj, path);
339 /* call all handlers related to a certain glob */
341 e_fm2_mime_handler_glob_handlers_call_all(Evas_Object *obj, const char *path, const char *glob_)
343 Eina_List *handlers = NULL;
345 E_Fm2_Mime_Handler *handler = NULL;
347 if ((!obj) || (!path) || (!glob_)) return;
349 handlers = e_fm2_mime_handler_glob_handlers_get(glob_);
350 if (!handlers) return;
352 EINA_LIST_FOREACH(handlers, l, handler)
354 if (!handler) continue;
356 e_fm2_mime_handler_call(handler, obj, path);
360 /* run a handlers test function */
362 e_fm2_mime_handler_test(E_Fm2_Mime_Handler *handler, Evas_Object *obj, const char *path)
364 if ((!handler) || (!obj) || (!path)) return 0;
365 if (!handler->test_func) return 1;
367 return handler->test_func(handler->test_data, obj, path);
370 /* local subsystem functions */
371 /* used to loop a glob hash and determine if the glob handler matches the filename */
373 _e_fm2_mime_handler_glob_match_foreach(const Eina_Hash *hash __UNUSED__, const void *key, void *data, void *fdata)
375 E_Fm2_Mime_Handler_Tuple *tuple;
376 Eina_List *handlers = NULL;
378 void *handler = NULL;
381 if (e_util_glob_match(tuple->str, key))
384 EINA_LIST_FOREACH(handlers, l, handler)
387 tuple->list = eina_list_append(tuple->list, handler);
395 _e_fm_mime_icon_foreach(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata)
397 Eina_List **freelist;
400 *freelist = eina_list_append(*freelist, data);