2 * Copyright (C) 2011, Jamie Thompson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This program 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 GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; If not, see
16 * <http://www.gnu.org/licenses/>.
20 #include <glib/glist.h>
22 #include <libosso-abook/osso-abook.h>
23 #include <libebook/e-book.h>
24 #include <evolution-data-server-1.4/libebook/e-vcard.h>
25 #include <evolution-data-server-1.4/libebook/e-contact.h>
27 #include "NumberToNameLookup.h"
33 NumberToNameLookup::NumberToNameLookup()
38 osso_context_t *osso_context(osso_initialize ("qwerkisync", "0.1", true, NULL));
39 if (NULL == osso_context)
40 throw std::runtime_error("Error initializing osso");
45 if (!osso_abook_init_with_name("qwerkisync", osso_context))
46 throw std::runtime_error("Error initializing libosso-abook");
48 EBook *ebook(e_book_new_system_addressbook (&gError));
51 QString error(QString("Error opening system address book: %1").arg(gError->message));
53 throw std::runtime_error(error.toStdString());
56 if (!e_book_open (ebook, true, &gError))
58 QString error(QString("Error opening system address book: %1").arg(gError->message));
60 throw std::runtime_error(error.toStdString());
63 EBookQuery *query(e_book_query_any_field_contains (""));
67 GList *contacts(NULL);
68 if (!e_book_get_contacts (ebook, query, &contacts, &gError))
70 QString error(QString("Error getting contacts: %1").arg(gError->message));
72 throw std::runtime_error(error.toStdString());
75 m_ContactDetails.reserve(g_list_length(contacts));
77 qDebug() << g_list_length(contacts) << " contacts.";
79 // Loop over each contact...
80 for (GList *currentContact(contacts); NULL != currentContact; currentContact = currentContact->next)
82 // Now work out the current name for the contact and add it to the lookup
83 EContactName *contactName = (EContactName*)e_contact_get((EContact*)currentContact->data, E_CONTACT_NAME);
84 char *completeName(e_contact_name_to_string(contactName));
85 //qDebug() << completeName;
87 // Looking at each number they have...
88 GList * attrs(e_contact_get_attributes(E_CONTACT(currentContact->data), E_CONTACT_FIRST_PHONE_ID));
89 for (GList *attr(attrs); NULL != attr; attr = attr->next)
91 for(GList *val(e_vcard_attribute_get_values((EVCardAttribute *)attr->data)); NULL != val; val = val->next)
93 const gchar *number((char*)val->data);
95 continue; // Ignore missing numbers
97 const char *remote_ebook_uid_string((const char *)e_contact_get_const (E_CONTACT (currentContact->data), E_CONTACT_UID));
98 if (NULL != remote_ebook_uid_string)
100 // This is what we really need for new events
101 uint remote_ebook_uid(QString(remote_ebook_uid_string).toUInt());
103 m_ContactDetails.insert(QString(number), QPair<uint, QString>(remote_ebook_uid, QString(completeName)));
105 //qDebug() << "Number: " << number << ", ID: " << remote_ebook_uid << ", completeName: " << completeName;
111 g_free(completeName);
112 e_contact_name_free(contactName);
114 // I hate C-style APIs.
115 g_object_unref(currentContact->data);
118 // I *really* hate C-style APIs.
119 g_list_free(contacts);
123 e_book_query_unref(query);
127 // Will wrap e_book_* in a class some other time (oh for 'finally' in C++).
128 e_book_query_unref(query);
132 osso_deinitialize (osso_context);
136 // Will wrap osso_* in a class some other time (oh for 'finally' in C++).
137 osso_deinitialize (osso_context);
139 // foreach(QString key, ContactDetails.keys())
141 // qDebug() << key << ", " << ContactDetails.value(key).first << ", " << ContactDetails.value(key).second << endl;