tizen 2.3 release
[framework/system/deviced.git] / src / ode / noti.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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 <stdio.h>
21
22 #include "core/log.h"
23 #include "core/edbus-handler.h"
24 #include "noti.h"
25
26 #define METHOD_COMPLETE_NOTI_ON         "CompNotiOn"
27 #define METHOD_PROGRESS_NOTI_ON         "ProgNotiOn"
28 #define METHOD_PROGRESS_NOTI_OFF        "ProgNotiOff"
29 #define METHOD_PROGRESS_NOTI_UPDATE     "ProgNotiUpdate"
30 #define METHOD_ERROR_NOTI_ON            "ErrorNotiOn"
31 #define METHOD_ERROR_NOTI_OFF           "ErrorNotiOff"
32
33 #define RETRY_CNT       3
34
35 static const char *ode_type_str[] = {
36         [ENCRYPT_TYPE] = "encrypt",
37         [DECRYPT_TYPE] = "decrypt",
38 };
39
40 static int progress_h;
41 static int error_h;
42
43 static int method_noti(const char *method, const char *sig, char *param[])
44 {
45         int ret, retry = RETRY_CNT;
46
47         while (retry--) {
48                 ret = dbus_method_sync(POPUP_BUS_NAME,
49                                 POPUP_PATH_ODE,
50                                 POPUP_INTERFACE_ODE,
51                                 method,
52                                 sig, param);
53                 if (ret > 0)
54                         break;
55         }
56
57         return ret;
58 }
59
60 int noti_progress_show(int type)
61 {
62         char str_type[32];
63         char *arr[1];
64         int ret;
65
66         /* If ongoing noti already exists, delete the noti */
67         if (progress_h > 0)
68                 noti_progress_clear();
69
70         /* If error noti already exists, delete the noti */
71         if (error_h > 0)
72                 noti_error_clear();
73
74         snprintf(str_type, sizeof(str_type), "%s", ode_type_str[type]);
75         arr[0] = str_type;
76
77         ret = method_noti(METHOD_PROGRESS_NOTI_ON, "s", arr);
78         if (ret < 0)
79                 return ret;
80
81         progress_h = ret;
82         _D("insert progress noti handle : %d", progress_h);
83         return 0;
84 }
85
86 int noti_progress_clear(void)
87 {
88         char str_h[32];
89         char *arr[1];
90         int ret;
91
92         if (progress_h <= 0) {
93                 _D("already ongoing noti clear");
94                 return 0;
95         }
96
97         snprintf(str_h, sizeof(str_h), "%d", progress_h);
98         arr[0] = str_h;
99
100         ret = method_noti(METHOD_PROGRESS_NOTI_OFF, "i", arr);
101         if (ret != 0)
102                 return ret;
103
104         _D("delete progress noti handle : %d", progress_h);
105         progress_h = 0;
106         return 0;
107 }
108
109 int noti_progress_update(int rate)
110 {
111         char str_h[32];
112         char str_r[32];
113         char *arr[2];
114
115         if (progress_h <= 0) {
116                 _E("need to create notification");
117                 return -ENOENT;
118         }
119
120         if (rate < 0 || rate > 100) {
121                 _E("Invalid parameter");
122                 return -EINVAL;
123         }
124
125         snprintf(str_h, sizeof(str_h), "%d", progress_h);
126         arr[0] = str_h;
127         snprintf(str_r, sizeof(str_r), "%d", rate);
128         arr[1] = str_r;
129
130         return method_noti(METHOD_PROGRESS_NOTI_UPDATE, "ii", arr);
131 }
132
133 int noti_finish_show(int type)
134 {
135         char str_type[32];
136         char *arr[1];
137         int ret;
138
139         snprintf(str_type, sizeof(str_type), "%s", ode_type_str[type]);
140         arr[0] = str_type;
141
142         return method_noti(METHOD_COMPLETE_NOTI_ON, "s", arr);
143 }
144
145 int noti_error_show(int type, int state, int val)
146 {
147         char str_type[32], str_state[32], str_val[32];
148         char *arr[3];
149         int ret;
150
151         /* If ongoing noti already exists, delete the noti */
152         if (error_h > 0)
153                 noti_error_clear();
154
155         snprintf(str_type, sizeof(str_type), "%s", ode_type_str[type]);
156         arr[0] = str_type;
157         snprintf(str_state, sizeof(str_state), "%d", state);
158         arr[1] = str_state;
159         snprintf(str_val, sizeof(str_val), "%d", val);
160         arr[2] = str_val;
161
162         ret = method_noti(METHOD_ERROR_NOTI_ON, "sii", arr);
163         if (ret < 0)
164                 return ret;
165
166         error_h = ret;
167         _D("insert error noti handle : %d", error_h);
168         return 0;
169 }
170
171 int noti_error_clear(void)
172 {
173         char str_h[32];
174         char *arr[1];
175         int ret;
176
177         if (error_h <= 0) {
178                 _D("already ongoing noti clear");
179                 return 0;
180         }
181
182         snprintf(str_h, sizeof(str_h), "%d", error_h);
183         arr[0] = str_h;
184
185         ret = method_noti(METHOD_ERROR_NOTI_OFF, "i", arr);
186         if (ret != 0)
187                 return ret;
188
189         _D("delete error noti handle : %d", error_h);
190         error_h = 0;
191         return 0;
192 }