Author: kelnos
Date: 2007-11-07 05:49:14 +0000 (Wed, 07 Nov 2007)
New Revision: 26267

Modified:
   xfdesktop/branches/xfce_4_4/src/xfdesktop-file-icon-manager.c
   xfdesktop/trunk/src/xfdesktop-file-icon-manager.c
Log:
rewrite entire icon positions file to clean out stale entries (bug 3267)


Modified: xfdesktop/branches/xfce_4_4/src/xfdesktop-file-icon-manager.c
===================================================================
--- xfdesktop/branches/xfce_4_4/src/xfdesktop-file-icon-manager.c       
2007-11-07 05:45:32 UTC (rev 26266)
+++ xfdesktop/branches/xfce_4_4/src/xfdesktop-file-icon-manager.c       
2007-11-07 05:49:14 UTC (rev 26267)
@@ -104,7 +104,6 @@
     gboolean show_removable_media;
     gboolean show_special[XFDESKTOP_SPECIAL_FILE_ICON_TRASH+1];
     
-    GList *icons_to_save;
     guint save_icons_id;
     
     GList *deferred_icons;
@@ -161,7 +160,10 @@
 static void 
xfdesktop_file_icon_manager_load_removable_media(XfdesktopFileIconManager 
*fmanager);
 static void 
xfdesktop_file_icon_manager_remove_removable_media(XfdesktopFileIconManager 
*fmanager);
 
+static void xfdesktop_file_icon_position_changed(XfdesktopFileIcon *icon,
+                                                 gpointer user_data);
 
+
 G_DEFINE_TYPE_EXTENDED(XfdesktopFileIconManager,
                        xfdesktop_file_icon_manager,
                        G_TYPE_OBJECT, 0,
@@ -789,6 +791,8 @@
         g_list_foreach(selected, (GFunc)g_object_unref, NULL);
         g_list_free(selected);
     }
+    
+    xfdesktop_file_icon_position_changed(NULL, fmanager);
 }
 
 static void
@@ -1629,12 +1633,13 @@
                     if(!g_ascii_strcasecmp("application/x-desktop",
                                            
thunar_vfs_mime_info_get_name(info->mime_info)))
                     {
+                        ThunarVfsInfo *info1 = (ThunarVfsInfo *)info;  /* why? 
*/
                         img = gtk_image_new_from_stock(GTK_STOCK_EDIT, 
GTK_ICON_SIZE_MENU);
                         gtk_widget_show(img);
                         mi = gtk_image_menu_item_new_with_mnemonic(_("_Edit 
Launcher"));
                         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mi), 
img);
                         g_object_set_data_full(G_OBJECT(mi), "thunar-vfs-info",
-                                               
thunar_vfs_info_ref((ThunarVfsInfo *)info),
+                                               thunar_vfs_info_ref(info1),
                                                
(GDestroyNotify)thunar_vfs_info_unref);
                         gtk_widget_show(mi);
                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
@@ -1997,45 +2002,67 @@
     /* don't free |selected|.  the menu deactivated handler does that */
 }
 
