2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <notification.h>
27 #include <app_service.h>
28 #include <app_service_private.h>
29 #include <app_ui_notification.h>
35 #define LOG_TAG "TIZEN_N_UI_NOTIFICATION"
37 struct ui_notification_s {
48 static int ui_notification_error_handler(int error, const char *func, const char *on_error)
55 case NOTIFICATION_ERROR_NONE:
56 retcode = UI_NOTIFICATION_ERROR_NONE;
59 case NOTIFICATION_ERROR_INVALID_DATA:
60 retcode = UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
61 error_msg = "INVALID_PARAMETER";
64 case NOTIFICATION_ERROR_NO_MEMORY:
65 retcode = UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
66 error_msg = "OUT_OF_MEMORY";
69 case NOTIFICATION_ERROR_FROM_DB:
70 retcode = UI_NOTIFICATION_ERROR_DB_FAILED;
71 error_msg = "DB_FAILED";
74 case NOTIFICATION_ERROR_ALREADY_EXIST_ID:
75 retcode = UI_NOTIFICATION_ERROR_ALREADY_POSTED;
76 error_msg = "ALREADY_POSTED";
80 retcode = UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
81 error_msg = "INVALID_PARAMETER";
84 if (retcode != UI_NOTIFICATION_ERROR_NONE)
86 LOGE("[%s] %s(0x%08x) : %s", func, error_msg, retcode, on_error);
93 int ui_notification_create(bool ongoing, ui_notification_h *notification)
95 ui_notification_h notification_out;
97 if (notification == NULL)
99 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
100 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
103 notification_out = (ui_notification_h)malloc(sizeof(struct ui_notification_s));
105 if (notification_out == NULL)
107 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
108 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
111 notification_out->ongoing = ongoing;
112 notification_out->posted = false;
113 notification_out->icon = NULL;
114 notification_out->time = NULL;
115 notification_out->title = NULL;
116 notification_out->content = NULL;
117 notification_out->service = NULL;
119 *notification = notification_out;
121 return UI_NOTIFICATION_ERROR_NONE;
124 int ui_notification_destroy(ui_notification_h notification)
126 if (notification == NULL)
128 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
129 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
132 if (notification->core)
133 notification_free(notification->core);
135 if (notification->icon)
136 free(notification->icon);
138 if (notification->time)
139 free(notification->time);
141 if (notification->title)
142 free(notification->title);
144 if (notification->content)
145 free(notification->content);
147 if (notification->service)
148 service_destroy(notification->service);
152 return UI_NOTIFICATION_ERROR_NONE;
156 int ui_notification_set_icon(ui_notification_h notification, const char *path)
160 if (notification == NULL || path == NULL)
162 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
163 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
166 if (notification->posted == true)
168 LOGE("[%s] ALREADY_POSTED(0x%08x) : unable to change the notification", __FUNCTION__, UI_NOTIFICATION_ERROR_ALREADY_POSTED);
169 return UI_NOTIFICATION_ERROR_ALREADY_POSTED;
172 path_dup = strdup(path);
174 if (path_dup == NULL)
176 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
177 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
180 if (notification->icon != NULL)
182 free(notification->icon);
185 notification->icon = path_dup;
187 return UI_NOTIFICATION_ERROR_NONE;
190 int ui_notification_get_icon(ui_notification_h notification, char **path)
192 char *path_dup = NULL;
194 if (notification == NULL || path == NULL)
196 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
197 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
200 if (notification->icon != NULL)
202 path_dup = strdup(notification->icon);
204 if (path_dup == NULL)
206 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
207 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
213 return UI_NOTIFICATION_ERROR_NONE;
216 int ui_notification_set_time(ui_notification_h notification, struct tm *time)
220 if (notification == NULL || time == NULL)
222 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
223 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
226 if (notification->posted == true)
228 LOGE("[%s] ALREADY_POSTED(0x%08x) : unable to change the notification", __FUNCTION__, UI_NOTIFICATION_ERROR_ALREADY_POSTED);
229 return UI_NOTIFICATION_ERROR_ALREADY_POSTED;
232 time_dup = malloc(sizeof(struct tm));
234 if (time_dup == NULL)
236 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
237 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
240 memcpy(time_dup, time, sizeof(struct tm));
242 if (notification->time != NULL)
244 free(notification->time);
247 notification->time = time_dup;
249 return UI_NOTIFICATION_ERROR_NONE;
252 int ui_notification_get_time(ui_notification_h notification, struct tm **time)
254 struct tm *time_dup = NULL;
256 if (notification == NULL || time == NULL)
258 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
259 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
262 if (notification->time != NULL)
264 time_dup = malloc(sizeof(struct tm));
266 if (time_dup == NULL)
268 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
269 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
272 memcpy(time_dup, notification->time, sizeof(struct tm));
277 return UI_NOTIFICATION_ERROR_NONE;
280 int ui_notification_set_title(ui_notification_h notification, const char *title)
284 if (notification == NULL || title == NULL)
286 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
287 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
290 if (notification->posted == true)
292 LOGE("[%s] ALREADY_POSTED(0x%08x) : unable to change the notification", __FUNCTION__, UI_NOTIFICATION_ERROR_ALREADY_POSTED);
293 return UI_NOTIFICATION_ERROR_ALREADY_POSTED;
296 title_dup = strdup(title);
298 if (title_dup == NULL)
300 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
301 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
304 if (notification->title != NULL)
306 free(notification->title);
309 notification->title = title_dup;
311 return UI_NOTIFICATION_ERROR_NONE;
314 int ui_notification_get_title(ui_notification_h notification, char **title)
316 char *title_dup = NULL;
318 if (notification == NULL || title == NULL)
320 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
321 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
324 if (notification->title != NULL)
326 title_dup = strdup(notification->title);
328 if (title_dup == NULL)
330 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
331 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
337 return UI_NOTIFICATION_ERROR_NONE;
341 int ui_notification_set_content(ui_notification_h notification, const char *content)
345 if (notification == NULL || content == NULL)
347 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
348 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
351 if (notification->posted == true)
353 LOGE("[%s] ALREADY_POSTED(0x%08x) : unable to change the notification", __FUNCTION__, UI_NOTIFICATION_ERROR_ALREADY_POSTED);
354 return UI_NOTIFICATION_ERROR_ALREADY_POSTED;
357 content_dup = strdup(content);
359 if (content_dup == NULL)
361 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
362 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
365 if (notification->content != NULL)
367 free(notification->content);
370 notification->content = content_dup;
372 return UI_NOTIFICATION_ERROR_NONE;
375 int ui_notification_get_content(ui_notification_h notification, char **content)
377 char *content_dup = NULL;
379 if (notification == NULL || content == NULL)
381 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
382 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
385 if (notification->content != NULL)
387 content_dup = strdup(notification->content);
389 if (content_dup == NULL)
391 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
392 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
396 *content = content_dup;
398 return UI_NOTIFICATION_ERROR_NONE;
402 int ui_notification_set_service(ui_notification_h notification, service_h service)
405 service_h service_dup;
407 if (notification == NULL || service == NULL)
409 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
410 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
413 if (notification->posted == true)
415 LOGE("[%s] ALREADY_POSTED(0x%08x) : unable to change the notification", __FUNCTION__, UI_NOTIFICATION_ERROR_ALREADY_POSTED);
416 return UI_NOTIFICATION_ERROR_ALREADY_POSTED;
419 retcode = service_clone(&service_dup, service);
421 if (retcode != SERVICE_ERROR_NONE)
423 if (retcode == SERVICE_ERROR_OUT_OF_MEMORY)
425 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
426 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
430 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid service handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
431 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
435 if (notification->service != NULL)
437 service_destroy(notification->service);
440 notification->service = service_dup;
442 return UI_NOTIFICATION_ERROR_NONE;
445 int ui_notification_get_service(ui_notification_h notification, service_h *service)
448 service_h service_dup = NULL;
450 if (notification == NULL || service == NULL)
452 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
453 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
456 if (notification->service != NULL)
458 retcode = service_clone(&service_dup, notification->service);
460 if (retcode != SERVICE_ERROR_NONE)
462 if (retcode == SERVICE_ERROR_OUT_OF_MEMORY)
464 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
465 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
469 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid service handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
470 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
475 *service = service_dup;
477 return UI_NOTIFICATION_ERROR_NONE;
481 int ui_notification_post(ui_notification_h notification)
485 bundle *service_data;
487 if (notification == NULL)
489 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
490 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
493 if (notification->posted == true)
495 LOGE("[%s] ALREADY_POSTED(0x%08x) : unable to change the notification", __FUNCTION__, UI_NOTIFICATION_ERROR_ALREADY_POSTED);
496 return UI_NOTIFICATION_ERROR_ALREADY_POSTED;
499 // STEP 1: core handle
500 if (notification->ongoing == true)
502 core = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
506 core = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_DEFAULT, NOTIFICATION_PRIV_ID_NONE);
511 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_OUT_OF_MEMORY);
512 return UI_NOTIFICATION_ERROR_OUT_OF_MEMORY;
515 notification->core = core;
518 if (notification->icon != NULL)
522 if (stat(notification->icon, &st) < 0)
524 LOGE("[%s] NO_SUCH_FILE(0x%08x) : invalid icon", __FUNCTION__, UI_NOTIFICATION_ERROR_NO_SUCH_FILE);
525 return UI_NOTIFICATION_ERROR_NO_SUCH_FILE;
528 notification_set_image(core, NOTIFICATION_IMAGE_TYPE_ICON, notification->icon);
532 if (notification->time != NULL)
534 notification_set_time(core, mktime(notification->time));
538 if (notification->title != NULL)
540 notification_set_text(core, NOTIFICATION_TEXT_TYPE_TITLE, notification->title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
544 if (notification->content != NULL)
546 notification_set_text(core, NOTIFICATION_TEXT_TYPE_CONTENT, notification->content, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
550 if (notification->service != NULL && service_to_bundle(notification->service, &service_data) == SERVICE_ERROR_NONE)
552 notification_set_execute_option(core, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, service_data);
556 notification_set_property(core, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
560 retcode = ui_notification_error_handler(notification_insert(core, NULL), __FUNCTION__, "failed to post a notification");
562 if (retcode == UI_NOTIFICATION_ERROR_NONE)
564 notification->posted = true;
570 int ui_notification_update_progress(ui_notification_h notification, ui_notification_progress_type_e type, double value)
574 if (notification == NULL)
576 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
577 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
580 if (notification->core == NULL || notification->posted == false)
582 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
583 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
588 case UI_NOTIFICATION_PROGRESS_TYPE_SIZE:
589 retcode = ui_notification_error_handler(
590 notification_update_size(notification->core, NOTIFICATION_PRIV_ID_NONE, value),
591 __FUNCTION__, "failed to update the progress");
594 case UI_NOTIFICATION_PROGRESS_TYPE_PERCENTAGE:
595 retcode = ui_notification_error_handler(
596 notification_update_progress(notification->core, NOTIFICATION_PRIV_ID_NONE, value),
597 __FUNCTION__, "failed to update the progress");
601 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid progress type", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
602 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
608 int ui_notification_cancel(ui_notification_h notification)
612 if (notification == NULL)
614 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
615 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
618 if (notification->core == NULL || notification->posted == false)
620 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid handle", __FUNCTION__, UI_NOTIFICATION_ERROR_INVALID_PARAMETER);
621 return UI_NOTIFICATION_ERROR_INVALID_PARAMETER;
624 retcode = ui_notification_error_handler(
625 notification_delete(notification->core),
626 __FUNCTION__, "failed to cancel the notification");
631 int ui_notification_cancel_all(void)
633 return ui_notification_error_handler(
634 notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NONE),
635 __FUNCTION__, "failed to cancel the notification");