Enlightenment CVS committal

Author  : onefang
Project : e17
Module  : libs/ecore

Dir     : e17/libs/ecore/src/lib/ecore_desktop


Modified Files:
        Ecore_Desktop.h ecore_desktop.c 


Log Message:
Free up all those nasty hashes.

===================================================================
RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore_desktop/Ecore_Desktop.h,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -3 -r1.26 -r1.27
--- Ecore_Desktop.h     13 Sep 2006 13:50:12 -0000      1.26
+++ Ecore_Desktop.h     19 Sep 2006 02:11:32 -0000      1.27
@@ -63,6 +63,7 @@
    unsigned char       hidden : 1;
    unsigned char       no_display : 1;
    unsigned char       allocated : 1;
+   unsigned char       ondisk : 1;
    /* Actually calling this st_mtime causes compile issues, must be some 
strange macros at work. */
    time_t              mtime;           /* For checking if the cache is valid. 
*/
 };
===================================================================
RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore_desktop/ecore_desktop.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -3 -r1.33 -r1.34
--- ecore_desktop.c     17 Sep 2006 13:34:11 -0000      1.33
+++ ecore_desktop.c     19 Sep 2006 02:11:32 -0000      1.34
@@ -20,10 +20,12 @@
 
 static int          init_count = 0;
 
-static Ecore_Hash  *ini_file_cache;
 static Ecore_Hash  *desktop_cache;
 
+Ecore_Desktop      *_ecore_desktop_get(const char *file, const char *lang);
 void _ecore_desktop_destroy(Ecore_Desktop * desktop);
+#define IFGETDUP(src, key, dst) src = (char *)ecore_hash_get(result->group, 
key); if (src) dst = strdup(src); else dst = NULL;
+#define IFFREE(src) if (src) free(src);  src = NULL;
 
 
 /**
@@ -51,7 +53,6 @@
 {
    Ecore_Hash         *result;
 
-/* FIXME: should probably look in ini_file_cache first. */
    result = ecore_hash_new(ecore_str_hash, ecore_str_compare);
    if (result)
      {
@@ -145,7 +146,6 @@
        buffer[0] = (char)0;
 
        fclose(f);
-       ecore_hash_set(ini_file_cache, strdup(file), result);
      }
    return result;
 }
@@ -153,12 +153,6 @@
 /**
  * Get the contents of a .desktop file.
  *
- * Everything that is in the .desktop file is returned in the
- * data member of the Ecore_Desktop structure, it's an Ecore_Hash 
- * as returned by ecore_desktop_ini_get().  Some of the data in the
- * .desktop file is decoded into specific members of the returned 
- * structure.
- *
  * Use ecore_desktop_destroy() to free this structure.
  *
  * @param   file Full path to the .desktop file.
@@ -170,7 +164,28 @@
 ecore_desktop_get(const char *file, const char *lang)
 {
    Ecore_Desktop      *result;
+
+   result = _ecore_desktop_get(file, lang);
+   if (result)
+      {
+         /* Kill the hash, it takes up way too much memory. */
+         if (result->data)
+           {
+              ecore_hash_destroy(result->data);
+              result->data = NULL;
+           }
+         result->group = NULL;
+      }
+      
+   return result;
+}
+
+Ecore_Desktop      *
+_ecore_desktop_get(const char *file, const char *lang)
+{
+   Ecore_Desktop      *result;
    struct stat         st;
+   char               *value;
    int                 stated = 0;
 
    result = (Ecore_Desktop *) ecore_hash_get(desktop_cache, (char *) file);
@@ -192,6 +207,7 @@
        result = calloc(1, sizeof(Ecore_Desktop));
        if (result)
          {
+            result->ondisk = 1;
             result->original_path = strdup(file);
             if (lang)
                result->original_lang = strdup(lang);
@@ -213,7 +229,6 @@
                                                      "KDE Desktop Entry");
                  if (result->group)
                    {
-                      char               *value;
                       char               *eap_name = NULL;
                       char               *exe = NULL;
                       char               *categories = NULL;
@@ -233,24 +248,16 @@
                          }
                       eap_name = result->eap_name;
 
-                      result->name =
-                         (char *)ecore_hash_get(result->group, "Name");
-                      result->generic =
-                         (char *)ecore_hash_get(result->group, "GenericName");
-                      result->comment =
-                         (char *)ecore_hash_get(result->group, "Comment");
-                      result->type =
-                         (char *)ecore_hash_get(result->group, "Type");
-
-                      result->path =
-                         (char *)ecore_hash_get(result->group, "Path");
-                      result->URL =
-                         (char *)ecore_hash_get(result->group, "URL");
-                      result->file =
-                         (char *)ecore_hash_get(result->group, "File");
+                       IFGETDUP(value, "Name", result->name);
+                       IFGETDUP(value, "GenericName", result->generic);
+                       IFGETDUP(value, "Comment", result->comment);
+                       IFGETDUP(value, "Type", result->type);
+
+                       IFGETDUP(value, "Path", result->path);
+                       IFGETDUP(value, "URL", result->URL);
+                       IFGETDUP(value, "File", result->file);
 
-                      result->exec =
-                         (char *)ecore_hash_get(result->group, "Exec");
+                       IFGETDUP(value, "Exec", result->exec);
                       if (result->exec)
                          {
                              exe = strchr(result->exec, ' ');
@@ -262,12 +269,8 @@
                              exe = result->exec;
                           }
 
-                      value =
-                         (char *)ecore_hash_get(result->group,
-                                                "StartupWMClass");
-                      if (value)
-                         result->window_class = strdup(value);
-                      else if (result->exec)
+                       IFGETDUP(value, "StartupWMClass", result->window_class);
+                      if ((!value) && (result->exec))
                          {
                             char *tmp;
 
@@ -290,24 +293,15 @@
                                  free(tmp);
                               }
                          }
