[Nautilus-list] [PATCH] fix scripts menu for new windows



We used to rely on callbacks to incrementally build the scripts menu,
but this was broken because new windows did not receive the same
"files_added" callback that the initial window did.  To fix this we
build the menus all at once.

This patch also makes the code (but not the patch ;) a little more
readable.

Changelog -

src/file-manager/fm-directory-view.c:

(update_directory_in_scripts_menu): Renamed build_scripts_menu () and
made recursive.

(add_scripts_directory): Moved into add_directory_to_scripts_directory
(add_directory_to_scripts_directory_list): See above.

(remove_scripts_directory): Renamed
remove_directory_from_scripts_directory_list ().
(remove_directory_from_scripts_directory_list): See above.

(clean_scripts_directory_list): New
(remove_scripts_menu): New
(build_scripts_menu): New.
(show_scripts_menu): New.

(update_scripts_menu): Made code more readable.
Index: src/file-manager/fm-directory-view.c
===================================================================
RCS file: /cvs/gnome/nautilus/src/file-manager/fm-directory-view.c,v
retrieving revision 1.504
diff -p -u -r1.504 fm-directory-view.c
--- src/file-manager/fm-directory-view.c	2002/02/14 23:22:00	1.504
+++ src/file-manager/fm-directory-view.c	2002/02/19 05:53:09
@@ -302,6 +302,10 @@ static void     zoomable_zoom_out_callba
 								FMDirectoryView      *directory_view);
 static void     zoomable_zoom_to_fit_callback                  (BonoboZoomable       *zoomable,
 								FMDirectoryView      *directory_view);
+static void     add_directory_to_scripts_directory_list        (FMDirectoryView *view,
+								NautilusDirectory *directory);
+static void     remove_directory_from_scripts_directory_list   (FMDirectoryView      *view,
+								NautilusDirectory    *directory);
 static void     schedule_update_menus                          (FMDirectoryView      *view);
 static void     schedule_update_menus_callback                 (gpointer              callback_data);
 static void     remove_update_menus_timeout_callback           (FMDirectoryView      *view);
@@ -311,8 +315,6 @@ static void     schedule_timeout_display
 static void     unschedule_timeout_display_of_pending_files    (FMDirectoryView      *view);
 static void     unschedule_display_of_pending_files            (FMDirectoryView      *view);
 static void     disconnect_model_handlers                      (FMDirectoryView      *view);
-static void     remove_scripts_directory                       (FMDirectoryView      *view,
-								NautilusDirectory    *directory);
 static void     filtering_changed_callback                     (gpointer              callback_data);
 static void     metadata_for_directory_as_file_ready_callback  (NautilusFile         *file,
 								gpointer              callback_data);
@@ -1120,34 +1122,6 @@ scripts_added_or_changed_callback (Nauti
 }
 
 static void
