[epiphany/wip/ephy-sync] ephy-sync: Fix ephy-notification-manager



commit 874764a3219481737541f5f68e1522ab26f2ffb3
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Tue Aug 30 15:11:09 2016 +0300

    ephy-sync: Fix ephy-notification-manager

 embed/ephy-embed.c                       |   13 ++++---
 embed/ephy-notification-manager.c        |   51 +++++------------------------
 embed/ephy-notification-manager.h        |    4 +--
 lib/widgets/ephy-password-notification.c |    2 +-
 4 files changed, 18 insertions(+), 52 deletions(-)
---
diff --git a/embed/ephy-embed.c b/embed/ephy-embed.c
index e003e4f..ed2dbbc 100644
--- a/embed/ephy-embed.c
+++ b/embed/ephy-embed.c
@@ -966,11 +966,8 @@ ephy_embed_attach_notification_manager (EphyEmbed *embed)
 
   g_return_if_fail (EPHY_IS_EMBED (embed));
 
-  manager = ephy_notification_manager_dup_singleton ();
+  manager = ephy_notification_manager_get_default ();
   gtk_overlay_add_overlay (GTK_OVERLAY (embed->overlay), GTK_WIDGET (manager));
-
-  if (ephy_notification_manager_get_children_num (manager) == 0)
-    gtk_widget_hide (GTK_WIDGET (manager));
 }
 
 void
@@ -980,6 +977,10 @@ ephy_embed_detach_notification_manager (EphyEmbed *embed)
 
   g_return_if_fail (EPHY_IS_EMBED (embed));
 
-  manager = ephy_notification_manager_dup_singleton ();
-  gtk_container_remove (GTK_CONTAINER (embed->overlay), GTK_WIDGET (manager));
+  manager = ephy_notification_manager_get_default ();
+  /* Since the overlay container will own the one and only reference to the
+   * notification widget, removing it from the container will destroy the
+   * singleton. To prevent this, add a reference to it before removing it
+   * from the container. */
+  gtk_container_remove (GTK_CONTAINER (embed->overlay), g_object_ref (manager));
 }
diff --git a/embed/ephy-notification-manager.c b/embed/ephy-notification-manager.c
index 046ff38..f0b7151 100644
--- a/embed/ephy-notification-manager.c
+++ b/embed/ephy-notification-manager.c
@@ -31,42 +31,18 @@ struct _EphyNotificationManagerClass {
 
 G_DEFINE_TYPE (EphyNotificationManager, ephy_notification_manager, GD_TYPE_NOTIFICATION);
 
-static GObject *
-ephy_notification_manager_constructor (GType                  type,
-                                       guint                  n_construct_params,
-                                       GObjectConstructParam *construct_params)
-{
-  static GObject *self = NULL;
-
-  if (self == NULL) {
-    self = G_OBJECT_CLASS (ephy_notification_manager_parent_class)->constructor (type,
-                                                                                 n_construct_params,
-                                                                                 construct_params);
-    g_object_add_weak_pointer (self, (gpointer) &self);
-  }
-
-  return g_object_ref_sink (self);
-}
-
-static void
-ephy_notification_manager_dispose (GObject *object)
-{
-  EphyNotificationManager *self = EPHY_NOTIFICATION_MANAGER (object);
-
-  if (ephy_notification_manager_get_children_num (self) > 0)
-    g_list_free (gtk_container_get_children (GTK_CONTAINER (self->grid)));
-
-  G_OBJECT_CLASS (ephy_notification_manager_parent_class)->dispose (object);
-}
+static EphyNotificationManager *notification_manager = NULL;
 
 static void
 ephy_notification_manager_init (EphyNotificationManager *self)
 {
-  g_return_if_fail (EPHY_IS_NOTIFICATION_MANAGER (self));
+  /* Globally accessible singleton */
+  g_assert (notification_manager == NULL);
+  notification_manager = self;
+  g_object_add_weak_pointer (G_OBJECT (notification_manager), (gpointer *)&notification_manager);
 
   gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
   gtk_widget_set_valign (GTK_WIDGET (self), GTK_ALIGN_START);
-  gtk_widget_set_visible (GTK_WIDGET (self), TRUE);
 
   self->grid = gtk_grid_new ();
   gtk_orientable_set_orientation (GTK_ORIENTABLE (self->grid), GTK_ORIENTATION_VERTICAL);
@@ -77,15 +53,14 @@ ephy_notification_manager_init (EphyNotificationManager *self)
 static void
 ephy_notification_manager_class_init (EphyNotificationManagerClass *klass)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-  object_class->constructor = ephy_notification_manager_constructor;
-  object_class->dispose = ephy_notification_manager_dispose;
 }
 
 EphyNotificationManager *
-ephy_notification_manager_dup_singleton (void)
+ephy_notification_manager_get_default (void)
 {
+  if (notification_manager != NULL)
+    return notification_manager;
+
   return g_object_new (EPHY_TYPE_NOTIFICATION_MANAGER,
                        "show-close-button", TRUE,
                        "timeout", -1,
@@ -102,11 +77,3 @@ ephy_notification_manager_add_notification (EphyNotificationManager *self,
   gtk_container_add (GTK_CONTAINER (self->grid), notification);
   gtk_widget_show_all (GTK_WIDGET (self));
 }
-
-guint
-ephy_notification_manager_get_children_num (EphyNotificationManager *self)
-{
-  g_return_val_if_fail (EPHY_IS_NOTIFICATION_MANAGER (self), 0);
-
-  return g_list_length (gtk_container_get_children (GTK_CONTAINER (self->grid)));
-}
diff --git a/embed/ephy-notification-manager.h b/embed/ephy-notification-manager.h
index 3b3424f..ed5ff51 100644
--- a/embed/ephy-notification-manager.h
+++ b/embed/ephy-notification-manager.h
@@ -38,13 +38,11 @@ typedef struct _EphyNotificationManagerClass EphyNotificationManagerClass;
 
 GType                    ephy_notification_manager_get_type         (void) G_GNUC_CONST;
 
-EphyNotificationManager *ephy_notification_manager_dup_singleton    (void);
+EphyNotificationManager *ephy_notification_manager_get_default      (void);
 
 void                     ephy_notification_manager_add_notification (EphyNotificationManager *self,
                                                                      GtkWidget               *notification);
 
-guint                    ephy_notification_manager_get_children_num (EphyNotificationManager *self);
-
 G_END_DECLS
 
 #endif
diff --git a/lib/widgets/ephy-password-notification.c b/lib/widgets/ephy-password-notification.c
index 091c94f..eee3be1 100644
--- a/lib/widgets/ephy-password-notification.c
+++ b/lib/widgets/ephy-password-notification.c
@@ -160,6 +160,6 @@ ephy_password_notification_show (EphyPasswordNotification *self)
 {
   g_return_if_fail (EPHY_IS_PASSWORD_NOTIFICATION (self));
 
-  ephy_notification_manager_add_notification (ephy_notification_manager_dup_singleton (),
+  ephy_notification_manager_add_notification (ephy_notification_manager_get_default (),
                                               GTK_WIDGET (self));
 }


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