Little changes at elm profile functios + some new free()-ing helpers.
authorGustavo Lima Chaves <glima@profusion.mobi>
Mon, 25 Oct 2010 17:36:07 +0000 (17:36 +0000)
committerGustavo Lima Chaves <glima@profusion.mobi>
Mon, 25 Oct 2010 17:36:07 +0000 (17:36 +0000)
SVN revision: 53878

src/bin/config.c
src/lib/Elementary.h.in
src/lib/elm_config.c
src/lib/elm_main.c

index 144d54b2400605f2ad03d843412f2c759df666fa..98a7c044cc39e497bcdb4736d1ccac8bb2029e35 100644 (file)
@@ -736,6 +736,7 @@ _profiles_list_fill(Evas_Object *l_widget, Eina_List *p_names)
    const char *cur_profile = NULL;
    const char *profile;
    void *sel_it = NULL;
+   Eina_List *l;
 
    if (!p_names)
      return;
@@ -745,7 +746,7 @@ _profiles_list_fill(Evas_Object *l_widget, Eina_List *p_names)
 
    cur_profile = elm_profile_current_get();
 
-   EINA_LIST_FREE(p_names, profile)
+   EINA_LIST_FOREACH(p_names, l, profile)
      {
         Efreet_Desktop *desk = NULL;
         char buf[PATH_MAX], *pdir;
@@ -790,7 +791,6 @@ _profiles_list_fill(Evas_Object *l_widget, Eina_List *p_names)
 
         if (pdir)
           free(pdir);
-        free((void *)profile);
 
         if (desk)
           efreet_desktop_free(desk);
@@ -910,6 +910,8 @@ _status_config_profiles(Evas_Object *win, Evas_Object *holder)
 
    _profiles_list_fill(li, profs);
 
+   elm_profile_list_free(profs);
+
    edje_thaw();
    evas_event_thaw(evas);
 
index fadb839c601d2195330914fc6064be1bd08c9284..c875f35a36ed4ea640df3fd9fdbe4bb9207952b3 100644 (file)
@@ -334,6 +334,7 @@ extern "C" {
    EAPI const char  *elm_profile_current_get(void);
    EAPI char        *elm_profile_dir_get(const char *profile);
    EAPI Eina_List   *elm_profile_list_get(void);
+   EAPI void         elm_profile_list_free(Eina_List *l);
    EAPI void         elm_profile_set(const char *profile);
    EAPI void         elm_profile_all_set(const char *profile);
 
index 87ce3f736d5b3e1e1d30c4c30b12f3d7a1c97638..3796324dbb738daad35421b2e840bec0335dbc5d 100644 (file)
@@ -283,7 +283,8 @@ _desc_shutdown(void)
    _config_edd = NULL;
 }
 
-static int _cb_sort_files(char *f1, char *f2)
+static int
+_sort_files_cb(const void *f1, const void *f2)
 {
    return strcmp(f1, f2);
 }
@@ -320,10 +321,13 @@ _elm_config_profile_dir_get(const char *prof)
 Eina_List *
 _elm_config_profiles_list(void)
 {
+   const Eina_File_Direct_Info *info;
    const char *home = NULL;
    Eina_List *flist = NULL;
-   char buf[PATH_MAX], *p;
-   Eina_List *files;
+   Eina_Iterator *file_it;
+   struct stat buffer;
+   char buf[PATH_MAX];
+   const char *dir;
    size_t len;
 
    home = getenv("HOME");
@@ -334,70 +338,110 @@ _elm_config_profiles_list(void)
                            ".elementary/config",
                            sizeof(".elementary/config") - 1);
 
-   files = ecore_file_ls(buf);
+   file_it = eina_file_direct_ls(buf);
+   if (!file_it)
+     goto sys;
 
    buf[len] = '/';
    len++;
 
-   p = buf + len;
    len = sizeof(buf) - len;
-   if (files)
+
+   EINA_ITERATOR_FOREACH(file_it, info)
      {
-       char *file;
-
-       files = eina_list_sort(files, 0, (Eina_Compare_Cb)_cb_sort_files);
-       EINA_LIST_FREE(files, file)
-         {
-            if (eina_strlcpy(p, file, len) >= len)
-              {
-                 free(file);
-                 continue;
-              }
-            if (ecore_file_is_dir(buf))
-              flist = eina_list_append(flist, file);
-            else
-              free(file);
-         }
+       if (info->name_length >= len)
+         continue;
+
+       switch (info->dirent->d_type)
+         {
+         case DT_UNKNOWN:
+           if (stat(info->path, &buffer) != 0)
+             goto it_free;
+
+           if (S_ISDIR(buffer.st_mode))
+             flist =
+               eina_list_sorted_insert(flist, _sort_files_cb,
+                                       eina_stringshare_add(info->path +
+                                                            info->name_start));
+           break;
+
+         case DT_DIR:
+           flist =
+             eina_list_sorted_insert(flist, _sort_files_cb,
+                                     eina_stringshare_add(info->path +
+                                                          info->name_start));
+           break;
+
+         default:
+           continue;
+         }
      }
 
+   eina_iterator_free(file_it);
+
+ sys:
    len = eina_str_join_len(buf, sizeof(buf), '/', _elm_data_dir,
                            strlen(_elm_data_dir), "config",
                            sizeof("config") - 1);
 
-   files = ecore_file_ls(buf);
+   file_it = eina_file_direct_ls(buf);
+   if (!file_it)
+     goto list_free;
 
    buf[len] = '/';
    len++;
 
-   p = buf + len;
+#define S_ISDIR_CASE_DO                                                     \
+     {                                                                      \
+       const Eina_List *l;                                                  \
+       const char *tmp;                                                     \
+       EINA_LIST_FOREACH(flist, l, tmp)                                     \
+         if (!strcmp(info->path + info->name_start, tmp))                   \
+           break;                                                           \
+                                                                            \
+       if (!l)                                                              \
+         flist =                                                            \
+           eina_list_sorted_insert(flist, _sort_files_cb,                   \
+                                   eina_stringshare_add(info->path +        \
+                                                        info->name_start)); \
+     }                                                                      \
+
    len = sizeof(buf) - len;
-   if (files)
+   EINA_ITERATOR_FOREACH(file_it, info)
      {
-       char *file;
-       files = eina_list_sort(files, 0, (Eina_Compare_Cb)_cb_sort_files);
-       EINA_LIST_FREE(files, file)
-         {
-            if (eina_strlcpy(p, file, len) >= len)
-              {
-                 free(file);
-                 continue;
-              }
-            if (ecore_file_is_dir(buf))
-              {
-                 const Eina_List *l;
-                 const char *tmp;
-                 EINA_LIST_FOREACH(flist, l, tmp)
-                   if (!strcmp(file, tmp)) break;
-
-                 if (!l)
-                    flist = eina_list_append(flist, file);
-                 else free(file);
-              }
-            else
-              free(file);
-         }
+       if (info->name_length >= len)
+         continue;
+
+       switch (info->dirent->d_type)
+         {
+         case DT_UNKNOWN:
+           if (stat(info->path, &buffer) != 0)
+             goto it_free;
+
+           if (S_ISDIR(buffer.st_mode))
+             S_ISDIR_CASE_DO
+           break;
+
+         case DT_DIR:
+           S_ISDIR_CASE_DO
+           break;
+
+         default:
+           continue;
+         }
      }
    return flist;
+
+#undef S_ISDIR_CASE_DO
+
+ it_free:
+   eina_iterator_free(file_it);
+
+ list_free:
+   EINA_LIST_FREE(flist, dir)
+     eina_stringshare_del(dir);
+
+   return NULL;
 }
 
 static void
