[Notification] added template feature 07/116007/1
authorPiotr Kosko <p.kosko@samsung.com>
Tue, 21 Feb 2017 13:12:19 +0000 (14:12 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Wed, 22 Feb 2017 09:43:20 +0000 (10:43 +0100)
[Feature] Template functionality was added to the notification module.
  Currently it is possible to save notification as a template and then
  new notifications could be created basing on the template.

[Verification] Code compiles without errors.
  TCT passrate is 100%.
  The feature was tested by hand in chrome console.

Change-Id: I8fc68f46b82165aa6c1a92750b3d87506611177c
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/notification/notification_api.js
src/notification/notification_instance.cc
src/notification/notification_instance.h
src/notification/notification_manager.cc
src/notification/notification_manager.h
src/notification/status_notification.cc

index 21bcadf..5c7ce37 100644 (file)
@@ -242,6 +242,42 @@ NotificationManager.prototype.stopLEDCustomEffect = function() {
   }
 };
 
+NotificationManager.prototype.saveNotificationAsTemplate = function(name, notification) {
+  var args = validator_.validateArgs(arguments, [
+    {name: 'name', type: types_.STRING},
+    {name: 'notification', type: types_.PLATFORM_OBJECT, values: StatusNotification}
+  ]);
+
+  var result = native_.callSync('NotificationManager_saveNotificationAsTemplate', args);
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+};
+
+NotificationManager.prototype.createNotificationFromTemplate = function(name) {
+  var args = validator_.validateArgs(arguments, [
+    {name: 'name', type: types_.STRING}
+  ]);
+
+  if (!arguments.length) {
+    throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
+  }
+
+  var result = native_.callSync('NotificationManager_createNotificationFromTemplate', args);
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+  var n = native_.getResultObject(result);
+
+  _edit.allow();
+  var returnObject = new StatusNotification(n.statusType, n.title, n);
+  _edit.disallow();
+
+  return returnObject;
+};
+
 
 function NotificationInitDict(data) {
   var _iconPath = null;
index ec92eb5..d4f06da 100644 (file)
@@ -52,6 +52,8 @@ NotificationInstance::NotificationInstance() {
       NotificationManagerPlayLEDCustomEffect);
   REGISTER_SYNC("NotificationManager_stopLEDCustomEffect",
       NotificationManagerStopLEDCustomEffect);
+  REGISTER_SYNC("NotificationManager_saveNotificationAsTemplate", NotificationManagerSaveTemplate);
+  REGISTER_SYNC("NotificationManager_createNotificationFromTemplate", NotificationManagerCreateFromTemplate);
 #undef REGISTER_SYNC
 
   manager_ = NotificationManager::GetInstance();
@@ -188,6 +190,37 @@ void NotificationInstance::NotificationManagerStopLEDCustomEffect(
   }
 }
 
+void NotificationInstance::NotificationManagerSaveTemplate(const picojson::value& args,
+                                                           picojson::object& out) {
+  LoggerD("Enter");
+  CHECK_PRIVILEGE_ACCESS(kPrivilegeNotification, &out);
+
+  PlatformResult status = manager_->SaveTemplate(args.get<picojson::object>());
+
+  if (status.IsSuccess()) {
+    ReportSuccess(out);
+  } else {
+    LogAndReportError(status, &out);
+  }
+}
+
+
+void NotificationInstance::NotificationManagerCreateFromTemplate(const picojson::value& args,
+                                                                 picojson::object& out) {
+  LoggerD("Enter");
+  CHECK_PRIVILEGE_ACCESS(kPrivilegeNotification, &out);
+  picojson::value val{picojson::object{}};
+
+  PlatformResult status =
+      manager_->CreateFromTemplate(args.get<picojson::object>(), val.get<picojson::object>());
+
+  if (status.IsSuccess()) {
+    ReportSuccess(val, out);
+  } else {
+    LogAndReportError(status, &out);
+  }
+}
+
 #undef CHECK_EXIST
 
 }  // namespace notification
index 3a1e2e7..4eef4f8 100644 (file)
@@ -48,6 +48,10 @@ class NotificationInstance : public common::ParsedInstance {
                                               picojson::object& out);
   void NotificationManagerStopLEDCustomEffect(const picojson::value& args,
                                               picojson::object& out);
