Tizen 2.1 base
[platform/framework/web/download-provider.git] / src / download-provider-notification.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <time.h>
18 #include <sys/time.h>
19 #include <stdlib.h>
20
21 #include "bundle.h"
22 #include "notification.h"
23 #include "appsvc.h"
24
25 #include "download-provider-notification.h"
26 #include "download-provider-request.h"
27 #include "download-provider-db.h"
28 #include "download-provider-log.h"
29
30 #include <libintl.h>
31 #define S_(s) dgettext("sys_string", s)
32
33 #define DP_NOTIFICATION_ICON_PATH IMAGE_DIR"/Q02_Notification_Download_failed.png"
34 /* This should be same value of SERVICE_OPERATION_DOWNLOAD_NOTIFICATION from download.h */
35 #define DP_DOWNLOAD_NOTI_OPERATION "http://tizen.org/appcontrol/operation/download_notification"
36
37 static void __print_app_error_message(int ret)
38 {
39         switch (ret) {
40         case APPSVC_RET_OK:
41                 TRACE_INFO("APPSVC_RET_OK");
42                 break;
43         case APPSVC_RET_ELAUNCH:
44                 TRACE_ERROR("APPSVC_RET_ELAUNCH");
45                 break;
46         case APPSVC_RET_ENOMATCH:
47                 TRACE_ERROR("APPSVC_RET_ENOMATCH");
48                 break;
49         case APPSVC_RET_EINVAL:
50                 TRACE_ERROR("APPSVC_RET_EINVAL");
51                 break;
52         case APPSVC_RET_ERROR:
53                 TRACE_ERROR("APPSVC_RET_ERROR");
54                 break;
55         }
56 }
57
58 static void __print_notification_error_message(int ret)
59 {
60         switch (ret) {
61         case NOTIFICATION_ERROR_INVALID_DATA:
62                 TRACE_ERROR("NOTIFICATION_ERROR_INVALID_DATA");
63                 break;
64         case NOTIFICATION_ERROR_NO_MEMORY:
65                 TRACE_ERROR("NOTIFICATION_ERROR_NO_MEMORY");
66                 break;
67         case NOTIFICATION_ERROR_FROM_DB:
68                 TRACE_ERROR("NOTIFICATION_ERROR_FROM_DB");
69                 break;
70         case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
71                 TRACE_ERROR("NOTIFICATION_ERROR_ALREADY_EXIST_ID");
72                 break;
73         case NOTIFICATION_ERROR_FROM_DBUS:
74                 TRACE_ERROR("NOTIFICATION_ERROR_FROM_DBUS");
75                 break;
76         case NOTIFICATION_ERROR_NOT_EXIST_ID:
77                 TRACE_ERROR("NOTIFICATION_ERROR_NOT_EXIST_ID");
78                 break;
79         default:
80                 TRACE_ERROR("Unknown error");
81                 break;
82         }
83 }
84
85 static char *__get_string_status(dp_state_type state)
86 {
87         char *message = NULL;
88         switch (state) {
89         case DP_STATE_COMPLETED:
90                 //message = S_("IDS_COM_POP_SUCCESS");
91                 message = "Completed";
92                 break;
93         case DP_STATE_CANCELED:
94                 //message = S_("IDS_COM_POP_CANCELLED");
95                 message = "Canceled";
96                 break;
97         case DP_STATE_FAILED:
98                 //message = S_("IDS_COM_POP_FAILED");
99                 message = "Failed";
100                 break;
101         default:
102                 break;
103         }
104         return message;
105 }
106
107 int dp_set_downloadinginfo_notification(int id, char *packagename)
108 {
109         notification_h noti_handle = NULL;
110         notification_error_e err = NOTIFICATION_ERROR_NONE;
111         int privId = 0;
112         bundle *b = NULL;
113
114 #ifdef NOTI_NEW_VERSION_API
115         noti_handle = notification_create(NOTIFICATION_TYPE_ONGOING);
116 #else
117         noti_handle = notification_new(NOTIFICATION_TYPE_ONGOING,
118                 NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
119 #endif
120
121         if (!noti_handle) {
122                 TRACE_ERROR("[FAIL] create notification handle");
123                 return -1;
124         }
125
126         char *content_name =
127                 dp_db_get_text_column
128                         (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_CONTENT_NAME);
129
130         if (content_name == NULL)
131                 content_name = strdup("No Name");
132
133         err = notification_set_text(noti_handle,
134                         NOTIFICATION_TEXT_TYPE_TITLE, content_name, NULL,
135                         NOTIFICATION_VARIABLE_TYPE_NONE);
136         if (content_name)
137                 free(content_name);
138
139         if (err != NOTIFICATION_ERROR_NONE) {
140                 TRACE_ERROR("[FAIL] set title [%d]", err);
141                 notification_free(noti_handle);
142                 return -1;
143         }
144
145         err = notification_set_image(noti_handle,
146                         NOTIFICATION_IMAGE_TYPE_ICON, DP_NOTIFICATION_ICON_PATH);
147         if (err != NOTIFICATION_ERROR_NONE) {
148                 TRACE_ERROR("[FAIL] set icon [%d]", err);
149                 notification_free(noti_handle);
150                 return -1;
151         }
152
153         b = bundle_create();
154         if (!b) {
155                 TRACE_ERROR("[FAIL] create bundle");
156                 notification_free(noti_handle);
157                 return -1;
158         }
159
160         if (packagename &&
161                 appsvc_set_pkgname(b, packagename) != APPSVC_RET_OK) {
162                 TRACE_ERROR("[FAIL] set pkg name");
163                 bundle_free(b);
164                 notification_free(noti_handle);
165                 return -1;
166         }
167
168         if (appsvc_set_operation(b, DP_DOWNLOAD_NOTI_OPERATION) !=
169                 APPSVC_RET_OK) {
170                 TRACE_ERROR("[FAIL] set noti operation");
171                 bundle_free(b);
172                 notification_free(noti_handle);
173                 return -1;
174         }
175
176         char *extra_key =
177                 dp_db_get_text_column(id, DP_DB_TABLE_NOTIFICATION,
178                         DP_DB_COL_EXTRA_KEY);
179         char *extra_value =
180                 dp_db_get_text_column(id, DP_DB_TABLE_NOTIFICATION,
181                         DP_DB_COL_EXTRA_VALUE);
182
183         if (extra_key && extra_value) {
184                 if (appsvc_add_data(b, extra_key, extra_value) != APPSVC_RET_OK) {
185                         TRACE_ERROR("[FAIL] set add data");
186                         free(extra_key);
187                         free(extra_value);
188                         bundle_free(b);
189                         notification_free(noti_handle);
190                         return -1;
191                 }
192         }
193         if (extra_key)
194                 free(extra_key);
195         if (extra_value)
196                 free(extra_value);
197
198         err = notification_set_execute_option(noti_handle,
199                         NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
200
201         if (err != NOTIFICATION_ERROR_NONE) {
202                 TRACE_ERROR("[FAIL] set execute option [%d]", err);
203                 bundle_free(b);
204                 notification_free(noti_handle);
205                 return -1;
206         }
207         bundle_free(b);
208
209         err = notification_set_property(noti_handle,
210                         NOTIFICATION_PROP_DISABLE_TICKERNOTI);
211         if (err != NOTIFICATION_ERROR_NONE) {
212                 TRACE_ERROR("[FAIL] set property [%d]", err);
213                 notification_free(noti_handle);
214                 return -1;
215         }
216
217         err = notification_insert(noti_handle, &privId);
218         if (err != NOTIFICATION_ERROR_NONE) {
219                 TRACE_ERROR("[FAIL] set insert [%d]", err);
220                 notification_free(noti_handle);
221                 return -1;
222         }
223
224         TRACE_INFO("m_noti_id [%d]", privId);
225         notification_free(noti_handle);
226         return privId;
227 }
228
229 int dp_set_downloadedinfo_notification(int priv_id, int id, char *packagename, dp_state_type state)
230 {
231         notification_h noti_handle = NULL;
232         notification_error_e err = NOTIFICATION_ERROR_NONE;
233         int privId = 0;
234         bundle *b = NULL;
235
236         if (priv_id >= 0) {
237                 err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_ONGOING,
238                                 priv_id);
239                 if (err != NOTIFICATION_ERROR_NONE) {
240                         TRACE_ERROR("[FAIL] delete notification handle, err", err);
241                 }
242         }
243
244 #ifdef NOTI_NEW_VERSION_API
245         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
246 #else
247         noti_handle = notification_new(NOTIFICATION_TYPE_NOTI,
248                 NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
249 #endif
250
251         if (!noti_handle) {
252                 TRACE_ERROR("[FAIL] create notification handle");
253                 return -1;
254         }
255
256         char *content_name =
257                 dp_db_get_text_column
258                         (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_CONTENT_NAME);
259
260         if (content_name == NULL)
261                 content_name = strdup("No Name");
262
263         err = notification_set_text(noti_handle,
264                         NOTIFICATION_TEXT_TYPE_TITLE, content_name,
265                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
266         if (content_name)
267                 free(content_name);
268
269         if (err != NOTIFICATION_ERROR_NONE) {
270                 TRACE_ERROR("[FAIL] set title [%d]", err);
271                 notification_free(noti_handle);
272                 return -1;
273         }
274
275         err = notification_set_text(noti_handle,
276                         NOTIFICATION_TEXT_TYPE_CONTENT,
277                         __get_string_status(state), NULL,
278                         NOTIFICATION_VARIABLE_TYPE_NONE);
279         if (err != NOTIFICATION_ERROR_NONE) {
280                 TRACE_ERROR("[FAIL] set text [%d]", err);
281                 notification_free(noti_handle);
282                 return -1;
283         }
284         time_t tt = time(NULL);
285
286         err = notification_set_time(noti_handle, tt);
287         if (err != NOTIFICATION_ERROR_NONE) {
288                 TRACE_ERROR("[FAIL] set time [%d]", err);
289                 notification_free(noti_handle);
290                 return -1;
291         }
292
293         b = bundle_create();
294         if (!b) {
295                 TRACE_ERROR("[FAIL] create bundle");
296                 notification_free(noti_handle);
297                 return -1;
298         }
299
300         if (state == DP_STATE_COMPLETED) {
301                 if (appsvc_set_operation(b, APPSVC_OPERATION_VIEW) != APPSVC_RET_OK) {
302                         TRACE_ERROR("[FAIL] appsvc set operation");
303                         bundle_free(b);
304                         notification_free(noti_handle);
305                         return -1;
306                 }
307                 
308                 char *savedpath =
309                         dp_db_get_text_column
310                                 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_SAVED_PATH);
311                 if (savedpath && appsvc_set_uri(b, savedpath) !=
312                                 APPSVC_RET_OK) {
313                         TRACE_ERROR("[FAIL] appsvc set uri");
314                         free(savedpath);
315                         bundle_free(b);
316                         notification_free(noti_handle);
317                         return -1;
318                 }
319                 if (savedpath)
320                         free(savedpath);
321                 err = notification_set_execute_option(noti_handle,
322                                 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
323         } else if (state == DP_STATE_CANCELED || state == DP_STATE_FAILED) {
324                 if (appsvc_set_operation(b, DP_DOWNLOAD_NOTI_OPERATION) !=
325                         APPSVC_RET_OK) {
326                         TRACE_ERROR("[FAIL] set noti operation [%d]", err);
327                         bundle_free(b);
328                         notification_free(noti_handle);
329                         return -1;
330                 }
331
332                 char *extra_key =
333                         dp_db_get_text_column(id, DP_DB_TABLE_NOTIFICATION,
334                                 DP_DB_COL_EXTRA_KEY);
335                 char *extra_value =
336                         dp_db_get_text_column(id, DP_DB_TABLE_NOTIFICATION,
337                                 DP_DB_COL_EXTRA_VALUE);
338                 if (extra_key && extra_value) {
339                         if (appsvc_add_data(b, extra_key, extra_value) !=
340                                 APPSVC_RET_OK) {
341                                 TRACE_ERROR("[FAIL] set add data");
342                                 free(extra_key);
343                                 free(extra_value);
344                                 bundle_free(b);
345                                 notification_free(noti_handle);
346                                 return -1;
347                         }
348                 }
349                 if (extra_key)
350                         free(extra_key);
351                 if (extra_value)
352                         free(extra_value);
353
354                 if (packagename &&
355                         appsvc_set_pkgname(b, packagename) !=
356                         APPSVC_RET_OK) {
357                         TRACE_ERROR("[FAIL] set pkg name");
358                         bundle_free(b);
359                         notification_free(noti_handle);
360                         return -1;
361                 }
362                 err = notification_set_execute_option(noti_handle,
363                                 NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "View", NULL, b);
364         } else {
365                 TRACE_ERROR("[CRITICAL] invalid state");
366                 bundle_free(b);
367                 notification_free(noti_handle);
368                 return -1;
369         }
370
371         if (err != NOTIFICATION_ERROR_NONE) {
372                 TRACE_ERROR("[FAIL] set time [%d]", err);
373                 notification_free(noti_handle);
374                 bundle_free(b);
375                 return -1;
376         }
377
378         bundle_free(b);
379
380         err = notification_set_image(noti_handle, NOTIFICATION_IMAGE_TYPE_ICON,
381                         DP_NOTIFICATION_ICON_PATH);
382         if (err != NOTIFICATION_ERROR_NONE) {
383                 TRACE_ERROR("[FAIL] set icon [%d]", err);
384                 notification_free(noti_handle);
385                 return -1;
386         }
387
388         err = notification_set_property(noti_handle,
389                         NOTIFICATION_PROP_DISABLE_TICKERNOTI);
390         if (err != NOTIFICATION_ERROR_NONE) {
391                 TRACE_ERROR("[FAIL] set property [%d]", err);
392                 notification_free(noti_handle);
393                 return -1;
394         }
395
396         err = notification_insert(noti_handle, &privId);
397         if (err != NOTIFICATION_ERROR_NONE) {
398                 TRACE_ERROR("[FAIL] set insert [%d]", err);
399                 notification_free(noti_handle);
400                 return -1;
401         }
402
403         TRACE_INFO("m_noti_id [%d]", privId);
404         notification_free(noti_handle);
405         return privId;
406 }
407
408 void dp_update_downloadinginfo_notification(int priv_id, double received_size, double file_size)
409 {
410         notification_error_e err = NOTIFICATION_ERROR_NONE;
411         if (priv_id < 0) {
412                 TRACE_ERROR("[FAIL] Invalid priv_id[%d]", priv_id);
413                 return;
414         }
415
416         if (file_size > 0) {
417                 double progress;
418                 progress = received_size / file_size;
419                 err = notification_update_progress(NULL, priv_id, progress);
420                 if (err != NOTIFICATION_ERROR_NONE)
421                         TRACE_ERROR("[FAIL] update noti progress[%d]", err);
422         } else {
423                 err = notification_update_size(NULL, priv_id, received_size);
424                 if (err != NOTIFICATION_ERROR_NONE)
425                         TRACE_ERROR("[FAIL]  update noti progress[%d]", err);
426         }
427 }
428
429 void dp_clear_downloadinginfo_notification()
430 {
431         notification_error_e err = NOTIFICATION_ERROR_NONE;
432         err = notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
433         if (err != NOTIFICATION_ERROR_NONE) {
434                 TRACE_ERROR("[FAIL] clear noti [%d]", err);
435         }
436         return;
437 }