[gedit/wip/loader-saver: 19/19] tab: more file loading post-processing (not finished)



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

    tab: more file loading post-processing (not finished)

 gedit/gedit-tab.c |  177 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 175 insertions(+), 2 deletions(-)
---
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 48da20d..db9bc1e 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -931,8 +931,8 @@ document_loaded (GeditDocument *document,
 {
        GFile *location;
 
-       g_return_if_fail ((tab->priv->state == GEDIT_TAB_STATE_LOADING) ||
-                         (tab->priv->state == GEDIT_TAB_STATE_REVERTING));
+       g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_LOADING ||
+                         tab->priv->state == GEDIT_TAB_STATE_REVERTING);
 
        if (tab->priv->timer != NULL)
        {
@@ -2025,6 +2025,103 @@ goto_line (GeditTab *tab)
 }
 
 static void
+document_loaded (GeditDocument *document,
+                const GError  *error,
+                GeditTab      *tab)
+{
+       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;
+}
+
+static void
 load_cb (GtkSourceFileLoader *loader,
         GAsyncResult        *result,
         GeditTab            *tab)
@@ -2033,6 +2130,9 @@ 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)
@@ -2041,6 +2141,14 @@ load_cb (GtkSourceFileLoader *loader,
                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,6 +2177,70 @@ load_cb (GtkSourceFileLoader *loader,
                error = NULL;
        }
 
+       /* 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)
+                       {
+                               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);
+               }
+
+               goto end;
+       }
+
        g_clear_object (&tab->priv->loader);
        g_clear_object (&tab->priv->cancellable);
        tab->priv->load_create = FALSE;
@@ -2077,6 +2249,7 @@ load_cb (GtkSourceFileLoader *loader,
 
        gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
 
+end:
        /* Async operation finished. */
        g_object_unref (tab);
 


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