+  void NotificationManagerSaveTemplate(const picojson::value& args,
+                                       picojson::object& out);
+  void NotificationManagerCreateFromTemplate(const picojson::value& args,
+                                             picojson::object& out);
 };
 
 }  // namespace notification
index 93f0c09..afaae6e 100644 (file)
@@ -244,5 +244,64 @@ PlatformResult NotificationManager::StopLEDCustomEffect() {
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
+common::PlatformResult NotificationManager::SaveTemplate(const picojson::object& args) {
+  LoggerD("Enter");
+  std::string name = FromJson<std::string>(args, "name");
+  notification_h noti_handle = nullptr;
+  int ret;
+  SCOPE_EXIT {
+    notification_free(noti_handle);
+  };
+
+  PlatformResult status = StatusNotification::GetNotiHandleFromJson(args, false, &noti_handle);
+
+  if (status.IsError()){
+    return status;
+  }
+
+  ret = notification_save_as_template(noti_handle, name.c_str());
+  if (ret != NOTIFICATION_ERROR_NONE) {
+    LoggerD("Error: %d (%s)", ret, get_error_message(ret));
+    if (ret == NOTIFICATION_ERROR_MAX_EXCEEDED) {
+      return LogAndCreateResult(ErrorCode::QUOTA_EXCEEDED_ERR,
+                                "Maximum number of templates exceeded",
+                                ("Maximum number of templates exceeded, error: %d", ret));
+    } else {
+      return LogAndCreateResult(ErrorCode::ABORT_ERR,
+                                "Saving template failed",
+                                ("Saving template failed, error: %d", ret));
+    }
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+
+common::PlatformResult NotificationManager::CreateFromTemplate(const picojson::object& args,
+                                                               picojson::object& out) {
+  LoggerD("Enter");
+  std::string name = FromJson<std::string>(args, "name");
+
+  notification_h noti_handle = nullptr;
+  noti_handle = notification_create_from_template(name.c_str());
+  if (!noti_handle) {
+    return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR,
+                              "The template with given name not found",
+                              ("The template with given name not found, handle is NULL"));
+  }
+
+  SCOPE_EXIT {
+    notification_free(noti_handle);
+  };
+
+  PlatformResult status = StatusNotification::ToJson(0, noti_handle, nullptr, &out);
+  if (status.IsError())
+  {
+    LoggerE("Failed: ToJson");
+    return status;
+  }
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
 }  // namespace notification
 }  // namespace extension
index eaeac9f..85cd85f 100644 (file)
@@ -41,6 +41,10 @@ class NotificationManager {
   common::PlatformResult PlayLEDCustomEffect(const picojson::object& args);
   common::PlatformResult StopLEDCustomEffect();
 
+  common::PlatformResult SaveTemplate(const picojson::object& args);
+  common::PlatformResult CreateFromTemplate(const picojson::object& args,
+                                            picojson::object& out);
+
  private:
   NotificationManager();
   virtual ~NotificationManager();
index c6d7bf3..e1ace9d 100644 (file)
@@ -1088,19 +1088,21 @@ PlatformResult StatusNotification::ToJson(int id,
     return status;
   out["vibration"] = picojson::value(vibration);
 
-  picojson::object app_control = picojson::object();
-  status = GetApplicationControl(app_handle, &app_control);
-  if (status.IsError())
-    return status;
-  if (app_control.size()) {
-    out["appControl"] = picojson::value(app_control);
-  }
+  if (app_handle) {
+    picojson::object app_control = picojson::object();
+    status = GetApplicationControl(app_handle, &app_control);
+    if (status.IsError())
+      return status;
+    if (app_control.size()) {
+      out["appControl"] = picojson::value(app_control);
+    }
 
-  status = GetApplicationId(app_handle, &value_str);
-  if (status.IsError())
-    return status;
-  if (value_str.length()) {
-    out["appId"] = picojson::value(value_str);
+    status = GetApplicationId(app_handle, &value_str);
+    if (status.IsError())
+      return status;
+    if (value_str.length()) {
+      out["appId"] = picojson::value(value_str);
+    }
   }
 
   std::string progress_type;