d582c94616b7be707087931b8d261ef99e8807c4
[platform/framework/web/crosswalk-tizen.git] / src / runtime / notification_manager.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "runtime/notification_manager.h"
6
7 #include <notification.h>
8 #include <memory>
9
10 #include "common/logger.h"
11
12 namespace wrt {
13
14 NotificationManager* NotificationManager::GetInstance() {
15   static NotificationManager instance;
16   return &instance;
17 }
18
19 NotificationManager::NotificationManager() {
20 }
21
22 bool NotificationManager::Show(uint64_t tag,
23                                const std::string& title,
24                                const std::string& body,
25                                const std::string& icon_path) {
26   auto found = keymapper_.find(tag);
27   if (found != keymapper_.end()) {
28     Hide(tag);
29   }
30
31   notification_h noti_h = NULL;
32   int ret = NOTIFICATION_ERROR_NONE;
33   noti_h = notification_new(
34       NOTIFICATION_TYPE_NOTI,
35       NOTIFICATION_GROUP_ID_DEFAULT,
36       NOTIFICATION_PRIV_ID_NONE);
37   if (noti_h == NULL) {
38     LOGGER(ERROR) << "Can't create notification handle";
39     return false;
40   }
41
42   std::unique_ptr<std::remove_pointer<notification_h>::type,
43                   decltype(notification_free)*>
44       auto_release {noti_h, notification_free};
45
46   // set notification title
47   ret = notification_set_text(
48       noti_h,
49       NOTIFICATION_TEXT_TYPE_TITLE,
50       title.c_str(),
51       NULL,
52       NOTIFICATION_VARIABLE_TYPE_NONE);
53   if (ret != NOTIFICATION_ERROR_NONE) {
54     LOGGER(ERROR) << "Can't set title";
55     return false;
56   }
57
58   // set notification content
59   ret = notification_set_text(
60       noti_h,
61       NOTIFICATION_TEXT_TYPE_CONTENT,
62       body.c_str(),
63       NULL,
64       NOTIFICATION_VARIABLE_TYPE_NONE);
65   if (ret != NOTIFICATION_ERROR_NONE) {
66     LOGGER(ERROR) << "Can't set content";
67     return false;
68   }
69
70   if (!icon_path.empty()) {
71     ret = notification_set_image(
72         noti_h,
73         NOTIFICATION_IMAGE_TYPE_ICON,
74         icon_path.c_str());
75     if (ret != NOTIFICATION_ERROR_NONE) {
76       LOGGER(ERROR) << "Can't set icon";
77       return false;
78     }
79   }
80
81   // insert notification
82   int platform_key = NOTIFICATION_PRIV_ID_NONE;
83   ret = notification_insert(noti_h, &platform_key);
84   if (ret != NOTIFICATION_ERROR_NONE) {
85     LOGGER(ERROR) << "Can't insert notification";
86     return false;
87   }
88   keymapper_[tag] = platform_key;
89   return true;
90 }
91
92 bool NotificationManager::Hide(uint64_t tag) {
93   auto found = keymapper_.find(tag);
94   if (found == keymapper_.end()) {
95     LOGGER(ERROR) << "Can't find notification";
96     return false;
97   }
98   notification_delete_by_priv_id(NULL,
99                                  NOTIFICATION_TYPE_NOTI,
100                                  found->second);
101   keymapper_.erase(found);
102   return true;
103 }
104
105
106 }  // namespace wrt