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