[evolution-data-server] ESourceSelector: Add e_source_selector_ref_primary_selection().



commit 061a346f0a27e8ce390cd7c6a59a3f639f530e76
Author: Matthew Barnes <mbarnes redhat com>
Date:   Sat Apr 21 12:01:12 2012 -0400

    ESourceSelector: Add e_source_selector_ref_primary_selection().
    
    Replaces e_source_selector_get_primary_selection().
    
    Returns a new ESource reference that the caller must unreference.

 .../libedataserverui/libedataserverui-sections.txt |    2 +-
 libedataserverui/e-source-selector-dialog.c        |  159 +++++++++++---------
 libedataserverui/e-source-selector.c               |   39 ++++--
 libedataserverui/e-source-selector.h               |    2 +-
 4 files changed, 116 insertions(+), 86 deletions(-)
---
diff --git a/docs/reference/libedataserverui/libedataserverui-sections.txt b/docs/reference/libedataserverui/libedataserverui-sections.txt
index 04d87d1..c45a894 100644
--- a/docs/reference/libedataserverui/libedataserverui-sections.txt
+++ b/docs/reference/libedataserverui/libedataserverui-sections.txt
@@ -404,7 +404,7 @@ e_source_selector_show_selection
 e_source_selector_selection_shown
 e_source_selector_set_select_new
 e_source_selector_edit_primary_selection
-e_source_selector_get_primary_selection
+e_source_selector_ref_primary_selection
 e_source_selector_set_primary_selection
 e_source_selector_get_primary_source_group
 e_source_selector_peek_primary_selection
diff --git a/libedataserverui/e-source-selector-dialog.c b/libedataserverui/e-source-selector-dialog.c
index dab186f..a3c20a0 100644
--- a/libedataserverui/e-source-selector-dialog.c
+++ b/libedataserverui/e-source-selector-dialog.c
@@ -45,6 +45,50 @@ G_DEFINE_TYPE (
 	GTK_TYPE_DIALOG)
 
 static void
