provide thumbnail path in contact and person views 17/67117/11
authorJongkyu Koo <jk.koo@samsung.com>
Mon, 25 Apr 2016 08:28:02 +0000 (17:28 +0900)
committerJongkyu Koo <jk.koo@samsung.com>
Thu, 12 May 2016 09:22:48 +0000 (02:22 -0700)
Change-Id: Ic1e437f934572ad027412ef3dbaeb02a8847e9e0
Signed-off-by: Jongkyu Koo <jk.koo@samsung.com>
12 files changed:
common/ctsvc_image_util.h
common/ctsvc_vcard.c
packaging/contacts-service.spec
server/ctsvc_server_person.c
server/db/ctsvc_db_plugin_contact.c
server/db/ctsvc_db_plugin_contact_helper.c
server/db/ctsvc_db_plugin_image.c
server/db/ctsvc_db_plugin_image_helper.c
server/db/ctsvc_db_plugin_image_helper.h
server/db/ctsvc_db_plugin_person_helper.c
server/db/ctsvc_db_utils.c
server/db/ctsvc_db_utils.h

index beb77ab..14c1df6 100644 (file)
@@ -22,6 +22,8 @@
 #include <image_util.h>
 
 #define CTSVC_IMAGE_MAX_SIZE 1080
+#define CTSVC_IMAGE_THUMBNAIL_SIZE 96
+#define CTSVC_IMAGE_THUMBNAIL_SUFFIX "_thumbnail"
 #define CTSVC_IMAGE_ENCODE_QUALITY 50
 
 int ctsvc_image_util_get_mimetype(image_util_colorspace_e colorspace, int *p_mimetype);
index d7be628..195f1fd 100644 (file)
@@ -1555,7 +1555,7 @@ static inline int __ctsvc_vcard_encode_photo(const char *src,
                unsigned char **image, unsigned int *image_size)
 {
        int ret;
-       vcard_image_info info = {.src = src, .image = image, .image_size = image_size, ret = CONTACTS_ERROR_SYSTEM};
+       vcard_image_info info = {src, image, image_size, CONTACTS_ERROR_SYSTEM};
 
        ret = image_util_foreach_supported_jpeg_colorspace(
                        _ctsvc_vcard_image_util_supported_jpeg_colorspace_cb, &info);
index 480ac28..c20b36b 100644 (file)
@@ -1,6 +1,6 @@
 Name:       contacts-service
 Summary:    Contacts Service
-Version:    0.13.51
+Version:    0.13.52
 Release:    0
 Group:      Social & Content/Service
 License:    Apache-2.0
index afc0bc3..8824d03 100644 (file)
@@ -32,6 +32,7 @@
 #include "ctsvc_server_setting.h"
 #include "ctsvc_list.h"
 #include "ctsvc_localize.h"
+#include "ctsvc_db_plugin_image_helper.h"
 
 #ifdef ENABLE_LOG_FEATURE
 #include "ctsvc_server_phonelog.h"
@@ -278,6 +279,7 @@ static inline int __ctsvc_put_person_default_image(int person_id, int id)
        cts_stmt stmt;
        char *image_path;
        char query[CTS_SQL_MAX_LEN] = {0};
+       char *thumbnail_path;
 
        ret = ctsvc_begin_trans();
        RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_begin_trans() Fail(%d)", ret);
@@ -325,8 +327,7 @@ static inline int __ctsvc_put_person_default_image(int person_id, int id)
        if (false == is_default) {
                snprintf(query, sizeof(query),
                                "UPDATE "CTS_TABLE_DATA" SET is_default=0 WHERE datatype=%d  AND is_my_profile = 0 "
-                               "AND contact_id = (SELECT contact_id FROM "CTS_TABLE_DATA" WHERE id=%d) ",
-                               CONTACTS_DATA_TYPE_IMAGE, id);
+                               "AND contact_id = %d ", CONTACTS_DATA_TYPE_IMAGE, contact_id);
 
                ret = ctsvc_query_exec(query);
                if (CONTACTS_ERROR_NONE != ret) {
@@ -335,15 +336,21 @@ static inline int __ctsvc_put_person_default_image(int person_id, int id)
                        ctsvc_end_trans(false);
                        return ret;
                }
+
+               thumbnail_path = ctsvc_utils_make_thumbnail(image_path);
+       } else {
+               thumbnail_path = ctsvc_utils_get_thumbnail_path(image_path);
        }
 
+       free(image_path);
+
        /* set is_default, is _primary_default */
        snprintf(query, sizeof(query),
                        "UPDATE "CTS_TABLE_DATA" SET is_primary_default=1, is_default=1 WHERE id=%d ", id);
        ret = ctsvc_query_exec(query);
        if (CONTACTS_ERROR_NONE != ret) {
                ERR("ctsvc_query_exec() Fail(%d)", ret);
-               free(image_path);
+               free(thumbnail_path);
                ctsvc_end_trans(false);
                return ret;
        }
