check alarm time
[platform/core/pim/calendar-service.git] / server / cal_server_ondemand.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
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_mutex.h"
25 #include "cal_server.h"
26
27 static pthread_mutex_t cal_mutex_timeout = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t cal_mutex_holding = PTHREAD_MUTEX_INITIALIZER;
29 static guint cal_timeout_id = 0;
30 static gboolean cal_holding = FALSE;
31
32 void cal_server_ondemand_stop(void)
33 {
34         int timeout = cal_server_get_timeout();
35         if (timeout < 1)
36                 return;
37
38         pthread_mutex_lock(&cal_mutex_timeout);
39         g_source_remove(cal_timeout_id);
40         cal_timeout_id = 0;
41         pthread_mutex_unlock(&cal_mutex_timeout);
42 }
43
44 static gboolean _timeout_cb(gpointer user_data)
45 {
46         pthread_mutex_lock(&cal_mutex_holding);
47         if (FALSE == cal_holding) {
48                 DBG("exit");
49                 cal_server_quit_loop();
50         }
51         pthread_mutex_unlock(&cal_mutex_holding);
52         return TRUE;
53 }
54
55 void cal_server_ondemand_start(void)
56 {
57         int timeout = cal_server_get_timeout();
58         if (timeout < 1)
59                 return;
60
61         DBG("timeout(%d)", timeout);
62         pthread_mutex_lock(&cal_mutex_timeout);
63         if (cal_timeout_id > 0)
64                 g_source_remove(cal_timeout_id);
65         cal_timeout_id = g_timeout_add_seconds(timeout, _timeout_cb, NULL);
66         pthread_mutex_unlock(&cal_mutex_timeout);
67 }
68
69 void cal_server_ondemand_hold(void)
70 {
71         pthread_mutex_lock(&cal_mutex_holding);
72         cal_holding = TRUE;
73         pthread_mutex_unlock(&cal_mutex_holding);
74 }
75
76 void cal_server_ondemand_release(void)
77 {
78         pthread_mutex_lock(&cal_mutex_holding);
79         cal_holding = FALSE;
80         pthread_mutex_unlock(&cal_mutex_holding);
81 }