[gedit/file_too_big] Initial implementation



commit 7e6372853d66fbcbc401998c53a5b8be3f555f66
Author: Jordi Mas <jmas softcatala org>
Date:   Mon Mar 22 17:39:52 2021 +0100

    Initial implementation

 gedit/gedit-io-error-info-bar.c |  95 +++++++++++++++++++++++++++++++++++-
 gedit/gedit-tab.c               | 105 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 199 insertions(+), 1 deletion(-)
---
diff --git a/gedit/gedit-io-error-info-bar.c b/gedit/gedit-io-error-info-bar.c
index 1edbe313a..ae0f3146f 100644
--- a/gedit/gedit-io-error-info-bar.c
+++ b/gedit/gedit-io-error-info-bar.c
@@ -395,6 +395,78 @@ create_combo_box (GtkWidget *info_bar,
        gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
 }
 
+static GtkWidget *
+create_file_too_big_error_info_bar (const gchar *primary_text,
+                                 const gchar *secondary_text,
+                                 gboolean     edit_anyway)
+{
+       GtkWidget *info_bar;
+       GtkWidget *hbox_content;
+       GtkWidget *vbox;
+       gchar *primary_markup;
+       gchar *secondary_markup;
+       GtkWidget *primary_label;
+       GtkWidget *secondary_label;
+
+       info_bar = gtk_info_bar_new ();
+       gtk_info_bar_set_show_close_button (GTK_INFO_BAR (info_bar), TRUE);
+
+       gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
+                                _("_Continue loading"),
+                                GTK_RESPONSE_ACCEPT);
+
+       if (edit_anyway)
+       {
+               gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
+               /* Translators: the access key chosen for this string should be
+                different from other main menu access keys (Open, Edit, View...) */
+                                        _("Edit Any_way"),
+                                        GTK_RESPONSE_YES);
+               gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
+                                              GTK_MESSAGE_WARNING);
+       }
+       else
+       {
+               gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
+                                              GTK_MESSAGE_ERROR);
+       }
+
+       hbox_content = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
+
+       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
+       gtk_box_pack_start (GTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);
+
+       primary_markup = g_strdup_printf ("<b>%s</b>", primary_text);
+       primary_label = gtk_label_new (primary_markup);
+       g_free (primary_markup);
+       gtk_box_pack_start (GTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
+       gtk_label_set_use_markup (GTK_LABEL (primary_label), TRUE);
+       gtk_label_set_line_wrap (GTK_LABEL (primary_label), TRUE);
+       gtk_widget_set_halign (primary_label, GTK_ALIGN_START);
+       gtk_widget_set_can_focus (primary_label, TRUE);
+       gtk_label_set_selectable (GTK_LABEL (primary_label), TRUE);
+
+       if (secondary_text != NULL)
+       {
+               secondary_markup = g_strdup_printf ("<small>%s</small>",
+                                                   secondary_text);
+               secondary_label = gtk_label_new (secondary_markup);
+               g_free (secondary_markup);
+               gtk_box_pack_start (GTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
+               gtk_widget_set_can_focus (secondary_label, TRUE);
+               gtk_label_set_use_markup (GTK_LABEL (secondary_label), TRUE);
+               gtk_label_set_line_wrap (GTK_LABEL (secondary_label), TRUE);
+               gtk_label_set_selectable (GTK_LABEL (secondary_label), TRUE);
+               gtk_widget_set_halign (secondary_label, GTK_ALIGN_START);
+       }
+
+       create_combo_box (info_bar, vbox);
+       gtk_widget_show_all (hbox_content);
+       set_contents (info_bar, hbox_content);
+
+       return info_bar;
+}
+
 static GtkWidget *
 create_conversion_error_info_bar (const gchar *primary_text,
                                  const gchar *secondary_text,
@@ -467,6 +539,7 @@ create_conversion_error_info_bar (const gchar *primary_text,
        return info_bar;
 }
 
+
 GtkWidget *
 gedit_io_loading_error_info_bar_new (GFile                   *location,
                                     const GtkSourceEncoding *encoding,
@@ -480,6 +553,7 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
        GtkWidget *info_bar;
        gboolean edit_anyway = FALSE;
        gboolean convert_error = FALSE;
+       gboolean file_too_big = FALSE;
 
        g_return_val_if_fail (error != NULL, NULL);
        g_return_val_if_fail (error->domain == GTK_SOURCE_FILE_LOADER_ERROR ||
@@ -549,6 +623,19 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
 
                g_free (encoding_name);
        }
+       else if (error->domain == GTK_SOURCE_FILE_LOADER_ERROR &&
+                error->code == GTK_SOURCE_FILE_LOADER_ERROR_TOO_BIG)
+       {
+               error_message = g_strdup_printf (_("The file ā€œ%sā€ is very big."),
+                                                uri_for_display);
+               message_details = g_strconcat (_("The file you opened has some invalid characters. "
+                                              "If you continue editing this file you could corrupt this "
+                                              "document."), "\n",
+                                              _("You can also choose another character encoding and try 
again."),
+                                              NULL);
+               file_too_big = TRUE;
+               edit_anyway = TRUE;
+       }
        else
        {
                parse_error (error, &error_message, &message_details, location, uri_for_display);
@@ -560,7 +647,13 @@ gedit_io_loading_error_info_bar_new (GFile                   *location,
                                                 uri_for_display);
        }
 
-       if (convert_error)
+       if (file_too_big)
+       {
+               info_bar = create_file_too_big_error_info_bar (error_message,
+                                                            message_details,
+                                                            edit_anyway);
+       }
+       else if (convert_error)
        {
                info_bar = create_conversion_error_info_bar (error_message,
                                                             message_details,
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index af72864e2..3e796d315 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -143,6 +143,9 @@ static void launch_loader (GTask                   *loading_task,
 
 static void launch_saver (GTask *saving_task);
 
+static void continue_loading(GTask     *loading_task);
+
+
 static SaverData *
 saver_data_new (void)
 {
@@ -675,6 +678,7 @@ io_loading_error_info_bar_response (GtkWidget *info_bar,
                        set_info_bar (data->tab, NULL, GTK_RESPONSE_NONE);
                        gedit_tab_set_state (data->tab, GEDIT_TAB_STATE_LOADING);
 
+
                        launch_loader (loading_task, encoding);
                        break;
 
@@ -688,6 +692,16 @@ io_loading_error_info_bar_response (GtkWidget *info_bar,
                        g_object_unref (loading_task);
                        break;
 
+
+               case GTK_RESPONSE_ACCEPT:
+                       encoding = gedit_conversion_error_info_bar_get_encoding (GTK_WIDGET (info_bar));
+
+                       set_info_bar (data->tab, NULL, GTK_RESPONSE_NONE);
+                       gedit_tab_set_state (data->tab, GEDIT_TAB_STATE_LOADING);
+
+                       continue_loading (loading_task);
+                       break;
+
                default:
                        if (location != NULL)
                        {
@@ -1978,6 +1992,46 @@ get_candidate_encodings (GeditTab *tab)
        return candidates;
 }
 
+static void
+continue_loading(GTask     *loading_task)
+{
+       LoaderData *data = g_task_get_task_data (loading_task);
+       // Pre loader
+       gtk_source_file_loader_load_async (data->loader,
+                                          G_PRIORITY_DEFAULT,
+                                          g_task_get_cancellable (loading_task),
+                                          (GFileProgressCallback) loader_progress_cb,
+                                          loading_task,
+                                          NULL,
+                                          (GAsyncReadyCallback) load_cb,
+                                          loading_task);
+}
+
+
+
+static
+guint64 get_file_size (GFile *file)
+{
+       GFileInfo *info;
+       GError *error = NULL;
+       guint64 res = 0;
+
+       if (file == NULL){
+               return 0;
+       }
+
+       info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &error);
+
+       if (error) {
+               g_error_free (error);
+               return 0;
+       }
+
+       res = g_file_info_get_size(info);
+       g_object_unref (info);
+       return res;
+}
+
 static void
 launch_loader (GTask                   *loading_task,
               const GtkSourceEncoding *encoding)
@@ -2010,6 +2064,57 @@ launch_loader (GTask                   *loading_task,
 
        data->timer = g_timer_new ();
 
+
+       GFile * file = gtk_source_file_loader_get_location (data->loader);
+       char * name = g_file_get_path (file);
+       guint64 size = get_file_size (file);
+       printf("Open file %s\n", name);
+       printf("Size: %lu\n", size);
+
+       /*if (g_error_matches (error,
+                            GTK_SOURCE_FILE_LOADER_ERROR,
+                            GTK_SOURCE_FILE_LOADER_ERROR_CONVERSION_FALLBACK))*/
+       {
+               GtkWidget *info_bar;
+               GError *error = NULL;
+               const GtkSourceEncoding *encoding;
+               GFile *location = gtk_source_file_loader_get_location (data->loader);
+               set_editable (data->tab, FALSE);
+
+               encoding = gtk_source_file_loader_get_encoding (data->loader);
+
+               error = g_error_new (GTK_SOURCE_FILE_LOADER_ERROR, 0,
+                                                   "%s: Is not a regular file",
+                                                   name);
+               error->code = GTK_SOURCE_FILE_LOADER_ERROR_TOO_BIG;
+
+               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),
+                                 loading_task);
+
+               set_info_bar (data->tab, info_bar, GTK_RESPONSE_CANCEL);
+
+               if (data->tab->state == GEDIT_TAB_STATE_LOADING)
+               {
+                       gtk_widget_show (GTK_WIDGET (data->tab->frame));
+                       gedit_tab_set_state (data->tab, GEDIT_TAB_STATE_LOADING_ERROR);
+               }
+               else
+               {
+                       gedit_tab_set_state (data->tab, GEDIT_TAB_STATE_REVERTING_ERROR);
+               }
+
+               successful_load (loading_task);
+               gedit_recent_add_document (doc);
+
+               g_error_free (error);
+               return;
+       }
+
+       // Pre loader
        gtk_source_file_loader_load_async (data->loader,
                                           G_PRIORITY_DEFAULT,
                                           g_task_get_cancellable (loading_task),


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