-                      result->window_name =
-                         (char *)ecore_hash_get(result->group, 
"X-Enlightenment-WindowName");
-                      result->window_title =
-                         (char *)ecore_hash_get(result->group, 
"X-Enlightenment-WindowTitle");
-                      result->window_role =
-                         (char *)ecore_hash_get(result->group, 
"X-Enlightenment-WindowRole");
-
+                       IFGETDUP(value, "X-Enlightenment-WindowName", 
result->window_name);
+                       IFGETDUP(value, "X-Enlightenment-WindowTitle", 
result->window_title);
+                       IFGETDUP(value, "X-Enlightenment-WindowRole", 
result->window_role);
 
-                      result->icon =
-                         (char *)ecore_hash_get(result->group, "Icon");
+                       IFGETDUP(value, "Icon", result->icon);
                       if (result->icon)
                          result->original_icon = strdup(result->icon);
-                      result->icon_class =
-                         (char *)ecore_hash_get(result->group, 
"X-Enlightenment-IconClass");
-                      value =
-                         (char *)ecore_hash_get(result->group, 
"X-Enlightenment-IconPath");
-                      if (value)
-                         result->icon_path = strdup(value);
+                       IFGETDUP(value, "X-Enlightenment-IconClass", 
result->icon_class);
+                       IFGETDUP(value, "X-Enlightenment-IconPath", 
result->icon_path);
 
                        if ((result->icon != NULL) && (result->icon_path == 
NULL) && (strchr(result->icon, '/') != NULL))
                          {
@@ -339,12 +333,12 @@
                                }
                          }
 
-                      result->categories =
-                         (char *)ecore_hash_get(result->group, "Categories");
+                       IFGETDUP(value, "Categories", result->categories);
                       if (result->categories)
                          result->Categories =
                             ecore_desktop_paths_to_hash(result->categories);
                       categories = result->categories;
+
                       value =
                          (char *)ecore_hash_get(result->group, "OnlyShowIn");
                       if (value)
@@ -473,11 +467,8 @@
                                                        "Trash Info");
                       if (result->group)
                         {
-                           result->path =
-                              (char *)ecore_hash_get(result->group, "Path");
-                           result->deletiondate =
-                              (char *)ecore_hash_get(result->group,
-                                                     "DeletionDate");
+                            IFGETDUP(value, "Path", result->path);
+                            IFGETDUP(value, "DeletionDate", 
result->deletiondate);
                         }
                    }
 
