[Messaging] SMS getMessageServices
authorMaciek Blim <m.blim@samsung.com>
Thu, 8 Jan 2015 12:14:38 +0000 (13:14 +0100)
committerMaciek Blim <m.blim@samsung.com>
Thu, 8 Jan 2015 13:34:19 +0000 (14:34 +0100)
Change-Id: I12bb11565c19741cd6dc8204aeccb634f11b690f
Signed-off-by: Maciek Blim <m.blim@samsung.com>
src/messaging/message_service_short_msg.cc [new file with mode: 0644]
src/messaging/message_service_short_msg.h [new file with mode: 0644]
src/messaging/messaging.gyp
src/messaging/messaging_manager.cc
src/messaging/messaging_manager.h

diff --git a/src/messaging/message_service_short_msg.cc b/src/messaging/message_service_short_msg.cc
new file mode 100644 (file)
index 0000000..dc648ba
--- /dev/null
@@ -0,0 +1,160 @@
+// Copyright 2014 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+#include "message_service_short_msg.h"
+#include "common/logger.h"
+#include "common/platform_exception.h"
+
+#include <tapi_common.h>
+#include <ITapiSim.h>
+#include <ITapiNetwork.h>
+
+//#include <JSWebAPIErrorFactory.h>
+//#include <JSWebAPIError.h>
+//#include <JSUtil.h>
+//#include "ShortMsgManager.h"
+//#include "JSMessage.h"
+
+//using namespace DeviceAPI::Common;
+
+namespace extension {
+namespace messaging {
+
+MessageServiceShortMsg::MessageServiceShortMsg(int id, MessageType msgType)
+        : MessageService(id,
+                msgType,
+                MessagingUtil::messageTypeToString(msgType))
+{
+    LoggerD("Entered");
+}
+
+MessageServiceShortMsg::~MessageServiceShortMsg()
+{
+    LoggerD("Entered");
+}
+
+static gboolean sendMessageThread(void* data)
+{
+    LoggerD("Entered");
+
+    auto callback = static_cast<MessageRecipientsCallbackData *>(data);
+    if (!callback) {
+        LoggerE("Callback is null");
+        throw common::UnknownException("Callback is null");
+    }
+
+    // TODO
+    //ShortMsgManager::getInstance().sendMessage(callback);
+    return FALSE;
+}
+
+void MessageServiceShortMsg::sendMessage(MessageRecipientsCallbackData *callback)
+{
+    if (!callback) {
+        LoggerE("Callback is null");
+        throw common::UnknownException("Callback is null");
+    }
+
+    if (m_msg_type != callback->getMessage()->getType()) {
+        LoggerE("Incorrect message type");
+        throw common::TypeMismatchException("Incorrect message type");
+    }
+
+    /*
+     * Set sim index.
+     * If user has set sim index manually, check sim index is valid.
+     * Otherwise, use default sim which is already set.
+     */
+    TelNetworkDefaultDataSubs_t default_sim =
+        TAPI_NETWORK_DEFAULT_DATA_SUBS_UNKNOWN;
+    TapiHandle *handle = tel_init(NULL);
+
+    int ret = tel_get_network_default_data_subscription(handle, &default_sim);
+    if (ret != TAPI_API_SUCCESS) {
+        LoggerE("Failed to find default sim index %d", ret);
+    }
+
+    LoggerD("Default sim index: %d", default_sim);
+    callback->setDefaultSimIndex(default_sim);
+    tel_deinit(handle);
+
+    // simIndex parameter is only available for sms message.
+    // In case of mms, the parameter is silently ignored.
+    if (callback->isSetSimIndex() &&
+        callback->getMessage()->getType() == MessageType::SMS) {
+        char **cp_list = tel_get_cp_name_list();
+        TelNetworkDefaultDataSubs_t sim_index = callback->getSimIndex();
+        int sim_count = 0;
+
+        if (cp_list) {
+            while (cp_list[sim_count]) {
+                sim_count++;
+            }
+            g_strfreev(cp_list);
+        } else {
+            LoggerD("Empty cp name list");
+        }
+
+        if (sim_index >= sim_count) {
+            LoggerE("Sim index out of count %d : %d", sim_index, sim_count);
+            throw common::InvalidValuesException("The index of sim is out of bound");
+        }
+
+        callback->getMessage()->setSimIndex(sim_index);
+    } else {
+        callback->getMessage()->setSimIndex(default_sim);
+    }
+
+    if(!g_idle_add(sendMessageThread, static_cast<void*>(callback))) {
+        LoggerE("g_idle_add fails");
+        throw common::UnknownException("Could not add task");
+    }
+}
+
+static gboolean loadMessageBodyTask(void* data)
+{
+    LoggerD("Entered");
+    MessageBodyCallbackData* callback = static_cast<MessageBodyCallbackData*>(data);
+    if(!callback) {
+        LoggerE("callback is NULL");
+        return FALSE;
+    }
+
+    try {
+        // TODO call success callback
+        //JSContextRef context = callback->getContext();
+        //JSObjectRef jsMessage = JSMessage::makeJSObject(context, callback->getMessage());
+        //callback->callSuccessCallback(jsMessage);
+    } catch (...) {
+        LoggerE("Couldn't create JSMessage object!");
+        // TODO call error callback
+        //callback->callErrorCallback();
+    }
+
+    return FALSE;
+}
+
+void MessageServiceShortMsg::loadMessageBody(MessageBodyCallbackData *callback)
+{
+    if (!callback) {
+        LoggerE("Callback is null");
+        throw common::UnknownException("Callback is null");
+    }
+
+    if (m_msg_type != callback->getMessage()->getType()) {
+        LoggerE("Incorrect message type");
+        throw common::TypeMismatchException("Incorrect message type");
+    }
+
+    guint id = g_idle_add(loadMessageBodyTask, static_cast<void*>(callback));
+    if (!id) {
+        LoggerE("g_idle_add fails");
+        throw common::UnknownException("Could not add task");
+    }
+}
+
+} // namespace messaging
+} // namespace extension
+
diff --git a/src/messaging/message_service_short_msg.h b/src/messaging/message_service_short_msg.h
new file mode 100644 (file)
index 0000000..2748993
--- /dev/null
@@ -0,0 +1,26 @@
+// Copyright 2014 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef __MESSAGING_MESSAGE_SERVICE_SHORT_MSG_H__
+#define __MESSAGING_MESSAGE_SERVICE_SHORT_MSG_H__
+
+#include "message_service.h"
+#include "messaging_util.h"
+
+namespace extension {
+namespace messaging {
+
+class MessageServiceShortMsg : public MessageService {
+public:
+    MessageServiceShortMsg(int id, MessageType msgType);
+    virtual ~MessageServiceShortMsg();
+
+    void sendMessage(MessageRecipientsCallbackData *callback);
+
+    virtual void loadMessageBody(MessageBodyCallbackData *callback);
+};
+
+} // namespace messaging
+} // namespace extension
+#endif // __MESSAGING_MESSAGE_SERVICE_SHORT_MSG_H__
index a5d32c4..de135e5 100644 (file)
         'conversations_change_callback.cc',
         'conversations_change_callback.h',
         'folders_change_callback.cc',
