TizenRefApp-4912 Track changing of contact's name changing 03/52203/10
authorOleksander Kostenko <o.kostenko@samsung.com>
Thu, 19 Nov 2015 17:28:31 +0000 (19:28 +0200)
committerOleksander Kostenko <o.kostenko@samsung.com>
Wed, 25 Nov 2015 08:38:56 +0000 (10:38 +0200)
Change-Id: Ieb89bacd50be7a538eae0ecb6104bc12ee8ef764
Signed-off-by: Oleksander Kostenko <o.kostenko@samsung.com>
src/Common/ContactManager/inc/ContactManager.h
src/Common/ContactManager/src/ContactManager.cpp
test/TC/ContactUtils.cpp
test/TC/ContactUtils.h
test/TC/TestContactManager.cpp

index fff13c7..0fb2c33 100644 (file)
 
 namespace Msg
 {
+    /*
+     *@brief        A listener to be notified after contact-information will be changed in contacts-db
+     */
+    class IContactDbChangeListener;
+    /*
+     *@brief        Class that work with contacts database.
+     *@brief        Search contacts, add listeners, register callback on changes in database.
+     */
     class ContactManager
     {
         public:
             ContactManager();
             ~ContactManager();
+            ContactManager(ContactManager&) = delete;
+            ContactManager &operator=(ContactManager&) = delete;
 
         public:
+            /*
+             *@brief        Return what kind of error occurred
+             *@param[in]    error - number of error
+             *@return       kind of error
+             */
             static std::string whatError(int error);
-
+            /*
+             * @brief       Looks for contact information based on a given keyword
+             * @param       keyword - search keyword
+             */
             template<typename T>
             ContactList<T> search(const std::string &keyword);
+            /*
+             *@brief        Find by number: id, name and thumbnail path
+             *@param[in]    number - contact person number
+             *@return       ContactPersonNumber
+             */
             ContactPersonNumber getContactPersonNumber(const std::string &number) const;
-    };
-}
+            /*
+             *@brief        Add listener on contacts database
+             *@param[in]    listener
+             */
+            void addListener(IContactDbChangeListener &listener);
+            /*
+             *@brief        Remove listener
+             *@param[in]    listener - listener to be remove
+             */
+            void removeListener(IContactDbChangeListener &listener);
 
+        private:
+            /*
+             *@brief        A callback passed to contacts_db_add_changed_cb() to get notifications
+                            about contacts modifications from contact service
+             *@param[in]    view_uri - The view URI of records whose changes are monitored
+             *@param[in]    user_data - the user data passed from the callback registration function
+             */
+            static void contactChangedCb(const char *view_uri, void *user_data);
+
+        private:
+            std::list<IContactDbChangeListener *> m_Listeners;
+    };
 
+    class IContactDbChangeListener
+    {
+        public:
+            virtual ~IContactDbChangeListener() {}
+            /*
+             *@brief        A method called when changing of some contact-record has been occurred
+             */
+            virtual void onContactChanged() {};
+    };
+}
 
 #endif /* __ContactManager_h__ */
index 337425c..779d515 100644 (file)
@@ -20,6 +20,8 @@
 #include "ContactPersonNumber.h"
 #include "ContactPersonEmail.h"
 
+#include <algorithm>
+
  namespace Msg
  {
     ContactManager::ContactManager()
         MSG_LOG("");
         int error = contacts_connect();
         if(error != 0)
+        {
+            MSG_LOG_ERROR(whatError(error));
+        }
+
+        error = contacts_db_add_changed_cb(_contacts_contact._uri, contactChangedCb, this);
+        if(error != 0)
+        {
             MSG_LOG_ERROR(whatError(error));
+        }
     }
 
     ContactManager::~ContactManager()
     {
         MSG_LOG("");
-        int error = contacts_disconnect();
+        int error = contacts_db_remove_changed_cb(_contacts_contact._uri, contactChangedCb, this);
+        if(error != 0)
+            MSG_LOG_ERROR(whatError(error));
+
+        error = contacts_disconnect();
         if(error != 0)
             MSG_LOG_ERROR(whatError(error));
     }
         return ContactPersonNumber (cResValue);
     }
 
+    void ContactManager::contactChangedCb(const char *view_uri, void *user_data)
+    {
+        ContactManager *self = static_cast<ContactManager *>(user_data);
+        for (auto listener : self->m_Listeners)
+        {
+            listener->onContactChanged();
+        }
+    }
+
+    void ContactManager::addListener(IContactDbChangeListener &listener)
+    {
+        auto found = std::find(m_Listeners.begin(), m_Listeners.end(), &listener);
+        if (found == m_Listeners.end())
+        {
+            m_Listeners.push_back(&listener);
+        }
+    }
+
+    void ContactManager::removeListener(IContactDbChangeListener &listener)
+    {
+        auto found = std::find(m_Listeners.begin(), m_Listeners.end(), &listener);
+        if (found != m_Listeners.end())
+        {
+            m_Listeners.erase(found);
+        }
+    }
 }
 
index 2255985..c22965f 100644 (file)
@@ -42,6 +42,73 @@ namespace Msg
             return contact_id;
         }
 
