1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2013 Intel Corporation
4  *
5  * This library is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Tristan Van Berkom <tristanvb@openismus.com>
18  */
19 
20 #include <libebook/libebook.h>
21 
22 #include "cursor-slot.h"
23 
24 struct _CursorSlotPrivate {
25 	/* Screen widgets */
26 	GtkWidget *area;
27 	GtkLabel  *name_label;
28 	GtkLabel  *emails_label;
29 	GtkLabel  *telephones_label;
30 };
31 
32 G_DEFINE_TYPE_WITH_PRIVATE (CursorSlot, cursor_slot, GTK_TYPE_GRID);
33 
34 /************************************************************************
35  *                          GObjectClass                                *
36  ************************************************************************/
37 static void
cursor_slot_class_init(CursorSlotClass * klass)38 cursor_slot_class_init (CursorSlotClass *klass)
39 {
40 	GtkWidgetClass *widget_class;
41 
42 	/* Bind to template */
43 	widget_class = GTK_WIDGET_CLASS (klass);
44 	gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/evolution/cursor-example/cursor-slot.ui");
45 	gtk_widget_class_bind_template_child_private (widget_class, CursorSlot, area);
46 	gtk_widget_class_bind_template_child_private (widget_class, CursorSlot, name_label);
47 	gtk_widget_class_bind_template_child_private (widget_class, CursorSlot, emails_label);
48 	gtk_widget_class_bind_template_child_private (widget_class, CursorSlot, telephones_label);
49 }
50 
51 static void
cursor_slot_init(CursorSlot * slot)52 cursor_slot_init (CursorSlot *slot)
53 {
54 	slot->priv = cursor_slot_get_instance_private (slot);
55 
56 	gtk_widget_init_template (GTK_WIDGET (slot));
57 }
58 
59 /************************************************************************
60  *                                API                                   *
61  ************************************************************************/
62 GtkWidget *
cursor_slot_new(EContact * contact)63 cursor_slot_new (EContact *contact)
64 {
65   CursorSlot *slot;
66 
67   slot = g_object_new (CURSOR_TYPE_SLOT, NULL);
68 
69   cursor_slot_set_from_contact (slot, contact);
70 
71   return (GtkWidget *) slot;
72 }
73 
74 static gchar *
make_string_from_list(EContact * contact,EContactField field)75 make_string_from_list (EContact *contact,
76                        EContactField field)
77 {
78 	GList *values, *l;
79 	GString *string;
80 
81 	string = g_string_new ("<span size=\"x-small\">");
82 	values = e_contact_get (contact, field);
83 
84 	for (l = values; l; l = l->next) {
85 		gchar *value = (gchar *) l->data;
86 
87 		if (l->prev != NULL)
88 			g_string_append (string, ", ");
89 
90 		g_string_append (string, value);
91 	}
92 
93 	if (!values)
94 		g_string_append (string, " - none - ");
95 
96 	e_contact_attr_list_free (values);
97 	g_string_append (string, "</span>");
98 
99 	return g_string_free (string, FALSE);
100 }
101 
102 void
cursor_slot_set_from_contact(CursorSlot * slot,EContact * contact)103 cursor_slot_set_from_contact (CursorSlot *slot,
104                               EContact *contact)
105 {
106 	CursorSlotPrivate *priv;
107 	const gchar *family_name, *given_name;
108 	gchar *str;
109 
110 	g_return_if_fail (CURSOR_IS_SLOT (slot));
111 	g_return_if_fail (contact == NULL || E_IS_CONTACT (contact));
112 
113 	priv = slot->priv;
114 
115 	if (!contact) {
116 		gtk_widget_hide (priv->area);
117 		return;
118 	}
119 
120 	family_name = (const gchar *) e_contact_get_const (contact, E_CONTACT_FAMILY_NAME);
121 	given_name = (const gchar *) e_contact_get_const (contact, E_CONTACT_GIVEN_NAME);
122 
123 	str = g_strdup_printf ("%s, %s", family_name, given_name);
124 	gtk_label_set_text (priv->name_label, str);
125 	g_free (str);
126 
127 	str = make_string_from_list (contact, E_CONTACT_EMAIL);
128 	gtk_label_set_markup (priv->emails_label, str);
129 	g_free (str);
130 
131 	str = make_string_from_list (contact, E_CONTACT_TEL);
132 	gtk_label_set_markup (priv->telephones_label, str);
133 	g_free (str);
134 
135 	gtk_widget_show (priv->area);
136 }
137 
138