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