[gnome-text-editor] session: track encoding when storing session information



commit 0ce4943a43077bda4ae7928fc3cef8a324a1a309
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jun 24 10:54:09 2021 -0700

    session: track encoding when storing session information

 src/editor-document-private.h | 99 ++++++++++++++++++++++---------------------
 src/editor-document.c         |  8 ++++
 src/editor-session.c          | 18 +++++++-
 3 files changed, 74 insertions(+), 51 deletions(-)
---
diff --git a/src/editor-document-private.h b/src/editor-document-private.h
index 157afc4..3df284f 100644
--- a/src/editor-document-private.h
+++ b/src/editor-document-private.h
@@ -24,54 +24,55 @@
 
 G_BEGIN_DECLS
 
-EditorDocument       *_editor_document_new                     (GFile                    *file,
-                                                                const gchar              *draft_id);
-const gchar          *_editor_document_get_draft_id            (EditorDocument           *self);
-void                  _editor_document_set_draft_id            (EditorDocument           *self,
-                                                                const gchar              *draft_id);
-GFile                *_editor_document_get_draft_file          (EditorDocument           *self);
-gchar                *_editor_document_dup_uri                 (EditorDocument           *self);
-void                  _editor_document_mark_busy               (EditorDocument           *self);
-void                  _editor_document_unmark_busy             (EditorDocument           *self);
-void                  _editor_document_set_externally_modified (EditorDocument           *self,
-                                                                gboolean                  
externally_modified);
-gboolean              _editor_document_get_was_restored        (EditorDocument           *self);
-void                  _editor_document_set_was_restored        (EditorDocument           *self,
-                                                                gboolean                  was_restored);
-void                  _editor_document_set_encoding            (EditorDocument           *document,
-                                                                const GtkSourceEncoding  *encoding);
-GtkSourceNewlineType  _editor_document_get_newline_type        (EditorDocument           *self);
-void                  _editor_document_set_newline_type        (EditorDocument           *self,
-                                                                GtkSourceNewlineType      newline_type);
-void                  _editor_document_load_async              (EditorDocument           *self,
-                                                                EditorWindow             *window,
-                                                                GCancellable             *cancellable,
-                                                                GAsyncReadyCallback       callback,
-                                                                gpointer                  user_data);
-gboolean              _editor_document_load_finish             (EditorDocument           *self,
-                                                                GAsyncResult             *result,
-                                                                GError                  **error);
-void                  _editor_document_save_async              (EditorDocument           *self,
-                                                                GFile                    *file,
-                                                                GCancellable             *cancellable,
-                                                                GAsyncReadyCallback       callback,
-                                                                gpointer                  user_data);
-gboolean              _editor_document_save_finish             (EditorDocument           *self,
-                                                                GAsyncResult             *result,
-                                                                GError                  **error);
-void                  _editor_document_save_draft_async        (EditorDocument           *self,
-                                                                GCancellable             *cancellable,
-                                                                GAsyncReadyCallback       callback,
-                                                                gpointer                  user_data);
-gboolean              _editor_document_save_draft_finish       (EditorDocument           *self,
-                                                                GAsyncResult             *result,
-                                                                GError                  **error);
-void                  _editor_document_guess_language_async    (EditorDocument           *self,
-                                                                GCancellable             *cancellable,
-                                                                GAsyncReadyCallback       callback,
-                                                                gpointer                  user_data);
-gboolean              _editor_document_guess_language_finish   (EditorDocument           *self,
-                                                                GAsyncResult             *result,
-                                                                GError                  **error);
+EditorDocument          *_editor_document_new                     (GFile                    *file,
+                                                                   const gchar              *draft_id);
+const gchar             *_editor_document_get_draft_id            (EditorDocument           *self);
+void                     _editor_document_set_draft_id            (EditorDocument           *self,
+                                                                   const gchar              *draft_id);
+GFile                   *_editor_document_get_draft_file          (EditorDocument           *self);
+gchar                   *_editor_document_dup_uri                 (EditorDocument           *self);
+void                     _editor_document_mark_busy               (EditorDocument           *self);
+void                     _editor_document_unmark_busy             (EditorDocument           *self);
+void                     _editor_document_set_externally_modified (EditorDocument           *self,
+                                                                   gboolean                  
externally_modified);
+gboolean                 _editor_document_get_was_restored        (EditorDocument           *self);
+void                     _editor_document_set_was_restored        (EditorDocument           *self,
+                                                                   gboolean                  was_restored);
+const GtkSourceEncoding *_editor_document_get_encoding            (EditorDocument           *self);
+void                     _editor_document_set_encoding            (EditorDocument           *document,
+                                                                   const GtkSourceEncoding  *encoding);
+GtkSourceNewlineType     _editor_document_get_newline_type        (EditorDocument           *self);
+void                     _editor_document_set_newline_type        (EditorDocument           *self,
+                                                                   GtkSourceNewlineType      newline_type);
+void                     _editor_document_load_async              (EditorDocument           *self,
+                                                                   EditorWindow             *window,
+                                                                   GCancellable             *cancellable,
+                                                                   GAsyncReadyCallback       callback,
+                                                                   gpointer                  user_data);
+gboolean                 _editor_document_load_finish             (EditorDocument           *self,
+                                                                   GAsyncResult             *result,
+                                                                   GError                  **error);
+void                     _editor_document_save_async              (EditorDocument           *self,
+                                                                   GFile                    *file,
+                                                                   GCancellable             *cancellable,
+                                                                   GAsyncReadyCallback       callback,
+                                                                   gpointer                  user_data);
+gboolean                 _editor_document_save_finish             (EditorDocument           *self,
+                                                                   GAsyncResult             *result,
+                                                                   GError                  **error);
+void                     _editor_document_save_draft_async        (EditorDocument           *self,
+                                                                   GCancellable             *cancellable,
+                                                                   GAsyncReadyCallback       callback,
+                                                                   gpointer                  user_data);
+gboolean                 _editor_document_save_draft_finish       (EditorDocument           *self,
+                                                                   GAsyncResult             *result,
+                                                                   GError                  **error);
+void                     _editor_document_guess_language_async    (EditorDocument           *self,
+                                                                   GCancellable             *cancellable,
+                                                                   GAsyncReadyCallback       callback,
+                                                                   gpointer                  user_data);
+gboolean                 _editor_document_guess_language_finish   (EditorDocument           *self,
+                                                                   GAsyncResult             *result,
+                                                                   GError                  **error);
 
 G_END_DECLS
