[gnome-builder/wip/slaf/spellcheck-sidebar: 23/33] spellchecker: IdeEditorSpellDict add/remove/contains support



commit 84f2455bf3b88a907507fc542221df8369ef35cd
Author: Sébastien Lafargue <slafargue gnome org>
Date:   Tue Jan 10 22:10:28 2017 +0100

    spellchecker: IdeEditorSpellDict add/remove/contains support

 libide/editor/ide-editor-spell-dict.c    |  102 +++++++++++++++++++++++++++++-
 libide/editor/ide-editor-spell-dict.h    |   32 +++++----
 libide/editor/ide-editor-spell-widget.c  |   11 ++--
 libide/editor/ide-editor-spell-widget.ui |    2 +-
 4 files changed, 125 insertions(+), 22 deletions(-)
---
diff --git a/libide/editor/ide-editor-spell-dict.c b/libide/editor/ide-editor-spell-dict.c
index 880d81b..8e60054 100644
--- a/libide/editor/ide-editor-spell-dict.c
+++ b/libide/editor/ide-editor-spell-dict.c
@@ -16,7 +16,10 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/* This GObject exists until Gspell handle managing the content of a dict */
+
 #include "ide-editor-spell-dict.h"
+#include <enchant.h>
 #include <gspell/gspell.h>
 
 #include <ide.h>
@@ -26,6 +29,8 @@ struct _IdeEditorSpellDict
   GtkBin                parent_instance;
 
   GspellChecker        *checker;
+  EnchantBroker        *broker;
+  EnchantDict          *dict;
   const GspellLanguage *language;
 };
 
@@ -139,6 +144,68 @@ open_file_cb (GObject      *object,
   read_line_async (g_steal_pointer (&task));
 }
 
