Some connection handling improvements made for daemon. Changelog updated to newest...
[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 namespace
23 {
24     const int COUNTRY_CODES[] = {358, 45, 46, 47, 354};
25     const int NUM_OF_CODES = 5;
26 }
27
28 ContactManager::ContactManager(): book_(0)
29 {
30 }
31
32 ContactManager::~ContactManager()
33 {
34     if(book_)
35     {
36         g_object_unref(book_);
37     }
38 }
39
40 bool ContactManager::numberExists(QString const& number)
41 {
42     if(!load())
43     {
44         return false;
45     }
46
47     EBookQuery* query;
48     GList *g_contacts;
49
50     QString copy(number);
51     QString clean = removeCountryCode(copy);
52
53     query = e_book_query_any_field_contains(clean.toLatin1());
54
55     if (!e_book_get_contacts (book_, query, &g_contacts, NULL))
56     {
57         qDebug() << "Couldn't get query results.\n";
58         return false;
59     }
60
61     e_book_query_unref(query);
62
63     if (g_contacts == 0)
64     {
65         return false;
66     }
67
68     return true;
69
70 }
71
72 bool ContactManager::addContact(Contact const& contact)
73 {
74     if(!load())
75     {
76         return false;
77     }
78
79     EContact* newContact = e_contact_new();
80     GError* error = NULL;
81     //EContactAddress* addr = new EContactAddress;
82
83     if(!contact.name.isEmpty())
84     {
85         char* name = contact.name.toLatin1().data();
86         e_contact_set(newContact, E_CONTACT_FULL_NAME, (gpointer)name);
87     }
88
89     // Doesn't work for some reason
90     /*if(!contact.city.isEmpty() || !contact.street.isEmpty())
91         {
92                 addr->street = contact.street.toLatin1().data();
93                 addr->locality = contact.city.toLatin1().data();
94                 e_contact_set(newContact, E_CONTACT_ADDRESS_HOME, (gpointer)addr);
95         }*/
96
97     if(!contact.number.isEmpty())
98     {
99         char* number = contact.number.toLatin1().data();
100         e_contact_set(newContact, E_CONTACT_PHONE_HOME, (gpointer)number);
101     }
102
103     if(!e_book_add_contact(book_, newContact, &error))
104     {
105         qDebug() << "Couldn't add contact: %s" <<  error->message;
106         g_error_free(error);
107         return false;
108     }
109
110     return true;
111 }
112
113 bool ContactManager::load()
114 {
115     if(book_)
116     {
117         return true;
118     }
119
120     GError *error;
121     error = 0;
122     book_ = e_book_new_system_addressbook(&error);
123
124     if (!book_)
125     {
126         qDebug() << "Couldn't open addressbook: %s" << error->message;
127         g_error_free(error);
128         return false;
129     }
130
131     /* Open connection to the address book */
132     if (!e_book_open(book_, FALSE, &error))
133     {
134         qDebug() << "Couldn't open addressbook: %s" << error->message;
135         g_error_free(error);
136         return false;
137     }
138
139     return true;
140
141 }
142
143 QString& ContactManager::removeCountryCode(QString& number)
144 {
145     if(number.isEmpty())
146     {
147         return number;
148     }
149
150     if(number.at(0) == '0')
151     {
152         return number.remove(0, 1);
153     }
154     else if(number.at(0) != '+')
155     {
156         return number;
157     }
158
159     static QRegExp countryCodeCleaner;
160     static bool countryCodeCleanerLoaded = false;
161
162     if(!countryCodeCleanerLoaded)
163     {
164         QString match = "";
165
166         for(int i = 0; i < NUM_OF_CODES; i++)
167         {
168             if(i > 0)
169             {
170                 match += "|";
171             }
172
173             match += "\\+" + QString::number(COUNTRY_CODES[i]);
174         }
175
176         countryCodeCleaner = QRegExp("^(" + match + ")");
177         countryCodeCleanerLoaded = true;
178     }
179
180     return number.replace(countryCodeCleaner, "");
181 }
182