@@ -354,45 +361,47 @@ static inline int __ctsvc_put_person_default_image(int person_id, int id)
        ret = ctsvc_query_prepare(query, &stmt);
        if (NULL == stmt) {
                ERR("ctsvc_query_prepare() Fail(%d)", ret);
-               free(image_path);
+               free(thumbnail_path);
                ctsvc_end_trans(false);
                return ret;
        }
 
-       ctsvc_stmt_bind_text(stmt, 1, image_path);
+       ctsvc_stmt_bind_text(stmt, 1, thumbnail_path);
        ret = ctsvc_stmt_step(stmt);
        if (CONTACTS_ERROR_NONE != ret) {
                ERR("ctsvc_stmt_step() Fail(%d)", ret);
                ctsvc_stmt_finalize(stmt);
-               free(image_path);
+               free(thumbnail_path);
                ctsvc_end_trans(false);
                return ret;
        }
        ctsvc_stmt_finalize(stmt);
 
        /* update contact's image_thumbnail_path */
-       snprintf(query, sizeof(query),
-                       "UPDATE "CTS_TABLE_CONTACTS" SET image_thumbnail_path=? WHERE contact_id=%d ", contact_id);
-       ret = ctsvc_query_prepare(query, &stmt);
-       if (NULL == stmt) {
-               ERR("ctsvc_query_prepare() Fail(%d)", ret);
-               free(image_path);
-               ctsvc_end_trans(false);
-               return ret;
-       }
+       if (false == is_default) {
+               snprintf(query, sizeof(query),
+                               "UPDATE "CTS_TABLE_CONTACTS" SET image_thumbnail_path=? WHERE contact_id=%d ", contact_id);
+               ret = ctsvc_query_prepare(query, &stmt);
+               if (NULL == stmt) {
+                       ERR("ctsvc_query_prepare() Fail(%d)", ret);
+                       free(thumbnail_path);
+                       ctsvc_end_trans(false);
+                       return ret;
+               }
 
-       ctsvc_stmt_bind_text(stmt, 1, image_path);
-       ret = ctsvc_stmt_step(stmt);
-       if (CONTACTS_ERROR_NONE != ret) {
-               ERR("ctsvc_stmt_step() Fail(%d)", ret);
+               ctsvc_stmt_bind_text(stmt, 1, thumbnail_path);
+               ret = ctsvc_stmt_step(stmt);
+               if (CONTACTS_ERROR_NONE != ret) {
+                       ERR("ctsvc_stmt_step() Fail(%d)", ret);
+                       ctsvc_stmt_finalize(stmt);
+                       free(thumbnail_path);
+                       ctsvc_end_trans(false);
+                       return ret;
+               }
                ctsvc_stmt_finalize(stmt);
-               free(image_path);
-               ctsvc_end_trans(false);
-               return ret;
        }
-       ctsvc_stmt_finalize(stmt);
 
-       free(image_path);
+       free(thumbnail_path);
 
        ret = ctsvc_end_trans(true);
        return ret;
@@ -624,17 +633,18 @@ int ctsvc_person_aggregate(int person_id)
 
        /* check image_thumbnail_path */
        if (person->image_thumbnail_path) {
-               temp = __ctsvc_get_image_filename(person->image_thumbnail_path);
+               char *image_path = NULL;
+
+               image_path = ctsvc_utils_get_image_path(person->image_thumbnail_path);
                snprintf(query, sizeof(query),
                                "SELECT D.id FROM "CTS_TABLE_CONTACTS" C, "CTS_TABLE_DATA" D "
                                "WHERE C.person_id=%d AND C.contact_id=D.contact_id AND C.deleted = 0 "
                                "AND D.datatype=%d AND D.is_primary_default = 1 AND D.data3='%s'",
-                               person->person_id, CONTACTS_DATA_TYPE_IMAGE, temp);
+                               person->person_id, CONTACTS_DATA_TYPE_IMAGE, image_path);
+               free(image_path);
                ret = ctsvc_query_get_first_int_result(query, &id);
                if (ret == CONTACTS_ERROR_NONE)
-                       image_thumbnail_path = SAFE_STRDUP(temp);
-       } else {
-               image_thumbnail_path = NULL;
+                       image_thumbnail_path = SAFE_STRDUP(person->image_thumbnail_path);
        }
 
        /* check name_contact_id */
@@ -787,9 +797,10 @@ int ctsvc_person_aggregate(int person_id)
                len += snprintf(addressbook_ids + len, addressbooks_len -len, "%d%s", addressbook_id, ADDRESSBOOK_ID_DELIM);
 
                if (NULL == image_thumbnail_path && contact_image_thumbnail_path && *contact_image_thumbnail_path) {
-                       temp = __ctsvc_get_image_filename(contact_image_thumbnail_path);
-                       image_thumbnail_path = SAFE_STRDUP(temp);
-                       /* update data table : is_primary_default */
+                       image_thumbnail_path = SAFE_STRDUP(contact_image_thumbnail_path);
+
+                       /*update data table : is_primary_default */
+                       ctsvc_db_image_set_primary_default(contact_id, image_thumbnail_path, true);
                }
                free(contact_image_thumbnail_path);
 
index 379f317..b33d740 100644 (file)
@@ -1229,6 +1229,7 @@ static int __ctsvc_db_contact_update_record(contacts_record_h record)
                int ret = CONTACTS_ERROR_NONE;
                int count = 0;
                ctsvc_image_s *image;
