aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/efreet
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-29 23:04:40 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-29 23:04:40 +0000
commit4bc0210bd31ed1de6554441562bd93ea863ee9d9 (patch)
tree5d83be12538f8c8d3816bbf65916ce383d050c2e /src/bin/efreet
parentefl: tell compiler to not yell about missing field initializers (diff)
downloadefl-4bc0210bd31ed1de6554441562bd93ea863ee9d9.tar.gz
efl: merge efreet.
seems to be fine, pass distcheck and friends. please report. changes: - documentation hierarchy fixes - replaced __UNUSED__ with EINA_UNUSED - replaced PKG_DATA_DIR with PACKAGE_DATA_DIR"/efreet" SVN revision: 81889
Diffstat (limited to 'src/bin/efreet')
-rw-r--r--src/bin/efreet/efreet_desktop_cache_create.c465
-rw-r--r--src/bin/efreet/efreet_icon_cache_create.c1070
-rw-r--r--src/bin/efreet/efreetd.c59
-rw-r--r--src/bin/efreet/efreetd.h34
-rw-r--r--src/bin/efreet/efreetd_cache.c575
-rw-r--r--src/bin/efreet/efreetd_cache.h13
-rw-r--r--src/bin/efreet/efreetd_dbus.c262
-rw-r--r--src/bin/efreet/efreetd_dbus.h10
8 files changed, 2488 insertions, 0 deletions
diff --git a/src/bin/efreet/efreet_desktop_cache_create.c b/src/bin/efreet/efreet_desktop_cache_create.c
new file mode 100644
index 0000000000..db3118ca1d
--- /dev/null
+++ b/src/bin/efreet/efreet_desktop_cache_create.c
@@ -0,0 +1,465 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <Eina.h>
+#include <Eet.h>
+#include <Ecore.h>
+#include <Ecore_File.h>
+
+#define EFREET_MODULE_LOG_DOM _efreet_desktop_cache_log_dom
+static int _efreet_desktop_cache_log_dom = -1;
+
+#include "Efreet.h"
+#include "efreet_private.h"
+#include "efreet_cache_private.h"
+
+static Eet_Data_Descriptor *edd = NULL;
+static Eet_File *ef = NULL;
+static Eet_File *util_ef = NULL;
+
+static Eina_Hash *desktops = NULL;
+
+static Eina_Hash *file_ids = NULL;
+static Efreet_Cache_Hash *old_file_ids = NULL;
+static Eina_Hash *paths = NULL;
+
+static Eina_Hash *mime_types = NULL;
+static Eina_Hash *categories = NULL;
+static Eina_Hash *startup_wm_class = NULL;
+static Eina_Hash *name = NULL;
+static Eina_Hash *generic_name = NULL;
+static Eina_Hash *comment = NULL;
+static Eina_Hash *exec = NULL;
+
+static int
+cache_add(const char *path, const char *file_id, int priority EINA_UNUSED, int *changed)
+{
+ Efreet_Desktop *desk;
+ char *ext;
+
+ INF("FOUND: %s", path);
+ if (file_id) INF(" (id): %s", file_id);
+ ext = strrchr(path, '.');
+ if (!ext || (strcmp(ext, ".desktop") && strcmp(ext, ".directory"))) return 1;
+ desk = efreet_desktop_new(path);
+ if (desk) INF(" OK");
+ else INF(" FAIL");
+ if (!desk) return 1;
+ if (!desk->eet)
+ {
+ /* This file isn't in cache */
+ *changed = 1;
+ INF(" NEW");
+ }
+ else if (ecore_file_mod_time(desk->orig_path) != desk->load_time)
+ {
+ efreet_desktop_free(desk);
+ *changed = 1;
+ desk = efreet_desktop_uncached_new(path);
+ if (desk) INF(" CHANGED");
+ else INF(" NO UNCACHED");
+ }
+ if (!desk) return 1;
+ if (file_id && old_file_ids && !eina_hash_find(old_file_ids->hash, file_id))
+ {
+ *changed = 1;
+ INF(" NOT IN UTILS");
+ }
+ if (!eina_hash_find(paths, desk->orig_path))
+ {
+ if (!eet_data_write(ef, edd, desk->orig_path, desk, 0))
+ return 0;
+ eina_hash_add(paths, desk->orig_path, (void *)1);
+ }
+ /* TODO: We should check priority, and not just hope we search in right order */
+ /* TODO: We need to find out if prioritized file id has changed because of
+ * changed search order. */
+ if (!desk->hidden && desk->type == EFREET_DESKTOP_TYPE_APPLICATION &&
+ file_id && !eina_hash_find(file_ids, file_id))
+ {
+ Eina_List *l;
+ char *data;
+ Efreet_Cache_Array_String *array;
+
+#define ADD_LIST(list, hash) \
+ EINA_LIST_FOREACH((list), l, data) \
+ { \
+ array = eina_hash_find((hash), data); \
+ if (!array) \
+ array = NEW(Efreet_Cache_Array_String, 1); \
+ array->array = realloc(array->array, sizeof (char *) * (array->array_count + 1)); \
+ array->array[array->array_count++] = desk->orig_path; \
+ eina_hash_set((hash), data, array); \
+ }
+#define ADD_ELEM(elem, hash) \
+ if ((elem)) \
+ { \
+ data = (elem); \
+ array = eina_hash_find((hash), data); \
+ if (!array) \
+ array = NEW(Efreet_Cache_Array_String, 1); \
+ array->array = realloc(array->array, sizeof (char *) * (array->array_count + 1)); \
+ array->array[array->array_count++] = desk->orig_path; \
+ eina_hash_set((hash), data, array); \
+ }
+ ADD_LIST(desk->mime_types, mime_types);
+ ADD_LIST(desk->categories, categories);
+ ADD_ELEM(desk->startup_wm_class, startup_wm_class);
+ ADD_ELEM(desk->name, name);
+ ADD_ELEM(desk->generic_name, generic_name);
+ ADD_ELEM(desk->comment, comment);
+ ADD_ELEM(desk->exec, exec);
+ eina_hash_add(file_ids, file_id, desk->orig_path);
+ eina_hash_add(desktops, desk->orig_path, desk);
+ }
+ else
+ efreet_desktop_free(desk);
+ return 1;
+}
+
+
+static int
+cache_scan(const char *path, const char *base_id, int priority, int recurse, int *changed)
+{
+ char *file_id = NULL;
+ char id[PATH_MAX];
+ char buf[PATH_MAX];
+ Eina_Iterator *it;
+ Eina_File_Direct_Info *info;
+
+ if (!ecore_file_is_dir(path)) return 1;
+
+ it = eina_file_stat_ls(path);
+ if (!it) return 1;
+
+ id[0] = '\0';
+ EINA_ITERATOR_FOREACH(it, info)
+ {
+ const char *fname;
+
+ fname = info->path + info->name_start;
+ if (base_id)
+ {
+ if (*base_id)
+ snprintf(id, sizeof(id), "%s-%s", base_id, fname);
+ else
+ strcpy(id, fname);
+ file_id = id;
+ }
+
+ snprintf(buf, sizeof(buf), "%s/%s", path, fname);
+ if (info->type == EINA_FILE_DIR)
+ {
+ if (recurse)
+ cache_scan(buf, file_id, priority, recurse, changed);
+ }
+ else
+ {
+ if (!cache_add(buf, file_id, priority, changed))
+ {
+ eina_iterator_free(it);
+ return 0;
+ }
+ }
+ }
+ eina_iterator_free(it);
+ return 1;
+}
+
+static int
+cache_lock_file(void)
+{
+ char file[PATH_MAX];
+ struct flock fl;
+ int lockfd;
+
+ snprintf(file, sizeof(file), "%s/efreet/desktop_data.lock", efreet_cache_home_get());
+ lockfd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+ if (lockfd < 0) return -1;
+ efreet_fsetowner(lockfd);
+
+ memset(&fl, 0, sizeof(struct flock));
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ if (fcntl(lockfd, F_SETLK, &fl) < 0)
+ {
+ INF("LOCKED! You may want to delete %s if this persists", file);
+ close(lockfd);
+ return -1;
+ }
+
+ return lockfd;
+}
+
+int
+main(int argc, char **argv)
+{
+ /* TODO:
+ * - Add file monitor on files, so that we catch changes on files
+ * during whilst this program runs.
+ * - Maybe linger for a while to reduce number of cache re-creates.
+ */
+ Efreet_Cache_Hash hash;
+ Efreet_Cache_Version version;
+ Eina_List *dirs = NULL;
+ Eina_List *systemdirs = NULL;
+ Eina_List *extra_dirs = NULL;
+ Eina_List *l = NULL;
+ int priority = 0;
+ char *dir = NULL;
+ char *path;
+ int lockfd = -1, tmpfd;
+ int changed = 0;
+ int i;
+ char file[PATH_MAX] = { '\0' };
+ char util_file[PATH_MAX] = { '\0' };
+
+ if (!eina_init()) goto eina_error;
+ _efreet_desktop_cache_log_dom =
+ eina_log_domain_register("efreet_desktop_cache", EFREET_DEFAULT_LOG_COLOR);
+ if (_efreet_desktop_cache_log_dom < 0)
+ {
+ EINA_LOG_ERR("Efreet: Could not create a log domain for efreet_desktop_cache.");
+ return -1;
+ }
+
+ for (i = 1; i < argc; i++)
+ {
+ if (!strcmp(argv[i], "-v"))
+ eina_log_domain_level_set("efreet_desktop_cache", EINA_LOG_LEVEL_DBG);
+ else if ((!strcmp(argv[i], "-h")) ||
+ (!strcmp(argv[i], "-help")) ||
+ (!strcmp(argv[i], "--h")) ||
+ (!strcmp(argv[i], "--help")))
+ {
+ printf("Options:\n");
+ printf(" -v Verbose mode\n");
+ printf(" -d dir1 dir2 Extra dirs\n");
+ exit(0);
+ }
+ else if (!strcmp(argv[i], "-d"))
+ {
+ while ((i < (argc - 1)) && (argv[(i + 1)][0] != '-'))
+ extra_dirs = eina_list_append(extra_dirs, argv[++i]);
+ }
+ }
+ extra_dirs = eina_list_sort(extra_dirs, -1, EINA_COMPARE_CB(strcmp));
+
+ /* init external subsystems */
+ if (!eet_init()) goto eet_error;
+ if (!ecore_init()) goto ecore_error;
+
+ efreet_cache_update = 0;
+ /* finish efreet init */
+ if (!efreet_init()) goto efreet_error;
+
+ /* create homedir */
+ snprintf(file, sizeof(file), "%s/efreet", efreet_cache_home_get());
+ if (!ecore_file_exists(file))
+ {
+ if (!ecore_file_mkpath(file)) goto efreet_error;
+ efreet_setowner(file);
+ }
+
+ /* lock process, so that we only run one copy of this program */
+ lockfd = cache_lock_file();
+ if (lockfd == -1) goto efreet_error;
+
+ edd = efreet_desktop_edd();
+ if (!edd) goto edd_error;
+
+ /* read user dirs from old cache */
+ ef = eet_open(efreet_desktop_util_cache_file(), EET_FILE_MODE_READ);
+ if (ef)
+ {
+ old_file_ids = eet_data_read(ef, efreet_hash_string_edd(), "file_id");
+ eet_close(ef);
+ }
+
+ /* create cache */
+ snprintf(file, sizeof(file), "%s.XXXXXX", efreet_desktop_cache_file());
+ tmpfd = mkstemp(file);
+ if (tmpfd < 0) goto error;
+ close(tmpfd);
+ ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
+ if (!ef) goto error;
+
+ snprintf(util_file, sizeof(util_file), "%s.XXXXXX", efreet_desktop_util_cache_file());
+ tmpfd = mkstemp(util_file);
+ if (tmpfd < 0) goto error;
+ close(tmpfd);
+ util_ef = eet_open(util_file, EET_FILE_MODE_READ_WRITE);
+ if (!util_ef) goto error;
+
+ /* write cache version */
+ version.major = EFREET_DESKTOP_UTILS_CACHE_MAJOR;
+ version.minor = EFREET_DESKTOP_UTILS_CACHE_MINOR;
+ eet_data_write(util_ef, efreet_version_edd(), EFREET_CACHE_VERSION, &version, 1);
+ version.major = EFREET_DESKTOP_CACHE_MAJOR;
+ version.minor = EFREET_DESKTOP_CACHE_MINOR;
+ eet_data_write(ef, efreet_version_edd(), EFREET_CACHE_VERSION, &version, 1);
+
+ desktops = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_desktop_free));
+
+ file_ids = eina_hash_string_superfast_new(NULL);
+ paths = eina_hash_string_superfast_new(NULL);
+
+ mime_types = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+ categories = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+ startup_wm_class = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+ name = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+ generic_name = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+ comment = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+ exec = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
+
+ dirs = efreet_default_dirs_get(efreet_data_home_get(), efreet_data_dirs_get(),
+ "applications");
+ if (!dirs) goto error;
+
+ EINA_LIST_FREE(dirs, path)
+ {
+ char file_id[PATH_MAX] = { '\0' };
+
+ if (!cache_scan(path, file_id, priority++, 1, &changed)) goto error;
+ systemdirs = eina_list_append(systemdirs, path);
+ }
+
+ EINA_LIST_FOREACH(extra_dirs, l, path)
+ if (!cache_scan(path, NULL, priority, 0, &changed)) goto error;
+
+ /* store util */
+#define STORE_HASH_ARRAY(_hash) \
+ if (eina_hash_population((_hash)) > 0) \
+ { \
+ Eina_Iterator *it; \
+ Efreet_Cache_Array_String array; \
+ const char *str; \
+ \
+ hash.hash = (_hash); \
+ eet_data_write(util_ef, efreet_hash_array_string_edd(), #_hash "_hash", &hash, 1); \
+ array.array_count = 0; \
+ array.array = malloc(eina_hash_population(hash.hash) * sizeof(char *)); \
+ it = eina_hash_iterator_key_new(hash.hash); \
+ EINA_ITERATOR_FOREACH(it, str) \
+ array.array[array.array_count++] = str; \
+ eina_iterator_free(it); \
+ eet_data_write(util_ef, efreet_array_string_edd(), #_hash "_list", &array, 1); \
+ free(array.array); \
+ }
+ STORE_HASH_ARRAY(mime_types);
+ STORE_HASH_ARRAY(categories);
+ STORE_HASH_ARRAY(startup_wm_class);
+ STORE_HASH_ARRAY(name);
+ STORE_HASH_ARRAY(generic_name);
+ STORE_HASH_ARRAY(comment);
+ STORE_HASH_ARRAY(exec);
+ if (eina_hash_population(file_ids) > 0)
+ {
+ hash.hash = file_ids;
+ eet_data_write(util_ef, efreet_hash_string_edd(), "file_id", &hash, 1);
+ }
+
+ eina_hash_free(mime_types);
+ eina_hash_free(categories);
+ eina_hash_free(startup_wm_class);
+ eina_hash_free(name);
+ eina_hash_free(generic_name);
+ eina_hash_free(comment);
+ eina_hash_free(exec);
+
+ if (old_file_ids)
+ {
+ eina_hash_free(old_file_ids->hash);
+ free(old_file_ids);
+ }
+
+ eina_hash_free(file_ids);
+ eina_hash_free(paths);
+
+ eina_hash_free(desktops);
+
+ /* check if old and new caches contain the same number of entries */
+ if (!changed)
+ {
+ Eet_File *old;
+
+ old = eet_open(efreet_desktop_cache_file(), EET_FILE_MODE_READ);
+ if (!old || eet_num_entries(old) != eet_num_entries(ef)) changed = 1;
+ if (old) eet_close(old);
+ old = eet_open(efreet_desktop_util_cache_file(), EET_FILE_MODE_READ);
+ if (!old || eet_num_entries(old) != eet_num_entries(util_ef)) changed = 1;
+ if (old) eet_close(old);
+ }
+
+ /* cleanup */
+ eet_close(util_ef);
+ eet_close(ef);
+
+ /* unlink old cache files */
+ if (changed)
+ {
+ if (unlink(efreet_desktop_cache_file()) < 0)
+ {
+ if (errno != ENOENT) goto error;
+ }
+ if (unlink(efreet_desktop_util_cache_file()) < 0)
+ {
+ if (errno != ENOENT) goto error;
+ }
+ /* rename tmp files to real files */
+ if (rename(util_file, efreet_desktop_util_cache_file()) < 0) goto error;
+ efreet_setowner(efreet_desktop_util_cache_file());
+ if (rename(file, efreet_desktop_cache_file()) < 0) goto error;
+ efreet_setowner(efreet_desktop_cache_file());
+ }
+ else
+ {
+ unlink(util_file);
+ unlink(file);
+ }
+
+ {
+ char c = 'n';
+
+ if (changed) c = 'c';
+ printf("%c\n", c);
+ }
+
+ EINA_LIST_FREE(systemdirs, dir)
+ eina_stringshare_del(dir);
+ eina_list_free(extra_dirs);
+ efreet_shutdown();
+ ecore_shutdown();
+ eet_shutdown();
+ eina_log_domain_unregister(_efreet_desktop_cache_log_dom);
+ eina_shutdown();
+ close(lockfd);
+ return 0;
+error:
+ IF_FREE(dir);
+edd_error:
+ if (old_file_ids)
+ {
+ eina_hash_free(old_file_ids->hash);
+ free(old_file_ids);
+ }
+ efreet_shutdown();
+efreet_error:
+ ecore_shutdown();
+ecore_error:
+ eet_shutdown();
+eet_error:
+ EINA_LIST_FREE(systemdirs, dir)
+ eina_stringshare_del(dir);
+ eina_list_free(extra_dirs);
+ eina_log_domain_unregister(_efreet_desktop_cache_log_dom);
+ eina_shutdown();
+eina_error:
+ if (lockfd >= 0) close(lockfd);
+ return 1;
+}
diff --git a/src/bin/efreet/efreet_icon_cache_create.c b/src/bin/efreet/efreet_icon_cache_create.c
new file mode 100644
index 0000000000..662dd09469
--- /dev/null
+++ b/src/bin/efreet/efreet_icon_cache_create.c
@@ -0,0 +1,1070 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "efreet_alloca.h"
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <Eina.h>
+#include <Eet.h>
+#include <Ecore.h>
+#include <Ecore_File.h>
+
+#define EFREET_MODULE_LOG_DOM _efreet_icon_cache_log_dom
+static int _efreet_icon_cache_log_dom = -1;
+
+#include "Efreet.h"
+#include "efreet_private.h"
+#include "efreet_cache_private.h"
+
+static Eina_Array *exts = NULL;
+static Eina_Array *extra_dirs = NULL;
+static Eina_Array *strs = NULL;
+static Eina_Hash *icon_themes = NULL;
+
+static Eina_Bool
+cache_directory_modified(Eina_Hash *dirs, const char *dir)
+{
+ Efreet_Cache_Directory *dcache;
+ struct stat st;
+
+ if (!dirs) return EINA_TRUE;
+
+ if (stat(dir, &st) < 0) return EINA_FALSE;
+ dcache = eina_hash_find(dirs, dir);
+ if (!dcache)
+ {
+ dcache = malloc(sizeof (Efreet_Cache_Directory));
+ if (!dcache) return EINA_TRUE;
+
+ dcache->modified_time = (long long) st.st_mtime;
+ eina_hash_add(dirs, dir, dcache);
+ }
+ else if (dcache->modified_time == (long long) st.st_mtime) return EINA_FALSE;
+ dcache->modified_time = st.st_mtime;
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+cache_extension_lookup(const char *ext)
+{
+ unsigned int i;
+
+ for (i = 0; i < exts->count; ++i)
+ if (!strcmp(exts->data[i], ext))
+ return EINA_TRUE;
+ return EINA_FALSE;
+}
+
+static Eina_Bool
+cache_fallback_scan_dir(Eina_Hash *icons, Eina_Hash *dirs, const char *dir)
+{
+ Eina_Iterator *it;
+ Eina_File_Direct_Info *entry;
+
+ if (!cache_directory_modified(dirs, dir)) return EINA_TRUE;
+
+ it = eina_file_stat_ls(dir);
+ if (!it) return EINA_TRUE;
+
+ EINA_ITERATOR_FOREACH(it, entry)
+ {
+ Efreet_Cache_Fallback_Icon *icon;
+ char *name;
+ char *ext;
+ unsigned int i;
+
+ if (entry->type == EINA_FILE_DIR)
+ continue;
+
+ ext = strrchr(entry->path + entry->name_start, '.');
+ if (!ext || !cache_extension_lookup(ext))
+ continue;
+
+ /* icon with known extension */
+ name = entry->path + entry->name_start;
+ *ext = '\0';
+
+ icon = eina_hash_find(icons, name);
+ if (!icon)
+ {
+ icon = NEW(Efreet_Cache_Fallback_Icon, 1);
+ icon->theme = NULL;
+ eina_hash_add(icons, name, icon);
+ }
+
+ *ext = '.';
+
+ for (i = 0; i < icon->icons_count; ++i)
+ if (!strcmp(icon->icons[i], entry->path))
+ break;
+
+ if (i != icon->icons_count)
+ continue;
+
+ icon->icons = realloc(icon->icons, sizeof (char *) * (icon->icons_count + 1));
+ icon->icons[icon->icons_count] = eina_stringshare_add(entry->path);
+ eina_array_push(strs, icon->icons[icon->icons_count++]);
+ }
+
+ eina_iterator_free(it);
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+cache_fallback_scan(Eina_Hash *icons, Eina_Hash *dirs)
+{
+ unsigned int i;
+ Eina_List *xdg_dirs, *l;
+ const char *dir;
+ char path[PATH_MAX];
+
+ for (i = 0; i < extra_dirs->count; i++)
+ cache_fallback_scan_dir(icons, dirs, extra_dirs->data[i]);
+
+ cache_fallback_scan_dir(icons, dirs, efreet_icon_deprecated_user_dir_get());
+ cache_fallback_scan_dir(icons, dirs, efreet_icon_user_dir_get());
+
+ xdg_dirs = efreet_data_dirs_get();
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(path, sizeof(path), "%s/icons", dir);
+ cache_fallback_scan_dir(icons, dirs, path);
+ }
+
+#ifndef STRICT_SPEC
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(path, sizeof(path), "%s/pixmaps", dir);
+ cache_fallback_scan_dir(icons, dirs, path);
+ }
+#endif
+
+ cache_fallback_scan_dir(icons, dirs, "/usr/share/pixmaps");
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+check_fallback_changed(Efreet_Cache_Icon_Theme *theme)
+{
+ unsigned int i;
+ Eina_List *xdg_dirs, *l;
+ const char *dir;
+ char path[PATH_MAX];
+
+ /* Check if the dirs we have cached are changed */
+ if (theme->dirs)
+ {
+ Eina_Iterator *it;
+ Eina_Bool changed = EINA_FALSE;
+
+ it = eina_hash_iterator_key_new(theme->dirs);
+ EINA_ITERATOR_FOREACH(it, dir)
+ {
+ changed = !ecore_file_exists(dir);
+ if (changed) break;
+ changed = cache_directory_modified(theme->dirs, dir);
+ if (changed) break;
+ }
+ eina_iterator_free(it);
+ if (changed) return EINA_TRUE;
+ }
+
+ /* Check if spec dirs have changed */
+ for (i = 0; i < extra_dirs->count; i++)
+ if (cache_directory_modified(theme->dirs, extra_dirs->data[i])) return EINA_TRUE;
+
+ if (cache_directory_modified(theme->dirs, efreet_icon_deprecated_user_dir_get())) return EINA_TRUE;
+ if (cache_directory_modified(theme->dirs, efreet_icon_user_dir_get())) return EINA_TRUE;
+
+ xdg_dirs = efreet_data_dirs_get();
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(path, sizeof(path), "%s/icons", dir);
+ if (cache_directory_modified(theme->dirs, path)) return EINA_TRUE;
+ }
+
+#ifndef STRICT_SPEC
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(path, sizeof(path), "%s/pixmaps", dir);
+ if (cache_directory_modified(theme->dirs, path)) return EINA_TRUE;
+ }
+#endif
+
+ if (cache_directory_modified(theme->dirs, "/usr/share/pixmaps")) return EINA_TRUE;
+ return EINA_FALSE;
+}
+
+static Eina_Bool
+cache_scan_path_dir(Efreet_Icon_Theme *theme,
+ const char *path,
+ Efreet_Icon_Theme_Directory *dir,
+ Eina_Hash *icons)
+{
+ Eina_Iterator *it;
+ char buf[PATH_MAX];
+ Eina_File_Direct_Info *entry;
+
+ snprintf(buf, sizeof(buf), "%s/%s", path, dir->name);
+
+ it = eina_file_stat_ls(buf);
+ if (!it) return EINA_TRUE;
+
+ EINA_ITERATOR_FOREACH(it, entry)
+ {
+ Efreet_Cache_Icon *icon;
+ char *name;
+ char *ext;
+ unsigned int i;
+
+ if (entry->type == EINA_FILE_DIR)
+ continue;
+
+ ext = strrchr(entry->path + entry->name_start, '.');
+ if (!ext || !cache_extension_lookup(ext))
+ continue;
+
+ /* icon with known extension */
+ name = entry->path + entry->name_start;
+ *ext = '\0';
+
+ icon = eina_hash_find(icons, name);
+ if (!icon)
+ {
+ icon = NEW(Efreet_Cache_Icon, 1);
+ icon->theme = eina_stringshare_add(theme->name.internal);
+ eina_array_push(strs, icon->theme);
+ eina_hash_add(icons, name, icon);
+ }
+
+ /* find if we have the same icon in another type */
+ for (i = 0; i < icon->icons_count; ++i)
+ {
+ if ((icon->icons[i]->type == dir->type) &&
+ (icon->icons[i]->normal == dir->size.normal) &&
+ (icon->icons[i]->max == dir->size.max) &&
+ (icon->icons[i]->min == dir->size.min))
+ break;
+ }
+
+ *ext = '.';
+
+ if (i != icon->icons_count)
+ {
+ unsigned int j;
+
+ /* check if the path already exist */
+ for (j = 0; j < icon->icons[i]->paths_count; ++j)
+ if (!strcmp(icon->icons[i]->paths[j], entry->path))
+ break;
+
+ if (j != icon->icons[i]->paths_count)
+ continue;
+
+ /* If we are inherited, check if we already have extension */
+ if (strcmp(icon->theme, theme->name.internal))
+ {
+ const char *ext2;
+ int has_ext = 0;
+ for (j = 0; j < icon->icons[i]->paths_count; ++j)
+ {
+ ext2 = strrchr(icon->icons[i]->paths[j], '.');
+ if (ext2)
+ {
+ ext2++;
+ has_ext = !strcmp((ext + 1), ext2);
+ if (has_ext) break;
+ }
+ }
+ if (has_ext)
+ continue;
+ }
+ }
+ /* no icon match so add a new one */
+ /* only allow to add new icon for main theme
+ * if we allow inherited theme to add new icons,
+ * we will get weird effects when icon scales
+ */
+ else if (!strcmp(icon->theme, theme->name.internal))
+ {
+ icon->icons = realloc(icon->icons,
+ sizeof (Efreet_Cache_Icon_Element*) * (++icon->icons_count));
+ icon->icons[i] = NEW(Efreet_Cache_Icon_Element, 1);
+ icon->icons[i]->type = dir->type;
+ icon->icons[i]->normal = dir->size.normal;
+ icon->icons[i]->min = dir->size.min;
+ icon->icons[i]->max = dir->size.max;
+ icon->icons[i]->paths = NULL;
+ icon->icons[i]->paths_count = 0;
+ }
+ else
+ {
+ continue;
+ }
+
+ /* and finally store the path */
+ icon->icons[i]->paths = realloc(icon->icons[i]->paths,
+ sizeof (char*) * (icon->icons[i]->paths_count + 1));
+ icon->icons[i]->paths[icon->icons[i]->paths_count] = eina_stringshare_add(entry->path);
+ eina_array_push(strs, icon->icons[i]->paths[icon->icons[i]->paths_count++]);
+ }
+
+ eina_iterator_free(it);
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+cache_scan_path(Efreet_Icon_Theme *theme, Eina_Hash *icons, const char *path)
+{
+ Eina_List *l;
+ Efreet_Icon_Theme_Directory *dir;
+
+ EINA_LIST_FOREACH(theme->directories, l, dir)
+ if (!cache_scan_path_dir(theme, path, dir, icons)) return EINA_FALSE;
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+cache_scan(Efreet_Icon_Theme *theme, Eina_Hash *themes, Eina_Hash *icons)
+{
+ Eina_List *l;
+ const char *path;
+ const char *name;
+
+ if (!theme) return EINA_TRUE;
+ if (eina_hash_find(themes, theme->name.internal)) return EINA_TRUE;
+ eina_hash_direct_add(themes, theme->name.internal, theme);
+
+ /* scan theme */
+ EINA_LIST_FOREACH(theme->paths, l, path)
+ if (!cache_scan_path(theme, icons, path)) return EINA_FALSE;
+
+ /* scan inherits */
+ if (theme->inherits)
+ {
+ EINA_LIST_FOREACH(theme->inherits, l, name)
+ {
+ Efreet_Icon_Theme *inherit;
+
+ inherit = eina_hash_find(icon_themes, name);
+ if (!inherit)
+ INF("Theme `%s` not found for `%s`.",
+ name, theme->name.internal);
+ if (!cache_scan(inherit, themes, icons)) return EINA_FALSE;
+ }
+ }
+ else if (strcmp(theme->name.internal, "hicolor"))
+ {
+ theme = eina_hash_find(icon_themes, "hicolor");
+ if (!cache_scan(theme, themes, icons)) return EINA_FALSE;
+ }
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+check_changed(Efreet_Cache_Icon_Theme *theme)
+{
+ Eina_List *l;
+ const char *name;
+
+ if (!theme) return EINA_FALSE;
+
+ if (theme->changed) return EINA_TRUE;
+ if (theme->theme.inherits)
+ {
+ EINA_LIST_FOREACH(theme->theme.inherits, l, name)
+ {
+ Efreet_Cache_Icon_Theme *inherit;
+
+ inherit = eina_hash_find(icon_themes, name);
+ if (!inherit)
+ INF("Theme `%s` not found for `%s`.",
+ name, theme->theme.name.internal);
+ if (check_changed(inherit)) return EINA_TRUE;
+ }
+ }
+ else if (strcmp(theme->theme.name.internal, "hicolor"))
+ {
+ theme = eina_hash_find(icon_themes, "hicolor");
+ if (check_changed(theme)) return EINA_TRUE;
+ }
+ return EINA_FALSE;
+}
+
+static Efreet_Icon_Theme_Directory *
+icon_theme_directory_new(Efreet_Ini *ini, const char *name)
+{
+ Efreet_Icon_Theme_Directory *dir;
+ int val;
+ const char *tmp;
+
+ if (!ini) return NULL;
+
+ dir = NEW(Efreet_Icon_Theme_Directory, 1);
+ if (!dir) return NULL;
+ dir->name = eina_stringshare_add(name);
+ eina_array_push(strs, dir->name);
+
+ efreet_ini_section_set(ini, name);
+
+ tmp = efreet_ini_string_get(ini, "Context");
+ if (tmp)
+ {
+ if (!strcasecmp(tmp, "Actions"))
+ dir->context = EFREET_ICON_THEME_CONTEXT_ACTIONS;
+
+ else if (!strcasecmp(tmp, "Devices"))
+ dir->context = EFREET_ICON_THEME_CONTEXT_DEVICES;
+
+ else if (!strcasecmp(tmp, "FileSystems"))
+ dir->context = EFREET_ICON_THEME_CONTEXT_FILESYSTEMS;
+
+ else if (!strcasecmp(tmp, "MimeTypes"))
+ dir->context = EFREET_ICON_THEME_CONTEXT_MIMETYPES;
+ }
+
+ /* Threshold is fallback */
+ dir->type = EFREET_ICON_SIZE_TYPE_THRESHOLD;
+
+ tmp = efreet_ini_string_get(ini, "Type");
+ if (tmp)
+ {
+ if (!strcasecmp(tmp, "Fixed"))
+ dir->type = EFREET_ICON_SIZE_TYPE_FIXED;
+
+ else if (!strcasecmp(tmp, "Scalable"))
+ dir->type = EFREET_ICON_SIZE_TYPE_SCALABLE;
+ }
+
+ dir->size.normal = efreet_ini_int_get(ini, "Size");
+
+ if (dir->type == EFREET_ICON_SIZE_TYPE_THRESHOLD)
+ {
+ val = efreet_ini_int_get(ini, "Threshold");
+ if (val < 0) val = 2;
+ dir->size.max = dir->size.normal + val;
+ dir->size.min = dir->size.normal - val;
+ }
+ else if (dir->type == EFREET_ICON_SIZE_TYPE_SCALABLE)
+ {
+ val = efreet_ini_int_get(ini, "MinSize");
+ if (val < 0) dir->size.min = dir->size.normal;
+ else dir->size.min = val;
+
+ val = efreet_ini_int_get(ini, "MaxSize");
+ if (val < 0) dir->size.max = dir->size.normal;
+ else dir->size.max = val;
+ }
+
+ return dir;
+}
+
+static Eina_Bool
+icon_theme_index_read(Efreet_Cache_Icon_Theme *theme, const char *path)
+{
+ Efreet_Ini *ini;
+ Efreet_Icon_Theme_Directory *dir;
+ const char *tmp;
+ struct stat st;
+
+ if (!theme || !path) return EINA_FALSE;
+
+ if (stat(path, &st) < 0) return EINA_FALSE;
+ if (theme->path && !strcmp(theme->path, path) && theme->last_cache_check >= (long long) st.st_mtime)
+ {
+ /* no change */
+ theme->valid = 1;
+ return EINA_TRUE;
+ }
+ if (!theme->path || strcmp(theme->path, path))
+ {
+ theme->path = eina_stringshare_add(path);
+ eina_array_push(strs, theme->path);
+ }
+ if ((long long) st.st_mtime > theme->last_cache_check)
+ theme->last_cache_check = (long long) st.st_mtime;
+ theme->changed = 1;
+
+ ini = efreet_ini_new(path);
+ if (!ini) return EINA_FALSE;
+ if (!ini->data)
+ {
+ efreet_ini_free(ini);
+ return EINA_FALSE;
+ }
+
+ efreet_ini_section_set(ini, "Icon Theme");
+ tmp = efreet_ini_localestring_get(ini, "Name");
+ if (tmp)
+ {
+ theme->theme.name.name = eina_stringshare_add(tmp);
+ eina_array_push(strs, theme->theme.name.name);
+ }
+
+ tmp = efreet_ini_localestring_get(ini, "Comment");
+ if (tmp)
+ {
+ theme->theme.comment = eina_stringshare_add(tmp);
+ eina_array_push(strs, theme->theme.comment);
+ }
+
+ tmp = efreet_ini_string_get(ini, "Example");
+ if (tmp)
+ {
+ theme->theme.example_icon = eina_stringshare_add(tmp);
+ eina_array_push(strs, theme->theme.example_icon);
+ }
+
+ theme->hidden = efreet_ini_boolean_get(ini, "Hidden");
+
+ theme->valid = 1;
+
+ /* Check the inheritance. If there is none we inherit from the hicolor theme */
+ tmp = efreet_ini_string_get(ini, "Inherits");
+ if (tmp)
+ {
+ char *t, *s, *p;
+ const char *i;
+ size_t len;
+
+ len = strlen(tmp) + 1;
+ t = alloca(len);
+ memcpy(t, tmp, len);
+ s = t;
+ p = strchr(s, ',');
+
+ while (p)
+ {
+ *p = '\0';
+
+ i = eina_stringshare_add(s);
+ theme->theme.inherits = eina_list_append(theme->theme.inherits, i);
+ eina_array_push(strs, i);
+ s = ++p;
+ p = strchr(s, ',');
+ }
+ i = eina_stringshare_add(s);
+ theme->theme.inherits = eina_list_append(theme->theme.inherits, i);
+ eina_array_push(strs, i);
+ }
+
+ /* make sure this one is done last as setting the directory will change
+ * the ini section ... */
+ tmp = efreet_ini_string_get(ini, "Directories");
+ if (tmp)
+ {
+ char *t, *s, *p;
+ size_t len;
+
+ len = strlen(tmp) + 1;
+ t = alloca(len);
+ memcpy(t, tmp, len);
+ s = t;
+ p = s;
+
+ while ((p) && (*s))
+ {
+ p = strchr(s, ',');
+
+ if (p) *p = '\0';
+
+ dir = icon_theme_directory_new(ini, s);
+ if (!dir) goto error;
+ theme->theme.directories = eina_list_append(theme->theme.directories, dir);
+
+ if (p) s = ++p;
+ }
+ }
+
+error:
+ efreet_ini_free(ini);
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+cache_theme_scan(const char *dir)
+{
+ Eina_Iterator *it;
+ Eina_File_Direct_Info *entry;
+
+ it = eina_file_stat_ls(dir);
+ if (!it) return EINA_TRUE;
+
+ EINA_ITERATOR_FOREACH(it, entry)
+ {
+ Efreet_Cache_Icon_Theme *theme;
+ const char *name;
+ const char *path;
+ char buf[PATH_MAX];
+ struct stat st;
+
+ if (stat(entry->path, &st) < 0) continue;
+
+ if ((entry->type != EINA_FILE_DIR) &&
+ (entry->type != EINA_FILE_LNK))
+ continue;
+
+ name = entry->path + entry->name_start;
+ theme = eina_hash_find(icon_themes, name);
+
+ if (!theme)
+ {
+ theme = NEW(Efreet_Cache_Icon_Theme, 1);
+ theme->theme.name.internal = eina_stringshare_add(name);
+ eina_array_push(strs, theme->theme.name.internal);
+ eina_hash_direct_add(icon_themes,
+ (void *)theme->theme.name.internal, theme);
+ theme->changed = 1;
+ }
+ if ((long long) st.st_mtime > theme->last_cache_check)
+ {
+ theme->last_cache_check = (long long) st.st_mtime;
+ theme->changed = 1;
+ }
+
+ /* TODO: We need to handle change in order of included paths */
+ if (!eina_list_search_unsorted(theme->theme.paths, EINA_COMPARE_CB(strcmp), entry->path))
+ {
+ path = eina_stringshare_add(entry->path);
+ theme->theme.paths = eina_list_append(theme->theme.paths, path);
+ eina_array_push(strs, path);
+ theme->changed = 1;
+ }
+
+ /* we're already valid so no reason to check for an index.theme file */
+ if (theme->valid) continue;
+
+ /* if the index.theme file exists we parse it into the theme */
+ memcpy(buf, entry->path, entry->path_length);
+ memcpy(buf + entry->path_length, "/index.theme", sizeof("/index.theme"));
+ if (ecore_file_exists(buf))
+ {
+ if (!icon_theme_index_read(theme, buf))
+ theme->valid = 0;
+ }
+ }
+ eina_iterator_free(it);
+ return EINA_TRUE;
+}
+
+static int
+cache_lock_file(void)
+{
+ char file[PATH_MAX];
+ struct flock fl;
+ int lockfd;
+
+ snprintf(file, sizeof(file), "%s/efreet/icon_data.lock", efreet_cache_home_get());
+ lockfd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+ if (lockfd < 0) return -1;
+ efreet_fsetowner(lockfd);
+
+ memset(&fl, 0, sizeof(struct flock));
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ if (fcntl(lockfd, F_SETLK, &fl) < 0)
+ {
+ WRN("LOCKED! You may want to delete %s if this persists", file);
+ close(lockfd);
+ return -1;
+ }
+
+ return lockfd;
+}
+
+static void
+icon_theme_free(Efreet_Cache_Icon_Theme *theme)
+{
+ void *data;
+
+ eina_list_free(theme->theme.paths);
+ eina_list_free(theme->theme.inherits);
+ EINA_LIST_FREE(theme->theme.directories, data)
+ free(data);
+ if (theme->dirs) efreet_hash_free(theme->dirs, free);
+ free(theme);
+}
+
+int
+main(int argc, char **argv)
+{
+ /* TODO:
+ * - Add file monitor on files, so that we catch changes on files
+ * during whilst this program runs.
+ * - Maybe linger for a while to reduce number of cache re-creates.
+ */
+ Eina_Iterator *it;
+ Efreet_Cache_Version *icon_version;
+ Efreet_Cache_Version *theme_version;
+ Efreet_Cache_Icon_Theme *theme;
+ Eet_Data_Descriptor *theme_edd;
+ Eet_Data_Descriptor *icon_edd;
+ Eet_Data_Descriptor *fallback_edd;
+ Eet_File *icon_ef;
+ Eet_File *theme_ef;
+ Eina_List *xdg_dirs = NULL;
+ Eina_List *l = NULL;
+ char file[PATH_MAX];
+ const char *path;
+ char *dir = NULL;
+ Eina_Bool changed = EINA_FALSE;
+ Eina_Bool flush = EINA_FALSE;
+ int lockfd = -1;
+ char **keys;
+ int num, i;
+
+ /* init external subsystems */
+ if (!eina_init()) return -1;
+ _efreet_icon_cache_log_dom =
+ eina_log_domain_register("efreet_icon_cache", EFREET_DEFAULT_LOG_COLOR);
+ if (_efreet_icon_cache_log_dom < 0)
+ {
+ EINA_LOG_ERR("Efreet: Could not create a log domain for efreet_icon_cache.");
+ return -1;
+ }
+
+ eina_log_domain_level_set("efreet_icon_cache", EINA_LOG_LEVEL_ERR);
+
+ exts = eina_array_new(10);
+ extra_dirs = eina_array_new(10);
+
+ for (i = 1; i < argc; i++)
+ {
+ if (!strcmp(argv[i], "-v"))
+ eina_log_domain_level_set("efreet_icon_cache", EINA_LOG_LEVEL_DBG);
+ else if ((!strcmp(argv[i], "-h")) ||
+ (!strcmp(argv[i], "-help")) ||
+ (!strcmp(argv[i], "--h")) ||
+ (!strcmp(argv[i], "--help")))
+ {
+ printf("Options:\n");
+ printf(" -v Verbose mode\n");
+ printf(" -e .ext1 .ext2 Extensions\n");
+ printf(" -d dir1 dir2 Extra dirs\n");
+ printf(" -f Flush\n");
+ exit(0);
+ }
+ else if (!strcmp(argv[i], "-e"))
+ {
+ while ((i < (argc - 1)) && (argv[(i + 1)][0] != '-'))
+ eina_array_push(exts, argv[++i]);
+ }
+ else if (!strcmp(argv[i], "-d"))
+ {
+ while ((i < (argc - 1)) && (argv[(i + 1)][0] != '-'))
+ eina_array_push(extra_dirs, argv[++i]);
+ }
+ else if (!strcmp(argv[i], "-d"))
+ flush = EINA_TRUE;
+ }
+
+ if (!eet_init()) return -1;
+ if (!ecore_init()) return -1;
+
+ efreet_cache_update = 0;
+ /* finish efreet init */
+ if (!efreet_init()) goto on_error;
+
+ strs = eina_array_new(32);
+
+ /* create homedir */
+ snprintf(file, sizeof(file), "%s/efreet", efreet_cache_home_get());
+ if (!ecore_file_exists(file))
+ {
+ if (!ecore_file_mkpath(file)) return -1;
+ efreet_setowner(file);
+ }
+
+ /* lock process, so that we only run one copy of this program */
+ lockfd = cache_lock_file();
+ if (lockfd == -1) goto on_error;
+
+ /* Need to init edd's, so they are like we want, not like userspace wants */
+ icon_edd = efreet_icon_edd();
+ fallback_edd = efreet_icon_fallback_edd();
+ theme_edd = efreet_icon_theme_edd(EINA_TRUE);
+
+ icon_themes = eina_hash_string_superfast_new(EINA_FREE_CB(icon_theme_free));
+
+ INF("opening theme cache");
+ /* open theme file */
+ theme_ef = eet_open(efreet_icon_theme_cache_file(), EET_FILE_MODE_READ_WRITE);
+ if (!theme_ef) goto on_error_efreet;
+ theme_version = eet_data_read(theme_ef, efreet_version_edd(), EFREET_CACHE_VERSION);
+ if (theme_version &&
+ ((theme_version->major != EFREET_ICON_CACHE_MAJOR) ||
+ (theme_version->minor != EFREET_ICON_CACHE_MINOR)))
+ {
+ // delete old cache
+ eet_close(theme_ef);
+ if (unlink(efreet_icon_theme_cache_file()) < 0)
+ {
+ if (errno != ENOENT) goto on_error_efreet;
+ }
+ theme_ef = eet_open(efreet_icon_theme_cache_file(), EET_FILE_MODE_READ_WRITE);
+ if (!theme_ef) goto on_error_efreet;
+ }
+ if (!theme_version)
+ theme_version = NEW(Efreet_Cache_Version, 1);
+
+ theme_version->major = EFREET_ICON_CACHE_MAJOR;
+ theme_version->minor = EFREET_ICON_CACHE_MINOR;
+
+ if (flush)
+ changed = EINA_TRUE;
+
+ if (exts->count == 0)
+ {
+ ERR("Need to pass extensions to icon cache create process");
+ goto on_error_efreet;
+ }
+
+ keys = eet_list(theme_ef, "*", &num);
+ if (keys)
+ {
+ for (i = 0; i < num; i++)
+ {
+ if (!strncmp(keys[i], "__efreet", 8)) continue;
+ theme = eet_data_read(theme_ef, theme_edd, keys[i]);
+ if (theme)
+ {
+ theme->valid = 0;
+ eina_hash_direct_add(icon_themes, theme->theme.name.internal, theme);
+ }
+ }
+ free(keys);
+ }
+
+ INF("scan for themes");
+ /* scan themes */
+ cache_theme_scan(efreet_icon_deprecated_user_dir_get());
+ cache_theme_scan(efreet_icon_user_dir_get());
+
+ xdg_dirs = efreet_data_dirs_get();
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(file, sizeof(file), "%s/icons", dir);
+ cache_theme_scan(file);
+ }
+
+#ifndef STRICT_SPEC
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(file, sizeof(file), "%s/pixmaps", dir);
+ cache_theme_scan(file);
+ }
+#endif
+
+ cache_theme_scan("/usr/share/pixmaps");
+
+ /* scan icons */
+ it = eina_hash_iterator_data_new(icon_themes);
+ EINA_ITERATOR_FOREACH(it, theme)
+ {
+ if (!theme->valid) continue;
+#ifndef STRICT_SPEC
+ if (!theme->theme.name.name) continue;
+#endif
+ INF("scan theme %s", theme->theme.name.name);
+
+ theme->changed = check_changed(theme);
+ if (flush)
+ theme->changed = EINA_TRUE;
+
+ INF("open icon file");
+ /* open icon file */
+ icon_ef = eet_open(efreet_icon_cache_file(theme->theme.name.internal), EET_FILE_MODE_READ_WRITE);
+ if (!icon_ef) goto on_error_efreet;
+ icon_version = eet_data_read(icon_ef, efreet_version_edd(), EFREET_CACHE_VERSION);
+ if (theme->changed || (icon_version &&
+ ((icon_version->major != EFREET_ICON_CACHE_MAJOR) ||
+ (icon_version->minor != EFREET_ICON_CACHE_MINOR))))
+ {
+ // delete old cache
+ eet_close(icon_ef);
+ if (unlink(efreet_icon_cache_file(theme->theme.name.internal)) < 0)
+ {
+ if (errno != ENOENT) goto on_error_efreet;
+ }
+ icon_ef = eet_open(efreet_icon_cache_file(theme->theme.name.internal), EET_FILE_MODE_READ_WRITE);
+ if (!icon_ef) goto on_error_efreet;
+ theme->changed = EINA_TRUE;
+ }
+
+ if (theme->changed)
+ changed = EINA_TRUE;
+
+ if (!icon_version)
+ icon_version = NEW(Efreet_Cache_Version, 1);
+
+ icon_version->major = EFREET_ICON_CACHE_MAJOR;
+ icon_version->minor = EFREET_ICON_CACHE_MINOR;
+
+ if (theme->changed)
+ {
+ Eina_Hash *themes;
+ Eina_Hash *icons;
+
+ themes = eina_hash_string_superfast_new(NULL);
+ icons = eina_hash_string_superfast_new(NULL);
+
+ INF("scan icons\n");
+ if (cache_scan(&(theme->theme), themes, icons))
+ {
+ Eina_Iterator *icons_it;
+ Eina_Hash_Tuple *tuple;
+
+ INF("generated: '%s' %i (%i)",
+ theme->theme.name.internal,
+ theme->changed,
+ eina_hash_population(icons));
+
+ icons_it = eina_hash_iterator_tuple_new(icons);
+ EINA_ITERATOR_FOREACH(icons_it, tuple)
+ eet_data_write(icon_ef, icon_edd, tuple->key, tuple->data, 1);
+ eina_iterator_free(icons_it);
+
+ INF("theme change: %s %lld", theme->theme.name.internal, theme->last_cache_check);
+ eet_data_write(theme_ef, theme_edd, theme->theme.name.internal, theme, 1);
+ }
+ eina_hash_free(themes);
+ eina_hash_free(icons);
+ changed = EINA_TRUE;
+ }
+
+ eet_data_write(icon_ef, efreet_version_edd(), EFREET_CACHE_VERSION, icon_version, 1);
+ eet_close(icon_ef);
+ efreet_setowner(efreet_icon_cache_file(theme->theme.name.internal));
+ free(icon_version);
+ }
+ eina_iterator_free(it);
+
+ INF("scan fallback icons");
+ theme = eet_data_read(theme_ef, theme_edd, EFREET_CACHE_ICON_FALLBACK);
+ if (!theme)
+ {
+ theme = NEW(Efreet_Cache_Icon_Theme, 1);
+ theme->changed = EINA_TRUE;
+ }
+ if (flush)
+ theme->changed = EINA_TRUE;
+
+ INF("open fallback file");
+ /* open icon file */
+ icon_ef = eet_open(efreet_icon_cache_file(EFREET_CACHE_ICON_FALLBACK), EET_FILE_MODE_READ_WRITE);
+ if (!icon_ef) goto on_error_efreet;
+ icon_version = eet_data_read(icon_ef, efreet_version_edd(), EFREET_CACHE_VERSION);
+ if (theme->changed || (icon_version &&
+ ((icon_version->major != EFREET_ICON_CACHE_MAJOR) ||
+ (icon_version->minor != EFREET_ICON_CACHE_MINOR))))
+ {
+ // delete old cache
+ eet_close(icon_ef);
+ if (unlink(efreet_icon_cache_file(EFREET_CACHE_ICON_FALLBACK)) < 0)
+ {
+ if (errno != ENOENT) goto on_error_efreet;
+ }
+ icon_ef = eet_open(efreet_icon_cache_file(EFREET_CACHE_ICON_FALLBACK), EET_FILE_MODE_READ_WRITE);
+ if (!icon_ef) goto on_error_efreet;
+ theme->changed = EINA_TRUE;
+ }
+ if (!theme->changed)
+ theme->changed = check_fallback_changed(theme);
+ if (theme->changed && theme->dirs)
+ {
+ efreet_hash_free(theme->dirs, free);
+ theme->dirs = NULL;
+ }
+ if (!theme->dirs)
+ theme->dirs = eina_hash_string_superfast_new(NULL);
+
+ if (theme->changed)
+ changed = EINA_TRUE;
+
+ if (!icon_version)
+ icon_version = NEW(Efreet_Cache_Version, 1);
+
+ icon_version->major = EFREET_ICON_CACHE_MAJOR;
+ icon_version->minor = EFREET_ICON_CACHE_MINOR;
+
+ if (theme->changed)
+ {
+ Eina_Hash *icons;
+
+ icons = eina_hash_string_superfast_new(NULL);
+
+ INF("scan fallback icons");
+ /* Save fallback in the right part */
+ if (cache_fallback_scan(icons, theme->dirs))
+ {
+ Eina_Iterator *icons_it;
+ Eina_Hash_Tuple *tuple;
+
+ INF("generated: fallback %i (%i)", theme->changed, eina_hash_population(icons));
+
+ icons_it = eina_hash_iterator_tuple_new(icons);
+ EINA_ITERATOR_FOREACH(icons_it, tuple)
+ eet_data_write(icon_ef, fallback_edd, tuple->key, tuple->data, 1);
+ eina_iterator_free(icons_it);
+ }
+ eina_hash_free(icons);
+
+ eet_data_write(theme_ef, theme_edd, EFREET_CACHE_ICON_FALLBACK, theme, 1);
+ }
+
+ icon_theme_free(theme);
+
+ eet_data_write(icon_ef, efreet_version_edd(), EFREET_CACHE_VERSION, icon_version, 1);
+ eet_close(icon_ef);
+ efreet_setowner(efreet_icon_cache_file(EFREET_CACHE_ICON_FALLBACK));
+ free(icon_version);
+
+ eina_hash_free(icon_themes);
+
+ /* save data */
+ eet_data_write(theme_ef, efreet_version_edd(), EFREET_CACHE_VERSION, theme_version, 1);
+
+ eet_close(theme_ef);
+ efreet_setowner(efreet_icon_theme_cache_file());
+ free(theme_version);
+
+ {
+ char c = 'n';
+
+ if (changed) c = 'c';
+ printf("%c\n", c);
+ }
+
+ INF("done");
+on_error_efreet:
+ efreet_shutdown();
+ if (theme_ef) eet_close(theme_ef);
+
+on_error:
+ if (lockfd >= 0) close(lockfd);
+
+ while ((path = eina_array_pop(strs)))
+ eina_stringshare_del(path);
+ eina_array_free(strs);
+ eina_array_free(exts);
+ eina_array_free(extra_dirs);
+
+ ecore_shutdown();
+ eet_shutdown();
+ eina_log_domain_unregister(_efreet_icon_cache_log_dom);
+ eina_shutdown();
+
+ return 0;
+}
diff --git a/src/bin/efreet/efreetd.c b/src/bin/efreet/efreetd.c
new file mode 100644
index 0000000000..a4ae720a4e
--- /dev/null
+++ b/src/bin/efreet/efreetd.c
@@ -0,0 +1,59 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <Ecore.h>
+#include <Ecore_File.h>
+
+#include "efreetd.h"
+#include "efreetd_dbus.h"
+#include "efreetd_cache.h"
+
+int efreetd_log_dom = -1;
+
+void
+quit(void)
+{
+ ecore_main_loop_quit();
+}
+
+int
+main(void)
+{
+ if (!eina_init()) return 1;
+ efreetd_log_dom = eina_log_domain_register("efreetd", EFREETD_DEFAULT_LOG_COLOR);
+ if (efreetd_log_dom < 0)
+ {
+ EINA_LOG_ERR("Efreet: Could not create a log domain for efreetd.");
+ goto ecore_error;
+ }
+ if (!ecore_init()) goto ecore_error;
+ if (!ecore_file_init()) goto ecore_file_error;
+
+ if (!dbus_init()) goto dbus_error;
+ if (!cache_init()) goto cache_error;
+
+ ecore_main_loop_begin();
+
+ cache_shutdown();
+ dbus_shutdown();
+ ecore_file_shutdown();
+ ecore_shutdown();
+ eina_log_domain_unregister(efreetd_log_dom);
+ efreetd_log_dom = -1;
+ eina_shutdown();
+ return 0;
+
+cache_error:
+ dbus_shutdown();
+dbus_error:
+ ecore_file_shutdown();
+ecore_file_error:
+ ecore_shutdown();
+ecore_error:
+ if (efreetd_log_dom >= 0)
+ eina_log_domain_unregister(efreetd_log_dom);
+ efreetd_log_dom = -1;
+ eina_shutdown();
+ return 1;
+}
diff --git a/src/bin/efreet/efreetd.h b/src/bin/efreet/efreetd.h
new file mode 100644
index 0000000000..a6931e3282
--- /dev/null
+++ b/src/bin/efreet/efreetd.h
@@ -0,0 +1,34 @@
+#ifndef __EFREETD_H
+#define __EFREETD_H
+
+#ifdef EFREETD_DEFAULT_LOG_COLOR
+#undef EFREETD_DEFAULT_LOG_COLOR
+#endif
+#define EFREETD_DEFAULT_LOG_COLOR "\033[36m"
+
+extern int efreetd_log_dom;
+
+#ifdef CRITICAL
+#undef CRITICAL
+#endif
+#define CRITICAL(...) EINA_LOG_DOM_CRIT(efreetd_log_dom, __VA_ARGS__)
+#ifdef ERR
+#undef ERR
+#endif
+#define ERR(...) EINA_LOG_DOM_ERR(efreetd_log_dom, __VA_ARGS__)
+#ifdef DBG
+#undef DBG
+#endif
+#define DBG(...) EINA_LOG_DOM_DBG(efreetd_log_dom, __VA_ARGS__)
+#ifdef INF
+#undef INF
+#endif
+#define INF(...) EINA_LOG_DOM_INFO(efreetd_log_dom, __VA_ARGS__)
+#ifdef WRN
+#undef WRN
+#endif
+#define WRN(...) EINA_LOG_DOM_WARN(efreetd_log_dom, __VA_ARGS__)
+
+void quit(void);
+
+#endif
diff --git a/src/bin/efreet/efreetd_cache.c b/src/bin/efreet/efreetd_cache.c
new file mode 100644
index 0000000000..5af928bde0
--- /dev/null
+++ b/src/bin/efreet/efreetd_cache.c
@@ -0,0 +1,575 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <Eina.h>
+#include <Ecore.h>
+#include <Ecore_File.h>
+
+#include "efreetd.h"
+#include "efreetd_dbus.h"
+
+#include "Efreet.h"
+#define EFREET_MODULE_LOG_DOM efreetd_log_dom
+#include "efreet_private.h"
+#include "efreetd_cache.h"
+
+static Eina_Hash *change_monitors = NULL;
+
+static Ecore_Event_Handler *cache_exe_del_handler = NULL;
+static Ecore_Event_Handler *cache_exe_data_handler = NULL;
+static Ecore_Exe *icon_cache_exe = NULL;
+static Ecore_Exe *desktop_cache_exe = NULL;
+static Ecore_Timer *icon_cache_timer = NULL;
+static Ecore_Timer *desktop_cache_timer = NULL;
+
+static Eina_Bool desktop_exists = EINA_FALSE;
+
+static Eina_List *desktop_system_dirs = NULL;
+static Eina_List *desktop_extra_dirs = NULL;
+static Eina_List *icon_extra_dirs = NULL;
+static Eina_List *icon_exts = NULL;
+static Eina_Bool icon_flush = EINA_FALSE;
+
+static Eina_Bool desktop_queue = EINA_FALSE;
+static Eina_Bool icon_queue = EINA_FALSE;
+
+static void desktop_changes_monitor_add(const char *path);
+
+/* internal */
+static Eina_Bool
+icon_cache_update_cache_cb(void *data EINA_UNUSED)
+{
+ char file[PATH_MAX];
+ int prio;
+
+ icon_cache_timer = NULL;
+
+ if (icon_cache_exe)
+ {
+ icon_queue = EINA_TRUE;
+ return ECORE_CALLBACK_CANCEL;
+ }
+ icon_queue = EINA_FALSE;
+ if ((!icon_flush) && (!icon_exts)) return ECORE_CALLBACK_CANCEL;
+
+ /* TODO: Queue if already running */
+ prio = ecore_exe_run_priority_get();
+ ecore_exe_run_priority_set(19);
+ // XXX: use eina_prefix, not hard-coded prefixes
+ eina_strlcpy(file, PACKAGE_LIB_DIR "/efreet/efreet_icon_cache_create", sizeof(file));
+ if (icon_extra_dirs)
+ {
+ Eina_List *ll;
+ char *p;
+
+ eina_strlcat(file, " -d", sizeof(file));
+ EINA_LIST_FOREACH(icon_extra_dirs, ll, p)
+ {
+ eina_strlcat(file, " ", sizeof(file));
+ eina_strlcat(file, p, sizeof(file));
+ }
+ }
+ if (icon_exts)
+ {
+ Eina_List *ll;
+ char *p;
+
+ eina_strlcat(file, " -e", sizeof(file));
+ EINA_LIST_FOREACH(icon_exts, ll, p)
+ {
+ eina_strlcat(file, " ", sizeof(file));
+ eina_strlcat(file, p, sizeof(file));
+ }
+ }
+ if (icon_flush)
+ eina_strlcat(file, " -f", sizeof(file));
+ icon_flush = EINA_FALSE;
+ icon_cache_exe =
+ ecore_exe_pipe_run(file, ECORE_EXE_PIPE_READ|ECORE_EXE_PIPE_READ_LINE_BUFFERED, NULL);
+ ecore_exe_run_priority_set(prio);
+
+ return ECORE_CALLBACK_CANCEL;
+}
+
+static void
+cache_icon_update(Eina_Bool flush)
+{
+ if (icon_cache_timer)
+ ecore_timer_del(icon_cache_timer);
+ if (flush)
+ icon_flush = flush;
+ icon_cache_timer = ecore_timer_add(1.0, icon_cache_update_cache_cb, NULL);
+}
+
+static Eina_Bool
+desktop_cache_update_cache_cb(void *data EINA_UNUSED)
+{
+ char file[PATH_MAX];
+ int prio;
+
+ desktop_cache_timer = NULL;
+
+ if (desktop_cache_exe)
+ {
+ desktop_queue = EINA_TRUE;
+ return ECORE_CALLBACK_CANCEL;
+ }
+ desktop_queue = EINA_FALSE;
+ prio = ecore_exe_run_priority_get();
+ ecore_exe_run_priority_set(19);
+ // XXX: use eina_prefix, not hard-coded prefixes
+ eina_strlcpy(file, PACKAGE_LIB_DIR "/efreet/efreet_desktop_cache_create", sizeof(file));
+ if (desktop_extra_dirs)
+ {
+ Eina_List *ll;
+ const char *str;
+
+ eina_strlcat(file, " -d", sizeof(file));
+ EINA_LIST_FOREACH(desktop_extra_dirs, ll, str)
+ {
+ eina_strlcat(file, " ", sizeof(file));
+ eina_strlcat(file, str, sizeof(file));
+ }
+ }
+ INF("Run desktop cache creation: %s", file);
+ desktop_cache_exe =
+ ecore_exe_pipe_run(file, ECORE_EXE_PIPE_READ|ECORE_EXE_PIPE_READ_LINE_BUFFERED, NULL);
+ ecore_exe_run_priority_set(prio);
+
+ return ECORE_CALLBACK_CANCEL;
+}
+
+static Eina_Bool
+cache_exe_data_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
+{
+ Ecore_Exe_Event_Data *ev;
+
+ ev = event;
+ if (ev->exe == desktop_cache_exe)
+ {
+ Eina_Bool update = EINA_FALSE;
+
+ if ((ev->lines) && (*ev->lines->line == 'c'))
+ update = EINA_TRUE;
+
+ desktop_exists = EINA_TRUE;
+ send_signal_desktop_cache_update(update);
+ }
+ else if (ev->exe == icon_cache_exe)
+ {
+ Eina_Bool update = EINA_FALSE;
+
+ if ((ev->lines) && (*ev->lines->line == 'c'))
+ update = EINA_TRUE;
+
+ send_signal_icon_cache_update(update);
+ }
+ return ECORE_CALLBACK_RENEW;
+}
+
+static Eina_Bool
+cache_exe_del_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
+{
+ Ecore_Exe_Event_Del *ev;
+
+ ev = event;
+ if (ev->exe == desktop_cache_exe)
+ {
+ desktop_cache_exe = NULL;
+ if (desktop_queue) cache_desktop_update();
+ }
+ else if (ev->exe == icon_cache_exe)
+ {
+ icon_cache_exe = NULL;
+ if (icon_queue) cache_icon_update(EINA_FALSE);
+ }
+ return ECORE_CALLBACK_RENEW;
+}
+
+static void
+icon_changes_cb(void *data EINA_UNUSED, Ecore_File_Monitor *em EINA_UNUSED,
+ Ecore_File_Event event, const char *path)
+{
+ /* TODO: If we get a stale symlink, we need to rerun cache creation */
+ switch (event)
+ {
+ case ECORE_FILE_EVENT_NONE:
+ /* noop */
+ break;
+
+ case ECORE_FILE_EVENT_CREATED_FILE:
+ case ECORE_FILE_EVENT_DELETED_FILE:
+ case ECORE_FILE_EVENT_MODIFIED:
+ case ECORE_FILE_EVENT_CLOSED:
+ case ECORE_FILE_EVENT_DELETED_DIRECTORY:
+ case ECORE_FILE_EVENT_CREATED_DIRECTORY:
+ cache_icon_update(EINA_FALSE);
+ break;
+
+ case ECORE_FILE_EVENT_DELETED_SELF:
+ eina_hash_del_by_key(change_monitors, path);
+ cache_icon_update(EINA_FALSE);
+ break;
+ }
+}
+
+static void
+icon_changes_monitor_add(const char *path)
+{
+ Ecore_File_Monitor *mon;
+
+ if (eina_hash_find(change_monitors, path)) return;
+ /* TODO: Check for symlink and monitor the real path */
+ mon = ecore_file_monitor_add(path,
+ icon_changes_cb,
+ NULL);
+ if (mon)
+ eina_hash_add(change_monitors, path, mon);
+}
+
+static void
+icon_changes_listen_recursive(const char *path, Eina_Bool base)
+{
+ Eina_Iterator *it;
+ Eina_File_Direct_Info *info;
+
+ if ((!ecore_file_is_dir(path)) && (base))
+ {
+ // XXX: if it doesn't exist... walk the parent dirs back down
+ // to this path until we find one that doesn't exist, then
+ // monitor its parent, and treat it specially as it needs
+ // to look for JUST the creation of this specific child
+ // and when this child is created, replace this monitor with
+ // monitoring the next specific child dir down until we are
+ // monitoring the original path again.
+ }
+ icon_changes_monitor_add(path);
+ it = eina_file_stat_ls(path);
+ if (!it) return;
+ EINA_ITERATOR_FOREACH(it, info)
+ {
+ if (((info->type == EINA_FILE_LNK) && (ecore_file_is_dir(info->path))) ||
+ (info->type == EINA_FILE_DIR))
+ icon_changes_monitor_add(info->path);
+ }
+ eina_iterator_free(it);
+}
+
+static void
+icon_changes_listen(void)
+{
+ Eina_List *l;
+ Eina_List *xdg_dirs;
+ char buf[PATH_MAX];
+ const char *dir;
+
+ icon_changes_listen_recursive(efreet_icon_deprecated_user_dir_get(), EINA_TRUE);
+ icon_changes_listen_recursive(efreet_icon_user_dir_get(), EINA_TRUE);
+ EINA_LIST_FOREACH(icon_extra_dirs, l, dir)
+ {
+ icon_changes_listen_recursive(dir, EINA_TRUE);
+ }
+
+ xdg_dirs = efreet_data_dirs_get();
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(buf, sizeof(buf), "%s/icons", dir);
+ icon_changes_listen_recursive(buf, EINA_TRUE);
+ }
+
+#ifndef STRICT_SPEC
+ EINA_LIST_FOREACH(xdg_dirs, l, dir)
+ {
+ snprintf(buf, sizeof(buf), "%s/pixmaps", dir);
+ icon_changes_listen_recursive(buf, EINA_TRUE);
+ }
+#endif
+
+ icon_changes_monitor_add("/usr/share/pixmaps");
+}
+
+static void
+desktop_changes_cb(void *data EINA_UNUSED, Ecore_File_Monitor *em EINA_UNUSED,
+ Ecore_File_Event event, const char *path)
+{
+ const char *ext;
+
+ /* TODO: If we get a stale symlink, we need to rerun cache creation */
+ /* TODO: Check for desktop*.cache, as this will be created when app is installed */
+ /* TODO: Do efreet_cache_icon_update() when app is installed, as it has the same
+ * symlink problem */
+ switch (event)
+ {
+ case ECORE_FILE_EVENT_NONE:
+ /* noop */
+ break;
+
+ case ECORE_FILE_EVENT_CREATED_FILE:
+ case ECORE_FILE_EVENT_DELETED_FILE:
+ case ECORE_FILE_EVENT_MODIFIED:
+ case ECORE_FILE_EVENT_CLOSED:
+ ext = strrchr(path, '.');
+ if (ext && (!strcmp(ext, ".desktop") || !strcmp(ext, ".directory")))
+ cache_desktop_update();
+ break;
+
+ case ECORE_FILE_EVENT_DELETED_SELF:
+ case ECORE_FILE_EVENT_DELETED_DIRECTORY:
+ eina_hash_del_by_key(change_monitors, path);
+ cache_desktop_update();
+ break;
+
+ case ECORE_FILE_EVENT_CREATED_DIRECTORY:
+ desktop_changes_monitor_add(path);
+ cache_desktop_update();
+ break;
+ }
+}
+
+static void
+desktop_changes_monitor_add(const char *path)
+{
+ Ecore_File_Monitor *mon;
+
+ if (eina_hash_find(change_monitors, path)) return;
+ /* TODO: Check for symlink and monitor the real path */
+ mon = ecore_file_monitor_add(path,
+ desktop_changes_cb,
+ NULL);
+ if (mon)
+ eina_hash_add(change_monitors, path, mon);
+}
+
+static void
+desktop_changes_listen_recursive(const char *path)
+{
+ Eina_Iterator *it;
+ Eina_File_Direct_Info *info;
+
+ if (!ecore_file_is_dir(path)) return;
+ desktop_changes_monitor_add(path);
+ it = eina_file_stat_ls(path);
+ if (!it) return;
+ EINA_ITERATOR_FOREACH(it, info)
+ {
+ if (((info->type == EINA_FILE_LNK) && (ecore_file_is_dir(info->path))) ||
+ (info->type == EINA_FILE_DIR))
+ desktop_changes_listen_recursive(info->path);
+ }
+ eina_iterator_free(it);
+}
+
+static void
+desktop_changes_listen(void)
+{
+ Eina_List *l;
+ const char *path;
+
+ EINA_LIST_FOREACH(desktop_system_dirs, l, path)
+ desktop_changes_listen_recursive(path);
+
+ EINA_LIST_FOREACH(desktop_extra_dirs, l, path)
+ desktop_changes_listen_recursive(path);
+}
+
+static void
+fill_list(const char *file, Eina_List **l)
+{
+ Eina_File *f = NULL;
+ Eina_Iterator *it = NULL;
+ Eina_File_Line *line = NULL;
+ char buf[PATH_MAX];
+
+ snprintf(buf, sizeof(buf), "%s/efreet/%s", efreet_cache_home_get(), file);
+ f = eina_file_open(buf, EINA_FALSE);
+ if (!f) return;
+ it = eina_file_map_lines(f);
+ if (!it) goto error;
+ EINA_ITERATOR_FOREACH(it, line)
+ {
+ const char *end;
+ end = line->end - 1;
+ *l = eina_list_append(*l, eina_stringshare_add_length(line->start, end - line->start));
+ }
+ eina_iterator_free(it);
+error:
+ eina_file_close(f);
+}
+
+static void
+read_lists(void)
+{
+ fill_list("extra_desktop.dirs", &desktop_extra_dirs);
+ fill_list("extra_icon.dirs", &icon_extra_dirs);
+ fill_list("icon.exts", &icon_exts);
+}
+
+static void
+save_list(const char *file, Eina_List *l)
+{
+ FILE *f;
+ char buf[PATH_MAX];
+ Eina_List *ll;
+ const char *path;
+
+ snprintf(buf, sizeof(buf), "%s/efreet/%s", efreet_cache_home_get(), file);
+ f = fopen(buf, "wb");
+ if (!f) return;
+ EINA_LIST_FOREACH(l, ll, path)
+ fprintf(f, "%s\n", path);
+ fclose(f);
+}
+
+static int
+strcmplen(const void *data1, const void *data2)
+{
+ return strncmp(data1, data2, eina_stringshare_strlen(data1));
+}
+
+/* external */
+void
+cache_desktop_dir_add(const char *dir)
+{
+ char *san;
+ Eina_List *l;
+
+ san = eina_file_path_sanitize(dir);
+ if (!san) return;
+ if ((l = eina_list_search_unsorted_list(desktop_system_dirs, strcmplen, san)))
+ {
+ /* Path is registered, but maybe not monitored */
+ const char *path = eina_list_data_get(l);
+ if (!eina_hash_find(change_monitors, path))
+ cache_desktop_update();
+ }
+ else if (!eina_list_search_unsorted_list(desktop_extra_dirs, EINA_COMPARE_CB(strcmp), san))
+ {
+ /* Not a registered path */
+ desktop_extra_dirs = eina_list_append(desktop_extra_dirs, eina_stringshare_add(san));
+ save_list("extra_desktop.dirs", desktop_extra_dirs);
+ cache_desktop_update();
+ }
+ free(san);
+}
+
+void
+cache_icon_dir_add(const char *dir)
+{
+ char *san;
+
+ san = eina_file_path_sanitize(dir);
+ if (!san) return;
+ if (!eina_list_search_unsorted_list(icon_extra_dirs, EINA_COMPARE_CB(strcmp), san))
+ {
+ icon_extra_dirs = eina_list_append(icon_extra_dirs, eina_stringshare_add(san));
+ save_list("extra_icon.dirs", icon_extra_dirs);
+ cache_icon_update(EINA_TRUE);
+ }
+ free(san);
+}
+
+void
+cache_icon_ext_add(const char *ext)
+{
+ if (!eina_list_search_unsorted_list(icon_exts, EINA_COMPARE_CB(strcmp), ext))
+ {
+ icon_exts = eina_list_append(icon_exts, eina_stringshare_add(ext));
+ save_list("icon.exts", icon_exts);
+ cache_icon_update(EINA_TRUE);
+ }
+}
+
+void
+cache_desktop_update(void)
+{
+ if (desktop_cache_timer)
+ ecore_timer_del(desktop_cache_timer);
+ desktop_cache_timer = ecore_timer_add(1.0, desktop_cache_update_cache_cb, NULL);
+}
+
+Eina_Bool
+cache_desktop_exists(void)
+{
+ return desktop_exists;
+}
+
+Eina_Bool
+cache_init(void)
+{
+ char buf[PATH_MAX];
+
+ snprintf(buf, sizeof(buf), "%s/efreet", efreet_cache_home_get());
+ if (!ecore_file_mkpath(buf))
+ {
+ ERR("Failed to create directory '%s'", buf);
+ goto error;
+ }
+
+ cache_exe_del_handler = ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
+ cache_exe_del_cb, NULL);
+ if (!cache_exe_del_handler)
+ {
+ ERR("Failed to add exe del handler");
+ goto error;
+ }
+ cache_exe_data_handler = ecore_event_handler_add(ECORE_EXE_EVENT_DATA,
+ cache_exe_data_cb, NULL);
+ if (!cache_exe_data_handler)
+ {
+ ERR("Failed to add exe data handler");
+ goto error;
+ }
+
+ change_monitors = eina_hash_string_superfast_new(EINA_FREE_CB(ecore_file_monitor_del));
+
+ efreet_cache_update = 0;
+ if (!efreet_init()) goto error;
+
+ read_lists();
+ /* TODO: Should check if system dirs has changed and handles extra_dirs */
+ desktop_system_dirs = efreet_default_dirs_get(efreet_data_home_get(),
+ efreet_data_dirs_get(), "applications");
+ desktop_system_dirs =
+ eina_list_merge(
+ desktop_system_dirs, efreet_default_dirs_get(efreet_data_home_get(),
+ efreet_data_dirs_get(), "desktop-directories"));
+ icon_changes_listen();
+ desktop_changes_listen();
+ cache_icon_update(EINA_FALSE);
+ cache_desktop_update();
+
+ return EINA_TRUE;
+error:
+ if (cache_exe_del_handler) ecore_event_handler_del(cache_exe_del_handler);
+ cache_exe_del_handler = NULL;
+ if (cache_exe_data_handler) ecore_event_handler_del(cache_exe_data_handler);
+ cache_exe_data_handler = NULL;
+ return EINA_FALSE;
+}
+
+Eina_Bool
+cache_shutdown(void)
+{
+ const char *data;
+
+ efreet_shutdown();
+
+ if (cache_exe_del_handler) ecore_event_handler_del(cache_exe_del_handler);
+ cache_exe_del_handler = NULL;
+ if (cache_exe_data_handler) ecore_event_handler_del(cache_exe_data_handler);
+ cache_exe_data_handler = NULL;
+
+ if (change_monitors)
+ eina_hash_free(change_monitors);
+ change_monitors = NULL;
+ EINA_LIST_FREE(desktop_system_dirs, data)
+ eina_stringshare_del(data);
+ EINA_LIST_FREE(desktop_extra_dirs, data)
+ eina_stringshare_del(data);
+ EINA_LIST_FREE(icon_extra_dirs, data)
+ eina_stringshare_del(data);
+ EINA_LIST_FREE(icon_exts, data)
+ eina_stringshare_del(data);
+ return EINA_TRUE;
+}
diff --git a/src/bin/efreet/efreetd_cache.h b/src/bin/efreet/efreetd_cache.h
new file mode 100644
index 0000000000..2fb520e384
--- /dev/null
+++ b/src/bin/efreet/efreetd_cache.h
@@ -0,0 +1,13 @@
+#ifndef __EFREETD_CACHE_H
+#define __EFREETD_CACHE_H
+
+void cache_desktop_dir_add(const char *dir);
+void cache_icon_dir_add(const char *dir);
+void cache_icon_ext_add(const char *ext);
+void cache_desktop_update(void);
+Eina_Bool cache_desktop_exists(void);
+
+Eina_Bool cache_init(void);
+Eina_Bool cache_shutdown(void);
+
+#endif
diff --git a/src/bin/efreet/efreetd_dbus.c b/src/bin/efreet/efreetd_dbus.c
new file mode 100644
index 0000000000..b5e98ad083
--- /dev/null
+++ b/src/bin/efreet/efreetd_dbus.c
@@ -0,0 +1,262 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <Ecore.h>
+#include <EDBus.h>
+
+#include "efreetd.h"
+#include "efreetd_cache.h"
+
+#define BUS "org.enlightenment.Efreet"
+#define PATH "/org/enlightenment/Efreet"
+#define INTERFACE "org.enlightenment.Efreet"
+
+/* internal */
+enum
+{
+ EFREET_SIGNAL_ICON_CACHE_UPDATE = 0,
+ EFREET_SIGNAL_DESKTOP_CACHE_UPDATE
+};
+
+static EDBus_Connection *conn;
+static EDBus_Service_Interface *iface;
+
+static Ecore_Timer *shutdown = NULL;
+static int clients = 0;
+
+static Eina_Bool
+do_shutdown(void *data EINA_UNUSED)
+{
+ quit();
+ return ECORE_CALLBACK_CANCEL;
+}
+
+static void
+disconnected(void *context EINA_UNUSED, EDBus_Connection *connection EINA_UNUSED, void *event_info EINA_UNUSED)
+{
+ INF("disconnected");
+ quit();
+}
+
+static void
+client_name_owner_changed_cb(void *data, const char *bus, const char *old_id, const char *new_id)
+{
+ if (new_id[0])
+ return;
+ edbus_name_owner_changed_callback_del(conn, bus,
+ client_name_owner_changed_cb, NULL);
+ clients--;
+ if (clients <= 0)
+ {
+ clients = 0;
+ if (shutdown) ecore_timer_del(shutdown);
+ shutdown = ecore_timer_add(10.0, do_shutdown, NULL);
+ }
+}
+
+static EDBus_Message *
+do_register(const EDBus_Service_Interface *ifc EINA_UNUSED, const EDBus_Message *message)
+{
+ EDBus_Message *reply;
+ const char *lang;
+
+ if (!edbus_message_arguments_get(message, "s", &lang))
+ {
+ ERR("Error getting arguments.");
+ return NULL;
+ }
+ setenv("LANG", lang, 1);
+
+ clients++;
+ if (shutdown) ecore_timer_del(shutdown);
+ shutdown = NULL;
+
+ edbus_name_owner_changed_callback_add(conn,
+ edbus_message_sender_get(message),
+ client_name_owner_changed_cb, NULL,
+ EINA_FALSE);
+ reply = edbus_message_method_return_new(message);
+ edbus_message_arguments_append(reply, "b", cache_desktop_exists());
+ return reply;
+}
+
+static EDBus_Message *
+add_desktop_dirs(const EDBus_Service_Interface *ifc EINA_UNUSED, const EDBus_Message *message)
+{
+ EDBus_Message_Iter *array = NULL;
+ const char *dir;
+
+ if (!edbus_message_arguments_get(message, "as", &array))
+ {
+ ERR("Error getting arguments.");
+ return NULL;
+ }
+
+ while (edbus_message_iter_get_and_next(array, 's', &dir))
+ {
+ cache_desktop_dir_add(dir);
+ }
+
+ return NULL;
+}
+
+static EDBus_Message *
+add_icon_dirs(const EDBus_Service_Interface *ifc EINA_UNUSED, const EDBus_Message *message)
+{
+ EDBus_Message_Iter *array = NULL;
+ const char *dir;
+
+ if (!edbus_message_arguments_get(message, "as", &array))
+ {
+ ERR("Error getting arguments.");
+ return NULL;
+ }
+
+ while (edbus_message_iter_get_and_next(array, 's', &dir))
+ {
+ cache_icon_dir_add(dir);
+ }
+
+ return NULL;
+}
+
+static EDBus_Message *
+build_desktop_cache(const EDBus_Service_Interface *ifc EINA_UNUSED, const EDBus_Message *message EINA_UNUSED)
+{
+ const char *lang;
+
+ if (!edbus_message_arguments_get(message, "s", &lang))
+ {
+ ERR("Error getting arguments.");
+ return NULL;
+ }
+ setenv("LANG", lang, 1);
+
+ cache_desktop_update();
+ return NULL;
+}
+
+static EDBus_Message *
+add_icon_exts(const EDBus_Service_Interface *ifc EINA_UNUSED, const EDBus_Message *message)
+{
+ EDBus_Message_Iter *array = NULL;
+ const char *ext;
+
+ if (!edbus_message_arguments_get(message, "as", &array))
+ {
+ ERR("Error getting arguments.");
+ return NULL;
+ }
+
+ while (edbus_message_iter_get_and_next(array, 's', &ext))
+ {
+ cache_icon_ext_add(ext);
+ }
+
+ return NULL;
+}
+
+static const EDBus_Signal signals[] = {
+ [EFREET_SIGNAL_ICON_CACHE_UPDATE] = {"IconCacheUpdate", EDBUS_ARGS({ "b", "update" }), 0},
+ [EFREET_SIGNAL_DESKTOP_CACHE_UPDATE] = {"DesktopCacheUpdate", EDBUS_ARGS({ "b", "update" }), 0},
+ { NULL, NULL, 0 }
+};
+
+static const EDBus_Method methods[] = {
+ {
+ "Register", EDBUS_ARGS({"s", "lang info"}), EDBUS_ARGS({"b", "cache exists"}),
+ do_register, 0
+ },
+ {
+ "AddDesktopDirs", EDBUS_ARGS({"as", "dirs"}), NULL,
+ add_desktop_dirs, EDBUS_METHOD_FLAG_NOREPLY
+ },
+ {
+ "BuildDesktopCache", EDBUS_ARGS({"s", "lang info"}), NULL,
+ build_desktop_cache, EDBUS_METHOD_FLAG_NOREPLY
+ },
+ {
+ "AddIconDirs", EDBUS_ARGS({"as", "dirs"}), NULL,
+ add_icon_dirs, EDBUS_METHOD_FLAG_NOREPLY
+ },
+ {
+ "AddIconExts", EDBUS_ARGS({"as", "exts"}), NULL,
+ add_icon_exts, EDBUS_METHOD_FLAG_NOREPLY
+ },
+ { NULL, NULL, NULL, NULL, 0 }
+};
+
+static const EDBus_Service_Interface_Desc desc = {
+ INTERFACE, methods, signals, NULL, NULL, NULL
+};
+
+static void
+on_name_request(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
+{
+ unsigned int reply;
+
+ if (edbus_message_error_get(msg, NULL, NULL))
+ {
+ ERR("error on on_name_request");
+ quit();
+ return;
+ }
+
+ if (!edbus_message_arguments_get(msg, "u", &reply))
+ {
+ ERR("error getting arguments on on_name_request");
+ quit();
+ return;
+ }
+
+ if (reply != EDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
+ {
+ ERR("error name already in use");
+ quit();
+ return;
+ }
+ INF("name requested");
+}
+
+/* external */
+void
+send_signal_icon_cache_update(Eina_Bool update)
+{
+ edbus_service_signal_emit(iface, EFREET_SIGNAL_ICON_CACHE_UPDATE, update);
+}
+
+void
+send_signal_desktop_cache_update(Eina_Bool update)
+{
+ edbus_service_signal_emit(iface, EFREET_SIGNAL_DESKTOP_CACHE_UPDATE, update);
+}
+
+Eina_Bool
+dbus_init(void)
+{
+ if (!edbus_init()) return EINA_FALSE;
+
+ conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
+ if (!conn) goto conn_error;
+
+ edbus_connection_event_callback_add(conn,
+ EDBUS_CONNECTION_EVENT_DISCONNECTED, disconnected, NULL);
+ iface = edbus_service_interface_register(conn, PATH, &desc);
+ edbus_name_request(conn, BUS, EDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
+ on_name_request, NULL);
+
+ return EINA_TRUE;
+conn_error:
+ edbus_shutdown();
+ return EINA_FALSE;
+}
+
+Eina_Bool
+dbus_shutdown(void)
+{
+ edbus_connection_unref(conn);
+ edbus_shutdown();
+ return EINA_TRUE;
+
+}
diff --git a/src/bin/efreet/efreetd_dbus.h b/src/bin/efreet/efreetd_dbus.h
new file mode 100644
index 0000000000..9e9fd70700
--- /dev/null
+++ b/src/bin/efreet/efreetd_dbus.h
@@ -0,0 +1,10 @@
+#ifndef __EFREETD_DBUS_H
+#define __EFREETD_DBUS_H
+
+void send_signal_icon_cache_update(Eina_Bool update);
+void send_signal_desktop_cache_update(Eina_Bool update);
+
+Eina_Bool dbus_init(void);
+Eina_Bool dbus_shutdown(void);
+
+#endif