[gtksourceview] CompletionWords: little clean-up and code simplification



commit 03da9f3d41eca20da0df316c2da8a5f36fcfd8b9
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat May 25 15:43:20 2013 +0200

    CompletionWords: little clean-up and code simplification
    
    Some new GLib functions simplifies the code.

 .../words/gtksourcecompletionwords.c               |   73 +++++++-------
 .../words/gtksourcecompletionwordsbuffer.c         |  110 ++++++--------------
 2 files changed, 71 insertions(+), 112 deletions(-)
---
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwords.c 
b/gtksourceview/completion-providers/words/gtksourcecompletionwords.c
index 2202637..90f2a15 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwords.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwords.c
@@ -84,19 +84,12 @@ struct _GtkSourceCompletionWordsPrivate
 
 typedef struct
 {
-       GObjectClass parent_class;
-} GscProposalWordsClass;
-
-typedef struct
-{
        GtkSourceCompletionWords *words;
        GtkSourceCompletionWordsBuffer *buffer;
 } BufferBinding;
 
 static void gtk_source_completion_words_iface_init (GtkSourceCompletionProviderIface *iface);
 
-GType gsc_proposal_words_get_type (void);
-
 G_DEFINE_TYPE_WITH_CODE (GtkSourceCompletionWords,
                         gtk_source_completion_words,
                         G_TYPE_OBJECT,
@@ -136,8 +129,7 @@ population_finished (GtkSourceCompletionWords *words)
                        words->priv->cancel_id = 0;
                }
 
-               g_object_unref (words->priv->context);
-               words->priv->context = NULL;
+               g_clear_object (&words->priv->context);
        }
 }
 
@@ -283,8 +275,8 @@ gtk_source_completion_words_populate (GtkSourceCompletionProvider *provider,
        words->priv->cancel_id =
                g_signal_connect_swapped (context,
                                          "cancelled",
-                                          G_CALLBACK (population_finished),
-                                          provider);
+                                         G_CALLBACK (population_finished),
+                                         provider);
 
        words->priv->context = g_object_ref (context);
 
@@ -341,9 +333,9 @@ update_buffers_batch_size (GtkSourceCompletionWords *words)
 {
        GList *item;
 
-       for (item = words->priv->buffers; item; item = g_list_next (item))
+       for (item = words->priv->buffers; item != NULL; item = g_list_next (item))
        {
-               BufferBinding *binding = (BufferBinding *)item->data;
+               BufferBinding *binding = item->data;
                gtk_source_completion_words_buffer_set_scan_batch_size (binding->buffer,
                                                                        words->priv->scan_batch_size);
        }
@@ -354,7 +346,7 @@ update_buffers_minimum_word_size (GtkSourceCompletionWords *words)
 {
        GList *item;
 
-       for (item = words->priv->buffers; item; item = g_list_next (item))
+       for (item = words->priv->buffers; item != NULL; item = g_list_next (item))
        {
                BufferBinding *binding = (BufferBinding *)item->data;
                gtk_source_completion_words_buffer_set_minimum_word_size (binding->buffer,
@@ -380,35 +372,38 @@ gtk_source_completion_words_set_property (GObject      *object,
                        {
                                self->priv->name = g_strdup (_("Document Words"));
                        }
-               break;
-               case PROP_ICON:
-                       if (self->priv->icon)
-                       {
-                               g_object_unref (self->priv->icon);
-                       }
+                       break;
 
+               case PROP_ICON:
+                       g_clear_object (&self->priv->icon);
                        self->priv->icon = g_value_dup_object (value);
-               break;
+                       break;
+
                case PROP_PROPOSALS_BATCH_SIZE:
                        self->priv->proposals_batch_size = g_value_get_uint (value);
-               break;
+                       break;
+
                case PROP_SCAN_BATCH_SIZE:
                        self->priv->scan_batch_size = g_value_get_uint (value);
                        update_buffers_batch_size (self);
-               break;
+                       break;
+
                case PROP_MINIMUM_WORD_SIZE:
                        self->priv->minimum_word_size = g_value_get_uint (value);
                        update_buffers_minimum_word_size (self);
-               break;
+                       break;
+
                case PROP_INTERACTIVE_DELAY:
                        self->priv->interactive_delay = g_value_get_int (value);
-               break;
+                       break;
+
                case PROP_PRIORITY:
                        self->priv->priority = g_value_get_int (value);
-               break;
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-               break;
+                       break;
        }
 }
 
@@ -424,28 +419,35 @@ gtk_source_completion_words_get_property (GObject    *object,
        {
                case PROP_NAME:
                        g_value_set_string (value, self->priv->name);
-               break;
+                       break;
+
                case PROP_ICON:
                        g_value_set_object (value, self->priv->icon);
-               break;
+                       break;
+
                case PROP_PROPOSALS_BATCH_SIZE:
                        g_value_set_uint (value, self->priv->proposals_batch_size);
-               break;
+                       break;
+
                case PROP_SCAN_BATCH_SIZE:
                        g_value_set_uint (value, self->priv->scan_batch_size);
-               break;
+                       break;
+
                case PROP_MINIMUM_WORD_SIZE:
                        g_value_set_uint (value, self->priv->minimum_word_size);
-               break;
+                       break;
+
                case PROP_INTERACTIVE_DELAY:
                        g_value_set_int (value, self->priv->interactive_delay);
-               break;
+                       break;
+
                case PROP_PRIORITY:
                        g_value_set_int (value, self->priv->priority);
-               break;
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-               break;
+                       break;
        }
 }
 
@@ -671,4 +673,3 @@ gtk_source_completion_words_unregister (GtkSourceCompletionWords *words,
 
        g_object_set_data (G_OBJECT (buffer), BUFFER_KEY, NULL);
 }
-
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c 
b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
index 4253970..04144b5 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
@@ -22,8 +22,6 @@
 #include "gtksourcecompletionwordsbuffer.h"
 #include "gtksourcecompletionwordsutils.h"
 
-#include <glib.h>
-
 #define GTK_SOURCE_COMPLETION_WORDS_BUFFER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), 
GTK_SOURCE_TYPE_COMPLETION_WORDS_BUFFER, GtkSourceCompletionWordsBufferPrivate))
 
 /* Timeout in seconds */
@@ -44,13 +42,6 @@ typedef struct
        GtkTextMark *end;
 } ScanRegion;
 
