Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / libedataserverui / test-contact-store.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* test-contact-store.c - Test program for EContactStore.
4  *
5  * Copyright (C) 2004 Novell, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * Author: Hans Petter Jansson <hpj@novell.com>
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include "e-contact-store.h"
29 #include <gtk/gtk.h>
30
31 static void
32 entry_changed (GtkWidget *entry, EContactStore *contact_store)
33 {
34         const gchar *text;
35         EBookQuery  *query;
36
37         text = gtk_entry_get_text (GTK_ENTRY (entry));
38
39         query = e_book_query_any_field_contains (text);
40         e_contact_store_set_query (contact_store, query);
41         e_book_query_unref (query);
42 }
43
44 static GtkTreeViewColumn *
45 create_text_column_for_field (EContactField field_id)
46 {
47         GtkTreeViewColumn *column;
48         GtkCellRenderer   *cell_renderer;
49
50         column = gtk_tree_view_column_new ();
51         cell_renderer = GTK_CELL_RENDERER (gtk_cell_renderer_text_new ());
52         gtk_tree_view_column_pack_start (column, cell_renderer, TRUE);
53         gtk_tree_view_column_set_resizable (column, TRUE);
54         gtk_tree_view_column_set_title (column, e_contact_pretty_name (field_id));
55         gtk_tree_view_column_add_attribute (column, cell_renderer, "text", field_id);
56         gtk_tree_view_column_set_sort_column_id (column, field_id);
57
58         return column;
59 }
60
61 static gint
62 start_test (const char *gconf_path)
63 {
64         EContactStore *contact_store;
65         GtkTreeModel *model_sort;
66         GtkWidget *scrolled_window;
67         GtkWidget *window;
68         GtkWidget *tree_view;
69         GtkWidget *box;
70         GtkWidget *entry;
71         GtkTreeViewColumn *column;
72         EBook *book;
73         EBookQuery *book_query;
74
75         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
76
77         box = gtk_vbox_new (FALSE, 2);
78         gtk_container_add (GTK_CONTAINER (window), box);
79
80         entry = gtk_entry_new ();
81         gtk_box_pack_start (GTK_BOX (box), entry, FALSE, TRUE, 0);
82
83         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
84         gtk_box_pack_start (GTK_BOX (box), scrolled_window, TRUE, TRUE, 0);
85
86         contact_store = e_contact_store_new ();
87         model_sort = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (contact_store));
88         tree_view = GTK_WIDGET (gtk_tree_view_new ());
89         gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), model_sort);
90
91         column = create_text_column_for_field (E_CONTACT_FILE_AS);
92         gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
93
94         column = create_text_column_for_field (E_CONTACT_FULL_NAME);
95         gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
96
97         column = create_text_column_for_field (E_CONTACT_EMAIL_1);
98         gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
99
100         gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
101
102         book = e_book_new_default_addressbook (NULL);
103         e_book_open (book, TRUE, NULL);
104         e_contact_store_add_book (contact_store, book);
105         g_object_unref (book);
106
107         book_query = e_book_query_any_field_contains ("");
108         e_contact_store_set_query (contact_store, book_query);
109         e_book_query_unref (book_query);
110
111         g_signal_connect (entry, "changed", G_CALLBACK (entry_changed), contact_store);
112
113         gtk_widget_show_all (window);
114
115         return FALSE;
116 }
117
118 int
119 main (int argc, char **argv)
120 {
121         const char *gconf_path;
122
123         gtk_init (&argc, &argv);
124
125         if (argc < 2)
126                 gconf_path = "/apps/evolution/addressbook/sources";
127         else
128                 gconf_path = argv [1];
129
130         g_idle_add ((GSourceFunc) start_test, (void *) gconf_path);
131
132         gtk_main ();
133
134         return 0;
135 }