]> git.0d.be Git - empathy.git/commitdiff
Make EmpathyProfileChooser a subclass of GtkComboBox.
authorJonny Lamb <jonny.lamb@collabora.co.uk>
Fri, 5 Jun 2009 12:57:49 +0000 (13:57 +0100)
committerJonny Lamb <jonny.lamb@collabora.co.uk>
Fri, 5 Jun 2009 12:57:49 +0000 (13:57 +0100)
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
libempathy-gtk/empathy-profile-chooser.c
libempathy-gtk/empathy-profile-chooser.h

index 53cfa3b3ef79ce53c447825bc7b0e978adf38f97..c5a9f10cd94e1c3cb58c5934f1488472936c3ef6 100644 (file)
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
 /*
- * Copyright (C) 2007-2008 Collabora Ltd.
+ * Copyright (C) 2007-2009 Collabora Ltd.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -17,6 +17,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Authors: Xavier Claessens <xclaesse@gmail.com>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk>
  */
 
 #include <config.h>
@@ -27,6 +28,8 @@
 #include <libmissioncontrol/mc-profile.h>
 #include <libmissioncontrol/mc-protocol.h>
 
+#include <libempathy/empathy-utils.h>
+
 #include "empathy-profile-chooser.h"
 #include "empathy-ui-utils.h"
 
  * @short_description: A widget used to choose from a list of profiles
  * @include: libempathy-gtk/empathy-account-chooser.h
  *
- * #EmpathyProfileChooser is a widget which provides a chooser of available
+ * #EmpathyProfileChooser is a widget which extends #GtkComboBox to provides a
+ * chooser of available profiles.
+ */
+
+/**
+ * EmpathyProfileChooser:
+ * @parent: parent object
+ *
+ * Widget which extends #GtkComboBox to provide a chooser of available
  * profiles.
  */
 
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProfileChooser)
+typedef struct
+{
+  GtkListStore *store;
+  gboolean dispose_run;
+} EmpathyProfileChooserPriv;
+
 enum {
   COL_ICON,
   COL_LABEL,
@@ -47,50 +65,8 @@ enum {
   COL_COUNT
 };
 
-/**
- * empathy_profile_chooser_dup_selected:
- * @widget: an #EmpathyProfileChooser
- *
- * Returns a new reference to the selected #McProfile in @widget. The returned
- * #McProfile should be unrefed with g_object_unref() when finished with.
- *
- * Return value: a new reference to the selected #McProfile
- */
-McProfile *
-empathy_profile_chooser_dup_selected (GtkWidget *widget)
-{
-  GtkTreeModel *model;
-  GtkTreeIter iter;
-  McProfile *profile = NULL;
-
-  model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
-  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter))
-    {
-      gtk_tree_model_get (model, &iter,
-          COL_PROFILE, &profile,
-          -1);
-    }
-
-  return profile;
-}
-
-/**
- * empathy_profile_chooser_n_profiles:
- * @widget: an #EmpathyProfileChooser
- *
- * Returns the number of profiles in @widget.
- *
- * Return value: the number of profiles in @widget
- */
-gint
-empathy_profile_chooser_n_profiles (GtkWidget *widget)
-{
-  GtkTreeModel *model;
-
-  model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
-
-  return gtk_tree_model_iter_n_children (model, NULL);
-}
+G_DEFINE_TYPE (EmpathyProfileChooser, empathy_profile_chooser,
+    GTK_TYPE_COMBO_BOX);
 
 static gint
 profile_chooser_sort_profile_value (McProfile *profile)
@@ -146,49 +122,52 @@ profile_chooser_sort_func (GtkTreeModel *model,
   return cmp;
 }
 