-enum
-{
-       EXT_INSERT_TEXT,
-       EXT_DELETE_RANGE,
-       NUM_EXT_SIGNALS
-};
-
 struct _GtkSourceCompletionWordsBufferPrivate
 {
        GtkSourceCompletionWordsLibrary *library;
@@ -60,13 +51,9 @@ struct _GtkSourceCompletionWordsBufferPrivate
        gulong batch_scan_id;
        gulong initiate_scan_id;
 
-       guint ext_signal_handlers[NUM_EXT_SIGNALS];
        guint scan_batch_size;
        guint minimum_word_size;
 
-       guint lock_handler_id;
-       guint unlock_handler_id;
-
        GtkTextMark *mark;
        GHashTable *words;
 };
@@ -169,7 +156,7 @@ gtk_source_completion_words_buffer_dispose (GObject *object)
                buffer->priv->mark = NULL;
        }
 
-       if (buffer->priv->words)
+       if (buffer->priv->words != NULL)
        {
                remove_words (buffer);
 
@@ -177,45 +164,23 @@ gtk_source_completion_words_buffer_dispose (GObject *object)
                buffer->priv->words = NULL;
        }
 
-       g_list_foreach (buffer->priv->scan_regions, (GFunc)scan_region_free, NULL);
-       g_list_free (buffer->priv->scan_regions);
+       g_list_free_full (buffer->priv->scan_regions, (GDestroyNotify)scan_region_free);
        buffer->priv->scan_regions = NULL;
 
-       if (buffer->priv->buffer)
-       {
-               gint i;
-
-               for (i = 0; i < NUM_EXT_SIGNALS; ++i)
-               {
-                       g_signal_handler_disconnect (buffer->priv->buffer,
-                                                    buffer->priv->ext_signal_handlers[i]);
-               }
-
-               g_object_unref (buffer->priv->buffer);
-               buffer->priv->buffer = NULL;
-       }
-
-       if (buffer->priv->batch_scan_id)
+       if (buffer->priv->batch_scan_id != 0)
        {
                g_source_remove (buffer->priv->batch_scan_id);
                buffer->priv->batch_scan_id = 0;
        }
 
-       if (buffer->priv->initiate_scan_id)
+       if (buffer->priv->initiate_scan_id != 0)
        {
                g_source_remove (buffer->priv->initiate_scan_id);
                buffer->priv->initiate_scan_id = 0;
        }
 
-       if (buffer->priv->library)
-       {
-               g_signal_handler_disconnect (buffer->priv->library,
-                                            buffer->priv->lock_handler_id);
-               g_signal_handler_disconnect (buffer->priv->library,
-                                            buffer->priv->unlock_handler_id);
-
-               g_clear_object (&buffer->priv->library);
-       }
+       g_clear_object (&buffer->priv->buffer);
+       g_clear_object (&buffer->priv->library);
 
        G_OBJECT_CLASS (gtk_source_completion_words_buffer_parent_class)->dispose (object);
 }