index 4a7f3cee64326bce034f80648e7d1f3f025a8cf5..60bac8fd5c86179a01f67c224ff59b12bedc28a0 100644 (file)
@@ -1232,7 +1232,7 @@ elm_profile_current_get(void)
  * @return The profile's directory path.
  * @ingroup Profile
  *
- * @note You must free() the return value after usage.
+ * @note You must free it with elm_profile_dir_free().
  */
 EAPI char *
 elm_profile_dir_get(const char *profile)
@@ -1240,15 +1240,28 @@ elm_profile_dir_get(const char *profile)
    return _elm_config_profile_dir_get(profile);
 }
 
+/**
+ * Free an Elementary's profile directory path, as returned by
+ * elm_profile_dir_get().
+ *
+ * @param p_dir The profile's path
+ * @ingroup Profile
+ *
+ */
+EAPI void
+elm_profile_dir_free(const char *p_dir)
+{
+   free((void *)p_dir);
+}
+
 /**
  * Get Elementary's list of available profiles.
  *
  * @return The profiles list.
  * @ingroup Profile
  *
- * @note You must free() the strings which came as list data after
- *       usage of the return value. Consider using the @c EINA_LIST_FREE()
- *       macro.
+ * @note One must free this list, after usage, with the function
+ *       elm_profile_list_free().
  */
 EAPI Eina_List *
 elm_profile_list_get(void)
@@ -1256,6 +1269,22 @@ elm_profile_list_get(void)
    return _elm_config_profiles_list();
 }
 
+/**
+ * Free Elementary's list of available profiles.
+ *
+ * @param The profiles list, as returned by elm_profile_list_get().
+ * @ingroup Profile
+ *
+ */
+EAPI void
+elm_profile_list_free(Eina_List *l)
+{
+  const char *dir;
+
+  EINA_LIST_FREE(l, dir)
+    eina_stringshare_del(dir);
+}
+
 /**
  * Set Elementary's profile.
  *