Some memory usage improvements to daemon.
[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     e_book_query_unref(query);
58
59     if (g_contacts == 0)
60     {
61         qDebug() << "no contacts";
62         return false;
63     }
64
65     return true;
66
67 }
68
69 bool ContactManager::addContact(Contact const& contact)
70 {
71     if(!load())
72     {
73         return false;
74     }
75
76     EContact* newContact = e_contact_new();
77     GError* error = NULL;
78     //EContactAddress* addr = new EContactAddress;
79
80     if(!contact.name.isEmpty())
81     {
82         char* name = contact.name.toLatin1().data();
83         e_contact_set(newContact, E_CONTACT_FULL_NAME, (gpointer)name);
84     }
85
86     // Doesn't work for some reason
87     /*if(!contact.city.isEmpty() || !contact.street.isEmpty())
88         {
89                 addr->street = contact.street.toLatin1().data();
90                 addr->locality = contact.city.toLatin1().data();
91                 e_contact_set(newContact, E_CONTACT_ADDRESS_HOME, (gpointer)addr);
92         }*/
93
94     if(!contact.number.isEmpty())
95     {
96         char* number = contact.number.toLatin1().data();
97         e_contact_set(newContact, E_CONTACT_PHONE_HOME, (gpointer)number);
98     }
99
100     if(!e_book_add_contact(book_, newContact, &error))
101     {
102         qDebug() << "Couldn't add contact: %s" <<  error->message;
103         g_error_free(error);
104         return false;
105     }
106
107     return true;
108 }
109
110 bool ContactManager::load()
111 {
112     if(book_)
113     {
114         return true;
115     }
116
117     GError *error;
118     error = 0;
119     book_ = e_book_new_system_addressbook(&error);
120
121     if (!book_)
122     {
123         qDebug() << "Couldn't open addressbook: %s" << error->message;
124         g_error_free(error);
125         return false;
126     }
127
128     /* Open connection to the address book */
129     if (!e_book_open(book_, FALSE, &error))
130     {
131         qDebug() << "Couldn't open addressbook: %s" << error->message;
132         g_error_free(error);
133         return false;
134     }
135
136     return true;
137
138 }
139
140 QString& ContactManager::removeCountryCode(QString& number)
141 {
142     if(number.isEmpty())
143     {
144         return number;
145     }
146
147     if(number.at(0) == '0')
148     {
149         return number.remove(0, 1);
150     }
151     else if(number.at(0) != '+')
152     {
153         return number;
154     }
155
156     static QRegExp countryCodeCleaner;
157     static bool countryCodeCleanerLoaded = false;
158
159     if(!countryCodeCleanerLoaded)
160     {
161         QString match = "";
162
163         for(int i = 0; i < NUM_OF_CODES; i++)
164         {
165             if(i > 0)
166             {
167                 match += "|";
168             }
169
170             match += "\\+" + QString::number(COUNTRY_CODES[i]);
171         }
172
173         countryCodeCleaner = QRegExp("^(" + match + ")");
174         countryCodeCleanerLoaded = true;
175     }
176
177     return number.replace(countryCodeCleaner, "");
178 }
179