@@ -314,11 +279,9 @@ static void
 remove_word (GtkSourceCompletionWordsBuffer *buffer,
              const gchar                    *word)
 {
-       ProposalCache *cache;
+       ProposalCache *cache = g_hash_table_lookup (buffer->priv->words, word);
 
-       cache = g_hash_table_lookup (buffer->priv->words, word);
-
-       if (!cache)
+       if (cache == NULL)
        {
                g_warning ("Could not find word to remove in buffer (%s), this should not happen!",
                           word);
@@ -342,7 +305,7 @@ add_words (GtkSourceCompletionWordsBuffer *buffer,
 {
        GSList *item;
 
-       for (item = words; item; item = g_slist_next (item))
+       for (item = words; item != NULL; item = g_slist_next (item))
        {
                GtkSourceCompletionWordsProposal *proposal;
                ProposalCache *cache;
@@ -353,7 +316,7 @@ add_words (GtkSourceCompletionWordsBuffer *buffer,
                cache = g_hash_table_lookup (buffer->priv->words,
                                             item->data);
 
-               if (cache)
+               if (cache != NULL)
                {
                        ++cache->use_count;
                        g_free (item->data);
@@ -656,8 +619,7 @@ on_delete_range_cb (GtkTextBuffer                  *text_buffer,
            gtk_text_iter_equal (end, &end_buf))
        {
                remove_words (buffer);
-               g_list_foreach (buffer->priv->scan_regions, (GFunc)scan_region_free, NULL);
-               g_list_free (buffer->priv->scan_regions);
+               g_list_free_full (buffer->priv->scan_regions, (GDestroyNotify)scan_region_free);
                buffer->priv->scan_regions = NULL;
 
                add_scan_region (buffer, NULL, start, end, FALSE);
@@ -675,27 +637,23 @@ connect_buffer (GtkSourceCompletionWordsBuffer *buffer)
        GtkTextIter start;
        GtkTextIter end;
 
-       buffer->priv->ext_signal_handlers[EXT_INSERT_TEXT] =
-               g_signal_connect (buffer->priv->buffer,
-                                 "insert-text",
-                                 G_CALLBACK (on_insert_text_cb),
-                                 buffer);
+       g_signal_connect_object (buffer->priv->buffer,
+                                "insert-text",
+                                G_CALLBACK (on_insert_text_cb),
+                                buffer,
+                                0);
 
-       buffer->priv->ext_signal_handlers[EXT_DELETE_RANGE] =
-               g_signal_connect (buffer->priv->buffer,
-                                 "delete-range",
-                                 G_CALLBACK (on_delete_range_cb),
-                                 buffer);
+       g_signal_connect_object (buffer->priv->buffer,
+                                "delete-range",
+                                G_CALLBACK (on_delete_range_cb),
+                                buffer,
+                                0);
 
        gtk_text_buffer_get_bounds (buffer->priv->buffer,
                                    &start,
                                    &end);
 
-       add_scan_region (buffer,
-                        NULL,
-                        &start,
-                        &end,
-                        FALSE);
+       add_scan_region (buffer, NULL, &start, &end, FALSE);
 }
 
 static void
@@ -706,7 +664,8 @@ on_library_lock (GtkSourceCompletionWordsBuffer *buffer)
                g_source_remove (buffer->priv->batch_scan_id);
                buffer->priv->batch_scan_id = 0;
        }
-       else if (buffer->priv->initiate_scan_id != 0)
+
+       if (buffer->priv->initiate_scan_id != 0)
        {
                g_source_remove (buffer->priv->initiate_scan_id);
                buffer->priv->initiate_scan_id = 0;
@@ -737,20 +696,19 @@ gtk_source_completion_words_buffer_new (GtkSourceCompletionWordsLibrary *library
        ret->priv->library = g_object_ref (library);
        ret->priv->buffer = g_object_ref (buffer);
 
-       ret->priv->lock_handler_id =
-               g_signal_connect_swapped (ret->priv->library,
-                                         "lock",
-                                         G_CALLBACK (on_library_lock),
-                                         ret);
-
-       ret->priv->unlock_handler_id =
-               g_signal_connect_swapped (ret->priv->library,
-                                         "unlock",
-                                         G_CALLBACK (on_library_unlock),
-                                         ret);
+       g_signal_connect_object (ret->priv->library,
+                                "lock",
+                                G_CALLBACK (on_library_lock),
+                                ret,
+                                G_CONNECT_SWAPPED);
 
        gtk_text_buffer_get_start_iter (buffer, &iter);
        ret->priv->mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, TRUE);
+       g_signal_connect_object (ret->priv->library,
+                                "unlock",
+                                G_CALLBACK (on_library_unlock),
+                                ret,
+                                G_CONNECT_SWAPPED);
 
        connect_buffer (ret);
 


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