Change dummy object atspi role
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / notification_manager.cc
1 /*
2  * Copyright (c) 2015 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 "runtime/browser/notification_manager.h"
18
19 #include <notification.h>
20 #include <notification_internal.h>
21
22 #include <memory>
23
24 #include "common/logger.h"
25
26 namespace runtime {
27
28 NotificationManager* NotificationManager::GetInstance() {
29   static NotificationManager instance;
30   return &instance;
31 }
32
33 NotificationManager::NotificationManager() {
34 }
35
36 bool NotificationManager::Show(uint64_t tag,
37                                const std::string& title,
38                                const std::string& body,
39                                const std::string& icon_path) {
40   auto found = keymapper_.find(tag);
41   if (found != keymapper_.end()) {
42     Hide(tag);
43   }
44
45   notification_h noti_h = NULL;
46   int ret = NOTIFICATION_ERROR_NONE;
47   noti_h = notification_new(
48       NOTIFICATION_TYPE_NOTI,
49       NOTIFICATION_GROUP_ID_DEFAULT,
50       NOTIFICATION_PRIV_ID_NONE);
51   if (noti_h == NULL) {
52     LOGGER(ERROR) << "Can't create notification handle";
53     return false;
54   }
55
56   std::unique_ptr<std::remove_pointer<notification_h>::type,
57                   decltype(notification_free)*>
58       auto_release {noti_h, notification_free};
59
60   // set notification title
61   ret = notification_set_text(
62       noti_h,
63       NOTIFICATION_TEXT_TYPE_TITLE,
64       title.c_str(),
65       NULL,
66       NOTIFICATION_VARIABLE_TYPE_NONE);
67   if (ret != NOTIFICATION_ERROR_NONE) {
68     LOGGER(ERROR) << "Can't set title";
69     return false;
70   }
71
72   // set notification content
73   ret = notification_set_text(
74       noti_h,
75       NOTIFICATION_TEXT_TYPE_CONTENT,
76       body.c_str(),
77       NULL,
78       NOTIFICATION_VARIABLE_TYPE_NONE);
79   if (ret != NOTIFICATION_ERROR_NONE) {
80     LOGGER(ERROR) << "Can't set content";
81     return false;
82   }
83
84   if (!icon_path.empty()) {
85     ret = notification_set_image(
86         noti_h,
87         NOTIFICATION_IMAGE_TYPE_ICON,
88         icon_path.c_str());
89     if (ret != NOTIFICATION_ERROR_NONE) {
90       LOGGER(ERROR) << "Can't set icon";
91       return false;
92     }
93   }
94
95   // insert notification
96   int platform_key = NOTIFICATION_PRIV_ID_NONE;
97   ret = notification_insert(noti_h, &platform_key);
98   if (ret != NOTIFICATION_ERROR_NONE) {
99     LOGGER(ERROR) << "Can't insert notification";
100     return false;
101   }
102   keymapper_[tag] = platform_key;
103   return true;
104 }
105
106 bool NotificationManager::Hide(uint64_t tag) {
107   auto found = keymapper_.find(tag);
108   if (found == keymapper_.end()) {
109     LOGGER(ERROR) << "Can't find notification";
110     return false;
111   }
112   notification_delete_by_priv_id(NULL,
113                                  NOTIFICATION_TYPE_NOTI,
114                                  found->second);
115   keymapper_.erase(found);
116   return true;
117 }
118
119 }  // namespace runtime