2 * This file is part of Jenirok.
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.
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.
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/>.
19 #include <QtCore/QDebug>
20 #include "contactmanager.h"
24 const int COUNTRY_CODES[] = {358, 45, 46, 47, 354};
25 const int NUM_OF_CODES = 5;
28 ContactManager::ContactManager(): book_(0)
32 ContactManager::~ContactManager()
36 g_object_unref(book_);
40 bool ContactManager::numberExists(QString const& number)
51 QString clean = removeCountryCode(copy);
53 query = e_book_query_any_field_contains(clean.toLatin1());
55 if (!e_book_get_contacts (book_, query, &g_contacts, NULL))
57 qDebug() << "Couldn't get query results.\n";
61 e_book_query_unref(query);
72 bool ContactManager::addContact(Contact const& contact)
79 EContact* newContact = e_contact_new();
81 //EContactAddress* addr = new EContactAddress;
83 if(!contact.name.isEmpty())
85 char* name = contact.name.toLatin1().data();
86 e_contact_set(newContact, E_CONTACT_FULL_NAME, (gpointer)name);
89 // Doesn't work for some reason
90 /*if(!contact.city.isEmpty() || !contact.street.isEmpty())
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);
97 if(!contact.number.isEmpty())
99 char* number = contact.number.toLatin1().data();
100 e_contact_set(newContact, E_CONTACT_PHONE_HOME, (gpointer)number);
103 if(!e_book_add_contact(book_, newContact, &error))
105 qDebug() << "Couldn't add contact: %s" << error->message;
113 bool ContactManager::load()
122 book_ = e_book_new_system_addressbook(&error);
126 qDebug() << "Couldn't open addressbook: %s" << error->message;
131 /* Open connection to the address book */
132 if (!e_book_open(book_, FALSE, &error))
134 qDebug() << "Couldn't open addressbook: %s" << error->message;
143 QString& ContactManager::removeCountryCode(QString& number)
150 if(number.at(0) == '0')
152 return number.remove(0, 1);
154 else if(number.at(0) != '+')
159 static QRegExp countryCodeCleaner;
160 static bool countryCodeCleanerLoaded = false;
162 if(!countryCodeCleanerLoaded)
166 for(int i = 0; i < NUM_OF_CODES; i++)
173 match += "\\+" + QString::number(COUNTRY_CODES[i]);
176 countryCodeCleaner = QRegExp("^(" + match + ")");
177 countryCodeCleanerLoaded = true;
180 return number.replace(countryCodeCleaner, "");