-/**
- * empathy_profile_chooser_new:
- *
- * Creates a new #EmpathyProfileChooser widget.
- *
- * Return value: a new #EmpathyProfileChooser widget
- */
-GtkWidget *
-empathy_profile_chooser_new (void)
+static GObject *
+profile_chooser_constructor (GType type,
+    guint n_construct_params,
+    GObjectConstructParam *construct_params)
 {
+  GObject *object;
+  EmpathyProfileChooser *profile_chooser;
+  EmpathyProfileChooserPriv *priv;
+
   GList *profiles, *l, *seen;
-  GtkListStore *store;
   GtkCellRenderer *renderer;
-  GtkWidget *combo_box;
   GtkTreeIter iter;
   gboolean iter_set = FALSE;
   McManager *btf_cm;
 
+  object = G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->constructor (
+      type, n_construct_params, construct_params);
+  priv = GET_PRIV (object);
+  profile_chooser = EMPATHY_PROFILE_CHOOSER (object);
+
   /* set up combo box with new store */
-  store = gtk_list_store_new (COL_COUNT,
-      G_TYPE_STRING,    /* Icon name */
-      G_TYPE_STRING,    /* Label     */
-      MC_TYPE_PROFILE); /* Profile   */
-  combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
+  priv->store = gtk_list_store_new (COL_COUNT,
+          G_TYPE_STRING,    /* Icon name */
+          G_TYPE_STRING,    /* Label     */
+          MC_TYPE_PROFILE); /* Profile   */
 
+  gtk_combo_box_set_model (GTK_COMBO_BOX (object),
+      GTK_TREE_MODEL (priv->store));
 
   renderer = gtk_cell_renderer_pixbuf_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
+  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
       "icon-name", COL_ICON,
       NULL);
   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
 
   renderer = gtk_cell_renderer_text_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
+  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
       "text", COL_LABEL,
       NULL);
 
   btf_cm = mc_manager_lookup ("butterfly");
   profiles = mc_profiles_list ();
   seen = NULL;
-  for (l = profiles; l; l = l->next)
+  for (l = profiles; l; l = g_list_next (l))
     {
       McProfile   *profile;
       McProtocol  *protocol;
@@ -214,7 +193,7 @@ empathy_profile_chooser_new (void)
 
       seen = g_list_append (seen, (char *) unique_name);
 
-      gtk_list_store_insert_with_values (store, &iter, 0,
+      gtk_list_store_insert_with_values (priv->store, &iter, 0,
           COL_ICON, mc_profile_get_icon_name (profile),
           COL_LABEL, mc_profile_get_display_name (profile),
           COL_PROFILE, profile,
@@ -228,20 +207,121 @@ empathy_profile_chooser_new (void)
     g_object_unref (btf_cm);
 
   /* Set the profile sort function */
-  gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
+  gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
       COL_PROFILE,
       profile_chooser_sort_func,
       NULL, NULL);
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
+  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
       COL_PROFILE,
       GTK_SORT_ASCENDING);
 
   if (iter_set)
-    gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
+    gtk_combo_box_set_active_iter (GTK_COMBO_BOX (object), &iter);
 
   mc_profiles_free_list (profiles);
-  g_object_unref (store);
 
-  return combo_box;
+  return object;
+}
+
+static void
+empathy_profile_chooser_init (EmpathyProfileChooser *profile_chooser)
+{
+  EmpathyProfileChooserPriv *priv =
+    G_TYPE_INSTANCE_GET_PRIVATE (profile_chooser,
+        EMPATHY_TYPE_PROFILE_CHOOSER, EmpathyProfileChooserPriv);
+
+  priv->dispose_run = FALSE;
+
+  profile_chooser->priv = priv;
+}
+
+static void
+profile_chooser_dispose (GObject *object)
+{
+  EmpathyProfileChooser *profile_chooser = EMPATHY_PROFILE_CHOOSER (object);
+  EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
+
+  if (priv->dispose_run)
+    return;
+
+  priv->dispose_run = TRUE;
+
+  if (priv->store)
+    {
+      g_object_unref (priv->store);
+      priv->store = NULL;
+    }
+
+  (G_OBJECT_CLASS (empathy_profile_chooser_parent_class)->dispose) (object);
 }
 
