One more fix to connection handling.
[jenirok] / src / common / contactmanager.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QDebug>
20 #include "contactmanager.h"
21
22
23 ContactManager::ContactManager(): book_(0)
24 {
25 }
26
27 ContactManager::~ContactManager()
28 {
29     if(book_)
30     {
31         g_object_unref(book_);
32     }
33 }
34
35 bool ContactManager::numberExists(QString const& number)
36 {
37     if(!load())
38     {
39         return false;
40     }
41
42     EBookQuery* query;
43     GList *g_contacts;
44
45     // Just check the last 7 digits
46     QString clean = number.right(7);
47
48     query = e_book_query_any_field_contains(clean.toLatin1());
49
50     if (!e_book_get_contacts (book_, query, &g_contacts, NULL))
51     {
52         qDebug() << "Couldn't get query results.\n";
53         return false;
54     }
55
56     e_book_query_unref(query);
57
58     if (g_contacts == 0)
59     {
60         return false;
61     }
62
63     return true;
64
65 }
66
67 bool ContactManager::addContact(Contact const& contact)
68 {
69     if(!load())
70     {
71         return false;
72     }
73
74     EContact* newContact = e_contact_new();
75     GError* error = NULL;
76     EContactAddress* addr = NULL;
77
78     char* firstname = contact.name.firstname.toUtf8().data();
79     char* surname = contact.name.surname.toUtf8().data();
80     e_contact_set(newContact, E_CONTACT_GIVEN_NAME, (gpointer)firstname);
81     e_contact_set(newContact, E_CONTACT_FAMILY_NAME, (gpointer)surname);
82
83     if(!contact.city.isEmpty() || !contact.street.isEmpty())
84     {
85         addr = g_new0 (EContactAddress, 1);
86         addr->address_format = g_strdup("");
87         addr->po = g_strdup("");
88         addr->ext = g_strdup("");
89         addr->region = g_strdup("");
90         addr->code = g_strdup(contact.zipCode.toUtf8().data());
91         addr->country = g_strdup(contact.country.toUtf8().data());
92         addr->street = g_strdup(contact.street.toUtf8().data());
93         addr->locality = g_strdup(contact.city.toUtf8().data());
94         e_contact_set(newContact, E_CONTACT_ADDRESS_OTHER, (gpointer)addr);
95     }
96
97     if(!contact.number.isEmpty())
98     {
99         char* number = contact.number.toUtf8().data();
100         e_contact_set(newContact, E_CONTACT_PHONE_OTHER, (gpointer)number);
101     }
102
103     bool ret = true;
104
105     if(!e_book_add_contact(book_, newContact, &error))
106     {
107         qDebug() << "Couldn't add contact: " <<  error->message;
108         g_error_free(error);
109         ret = false;
110     }
111
112     if(addr)
113     {
114         e_contact_address_free(addr);
115     }
116
117     return ret;
118 }
119
120 void ContactManager::stringToName(QString const& strname, ContactManager::Name& name)
121 {
122     EContactName* ename = e_contact_name_from_string(strname.toUtf8().data());
123
124     if(ename)
125     {
126         QString additional = QString(ename->additional);
127
128         if(additional.isEmpty())
129         {
130             name.firstname = QString(ename->given);
131             name.surname = QString(ename->family);
132         }
133         else
134         {
135             name.surname = strname;
136         }
137
138         e_contact_name_free(ename);
139     }
140     else
141     {
142         name.surname = strname;
143     }
144 }
145
146
147 bool ContactManager::load()
148 {
149     if(book_)
150     {
151         return true;
152     }
153
154     GError *error;
155     error = 0;
156     book_ = e_book_new_system_addressbook(&error);
157
158     if (!book_)
159     {
160         qDebug() << "Couldn't open addressbook: %s" << error->message;
161         g_error_free(error);
162         return false;
163     }
164
165     /* Open connection to the address book */
166     if (!e_book_open(book_, FALSE, &error))
167     {
168         qDebug() << "Couldn't open addressbook: %s" << error->message;
169         g_error_free(error);
170         return false;
171     }
172
173     return true;
174
175 }
176