+static void
+file_icon_hash_write_icons(gpointer key,
+                           gpointer value,
+                           gpointer data)
+{
+    XfceRc *rcfile = data;
+    XfdesktopIcon *icon = value;
+    guint16 row, col;
+    
+    if(xfdesktop_icon_get_position(icon, &row, &col)) {
+        xfce_rc_set_group(rcfile, xfdesktop_icon_peek_label(icon));
+        xfce_rc_write_int_entry(rcfile, "row", row);
+        xfce_rc_write_int_entry(rcfile, "col", col);
+    }
+}
+
 static gboolean
 xfdesktop_file_icon_manager_save_icons(gpointer user_data)
 {
     XfdesktopFileIconManager *fmanager = 
XFDESKTOP_FILE_ICON_MANAGER(user_data);
-    gchar relpath[PATH_MAX];
+    gchar relpath[PATH_MAX], *tmppath, *path;
     XfceRc *rcfile;
-    GList *l;
-    XfdesktopIcon *icon;
-    guint16 row, col;
     
     fmanager->priv->save_icons_id = 0;
     
-    g_return_val_if_fail(fmanager->priv->icons_to_save, FALSE);
-    
     g_snprintf(relpath, PATH_MAX, "xfce4/desktop/icons.screen%d.rc",
                gdk_screen_get_number(fmanager->priv->gscreen));
+    path = xfce_resource_save_location(XFCE_RESOURCE_CONFIG, relpath, TRUE);
+    if(!path)
+        return FALSE;
     
-    rcfile = xfce_rc_config_open(XFCE_RESOURCE_CONFIG, relpath, FALSE);
+    tmppath = g_strconcat(path, ".new", NULL);
+    
+    rcfile = xfce_rc_simple_open(tmppath, FALSE);
     if(!rcfile) {
-        g_critical("Unable to determine location of icon position cache file.  
" \
-                   "Icon positions will not be saved.");
+        g_warning("Unable to determine location of icon position cache file.  
" \
+                  "Icon positions will not be saved.");
+        g_free(path);
+        g_free(tmppath);
         return FALSE;
     }
     
-    for(l = fmanager->priv->icons_to_save; l; l = l->next) {
-        icon = XFDESKTOP_ICON(l->data);
-        if(xfdesktop_icon_get_position(icon, &row, &col)) {
-            xfce_rc_set_group(rcfile, xfdesktop_icon_peek_label(icon));
-            xfce_rc_write_int_entry(rcfile, "row", row);
-            xfce_rc_write_int_entry(rcfile, "col", col);
-        }
-        g_object_unref(G_OBJECT(icon));
-    }
-    g_list_free(fmanager->priv->icons_to_save);
-    fmanager->priv->icons_to_save = NULL;
+    g_hash_table_foreach(fmanager->priv->icons,
+                         file_icon_hash_write_icons, rcfile);
+    g_hash_table_foreach(fmanager->priv->removable_icons,
+                         file_icon_hash_write_icons, rcfile);
+    g_hash_table_foreach(fmanager->priv->special_icons,
+                         file_icon_hash_write_icons, rcfile);
     
     xfce_rc_flush(rcfile);
     xfce_rc_close(rcfile);
     
+    if(rename(tmppath, path)) {
+        g_warning("Unable to rename temp file to %s: %s", path,
+                  strerror(errno));
+        unlink(tmppath);
+    }
+    
+    g_free(path);
+    g_free(tmppath);
+    
     return FALSE;
 }
 
@@ -2048,9 +2075,6 @@
     if(fmanager->priv->save_icons_id)
         g_source_remove(fmanager->priv->save_icons_id);
     
-    fmanager->priv->icons_to_save = 
g_list_prepend(fmanager->priv->icons_to_save,
-                                                   
g_object_ref(G_OBJECT(icon)));
-    
     fmanager->priv->save_icons_id = g_timeout_add(SAVE_DELAY,
                                                   
xfdesktop_file_icon_manager_save_icons,
                                                   fmanager);

Modified: xfdesktop/trunk/src/xfdesktop-file-icon-manager.c
===================================================================
--- xfdesktop/trunk/src/xfdesktop-file-icon-manager.c   2007-11-07 05:45:32 UTC 
(rev 26266)
+++ xfdesktop/trunk/src/xfdesktop-file-icon-manager.c   2007-11-07 05:49:14 UTC 
(rev 26267)
@@ -106,7 +106,6 @@
     gboolean show_removable_media;
     gboolean show_special[XFDESKTOP_SPECIAL_FILE_ICON_TRASH+1];
     
-    GList *icons_to_save;
     guint save_icons_id;
     
     GList *deferred_icons;
@@ -791,6 +790,8 @@
         g_list_foreach(selected, (GFunc)g_object_unref, NULL);
         g_list_free(selected);
     }
+    
+    xfdesktop_file_icon_position_changed(NULL, fmanager);
 }
 
 static void
@@ -1982,45 +1983,67 @@
     /* don't free |selected|.  the menu deactivated handler does that */
 }
 