+               char *contact_image_path = NULL;
 
                contacts_list_get_count((contacts_list_h)contact->images, &count);
                if (count) {
@@ -1245,16 +1246,17 @@ static int __ctsvc_db_contact_update_record(contacts_record_h record)
                                        break;
                        } while (CONTACTS_ERROR_NONE == contacts_list_next((contacts_list_h)contact->images));
 
-                       if ((NULL == contact->image_thumbnail_path && image->path) ||
-                                       (contact->image_thumbnail_path && NULL == image->path) ||
-                                       (contact->image_thumbnail_path && image->path && (STRING_EQUAL != strcmp(contact->image_thumbnail_path, image->path)))) {
+                       contact_image_path = ctsvc_utils_get_image_path(contact->image_thumbnail_path);
+
+                       if ((NULL == contact_image_path && image->path) ||
+                                       (contact_image_path && NULL == image->path) ||
+                                       (contact_image_path && image->path && (STRING_EQUAL != strcmp(contact_image_path, image->path)))) {
                                ctsvc_record_set_property_flag((ctsvc_record_s*)contact, _contacts_contact.image_thumbnail_path, CTSVC_PROPERTY_FLAG_DIRTY);
 
-                               if (ctsvc_contact_check_image_location(image->path))
-                                       contact->image_thumbnail_path = SAFE_STRDUP(image->path + strlen(CTSVC_CONTACT_IMG_FULL_LOCATION) + 1);
-                               else
-                                       contact->image_thumbnail_path = SAFE_STRDUP(image->path);
+                               if (image->path)
+                                       contact->image_thumbnail_path = ctsvc_utils_make_thumbnail(image->path);
                        }
+                       free(contact_image_path);
                } else if (contact->image_thumbnail_path) {
                        free(contact->image_thumbnail_path);
                        contact->image_thumbnail_path = NULL;
@@ -2079,7 +2081,7 @@ static int __ctsvc_db_contact_insert_record(contacts_record_h record, int *id)
                                }
 
                                if (image->path && image->is_default) {
-                                       contact->image_thumbnail_path = strdup(image->path);
+                                       contact->image_thumbnail_path = ctsvc_utils_make_thumbnail(image->path);
                                        break;
                                }
                        } while (CONTACTS_ERROR_NONE == contacts_list_next((contacts_list_h)contact->images));