@@ -497,14 +488,30 @@
 ecore_desktop_save(Ecore_Desktop * desktop)
 {
    char *temp;
+   int trash = 0;
 
    if (!desktop->group)
       {
-         desktop->group = ecore_hash_new(ecore_str_hash, ecore_str_compare);
-         if (desktop->group)
+         if ((desktop->ondisk) && (desktop->original_path))
+           {
+              desktop->data = ecore_desktop_ini_get(desktop->original_path);
+              desktop->group = (Ecore_Hash *) ecore_hash_get(desktop->data, 
"Desktop Entry");
+              if (!desktop->group)
+                 desktop->group = (Ecore_Hash *) ecore_hash_get(desktop->data, 
"KDE Desktop Entry");
+              if (!desktop->group)
+                 {
+                    trash = 1;
+                    desktop->group = (Ecore_Hash *) 
ecore_hash_get(desktop->data, "Trash Info");
+                 }
+           }
+        else
            {
-               ecore_hash_set_free_key(desktop->group, free);
-              ecore_hash_set_free_value(desktop->group, free);
+               desktop->group = ecore_hash_new(ecore_str_hash, 
ecore_str_compare);
+               if (desktop->group)
+                 {
+                     ecore_hash_set_free_key(desktop->group, free);
+                    ecore_hash_set_free_value(desktop->group, free);
+                 }
            }
       }
 
@@ -575,7 +582,10 @@
                list = ecore_hash_keys(desktop->group);
                if ((!f) || (!list)) return;
 
-               fprintf(f, "[Desktop Entry]\n");
+               if (trash)
+                  fprintf(f, "[Trash Info]\n");
+              else
+                  fprintf(f, "[Desktop Entry]\n");
               ecore_list_goto_first(list);
                while ((key = (char *) ecore_list_next(list)))
                  {
@@ -587,6 +597,15 @@
                  }
                fclose(f);
            }
+
+         if (desktop->data)
+           {
+              ecore_hash_destroy(desktop->data);
+              desktop->data = NULL;
+           }
+        else
+           ecore_hash_destroy(desktop->group);
+         desktop->group = NULL;
       }
 }
 
@@ -607,16 +626,6 @@
 
    if (!ecore_desktop_paths_init()) return --init_count;
 
-   if (!ini_file_cache)
-     {
-       ini_file_cache = ecore_hash_new(ecore_str_hash, ecore_str_compare);
-       if (ini_file_cache)
-         {
-            ecore_hash_set_free_key(ini_file_cache, free);
-            ecore_hash_set_free_value(ini_file_cache,
-                                      (Ecore_Free_Cb) ecore_hash_destroy);
-         }
-     }
    if (!desktop_cache)
      {
        desktop_cache = ecore_hash_new(ecore_str_hash, ecore_str_compare);
@@ -648,11 +657,6 @@
 
    ecore_desktop_icon_shutdown();
 
-   if (ini_file_cache)
-     {
-       ecore_hash_destroy(ini_file_cache);
-       ini_file_cache = NULL;
-     }
    if (desktop_cache)
      {
        ecore_hash_destroy(desktop_cache);
@@ -683,26 +687,37 @@
 void
 _ecore_desktop_destroy(Ecore_Desktop * desktop)
 {
-   if (desktop->original_path)
-      free(desktop->original_path);
-   if (desktop->icon_path)
-      free(desktop->icon_path);
-   if (desktop->original_lang)
-      free(desktop->original_lang);
-   if (desktop->eap_name)
-      free(desktop->eap_name);
-   if (desktop->original_icon)
-      free(desktop->original_icon);
-   if (desktop->icon_class)
-      free(desktop->icon_class);
-   if (desktop->window_class)
-      free(desktop->window_class);
-   if (desktop->NotShowIn)
-      ecore_hash_destroy(desktop->NotShowIn);
-   if (desktop->OnlyShowIn)
-      ecore_hash_destroy(desktop->OnlyShowIn);
-   if (desktop->Categories)
-      ecore_hash_destroy(desktop->Categories);
+   IFFREE(desktop->original_path)
+   IFFREE(desktop->original_lang)
+   IFFREE(desktop->eap_name)
+   IFFREE(desktop->name)
+   IFFREE(desktop->generic)
+   IFFREE(desktop->comment)
+   IFFREE(desktop->type)
+   IFFREE(desktop->exec)
+   IFFREE(desktop->exec_params)
+   IFFREE(desktop->categories)
+   IFFREE(desktop->icon)
+   IFFREE(desktop->original_icon)
+   IFFREE(desktop->icon_class)
+   IFFREE(desktop->icon_path)
+   IFFREE(desktop->path)
+   IFFREE(desktop->URL)
+   IFFREE(desktop->file)
+   IFFREE(desktop->deletiondate)
+   IFFREE(desktop->window_class)
+   IFFREE(desktop->window_name)
+   IFFREE(desktop->window_title)
+   IFFREE(desktop->window_role)
+   IFFREE(desktop->NotShowIn)
+   IFFREE(desktop->OnlyShowIn)
+   IFFREE(desktop->Categories)
+   if (desktop->data)
+      {
+         ecore_hash_destroy(desktop->data);
+        desktop->data = NULL;
+      }
+   desktop->group = NULL;
    free(desktop);
 }
 



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
enlightenment-cvs mailing list
enlightenment-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to