[latexila/wip/gspell] spell: store document spell settings in file metadata



commit f0d301c18b3e94c06042dc99d83d6820ca7c739e
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Sep 25 17:23:48 2015 +0200

    spell: store document spell settings in file metadata

 src/document.vala      |   57 ++++++++++++++++++++++++++++++++++++++++++++
 src/document_view.vala |   61 ++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 109 insertions(+), 9 deletions(-)
---
diff --git a/src/document.vala b/src/document.vala
index 23e8062..cb6941c 100644
--- a/src/document.vala
+++ b/src/document.vala
@@ -38,6 +38,7 @@ public class Document : Gtk.SourceBuffer
     private string? encoding = null;
     private bool new_file = true;
     private DocumentStructure _structure = null;
+    private FileInfo _metadata_info = new FileInfo ();
 
     public signal void cursor_moved ();
 
@@ -92,6 +93,18 @@ public class Document : Gtk.SourceBuffer
 
     public void load (File location)
     {
+        // First load metadata so when the notify::location signal is emitted,
+        // get_metadata() works.
+        try
+        {
+            _metadata_info = location.query_info ("metadata::*", FileQueryInfoFlags.NONE);
+        }
+        catch (Error e)
+        {
+            warning ("Get document metadata failed: %s", e.message);
+            _metadata_info = new FileInfo ();
+        }
+
         this.location = location;
 
         try
@@ -189,6 +202,8 @@ public class Document : Gtk.SourceBuffer
 
             RecentManager.get_default ().add_item (location.get_uri ());
             backup_made = true;
+
+            save_metadata ();
         }
         catch (Error e)
         {
@@ -599,4 +614,46 @@ public class Document : Gtk.SourceBuffer
 
         return true;
     }
+
+    private void save_metadata ()
+    {
+        return_if_fail (_metadata_info != null);
+
+        if (this.location == null)
+            return;
+
+        try
+        {
+            this.location.set_attributes_from_info (_metadata_info,
+                FileQueryInfoFlags.NONE);
+        }
+        catch (Error error)
+        {
+            warning ("Set document metadata failed: %s", error.message);
+        }
+    }
+
+    public void set_metadata (string key, string? val)
+    {
+        return_if_fail (_metadata_info != null);
+
+        if (val != null)
+            _metadata_info.set_attribute_string (key, val);
+        else
+            // Unset the key
+            _metadata_info.set_attribute (key, FileAttributeType.INVALID, null);
+
+        save_metadata ();
+    }
+
+    public string? get_metadata (string key)
+    {
+        return_val_if_fail (_metadata_info != null, null);
+
+        if (_metadata_info.has_attribute (key) &&
+            _metadata_info.get_attribute_type (key) == FileAttributeType.STRING)
+            return _metadata_info.get_attribute_string (key);
+
+        return null;
+    }
 }
diff --git a/src/document_view.vala b/src/document_view.vala
index cb08d3c..a82fd10 100644
--- a/src/document_view.vala
+++ b/src/document_view.vala
@@ -23,6 +23,11 @@ public class DocumentView : Gtk.SourceView
 {
     public const double SCROLL_MARGIN = 0.02;
 
+    private static const string METADATA_ATTRIBUTE_SPELL_LANGUAGE =
+        "metadata::latexila-spell-language";
+    private static const string METADATA_ATTRIBUTE_INLINE_SPELL =
+        "metadata::latexila-inline-spell";
+
     private GLib.Settings _editor_settings;
     private Pango.FontDescription _font_desc;
     private Gspell.Checker? _spell_checker = null;
@@ -96,20 +101,51 @@ public class DocumentView : Gtk.SourceView
         }
 
         // spell checking
-        unowned Gspell.Language? lang = null;
-        string lang_key = _editor_settings.get_string ("spell-checking-language");
-        if (lang_key[0] != '\0')
-            lang = Gspell.Language.from_key (lang_key);
-
-        _spell_checker = new Gspell.Checker (lang);
+        _spell_checker = new Gspell.Checker (get_spell_language ());
+        setup_inline_spell_checker ();
 
-        if (_editor_settings.get_boolean ("highlight-misspelled-words"))
-            activate_inline_spell_checker ();
+        doc.notify["location"].connect (() =>
+        {
+            _spell_checker.set_language (get_spell_language ());
+            setup_inline_spell_checker ();
+        });
 
         // forward search
         button_release_event.connect (on_button_release_event);
     }
 
+    private unowned Gspell.Language? get_spell_language ()
+    {
+        Document doc = get_buffer () as Document;
+
+        string? lang_key = doc.get_metadata (METADATA_ATTRIBUTE_SPELL_LANGUAGE);
+        if (lang_key == null)
+            lang_key = _editor_settings.get_string ("spell-checking-language");
+
+        if (lang_key[0] == '\0')
+            return null;
+
+        return Gspell.Language.from_key (lang_key);
+    }
+
+    private void setup_inline_spell_checker ()
+    {
+        Document doc = get_buffer () as Document;
+
+        bool activate;
+
+        string? metadata = doc.get_metadata (METADATA_ATTRIBUTE_INLINE_SPELL);
+        if (metadata != null)
+            activate = metadata == "1";
+        else
+            activate = _editor_settings.get_boolean ("highlight-misspelled-words");
+
+        if (activate)
+            activate_inline_spell_checker ();
+        else
+            deactivate_inline_spell_checker ();
+    }
+
     public void scroll_to_cursor (double margin = 0.25)
     {
         scroll_to_mark (this.buffer.get_insert (), margin, false, 0, 0);
@@ -219,7 +255,14 @@ public class DocumentView : Gtk.SourceView
 
         dialog.run ();
 
-        _spell_checker.set_language (dialog.get_language ());
+        unowned Gspell.Language? lang = dialog.get_language ();
+        _spell_checker.set_language (lang);
+
+        Document doc = get_buffer () as Document;
+        if (lang != null)
+            doc.set_metadata (METADATA_ATTRIBUTE_SPELL_LANGUAGE, lang.to_key ());
+        else
+            doc.set_metadata (METADATA_ATTRIBUTE_SPELL_LANGUAGE, null);
 
         dialog.destroy ();
     }


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