evolution-3.6.4/modules/cal-config-google/e-google-chooser-button.c

No issues found

  1 /*
  2  * e-google-chooser-button.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <webcal://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #include "e-google-chooser-button.h"
 20 
 21 #include <config.h>
 22 #include <glib/gi18n-lib.h>
 23 
 24 #include "e-google-chooser-dialog.h"
 25 
 26 #define E_GOOGLE_CHOOSER_BUTTON_GET_PRIVATE(obj) \
 27 	(G_TYPE_INSTANCE_GET_PRIVATE \
 28 	((obj), E_TYPE_GOOGLE_CHOOSER_BUTTON, EGoogleChooserButtonPrivate))
 29 
 30 struct _EGoogleChooserButtonPrivate {
 31 	ESource *source;
 32 	GtkWidget *label;
 33 };
 34 
 35 enum {
 36 	PROP_0,
 37 	PROP_DISPLAY_NAME,
 38 	PROP_SOURCE
 39 };
 40 
 41 G_DEFINE_DYNAMIC_TYPE (
 42 	EGoogleChooserButton,
 43 	e_google_chooser_button,
 44 	GTK_TYPE_BUTTON)
 45 
 46 static void
 47 google_chooser_button_set_source (EGoogleChooserButton *button,
 48                                   ESource *source)
 49 {
 50 	g_return_if_fail (E_IS_SOURCE (source));
 51 	g_return_if_fail (button->priv->source == NULL);
 52 
 53 	button->priv->source = g_object_ref (source);
 54 }
 55 
 56 static void
 57 google_chooser_button_set_property (GObject *object,
 58                                     guint property_id,
 59                                     const GValue *value,
 60                                     GParamSpec *pspec)
 61 {
 62 	switch (property_id) {
 63 		case PROP_SOURCE:
 64 			google_chooser_button_set_source (
 65 				E_GOOGLE_CHOOSER_BUTTON (object),
 66 				g_value_get_object (value));
 67 			return;
 68 	}
 69 
 70 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 71 }
 72 
 73 static void
 74 google_chooser_button_get_property (GObject *object,
 75                                     guint property_id,
 76                                     GValue *value,
 77                                     GParamSpec *pspec)
 78 {
 79 	switch (property_id) {
 80 		case PROP_SOURCE:
 81 			g_value_set_object (
 82 				value,
 83 				e_google_chooser_button_get_source (
 84 				E_GOOGLE_CHOOSER_BUTTON (object)));
 85 			return;
 86 	}
 87 
 88 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 89 }
 90 
 91 static void
 92 google_chooser_button_dispose (GObject *object)
 93 {
 94 	EGoogleChooserButtonPrivate *priv;
 95 
 96 	priv = E_GOOGLE_CHOOSER_BUTTON_GET_PRIVATE (object);
 97 
 98 	if (priv->source != NULL) {
 99 		g_object_unref (priv->source);
100 		priv->source = NULL;
101 	}
102 
103 	if (priv->label != NULL) {
104 		g_object_unref (priv->label);
105 		priv->label = NULL;
106 	}
107 
108 	/* Chain up to parent's dispose() method. */
109 	G_OBJECT_CLASS (e_google_chooser_button_parent_class)->dispose (object);
110 }
111 
112 static void
113 google_chooser_button_constructed (GObject *object)
114 {
115 	EGoogleChooserButton *button;
116 	ESourceWebdav *webdav_extension;
117 	GBindingFlags binding_flags;
118 	GtkWidget *widget;
119 	const gchar *display_name;
120 
121 	button = E_GOOGLE_CHOOSER_BUTTON (object);
122 
123 	/* Chain up to parent's constructed() method. */
124 	G_OBJECT_CLASS (e_google_chooser_button_parent_class)->
125 		constructed (object);
126 
127 	widget = gtk_label_new (_("Default User Calendar"));
128 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
129 	gtk_container_add (GTK_CONTAINER (button), widget);
130 	button->priv->label = g_object_ref (widget);
131 	gtk_widget_show (widget);
132 
133 	webdav_extension = e_source_get_extension (
134 		button->priv->source, E_SOURCE_EXTENSION_WEBDAV_BACKEND);
135 	display_name = e_source_webdav_get_display_name (webdav_extension);
136 
137 	binding_flags = G_BINDING_DEFAULT;
138 	if (display_name != NULL && *display_name != '\0')
139 		binding_flags |= G_BINDING_SYNC_CREATE;
140 
141 	g_object_bind_property (
142 		webdav_extension, "display-name",
143 		button->priv->label, "label",
144 		binding_flags);
145 }
146 
147 static void
148 google_chooser_button_clicked (GtkButton *button)
149 {
150 	EGoogleChooserButtonPrivate *priv;
151 	GtkWidget *chooser;
152 	GtkWidget *dialog;
153 	gpointer parent;
154 
155 	priv = E_GOOGLE_CHOOSER_BUTTON_GET_PRIVATE (button);
156 
157 	parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
158 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
159 
160 	chooser = e_google_chooser_new (priv->source);
161 
162 	dialog = e_google_chooser_dialog_new (
163 		E_GOOGLE_CHOOSER (chooser), parent);
164 
165 	if (parent != NULL)
166 		g_object_bind_property (
167 			parent, "icon-name",
168 			dialog, "icon-name",
169 			G_BINDING_SYNC_CREATE);
170 
171 	gtk_dialog_run (GTK_DIALOG (dialog));
172 
173 	gtk_widget_destroy (dialog);
174 }
175 
176 static void
177 e_google_chooser_button_class_init (EGoogleChooserButtonClass *class)
178 {
179 	GObjectClass *object_class;
180 	GtkButtonClass *button_class;
181 
182 	g_type_class_add_private (class, sizeof (EGoogleChooserButtonPrivate));
183 
184 	object_class = G_OBJECT_CLASS (class);
185 	object_class->set_property = google_chooser_button_set_property;
186 	object_class->get_property = google_chooser_button_get_property;
187 	object_class->dispose = google_chooser_button_dispose;
188 	object_class->constructed = google_chooser_button_constructed;
189 
190 	button_class = GTK_BUTTON_CLASS (class);
191 	button_class->clicked = google_chooser_button_clicked;
192 
193 	g_object_class_install_property (
194 		object_class,
195 		PROP_SOURCE,
196 		g_param_spec_object (
197 			"source",
198 			NULL,
199 			NULL,
200 			E_TYPE_SOURCE,
201 			G_PARAM_READWRITE |
202 			G_PARAM_CONSTRUCT_ONLY));
203 }
204 
205 static void
206 e_google_chooser_button_class_finalize (EGoogleChooserButtonClass *class)
207 {
208 }
209 
210 static void
211 e_google_chooser_button_init (EGoogleChooserButton *button)
212 {
213 	button->priv = E_GOOGLE_CHOOSER_BUTTON_GET_PRIVATE (button);
214 }
215 
216 void
217 e_google_chooser_button_type_register (GTypeModule *type_module)
218 {
219 	/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
220 	 *     function, so we have to wrap it with a public function in
221 	 *     order to register types from a separate compilation unit. */
222 	e_google_chooser_button_register_type (type_module);
223 }
224 
225 GtkWidget *
226 e_google_chooser_button_new (ESource *source)
227 {
228 	g_return_val_if_fail (E_IS_SOURCE (source), NULL);
229 
230 	return g_object_new (
231 		E_TYPE_GOOGLE_CHOOSER_BUTTON,
232 		"source", source, NULL);
233 }
234 
235 ESource *
236 e_google_chooser_button_get_source (EGoogleChooserButton *button)
237 {
238 	g_return_val_if_fail (E_IS_GOOGLE_CHOOSER_BUTTON (button), NULL);
239 
240 	return button->priv->source;
241 }