-        'folders_change_callback.h'
+        'folders_change_callback.h',
+        'message_service_short_msg.cc',
+        'message_service_short_msg.h'
       ],
       'includes': [
         '../common/pkg-config.gypi',
index 5b7498a..5111a1b 100755 (executable)
@@ -80,14 +80,32 @@ static void* getMsgServicesThread(const std::shared_ptr<MsgManagerCallbackData>&
         bool isSupported = false;
         switch (type) {
         case MessageType::SMS:
-            LoggerD("Currently unsupported");
-            // TODO add class which will extended message_service and call message_service_short_msg
+            LoggerD("MessageService for SMS");
+            {
+                MessageService* service = new(std::nothrow) MessageServiceShortMsg(
+                        MessageServiceAccountId::SMS_ACCOUNT_ID,
+                        MessageType::SMS);
+                if (!service) {
+                    LoggerE("MessageService for SMS creation failed");
+                    throw common::UnknownException("MessageService for email creation failed");
+                }
+
+                // TODO FIXME
+                picojson::array array;
+                array.push_back(picojson::value(service->toPicoJS()));
+                obj[JSON_DATA] = picojson::value(array);
+                obj[JSON_ACTION] = picojson::value(JSON_CALLBACK_SUCCCESS);
+
+                delete service;
+                service = NULL;
+            }
             break;
         case MessageType::MMS:
             LoggerD("Currently unsupported");
             // TODO add class which will extended message_service and call message_service_short_msg
             break;
         case MessageType::EMAIL:
+                // TODO FIXME need to work on readability of that case
                 if (email_get_account_list(&email_accounts, &count) != EMAIL_ERROR_NONE) {
                     LoggerE("Method failed: email_get_account_list()");
                     throw common::UnknownException("Error during getting account list");
index b584b44..a9de957 100755 (executable)
@@ -10,6 +10,7 @@
 #include <map>
 
 #include "message_service_email.h"
+#include "message_service_short_msg.h"
 
 namespace extension {
 namespace messaging {