[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_group_helper.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <ctype.h>
20 #include <unistd.h>
21
22 #include "contacts.h"
23 #include "ctsvc_internal.h"
24 #include "ctsvc_db_schema.h"
25 #include "ctsvc_db_sqlite.h"
26 #include "ctsvc_db_utils.h"
27 #include "ctsvc_db_plugin_group_helper.h"
28 #include "ctsvc_notify.h"
29 #include "ctsvc_normalize.h"
30 #include "ctsvc_server_setting.h"
31 #include "ctsvc_localize.h"
32
33 /*
34  * Whenever deleting group, this function will be called
35  * in order to deleting group image file
36  */
37 void ctsvc_db_group_delete_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
38 {
39         int ret;
40         const unsigned char *path;
41
42         if (1 < argc) {
43                 sqlite3_result_null(context);
44                 return;
45         }
46         path = sqlite3_value_text(argv[0]);
47
48         if (path) {
49                 char full_path[CTSVC_IMG_FULL_PATH_SIZE_MAX] = {0};
50                 snprintf(full_path, sizeof(full_path), "%s/%s", CTS_GROUP_IMAGE_LOCATION, path);
51                 ret = unlink(full_path);
52                 if (ret < 0)
53                         WARN("unlink() Fail(%d)", errno);
54         }
55
56         return;
57 }
58
59 static int _ctsvc_db_group_name_collation_str(const char *str1, const char *str2)
60 {
61         int ret;
62         int len1, len2;
63         char *str_dest1 = NULL;
64         char *str_dest2 = NULL;
65
66         ctsvc_collation_str((char *)str1, &str_dest1);
67         ctsvc_collation_str((char *)str2, &str_dest2);
68
69         len1 = strlen(str_dest1);
70         len2 = strlen(str_dest2);
71         if (len1 < len2)
72                 ret = strncmp(str_dest1, str_dest2, len1);
73         else
74                 ret = strncmp(str_dest1, str_dest2, len2);
75
76         free(str_dest1);
77         free(str_dest2);
78         return ret;
79 }
80
81 int ctsvc_db_group_name_sort_callback(void *context, int str1_len, const void *str1, int str2_len, const void *str2)
82 {
83         int ret;
84         int str1_sort_type;
85         int str2_sort_type;
86         char str_src1[CTSVC_STR_SHORT_LEN] = {0};
87         char str_src2[CTSVC_STR_SHORT_LEN] = {0};
88         int prim_sort  = ctsvc_get_primary_sort();
89
90         if (CTSVC_STR_SHORT_LEN <= str1_len)
91                 str1_len = CTSVC_STR_SHORT_LEN -1;
92         if (CTSVC_STR_SHORT_LEN <= str2_len)
93                 str2_len = CTSVC_STR_SHORT_LEN -1;
94
95         strncpy(str_src1, str1, str1_len);
96         strncpy(str_src2, str2, str2_len);
97
98         str1_sort_type = ctsvc_get_name_sort_type(str_src1);
99         str2_sort_type = ctsvc_get_name_sort_type(str_src2);
100
101         switch (str1_sort_type) {
102         case CTSVC_SORT_NUMBER:
103                 if (CTSVC_SORT_OTHERS == str2_sort_type)
104                         ret = 1;
105                 else if (CTSVC_SORT_NUMBER == str2_sort_type)
106                         ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2);
107                 else
108                         ret = -1;
109                 break;
110
111         case CTSVC_SORT_OTHERS:
112                 if (CTSVC_SORT_OTHERS == str2_sort_type)
113                         ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2);
114                 else
115                         ret = -1;
116                 break;
117
118         default:
119                 if (CTSVC_SORT_NUMBER >= str2_sort_type) {
120                         ret = 1;
121                 } else {
122                         if (str1_sort_type != str2_sort_type) {
123                                 if (str1_sort_type == prim_sort)
124                                         ret = -1;
125                                 else if (str2_sort_type == prim_sort)
126                                         ret = 1;
127                                 else
128                                         ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2);
129                         } else {
130                                 ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2);
131                         }
132                 }
133                 break;
134         }
135
136         return ret;
137 }
138