dbc8916f11bc6c28f74ae01259c1d1a8d330d083
[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 <QtCore/QRegExp>
21 #include "contactmanager.h"
22
23
24 ContactManager::ContactManager(): book_(0)
25 {
26 }
27
28 ContactManager::~ContactManager()
29 {
30     if(book_)
31     {
32         g_object_unref(book_);
33     }
34 }
35
36 bool ContactManager::numberExists(QString const& number)
37 {
38     if(!load())
39     {
40         return false;
41     }
42
43     EBookQuery* query;
44     GList *g_contacts;
45
46     // Just check the last 7 digits
47     QString clean = number.right(7);
48
49     query = e_book_query_any_field_contains(clean.toLatin1());
50
51     if (!e_book_get_contacts (book_, query, &g_contacts, NULL))
52     {
53         qDebug() << "Couldn't get query results.\n";
54         return false;
55     }
56
57     e_book_query_unref(query);
58
59     if (g_contacts == 0)
60     {
61         return false;
62     }
63
64     return true;
65
66 }
67
68 bool ContactManager::addContact(Contact const& contact)
69 {
70     if(!load())
71     {
72         return false;
73     }
74
75     EContact* newContact = e_contact_new();
76     GError* error = NULL;
77     EContactAddress* addr = NULL;
78
79     char* firstname = contact.name.firstname.toUtf8().data();
80     char* surname = contact.name.surname.toUtf8().data();
81
82     e_contact_set(newContact, E_CONTACT_GIVEN_NAME, (gpointer)firstname);
83     e_contact_set(newContact, E_CONTACT_FAMILY_NAME, (gpointer)surname);
84
85     if(!contact.city.isEmpty() || !contact.street.isEmpty())
86     {
87         addr = g_new0 (EContactAddress, 1);
88         addr->address_format = g_strdup("");
89         addr->po = g_strdup("");
90         addr->ext = g_strdup("");
91         addr->region = g_strdup("");
92         addr->code = g_strdup(contact.zipCode.toUtf8().data());
93         addr->country = g_strdup(contact.country.toUtf8().data());
94         addr->street = g_strdup(contact.street.toUtf8().data());
95         addr->locality = g_strdup(contact.city.toUtf8().data());
96         e_contact_set(newContact, E_CONTACT_ADDRESS_OTHER, (gpointer)addr);
97     }
98
99     if(!contact.number.isEmpty())
100     {
101         char* number = contact.number.toUtf8().data();
102         e_contact_set(newContact, E_CONTACT_PHONE_OTHER, (gpointer)number);
103     }
104
105     bool ret = true;
106
107     if(!e_book_add_contact(book_, newContact, &error))
108     {
109         qDebug() << "Couldn't add contact: " <<  error->message;
110         g_error_free(error);
111         ret = false;
112     }
113
114     if(addr)
115     {
116         e_contact_address_free(addr);
117     }
118
119     return ret;
120 }
121
122 void ContactManager::stringToName(QString const& strname, ContactManager::Name& name)
123 {
124     EContactName* ename = e_contact_name_from_string(strname.toUtf8().data());
125
126     if(ename)
127     {
128         static QRegExp check("([A-Z]+)");
129         QString additional = QString::fromUtf8(ename->additional);
130         QString firstname = QString::fromUtf8(ename->given);
131         QString surname = QString::fromUtf8(ename->family);
132
133         if(additional.isEmpty() && check.indexIn(firstname, 1) == -1 && check.indexIn(surname, 1) == -1 && firstname != surname)
134         {
135             name.firstname = firstname;
136             name.surname = surname;
137         }
138         else
139         {
140             name.surname = strname;
141         }
142
143         e_contact_name_free(ename);
144     }
145     else
146     {
147         name.surname = strname;
148     }
149 }
150
151
152 bool ContactManager::load()
153 {
154     if(book_)
155     {
156         return true;
157     }
158
159     GError *error;
160     error = 0;
161     book_ = e_book_new_system_addressbook(&error);
162
163     if (!book_)
164     {
165         qDebug() << "Couldn't open addressbook: %s" << error->message;
166         g_error_free(error);
167         return false;
168     }
169
170     /* Open connection to the address book */
171     if (!e_book_open(book_, FALSE, &error))
172     {
173         qDebug() << "Couldn't open addressbook: %s" << error->message;
174         g_error_free(error);
175         return false;
176     }
177
178     return true;
179
180 }
181