3f8affee425ce3f3e53a0802438d5a75cb297fa1
[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 <QDebug>
20 #include "contactmanager.h"
21
22 namespace
23 {
24         const int COUNTRY_CODES[] = {358, 45, 46, 47};
25         const int NUM_OF_CODES = 4;
26 }
27
28 ContactManager::ContactManager(): book_(0)
29 {
30 }
31
32 ContactManager::~ContactManager()
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         QString copy(number);
47         QString clean = removeCountryCode(copy);
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         if (g_contacts == 0)
58         {
59                 qDebug() << "no contacts";
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 = new EContactAddress;
77
78         if(!contact.name.isEmpty())
79         {
80                 char* name = contact.name.toLatin1().data();
81                 e_contact_set(newContact, E_CONTACT_FULL_NAME, (gpointer)name);
82         }
83
84         // Doesn't work for some reason
85         /*if(!contact.city.isEmpty() || !contact.street.isEmpty())
86         {
87                 addr->street = contact.street.toLatin1().data();
88                 addr->locality = contact.city.toLatin1().data();
89                 e_contact_set(newContact, E_CONTACT_ADDRESS_HOME, (gpointer)addr);
90         }*/
91
92         if(!contact.number.isEmpty())
93         {
94                 char* number = contact.number.toLatin1().data();
95                 e_contact_set(newContact, E_CONTACT_PHONE_HOME, (gpointer)number);
96         }
97
98         if(!e_book_add_contact(book_, newContact, &error))
99         {
100                 qDebug() << "Couldn't add contact: %s" <<  error->message;
101                 g_error_free(error);
102                 return false;
103         }
104
105         return true;
106 }
107
108 bool ContactManager::load()
109 {
110         if(book_)
111         {
112                 return true;
113         }
114
115         GError* error = NULL;
116         book_ = e_book_new_system_addressbook(&error);
117
118         if (!book_)
119         {
120                 qDebug() << "Couldn't open addressbook: %s" <<  error->message;
121                 g_error_free(error);
122                 return false;
123         }
124
125         /* Open connection to the address book */
126         if (!e_book_open(book_, FALSE, &error))
127         {
128                 qDebug() << "Couldn't open addressbook: %s" << error->message;
129                 g_error_free(error);
130                 return false;
131         }
132
133         return true;
134
135 }
136
137 QString& ContactManager::removeCountryCode(QString& number)
138 {
139         if(number.isEmpty())
140         {
141                 return number;
142         }
143
144         if(number.at(0) == '0')
145         {
146                 return number.remove(0, 1);
147         }
148         else if(number.at(0) != '+')
149         {
150                 return number;
151         }
152
153         static QRegExp countryCodeCleaner;
154         static bool countryCodeCleanerLoaded = false;
155
156         if(!countryCodeCleanerLoaded)
157         {
158                 QString match = "";
159
160                 for(int i = 0; i < NUM_OF_CODES; i++)
161                 {
162                         if(i > 0)
163                         {
164                                 match += "|";
165                         }
166
167                         match += "\\+" + QString::number(COUNTRY_CODES[i]);
168                 }
169
170                 countryCodeCleaner = QRegExp("^(" + match + ")");
171                 countryCodeCleanerLoaded = true;
172         }
173
174         return number.replace(countryCodeCleaner, "");
175 }
176