+        bool ContactUtils::getContactName(int contactId, contacts_record_h &contact, contacts_record_h &name)
+        {
+            int error = CONTACTS_ERROR_NONE;
+
+            error = contacts_db_get_record(_contacts_contact._uri, contactId, &contact);
+            if (error != CONTACTS_ERROR_NONE)
+            {
+                MSG_LOG_ERROR("contacts_db_get_record failed");
+                return false;
+            }
+
+            error = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &name);
+            if (error != CONTACTS_ERROR_NONE)
+            {
+                MSG_LOG_ERROR("contacts_record_get_child_record_at_p failed");
+                contacts_record_destroy(contact, true);
+                return false;
+            }
+
+            return true;
+        }
+
+        void ContactUtils::renameContact(int contactId, const std::string &newName)
+        {
+            contacts_record_h contact = NULL;
+            contacts_record_h name = NULL;
+            int error = CONTACTS_ERROR_NONE;
+
+            if (getContactName(contactId, contact, name))
+            {
+                contacts_record_set_str(name, _contacts_name.first, newName.c_str());
+
+                error = contacts_db_update_record(contact);
+                if (error != CONTACTS_ERROR_NONE)
+                {
+                    MSG_LOG_ERROR("contacts_db_update_record failed");
+                    contacts_record_destroy(contact, true);
+                    return;
+                }
+
+                contacts_record_destroy(contact, true);
+            }
+        }
+
+        std::string ContactUtils::getNameById(int contactId) const
+        {
+            contacts_record_h contact = NULL;
+            contacts_record_h name = NULL;
+            char *str = nullptr;
+            int error = CONTACTS_ERROR_NONE;
+            std::string result;
+
+            if (getContactName(contactId, contact, name))
+            {
+                contacts_record_get_str(name, _contacts_name.first, &str);
+                if (str)
+                {
+                    result = str;
+                    free(str);
+                }
+
+                contacts_record_destroy(contact, true);
+            }
+
+            return result;
+        }
+
         void ContactUtils::removeContact(int contactId)
         {
                contacts_db_delete_record(_contacts_contact._uri, contactId);
index 0185a1e..6d8c1a4 100644 (file)
@@ -6,6 +6,7 @@
 #include "ContactPersonNumber.h"
 #include "contacts.h"
 #include <functional>
+#include "Logger.h"
 
 namespace Msg
 {
@@ -17,6 +18,9 @@ namespace Msg
                 static ContactUtils &getInst();
 
                 int createContact(const std::string &strName, const std::string &strNumber);
+                bool getContactName(int contactId, contacts_record_h &contact, contacts_record_h &name);
+                void renameContact(int contactId, const std::string &newName);
+                std::string getNameById(int contactId) const;
                 void removeContact(int contactId);
 
             private:
index b0606a6..d4bca1c 100644 (file)
 #include <gtest/gtest.h>
 #include <gmock/gmock.h>
 #include "ContactUtils.h"
+#include "BaseController.h"
+#include "Logger.h"
 
 using namespace Msg;
 
 class TestContactManager: public testing::Test
 {
-protected:
-       virtual void SetUp()
-       {
-               m_ContactId = -100; //dummy id
-       }
-       virtual void TearDown()
-       {
-               if (m_ContactId != -100)
-               {
-                       Msg::Test::ContactUtils::getInst().removeContact(m_ContactId);
-               }
-       }
-
-       int m_ContactId;
-       ContactManager m_ContactManager;
+    protected:
+        virtual void SetUp()
+        {
+            m_ContactId = -100; //dummy id
+        }
+
+        virtual void TearDown()
+        {
+            if (m_ContactId != -100)
+            {
+                Msg::Test::ContactUtils::getInst().removeContact(m_ContactId);
+            }
+        }
+        int m_ContactId;
+        ContactManager m_ContactManager;
 };
 
+//____________________________________________________________________________//
+
 TEST_F( TestContactManager, FindNameSasha )
 {
     const std::string strNumber = "0936403503";
@@ -73,3 +77,39 @@ TEST_F( TestContactManager, FindNameVova )
 
     ASSERT_EQ(result, strName);
 }
+
+//____________________________________________________________________________//
+
+TEST_F( TestContactManager, TestGetNameById )
+{
+    const std::string strNumber = "0971234567";
+    const std::string strName = "Test1";
+
+    m_ContactId = Msg::Test::ContactUtils::getInst().createContact(strName, strNumber);
+
+    ContactPersonNumber item = m_ContactManager.getContactPersonNumber(strNumber);
+    std::string result1 = item.getDispName();
+
+    std::string result2 = Msg::Test::ContactUtils::getInst().getNameById(m_ContactId);
+
+    ASSERT_EQ(result1, result2);
+}
+
+//____________________________________________________________________________//
+
+TEST_F( TestContactManager, TestRenameContact )
+{
+    const std::string strNumber = "0971234567";
+    const std::string strName = "Test1";
+    const std::string newName = "Test2";
+
+    m_ContactId = Msg::Test::ContactUtils::getInst().createContact(strName, strNumber);
+    Msg::Test::ContactUtils::getInst().renameContact(m_ContactId, newName);
+
+    std::string result = Msg::Test::ContactUtils::getInst().getNameById(m_ContactId);
+
+    ASSERT_EQ(newName, result);
+}
+
+//____________________________________________________________________________//
+