[gedit/wip/loader-saver: 1/3] tab: more file loading post-processing
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/loader-saver: 1/3] tab: more file loading post-processing
- Date: Wed, 25 Jun 2014 18:15:11 +0000 (UTC)
commit fbf721b24c1699333aedb35703c8a9bee9f8aadc
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 | 416 ++++++++++++++++++---------------------
2 files changed, 210 insertions(+), 239 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..953a271 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -104,6 +104,11 @@ static guint signals[LAST_SIGNAL] = { 0 };
static gboolean gedit_tab_auto_save (GeditTab *tab);
+static void load (GeditTab *tab,
+ const GtkSourceEncoding *encoding,
+ gint line_pos,
+ gint column_pos);
+
static void
install_auto_save_timeout (GeditTab *tab)
{
@@ -573,37 +578,29 @@ io_loading_error_info_bar_response (GtkWidget *info_bar,
gint response_id,
GeditTab *tab)
{
- GeditDocument *doc;
GeditView *view;
GFile *location;
const GtkSourceEncoding *encoding;
- doc = gedit_tab_get_document (tab);
+ g_return_if_fail (tab->priv->loader != NULL);
+
view = gedit_tab_get_view (tab);
- location = gedit_document_get_location (doc);
+ location = gtk_source_file_loader_get_location (tab->priv->loader);
switch (response_id)
{
case GTK_RESPONSE_OK:
- g_return_if_fail (location != NULL);
-
encoding = gedit_conversion_error_info_bar_get_encoding (GTK_WIDGET (info_bar));
- if (encoding != NULL)
- {
- tab->priv->tmp_encoding = encoding;
- }
-
set_info_bar (tab, NULL, GTK_RESPONSE_NONE);
- gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
-
- _gedit_document_load (doc,
- location,
- tab->priv->tmp_encoding,
- tab->priv->tmp_line_pos,
- tab->priv->tmp_column_pos,
- FALSE);
+
+ tab->priv->load_create = FALSE;
+
+ load (tab,
+ encoding,
+ tab->priv->tmp_line_pos,
+ tab->priv->tmp_column_pos);
break;
case GTK_RESPONSE_YES:
@@ -622,11 +619,6 @@ io_loading_error_info_bar_response (GtkWidget *info_bar,
remove_tab (tab);
break;
}
-
- if (location != NULL)
- {
- g_object_unref (location);
- }
}
static void
@@ -925,193 +917,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 +1434,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 +1833,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 +1879,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);
+
+ 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;
- g_signal_emit_by_name (doc, "loaded", NULL);
+ /* 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);
@@ -2093,7 +2067,6 @@ load (GeditTab *tab,
gint column_pos)
{
g_return_if_fail (GTK_SOURCE_IS_FILE_LOADER (tab->priv->loader));
- g_return_if_fail (tab->priv->cancellable == NULL);
gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
@@ -2114,6 +2087,7 @@ load (GeditTab *tab,
tab->priv->tmp_line_pos = line_pos;
tab->priv->tmp_column_pos = column_pos;
+ g_clear_object (&tab->priv->cancellable);
tab->priv->cancellable = g_cancellable_new ();
/* Keep the tab alive during the async operation. */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]