evolution-3.6.4/addressbook/gui/contact-editor/e-contact-editor-fullname.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Chris Toshok <toshok@ximian.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include <glib/gi18n.h>
 28 
 29 #include "e-util/e-util.h"
 30 #include "e-util/e-util-private.h"
 31 
 32 #include "e-contact-editor-fullname.h"
 33 
 34 static void fill_in_info (EContactEditorFullname *editor);
 35 static void extract_info (EContactEditorFullname *editor);
 36 
 37 enum {
 38 	PROP_0,
 39 	PROP_NAME,
 40 	PROP_EDITABLE
 41 };
 42 
 43 G_DEFINE_TYPE (
 44 	EContactEditorFullname,
 45 	e_contact_editor_fullname,
 46 	GTK_TYPE_DIALOG)
 47 
 48 static void
 49 e_contact_editor_fullname_set_property (GObject *object,
 50                                         guint property_id,
 51                                         const GValue *value,
 52                                         GParamSpec *pspec)
 53 {
 54 	EContactEditorFullname *e_contact_editor_fullname;
 55 
 56 	e_contact_editor_fullname = E_CONTACT_EDITOR_FULLNAME (object);
 57 
 58 	switch (property_id) {
 59 	case PROP_NAME:
 60 		e_contact_name_free (e_contact_editor_fullname->name);
 61 
 62 		if (g_value_get_pointer (value) != NULL) {
 63 			e_contact_editor_fullname->name =
 64 				e_contact_name_copy (
 65 				g_value_get_pointer (value));
 66 			fill_in_info (e_contact_editor_fullname);
 67 		}
 68 		else {
 69 			e_contact_editor_fullname->name = NULL;
 70 		}
 71 		break;
 72 	case PROP_EDITABLE: {
 73 		gboolean editable;
 74 		gint i;
 75 
 76 		const gchar *widget_names[] = {
 77 			"comboentry-title",
 78 			"comboentry-suffix",
 79 			"entry-first",
 80 			"entry-middle",
 81 			"entry-last",
 82 			"label-title",
 83 			"label-suffix",
 84 			"label-first",
 85 			"label-middle",
 86 			"label-last",
 87 			NULL
 88 		};
 89 
 90 		editable = g_value_get_boolean (value);
 91 		e_contact_editor_fullname->editable = editable;
 92 
 93 		for (i = 0; widget_names[i] != NULL; i++) {
 94 			GtkWidget *widget;
 95 
 96 			widget = e_builder_get_widget (
 97 				e_contact_editor_fullname->builder,
 98 				widget_names[i]);
 99 
100 			if (GTK_IS_ENTRY (widget)) {
101 				gtk_editable_set_editable (
102 					GTK_EDITABLE (widget), editable);
103 
104 			} else if (GTK_IS_COMBO_BOX (widget)) {
105 				GtkWidget *child;
106 
107 				child = gtk_bin_get_child (GTK_BIN (widget));
108 
109 				gtk_editable_set_editable (
110 					GTK_EDITABLE (child), editable);
111 				gtk_widget_set_sensitive (widget, editable);
112 
113 			} else if (GTK_IS_LABEL (widget)) {
114 				gtk_widget_set_sensitive (widget, editable);
115 			}
116 		}
117 		break;
118 	}
119 	default:
120 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
121 		break;
122 	}
123 }
124 
125 static void
126 e_contact_editor_fullname_get_property (GObject *object,
127                                         guint property_id,
128                                         GValue *value,
129                                         GParamSpec *pspec)
130 {
131 	EContactEditorFullname *e_contact_editor_fullname;
132 
133 	e_contact_editor_fullname = E_CONTACT_EDITOR_FULLNAME (object);
134 
135 	switch (property_id) {
136 	case PROP_NAME:
137 		extract_info (e_contact_editor_fullname);
138 		g_value_set_pointer (
139 			value, e_contact_name_copy (
140 			e_contact_editor_fullname->name));
141 		break;
142 	case PROP_EDITABLE:
143 		g_value_set_boolean (
144 			value, e_contact_editor_fullname->editable);
145 		break;
146 	default:
147 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
148 		break;
149 	}
150 }
151 
152 static void
153 e_contact_editor_fullname_dispose (GObject *object)
154 {
155 	EContactEditorFullname *e_contact_editor_fullname;
156 
157 	e_contact_editor_fullname = E_CONTACT_EDITOR_FULLNAME (object);
158 
159 	if (e_contact_editor_fullname->builder) {
160 		g_object_unref (e_contact_editor_fullname->builder);
161 		e_contact_editor_fullname->builder = NULL;
162 	}
163 
164 	if (e_contact_editor_fullname->name) {
165 		e_contact_name_free (e_contact_editor_fullname->name);
166 		e_contact_editor_fullname->name = NULL;
167 	}
168 
169 	/* Chain up to parent's dispose() method. */
170 	G_OBJECT_CLASS (e_contact_editor_fullname_parent_class)->dispose (object);
171 }
172 
173 static void
174 e_contact_editor_fullname_class_init (EContactEditorFullnameClass *class)
175 {
176 	GObjectClass *object_class;
177 
178 	object_class = G_OBJECT_CLASS (class);
179 	object_class->set_property = e_contact_editor_fullname_set_property;
180 	object_class->get_property = e_contact_editor_fullname_get_property;
181 	object_class->dispose = e_contact_editor_fullname_dispose;
182 
183 	g_object_class_install_property (
184 		object_class,
185 		PROP_NAME,
186 		g_param_spec_pointer (
187 			"name",
188 			"Name",
189 			NULL,
190 			G_PARAM_READWRITE));
191 
192 	g_object_class_install_property (
193 		object_class,
194 		PROP_EDITABLE,
195 		g_param_spec_boolean (
196 			"editable",
197 			"Editable",
198 			NULL,
199 			FALSE,
200 			G_PARAM_READWRITE));
201 }
202 
203 static void
204 e_contact_editor_fullname_init (EContactEditorFullname *e_contact_editor_fullname)
205 {
206 	GtkBuilder *builder;
207 	GtkDialog *dialog;
208 	GtkWidget *parent;
209 	GtkWidget *widget;
210 	GtkWidget *action_area;
211 	GtkWidget *content_area;
212 	const gchar *title;
213 
214 	dialog = GTK_DIALOG (e_contact_editor_fullname);
215 	action_area = gtk_dialog_get_action_area (dialog);
216 	content_area = gtk_dialog_get_content_area (dialog);
217 
218 	gtk_widget_realize (GTK_WIDGET (e_contact_editor_fullname));
219 	gtk_container_set_border_width (GTK_CONTAINER (action_area), 12);
220 	gtk_container_set_border_width (GTK_CONTAINER (content_area), 0);
221 
222 	gtk_dialog_add_buttons (
223 		dialog,
224 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
225 		GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
226 
227 	gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
228 
229 	e_contact_editor_fullname->name = NULL;
230 
231 	builder = gtk_builder_new ();
232 	e_load_ui_builder_definition (builder, "fullname.ui");
233 
234 	e_contact_editor_fullname->builder = builder;
235 
236 	widget = e_builder_get_widget (builder, "dialog-checkfullname");
237 	title = gtk_window_get_title (GTK_WINDOW (widget));
238 	gtk_window_set_title (GTK_WINDOW (e_contact_editor_fullname), title);
239 
240 	widget = e_builder_get_widget (builder, "table-checkfullname");
241 	parent = gtk_widget_get_parent (widget);
242 	g_object_ref (widget);
243 	gtk_container_remove (GTK_CONTAINER (parent), widget);
244 	gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);
245 	g_object_unref (widget);
246 
247 	gtk_window_set_icon_name (
248 		GTK_WINDOW (e_contact_editor_fullname), "contact-new");
249 }
250 
251 GtkWidget *
252 e_contact_editor_fullname_new (const EContactName *name)
253 {
254 	GtkWidget *widget = g_object_new (E_TYPE_CONTACT_EDITOR_FULLNAME, NULL);
255 
256 	g_object_set (
257 		widget,
258 		"name", name,
259 		NULL);
260 	return widget;
261 }
262 
263 static void
264 fill_in_field (EContactEditorFullname *editor,
265                const gchar *field,
266                const gchar *string)
267 {
268 	GtkWidget *widget = e_builder_get_widget (editor->builder, field);
269 	GtkEntry *entry = NULL;
270 
271 	if (GTK_IS_ENTRY (widget))
272 		entry = GTK_ENTRY (widget);
273 	else if (GTK_IS_COMBO_BOX (widget))
274 		entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget)));
275 
276 	if (entry) {
277 		if (string)
278 			gtk_entry_set_text (entry, string);
279 		else
280 			gtk_entry_set_text (entry, "");
281 	}
282 }
283 
284 static void
285 fill_in_info (EContactEditorFullname *editor)
286 {
287 	EContactName *name = editor->name;
288 	if (name) {
289 		fill_in_field (editor, "comboentry-title",  name->prefixes);
290 		fill_in_field (editor, "entry-first",  name->given);
291 		fill_in_field (editor, "entry-middle", name->additional);
292 		fill_in_field (editor, "entry-last",   name->family);
293 		fill_in_field (editor, "comboentry-suffix", name->suffixes);
294 	}
295 }
296 
297 static gchar *
298 extract_field (EContactEditorFullname *editor,
299                const gchar *field)
300 {
301 	GtkWidget *widget = e_builder_get_widget (editor->builder, field);
302 	GtkEntry *entry = NULL;
303 
304 	if (GTK_IS_ENTRY (widget))
305 		entry = GTK_ENTRY (widget);
306 	else if (GTK_IS_COMBO_BOX (widget))
307 		entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget)));
308 
309 	if (entry)
310 		return g_strdup (gtk_entry_get_text (entry));
311 	else
312 		return NULL;
313 }
314 
315 static void
316 extract_info (EContactEditorFullname *editor)
317 {
318 	EContactName *name = editor->name;
319 	if (!name) {
320 		name = e_contact_name_new ();
321 		editor->name = name;
322 	}
323 
324 	name->prefixes   = extract_field (editor, "comboentry-title");
325 	name->given      = extract_field (editor, "entry-first");
326 	name->additional = extract_field (editor, "entry-middle");
327 	name->family     = extract_field (editor, "entry-last");
328 	name->suffixes   = extract_field (editor, "comboentry-suffix");
329 }