[nautilus/wip/gaction_reordered: 36/37] nautilus-view: Drop recursive scripts and templates



commit 7bf152f1e9fa8d19ff48cbb62a8f5c39e678c2c0
Author: Carlos Soriano <carlos sorian89 gmail com>
Date:   Thu Jan 8 14:50:07 2015 +0100

    nautilus-view: Drop recursive scripts and templates
    
    Having support for folders of scripts or templates makes the submenu
    has more than two level of deep. That's discourage in the HIG and
    makes the menu a burden.
    On the other hand the code to handle this is a little complex if we want
    to support it with GMenu.
    
    For these reasons, drop the support for folders inside folders in the
    templates folder and scripts folder, and therefore multiple levels of
    menus in the right click menu.

 src/nautilus-view.c |  553 ++++++++++++---------------------------------------
 1 files changed, 128 insertions(+), 425 deletions(-)
---
diff --git a/src/nautilus-view.c b/src/nautilus-view.c
index a0273d9..28abcac 100644
--- a/src/nautilus-view.c
+++ b/src/nautilus-view.c
@@ -131,8 +131,7 @@
 
 #define NAUTILUS_VIEW_POPUP_PATH_LOCATION                        "/location"
 
-#define MAX_MENU_LEVELS 5
-#define TEMPLATE_LIMIT 30
+#define MENU_ITEMS_LIMIT 30
 
 enum {
        ADD_FILE,
@@ -177,11 +176,9 @@ struct NautilusViewDetails
 
        gboolean supports_zooming;
 
-       GList *scripts_directory_list;
        GtkActionGroup *scripts_action_group;
        guint scripts_merge_id;
        
-       GList *templates_directory_list;
        GtkActionGroup *templates_action_group;
        guint templates_merge_id;
 
@@ -300,8 +297,6 @@ static void     nautilus_view_trash_state_changed_callback     (NautilusTrashMon
 static void     nautilus_view_select_file                      (NautilusView      *view,
                                                                NautilusFile         *file);
 
-static void     update_templates_directory                     (NautilusView *view);
-
 static gboolean file_list_all_are_folders                      (GList *file_list);
 static void unschedule_pop_up_pathbar_context_menu (NautilusView *view);
 
@@ -2048,88 +2043,43 @@ templates_added_or_changed_callback (NautilusDirectory *directory,
 }
 
 static void
-add_directory_to_directory_list (NautilusView *view,
-                                NautilusDirectory *directory,
-                                GList **directory_list,
-                                GCallback changed_callback)
+monitor_directory_changes (NautilusView *view,
+                          NautilusDirectory *directory,
+                          GCallback changed_callback)
 {
        NautilusFileAttributes attributes;
 
-       if (g_list_find (*directory_list, directory) == NULL) {
-               nautilus_directory_ref (directory);
-
-               attributes =
-                       NAUTILUS_FILE_ATTRIBUTES_FOR_ICON |
-                       NAUTILUS_FILE_ATTRIBUTE_INFO |
-                       NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT;
+       nautilus_directory_ref (directory);
 
-               nautilus_directory_file_monitor_add (directory, directory_list,
-                                                    FALSE, attributes,
-                                                    (NautilusDirectoryCallback)changed_callback, view);
+       attributes =
+               NAUTILUS_FILE_ATTRIBUTES_FOR_ICON |
+               NAUTILUS_FILE_ATTRIBUTE_INFO |
+               NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT;
 
-               g_signal_connect_object (directory, "files-added",
-                                        G_CALLBACK (changed_callback), view, 0);
-               g_signal_connect_object (directory, "files-changed",
-                                        G_CALLBACK (changed_callback), view, 0);
+       nautilus_directory_file_monitor_add (directory, view,
+                                            FALSE, attributes,
+                                            (NautilusDirectoryCallback)changed_callback, view);
 
-               *directory_list = g_list_append (*directory_list, directory);
-       }
+       g_signal_connect_object (directory, "files-added",
+                                G_CALLBACK (changed_callback), view, 0);
+       g_signal_connect_object (directory, "files-changed",
+                                G_CALLBACK (changed_callback), view, 0);
 }
 
 static void
-remove_directory_from_directory_list (NautilusView *view,
-                                     NautilusDirectory *directory,
-                                     GList **directory_list,
-                                     GCallback changed_callback)
+remove_monitor_directory_changes (NautilusView *view,
+                                 NautilusDirectory *directory,
+                                 GCallback changed_callback)
 {
-       *directory_list = g_list_remove (*directory_list, directory);
-
        g_signal_handlers_disconnect_by_func (directory,
                                              G_CALLBACK (changed_callback),
                                              view);
 
-       nautilus_directory_file_monitor_remove (directory, directory_list);
+       nautilus_directory_file_monitor_remove (directory, view);
 
        nautilus_directory_unref (directory);
 }
 
-
-static void
-add_directory_to_scripts_directory_list (NautilusView *view,
-                                        NautilusDirectory *directory)
-{
-       add_directory_to_directory_list (view, directory,
-                                        &view->details->scripts_directory_list,
-                                        G_CALLBACK (scripts_added_or_changed_callback));
-}
-
-static void
-remove_directory_from_scripts_directory_list (NautilusView *view,
-                                             NautilusDirectory *directory)
-{
-       remove_directory_from_directory_list (view, directory,
-                                             &view->details->scripts_directory_list,
-                                             G_CALLBACK (scripts_added_or_changed_callback));
-}
-
-static void
-add_directory_to_templates_directory_list (NautilusView *view,
-                                          NautilusDirectory *directory)
-{
-       add_directory_to_directory_list (view, directory,
-                                        &view->details->templates_directory_list,
-                                        G_CALLBACK (templates_added_or_changed_callback));
-}
-
-static void
-remove_directory_from_templates_directory_list (NautilusView *view,
-                                               NautilusDirectory *directory)
-{
-       remove_directory_from_directory_list (view, directory,
-                                             &view->details->templates_directory_list,
-                                             G_CALLBACK (templates_added_or_changed_callback));
-}
-
 static void
 slot_active (NautilusWindowSlot *slot,
             NautilusView *view)
@@ -2349,7 +2299,9 @@ nautilus_view_init (NautilusView *view)
 
        if (set_up_scripts_directory_global ()) {
                scripts_directory = nautilus_directory_get_by_uri (scripts_directory_uri);
-               add_directory_to_scripts_directory_list (view, scripts_directory);
+               monitor_directory_changes (view,
+                                          scripts_directory,
+                                          G_CALLBACK (scripts_added_or_changed_callback));
                nautilus_directory_unref (scripts_directory);
        } else {
                g_warning ("Ignoring scripts directory, it may be a broken link\n");
@@ -2359,10 +2311,11 @@ nautilus_view_init (NautilusView *view)
                templates_uri = nautilus_get_templates_directory_uri ();
                templates_directory = nautilus_directory_get_by_uri (templates_uri);
                g_free (templates_uri);
-               add_directory_to_templates_directory_list (view, templates_directory);
+               monitor_directory_changes (view,
+                                          templates_directory,
+                                          G_CALLBACK (templates_added_or_changed_callback));
                nautilus_directory_unref (templates_directory);
        }
-       update_templates_directory (view);
 
        view->details->sort_directories_first =
                g_settings_get_boolean (nautilus_preferences, NAUTILUS_PREFERENCES_SORT_DIRECTORIES_FIRST);
@@ -2441,7 +2394,9 @@ static void
 nautilus_view_destroy (GtkWidget *object)
 {
        NautilusView *view;
-       GList *node, *next;
+       NautilusDirectory *scripts_directory;
+       NautilusDirectory *templates_directory;
+       gchar *templates_uri;
 
        view = NAUTILUS_VIEW (object);
 
@@ -2450,16 +2405,6 @@ nautilus_view_destroy (GtkWidget *object)
        nautilus_view_unmerge_menus (view);
        nautilus_view_stop_loading (view);
 
-       for (node = view->details->scripts_directory_list; node != NULL; node = next) {
-               next = node->next;
-               remove_directory_from_scripts_directory_list (view, node->data);
-       }
-
-       for (node = view->details->templates_directory_list; node != NULL; node = next) {
-               next = node->next;
-               remove_directory_from_templates_directory_list (view, node->data);
-       }
-
        while (view->details->subdirectory_list != NULL) {
                nautilus_view_remove_subdirectory (view,
                                                   view->details->subdirectory_list->data);
@@ -2468,6 +2413,24 @@ nautilus_view_destroy (GtkWidget *object)
        remove_update_menus_timeout_callback (view);
        remove_update_status_idle_callback (view);
 
+       scripts_directory = nautilus_directory_get_by_uri (scripts_directory_uri);
+       if (scripts_directory) {
+               remove_monitor_directory_changes (view,
+                                                 scripts_directory,
+                                                 G_CALLBACK (scripts_added_or_changed_callback));
+               nautilus_directory_unref (scripts_directory);
+       }
+
+       if (nautilus_should_use_templates_directory ()) {
+               templates_uri = nautilus_get_templates_directory_uri ();
+               templates_directory = nautilus_directory_get_by_uri (templates_uri);
+               g_free (templates_uri);
+               remove_monitor_directory_changes (view,
+                                                 templates_directory,
+                                                 G_CALLBACK (templates_added_or_changed_callback));
+               nautilus_directory_unref (templates_directory);
+       }
+
        if (view->details->display_selection_idle_id != 0) {
                g_source_remove (view->details->display_selection_idle_id);
                view->details->display_selection_idle_id = 0;
@@ -3884,59 +3847,6 @@ escape_action_path (const char *action_path)
        return g_string_free (s, FALSE);
 }
 
-
-static void
-add_submenu (GtkUIManager *ui_manager,
-            GtkActionGroup *action_group,
-            guint merge_id,
-            const char *parent_path,
-            const char *uri,
-            const char *label,
-            GdkPixbuf *pixbuf,
-            gboolean add_action)
-{
-       char *escaped_label;
-       char *action_name;
-       char *submenu_name;
-       char *escaped_submenu_name;
-       GtkAction *action;
-       
-       if (parent_path != NULL) {
-               action_name = nautilus_escape_action_name (uri, "submenu_");
-               submenu_name = g_path_get_basename (uri);
-               escaped_submenu_name = escape_action_path (submenu_name);
-               escaped_label = eel_str_double_underscores (label);
-
-               if (add_action) {
-                       action = gtk_action_new (action_name,
-                                                escaped_label,
-                                                NULL,
-                                                NULL);
-                       if (pixbuf != NULL) {
-                               gtk_action_set_gicon (action, G_ICON (pixbuf));
-                       }
-                       
-                       g_object_set (action, "hide-if-empty", FALSE, NULL);
-                       
-                       gtk_action_group_add_action (action_group,
-                                                    action);
-                       g_object_unref (action);
-               }
-
-               gtk_ui_manager_add_ui (ui_manager,
-                                      merge_id,
-                                      parent_path,
-                                      escaped_submenu_name,
-                                      action_name,
-                                      GTK_UI_MANAGER_MENU,
-                                      FALSE);
-               g_free (action_name);
-               g_free (escaped_label);
-               g_free (submenu_name);
-               g_free (escaped_submenu_name);
-       }
-}
-
 static void
 menu_item_show_image (GtkUIManager *ui_manager,
                      const char   *parent_path,
@@ -4709,10 +4619,7 @@ run_script_callback (GtkAction *action, gpointer callback_data)
 
 static void
 add_script_to_scripts_menus (NautilusView *directory_view,
-                            NautilusFile *file,
-                            const char *menu_path,
-                            const char *popup_path, 
-                            const char *popup_bg_path)
+                            NautilusFile *file)
 {
        ScriptLaunchParameters *launch_parameters;
        char *tip;
@@ -4720,6 +4627,8 @@ add_script_to_scripts_menus (NautilusView *directory_view,
        char *uri;
        char *action_name;
        char *escaped_label;
+       char *menu_path, *popup_path, *popup_bg_path;
+       char *escaped_path;
        GdkPixbuf *pixbuf;
        GtkUIManager *ui_manager;
        GtkAction *action;
@@ -4728,6 +4637,18 @@ add_script_to_scripts_menus (NautilusView *directory_view,
        uri = nautilus_file_get_uri (file);
        tip = g_strdup_printf (_("Run “%s” on any selected items"), name);
 
+       escaped_path = escape_action_path (scripts_directory_uri + strlen (scripts_directory_uri));
+       menu_path = g_strconcat (NAUTILUS_VIEW_MENU_PATH_SCRIPTS_PLACEHOLDER,
+                                escaped_path,
+                                NULL);
+       popup_path = g_strconcat (NAUTILUS_VIEW_POPUP_PATH_SCRIPTS_PLACEHOLDER,
+                                 escaped_path,
+                                 NULL);
+       popup_bg_path = g_strconcat (NAUTILUS_VIEW_POPUP_PATH_BACKGROUND_SCRIPTS_PLACEHOLDER,
+                                    escaped_path,
+                                    NULL);
+       g_free (escaped_path);
+
        launch_parameters = script_launch_parameters_new (file, directory_view);
 
        action_name = nautilus_escape_action_name (uri, "script_");
@@ -4786,132 +4707,19 @@ add_script_to_scripts_menus (NautilusView *directory_view,
        g_free (tip);
        g_free (action_name);
        g_free (escaped_label);
-}
-
-static void
-add_submenu_to_directory_menus (NautilusView *directory_view,
-                               GtkActionGroup *action_group,
-                               guint merge_id,
-                               NautilusFile *file,
-                               const char *menu_path,
-                               const char *popup_path,
-                               const char *popup_bg_path)
-{
-       char *name;
-       GdkPixbuf *pixbuf;
-       char *uri;
-       GtkUIManager *ui_manager;
-
-       ui_manager = nautilus_view_get_ui_manager (directory_view);
-       uri = nautilus_file_get_uri (file);
-       name = nautilus_file_get_display_name (file);
-       pixbuf = get_menu_icon_for_file (file, GTK_WIDGET (directory_view));
-       add_submenu (ui_manager, action_group, merge_id, menu_path, uri, name, pixbuf, TRUE);
-       add_submenu (ui_manager, action_group, merge_id, popup_path, uri, name, pixbuf, FALSE);
-       add_submenu (ui_manager, action_group, merge_id, popup_bg_path, uri, name, pixbuf, FALSE);
-       if (pixbuf) {
-               g_object_unref (pixbuf);
-       }
-       g_free (name);
-       g_free (uri);
-}
-
-static gboolean
-directory_belongs_in_scripts_menu (const char *uri)
-{
-       int num_levels;
-       int i;
-
-       if (!g_str_has_prefix (uri, scripts_directory_uri)) {
-               return FALSE;
-       }
-
-       num_levels = 0;
-       for (i = scripts_directory_uri_length; uri[i] != '\0'; i++) {
-               if (uri[i] == '/') {
-                       num_levels++;
-               }
-       }
-
-       if (num_levels > MAX_MENU_LEVELS) {
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
-static gboolean
-update_directory_in_scripts_menu (NautilusView *view, NautilusDirectory *directory)
-{
-       char *menu_path, *popup_path, *popup_bg_path;
-       GList *file_list, *filtered, *node;
-       gboolean any_scripts;
-       NautilusFile *file;
-       NautilusDirectory *dir;
-       char *uri;
-       char *escaped_path;
-       
-       uri = nautilus_directory_get_uri (directory);
-       escaped_path = escape_action_path (uri + scripts_directory_uri_length);
-       g_free (uri);
-       menu_path = g_strconcat (NAUTILUS_VIEW_MENU_PATH_SCRIPTS_PLACEHOLDER,
-                                escaped_path,
-                                NULL);
-       popup_path = g_strconcat (NAUTILUS_VIEW_POPUP_PATH_SCRIPTS_PLACEHOLDER,
-                                 escaped_path,
-                                 NULL);
-       popup_bg_path = g_strconcat (NAUTILUS_VIEW_POPUP_PATH_BACKGROUND_SCRIPTS_PLACEHOLDER,
-                                    escaped_path,
-                                    NULL);
-       g_free (escaped_path);
-
-       file_list = nautilus_directory_get_file_list (directory);
-       filtered = nautilus_file_list_filter_hidden (file_list, FALSE);
-       nautilus_file_list_free (file_list);
-
-       file_list = nautilus_file_list_sort_by_display_name (filtered);
-
-       any_scripts = FALSE;
-       for (node = file_list; node != NULL; node = node->next) {
-               file = node->data;
-
-               if (nautilus_file_is_launchable (file)) {
-                       add_script_to_scripts_menus (view, file, menu_path, popup_path, popup_bg_path);
-                       any_scripts = TRUE;
-               } else if (nautilus_file_is_directory (file)) {
-                       uri = nautilus_file_get_uri (file);
-                       if (directory_belongs_in_scripts_menu (uri)) {
-                               dir = nautilus_directory_get_by_uri (uri);
-                               add_directory_to_scripts_directory_list (view, dir);
-                               nautilus_directory_unref (dir);
-
-                               add_submenu_to_directory_menus (view,
-                                                               view->details->scripts_action_group,
-                                                               view->details->scripts_merge_id,
-                                                               file, menu_path, popup_path, popup_bg_path);
-
-                               any_scripts = TRUE;
-                       }
-                       g_free (uri);
-               }
-       }
-
-       nautilus_file_list_free (file_list);
-
-       g_free (popup_path);
-       g_free (popup_bg_path);
        g_free (menu_path);
-
-       return any_scripts;
+       g_free (popup_bg_path);
+       g_free (popup_path);
 }
 
 static void
 update_scripts_menu (NautilusView *view)
 {
        gboolean any_scripts;
-       GList *sorted_copy, *node;
-       NautilusDirectory *directory;
-       char *uri;
+       GList *file_list, *filtered, *node;
+       NautilusFile *file;
+       NautilusDirectory *scripts_directory;
+       gint num;
        GtkUIManager *ui_manager;
        GtkAction *action;
 
@@ -4930,22 +4738,26 @@ update_scripts_menu (NautilusView *view)
                                      &view->details->scripts_merge_id,
                                      &view->details->scripts_action_group);
 
-       /* As we walk through the directories, remove any that no longer belong. */
+       scripts_directory = nautilus_directory_get_by_uri (scripts_directory_uri);
+
+       file_list = nautilus_directory_get_file_list (scripts_directory);
+       filtered = nautilus_file_list_filter_hidden (file_list, FALSE);
+       nautilus_file_list_free (file_list);
+
+       file_list = nautilus_file_list_sort_by_display_name (filtered);
+
        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_directory_from_scripts_directory_list (view, directory);
-               } else if (update_directory_in_scripts_menu (view, directory)) {
+       num = 0;
+       for (node = file_list; num < MENU_ITEMS_LIMIT && node != NULL; node = node->next, num++) {
+               file = node->data;
+
+               if (nautilus_file_is_launchable (file)) {
+                       add_script_to_scripts_menus (view, file);
                        any_scripts = TRUE;
                }
-               g_free (uri);
        }
-       nautilus_directory_list_free (sorted_copy);
+
+       nautilus_file_list_free (file_list);
 
        action = gtk_action_group_get_action (view->details->dir_action_group, NAUTILUS_ACTION_SCRIPTS);
        gtk_action_set_visible (action, any_scripts);
@@ -4963,14 +4775,14 @@ create_template_callback (GtkAction *action, gpointer callback_data)
 
 static void
 add_template_to_templates_menus (NautilusView *directory_view,
-                                NautilusFile *file,
-                                const char *menu_path,
-                                const char *popup_bg_path)
+                                NautilusFile *file)
 {
-       char *tmp, *tip, *uri, *name;
+       char *tmp, *tip, *uri, *escaped_path, *name;
        char *escaped_label;
        GdkPixbuf *pixbuf;
        char *action_name;
+       char *templates_directory_uri;
+       char *menu_path, *popup_bg_path;
        CreateTemplateParameters *parameters;
        GtkUIManager *ui_manager;
        GtkAction *action;
@@ -4982,6 +4794,18 @@ add_template_to_templates_menus (NautilusView *directory_view,
        uri = nautilus_file_get_uri (file);
        tip = g_strdup_printf (_("Create a new document from template “%s”"), name);
 
+       templates_directory_uri = nautilus_get_templates_directory_uri ();
+       escaped_path = escape_action_path (templates_directory_uri + strlen (templates_directory_uri));
+       g_free (templates_directory_uri);
+
+       menu_path = g_strconcat (NAUTILUS_VIEW_MENU_PATH_NEW_DOCUMENTS_PLACEHOLDER,
+                                escaped_path,
+                                NULL);
+       popup_bg_path = g_strconcat (NAUTILUS_VIEW_POPUP_PATH_BACKGROUND_NEW_DOCUMENTS_PLACEHOLDER,
+                                    escaped_path,
+                                    NULL);
+       g_free (escaped_path);
+
        action_name = nautilus_escape_action_name (uri, "template_");
        escaped_label = eel_str_double_underscores (name);
        
@@ -5033,143 +4857,22 @@ add_template_to_templates_menus (NautilusView *directory_view,
        g_free (tip);
        g_free (uri);
        g_free (action_name);
-}
-
-static void
-update_templates_directory (NautilusView *view)
-{
-       NautilusDirectory *templates_directory;
-       GList *node, *next;
-       char *templates_uri;
-
-       for (node = view->details->templates_directory_list; node != NULL; node = next) {
-               next = node->next;
-               remove_directory_from_templates_directory_list (view, node->data);
-       }
-       
-       if (nautilus_should_use_templates_directory ()) {
-               templates_uri = nautilus_get_templates_directory_uri ();
-               templates_directory = nautilus_directory_get_by_uri (templates_uri);
-               g_free (templates_uri);
-               add_directory_to_templates_directory_list (view, templates_directory);
-               nautilus_directory_unref (templates_directory);
-       }
-}
-
-static gboolean
-directory_belongs_in_templates_menu (const char *templates_directory_uri,
-                                    const char *uri)
-{
-       int num_levels;
-       int i;
-
-       if (templates_directory_uri == NULL) {
-               return FALSE;
-       }
-       
-       if (!g_str_has_prefix (uri, templates_directory_uri)) {
-               return FALSE;
-       }
-
-       num_levels = 0;
-       for (i = strlen (templates_directory_uri); uri[i] != '\0'; i++) {
-               if (uri[i] == '/') {
-                       num_levels++;
-               }
-       }
-
-       if (num_levels > MAX_MENU_LEVELS) {
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
-static gboolean
-update_directory_in_templates_menu (NautilusView *view,
-                                   const char *templates_directory_uri,
-                                   NautilusDirectory *directory)
-{
-       char *menu_path, *popup_bg_path;
-       GList *file_list, *filtered, *node;
-       gboolean any_templates;
-       NautilusFile *file;
-       NautilusDirectory *dir;
-       char *escaped_path;
-       char *uri;
-       int num;
-
-       /* We know this directory belongs to the template dir, so it must exist */
-       g_assert (templates_directory_uri);
-       
-       uri = nautilus_directory_get_uri (directory);
-       escaped_path = escape_action_path (uri + strlen (templates_directory_uri));
-       g_free (uri);
-       menu_path = g_strconcat (NAUTILUS_VIEW_MENU_PATH_NEW_DOCUMENTS_PLACEHOLDER,
-                                escaped_path,
-                                NULL);
-       popup_bg_path = g_strconcat (NAUTILUS_VIEW_POPUP_PATH_BACKGROUND_NEW_DOCUMENTS_PLACEHOLDER,
-                                    escaped_path,
-                                    NULL);
-       g_free (escaped_path);
-
-       file_list = nautilus_directory_get_file_list (directory);
-       filtered = nautilus_file_list_filter_hidden (file_list, FALSE);
-       nautilus_file_list_free (file_list);
-
-       file_list = nautilus_file_list_sort_by_display_name (filtered);
-
-       num = 0;
-       any_templates = FALSE;
-       for (node = file_list; num < TEMPLATE_LIMIT && node != NULL; node = node->next, num++) {
-               file = node->data;
-
-               if (nautilus_file_is_directory (file)) {
-                       uri = nautilus_file_get_uri (file);
-                       if (directory_belongs_in_templates_menu (templates_directory_uri, uri)) {
-                               dir = nautilus_directory_get_by_uri (uri);
-                               add_directory_to_templates_directory_list (view, dir);
-                               nautilus_directory_unref (dir);
-
-                               add_submenu_to_directory_menus (view,
-                                                               view->details->templates_action_group,
-                                                               view->details->templates_merge_id,
-                                                               file, menu_path, NULL, popup_bg_path);
-
-                               any_templates = TRUE;
-                       }
-                       g_free (uri);
-               } else if (nautilus_file_can_read (file)) {
-                       add_template_to_templates_menus (view, file, menu_path, popup_bg_path);
-                       any_templates = TRUE;
-               }
-       }
-
-       nautilus_file_list_free (file_list);
-
-       g_free (popup_bg_path);
        g_free (menu_path);
-
-       return any_templates;
+       g_free (popup_bg_path);
 }
 
-
-
 static void
 update_templates_menu (NautilusView *view)
 {
-       gboolean any_templates;
-       GList *sorted_copy, *node;
-       NautilusDirectory *directory;
+       GList *file_list, *filtered, *node;
+       NautilusFile *file;
+       gint num;
+       NautilusDirectory *templates_directory;
+       gchar *templates_directory_uri;
        GtkUIManager *ui_manager;
-       char *uri;
-       char *templates_directory_uri;
 
-       if (nautilus_should_use_templates_directory ()) {
-               templates_directory_uri = nautilus_get_templates_directory_uri ();
-       } else {
-               templates_directory_uri = NULL;
-       }
+       if (!nautilus_should_use_templates_directory ())
+               return;
 
        /* There is a race condition here.  If we don't mark the scripts menu as
           valid before we begin our task then we can lose template menu updates that
@@ -5186,28 +4889,28 @@ update_templates_menu (NautilusView *view)
                                      &view->details->templates_merge_id,
                                      &view->details->templates_action_group);
 
-       /* As we walk through the directories, remove any that no longer belong. */
-       any_templates = FALSE;
-       sorted_copy = nautilus_directory_list_sort_by_uri
-               (nautilus_directory_list_copy (view->details->templates_directory_list));
-       for (node = sorted_copy; node != NULL; node = node->next) {
-               directory = node->data;
-
-               uri = nautilus_directory_get_uri (directory);
-               if (!directory_belongs_in_templates_menu (templates_directory_uri, uri)) {
-                       remove_directory_from_templates_directory_list (view, directory);
-               } else if (update_directory_in_templates_menu (view,
-                                                              templates_directory_uri,
-                                                              directory)) {
-                       any_templates = TRUE;
-               }
-               g_free (uri);
-       }
-       nautilus_directory_list_free (sorted_copy);
+       templates_directory_uri = nautilus_get_templates_directory_uri ();
+       templates_directory = nautilus_directory_get_by_uri (templates_directory_uri);
+       g_free (templates_directory_uri);
+
+       file_list = nautilus_directory_get_file_list (templates_directory);
+       filtered = nautilus_file_list_filter_hidden (file_list, FALSE);
+       nautilus_file_list_free (file_list);
 
-       view->details->templates_present = any_templates;
+       file_list = nautilus_file_list_sort_by_display_name (filtered);
 
-       g_free (templates_directory_uri);
+       view->details->templates_present = FALSE;
+       num = 0;
+       for (node = file_list; num < MENU_ITEMS_LIMIT && node != NULL; node = node->next, num++) {
+               file = node->data;
+
+               if (nautilus_file_can_read (file) &&
+                   !nautilus_file_is_directory (file)) {
+                       add_template_to_templates_menus (view, file);
+                       view->details->templates_present = TRUE;
+               }
+       }
+       nautilus_file_list_free (file_list);
 }
 
 


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