+static void
+file_icon_hash_write_icons(gpointer key,
+                           gpointer value,
+                           gpointer data)
+{
+    XfceRc *rcfile = data;
+    XfdesktopIcon *icon = value;
+    guint16 row, col;
+    
+    if(xfdesktop_icon_get_position(icon, &row, &col)) {
+        xfce_rc_set_group(rcfile, xfdesktop_icon_peek_label(icon));
+        xfce_rc_write_int_entry(rcfile, "row", row);
+        xfce_rc_write_int_entry(rcfile, "col", col);
+    }
+}
+
 static gboolean
 xfdesktop_file_icon_manager_save_icons(gpointer user_data)
 {
     XfdesktopFileIconManager *fmanager = 
XFDESKTOP_FILE_ICON_MANAGER(user_data);
-    gchar relpath[PATH_MAX];
+    gchar relpath[PATH_MAX], *tmppath, *path;
     XfceRc *rcfile;
-    GList *l;
-    XfdesktopIcon *icon;
-    guint16 row, col;
     
     fmanager->priv->save_icons_id = 0;
     
-    g_return_val_if_fail(fmanager->priv->icons_to_save, FALSE);
-    
     g_snprintf(relpath, PATH_MAX, "xfce4/desktop/icons.screen%d.rc",
                gdk_screen_get_number(fmanager->priv->gscreen));
+    path = xfce_resource_save_location(XFCE_RESOURCE_CONFIG, relpath, TRUE);
+    if(!path)
+        return FALSE;
     
-    rcfile = xfce_rc_config_open(XFCE_RESOURCE_CONFIG, relpath, FALSE);
+    tmppath = g_strconcat(path, ".new", NULL);
+    
+    rcfile = xfce_rc_simple_open(tmppath, FALSE);
     if(!rcfile) {
-        g_critical("Unable to determine location of icon position cache file.  
" \
-                   "Icon positions will not be saved.");
+        g_warning("Unable to determine location of icon position cache file.  
" \
+                  "Icon positions will not be saved.");
+        g_free(path);
+        g_free(tmppath);
         return FALSE;
     }
     
-    for(l = fmanager->priv->icons_to_save; l; l = l->next) {
-        icon = XFDESKTOP_ICON(l->data);
-        if(xfdesktop_icon_get_position(icon, &row, &col)) {
-            xfce_rc_set_group(rcfile, xfdesktop_icon_peek_label(icon));
-            xfce_rc_write_int_entry(rcfile, "row", row);
-            xfce_rc_write_int_entry(rcfile, "col", col);
-        }
-        g_object_unref(G_OBJECT(icon));
-    }
-    g_list_free(fmanager->priv->icons_to_save);
-    fmanager->priv->icons_to_save = NULL;
+    g_hash_table_foreach(fmanager->priv->icons,
+                         file_icon_hash_write_icons, rcfile);
+    g_hash_table_foreach(fmanager->priv->removable_icons,
+                         file_icon_hash_write_icons, rcfile);
+    g_hash_table_foreach(fmanager->priv->special_icons,
+                         file_icon_hash_write_icons, rcfile);
     
     xfce_rc_flush(rcfile);
     xfce_rc_close(rcfile);
     
+    if(rename(tmppath, path)) {
+        g_warning("Unable to rename temp file to %s: %s", path,
+                  strerror(errno));
+        unlink(tmppath);
+    }
+    
+    g_free(path);
+    g_free(tmppath);
+    
     return FALSE;
 }
 
@@ -2033,9 +2056,6 @@
     if(fmanager->priv->save_icons_id)
         g_source_remove(fmanager->priv->save_icons_id);
     
-    fmanager->priv->icons_to_save = 
g_list_prepend(fmanager->priv->icons_to_save,
-                                                   
g_object_ref(G_OBJECT(icon)));
-    
     fmanager->priv->save_icons_id = g_timeout_add(SAVE_DELAY,
                                                   
xfdesktop_file_icon_manager_save_icons,
                                                   fmanager);

_______________________________________________
Xfce4-commits mailing list
[email protected]
http://foo-projects.org/mailman/listinfo/xfce4-commits

Reply via email to