Fixed some issues with special characters and adding contact to address book.
[jenirok] / src / common / contactmanager.cpp
index 1289f1a..f9047b5 100644 (file)
  *
  */
 
-#include <QDebug>
+#include <QtCore/QDebug>
 #include "contactmanager.h"
 
-namespace
-{
-    const int COUNTRY_CODES[] = {358, 45, 46, 47};
-    const int NUM_OF_CODES = 4;
-}
 
 ContactManager::ContactManager(): book_(0)
 {
@@ -31,6 +26,10 @@ ContactManager::ContactManager(): book_(0)
 
 ContactManager::~ContactManager()
 {
+    if(book_)
+    {
+        g_object_unref(book_);
+    }
 }
 
 bool ContactManager::numberExists(QString const& number)
@@ -43,8 +42,8 @@ bool ContactManager::numberExists(QString const& number)
     EBookQuery* query;
     GList *g_contacts;
 
-    QString copy(number);
-    QString clean = removeCountryCode(copy);
+    // Just check the last 7 digits
+    QString clean = number.right(7);
 
     query = e_book_query_any_field_contains(clean.toLatin1());
 
@@ -54,9 +53,10 @@ bool ContactManager::numberExists(QString const& number)
         return false;
     }
 
+    e_book_query_unref(query);
+
     if (g_contacts == 0)
     {
-        qDebug() << "no contacts";
         return false;
     }
 
@@ -73,38 +73,77 @@ bool ContactManager::addContact(Contact const& contact)
 
     EContact* newContact = e_contact_new();
     GError* error = NULL;
-    //EContactAddress* addr = new EContactAddress;
+    EContactAddress* addr = NULL;
+
+    char* firstname = contact.name.firstname.toUtf8().data();
+    char* surname = contact.name.surname.toUtf8().data();
+    e_contact_set(newContact, E_CONTACT_GIVEN_NAME, (gpointer)firstname);
+    e_contact_set(newContact, E_CONTACT_FAMILY_NAME, (gpointer)surname);
 
-    if(!contact.name.isEmpty())
+    if(!contact.city.isEmpty() || !contact.street.isEmpty())
     {
-        char* name = contact.name.toLatin1().data();
-        e_contact_set(newContact, E_CONTACT_FULL_NAME, (gpointer)name);
+        addr = g_new0 (EContactAddress, 1);
+        addr->address_format = g_strdup("");
+        addr->po = g_strdup("");
+        addr->ext = g_strdup("");
+        addr->region = g_strdup("");
+        addr->code = g_strdup(contact.zipCode.toUtf8().data());
+        addr->country = g_strdup(contact.country.toUtf8().data());
+        addr->street = g_strdup(contact.street.toUtf8().data());
+        addr->locality = g_strdup(contact.city.toUtf8().data());
+        e_contact_set(newContact, E_CONTACT_ADDRESS_OTHER, (gpointer)addr);
     }
 
-    // Doesn't work for some reason
-    /*if(!contact.city.isEmpty() || !contact.street.isEmpty())
-       {
-               addr->street = contact.street.toLatin1().data();
-               addr->locality = contact.city.toLatin1().data();
-               e_contact_set(newContact, E_CONTACT_ADDRESS_HOME, (gpointer)addr);
-       }*/
-
     if(!contact.number.isEmpty())
     {
-        char* number = contact.number.toLatin1().data();
-        e_contact_set(newContact, E_CONTACT_PHONE_HOME, (gpointer)number);
+        char* number = contact.number.toUtf8().data();
+        e_contact_set(newContact, E_CONTACT_PHONE_OTHER, (gpointer)number);
     }
 
+    bool ret = true;
+
     if(!e_book_add_contact(book_, newContact, &error))
     {
-        qDebug() << "Couldn't add contact: %s" <<  error->message;
+        qDebug() << "Couldn't add contact: " <<  error->message;
         g_error_free(error);
-        return false;
+        ret = false;
     }
 
-    return true;
+    if(addr)
+    {
+        e_contact_address_free(addr);
+    }
+
+    return ret;
 }
 
+void ContactManager::stringToName(QString const& strname, ContactManager::Name& name)
+{
+    EContactName* ename = e_contact_name_from_string(strname.toUtf8().data());
+
+    if(ename)
+    {
+        QString additional = QString::fromUtf8(ename->additional);
+
+        if(additional.isEmpty())
+        {
+            name.firstname = QString::fromUtf8(ename->given);
+            name.surname = QString::fromUtf8(ename->family);
+        }
+        else
+        {
+            name.surname = strname;
+        }
+
+        e_contact_name_free(ename);
+    }
+    else
+    {
+        name.surname = strname;
+    }
+}
+
+
 bool ContactManager::load()
 {
     if(book_)
@@ -112,12 +151,13 @@ bool ContactManager::load()
         return true;
     }
 
-    GError* error = NULL;
+    GError *error;
+    error = 0;
     book_ = e_book_new_system_addressbook(&error);
 
     if (!book_)
     {
-        qDebug() << "Couldn't open addressbook: %s" <<  error->message;
+        qDebug() << "Couldn't open addressbook: %s" << error->message;
         g_error_free(error);
         return false;
     }
@@ -134,43 +174,3 @@ bool ContactManager::load()
 
 }
 
-QString& ContactManager::removeCountryCode(QString& number)
-{
-    if(number.isEmpty())
-    {
-        return number;
-    }
-
-    if(number.at(0) == '0')
-    {
-        return number.remove(0, 1);
-    }
-    else if(number.at(0) != '+')
-    {
-        return number;
-    }
-
-    static QRegExp countryCodeCleaner;
-    static bool countryCodeCleanerLoaded = false;
-
-    if(!countryCodeCleanerLoaded)
-    {
-        QString match = "";
-
-        for(int i = 0; i < NUM_OF_CODES; i++)
-        {
-            if(i > 0)
-            {
-                match += "|";
-            }
-
-            match += "\\+" + QString::number(COUNTRY_CODES[i]);
-        }
-
-        countryCodeCleaner = QRegExp("^(" + match + ")");
-        countryCodeCleanerLoaded = true;
-    }
-
-    return number.replace(countryCodeCleaner, "");
-}
-