[gnome-media] Make GMAudioProfileChoose real widget
- From: Marc-Andre Lureau <malureau src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-media] Make GMAudioProfileChoose real widget
- Date: Tue, 8 Jun 2010 22:32:22 +0000 (UTC)
commit d9a4678333d65f099c981eb9516f24d739c963c0
Author: Tadej Borovšak <tadeboro gmail com>
Date: Wed May 5 05:21:50 2010 +0200
Make GMAudioProfileChoose real widget
https://bugzilla.gnome.org/show_bug.cgi?id=608169
profiles/audio-profile-choose.c | 468 +++++++++++++++++++++++--------
profiles/audio-profile-choose.h | 47 +++-
profiles/glade/gnome-media-profiles.xml | 21 ++-
3 files changed, 417 insertions(+), 119 deletions(-)
---
diff --git a/profiles/audio-profile-choose.c b/profiles/audio-profile-choose.c
index 0990c75..18d65d3 100644
--- a/profiles/audio-profile-choose.c
+++ b/profiles/audio-profile-choose.c
@@ -25,168 +25,412 @@
#include <string.h>
#include <glib/gi18n.h>
-#include <gtk/gtk.h>
#include <gio/gio.h>
#include <gst/gst.h>
+#include <gconf/gconf-client.h>
#include "gmp-util.h"
#include "audio-profile-choose.h"
-#include "audio-profile.h"
+
+/* Private structure */
+struct _GMAudioProfileChoosePrivate
+{
+ GtkTreeModel *model;
+ GMAudioProfile *profile;
+};
+
+enum
+{
+ P_0,
+ P_ACTIVE_PROFILE
+};
+
+enum
+{
+ S_PROFILE_CHANGED,
+ LAST_SIGNAL
+};
enum
{
NAME_COLUMN,
ID_COLUMN,
+ PROFILE_COLUMN,
N_COLUMNS
};
+static guint signals[LAST_SIGNAL] = { 0 };
+
+/* GObject methods */
+static void gm_audio_profile_choose_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec);
+static void gm_audio_profile_choose_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec);
+static void gm_audio_profile_choose_dispose (GObject *object);
+
+/* GtkComboBox methods */
+static void gm_audio_profile_choose_changed (GtkComboBox *combo);
+
+/* Internal routines */
+static void audio_profile_forgotten (GMAudioProfile *profile,
+ GMAudioProfileChoose *choose);
+
+G_DEFINE_TYPE (GMAudioProfileChoose, gm_audio_profile_choose, GTK_TYPE_COMBO_BOX)
+
+/* Initialization */
+static void
+gm_audio_profile_choose_init (GMAudioProfileChoose *choose)
+{
+ GMAudioProfileChoosePrivate *priv;
+ GtkListStore *store;
+ GList *orig,
+ *profiles;
+ GtkCellRenderer *cell;
+
+ priv = G_TYPE_INSTANCE_GET_PRIVATE (choose,
+ GM_AUDIO_TYPE_PROFILE_CHOOSE,
+ GMAudioProfileChoosePrivate);
+
+ /* Create model */
+ store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING,
+ G_TYPE_STRING,
+ GM_AUDIO_TYPE_PROFILE);
+ for (orig = profiles = gm_audio_profile_get_active_list ();
+ profiles;
+ profiles = g_list_next (profiles))
+ {
+ GMAudioProfile *profile = profiles->data;
+ gchar *profile_name,
+ *temp_file_name,
+ *mime_type_description;
+ GtkTreeIter iter;
+
+ temp_file_name = g_strdup_printf (".%s", gm_audio_profile_get_extension (profile));
+ mime_type_description = g_content_type_get_description (temp_file_name);
+ g_free (temp_file_name);
+
+ profile_name = g_strdup_printf (gettext ("%s (%s)"),
+ gm_audio_profile_get_name (profile),
+ mime_type_description);
+ g_free (mime_type_description);
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter,
+ NAME_COLUMN, profile_name,
+ ID_COLUMN, gm_audio_profile_get_id (profile),
+ PROFILE_COLUMN, profile,
+ -1);
+
+ g_signal_connect (profile, "forgotten",
+ G_CALLBACK (audio_profile_forgotten), choose);
+
+ g_free (profile_name);
+ }
+ g_list_free (orig);
+
+ /* Display name in the combo */
+ cell = gtk_cell_renderer_text_new ();
+ gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (choose), cell, TRUE );
+ gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (choose), cell, "text", NAME_COLUMN);
+ gtk_combo_box_set_model (GTK_COMBO_BOX (choose), GTK_TREE_MODEL (store));
+ g_object_unref (store);
+
+ /* Populate private struct */
+ priv->model = GTK_TREE_MODEL (store);
+ priv->profile = NULL;
+
+ /* cache access to private data */
+ choose->priv = priv;
+}
+
+static void
+gm_audio_profile_choose_class_init (GMAudioProfileChooseClass *klass)
+{
+ GObjectClass *g_class = G_OBJECT_CLASS (klass);
+ GtkComboBoxClass *c_class = GTK_COMBO_BOX_CLASS (klass);
+ GParamSpec *pspec;
+
+ g_class->get_property = gm_audio_profile_choose_get_property;
+ g_class->set_property = gm_audio_profile_choose_set_property;
+ g_class->dispose = gm_audio_profile_choose_dispose;
+
+ c_class->changed = gm_audio_profile_choose_changed;
+
+ /* Signals */
+ /**
+ * GMAudioProfileChoose::profile-changed:
+ * @choose: the object which received the signal
+ *
+ * The profile-changed signal is emitted when the active profile is changed.
+ *
+ * Since: 2.32/3.0
+ */
+ signals[S_PROFILE_CHANGED] =
+ g_signal_new (g_intern_static_string ("profile-changed"),
+ G_OBJECT_CLASS_TYPE (klass),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GMAudioProfileChooseClass, profile_changed),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE,
+ 1, GM_AUDIO_TYPE_PROFILE);
+
+ /* Properties */
+ /**
+ * GMAudioProfileChoose:active-profile:
+ *
+ * Currently shown #GMAudioProfile.
+ *
+ * Since: 2.32/3.0
+ */
+ pspec = g_param_spec_object ("active-profile",
+ "Active profile",
+ "Currently selected GMAudioProfile",
+ GM_AUDIO_TYPE_PROFILE,
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+ g_object_class_install_property (g_class, P_ACTIVE_PROFILE, pspec);
+
+ /* Add private data */
+ g_type_class_add_private (g_class, sizeof (GMAudioProfileChoose));
+}
+
+static void
+gm_audio_profile_choose_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ switch (prop_id)
+ {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gm_audio_profile_choose_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GMAudioProfileChoosePrivate *priv = GM_AUDIO_PROFILE_CHOOSE (object)->priv;
+
+ switch (prop_id)
+ {
+ case P_ACTIVE_PROFILE:
+ g_value_set_object (value, priv->profile);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gm_audio_profile_choose_dispose (GObject *object)
+{
+ GMAudioProfileChoosePrivate *priv = GM_AUDIO_PROFILE_CHOOSE (object)->priv;
+
+ if (priv->profile)
+ {
+ g_object_unref (priv->profile);
+ priv->profile = NULL;
+ }
+
+ G_OBJECT_CLASS (gm_audio_profile_choose_parent_class)->dispose (object);
+}
+
static void
-audio_profile_forgotten (GMAudioProfile *profile, GtkWidget *combo)
+gm_audio_profile_choose_changed (GtkComboBox *combo)
{
- GtkTreeModel *model;
- GtkTreeIter iter;
- char *tmp;
- const char *id;
+ GMAudioProfileChoosePrivate *priv;
+ GtkTreeIter iter;
+ GMAudioProfile *new_profile = NULL;
- g_return_if_fail (GTK_IS_COMBO_BOX (combo));
- g_return_if_fail (GM_AUDIO_PROFILE (profile));
+ priv = GM_AUDIO_PROFILE_CHOOSE (combo)->priv;
+ if (gtk_combo_box_get_active_iter (combo, &iter))
+ gtk_tree_model_get (priv->model, &iter, PROFILE_COLUMN, &new_profile, -1);
- id = gm_audio_profile_get_id (profile);
- GST_DEBUG ("forgotten id: %s", id);
+ if (priv->profile != new_profile)
+ {
+ if (priv->profile)
+ g_object_unref (priv->profile);
+ priv->profile = new_profile;
+ g_signal_emit (combo, signals[S_PROFILE_CHANGED], 0, new_profile);
+ g_object_notify (G_OBJECT (combo), "active-profile");
+ }
+ else
+ if (new_profile)
+ g_object_unref (new_profile);
+}
- model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
+static void
+audio_profile_forgotten (GMAudioProfile *profile,
+ GMAudioProfileChoose *choose)
+{
+ GMAudioProfileChoosePrivate *priv;
+ GtkTreeIter iter;
+ GMAudioProfile *tmp;
- if (!gtk_tree_model_get_iter_first (model, &iter))
+ priv = choose->priv;
+
+ if (!gtk_tree_model_get_iter_first (priv->model, &iter))
return;
- while (1)
+ do
{
- gtk_tree_model_get (model, &iter, ID_COLUMN, &tmp, -1);
- if (g_str_equal (tmp, id))
+ gtk_tree_model_get (priv->model, &iter, PROFILE_COLUMN, &tmp, -1);
+ if (tmp == profile)
{
- gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
- g_free (tmp);
+ gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
+ g_object_unref (tmp);
return;
}
- g_free (tmp);
-
- if (!gtk_tree_model_iter_next (model, &iter))
- break;
+ g_object_unref (tmp);
}
+ while (gtk_tree_model_iter_next (priv->model, &iter));
}
-/* create and return a new Profile Choose combobox widget
- * given the GConf connection
+/* Public API */
+/**
+ * gm_audio_profile_choose_new:
+ *
+ * Creates new #GMAudioProfileChoose, populated with currently active profiles.
+ *
+ * Return value: A new #GMAudioProfileChoose.
*/
-
GtkWidget*
gm_audio_profile_choose_new (void)
{
- GtkListStore *list_store;
- GtkTreeIter iter;
- GtkCellRenderer *renderer;
- GList *profiles, *orig;
- GtkWidget *combo;
-
- /* Create the model */
- list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
- orig = profiles = gm_audio_profile_get_active_list ();
- combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list_store));
-
- while (profiles) {
- GMAudioProfile *profile = profiles->data;
- char *profile_name, *temp_file_name;
- char* mime_type_description;
-
- temp_file_name = g_strdup_printf (".%s", gm_audio_profile_get_extension (profile));
- mime_type_description = g_content_type_get_description (temp_file_name);
- g_free (temp_file_name);
-
- profile_name = g_strdup_printf (gettext ("%s (%s)"), gm_audio_profile_get_name (profile), mime_type_description);
- g_free (mime_type_description);
- gtk_list_store_append (list_store, &iter);
- gtk_list_store_set (list_store, &iter,
- NAME_COLUMN, profile_name,
- ID_COLUMN, gm_audio_profile_get_id (profile),
- -1);
-
- g_signal_connect (profile, "forgotten", G_CALLBACK (audio_profile_forgotten), combo);
-
- profiles = profiles->next;
- g_free (profile_name);
- }
- g_list_free (orig);
-
- /* display name in the combobox */
- renderer = gtk_cell_renderer_text_new ();
- gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo),
- renderer,
- TRUE);
- gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
- "text", NAME_COLUMN,
- NULL);
-
- /* activate first one */
- gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
-
- return combo;
+ return g_object_new (GM_AUDIO_TYPE_PROFILE_CHOOSE, NULL);
}
-/* get the currently active gm_audio profile */
-GMAudioProfile*
-gm_audio_profile_choose_get_active (GtkWidget *choose)
+/**
+ * gm_audio_profile_choose_get_active_profile:
+ * @choose: #GMAudioProfileChoose widget
+ *
+ * This function retrieves currently selected #GMAudioProfile. Returned object is
+ * owned by #GMAudioProfileChoose and should not be unreferenced.
+ *
+ * Return value: Currently selected #GMAudioProfile or %NULL if none is
+ * selected.
+ *
+ * Since: 2.32/3.0
+ */
+GMAudioProfile *
+gm_audio_profile_choose_get_active_profile (GMAudioProfileChoose *choose)
{
- GtkTreeIter iter;
- GtkComboBox *combo = GTK_COMBO_BOX (choose);
- gchar *id;
- GMAudioProfile *profile = NULL;
-
- g_return_val_if_fail (GTK_IS_COMBO_BOX (choose), NULL);
- /* get active id */
- if (gtk_combo_box_get_active_iter (combo, &iter)) {
- gtk_tree_model_get (gtk_combo_box_get_model (combo), &iter,
- ID_COLUMN, &id, -1);
-
- /* look up gm_audio profile */
- profile = gm_audio_profile_lookup (id);
- g_free (id);
- }
- return profile;
+ g_return_val_if_fail (GM_AUDIO_IS_PROFILE_CHOOSE (choose), NULL);
+
+ return choose->priv->profile;
}
+/**
+ * gm_audio_profile_choose_set_active_profile:
+ * @choose: #GMAudioProfileChoose widget
+ * @id: A id of #GMAudioProfile that should be selected
+ *
+ * This function sets currently selected #GMAudioProfile. If no profile matches
+ * the @id, first available profile is set as selected and function returns
+ * %FALSE.
+ *
+ * Return value: %TRUE if profile has been successfully set, %FALSE otherwise.
+ *
+ * Since: 2.32/3.0
+ */
gboolean
-gm_audio_profile_choose_set_active (GtkWidget *choose,
- const char *id)
+gm_audio_profile_choose_set_active_profile (GMAudioProfileChoose *choose,
+ const gchar *id)
{
- GtkTreeModel *model;
- GtkTreeIter iter;
- char *tmp;
-
- g_return_val_if_fail (GTK_IS_COMBO_BOX (choose), FALSE);
+ GMAudioProfileChoosePrivate *priv;
+ GtkTreeIter iter;
+ gchar *tmp;
+
+ g_return_val_if_fail (GM_AUDIO_IS_PROFILE_CHOOSE (choose), FALSE);
+
+ priv = choose->priv;
- model = gtk_combo_box_get_model (GTK_COMBO_BOX (choose));
-
- if (!gtk_tree_model_get_iter_first (model, &iter))
+ if (!gtk_tree_model_get_iter_first (priv->model, &iter))
return FALSE;
- while (1)
+ do
{
- gtk_tree_model_get (model, &iter, ID_COLUMN, &tmp, -1);
- if (!strcmp (tmp, id))
+ gtk_tree_model_get (priv->model, &iter, ID_COLUMN, &tmp, -1);
+ if (!g_strcmp0 (tmp, id))
{
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (choose), &iter);
g_free (tmp);
return TRUE;
}
- g_free (tmp);
-
- if (!gtk_tree_model_iter_next (model, &iter))
- break;
+ g_free (tmp);
}
+ while (gtk_tree_model_iter_next (priv->model, &iter));
/* Fallback to first entry */
- gtk_tree_model_get_iter_first (model, &iter);
- gtk_combo_box_set_active_iter (GTK_COMBO_BOX (choose), &iter);
-
+ gtk_combo_box_set_active (GTK_COMBO_BOX (choose), 0);
+
return FALSE;
}
+
+/* Deprecated functions */
+/**
+ * gm_audio_profile_choose_get_active:
+ * @choose: A #GMAudioProfileChoose widget
+ *
+ * This function retrieves currently selected #GMAudioProfile. Returned object is
+ * owned by #GMAudioProfileChoose and should not be unreferenced.
+ *
+ * Return value: Currently selected #GMAudioProfile or %NULL if none is
+ * selected.
+ *
+ * Deprecated: 2.32/3.0: Use gm_audio_profile_choose_get_active_profile()
+ * instead.
+ */
+GMAudioProfile*
+gm_audio_profile_choose_get_active (GtkWidget *choose)
+{
+ /* We cannot simply wrap gm_audio_profile_choose_get_active_profile() here,
+ * since this function can be (and in case of sound-juicer is) called from
+ * GtkComboBox::changed signal handler. In this situation, code would return
+ * invalid profile, since we haven't updated the current selection yet. */
+ GMAudioProfileChoosePrivate *priv;
+ GtkTreeIter iter;
+ GMAudioProfile *new_profile = NULL;
+
+ g_return_val_if_fail (GM_AUDIO_IS_PROFILE_CHOOSE (choose), NULL);
+
+ priv = GM_AUDIO_PROFILE_CHOOSE (choose)->priv;
+ if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (choose), &iter))
+ {
+ gtk_tree_model_get (priv->model, &iter, PROFILE_COLUMN, &new_profile, -1);
+ g_object_unref (new_profile);
+ }
+
+ return new_profile;
+}
+
+/**
+ * gm_audio_profile_choose_set_active:
+ * @choose: #GMAudioProfileChoose widget
+ * @id: A id of #GMAudioProfile that should be selected
+ *
+ * This function sets currently selected #GMAudioProfile. If no profile matches
+ * the @id, first available profile is set as selected and function returns
+ * %FALSE.
+ *
+ * Return value: %TRUE if profile has been successfully set, %FALSE otherwise.
+ *
+ * Deprecated: 2.32/3.0: Use gm_audio_profile_choose_set_active_profile()
+ * instead.
+ */
+gboolean
+gm_audio_profile_choose_set_active (GtkWidget *choose,
+ const char *id)
+{
+ return gm_audio_profile_choose_set_active_profile (GM_AUDIO_PROFILE_CHOOSE (choose), id);
+}
diff --git a/profiles/audio-profile-choose.h b/profiles/audio-profile-choose.h
index 6091a79..cc4dfcc 100644
--- a/profiles/audio-profile-choose.h
+++ b/profiles/audio-profile-choose.h
@@ -24,15 +24,50 @@
#include "audio-profile.h"
#include <gtk/gtk.h>
-#include <gconf/gconf-client.h>
G_BEGIN_DECLS
-/* create a new Profile Choose Dialog */
-GtkWidget* gm_audio_profile_choose_new (void);
-GMAudioProfile* gm_audio_profile_choose_get_active (GtkWidget *choose);
-gboolean gm_audio_profile_choose_set_active (GtkWidget *choose,
- const char *id);
+/* Standard macros */
+#define GM_AUDIO_TYPE_PROFILE_CHOOSE (gm_audio_profile_choose_get_type ())
+#define GM_AUDIO_PROFILE_CHOOSE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GM_AUDIO_TYPE_PROFILE_CHOOSE, GMAudioProfileChoose))
+#define GM_AUDIO_PROFILE_CHOOSE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GM_AUDIO_TYPE_PROFILE_CHOOSE, GMAudioProfileChooseClass))
+#define GM_AUDIO_IS_PROFILE_CHOOSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GM_AUDIO_TYPE_PROFILE_CHOOSE))
+#define GM_AUDIO_IS_PROFILE_CHOOSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GM_AUDIO_TYPE_PROFILE_CHOOSE))
+#define GM_AUDIO_PROFILE_CHOOSE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GM_AUDIO_TYPE_PROFILE_CHOOSE, GMAudioProfileChooseClass))
+
+/* Structs */
+typedef struct _GMAudioProfileChoose GMAudioProfileChoose;
+typedef struct _GMAudioProfileChooseClass GMAudioProfileChooseClass;
+typedef struct _GMAudioProfileChoosePrivate GMAudioProfileChoosePrivate;
+
+struct _GMAudioProfileChoose
+{
+ GtkComboBox parent;
+
+ /*< Private >*/
+ GMAudioProfileChoosePrivate *priv;
+};
+
+struct _GMAudioProfileChooseClass
+{
+ GtkComboBoxClass parent_class;
+
+ /* Signals */
+ void (*profile_changed) (GMAudioProfileChoose *choose,
+ GMAudioProfile *profile);
+};
+
+/* Public API */
+GType gm_audio_profile_choose_get_type (void) G_GNUC_CONST;
+GtkWidget *gm_audio_profile_choose_new (void);
+GMAudioProfile *gm_audio_profile_choose_get_active_profile (GMAudioProfileChoose *choose);
+gboolean gm_audio_profile_choose_set_active_profile (GMAudioProfileChoose *choose,
+ const gchar *id);
+
+/* Deprecated API */
+GMAudioProfile *gm_audio_profile_choose_get_active (GtkWidget *choose);
+gboolean gm_audio_profile_choose_set_active (GtkWidget *choose,
+ const char *id);
G_END_DECLS
diff --git a/profiles/glade/gnome-media-profiles.xml b/profiles/glade/gnome-media-profiles.xml
index c2bd46d..d80b55b 100644
--- a/profiles/glade/gnome-media-profiles.xml
+++ b/profiles/glade/gnome-media-profiles.xml
@@ -1,10 +1,29 @@
-<glade-catalog supports="gtkbuilder" name="gnome-media-profiles" library="gnome-media-profiles" depends="gtk+">
+<?xml version="1.0" encoding="UTF-8"?>
+
+<glade-catalog supports="gtkbuilder" name="gnome-media-profiles" library="gnome-media-profiles" book="gnome-media" depends="gtk+">
<init-function>gnome_media_profiles_catalog_init</init-function>
<glade-widget-classes>
<glade-widget-class name="GMAudioProfileEdit" get-type-function="gm_audio_profile_edit_get_type"
generic-name="audio-profile-edit" title="AudioProfileEdit"/>
+
+ <glade-widget-class name="GMAudioProfileChoose"
+ get-type-function="gm_audio_profile_choose_get_type"
+ generic-name="audio-profile-choose"
+ title="AudioProfileChoose">
+ <properties>
+ <property id="active-profile" ignore="True"/>
+ </properties>
+ <signals>
+ <signal id="profile-changed"/>
+ </signals>
+ </glade-widget-class>
</glade-widget-classes>
+ <glade-widget-group name="gnome-media" title="Gnome Media">
+ <glade-widget-class-ref name="GMAudioProfileEdit"/>
+ <glade-widget-class-ref name="GMAudioProfileChoose"/>
+ </glade-widget-group>
+
</glade-catalog>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]