[Notification] Implement very first post api only with title, content text
authorpius.lee <pius.lee@samsung.com>
Wed, 28 Jan 2015 13:14:50 +0000 (22:14 +0900)
committerpius.lee <pius.lee@samsung.com>
Wed, 28 Jan 2015 13:15:59 +0000 (22:15 +0900)
[Verification] Test with custom web app.

Change-Id: Ifd5a9c958fe0493bdb770b1df3d927c13bdeeae4
Signed-off-by: pius.lee <pius.lee@samsung.com>
src/notification/notification_api.js
src/notification/notification_instance.cc

index 031c72c2c7ce0a8724143c0b9753f9602fc2b979..a0c04914a98c7ffb672591bd12b297ddff2edf3e 100644 (file)
@@ -9,6 +9,30 @@ var validator_ = xwalk.utils.validator;
 var types_ = validator_.Types;
 
 
+function callNative(cmd, args) {
+  var json = {'cmd': cmd, 'args': args};
+  var argjson = JSON.stringify(json);
+  var resultString = extension.internal.sendSyncMessage(argjson);
+  var result = JSON.parse(resultString);
+
+  if (typeof result !== 'object') {
+    throw new tizen.WebAPIException(tizen.WebAPIException.UNKNOWN_ERR);
+  }
+
+  if (result['status'] == 'success') {
+    if (result['result']) {
+      return result['result'];
+    }
+    return true;
+  } else if (result['status'] == 'error') {
+    var err = result['error'];
+    if (err) {
+      throw new tizen.WebAPIException(err.name, err.message);
+    }
+    return false;
+  }
+}
+
 function SetReadOnlyProperty(obj, n, v) {
   Object.defineProperty(obj, n, {'value': v, 'writable': false});
 }
