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"
23 ContactManager::ContactManager(): book_(0)
27 ContactManager::~ContactManager()
31 g_object_unref(book_);
35 bool ContactManager::numberExists(QString const& number)
45 // Just check the last 7 digits
46 QString clean = number.right(7);
48 query = e_book_query_any_field_contains(clean.toLatin1());
50 if (!e_book_get_contacts (book_, query, &g_contacts, NULL))
52 qDebug() << "Couldn't get query results.\n";
56 e_book_query_unref(query);
67 bool ContactManager::addContact(Contact const& contact)
74 EContact* newContact = e_contact_new();
76 EContactAddress* addr = NULL;
78 if(!contact.name.isEmpty())
80 char* name = contact.name.toUtf8().data();
81 e_contact_set(newContact, E_CONTACT_FULL_NAME, (gpointer)name);
84 if(!contact.city.isEmpty() || !contact.street.isEmpty())
86 addr = g_new0 (EContactAddress, 1);
87 addr->address_format = g_strdup("");
88 addr->po = g_strdup("");
89 addr->ext = g_strdup("");
90 addr->region = g_strdup("");
91 addr->code = g_strdup(contact.zipCode.toUtf8().data());
92 addr->country = g_strdup(contact.country.toUtf8().data());
93 addr->street = g_strdup(contact.street.toUtf8().data());
94 addr->locality = g_strdup(contact.city.toUtf8().data());
95 e_contact_set(newContact, E_CONTACT_ADDRESS_OTHER, (gpointer)addr);
98 if(!contact.number.isEmpty())
100 char* number = contact.number.toUtf8().data();
101 e_contact_set(newContact, E_CONTACT_PHONE_OTHER, (gpointer)number);
106 if(!e_book_add_contact(book_, newContact, &error))
108 qDebug() << "Couldn't add contact: " << error->message;
115 e_contact_address_free(addr);
121 bool ContactManager::load()
130 book_ = e_book_new_system_addressbook(&error);
134 qDebug() << "Couldn't open addressbook: %s" << error->message;
139 /* Open connection to the address book */
140 if (!e_book_open(book_, FALSE, &error))
142 qDebug() << "Couldn't open addressbook: %s" << error->message;