From e8499acc40dc33d78df27d1b8ac14c296098fc9e Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Fri, 9 Jan 2009 10:06:10 +0000 Subject: Port EmpathySmileyManager to the new singleton policy. svn path=/trunk/; revision=2116 --- libempathy-gtk/empathy-chat-text-view.c | 2 +- libempathy-gtk/empathy-chat.c | 2 +- libempathy-gtk/empathy-smiley-manager.c | 39 +++++++++++++++++++++++---------- libempathy-gtk/empathy-smiley-manager.h | 2 +- libempathy-gtk/empathy-theme.c | 2 +- 5 files changed, 31 insertions(+), 16 deletions(-) (limited to 'libempathy-gtk') diff --git a/libempathy-gtk/empathy-chat-text-view.c b/libempathy-gtk/empathy-chat-text-view.c index 9362303a2..a3d14a07d 100644 --- a/libempathy-gtk/empathy-chat-text-view.c +++ b/libempathy-gtk/empathy-chat-text-view.c @@ -637,7 +637,7 @@ empathy_chat_text_view_init (EmpathyChatTextView *view) priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); priv->last_timestamp = 0; priv->allow_scrolling = TRUE; - priv->smiley_manager = empathy_smiley_manager_new (); + priv->smiley_manager = empathy_smiley_manager_dup_singleton (); g_object_set (view, "wrap-mode", GTK_WRAP_WORD_CHAR, diff --git a/libempathy-gtk/empathy-chat.c b/libempathy-gtk/empathy-chat.c index 0bfad9536..ce5f69880 100644 --- a/libempathy-gtk/empathy-chat.c +++ b/libempathy-gtk/empathy-chat.c @@ -968,7 +968,7 @@ chat_input_populate_popup_cb (GtkTextView *view, gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item); gtk_widget_show (item); - smiley_manager = empathy_smiley_manager_new (); + smiley_manager = empathy_smiley_manager_dup_singleton (); smiley_menu = empathy_smiley_menu_new (smiley_manager, chat_insert_smiley_activate_cb, chat); diff --git a/libempathy-gtk/empathy-smiley-manager.c b/libempathy-gtk/empathy-smiley-manager.c index 4137db63d..3f6ea25c3 100644 --- a/libempathy-gtk/empathy-smiley-manager.c +++ b/libempathy-gtk/empathy-smiley-manager.c @@ -44,6 +44,8 @@ struct _SmileyManagerTree { G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT); +static EmpathySmileyManager *manager_singleton = NULL; + static SmileyManagerTree * smiley_manager_tree_new (gunichar c) { @@ -115,12 +117,33 @@ smiley_manager_finalize (GObject *object) g_slist_free (priv->smileys); } +static GObject * +smiley_manager_constructor (GType type, + guint n_props, + GObjectConstructParam *props) +{ + GObject *retval; + + if (manager_singleton) { + retval = g_object_ref (manager_singleton); + } else { + retval = G_OBJECT_CLASS (empathy_smiley_manager_parent_class)->constructor + (type, n_props, props); + g_object_add_weak_pointer (retval, (gpointer *) &retval); + + manager_singleton = EMPATHY_SMILEY_MANAGER (retval); + } + + return retval; +} + static void empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->finalize = smiley_manager_finalize; + object_class->constructor = smiley_manager_constructor; g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv)); } @@ -134,22 +157,14 @@ empathy_smiley_manager_init (EmpathySmileyManager *manager) manager->priv = priv; priv->tree = smiley_manager_tree_new ('\0'); priv->smileys = NULL; + + empathy_smiley_manager_load (manager); } EmpathySmileyManager * -empathy_smiley_manager_new (void) +empathy_smiley_manager_dup_singleton (void) { - static EmpathySmileyManager *manager = NULL; - - if (!manager) { - manager = g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL); - g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager); - empathy_smiley_manager_load (manager); - } else { - g_object_ref (manager); - } - - return manager; + return g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL); } static SmileyManagerTree * diff --git a/libempathy-gtk/empathy-smiley-manager.h b/libempathy-gtk/empathy-smiley-manager.h index c73675742..dae022dfa 100644 --- a/libempathy-gtk/empathy-smiley-manager.h +++ b/libempathy-gtk/empathy-smiley-manager.h @@ -57,7 +57,7 @@ typedef void (*EmpathySmileyMenuFunc) (EmpathySmileyManager *manager, gpointer user_data); GType empathy_smiley_manager_get_type (void) G_GNUC_CONST; -EmpathySmileyManager *empathy_smiley_manager_new (void); +EmpathySmileyManager *empathy_smiley_manager_dup_singleton (void); void empathy_smiley_manager_load (EmpathySmileyManager *manager); void empathy_smiley_manager_add (EmpathySmileyManager *manager, const gchar *icon_name, diff --git a/libempathy-gtk/empathy-theme.c b/libempathy-gtk/empathy-theme.c index 971c6b1c9..ca4f66663 100644 --- a/libempathy-gtk/empathy-theme.c +++ b/libempathy-gtk/empathy-theme.c @@ -102,7 +102,7 @@ empathy_theme_init (EmpathyTheme *theme) EMPATHY_TYPE_THEME, EmpathyThemePriv); theme->priv = priv; - priv->smiley_manager = empathy_smiley_manager_new (); + priv->smiley_manager = empathy_smiley_manager_dup_singleton (); } static void -- cgit v1.2.3