@@ -2322,6 +2324,7 @@ static int __ctsvc_db_contact_replace_record(contacts_record_h record, int conta
                int ret = CONTACTS_ERROR_NONE;
                int count = 0;
                ctsvc_image_s *image;
+               char *contact_image_path = NULL;
 
                contacts_list_get_count((contacts_list_h)contact->images, &count);
                if (count) {
@@ -2339,16 +2342,16 @@ static int __ctsvc_db_contact_replace_record(contacts_record_h record, int conta
                                        break;
                        } while (CONTACTS_ERROR_NONE == contacts_list_next((contacts_list_h)contact->images));
 
-                       if ((NULL == contact->image_thumbnail_path && image->path) ||
-                                       (contact->image_thumbnail_path && NULL == image->path) ||
-                                       (contact->image_thumbnail_path && image->path && STRING_EQUAL != strcmp(contact->image_thumbnail_path, image->path))) {
+                       contact_image_path = ctsvc_utils_get_image_path(contact->image_thumbnail_path);
+                       if ((NULL == contact_image_path && image->path) ||
+                                       (contact_image_path && NULL == image->path) ||
+                                       (contact_image_path && image->path && STRING_EQUAL != strcmp(contact_image_path, image->path))) {
                                ctsvc_record_set_property_flag((ctsvc_record_s*)contact, _contacts_contact.image_thumbnail_path, CTSVC_PROPERTY_FLAG_DIRTY);
 
-                               if (ctsvc_contact_check_image_location(image->path))
-                                       contact->image_thumbnail_path = SAFE_STRDUP(image->path + strlen(CTSVC_CONTACT_IMG_FULL_LOCATION) + 1);
-                               else
-                                       contact->image_thumbnail_path = SAFE_STRDUP(image->path);
+                               if (image->path)
+                                       contact->image_thumbnail_path = ctsvc_utils_make_thumbnail(image->path);
                        }
+                       free(contact_image_path);
                } else if (contact->image_thumbnail_path) {
                        free(contact->image_thumbnail_path);
                        contact->image_thumbnail_path = NULL;
index 097e9f3..fc8ee27 100644 (file)
@@ -163,6 +163,7 @@ int ctsvc_contact_update_image_file(int parent_id, int img_id,
 {
        int ret;
        char dest[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
+       char *thumbnail_path = NULL;
 
        ret = __ctsvc_contact_get_current_image_file(img_id, dest, sizeof(dest));
 
@@ -177,6 +178,15 @@ int ctsvc_contact_update_image_file(int parent_id, int img_id,
                ret = unlink(dest);
                if (ret < 0)
                        WARN("unlink() Fail(%d)", errno);
+
+               /* remove thumbnail */
+               thumbnail_path = ctsvc_utils_get_thumbnail_path(dest);
+               if (thumbnail_path && 0 == access(thumbnail_path, F_OK)) {
+                       ret = unlink(thumbnail_path);
+                       if (ret < 0)
+                               WARN("unlink() Fail(%d)", errno);
+               }
+               free(thumbnail_path);
        }
 
        if (src_img) {
@@ -210,10 +220,20 @@ int ctsvc_contact_delete_image_file_with_path(const unsigned char *image_path)
 
        if (image_path) {
                char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
+               char *thumbnail_path = NULL;
                snprintf(full_path, sizeof(full_path), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, image_path);
                ret = unlink(full_path);
                if (ret < 0)
                        WARN("unlink() Fail(%d)", errno);
+
+               /* remove thumbnail */
+               thumbnail_path = ctsvc_utils_get_thumbnail_path(full_path);
+               if (thumbnail_path && 0 == access(thumbnail_path, F_OK)) {
+                       ret = unlink(thumbnail_path);
+                       if (ret < 0)
+                               WARN("unlink() Fail(%d)", errno);
+               }
+               free(thumbnail_path);
        }
 
        return CONTACTS_ERROR_NONE;
index 3b62770..bae08da 100644 (file)
@@ -72,20 +72,38 @@ static int __ctsvc_db_image_set_default(int image_id, int contact_id, bool is_de
        return ret;
 }
 
-static int __ctsvc_db_image_get_primary_default_image_id(int person_id)
+static int __ctsvc_db_image_get_primary_default_image_id(int person_id, int *image_id, char** image_path)
 {
        int ret;
-       int default_image_id;
+       cts_stmt stmt;
        char query[CTS_SQL_MAX_LEN] = {0};
 
        snprintf(query, sizeof(query),
-                       "SELECT id FROM "CTS_TABLE_CONTACTS" c, "CTS_TABLE_DATA" d "
+                       "SELECT id d.data3 FROM "CTS_TABLE_CONTACTS" c, "CTS_TABLE_DATA" d "
                        "WHERE c.person_id = %d AND d.datatype = %d AND c.contact_id = d.contact_id AND d.is_default = 1",
                        person_id, CONTACTS_DATA_TYPE_IMAGE);
-       ret = ctsvc_query_get_first_int_result(query, &default_image_id);
-       if (CONTACTS_ERROR_NONE != ret)
-               return 0;
-       return default_image_id;
+
+       ret = ctsvc_query_prepare(query, &stmt);
+       if (NULL == stmt) {
+               ERR("ctsvc_query_prepare() Fail(%d)", ret);
+               return ret;
+       }
+
+       ret = ctsvc_stmt_step(stmt);
+       if (1 != ret) {
+               ERR("ctsvc_stmt_step() Fail(%d)", ret);
+               ctsvc_stmt_finalize(stmt);
+               if (ret == CONTACTS_ERROR_NONE)
+                       return CONTACTS_ERROR_NO_DATA;
+               else
+                       return ret;
+       }
+
+       *image_id = ctsvc_stmt_get_int(stmt, 0);
+       *image_path = ctsvc_stmt_get_text(stmt, 1);
+       ctsvc_stmt_finalize(stmt);
+
+       return CONTACTS_ERROR_NONE;
 }
 
 static int __ctsvc_db_image_get_primary_default_contact_id(int person_id)
@@ -104,7 +122,7 @@ static int __ctsvc_db_image_get_primary_default_contact_id(int person_id)
        return default_contact_id;
 }
 
-static int __ctsvc_db_image_update_contact_image(int contact_id, const char *image_path)
+static int __ctsvc_db_image_update_contact_image(int contact_id, const char *image_thumbnail_path)
 {
        int ret;
        char query[CTS_SQL_MAX_LEN] = {0};
@@ -115,8 +133,8 @@ static int __ctsvc_db_image_update_contact_image(int contact_id, const char *ima
        ret = ctsvc_query_prepare(query, &stmt);
        RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
 
-       if (image_path)
-               ctsvc_stmt_bind_text(stmt, 1, image_path);
+       if (image_thumbnail_path)
+               ctsvc_stmt_bind_text(stmt, 1, image_thumbnail_path);
 
        ret = ctsvc_stmt_step(stmt);
        WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_stmt_step() Fail(%d)", ret);
@@ -126,7 +144,7 @@ static int __ctsvc_db_image_update_contact_image(int contact_id, const char *ima
        return ret;
 }
 
-static int __ctsvc_db_image_update_person_image(int person_id, const char *image_path)
+static int __ctsvc_db_image_update_person_image(int person_id, const char *image_thumbnail_path)
 {
        int ret;
        char query[CTS_SQL_MAX_LEN] = {0};
@@ -137,8 +155,8 @@ static int __ctsvc_db_image_update_person_image(int person_id, const char *image
        ret = ctsvc_query_prepare(query, &stmt);
        RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
 
-       if (image_path)
-               ctsvc_stmt_bind_text(stmt, 1, image_path);
+       if (image_thumbnail_path)
+               ctsvc_stmt_bind_text(stmt, 1, image_thumbnail_path);
 
        ret = ctsvc_stmt_step(stmt);
        WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_stmt_step() Fail(%d)", ret);
@@ -159,6 +177,7 @@ static int __ctsvc_db_image_insert_record(contacts_record_h record, int *id)
        char query[CTS_SQL_MAX_LEN] = {0};
        ctsvc_image_s *image = (ctsvc_image_s*)record;
        cts_stmt stmt = NULL;
+       char *thumbnail_path = NULL;
 
        RETV_IF(NULL == image->path, CONTACTS_ERROR_INVALID_PARAMETER);
 
@@ -222,22 +241,25 @@ static int __ctsvc_db_image_insert_record(contacts_record_h record, int *id)
        ret = ctsvc_query_prepare(query, &stmt);
        RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
 
-       if (image->is_default)
-               ctsvc_stmt_bind_text(stmt, 1, image->path);
-
+       if (image->is_default) {
+               thumbnail_path = ctsvc_utils_make_thumbnail(image->path);
+               if (thumbnail_path)
+                       ctsvc_stmt_bind_text(stmt, 1, thumbnail_path);
+       }
        ret = ctsvc_stmt_step(stmt);
        WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_stmt_step() Fail(%d)", ret);
        ctsvc_stmt_finalize(stmt);
 
        if (image->is_default) {
                int primary_default_contact_id;
-
+               __ctsvc_db_image_update_contact_image(image->contact_id, thumbnail_path);
                primary_default_contact_id = __ctsvc_db_image_get_primary_default_contact_id(person_id);
                if (primary_default_contact_id == 0 || primary_default_contact_id == image->contact_id) {
                        __ctsvc_db_image_set_primary_default(*id, true);
-                       __ctsvc_db_image_update_person_image(person_id, image->path);
+                       __ctsvc_db_image_update_person_image(person_id, thumbnail_path);
                }
        }
+       free(thumbnail_path);
 
        ctsvc_set_contact_noti();
        ctsvc_set_person_noti();
@@ -297,6 +319,8 @@ static int __ctsvc_db_image_update_record(contacts_record_h record)
        char query[CTS_SQL_MAX_LEN] = {0};
        ctsvc_image_s *image = (ctsvc_image_s*)record;
        cts_stmt stmt = NULL;
+       char *thumbnail_path = NULL;
+
        RETVM_IF(NULL == image->path, CONTACTS_ERROR_INVALID_PARAMETER, "path is empty");
 
        ret = ctsvc_begin_trans();
@@ -354,8 +378,10 @@ static int __ctsvc_db_image_update_record(contacts_record_h record)
        ret = ctsvc_query_prepare(query, &stmt);
        RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
 
-       if (image->is_default)
-               ctsvc_stmt_bind_text(stmt, 1, image->path);
+       if (image->is_default) {
+               thumbnail_path = ctsvc_utils_make_thumbnail(image->path);
+               ctsvc_stmt_bind_text(stmt, 1, thumbnail_path);
+       }
 
        ret = ctsvc_stmt_step(stmt);
        WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_stmt_step() Fail(%d)", ret);
@@ -363,12 +389,14 @@ static int __ctsvc_db_image_update_record(contacts_record_h record)
 
        if (image->is_default) {
                int primary_default_contact_id;
-               primary_default_contact_id = __ctsvc_db_image_get_primary_default_contact_id(image->contact_id);
-               if (image->contact_id == primary_default_contact_id) {
+               __ctsvc_db_image_update_contact_image(image->contact_id, thumbnail_path);
+               primary_default_contact_id = __ctsvc_db_image_get_primary_default_contact_id(person_id);
+               if (primary_default_contact_id == image->contact_id) {
                        __ctsvc_db_image_set_primary_default(image->id, true);
-                       __ctsvc_db_image_update_person_image(person_id, image->path);
+                       __ctsvc_db_image_update_person_image(person_id, thumbnail_path);
                }
        }
+       free(thumbnail_path);
 
        ctsvc_set_contact_noti();
        ctsvc_set_person_noti();
@@ -386,7 +414,7 @@ static int __ctsvc_db_image_delete_record(int id)
 {
        int ret;
        int version;
-       int image_id;
+       int image_id = 0;
        int contact_id;
        int person_id;
        int is_default;
@@ -479,24 +507,57 @@ static int __ctsvc_db_image_delete_record(int id)
        }
 
        if (is_default) {
-               snprintf(query, sizeof(query),
-                               "SELECT id FROM "CTS_TABLE_DATA" WHERE datatype = %d AND contact_id = %d AND is_my_profile = 0 limit 1",
-                               CONTACTS_DATA_TYPE_IMAGE, contact_id);
-               ret = ctsvc_query_get_first_int_result(query, &image_id);
+               char *image_path = NULL;
+               char *thumbnail_path = NULL;
+
+               //TODO : make function
+               do {
+                       snprintf(query, sizeof(query),
+                                       "SELECT id data3 FROM "CTS_TABLE_DATA" WHERE datatype = %d AND contact_id = %d AND is_my_profile = 0 limit 1",
+                                       CONTACTS_DATA_TYPE_IMAGE, contact_id);
+
+                       ret = ctsvc_query_prepare(query, &stmt);
+                       if (NULL == stmt) {
+                               ERR("ctsvc_query_prepare() Fail(%d)", ret);
+                               break;
+                       }
+
+                       ret = ctsvc_stmt_step(stmt);
+                       if (1 != ret) {
+                               ERR("ctsvc_stmt_step() Fail(%d)", ret);
+                               ctsvc_stmt_finalize(stmt);
+                               break;
+                       }
+
+                       image_id = ctsvc_stmt_get_int(stmt, 0);
+                       image_path = ctsvc_stmt_get_text(stmt, 1);
+                       ctsvc_stmt_finalize(stmt);
+               }  while (0);
 
-               if (image_id) {
+               if (0 != image_id && NULL != image_path) {
                        __ctsvc_db_image_set_default(image_id, contact_id, is_default, is_primary_default);
+                       thumbnail_path = ctsvc_utils_make_thumbnail(image_path);
+                       __ctsvc_db_image_update_contact_image(contact_id, thumbnail_path);
+
+                       if (is_primary_default)
+                               __ctsvc_db_image_update_person_image(person_id, thumbnail_path);
                } else {
                        __ctsvc_db_image_update_contact_image(contact_id, NULL);
+
                        if (is_primary_default) {
-                               int default_img_id = 0;
-                               default_img_id = __ctsvc_db_image_get_primary_default_image_id(person_id);
-                               if (default_img_id)
-                                       __ctsvc_db_image_set_primary_default(default_img_id, true);
-                               else
+                               image_id = 0;
+                               image_path = NULL;
+                               ret = __ctsvc_db_image_get_primary_default_image_id(person_id, &image_id, &image_path);
+                               if (CONTACTS_ERROR_NONE == ret && 0 != image_id && NULL != image_path) {
+                                       __ctsvc_db_image_set_primary_default(image_id, true);
+                                       thumbnail_path = ctsvc_utils_get_thumbnail_path(image_path);
+                                       __ctsvc_db_image_update_person_image(person_id, thumbnail_path);
+                               } else {
                                        __ctsvc_db_image_update_person_image(person_id, NULL);
+                               }
                        }
                }
+               free(thumbnail_path);
        }
 
        ctsvc_set_contact_noti();
index 5d1407d..c1d611a 100644 (file)
@@ -28,6 +28,7 @@
 #include "ctsvc_notification.h"
 #include "ctsvc_db_access_control.h"
 #include "ctsvc_notify.h"
+#include "ctsvc_db_utils.h"
 
 int ctsvc_db_image_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record,
                int start_count)
@@ -240,4 +241,30 @@ void ctsvc_db_image_delete_callback(sqlite3_context *context, int argc, sqlite3_
        return;
 }
 
+int ctsvc_db_image_set_primary_default(int contact_id, const char *image_thumbnail_path,
+               bool is_primary_default)
+{
+       int ret;
+       char *image_path = NULL;
+       char query[CTS_SQL_MAX_LEN] = {0};
+
+       RETV_IF(contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER);
+       RETV_IF(NULL == image_thumbnail_path, CONTACTS_ERROR_INVALID_PARAMETER);
+
+       image_path = ctsvc_utils_get_image_path(image_thumbnail_path);
+
+       if (NULL == image_path) {
+               ERR("ctsvc_utils_get_image_path() Fail");
+               return CONTACTS_ERROR_INTERNAL;
+       }
+
+       snprintf(query, sizeof(query),
+                       "UPDATE "CTS_TABLE_DATA" SET is_primary_default = %d WHERE contact_id = %d AND datatype = %d AND data3 = %s",
+                       is_primary_default, contact_id, CONTACTS_DATA_TYPE_IMAGE, image_path);
+
+       free(image_path);
+       ret = ctsvc_query_exec(query);
+       WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_query_exec() Fail(%d)", ret);
+       return ret;
+}
 
index 2c20242..57c69f4 100644 (file)
@@ -30,5 +30,7 @@ int ctsvc_db_image_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record,
                int start_count);
 void ctsvc_db_image_delete_callback(sqlite3_context *context, int argc,
                sqlite3_value **argv);
+int ctsvc_db_image_set_primary_default(int contact_id, const char *image_thumbnail_path,
+               bool is_primary_default);
 
 #endif /* __CTSVC_DB_PLUGIN_RELATIONSHIP_HELPER_H__ */
index 0609882..010bcc3 100644 (file)
@@ -387,7 +387,7 @@ static inline int __ctsvc_db_update_person_default(int person_id, int datatype)
        cts_stmt stmt = NULL;
        char query[CTS_SQL_MIN_LEN] = {0};
        char *temp = NULL;
-       char *image_thumbnail_path = NULL;
+       char *image_path = NULL;
 
        snprintf(query, sizeof(query),
                        "SELECT D.id FROM "CTS_TABLE_CONTACTS" C, "CTS_TABLE_DATA" D "
@@ -420,28 +420,33 @@ static inline int __ctsvc_db_update_person_default(int person_id, int datatype)
                                ctsvc_stmt_finalize(stmt);
                                return ret;
                        }
-                       temp = ctsvc_stmt_get_text(stmt, 1);
-                       image_thumbnail_path = SAFE_STRDUP(temp);
+
+                       if (CONTACTS_DATA_TYPE_IMAGE == datatype) {
+                               temp = ctsvc_stmt_get_text(stmt, 1);
+                               image_path = SAFE_STRDUP(temp);
+                       }
                }
                ctsvc_stmt_finalize(stmt);
 
                if (CONTACTS_DATA_TYPE_IMAGE == datatype) {
-                       if (image_thumbnail_path) {
+                       if (image_path) {
+                               char *thumbnail_path = NULL;
+
                                snprintf(query, sizeof(query),
                                                "UPDATE "CTS_TABLE_PERSONS" SET image_thumbnail_path=? WHERE person_id=%d", person_id);
                                ret = ctsvc_query_prepare(query, &stmt);
                                RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
-                               ctsvc_stmt_bind_text(stmt, 1, image_thumbnail_path);
+                               thumbnail_path = ctsvc_utils_get_thumbnail_path(image_path);
+                               ctsvc_stmt_bind_text(stmt, 1, thumbnail_path);
                                ret = ctsvc_stmt_step(stmt);
                                ctsvc_stmt_finalize(stmt);
-                               free(image_thumbnail_path);
+                               free(image_path);
+                               free(thumbnail_path);
                                if (CONTACTS_ERROR_NONE != ret) {
                                        ERR("ctsvc_stmt_step() Fail(%d)", ret);
                                        return ret;
                                }
                        }
-               } else {
-                       free(image_thumbnail_path);
                }
        }
 
index ae8706b..e334f4e 100644 (file)
@@ -206,7 +206,7 @@ void ctsvc_utils_make_image_file_name(int parent_id, int id, char *src_img, char
        free(lower_ext);
 }
 
-static inline bool ctsvc_check_available_image_space(void)
+static inline bool _ctsvc_check_available_image_space(int need_size)
 {
        int ret;
        struct statfs buf;
@@ -217,7 +217,7 @@ static inline bool ctsvc_check_available_image_space(void)
 
        size = (long long)buf.f_bavail * (buf.f_bsize);
 
-       if (1024*1024 < size)  /* if available space to copy a image is larger than 1M */
+       if (need_size < size)  /* if available space to copy a image is larger than need_size */
                return true;
        return false;
 }
@@ -279,6 +279,7 @@ static image_util_rotation_e _ctsvc_image_get_rotation_info(const char *path)
 typedef struct {
        const char *src;
        const char *dest;
+       int max_size;
        int ret;
 } image_info;
 
@@ -351,7 +352,7 @@ static bool _ctsvc_image_util_supported_jpeg_colorspace_cb(
                buffer = buffer_temp;
        }
 
-       if (CTSVC_IMAGE_MAX_SIZE < width || CTSVC_IMAGE_MAX_SIZE < height) { /* need resize */
+       if (info->max_size < width || info->max_size < height) { /* need resize */
                int resized_width;
                int resized_height;
                media_format_h fmt;
@@ -359,11 +360,11 @@ static bool _ctsvc_image_util_supported_jpeg_colorspace_cb(
 
                /* set resize */
                if (width > height) {
-                       resized_width = CTSVC_IMAGE_MAX_SIZE;
-                       resized_height = height * CTSVC_IMAGE_MAX_SIZE / width;
+                       resized_width = info->max_size;
+                       resized_height = height * info->max_size / width;
                } else {
-                       resized_height = CTSVC_IMAGE_MAX_SIZE;
-                       resized_width = width * CTSVC_IMAGE_MAX_SIZE / height;
+                       resized_height = info->max_size;
+                       resized_width = width * info->max_size / height;
                }
 
                if (resized_height % 8)
@@ -436,10 +437,10 @@ static bool _ctsvc_image_util_supported_jpeg_colorspace_cb(
        return false;
 }
 
-static int _ctsvc_image_encode(const char *src, const char *dest)
+static int _ctsvc_image_encode(const char *src, const char *dest, int max_size)
 {
        int ret;
-       image_info info = {.src = src, .dest = dest, ret = CONTACTS_ERROR_SYSTEM};
+       image_info info = {src, dest, max_size, CONTACTS_ERROR_SYSTEM};
 
        ret = image_util_foreach_supported_jpeg_colorspace(
                        _ctsvc_image_util_supported_jpeg_colorspace_cb, &info);
@@ -464,10 +465,10 @@ int ctsvc_utils_copy_image(const char *dir, const char *src, const char *file)
        char dest[strlen(dir) + strlen(file) + 2];
        snprintf(dest, sizeof(dest), "%s/%s", dir, file);
 
-       if (false == ctsvc_check_available_image_space())
+       if (false == _ctsvc_check_available_image_space(1204 * 1204)) /*need larger than 1M*/
                return CONTACTS_ERROR_FILE_NO_SPACE;
 
-       ret = _ctsvc_image_encode(src, dest);
+       ret = _ctsvc_image_encode(src, dest, CTSVC_IMAGE_MAX_SIZE);
        if (CONTACTS_ERROR_NONE == ret)
                return ret;
        else
@@ -515,6 +516,104 @@ int ctsvc_utils_copy_image(const char *dir, const char *src, const char *file)
        return CONTACTS_ERROR_NONE;
 }
 
+char* ctsvc_utils_get_thumbnail_path(const char *image_path)
+{
+       int name_len = 0;
+       int full_len = 0;
+       char *thumbnail_path = NULL;
+       char *ext = NULL;
+
+       RETV_IF(NULL == image_path, NULL);
+       RETV_IF(NULL != strstr(image_path, CTSVC_IMAGE_THUMBNAIL_SUFFIX), NULL);
+
+       full_len = strlen(image_path) + strlen(CTSVC_IMAGE_THUMBNAIL_SUFFIX) + 1;
+       thumbnail_path = calloc(1, full_len);
+       if (NULL == thumbnail_path) {
+               ERR("calloc() Fail");
+               return NULL;
+       }
+
+       ext = strrchr(image_path, '.');
+       if (ext) {
+               name_len = ext -image_path;
+               strncpy(thumbnail_path, image_path, name_len);
+               snprintf(thumbnail_path+name_len, full_len-name_len, "%s%s", CTSVC_IMAGE_THUMBNAIL_SUFFIX, ext);
+       } else {
+               snprintf(thumbnail_path, full_len, "%s%s", image_path, CTSVC_IMAGE_THUMBNAIL_SUFFIX);
+       }
+
+       return thumbnail_path;
+}
+
+char* ctsvc_utils_get_image_path(const char *thumbnail_path)
+{
+       int name_len = 0;
+       int full_len = 0;
+       char *image_path = NULL;
+       char *ext = NULL;
+
+       RETV_IF(NULL == thumbnail_path, NULL);
+       RETV_IF(NULL == strstr(thumbnail_path, CTSVC_IMAGE_THUMBNAIL_SUFFIX), NULL);
+
+       full_len = strlen(thumbnail_path) - strlen(CTSVC_IMAGE_THUMBNAIL_SUFFIX) + 1;
+       image_path = calloc(1, full_len);
+       if (NULL == image_path) {
+          ERR("calloc() Fail");
+          return NULL;
+       }
+
+       ext = strrchr(thumbnail_path, '.');
+       if (ext) {
+               name_len = ext -thumbnail_path - strlen(CTSVC_IMAGE_THUMBNAIL_SUFFIX);
+               strncpy(image_path, thumbnail_path, name_len);
+               snprintf(image_path + name_len, full_len -name_len, "%s", ext);
+       } else {
+               name_len = strlen(thumbnail_path) - strlen(CTSVC_IMAGE_THUMBNAIL_SUFFIX);
+               strncpy(image_path, thumbnail_path, name_len);
+       }
+       return image_path;
+}
+
+char* ctsvc_utils_make_thumbnail(const char *image_path)
+{
+       int ret;
+       char src[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
+       char dest[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
+       char *thumbnail_path = NULL;
+
+       RETV_IF(NULL == image_path, NULL);
+       RETV_IF(NULL != strstr(image_path, CTSVC_IMAGE_THUMBNAIL_SUFFIX), NULL);
+
+       if (false == _ctsvc_check_available_image_space(
+                       CTSVC_IMAGE_THUMBNAIL_SIZE * CTSVC_IMAGE_THUMBNAIL_SIZE)) {
+                       ERR("No space to make thumbnail");
+                       return NULL;
+       }
+
+       thumbnail_path = ctsvc_utils_get_thumbnail_path(image_path);
+       if (NULL == thumbnail_path) {
+               ERR("ctsvc_image_util_get_thumbnail_path() Fail");
+               return NULL;
+       }
+
+       if (0 == access(dest, F_OK)) {
+               DBG("already exist");
+               return thumbnail_path;
+       }
+
+       snprintf(src, sizeof(src), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, image_path);
+       snprintf(dest, sizeof(dest), "%s/%s", CTSVC_CONTACT_IMG_FULL_LOCATION, thumbnail_path);
+
+       ret = _ctsvc_image_encode(src, dest, CTSVC_IMAGE_THUMBNAIL_SIZE);
+       if (CONTACTS_ERROR_NONE != ret) {
+               ERR("_ctsvc_image_encode() Fail(%d)", ret);
+               free(thumbnail_path);
+               return NULL;
+       }
+
+       return strdup(thumbnail_path);
+}
+
 int ctsvc_get_next_ver(void)
 {
        const char *query;
index d05862a..19ce400 100644 (file)
@@ -32,6 +32,9 @@ int ctsvc_get_transaction_ver(void);
 int ctsvc_utils_copy_image(const char *dir, const char *src, const char *file);
 void ctsvc_utils_make_image_file_name(int parent_id, int id, char *src_img, char *dest,
                int dest_size);
+char* ctsvc_utils_get_thumbnail_path(const char *image_path);
+char* ctsvc_utils_get_image_path(const char *thumbnail_path);
+char* ctsvc_utils_make_thumbnail(const char *image_path);
 
 int SAFE_SNPRINTF(char **buf, int *buf_size, int len, const char *src);