-add_scripts_directory (FMDirectoryView *view,
-		       NautilusDirectory *directory)
-{
-	GList *attributes;
-
-	nautilus_directory_ref (directory);
-
-	attributes = nautilus_icon_factory_get_required_file_attributes ();
-	attributes = g_list_prepend (attributes, NAUTILUS_FILE_ATTRIBUTE_CAPABILITIES);
-	attributes = g_list_prepend (attributes, NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT);
- 
-	nautilus_directory_file_monitor_add (directory, &view->details->scripts_directory_list,
-					     FALSE, FALSE, attributes,
-					     scripts_added_or_changed_callback, view);
-
-	g_list_free (attributes);
-
-	g_signal_connect (directory, "files_added",
-			  G_CALLBACK (scripts_added_or_changed_callback), view);
-
-	g_signal_connect (directory, "files_changed",
-			  G_CALLBACK (scripts_added_or_changed_callback), view);
-
-	view->details->scripts_directory_list = g_list_prepend
-		(view->details->scripts_directory_list, directory);
-}
-
-static void
 fm_directory_view_init (FMDirectoryView *view)
 {
 	static gboolean setup_autos = FALSE;
@@ -1186,7 +1160,7 @@ fm_directory_view_init (FMDirectoryView 
 	set_up_scripts_directory_global ();
 
 	scripts_directory = nautilus_directory_get (scripts_directory_uri);
-	add_scripts_directory (view, scripts_directory);
+	add_directory_to_scripts_directory_list (view, scripts_directory);
 	nautilus_directory_unref (scripts_directory);
 
 	view->details->zoomable = bonobo_zoomable_new ();
@@ -1288,7 +1262,7 @@ fm_directory_view_finalize (GObject *obj
 
 	for (node = view->details->scripts_directory_list; node != NULL; node = next) {
 		next = node->next;
-		remove_scripts_directory (view, node->data);
+		remove_directory_from_scripts_directory_list (view, node->data);
 	}
 
 	disconnect_model_handlers (view);
@@ -3618,11 +3592,11 @@ run_script_callback (BonoboUIComponent *
 }
 
 static void
-add_script_to_script_menus (FMDirectoryView *directory_view,
-			  NautilusFile *file,
-			  int index,
-			  const char *menu_path,
-			  const char *popup_path)
+add_script_to_scripts_menu (FMDirectoryView *directory_view,
+			    NautilusFile *file,
+			    int index,
+			    const char *menu_path,
+			    const char *popup_path)
 {
 	ScriptLaunchParameters *launch_parameters;
 	char *tip;
@@ -3665,10 +3639,10 @@ add_script_to_script_menus (FMDirectoryV
 }
 
 static void
-add_submenu_to_script_menus (FMDirectoryView *directory_view,
-			  NautilusFile *file,
-			  const char *menu_path,
-			  const char *popup_path)
+add_submenu_to_scripts_menu (FMDirectoryView *directory_view,
+			     NautilusFile *file,
+			     const char *menu_path,
+			     const char *popup_path)
 {
 	char *name;
 	GdkPixbuf *pixbuf;
@@ -3706,47 +3680,99 @@ directory_belongs_in_scripts_menu (const
 	return TRUE;
 }
 
-static gboolean
-add_directory_to_scripts_directory_list (FMDirectoryView *view, NautilusFile *file)
+static void
+add_directory_to_scripts_directory_list (FMDirectoryView *view, NautilusDirectory *directory)
 {
-	char *uri;
-	NautilusDirectory *directory;
+	GList *attributes;
 
-	uri = nautilus_file_get_uri (file);
+	if (g_list_find (view->details->scripts_directory_list, directory) == NULL) {
+		nautilus_directory_ref (directory);
 
-	if (!directory_belongs_in_scripts_menu (uri)) {
-		g_free (uri);
-		return FALSE;
-	}
+		attributes = nautilus_icon_factory_get_required_file_attributes ();
+		attributes = g_list_prepend (attributes, NAUTILUS_FILE_ATTRIBUTE_CAPABILITIES);
+		attributes = g_list_prepend (attributes, NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT);
+ 
+		nautilus_directory_file_monitor_add (directory, &view->details->scripts_directory_list,
+						     FALSE, FALSE, attributes,
+						     scripts_added_or_changed_callback, view);
 
-	directory = nautilus_directory_get (uri);
-	if (g_list_find (view->details->scripts_directory_list, directory) == NULL) {
-		add_scripts_directory (view, directory);
+		g_list_free (attributes);
+
+		g_signal_connect (directory, "files_added",
+				  G_CALLBACK (scripts_added_or_changed_callback), view);
+
+		g_signal_connect (directory, "files_changed",
+				  G_CALLBACK (scripts_added_or_changed_callback), view);
+
+		view->details->scripts_directory_list = g_list_append
+			(view->details->scripts_directory_list, directory);
 	}
+}
+
+static void
+remove_directory_from_scripts_directory_list (FMDirectoryView *view, NautilusDirectory *directory)
+{
+	view->details->scripts_directory_list = g_list_remove
+		(view->details->scripts_directory_list, directory);
+
+	g_signal_handlers_disconnect_by_func (directory,
+					      G_CALLBACK (scripts_added_or_changed_callback),
+					      view);
+
+	nautilus_directory_file_monitor_remove (directory, &view->details->scripts_directory_list);
+
 	nautilus_directory_unref (directory);
+}
 
-	g_free (uri);
-	return TRUE;
+static void
+clean_scripts_directory_list (FMDirectoryView *view)
+{
+	GList *copy, *node;
+	NautilusDirectory *directory;
+	char *uri;
+
+	/* As we walk through the directories, remove any that no longer belong. */
+	copy = g_list_copy (view->details->scripts_directory_list);
+	for (node = copy; node != NULL; node = node->next) {
+		directory = node->data;
+
+		uri = nautilus_directory_get_uri (directory);
+		if (!directory_belongs_in_scripts_menu (uri)) {
+			remove_directory_from_scripts_directory_list (view, directory);
+		}
+		g_free (uri);
+	}
+	g_list_free (copy);
+}
+
+static void
+remove_scripts_menu (FMDirectoryView *view)
+{
+	nautilus_bonobo_remove_menu_items_and_commands
+		(view->details->ui, FM_DIRECTORY_VIEW_MENU_PATH_SCRIPTS_PLACEHOLDER);
+	nautilus_bonobo_remove_menu_items_and_commands
+		(view->details->ui, FM_DIRECTORY_VIEW_POPUP_PATH_SCRIPTS_PLACEHOLDER);
 }
 
 static gboolean
-update_directory_in_scripts_menu (FMDirectoryView *view, NautilusDirectory *directory)
+build_scripts_menu (FMDirectoryView *view, NautilusDirectory *directory)
 {
-	char *directory_uri;
 	char *menu_path, *popup_path;
 	GList *file_list, *filtered, *node;
 	gboolean any_scripts;
-	int i;
 	NautilusFile *file;
+	NautilusDirectory *dir;
+	char *uri;
+	int i;
 
-	directory_uri = nautilus_directory_get_uri (directory);
+	uri = nautilus_directory_get_uri (directory);
 	menu_path = g_strconcat (FM_DIRECTORY_VIEW_MENU_PATH_SCRIPTS_PLACEHOLDER,
-				 directory_uri + scripts_directory_uri_length,
+				 uri + scripts_directory_uri_length,
 				 NULL);
 	popup_path = g_strconcat (FM_DIRECTORY_VIEW_POPUP_PATH_SCRIPTS_PLACEHOLDER,
-				  directory_uri + scripts_directory_uri_length,
+				  uri + scripts_directory_uri_length,
 				  NULL);
-	g_free (directory_uri);
+	g_free (uri);
 
 	file_list = nautilus_directory_get_file_list (directory);
 	filtered = nautilus_file_list_filter_hidden_and_backup (file_list, FALSE, FALSE);
@@ -3760,13 +3786,21 @@ update_directory_in_scripts_menu (FMDire
 		file = node->data;
 
 		if (file_is_launchable (file)) {
-			add_script_to_script_menus (view, file, i++, menu_path, popup_path);
+			add_script_to_scripts_menu (view, file, i++, menu_path, popup_path);
 			any_scripts = TRUE;
 		} else if (nautilus_file_is_directory (file)) {
-			if (add_directory_to_scripts_directory_list (view, file)) {
-				add_submenu_to_script_menus (view, file, menu_path, popup_path);
+			uri = nautilus_file_get_uri (file);
+			if (directory_belongs_in_scripts_menu (uri)) {
+				dir = nautilus_directory_get (uri);
+
+				add_directory_to_scripts_directory_list (view, dir);
+				add_submenu_to_scripts_menu (view, file, menu_path, popup_path);
+				build_scripts_menu (view, dir);
 				any_scripts = TRUE;
+
+				nautilus_directory_unref (dir);
 			}
+			g_free (uri);
 		}
 	}
 
@@ -3779,44 +3813,29 @@ update_directory_in_scripts_menu (FMDire
 }
 
 static void
-update_scripts_menu (FMDirectoryView *view)
+show_scripts_menu (FMDirectoryView *view, gboolean any_scripts)
 {
-	gboolean any_scripts;
-	GList *sorted_copy, *node;
-	NautilusDirectory *directory;
-	char *uri;
-
-	nautilus_bonobo_remove_menu_items_and_commands
-		(view->details->ui, FM_DIRECTORY_VIEW_MENU_PATH_SCRIPTS_PLACEHOLDER);
-	nautilus_bonobo_remove_menu_items_and_commands
-		(view->details->ui, FM_DIRECTORY_VIEW_POPUP_PATH_SCRIPTS_PLACEHOLDER);
-
-	/* As we walk through the directories, remove any that no longer belong. */
-	any_scripts = FALSE;
-	sorted_copy = nautilus_directory_list_sort_by_uri
-		(nautilus_directory_list_copy (view->details->scripts_directory_list));
-	for (node = sorted_copy; node != NULL; node = node->next) {
-		directory = node->data;
-
-		uri = nautilus_directory_get_uri (directory);
-		if (!directory_belongs_in_scripts_menu (uri)) {
-			remove_scripts_directory (view, directory);
-		} else {
-			if (update_directory_in_scripts_menu (view, directory)) {
-				any_scripts = TRUE;
-			}
-		}
-		g_free (uri);
-	}
-	nautilus_directory_list_free (sorted_copy);
-
 	nautilus_bonobo_set_hidden (view->details->ui, 
 				    FM_DIRECTORY_VIEW_MENU_PATH_SCRIPTS_SEPARATOR, 
 				    !any_scripts);
 	nautilus_bonobo_set_hidden (view->details->ui, 
 				    FM_DIRECTORY_VIEW_POPUP_PATH_SCRIPTS_SEPARATOR, 
 				    !any_scripts);
+}
+
+static void
+update_scripts_menu (FMDirectoryView *view)
+{
+	NautilusDirectory *directory;
+	gboolean any_scripts;
+
+	clean_scripts_directory_list (view);
 
+	remove_scripts_menu (view);
+	directory = NAUTILUS_DIRECTORY (view->details->scripts_directory_list->data);
+	any_scripts = build_scripts_menu (view, directory);
+	show_scripts_menu (view, any_scripts);
+
 	view->details->scripts_invalid = FALSE;
 }
 
@@ -5058,21 +5077,6 @@ static void
 disconnect_directory_handler (FMDirectoryView *view, int *id)
 {
 	disconnect_handler (GTK_OBJECT (view->details->model), id);
-}
-
-static void
-remove_scripts_directory (FMDirectoryView *view, NautilusDirectory *directory)
-{
-	view->details->scripts_directory_list = g_list_remove
-		(view->details->scripts_directory_list, directory);
-
-	g_signal_handlers_disconnect_by_func (directory,
-					      G_CALLBACK (scripts_added_or_changed_callback),
-					      view);
-
-	nautilus_directory_file_monitor_remove (directory, &view->details->scripts_directory_list);
-
-	nautilus_directory_unref (directory);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]