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



commit ea26ab3f25c0764532b1d40bc1a55613628c9030
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      |   42 ++++++++++++++++++++++++++++++++++++++++++
 src/document_view.vala |   38 ++++++++++++++++++++++++++++++++------
 2 files changed, 74 insertions(+), 6 deletions(-)
---
diff --git a/src/document.vala b/src/document.vala
index 23e8062..ec2839f 100644
--- a/src/document.vala
+++ b/src/document.vala
@@ -599,4 +599,46 @@ public class Document : Gtk.SourceBuffer
 
         return true;
     }
+
+    public void set_metadata (string key, string? val)
+    {
+        FileInfo info = new FileInfo ();
+
+        if (val != null)
+            info.set_attribute_string (key, val);
+        else
+            // Unset the key
+            info.set_attribute (key, FileAttributeType.INVALID, null);
+
+        if (this.location != null)
+        {
+            try
+            {
+                this.location.set_attributes_from_info (info, FileQueryInfoFlags.NONE);
+            }
+            catch (Error error)
+            {
+                warning ("Set document metadata failed: %s", error.message);
+            }
+        }
+    }
+
+    public string? get_metadata (string key)
+    {
+        if (this.location == null)
+            return null;
+
+        try
+        {
+            FileInfo info = this.location.query_info (key, FileQueryInfoFlags.NONE);
+            if (info.has_attribute (key))
+                return info.get_attribute_string (key);
+        }
+        catch (Error error)
+        {
+            warning ("Get document metadata failed: %s", error.message);
+        }
+
+        return null;
+    }
 }
diff --git a/src/document_view.vala b/src/document_view.vala
index cb08d3c..3dbbf4b 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,12 +101,12 @@ 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 (get_spell_language ());
 
-        _spell_checker = new Gspell.Checker (lang);
+        doc.notify["location"].connect (() =>
+        {
+            _spell_checker.set_language (get_spell_language ());
+        });
 
         if (_editor_settings.get_boolean ("highlight-misspelled-words"))
             activate_inline_spell_checker ();
@@ -110,6 +115,20 @@ public class DocumentView : Gtk.SourceView
         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);
+    }
+
     public void scroll_to_cursor (double margin = 0.25)
     {
         scroll_to_mark (this.buffer.get_insert (), margin, false, 0, 0);
@@ -219,7 +238,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]