[gedit/wip/loader-saver] tab: more file loading post-processing



commit c865a0961e69741ff64d85944da253f5f4c556ba
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Jun 25 00:22:45 2014 +0200

    tab: more file loading post-processing

 gedit/gedit-io-error-info-bar.c |   33 ++--
 gedit/gedit-tab.c               |  376 +++++++++++++++++++--------------------
 2 files changed, 194 insertions(+), 215 deletions(-)
---
diff --git a/gedit/gedit-io-error-info-bar.c b/gedit/gedit-io-error-info-bar.c
index ce0d02a..2fdb252 100644
--- a/gedit/gedit-io-error-info-bar.c
+++ b/gedit/gedit-io-error-info-bar.c
@@ -346,7 +346,7 @@ parse_error (const GError *error,
 
 GtkWidget *
 gedit_unrecoverable_reverting_error_info_bar_new (GFile        *location,
-                                                     const GError *error)
+                                                 const GError *error)
 {
        gchar *error_message = NULL;
        gchar *message_details = NULL;
@@ -357,8 +357,8 @@ gedit_unrecoverable_reverting_error_info_bar_new (GFile        *location,
 
        g_return_val_if_fail (G_IS_FILE (location), NULL);
        g_return_val_if_fail (error != NULL, NULL);
-       g_return_val_if_fail ((error->domain == GEDIT_DOCUMENT_ERROR) ||
-                             (error->domain == G_IO_ERROR), NULL);
+       g_return_val_if_fail (error->domain == GTK_SOURCE_FILE_LOADER_ERROR ||
+                             error->domain == G_IO_ERROR, NULL);
 
        full_formatted_uri = g_file_get_parse_name (location);
 
@@ -590,7 +590,6 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
        gchar *error_message = NULL;
        gchar *message_details = NULL;
        gchar *full_formatted_uri;
-       gchar *encoding_name;
        gchar *uri_for_display;
        gchar *temp_uri_for_display;
        GtkWidget *info_bar;
@@ -598,11 +597,11 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
        gboolean convert_error = FALSE;
 
        g_return_val_if_fail (error != NULL, NULL);
-       g_return_val_if_fail ((error->domain == G_CONVERT_ERROR) ||
-                             (error->domain == GEDIT_DOCUMENT_ERROR) ||
-                             (error->domain == G_IO_ERROR), NULL);
+       g_return_val_if_fail (error->domain == GTK_SOURCE_FILE_LOADER_ERROR ||
+                             error->domain == G_IO_ERROR ||
+                             error->domain == G_CONVERT_ERROR, NULL);
 
-       if (location)
+       if (location != NULL)
        {
                full_formatted_uri = g_file_get_parse_name (location);
        }
@@ -622,11 +621,6 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
        uri_for_display = g_markup_escape_text (temp_uri_for_display, -1);
        g_free (temp_uri_for_display);
 
-       if (encoding != NULL)
-               encoding_name = gtk_source_encoding_to_string (encoding);
-       else
-               encoding_name = g_strdup ("UTF-8");
-
        if (is_gio_error (error, G_IO_ERROR_TOO_MANY_LINKS))
        {
                message_details = g_strdup (_("The number of followed links is limited and the actual file 
could not be found within this limit."));
@@ -636,16 +630,16 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
                message_details = g_strdup (_("You do not have the permissions necessary to open the file."));
        }
        else if ((is_gio_error (error, G_IO_ERROR_INVALID_DATA) && encoding == NULL) ||
-                (error->domain == GEDIT_DOCUMENT_ERROR &&
-                error->code == GEDIT_DOCUMENT_ERROR_ENCODING_AUTO_DETECTION_FAILED))
+                (error->domain == GTK_SOURCE_FILE_LOADER_ERROR &&
+                 error->code == GTK_SOURCE_FILE_LOADER_ERROR_ENCODING_AUTO_DETECTION_FAILED))
        {
                message_details = g_strconcat (_("Unable to detect the character encoding."), "\n",
                                               _("Please check that you are not trying to open a binary 
file."), "\n",
                                               _("Select a character encoding from the menu and try again."), 
NULL);
                convert_error = TRUE;
        }
-       else if (error->domain == GEDIT_DOCUMENT_ERROR &&
-                error->code == GEDIT_DOCUMENT_ERROR_CONVERSION_FALLBACK)
+       else if (error->domain == GTK_SOURCE_FILE_LOADER_ERROR &&
+                error->code == GTK_SOURCE_FILE_LOADER_ERROR_CONVERSION_FALLBACK)
        {
                error_message = g_strdup_printf (_("There was a problem opening the file “%s”."),
                                                 uri_for_display);
@@ -659,12 +653,16 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
        }
        else if (is_gio_error (error, G_IO_ERROR_INVALID_DATA) && encoding != NULL)
        {
+               gchar *encoding_name = gtk_source_encoding_to_string (encoding);
+
                error_message = g_strdup_printf (_("Could not open the file “%s” using the “%s” character 
encoding."),
                                                 uri_for_display,
                                                 encoding_name);
                message_details = g_strconcat (_("Please check that you are not trying to open a binary 
file."), "\n",
                                               _("Select a different character encoding from the menu and try 
again."), NULL);
                convert_error = TRUE;
+
+               g_free (encoding_name);
        }
        else
        {
@@ -691,7 +689,6 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
        }
 
        g_free (uri_for_display);
-       g_free (encoding_name);
        g_free (error_message);
        g_free (message_details);
 
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 48da20d..724207f 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -925,193 +925,6 @@ scroll_to_cursor (GeditTab *tab)
 }
 
 static void
-document_loaded (GeditDocument *document,
-                const GError  *error,
-                GeditTab      *tab)
-{
-       GFile *location;
-
-       g_return_if_fail ((tab->priv->state == GEDIT_TAB_STATE_LOADING) ||
-                         (tab->priv->state == GEDIT_TAB_STATE_REVERTING));
-
-       if (tab->priv->timer != NULL)
-       {
-               g_timer_destroy (tab->priv->timer);
-               tab->priv->timer = NULL;
-       }
-
-       set_info_bar (tab, NULL, GTK_RESPONSE_NONE);
-
-       location = gedit_document_get_location (document);
-
-       /* if the error is CONVERSION FALLBACK don't treat it as a normal error */
-       if (error != NULL &&
-           (error->domain != GEDIT_DOCUMENT_ERROR || error->code != 
GEDIT_DOCUMENT_ERROR_CONVERSION_FALLBACK))
-       {
-               if (tab->priv->state == GEDIT_TAB_STATE_LOADING)
-               {
-                       gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING_ERROR);
-               }
-               else
-               {
-                       gedit_tab_set_state (tab, GEDIT_TAB_STATE_REVERTING_ERROR);
-               }
-
-               if (error->domain == G_IO_ERROR &&
-                   error->code == G_IO_ERROR_CANCELLED)
-               {
-                       /* remove the tab, but in an idle handler, since
-                        * we are in the handler of doc loaded and we
-                        * don't want doc and tab to be finalized now.
-                        */
-                       g_idle_add ((GSourceFunc) remove_tab_idle, tab);
-
-                       goto end;
-               }
-               else
-               {
-                       GtkWidget *info_bar;
-
-                       if (location != NULL)
-                       {
-                               gedit_recent_remove_if_local (location);
-                       }
-
-                       if (tab->priv->state == GEDIT_TAB_STATE_LOADING_ERROR)
-                       {
-                               info_bar = gedit_io_loading_error_info_bar_new (location,
-                                                                               tab->priv->tmp_encoding,
-                                                                               error);
-                               g_signal_connect (info_bar,
-                                                 "response",
-                                                 G_CALLBACK (io_loading_error_info_bar_response),
-                                                 tab);
-                       }
-                       else
-                       {
-                               g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_REVERTING_ERROR);
-
-                               info_bar = gedit_unrecoverable_reverting_error_info_bar_new (location, error);
-
-                               g_signal_connect (info_bar,
-                                                 "response",
-                                                 G_CALLBACK 
(unrecoverable_reverting_error_info_bar_response),
-                                                 tab);
-                       }
-
-                       set_info_bar (tab, info_bar, GTK_RESPONSE_CANCEL);
-               }
-
-               if (location)
-               {
-                       g_object_unref (location);
-               }
-
-               return;
-       }
-
-       gedit_recent_add_document (document);
-
-       if (error != NULL &&
-           error->domain == GEDIT_DOCUMENT_ERROR &&
-           error->code == GEDIT_DOCUMENT_ERROR_CONVERSION_FALLBACK)
-       {
-               GtkWidget *info_bar;
-
-               /* Set the tab as not editable as we have an error, the
-                  user can decide to make it editable again */
-               tab->priv->editable = FALSE;
-
-               info_bar = gedit_io_loading_error_info_bar_new (location,
-                                                               tab->priv->tmp_encoding,
-                                                               error);
-
-               g_signal_connect (info_bar,
-                                 "response",
-                                 G_CALLBACK (io_loading_error_info_bar_response),
-                                 tab);
-
-               set_info_bar (tab, info_bar, GTK_RESPONSE_CANCEL);
-       }
-
-       /* Scroll to the cursor when the document is loaded, we need
-          to do it in an idle as after the document is loaded the
-          textview is still redrawing and relocating its internals */
-       if (tab->priv->idle_scroll == 0)
-       {
-               tab->priv->idle_scroll = g_idle_add ((GSourceFunc)scroll_to_cursor, tab);
-       }
-
-       /* if the document is readonly we don't care how many times the document
-          is opened */
-       if (!gedit_document_get_readonly (document))
-       {
-               GList *all_documents;
-               GList *l;
-
-               all_documents = gedit_app_get_documents (GEDIT_APP (g_application_get_default ()));
-
-               for (l = all_documents; l != NULL; l = g_list_next (l))
-               {
-                       GeditDocument *cur_doc = l->data;
-
-                       if (cur_doc != document)
-                       {
-                               GFile *cur_location;
-
-                               cur_location = gedit_document_get_location (cur_doc);
-
-                               if (cur_location != NULL && location != NULL &&
-                                   g_file_equal (location, cur_location))
-                               {
-                                       GtkWidget *info_bar;
-
-                                       tab->priv->editable = FALSE;
-
-                                       info_bar = gedit_file_already_open_warning_info_bar_new (location);
-
-                                       g_signal_connect (info_bar,
-                                                         "response",
-                                                         G_CALLBACK 
(file_already_open_warning_info_bar_response),
-                                                         tab);
-
-                                       set_info_bar (tab, info_bar, GTK_RESPONSE_CANCEL);
-
-                                       g_object_unref (cur_location);
-                                       break;
-                               }
-
-                               if (cur_location != NULL)
-                               {
-                                       g_object_unref (cur_location);
-                               }
-                       }
-               }
-
-               g_list_free (all_documents);
-       }
-
-       gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
-
-       if (location == NULL)
-       {
-               /* FIXME: hackish */
-               gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (document), TRUE);
-       }
-
-       tab->priv->ask_if_externally_modified = TRUE;
-
-end:
-       if (location != NULL)
-       {
-               g_object_unref (location);
-       }
-
-       tab->priv->tmp_line_pos = 0;
-       tab->priv->tmp_encoding = NULL;
-}
-
-static void
 document_saving (GeditDocument *document,
                 goffset        size,
                 goffset        total_size,
@@ -1629,11 +1442,6 @@ gedit_tab_init (GeditTab *tab)
                          tab);
 
        g_signal_connect (doc,
-                         "loaded",
-                         G_CALLBACK (document_loaded),
-                         tab);
-
-       g_signal_connect (doc,
                          "saving",
                          G_CALLBACK (document_saving),
                          tab);
@@ -2033,14 +1841,24 @@ load_cb (GtkSourceFileLoader *loader,
        GFile *location = gtk_source_file_loader_get_location (loader);
        GError *error = NULL;
 
+       g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_LOADING ||
+                         tab->priv->state == GEDIT_TAB_STATE_REVERTING);
+
        gtk_source_file_loader_load_finish (loader, result, &error);
 
        if (error != NULL)
        {
                g_warning ("File loading error: %s", error->message);
-               g_error_free (error);
        }
 
+       if (tab->priv->timer != NULL)
+       {
+               g_timer_destroy (tab->priv->timer);
+               tab->priv->timer = NULL;
+       }
+
+       set_info_bar (tab, NULL, GTK_RESPONSE_NONE);
+
        /* Load was successful. */
        if (error == NULL ||
            (error->domain == GTK_SOURCE_FILE_LOADER_ERROR &&
@@ -2069,14 +1887,178 @@ load_cb (GtkSourceFileLoader *loader,
                error = NULL;
        }
 
-       g_clear_object (&tab->priv->loader);
-       g_clear_object (&tab->priv->cancellable);
-       tab->priv->load_create = FALSE;
+       /* If the error is CONVERSION FALLBACK don't treat it as a normal error. */
+       if (error != NULL &&
+           (error->domain != GTK_SOURCE_FILE_LOADER_ERROR ||
+            error->code != GTK_SOURCE_FILE_LOADER_ERROR_CONVERSION_FALLBACK))
+       {
+               if (tab->priv->state == GEDIT_TAB_STATE_LOADING)
+               {
+                       gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING_ERROR);
+               }
+               else
+               {
+                       gedit_tab_set_state (tab, GEDIT_TAB_STATE_REVERTING_ERROR);
+               }
+
+               if (error->domain == G_IO_ERROR &&
+                   error->code == G_IO_ERROR_CANCELLED)
+               {
+                       /* remove the tab, but in an idle handler, since
+                        * we are in the handler of doc loaded and we
+                        * don't want doc and tab to be finalized now.
+                        */
+                       /* FIXME idle still needed? */
+                       /* FIXME if an idle is still needed, it's safer to
+                        * remove the idle source during dispose().
+                        */
+                       g_idle_add ((GSourceFunc) remove_tab_idle, tab);
+               }
+               else
+               {
+                       GtkWidget *info_bar;
+
+                       if (location != NULL)
+                       {
+                               gedit_recent_remove_if_local (location);
+                       }
+
+                       if (tab->priv->state == GEDIT_TAB_STATE_LOADING_ERROR)
+                       {
+                               const GtkSourceEncoding *encoding;
+
+                               encoding = gtk_source_file_loader_get_encoding (loader);
+
+                               info_bar = gedit_io_loading_error_info_bar_new (location, encoding, error);
+
+                               g_signal_connect (info_bar,
+                                                 "response",
+                                                 G_CALLBACK (io_loading_error_info_bar_response),
+                                                 tab);
+                       }
+                       else
+                       {
+                               g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_REVERTING_ERROR);
+
+                               info_bar = gedit_unrecoverable_reverting_error_info_bar_new (location, error);
+
+                               g_signal_connect (info_bar,
+                                                 "response",
+                                                 G_CALLBACK 
(unrecoverable_reverting_error_info_bar_response),
+                                                 tab);
+                       }
+
+                       set_info_bar (tab, info_bar, GTK_RESPONSE_CANCEL);
+               }
+
+               goto end;
+       }
+
+       gedit_recent_add_document (doc);
 
-       g_signal_emit_by_name (doc, "loaded", NULL);
+       if (error != NULL &&
+           error->domain == GTK_SOURCE_FILE_LOADER_ERROR &&
+           error->code == GTK_SOURCE_FILE_LOADER_ERROR_CONVERSION_FALLBACK)
+       {
+               GtkWidget *info_bar;
+               const GtkSourceEncoding *encoding;
+
+               /* Set the tab as not editable as we have an error, the user can
+                * decide to make it editable again.
+                */
+               tab->priv->editable = FALSE;
+
+               encoding = gtk_source_file_loader_get_encoding (loader);
+
+               info_bar = gedit_io_loading_error_info_bar_new (location, encoding, error);
+
+               g_signal_connect (info_bar,
+                                 "response",
+                                 G_CALLBACK (io_loading_error_info_bar_response),
+                                 tab);
+
+               set_info_bar (tab, info_bar, GTK_RESPONSE_CANCEL);
+       }
+
+       /* Scroll to the cursor when the document is loaded, we need to do it in
+        * an idle as after the document is loaded the textview is still
+        * redrawing and relocating its internals.
+        */
+       if (tab->priv->idle_scroll == 0)
+       {
+               tab->priv->idle_scroll = g_idle_add ((GSourceFunc)scroll_to_cursor, tab);
+       }
+
+       /* If the document is readonly we don't care how many times the document
+        * is opened.
+        */
+       if (!gedit_document_get_readonly (doc))
+       {
+               GList *all_documents;
+               GList *l;
+
+               all_documents = gedit_app_get_documents (GEDIT_APP (g_application_get_default ()));
+
+               for (l = all_documents; l != NULL; l = g_list_next (l))
+               {
+                       GeditDocument *cur_doc = l->data;
+
+                       if (cur_doc != doc)
+                       {
+                               GFile *cur_location;
+
+                               cur_location = gedit_document_get_location (cur_doc);
+
+                               if (cur_location != NULL && location != NULL &&
+                                   g_file_equal (location, cur_location))
+                               {
+                                       GtkWidget *info_bar;
+
+                                       tab->priv->editable = FALSE;
+
+                                       info_bar = gedit_file_already_open_warning_info_bar_new (location);
+
+                                       g_signal_connect (info_bar,
+                                                         "response",
+                                                         G_CALLBACK 
(file_already_open_warning_info_bar_response),
+                                                         tab);
+
+                                       set_info_bar (tab, info_bar, GTK_RESPONSE_CANCEL);
+
+                                       g_object_unref (cur_location);
+                                       break;
+                               }
+
+                               if (cur_location != NULL)
+                               {
+                                       g_object_unref (cur_location);
+                               }
+                       }
+               }
+
+               g_list_free (all_documents);
+       }
 
        gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
 
+       if (location == NULL)
+       {
+               /* FIXME: hackish */
+               gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), TRUE);
+       }
+
+       tab->priv->ask_if_externally_modified = TRUE;
+
+       if (error == NULL)
+       {
+               g_clear_object (&tab->priv->loader);
+               g_clear_object (&tab->priv->cancellable);
+               tab->priv->load_create = FALSE;
+
+               g_signal_emit_by_name (doc, "loaded", NULL);
+       }
+
+end:
        /* Async operation finished. */
        g_object_unref (tab);
 


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