check alarm time
[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 #include <stdbool.h>
23
24 #include "calendar.h"
25 #include "cal_typedef.h"
26 #include "cal_internal.h"
27 #include "cal_db_plugin_book_helper.h"
28 #include "cal_server_contacts.h"
29 #include "cal_server_profile.h"
30
31 static pthread_mutex_t cal_mutex_account = PTHREAD_MUTEX_INITIALIZER;
32 static account_subscribe_h cal_account_h = NULL;
33
34 #ifndef TIZEN_FEATURE_NO_CONTACTS
35 static bool _noti_cb(const char* event_type, int account_id,
36                 void* user_data)
37 {
38         CAL_FN_CALL();
39
40         if (CAL_STRING_EQUAL == strcmp(event_type, ACCOUNT_NOTI_NAME_DELETE)) {
41                 cal_db_delete_account(account_id);
42                 cal_server_contacts_delete(account_id);
43         }
44         return true;
45 }
46 #endif
47
48 int cal_server_account_init(void)
49 {
50         if (TIZEN_PROFILE_TV)
51                 return 0;
52
53         int ret = 0;
54
55         pthread_mutex_lock(&cal_mutex_account);
56         ret = account_subscribe_create(&cal_account_h);
57         if (ACCOUNT_ERROR_NONE != ret) {
58                 /* LCOV_EXCL_START */
59                 ERR("account_subscribe_create() Fail(%d)", ret);
60                 pthread_mutex_unlock(&cal_mutex_account);
61                 return CALENDAR_ERROR_SYSTEM;
62                 /* LCOV_EXCL_STOP */
63         }
64
65         ret = account_subscribe_notification(cal_account_h, _noti_cb, NULL);
66         if (ACCOUNT_ERROR_NONE != ret)
67                 WARN("account_subscribe_notification Failed (%d)", ret);
68
69         pthread_mutex_unlock(&cal_mutex_account);
70         return CALENDAR_ERROR_NONE;
71 }
72
73 void cal_server_account_deinit(void)
74 {
75         if (TIZEN_PROFILE_TV)
76                 return;
77
78         pthread_mutex_lock(&cal_mutex_account);
79
80         if (cal_account_h) {
81                 account_unsubscribe_notification(cal_account_h);
82                 cal_account_h = NULL;
83         }
84
85         pthread_mutex_unlock(&cal_mutex_account);
86 }
87