modify terminology:calendar->book,allday->localtime,normal->utime,svc->service
[platform/core/pim/calendar-service.git] / server / cal_server_account.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 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
20 #include <pthread.h>
21 #include <account.h>
22
23 #include "calendar.h"
24 #include "cal_typedef.h"
25 #include "cal_internal.h"
26 #include "cal_db_plugin_book_helper.h"
27 #include "cal_server_contacts.h"
28
29 static pthread_mutex_t cal_mutex_account = PTHREAD_MUTEX_INITIALIZER;
30 static account_subscribe_h cal_account_h = NULL;
31
32 static bool _noti_cb(const char* event_type, int account_id,
33                 void* user_data)
34 {
35         CAL_FN_CALL();
36
37         if (CAL_STRING_EQUAL == strcmp(event_type, ACCOUNT_NOTI_NAME_DELETE)) {
38                 cal_db_delete_account(account_id);
39                 cal_server_contacts_delete(account_id);
40         }
41         return true;
42 }
43
44 int cal_server_account_init(void)
45 {
46         int ret = 0;
47
48         pthread_mutex_lock(&cal_mutex_account);
49         ret = account_subscribe_create(&cal_account_h);
50         if (ACCOUNT_ERROR_NONE != ret) {
51                 /* LCOV_EXCL_START */
52                 ERR("account_subscribe_create() Fail(%d)", ret);
53                 pthread_mutex_unlock(&cal_mutex_account);
54                 return CALENDAR_ERROR_SYSTEM;
55                 /* LCOV_EXCL_STOP */
56         }
57
58         ret = account_subscribe_notification(cal_account_h, _noti_cb, NULL);
59         if (ACCOUNT_ERROR_NONE != ret)
60                 WARN("account_subscribe_notification Failed (%d)", ret);
61
62         pthread_mutex_unlock(&cal_mutex_account);
63         return CALENDAR_ERROR_NONE;
64 }
65
66 void cal_server_account_deinit(void)
67 {
68         pthread_mutex_lock(&cal_mutex_account);
69
70         if (cal_account_h) {
71                 account_unsubscribe_notification(cal_account_h);
72                 cal_account_h = NULL;
73         }
74
75         pthread_mutex_unlock(&cal_mutex_account);
76 }
77