+source_selector_dialog_row_activated_cb (GtkTreeView *tree_view,
+                                         GtkTreePath *path,
+                                         GtkTreeViewColumn *column,
+                                         GtkWidget *dialog)
+{
+	gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+}
+
+static void
+primary_selection_changed_cb (ESourceSelector *selector,
+                              ESourceSelectorDialog *dialog)
+{
+	ESourceSelectorDialogPrivate *priv = dialog->priv;
+
+	if (priv->selected_source != NULL)
+		g_object_unref (priv->selected_source);
+	priv->selected_source =
+		e_source_selector_ref_primary_selection (selector);
+
+	/* FIXME Add an API for "except-source" or to
+	 *       get the ESourceSelector from outside. */
+	if (priv->selected_source != NULL) {
+		ESource *except_source;
+
+		except_source = g_object_get_data (
+			G_OBJECT (dialog), "except-source");
+
+		if (except_source != NULL) {
+			const gchar *except_uid, *selected_uid;
+
+			except_uid = e_source_peek_uid (except_source);
+			selected_uid = e_source_peek_uid (priv->selected_source);
+
+			if (except_uid && selected_uid && g_str_equal (except_uid, selected_uid))
+				priv->selected_source = NULL;
+		}
+	}
+
+	gtk_dialog_set_response_sensitive (
+		GTK_DIALOG (dialog), GTK_RESPONSE_OK,
+		(priv->selected_source != NULL));
+}
+
+static void
 source_selector_dialog_dispose (GObject *object)
 {
 	ESourceSelectorDialogPrivate *priv;
@@ -102,101 +146,68 @@ e_source_selector_dialog_init (ESourceSelectorDialog *dialog)
 
 /* Public API */
 
-static void
-row_activated_cb (GtkTreeView *tree_view,
-                  GtkTreePath *path,
-                  GtkTreeViewColumn *column,
-                  GtkWidget *dialog)
-{
-	gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
-}
-
-static void
-primary_selection_changed_cb (ESourceSelector *selector,
-                              gpointer user_data)
-{
-	ESourceSelectorDialog *dialog = user_data;
-	ESourceSelectorDialogPrivate *priv = dialog->priv;
-
-	if (priv->selected_source)
-		g_object_unref (priv->selected_source);
-	priv->selected_source = e_source_selector_get_primary_selection (selector);
-
-	/* FIXME: add an API to "except-source" or to get the ESourceSelector from outside */
-	if (priv->selected_source) {
-		ESource *except_source = g_object_get_data (G_OBJECT (dialog), "except-source");
-
-		if (except_source) {
-			const gchar *except_uid, *selected_uid;
-
-			except_uid = e_source_peek_uid (except_source);
-			selected_uid = e_source_peek_uid (priv->selected_source);
-
-			if (except_uid && selected_uid && g_str_equal (except_uid, selected_uid))
-				priv->selected_source = NULL;
-		}
-	}
-
-	if (priv->selected_source) {
-		g_object_ref (priv->selected_source);
-		gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
-	} else
-		gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
-}
-
 static GtkWidget *
 setup_dialog (GtkWindow *parent,
               ESourceSelectorDialog *dialog,
               ESourceList *source_list)
 {
-	GtkWidget *vbox, *label, *scroll, *hbox, *spacer;
-	GtkWidget *content_area;
+	GtkWidget *label, *hbox;
+	GtkWidget *container;
+	GtkWidget *widget;
 	gchar *label_text;
 	ESourceSelectorDialogPrivate *priv = dialog->priv;
 
 	priv->source_list = g_object_ref (source_list);
 
-	content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+	container = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
 
-	vbox = gtk_vbox_new (FALSE, 12);
-	gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
-	gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
-	gtk_widget_show (vbox);
+	widget = gtk_vbox_new (FALSE, 12);
+	gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
+	gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
+	gtk_widget_show (widget);
+
+	container = widget;
 
 	label_text = g_strdup_printf ("<b>%s</b>", _("_Destination"));
 	label = gtk_label_new_with_mnemonic (label_text);
 	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
-	g_free (label_text);
+	gtk_box_pack_start (GTK_BOX (container), label, FALSE, FALSE, 0);
 	gtk_widget_show (label);
-	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
+	g_free (label_text);
 
 	hbox = gtk_hbox_new (FALSE, 12);
-	gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
+	gtk_box_pack_start (GTK_BOX (container), hbox, TRUE, TRUE, 0);
 	gtk_widget_show (hbox);
 
-	spacer = gtk_label_new ("");
-	gtk_box_pack_start (GTK_BOX (hbox), spacer, FALSE, FALSE, 0);
-	gtk_widget_show (spacer);
-
-	scroll = gtk_scrolled_window_new (NULL, NULL);
-	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
-					GTK_POLICY_AUTOMATIC,
-					GTK_POLICY_AUTOMATIC);
-	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll),
-					     GTK_SHADOW_IN);
-	gtk_widget_show (scroll);
-	priv->source_selector = e_source_selector_new (source_list);
-	e_source_selector_show_selection (E_SOURCE_SELECTOR (priv->source_selector), FALSE);
-	g_signal_connect (G_OBJECT (priv->source_selector), "row_activated",
-			  G_CALLBACK (row_activated_cb), dialog);
-	g_signal_connect (G_OBJECT (priv->source_selector), "primary_selection_changed",
-			  G_CALLBACK (primary_selection_changed_cb), dialog);
-	gtk_widget_show (priv->source_selector);
-	gtk_container_add (GTK_CONTAINER (scroll), priv->source_selector);
-	gtk_box_pack_start (GTK_BOX (hbox), scroll, TRUE, TRUE, 0);
-
-	gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->source_selector);
+	widget = gtk_label_new ("");
+	gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
+	gtk_widget_show (widget);
+
+	widget = gtk_scrolled_window_new (NULL, NULL);
+	gtk_scrolled_window_set_policy (
+		GTK_SCROLLED_WINDOW (widget),
+		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+	gtk_scrolled_window_set_shadow_type (
+		GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
+	gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
+	gtk_widget_show (widget);
+
+	container = widget;
+
+	widget = e_source_selector_new (source_list);
+	e_source_selector_show_selection (E_SOURCE_SELECTOR (widget), FALSE);
+	gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
+	gtk_container_add (GTK_CONTAINER (container), widget);
+	dialog->priv->source_selector = widget;
+	gtk_widget_show (widget);
+
+	g_signal_connect (
+		widget, "row_activated",
+		G_CALLBACK (source_selector_dialog_row_activated_cb), dialog);
+	g_signal_connect (
+		widget, "primary_selection_changed",
+		G_CALLBACK (primary_selection_changed_cb), dialog);
 
 	return GTK_WIDGET (dialog);
 }
diff --git a/libedataserverui/e-source-selector.c b/libedataserverui/e-source-selector.c
index 9d6986b..374e56d 100644
--- a/libedataserverui/e-source-selector.c
+++ b/libedataserverui/e-source-selector.c
@@ -353,7 +353,14 @@ rebuild_model (ESourceSelector *selector)
 	store = GTK_TREE_STORE (model);
 
 	rebuild_data = create_rebuild_data (selector);
-	set_primary = e_source_selector_get_primary_selection (selector) != NULL;
+	source = e_source_selector_ref_primary_selection (selector);
+
+	if (source != NULL) {
+		set_primary = TRUE;
+		g_object_unref (source);
+	} else {
+		set_primary = FALSE;
+	}
 
 	gtk_tree_model_foreach (model, rebuild_existing_cb, rebuild_data);
 
@@ -452,11 +459,13 @@ rebuild_model (ESourceSelector *selector)
 	if (rebuild_data->selection_changed)
 		g_signal_emit (selector, signals[SELECTION_CHANGED], 0);
 
-	source = e_source_selector_get_primary_selection (selector);
+	source = e_source_selector_ref_primary_selection (selector);
 	if (set_primary && source == NULL) {
 		ESourceList *source_list = selector->priv->list;
 		source = e_source_list_peek_source_any (source_list);
 		e_source_selector_set_primary_selection (selector, source);
+	} else if (source != NULL) {
+		g_object_unref (source);
 	}
 
 	free_rebuild_data (rebuild_data);
@@ -830,9 +839,9 @@ source_selector_get_property (GObject *object,
 {
 	switch (property_id) {
 		case PROP_PRIMARY_SELECTION:
-			g_value_set_object (
+			g_value_take_object (
 				value,
-				e_source_selector_get_primary_selection (
+				e_source_selector_ref_primary_selection (
 				E_SOURCE_SELECTOR (object)));
 			return;
 
@@ -893,6 +902,7 @@ source_selector_button_press_event (GtkWidget *widget,
 	GtkWidgetClass *widget_class;
 	GtkTreePath *path;
 	ESource *source = NULL;
+	ESource *primary;
 	gboolean right_click = FALSE;
 	gboolean triple_click = FALSE;
 	gboolean row_exists;
@@ -943,8 +953,11 @@ source_selector_button_press_event (GtkWidget *widget,
 	if (source == NULL)
 		goto chainup;
 
-	if (source != e_source_selector_get_primary_selection (selector))
+	primary = e_source_selector_ref_primary_selection (selector);
+	if (source != primary)
 		e_source_selector_set_primary_selection (selector, source);
+	if (primary != NULL)
+		g_object_unref (primary);
 
 	if (right_click)
 		g_signal_emit (
@@ -1117,9 +1130,12 @@ source_selector_popup_menu (GtkWidget *widget)
 	gboolean res = FALSE;
 
 	selector = E_SOURCE_SELECTOR (widget);
-	source = e_source_selector_get_primary_selection (selector);
+	source = e_source_selector_ref_primary_selection (selector);
 	g_signal_emit (selector, signals[POPUP_EVENT], 0, source, NULL, &res);
 
+	if (source != NULL)
+		g_object_unref (source);
+
 	return res;
 }
 
@@ -1769,7 +1785,7 @@ e_source_selector_edit_primary_selection (ESourceSelector *selector)
 }
 
 /**
- * e_source_selector_get_primary_selection:
+ * e_source_selector_ref_primary_selection:
  * @selector: An #ESourceSelector widget
  *
  * Get the primary selected source.  The primary selection is the one that is
@@ -1777,10 +1793,15 @@ e_source_selector_edit_primary_selection (ESourceSelector *selector)
  * to the "normal" selection, which is the set of source whose checkboxes are
  * checked).
  *
+ * The returned #ESource is referenced for thread-safety and must be
+ * unreferenced with g_object_unref() when finished with it.
+ *
  * Returns: The selected source.
+ *
+ * Since: 3.6
  **/
 ESource *
-e_source_selector_get_primary_selection (ESourceSelector *selector)
+e_source_selector_ref_primary_selection (ESourceSelector *selector)
 {
 	GtkTreeRowReference *reference;
 	GtkTreeSelection *selection;
@@ -1823,8 +1844,6 @@ e_source_selector_get_primary_selection (ESourceSelector *selector)
 		return NULL;
 	}
 
-	g_object_unref (data);
-
 	return E_SOURCE (data);
 }
 
diff --git a/libedataserverui/e-source-selector.h b/libedataserverui/e-source-selector.h
index 19c4930..edfa5bc 100644
--- a/libedataserverui/e-source-selector.h
+++ b/libedataserverui/e-source-selector.h
@@ -105,7 +105,7 @@ void		e_source_selector_set_select_new
 						 gboolean state);
 void		e_source_selector_edit_primary_selection
 						(ESourceSelector *selector);
-ESource *	e_source_selector_get_primary_selection
+ESource *	e_source_selector_ref_primary_selection
 						(ESourceSelector *selector);
 void		e_source_selector_set_primary_selection
 						(ESourceSelector *selector,



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