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 <QtCore/QRegExp>
21 #include "contactmanager.h"
24 ContactManager::ContactManager(): book_(0)
28 ContactManager::~ContactManager()
32 g_object_unref(book_);
36 bool ContactManager::numberExists(QString const& number)
46 // Just check the last 7 digits
47 QString clean = number.right(7);
49 query = e_book_query_any_field_contains(clean.toLatin1());
51 if (!e_book_get_contacts (book_, query, &g_contacts, NULL))
53 qDebug() << "Couldn't get query results.\n";
57 e_book_query_unref(query);
68 bool ContactManager::addContact(Contact const& contact)
75 EContact* newContact = e_contact_new();
77 EContactAddress* addr = NULL;
79 char* firstname = contact.name.firstname.toUtf8().data();
80 char* surname = contact.name.surname.toUtf8().data();
82 e_contact_set(newContact, E_CONTACT_GIVEN_NAME, (gpointer)firstname);
83 e_contact_set(newContact, E_CONTACT_FAMILY_NAME, (gpointer)surname);
85 if(!contact.city.isEmpty() || !contact.street.isEmpty())
87 addr = g_new0 (EContactAddress, 1);
88 addr->address_format = g_strdup("");
89 addr->po = g_strdup("");
90 addr->ext = g_strdup("");
91 addr->region = g_strdup("");
92 addr->code = g_strdup(contact.zipCode.toUtf8().data());
93 addr->country = g_strdup(contact.country.toUtf8().data());
94 addr->street = g_strdup(contact.street.toUtf8().data());
95 addr->locality = g_strdup(contact.city.toUtf8().data());
96 e_contact_set(newContact, E_CONTACT_ADDRESS_OTHER, (gpointer)addr);
99 if(!contact.number.isEmpty())
101 char* number = contact.number.toUtf8().data();
102 e_contact_set(newContact, E_CONTACT_PHONE_OTHER, (gpointer)number);
107 if(!e_book_add_contact(book_, newContact, &error))
109 qDebug() << "Couldn't add contact: " << error->message;
116 e_contact_address_free(addr);
122 void ContactManager::stringToName(QString const& strname, ContactManager::Name& name)
124 EContactName* ename = e_contact_name_from_string(strname.toUtf8().data());
128 static QRegExp check("([A-Z]+)");
129 QString additional = QString::fromUtf8(ename->additional);
130 QString firstname = QString::fromUtf8(ename->given);
131 QString surname = QString::fromUtf8(ename->family);
133 if(additional.isEmpty() && check.indexIn(firstname, 1) == -1 && check.indexIn(surname, 1) == -1)
135 name.firstname = firstname;
136 name.surname = surname;
140 name.surname = strname;
143 e_contact_name_free(ename);
147 name.surname = strname;
152 bool ContactManager::load()
161 book_ = e_book_new_system_addressbook(&error);
165 qDebug() << "Couldn't open addressbook: %s" << error->message;
170 /* Open connection to the address book */
171 if (!e_book_open(book_, FALSE, &error))
173 qDebug() << "Couldn't open addressbook: %s" << error->message;