@@ -36,23 +60,12 @@ function NotificationManager() {
 
 NotificationManager.prototype.post = function(notification) {
   var args = validator_.validateArgs(arguments, [
-    {'name': 'notification', 'type': types_.PLATFORM_OBJECT, 'values': ['Notification']}
+    {'name': 'notification', 'type': types_.PLATFORM_OBJECT, 'values': [StatusNotification]}
   ]);
 
   var nativeParam = notificationToNativeParam(notification);
-
-  var id = notification.id;
-  var type = notification.type;
-  var ptime = notification.postedTime;
-  var title = notification.title;
-  var content = notification.content;
-
-  var nativeParam = {
-    'type': notification.type,
-    'title' : notification.title,
-    'content' : notification.content
-  };
-
+  nativeParam['type'] = notification.type;
+  nativeParam['content'] = notification.content;
 
   try {
     var syncResult = callNative('NotificationManager_post', nativeParam);
@@ -202,8 +215,8 @@ function notificationToNativeParam(n) {
   }
 
   return {
-    'statusType': args.statusType,
-    'title': args.title,
+    'statusType': n.statusType,
+    'title': n.title,
     'content': n.content,
     'iconPath': n.iconPath,
     'soundPath': n.soundPath,
@@ -228,9 +241,9 @@ function notificationToNativeParam(n) {
 function StatusNotification(statusType, title, notificationInitDict) {
   // constructor of StatusNotification
 
-  SetHidedProperty(this, 'id', undefined);
+  SetReadOnlyProperty(this, 'id', undefined);
   SetReadOnlyProperty(this, 'type', 'STATUS');
-  SetHidedProperty(this, 'postedTime', undefined);
+  SetReadOnlyProperty(this, 'postedTime', undefined);
 
   this.title = title;
   this.content = notificationInitDict.content;
index c18ee8ebb02279348879071d8ee75949093adcde..243a1d1ba7443b48553d7cc79a3dc366b6bd9a6d 100644 (file)
@@ -2,17 +2,17 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "notification/notification_instance.h"
-
 #include <notification.h>
 
 #include <string>
 #include <functional>
 
+#include "notification/notification_instance.h"
 #include "common/picojson.h"
 #include "common/logger.h"
 #include "common/platform_exception.h"
 #include "common/typeutil.h"
+#include "common/scope_exit.h"
 
 namespace extension {
 namespace notification {
@@ -23,7 +23,10 @@ const std::string kPrivilegeNotification = "";
 
 }  // namespace
 
+using common::UnknownException;
 using common::TypeMismatchException;
+using common::ScopeExit;
+using common::operator+;
 
 NotificationInstance::NotificationInstance() {
   using std::placeholders::_1;
@@ -42,31 +45,66 @@ NotificationInstance::NotificationInstance() {
 NotificationInstance::~NotificationInstance() {
 }
 
-
-
-#define CHECK_EXIST(args, name, out) \
-    if (!args.contains(name)) {\
-      ReportError(TypeMismatchException(name" is required argument"), out);\
-      return;\
-    }
-
 using common::WIDLTypeValidator::WIDLType;
 using common::WIDLTypeValidator::IsType;
 
+#define WIDL_TYPE_CHECK(args, name, wtype, out) \
+    do { if (!IsType<wtype>(args, name)) {\
+      ReportError(TypeMismatchException(name" is not valid type."), out);\
+    }} while (0)
+
 void NotificationInstance::NotificationManagerPost(
     const picojson::value& args, picojson::object& out) {
-  CHECK_EXIST(args, "id", out)
-  CHECK_EXIST(args, "type", out)
-  CHECK_EXIST(args, "title", out)
 
-  bool check;
-  check = IsType<WIDLType::StringType>(args, "id");
-  check = IsType<WIDLType::StringType>(args, "type");
-  check = IsType<WIDLType::StringType>(args, "title");
+  WIDL_TYPE_CHECK(args, "type", WIDLType::StringType, out);
+  WIDL_TYPE_CHECK(args, "title", WIDLType::StringType, out);
 
+  int err;
   notification_type_e noti_type = NOTIFICATION_TYPE_NOTI;
-
   notification_h noti = notification_create(noti_type);
+  SCOPE_EXIT {
+    notification_free(noti);
+  };
+
+  /*
+  if (IsType<WIDLType::StringType>(args, "iconPath")) {
+    err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON,
+                           args.get("iconPath").get<std::string>().c_str());
+    if (err != NOTIFICATION_ERROR_NONE) {
+      LoggerE("Fail to set icon path. [%s]", get_error_message(err));
+      return;
+    }
+    LoggerD("iconPath : %s", args.get("iconPath").get<std::string>().c_str());
+  }
+  */
+
+  const std::string& title = args.get("title").get<std::string>();
+  err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
+                              title.c_str(),
+                              NULL,
+                              NOTIFICATION_VARIABLE_TYPE_NONE);
+  if (err != NOTIFICATION_ERROR_NONE) {
+    LoggerE("Fail to set title. [%s]", get_error_message(err));
+    return;
+  }
+
+  if (IsType<WIDLType::StringType>(args, "content")) {
+    const std::string& content = args.get("content").get<std::string>();
+    err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
+                                content.c_str(),
+                                NULL,
+                                NOTIFICATION_VARIABLE_TYPE_NONE);
+
+    if (err != NOTIFICATION_ERROR_NONE) {
+      LoggerE("Fail to set content. [%s]", get_error_message(err));
+      return;
+    }
+  }
+
+  err = notification_post(noti);
+  if (err != NOTIFICATION_ERROR_NONE) {
+    ReportError(UnknownException("failed to post notification"), out);
+  }
 }
 void NotificationInstance::NotificationManagerUpdate(
     const picojson::value& args, picojson::object& out) {
@@ -85,7 +123,7 @@ void NotificationInstance::NotificationManagerGetall(
 }
 
 
-#undef CHECK_EXIST
+#undef WIDL_TYPE_CHECK
 
 }  // namespace notification
 }  // namespace extension