11dac3dd26a846ae3accd49baafc93303fd87903
[modest] / src / maemo / modest-address-book.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /* modest-address-book.c */
31
32 #include <modest-address-book.h>
33 #include <libebook/e-book.h>
34 #include <libebook/e-book-view.h>
35 #include <libosso-abook/osso-abook.h>
36
37 static OssoABookContactModel *contact_model =  NULL;
38 static EBook *book = NULL;
39 static EBookView * book_view = NULL;
40
41
42 static void
43 get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
44 {
45         if (status != E_BOOK_ERROR_OK) {
46                 g_object_unref (book);
47                 book = NULL;
48                 return;
49         }
50         book_view = bookview;
51
52         if (contact_model)
53                 osso_abook_tree_model_set_book_view (OSSO_ABOOK_TREE_MODEL (contact_model),
54                                                      book_view);
55
56         e_book_view_start (book_view);
57 }
58
59 static void
60 book_open_cb (EBook *view, EBookStatus status, gpointer data)
61 {
62         EBookQuery *query = NULL;
63
64         if (status != E_BOOK_ERROR_OK) {
65                 g_object_unref (book);
66                 book = NULL;
67                 return;
68         }
69         query = e_book_query_any_field_contains ("");
70         e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
71         e_book_query_unref (query);
72 }
73
74 static gboolean 
75 open_addressbook ()
76 {
77         book = e_book_new_system_addressbook (NULL);
78         if (!book)
79                 return FALSE;
80
81         e_book_async_open (book, FALSE, book_open_cb, NULL);
82                 
83 }
84
85 void
86 modest_address_book_add_address (const gchar *address)
87 {
88         OssoABookAccount *account = NULL;
89         GtkWidget *dialog = NULL;
90
91         contact_model = osso_abook_contact_model_new ();
92         if (!open_addressbook ()) {
93                 if (contact_model) {
94                         g_object_unref (contact_model);
95                         contact_model = NULL;
96                 }
97                 return;
98         }
99         
100         account = osso_abook_account_get (EVC_EMAIL, NULL, address);
101         dialog = osso_abook_add_to_contacts_dialog_new (contact_model, account);
102         g_object_unref (account);
103         gtk_dialog_run (GTK_DIALOG (dialog));
104
105         if (contact_model) {
106                 g_object_unref (contact_model);
107                 contact_model = NULL;
108         }
109
110         gtk_widget_destroy (dialog);
111
112 }
113
114 gchar *
115 modest_address_book_select_addresses (void)
116 {
117         g_message (__FUNCTION__);
118         return NULL;
119 }
120