38d1b2a4e5232a545cce31189e3435724585c9eb
[platform/core/pim/contacts-service.git] / server / ctsvc_server_change_subject.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 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
20 #include <pims-ipc.h>
21 #include <pims-ipc-svc.h>
22 #include <pims-ipc-data.h>
23
24 #include "contacts.h"
25 #include "ctsvc_internal.h"
26 #include "ctsvc_ipc_define.h"
27 #include "ctsvc_ipc_marshal.h"
28 #include "ctsvc_server_change_subject.h"
29
30 #define CTSVC_SUBSCRIBE_MAX_LEN 1024
31
32 #ifdef ENABLE_LOG_FEATURE
33 static __thread char *__phone_log_chanaged_info = NULL;
34 static __thread unsigned int __phone_log_buf_size = 0;
35 #endif /* ENABLE_LOG_FEATURE */
36
37 static __thread char *__person_changed_info = NULL;
38 static __thread unsigned int __person_buf_size = 0;
39
40 static gboolean __ctsvc_publish_changes_with_data(const char *view_uri, char *data)
41 {
42         pims_ipc_data_h indata = NULL;
43         if (NULL == data)
44                 return true;
45
46         indata = pims_ipc_data_create(0);
47         if (NULL == indata) {
48                 CTS_ERR("pims_ipc_data_create error\n");
49                 return false;
50         }
51
52         if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_string(data, indata)) {
53                 CTS_ERR("ctsvc_ipc_marshal_string() Fail");
54                 pims_ipc_data_destroy(indata);
55                 return false;
56         }
57
58         if (pims_ipc_svc_publish(CTSVC_IPC_SUBSCRIBE_MODULE, (char*)view_uri, indata) != 0) {
59                 CTS_ERR("pims_ipc_svc_publish error (%s)\n", view_uri);
60                 return false;
61         }
62
63         pims_ipc_data_destroy(indata);
64
65         return true;
66 }
67
68 void ctsvc_change_subject_publish_changed_info()
69 {
70         __ctsvc_publish_changes_with_data(_contacts_person._uri, __person_changed_info);
71 #ifdef ENABLE_LOG_FEATURE
72         __ctsvc_publish_changes_with_data(_contacts_phone_log._uri, __phone_log_chanaged_info);
73 #endif /* ENABLE_LOG_FEATURE */
74         ctsvc_change_subject_clear_changed_info();
75 }
76
77 void ctsvc_change_subject_clear_changed_info()
78 {
79 #ifdef ENABLE_LOG_FEATURE
80         free(__phone_log_chanaged_info);
81         __phone_log_chanaged_info = NULL;
82         __phone_log_buf_size = 0;
83 #endif /* ENABLE_LOG_FEATURE */
84
85         free(__person_changed_info);
86         __person_changed_info = NULL;
87         __person_buf_size = 0;
88 }
89
90 #ifdef ENABLE_LOG_FEATURE
91 void ctsvc_change_subject_add_changed_phone_log_id(contacts_changed_e type, int id)
92 {
93         CTS_FN_CALL;
94         int cur_len = 0;
95         int info_len = 0;
96         char changed_info[30] = {0};
97
98         if (NULL == __phone_log_chanaged_info) {
99                 __phone_log_chanaged_info = (char*)calloc(CTSVC_SUBSCRIBE_MAX_LEN, sizeof(char));
100                 __phone_log_buf_size = CTSVC_SUBSCRIBE_MAX_LEN;
101                 __phone_log_chanaged_info[0] = '\0';
102         }
103
104         info_len = snprintf(changed_info, sizeof(changed_info), "%d:%d,", type, id);
105         cur_len = strlen(__phone_log_chanaged_info);
106         CTS_DBG("%s(info_len : %d), %s(crrent_len : %d), max_len : %u",
107                 changed_info, info_len, __phone_log_chanaged_info, cur_len, __phone_log_buf_size);
108
109         if (__phone_log_buf_size <= (cur_len + info_len)) {
110                 __phone_log_buf_size *= 2;
111                 __phone_log_chanaged_info = realloc(__phone_log_chanaged_info, __phone_log_buf_size);
112         }
113         snprintf(__phone_log_chanaged_info + cur_len, __phone_log_buf_size - cur_len, "%s", changed_info);
114         CTS_DBG("%s", __phone_log_chanaged_info);
115 }
116 #endif /* ENABLE_LOG_FEATURE */
117
118 void ctsvc_change_subject_add_changed_person_id(contacts_changed_e type, int id)
119 {
120         CTS_FN_CALL;
121         int cur_len = 0;
122         int info_len = 0;
123         char changed_info[30] = {0};
124
125         if (NULL == __person_changed_info) {
126                 __person_changed_info = (char*)calloc(CTSVC_SUBSCRIBE_MAX_LEN, sizeof(char));
127                 __person_buf_size = CTSVC_SUBSCRIBE_MAX_LEN;
128                 __person_changed_info[0] = '\0';
129         }
130
131         info_len = snprintf(changed_info, sizeof(changed_info), "%d:%d,", type, id);
132         cur_len = strlen(__person_changed_info);
133         CTS_DBG("%s(info_len : %d), %s(crrent_len : %d), max_len : %u",
134                 changed_info, info_len, __person_changed_info, cur_len, __person_buf_size);
135
136         if (__person_buf_size <= (cur_len + info_len)) {
137                 __person_buf_size *= 2;
138                 __person_changed_info = realloc(__person_changed_info, __person_buf_size);
139         }
140         snprintf(__person_changed_info + cur_len, __person_buf_size - cur_len, "%s", changed_info);
141 }
142
143 void ctsvc_change_subject_publish_setting(const char *setting_id, int value)
144 {
145         pims_ipc_data_h indata = NULL;
146         indata = pims_ipc_data_create(0);
147         RETM_IF(NULL == indata, "pims_ipc_data_create() Fail");
148
149         if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(value, indata)) {
150                 CTS_ERR("ctsvc_ipc_marshal_int() Fail");
151                 pims_ipc_data_destroy(indata);
152                 return;
153         }
154
155         if (pims_ipc_svc_publish(CTSVC_IPC_SUBSCRIBE_MODULE, (char*)setting_id, indata) != 0)
156                 CTS_ERR("pims_ipc_svc_publish error (%s)", setting_id);
157
158         pims_ipc_data_destroy(indata);
159 }
160
161 void ctsvc_change_subject_publish_status(contacts_db_status_e status)
162 {
163         pims_ipc_data_h indata = NULL;
164         indata = pims_ipc_data_create(0);
165         RETM_IF(NULL == indata, "pims_ipc_data_create() Fail");
166
167         if (CONTACTS_ERROR_NONE != ctsvc_ipc_marshal_int(status, indata)) {
168                 CTS_ERR("ctsvc_ipc_marshal_int() Fail");
169                 pims_ipc_data_destroy(indata);
170                 return;
171         }
172
173         if (pims_ipc_svc_publish(CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_IPC_SERVER_DB_STATUS_CHANGED, indata) != 0)
174                 CTS_ERR("pims_ipc_svc_publish error(service status)");
175
176         pims_ipc_data_destroy(indata);
177 }
178