+static void
+empathy_profile_chooser_class_init (EmpathyProfileChooserClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructor = profile_chooser_constructor;
+  object_class->dispose = profile_chooser_dispose;
+
+  g_type_class_add_private (object_class, sizeof (EmpathyProfileChooserPriv));
+}
+
+/**
+ * empathy_profile_chooser_dup_selected:
+ * @profile_chooser: an #EmpathyProfileChooser
+ *
+ * Returns a new reference to the selected #McProfile in @profile_chooser. The
+ * returned #McProfile should be unrefed with g_object_unref() when finished
+ * with.
+ *
+ * Return value: a new reference to the selected #McProfile
+ */
+McProfile *
+empathy_profile_chooser_dup_selected (EmpathyProfileChooser *profile_chooser)
+{
+  EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
+  GtkTreeIter iter;
+  McProfile *profile = NULL;
+
+  g_return_val_if_fail (EMPATHY_IS_PROFILE_CHOOSER (profile_chooser), NULL);
+
+  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (profile_chooser), &iter))
+    {
+      gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
+          COL_PROFILE, &profile,
+          -1);
+    }
+
+  return profile;
+}
+
+/**
+ * empathy_profile_chooser_n_profiles:
+ * @profile_chooser: an #EmpathyProfileChooser
+ *
+ * Returns the number of profiles in @profile_chooser.
+ *
+ * Return value: the number of profiles in @profile_chooser
+ */
+gint
+empathy_profile_chooser_n_profiles (EmpathyProfileChooser *profile_chooser)
+{
+  EmpathyProfileChooserPriv *priv = GET_PRIV (profile_chooser);
+
+  g_return_val_if_fail (EMPATHY_IS_PROFILE_CHOOSER (profile_chooser), 0);
+
+  return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
+}
+
+/**
+ * empathy_profile_chooser_new:
+ *
+ * Creates a new #EmpathyProfileChooser widget.
+ *
+ * Return value: a new #EmpathyProfileChooser widget
+ */
+GtkWidget *
+empathy_profile_chooser_new (void)
+{
+  return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROFILE_CHOOSER, NULL));
+}
index caea8b6f29a11c4123d2b83290720f8094593e3c..c78caf42c345f49c50c420f2903fe70e987e2c05 100644 (file)
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
 /*
- * Copyright (C) 2007-2008 Collabora Ltd.
+ * Copyright (C) 2007-2009 Collabora Ltd.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Authors: Xavier Claessens <xclaesse@gmail.com>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk
  */
 
-#ifndef __EMPATHY_PROTOCOL_CHOOSER_H__
-#define __EMPATHY_PROTOCOL_CHOOSER_H__
+#ifndef __EMPATHY_PROFILE_CHOOSER_H__
+#define __EMPATHY_PROFILE_CHOOSER_H__
+
+#include <glib-object.h>
+#include <gtk/gtkcombobox.h>
 
 #include <libmissioncontrol/mc-profile.h>
 
 G_BEGIN_DECLS
 
+#define EMPATHY_TYPE_PROFILE_CHOOSER (empathy_profile_chooser_get_type ())
+#define EMPATHY_PROFILE_CHOOSER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), \
+    EMPATHY_TYPE_PROFILE_CHOOSER, EmpathyProfileChooser))
+#define EMPATHY_PROFILE_CHOOSER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), \
+    EMPATHY_TYPE_PROFILE_CHOOSER, EmpathyProfileChooserClass))
+#define EMPATHY_IS_PROFILE_CHOOSER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), \
+    EMPATHY_TYPE_PROFILE_CHOOSER))
+#define EMPATHY_IS_PROFILE_CHOOSER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), \
+    EMPATHY_TYPE_PROFILE_CHOOSER))
+#define EMPATHY_PROFILE_CHOOSER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), \
+    EMPATHY_TYPE_PROFILE_CHOOSER, EmpathyProfileChooserClass))
+
+typedef struct _EmpathyProfileChooser EmpathyProfileChooser;
+typedef struct _EmpathyProfileChooserClass EmpathyProfileChooserClass;
+
+struct _EmpathyProfileChooser
+{
+  GtkComboBox parent;
+
+  /*<private>*/
+  gpointer priv;
+};
+
+struct _EmpathyProfileChooserClass
+{
+  GtkComboBoxClass parent_class;
+};
+
+GType empathy_profile_chooser_get_type (void) G_GNUC_CONST;
 GtkWidget * empathy_profile_chooser_new (void);
-McProfile * empathy_profile_chooser_dup_selected (GtkWidget *widget);
-gint empathy_profile_chooser_n_profiles (GtkWidget *widget);
+McProfile * empathy_profile_chooser_dup_selected (
+    EmpathyProfileChooser *profile_chooser);
+gint empathy_profile_chooser_n_profiles (
+    EmpathyProfileChooser *profile_chooser);
 
 G_END_DECLS
-#endif /*  __EMPATHY_PROTOCOL_CHOOSER_H__ */
+#endif /*  __EMPATHY_PROFILE_CHOOSER_H__ */