diff --git a/src/editor-document.c b/src/editor-document.c
index 12b9203..7d8741f 100644
--- a/src/editor-document.c
+++ b/src/editor-document.c
@@ -1620,6 +1620,14 @@ _editor_document_get_was_restored (EditorDocument *self)
   return self->was_restored;
 }
 
+const GtkSourceEncoding *
+_editor_document_get_encoding (EditorDocument *self)
+{
+  g_return_val_if_fail (EDITOR_IS_DOCUMENT (self), NULL);
+
+  return self->encoding;
+}
+
 void
 _editor_document_set_encoding (EditorDocument          *self,
                                const GtkSourceEncoding *encoding)
diff --git a/src/editor-session.c b/src/editor-session.c
index 62e8b71..cf9c7e5 100644
--- a/src/editor-session.c
+++ b/src/editor-session.c
@@ -217,6 +217,7 @@ add_window_state (EditorSession   *self,
           GtkSourceLanguage *language = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (document));
           GFile *file = editor_document_get_file (document);
           const gchar *draft_id = _editor_document_get_draft_id (document);
+          const GtkSourceEncoding *encoding = _editor_document_get_encoding (document);
           gboolean page_is_active = editor_page_is_active (page);
           GtkTextMark *insert = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (document));
           GtkTextMark *bound = gtk_text_buffer_get_selection_bound (GTK_TEXT_BUFFER (document));
@@ -244,6 +245,10 @@ add_window_state (EditorSession   *self,
             g_variant_builder_add_parsed (builder,
                                           "{'language', <%s>}",
                                           gtk_source_language_get_id (language));
+          if (encoding != NULL)
+            g_variant_builder_add_parsed (builder,
+                                          "{'encoding', <%s>}",
+                                          gtk_source_encoding_get_charset (encoding));
           g_variant_builder_add (builder, "{sv}", "selection", selection_to_variant (&sel));
           if (page_is_active)
             g_variant_builder_add_parsed (builder, "{'is-active', <%b>}", page_is_active);
@@ -1467,9 +1472,11 @@ editor_session_restore_v1_pages (EditorSession *self,
       g_autoptr(EditorDocument) document = NULL;
       g_autoptr(GVariant) sel_value = NULL;
       g_autoptr(GFile) file = NULL;
+      const GtkSourceEncoding *encoding = NULL;
       EditorPage *epage = NULL;
-      const gchar *draft_id;
-      const gchar *uri;
+      const char *draft_id;
+      const char *charset;
+      const char *uri;
       gboolean is_active;
       Selection sel;
 
@@ -1479,6 +1486,9 @@ editor_session_restore_v1_pages (EditorSession *self,
       if (!g_variant_lookup (page, "uri", "&s", &uri))
         uri = NULL;
 
+      if (g_variant_lookup (page, "encoding", "&s", &charset) && charset[0])
+        encoding = gtk_source_encoding_get_from_charset (charset);
+
       if (!(sel_value = g_variant_lookup_value (page, "selection", NULL)) ||
           !selection_from_variant (&sel, sel_value))
         {
@@ -1499,6 +1509,10 @@ editor_session_restore_v1_pages (EditorSession *self,
         file = g_file_new_for_uri (uri);
 
       document = _editor_document_new (file, draft_id);
+
+      if (encoding != NULL)
+        _editor_document_set_encoding (document, encoding);
+
       epage = editor_page_new_for_document (document);
       editor_session_add_page (self, window, epage);
 


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