+gboolean
+ide_editor_spell_dict_add_word_to_personal (IdeEditorSpellDict *self,
+                                            const gchar        *word)
+{
+  g_assert (IDE_IS_EDITOR_SPELL_DICT (self));
+  g_assert (!ide_str_empty0 (word));
+
+ if (self->dict != NULL)
+    {
+      if (enchant_dict_is_added (self->dict, word, -1))
+        return FALSE;
+
+      enchant_dict_add (self->dict, word, -1);
+      return TRUE;
+    }
+  else
+    {
+      g_warning ("No dictionaries loaded");
+      return FALSE;
+    }
+}
+
+gboolean
+ide_editor_spell_dict_remove_word_from_personal (IdeEditorSpellDict *self,
+                                                 const gchar        *word)
+{
+  g_assert (IDE_IS_EDITOR_SPELL_DICT (self));
+  g_assert (!ide_str_empty0 (word));
+
+  if (self->dict != NULL)
+    {
+      if (!enchant_dict_is_added (self->dict, word, -1))
+        return FALSE;
+
+      enchant_dict_remove (self->dict, word, -1);
+      return TRUE;
+    }
+  else
+    {
+      g_warning ("No dictionaries loaded");
+      return FALSE;
+    }
+}
+
+gboolean
+ide_editor_spell_dict_personal_contains (IdeEditorSpellDict *self,
+                                         const gchar        *word)
+{
+  g_assert (IDE_IS_EDITOR_SPELL_DICT (self));
+  g_assert (!ide_str_empty0 (word));
+
+  if (self->dict != NULL)
+    {
+      return !!enchant_dict_is_added (self->dict, word, -1);
+    }
+  else
+    {
+      g_warning ("No dictionaries loaded");
+      return FALSE;
+    }
+}
+
 void
 ide_editor_spell_dict_get_words_async (IdeEditorSpellDict  *self,
                                        GAsyncReadyCallback  callback,
@@ -187,6 +254,26 @@ ide_editor_spell_dict_get_words_finish (IdeEditorSpellDict   *self,
 }
 
 static void
+ide_editor_spell_dict_set_dict (IdeEditorSpellDict    *self,
+                                const GspellLanguage  *language)
+{
+  const gchar *lang_code;
+
+  g_assert (IDE_IS_EDITOR_SPELL_DICT (self));
+
+  if (language != NULL)
+    {
+      lang_code = gspell_language_get_code (language);
+      self->dict = enchant_broker_request_dict (self->broker, lang_code);
+    }
+  else if (self->dict != NULL)
+    {
+      enchant_broker_free_dict (self->broker, self->dict);
+      self->dict = NULL;
+    }
+}
+
+static void
 language_notify_cb (IdeEditorSpellDict  *self,
                     GParamSpec          *pspec,
                     GspellChecker       *checker)
@@ -201,7 +288,10 @@ language_notify_cb (IdeEditorSpellDict  *self,
   if ((self->language == NULL && language != NULL) ||
       (self->language != NULL && language == NULL) ||
       0 != gspell_language_compare (language, self->language))
-    self->language = language;
+    {
+      self->language = language;
+      ide_editor_spell_dict_set_dict (self, language);
+    }
 }
 
 static void
@@ -265,6 +355,15 @@ ide_editor_spell_dict_new (GspellChecker *checker)
 static void
 ide_editor_spell_dict_finalize (GObject *object)
 {
+  IdeEditorSpellDict *self = (IdeEditorSpellDict *)object;
+
+  if (self->broker != NULL)
+    {
+      if (self->dict != NULL)
+        enchant_broker_free_dict (self->broker, self->dict);
+
+      enchant_broker_free (self->broker);
+    }
 }
 
 static void
@@ -327,4 +426,5 @@ ide_editor_spell_dict_class_init (IdeEditorSpellDictClass *klass)
 static void
 ide_editor_spell_dict_init (IdeEditorSpellDict *self)
 {
+  self->broker = enchant_broker_init ();
 }
diff --git a/libide/editor/ide-editor-spell-dict.h b/libide/editor/ide-editor-spell-dict.h
index 85a7d00..8e9955e 100644
--- a/libide/editor/ide-editor-spell-dict.h
+++ b/libide/editor/ide-editor-spell-dict.h
@@ -28,20 +28,24 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (IdeEditorSpellDict, ide_editor_spell_dict, IDE, EDITOR_SPELL_DICT, GObject)
 
-IdeEditorSpellDict         *ide_editor_spell_dict_new               (GspellChecker         *checker);
-
-GspellChecker              *ide_editor_spell_dict_get_checker       (IdeEditorSpellDict    *self);
-void                        ide_editor_spell_dict_get_words_async   (IdeEditorSpellDict    *self,
-                                                                     GAsyncReadyCallback    callback,
-                                                                     GCancellable          *cancellable,
-                                                                     gpointer               user_data);
-GPtrArray                  *ide_editor_spell_dict_get_words_finish  (IdeEditorSpellDict    *self,
-                                                                     GAsyncResult          *result,
-                                                                     GError               **error);
-void                        ide_editor_spell_dict_set_checker       (IdeEditorSpellDict    *self,
-                                                                     GspellChecker         *checker);
-void                        ide_editor_spell_dict_add_word          (IdeEditorSpellDict    *self,
-                                                                     const gchar           *word);
+IdeEditorSpellDict         *ide_editor_spell_dict_new                        (GspellChecker         
*checker);
+
+GspellChecker              *ide_editor_spell_dict_get_checker                (IdeEditorSpellDict    *self);
+void                        ide_editor_spell_dict_get_words_async            (IdeEditorSpellDict    *self,
+                                                                              GAsyncReadyCallback    
callback,
+                                                                              GCancellable          
*cancellable,
+                                                                              gpointer               
user_data);
+GPtrArray                  *ide_editor_spell_dict_get_words_finish           (IdeEditorSpellDict    *self,
+                                                                              GAsyncResult          *result,
+                                                                              GError               **error);
+void                        ide_editor_spell_dict_set_checker                (IdeEditorSpellDict    *self,
+                                                                              GspellChecker         
*checker);
+gboolean                    ide_editor_spell_dict_add_word_to_personal       (IdeEditorSpellDict    *self,
+                                                                              const gchar           *word);
+gboolean                    ide_editor_spell_dict_remove_word_from_personal  (IdeEditorSpellDict    *self,
+                                                                              const gchar           *word);
+gboolean                    ide_editor_spell_dict_personal_contains          (IdeEditorSpellDict    *self,
+                                                                              const gchar           *word);
 
 G_END_DECLS
 
diff --git a/libide/editor/ide-editor-spell-widget.c b/libide/editor/ide-editor-spell-widget.c
index f49c8ba..6eded56 100644
--- a/libide/editor/ide-editor-spell-widget.c
+++ b/libide/editor/ide-editor-spell-widget.c
@@ -40,7 +40,7 @@ struct _IdeEditorSpellWidget
   IdeSourceView         *view;
   IdeBuffer             *buffer;
   GspellChecker         *checker;
-  IdeEditorSpellDict    *dict_widget;
+  IdeEditorSpellDict    *dict;
   GPtrArray             *words_array;
   const GspellLanguage  *spellchecker_language;
 
@@ -270,7 +270,6 @@ static gboolean
 check_word_timeout_cb (IdeEditorSpellWidget *self)
 {
   const gchar *word;
-  g_autofree gchar *first_result = NULL;
   g_autoptr(GError) error = NULL;
   gchar *icon_name;
   gboolean ret = TRUE;
@@ -725,7 +724,7 @@ ide_editor_spell_widget_get_dict_words_cb (GObject      *object,
   g_assert (IDE_IS_EDITOR_SPELL_WIDGET (self));
   g_assert (G_IS_ASYNC_RESULT (result));
 
-  if (NULL == (self->words_array = ide_editor_spell_dict_get_words_finish (self->dict_widget,
+  if (NULL == (self->words_array = ide_editor_spell_dict_get_words_finish (self->dict,
                                                                            result,
                                                                            &error)))
     {
@@ -742,7 +741,7 @@ ide_editor_spell_widget_get_dict_words_async (IdeEditorSpellWidget *self)
 {
   g_assert (IDE_IS_EDITOR_SPELL_WIDGET (self));
 
-  ide_editor_spell_dict_get_words_async (self->dict_widget,
+  ide_editor_spell_dict_get_words_async (self->dict,
                                          ide_editor_spell_widget_get_dict_words_cb,
                                          NULL,
                                          self);
@@ -802,7 +801,7 @@ ide_editor_spell_widget_constructed (GObject *object)
 
   spell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (GTK_TEXT_BUFFER (self->buffer));
   self->checker = gspell_text_buffer_get_spell_checker (spell_buffer);
-  ide_editor_spell_dict_set_checker (self->dict_widget, self->checker);
+  ide_editor_spell_dict_set_checker (self->dict, self->checker);
 
   self->spellchecker_language = gspell_checker_get_language (self->checker);
   gspell_language_chooser_set_language (GSPELL_LANGUAGE_CHOOSER (self->language_chooser_button),
@@ -1005,7 +1004,7 @@ static void
 ide_editor_spell_widget_init (IdeEditorSpellWidget *self)
 {
   gtk_widget_init_template (GTK_WIDGET (self));
-  self->dict_widget = ide_editor_spell_dict_new (NULL);
+  self->dict = ide_editor_spell_dict_new (NULL);
 
   self->view_spellchecker_set = FALSE;
   /* FIXME: do not work, Gtk+ bug */
diff --git a/libide/editor/ide-editor-spell-widget.ui b/libide/editor/ide-editor-spell-widget.ui
index 70b7757..e8676a1 100644
--- a/libide/editor/ide-editor-spell-widget.ui
+++ b/libide/editor/ide-editor-spell-widget.ui
@@ -210,7 +210,7 @@
             </child>
             <child>
               <object class="GtkButton" id="dict_add_button">
-                <property name="label" translatable="yes">Add</property>
+                <property name="label" translatable="yes">A_dd</property>
                 <property name="visible">true</property>
                 <property name="can_focus">true</property>
                 <property name="use_underline">True</property>


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