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