Fix uncheck return of vconf_get_bool
[platform/core/messaging/msg-service.git] / manager / src / msg-manager.cpp
index ef0d2e1..6deb06a 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+
+/*==================================================================================================
+                                         INCLUDE FILES
+==================================================================================================*/
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <glib.h>
@@ -16,6 +36,7 @@ extern "C"
 
 #include "msg.h"
 #include "msg_storage.h"
+#include "msg_transport.h"
 
 #include "msg-manager-contact.h"
 #include "msg-manager-debug.h"
@@ -23,17 +44,19 @@ extern "C"
 #include "msg-manager-sound.h"
 #include "msg-manager-util.h"
 
-/* below defines will be removed */
-#define EVENT_KEY_OUT_MSG_TYPE "msg_type"
-#define EVENT_KEY_OUT_MSG_ID "msg_id"
-
+/*==================================================================================================
+                                     VARIABLES
+==================================================================================================*/
 msg_handle_t msg_handle = NULL;
 cm_client_h cm_handle = NULL;
 
-pthread_mutex_t mx;
+pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
 static int job_cnt = 0;
 static bool terminated = false;
 
+/*==================================================================================================
+                            FUNCTION IMPLEMENTATION
+==================================================================================================*/
 static int _service_app_exit(void *data)
 {
        MSG_MGR_INFO("kill msg-manager");
@@ -64,21 +87,26 @@ bool service_app_create(void *data)
 
        int msg_server_ready = 0;
        for (int i = 0; i < 100; i++) {
-               vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &msg_server_ready);
-               if (msg_server_ready == 1) {
-                       int msg_err = msg_open_msg_handle(&msg_handle);
-                       if (msg_err != MSG_SUCCESS)
-                               MSG_MGR_DEBUG("msg_open_msg_handle() failed [%d]", msg_err);
-                       else
-                               MSG_MGR_DEBUG("msg_open_msg_handle() success");
-
-                       break;
-               } else {
-                       MSG_MGR_DEBUG("msg-server is not ready.");
-                       sleep(1);
+               if (vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &msg_server_ready) == 0) {
+                       if (msg_server_ready == 1) {
+                               int msg_err = msg_open_msg_handle(&msg_handle);
+                               if (msg_err != MSG_SUCCESS)
+                                       MSG_MGR_ERR("msg_open_msg_handle() failed [%d]", msg_err);
+                               else
+                                       MSG_MGR_INFO("msg_open_msg_handle() success");
+
+                               break;
+                       }
+                       else {
+                               MSG_MGR_INFO("msg-server is not ready.");
+                               sleep(1);
+                       }
                }
        }
 
+       if (msg_handle == NULL)
+               return false;
+
        MsgMgrInitNoti();
        initMsgMgrSoundPlayer();
        cm_init(&cm_handle);
@@ -97,126 +125,6 @@ void service_app_terminate(void *data)
        return;
 }
 
-void _incoming_msg_func(app_control_h app_control)
-{
-       MSG_MGR_BEGIN();
-
-       int ret = 0;
-       char *rcv_msg_type = NULL;
-       char *rcv_msg_id = NULL;
-
-       ret = app_control_get_extra_data(app_control, EVENT_KEY_MSG_ID, &rcv_msg_id);
-       if (ret != APP_CONTROL_ERROR_NONE || rcv_msg_id == NULL) {
-               MSG_MGR_ERR("app_control_get_extra_data failed");
-               return;
-       }
-
-       ret = app_control_get_extra_data(app_control, EVENT_KEY_MSG_TYPE, &rcv_msg_type);
-       if (ret != APP_CONTROL_ERROR_NONE || rcv_msg_type == NULL) {
-               MSG_MGR_ERR("app_control_get_extra_data failed");
-               g_free(rcv_msg_id);
-               return;
-       }
-
-       MSG_MGR_INFO("rcv_msg_type(%s), rcv_msg_id(%s)", rcv_msg_type, rcv_msg_id);
-
-       int msg_err = MSG_SUCCESS;
-       msg_message_id_t msg_id = atoi(rcv_msg_id);
-       msg_struct_t msg = NULL;
-       msg_struct_t opt = NULL;
-       contactInfo contact_info = {0,};
-       contact_info.msgId = msg_id;
-
-       msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
-       opt = msg_create_struct(MSG_STRUCT_SENDOPT);
-       msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
-       if (msg_err != MSG_SUCCESS) {
-               MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
-               return;
-       }
-
-       msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &contact_info.msgType);
-       msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &contact_info.folderId);
-       msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &contact_info.simIndex);
-       msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&contact_info.addrList);
-       msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, contact_info.subject, MAX_CONTACT_TEXT_LEN);
-       int msgSize = 0;
-       msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
-       if (msgSize > 0)
-               msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, contact_info.msgText, MAX_CONTACT_TEXT_LEN);
-
-       if ((contact_info.folderId == MSG_INBOX_ID || contact_info.folderId == MSG_SPAMBOX_ID)) {
-               MsgMgrAddPhoneLog(&contact_info);
-       }
-
-       msg_release_struct(&msg);
-       msg_release_struct(&opt);
-
-       g_free(rcv_msg_id);
-       g_free(rcv_msg_type);
-
-       MSG_MGR_END();
-}
-
-void _outgoing_msg_func(app_control_h app_control)
-{
-       MSG_MGR_BEGIN();
-
-       int ret = 0;
-       char *sent_msg_type = NULL;
-       char *sent_msg_id = NULL;
-
-       ret = app_control_get_extra_data(app_control, EVENT_KEY_OUT_MSG_ID, &sent_msg_id);
-       if (ret != APP_CONTROL_ERROR_NONE || sent_msg_id == NULL) {
-               MSG_MGR_ERR("app_control_get_extra_data failed");
-               return;
-       }
-
-       ret = app_control_get_extra_data(app_control, EVENT_KEY_OUT_MSG_TYPE, &sent_msg_type);
-       if (ret != APP_CONTROL_ERROR_NONE || sent_msg_type == NULL) {
-               MSG_MGR_ERR("app_control_get_extra_data failed");
-               g_free(sent_msg_id);
-               return;
-       }
-
-       MSG_MGR_INFO("sent_msg_type(%s) sent_msg_id(%s)", sent_msg_type, sent_msg_id);
-
-       int msg_err = MSG_SUCCESS;
-       msg_message_id_t msg_id = atoi(sent_msg_id);
-       msg_struct_t msg = NULL;
-       msg_struct_t opt = NULL;
-       contactInfo contact_info = {0,};
-       contact_info.msgId = msg_id;
-
-       msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
-       opt = msg_create_struct(MSG_STRUCT_SENDOPT);
-       msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
-       if (msg_err != MSG_SUCCESS) {
-               MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
-               return;
-       }
-
-       msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &contact_info.msgType);
-       msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &contact_info.folderId);
-       msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &contact_info.simIndex);
-       msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&contact_info.addrList);
-       msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, contact_info.subject, 100);
-       int msgSize = 0;
-       msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
-       if (msgSize > 0)
-               msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, contact_info.msgText, 100);
-
-       MsgMgrAddPhoneLog(&contact_info);
-
-       msg_release_struct(&msg);
-       msg_release_struct(&opt);
-
-       g_free(sent_msg_id);
-       g_free(sent_msg_type);
-
-       MSG_MGR_END();
-}
-
 void _refresh_noti_func(app_control_h app_control)
 {
        char *type = NULL;
@@ -231,6 +139,8 @@ void _refresh_noti_func(app_control_h app_control)
                MSG_MGR_DEBUG("type [%s]", type);
                if (g_strcmp0(type, "normal") == 0)
                        noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
+               else if (g_strcmp0(type, "failed") == 0)
+                       noti_type = MSG_MGR_NOTI_TYPE_FAILED;
 
                g_free(type);
        } else {
@@ -280,6 +190,8 @@ void _add_noti_func(app_control_h app_control)
                        noti_type = MSG_MGR_NOTI_TYPE_MWI;
                } else if (g_strcmp0(type, "class0") == 0) {
                        noti_type = MSG_MGR_NOTI_TYPE_CLASS0;
+               } else if (g_strcmp0(type, "read_report_sent") == 0) {
+                       noti_type = MSG_MGR_NOTI_TYPE_MMS_READ_REPORT_SENT;
                }
 
                g_free(type);
@@ -300,14 +212,18 @@ void _add_noti_func(app_control_h app_control)
        msg_struct_t msg = NULL;
        msg_struct_t opt = NULL;
        msg_list_handle_t addr_list = NULL;
-       MSG_MGR_MESSAGE_INFO_S msg_info = {0,};
+       MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
        msg_info.msgId = msg_id;
 
+       g_free(msgId);
+
        msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
        opt = msg_create_struct(MSG_STRUCT_SENDOPT);
        msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
        if (msg_err != MSG_SUCCESS) {
                MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
+               msg_release_struct(&msg);
+               msg_release_struct(&opt);
                return;
        }
 
@@ -330,8 +246,6 @@ void _add_noti_func(app_control_h app_control)
        msg_release_struct(&msg);
        msg_release_struct(&opt);
 
-       g_free(msgId);
-
        MsgMgrAddNotification(noti_type, &msg_info);
 }
 
@@ -407,14 +321,18 @@ void _add_report_noti_func(app_control_h app_control)
        msg_struct_t msg = NULL;
        msg_struct_t opt = NULL;
        msg_list_handle_t addr_list = NULL;
-       MSG_MGR_MESSAGE_INFO_S msg_info = {0,};
+       MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
        msg_info.msgId = msg_id;
 
+       g_free(msgId);
+
        msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
        opt = msg_create_struct(MSG_STRUCT_SENDOPT);
        msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
        if (msg_err != MSG_SUCCESS) {
                MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
+               msg_release_struct(&msg);
+               msg_release_struct(&opt);
                return;
        }
 
@@ -430,8 +348,6 @@ void _add_report_noti_func(app_control_h app_control)
        msg_release_struct(&msg);
        msg_release_struct(&opt);
 
-       g_free(msgId);
-
        MsgMgrAddReportNotification(noti_type, &msg_info);
 }
 
@@ -449,6 +365,11 @@ void _del_report_noti_func(app_control_h app_control)
        g_free(addr);
 }
 
+void _del_sent_read_report_noti_func(app_control_h app_control)
+{
+       MsgMgrDeleteSentReadReportNotification();
+}
+
 void _insert_only_active_noti_func(app_control_h app_control)
 {
        char *type;
@@ -480,14 +401,18 @@ void _insert_only_active_noti_func(app_control_h app_control)
        msg_struct_t msg = NULL;
        msg_struct_t opt = NULL;
        msg_list_handle_t addr_list = NULL;
-       MSG_MGR_MESSAGE_INFO_S msg_info = {0,};
+       MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
        msg_info.msgId = msg_id;
 
+       g_free(msgId);
+
        msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
        opt = msg_create_struct(MSG_STRUCT_SENDOPT);
        msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
        if (msg_err != MSG_SUCCESS) {
                MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
+               msg_release_struct(&msg);
+               msg_release_struct(&opt);
                return;
        }
 
@@ -509,8 +434,6 @@ void _insert_only_active_noti_func(app_control_h app_control)
        msg_release_struct(&msg);
        msg_release_struct(&opt);
 
-       g_free(msgId);
-
        MsgMgrInsertOnlyActiveNotification(noti_type, &msg_info);
 }
 
@@ -599,7 +522,7 @@ void _sound_play_start_func(app_control_h app_control)
        ret = app_control_get_extra_data(app_control, "address", &addr);
 
        if (addr) {
-               MSG_MGR_ADDRESS_INFO_S addr_info = {0,};
+               MSG_MGR_ADDRESS_INFO_S addr_info = {0, };
                snprintf(addr_info.addressVal, MAX_ADDRESS_VAL_LEN, "%s", addr);
 
                MsgMgrSoundPlayStart(&addr_info, sound_type);
@@ -615,6 +538,84 @@ void _change_pm_state_func(app_control_h app_control)
        MsgMgrChangePmState();
 }
 
+void _on_boot_func()
+{
+       MsgMgrOnBoot();
+}
+
+void _reply_msg_func(app_control_h app_control)
+{
+       char *text = NULL;
+       char *addr = NULL;
+       char *slot_id = NULL;
+
+       int ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_TEXT, &text);
+       if (ret != APP_CONTROL_ERROR_NONE || text == NULL) {
+               MSG_MGR_ERR("app_control_get_extra_data failed");
+               return;
+       }
+
+       ret = app_control_get_extra_data(app_control, "addr", &addr);
+       if (ret != APP_CONTROL_ERROR_NONE || addr == NULL) {
+               MSG_MGR_ERR("app_control_get_extra_data failed");
+               g_free(text);
+               return;
+       }
+
+       ret = app_control_get_extra_data(app_control, "slot_id", &slot_id);
+       if (ret != APP_CONTROL_ERROR_NONE || slot_id == NULL) {
+               MSG_MGR_ERR("app_control_get_extra_data failed");
+               g_free(text);
+               g_free(addr);
+               return ;
+       }
+
+       MSG_MGR_DEBUG("text [%s]", text);
+       MSG_MGR_DEBUG("addr [%s]", addr);
+       MSG_MGR_DEBUG("slot_id [%s]", slot_id);
+
+       msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
+       msg_struct_t opt = msg_create_struct(MSG_STRUCT_SENDOPT);
+
+       msg_struct_t addr_item = NULL;
+       msg_list_add_item(msg, MSG_MESSAGE_ADDR_LIST_HND, &addr_item);
+       msg_set_int_value(addr_item, MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
+       msg_set_int_value(addr_item, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_TO);
+       msg_set_str_value(addr_item, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr, strlen(addr));
+
+       msg_set_int_value(msg, MSG_MESSAGE_TYPE_INT, MSG_TYPE_SMS);
+       msg_set_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, atoi(slot_id));
+       msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, text, strlen(text));
+
+       msg_struct_t req = msg_create_struct(MSG_STRUCT_REQUEST_INFO);
+       msg_set_struct_handle(req, MSG_REQUEST_MESSAGE_HND, msg);
+       msg_set_struct_handle(req, MSG_REQUEST_SENDOPT_HND, opt);
+
+       int msg_err = msg_sms_send_message(msg_handle, req);
+       if (msg_err != MSG_SUCCESS)
+               MSG_MGR_ERR("msg_sms_send_message() failed [%d]", msg_err);
+
+       char *msg_id = NULL;
+       ret = app_control_get_extra_data(app_control, "msg_id", &msg_id);
+       if (ret != APP_CONTROL_ERROR_NONE || msg_id == NULL) {
+               MSG_MGR_ERR("app_control_get_extra_data failed");
+       } else {
+               msg_err = msg_update_read_status(msg_handle, atoi(msg_id), true);
+               if (msg_err != MSG_SUCCESS)
+                       MSG_MGR_ERR("msg_update_read_status() failed [%d]", msg_err);
+
+               g_free(msg_id);
+       }
+
+       msg_release_struct(&msg);
+       msg_release_struct(&opt);
+       msg_release_struct(&req);
+
+       g_free(text);
+       g_free(addr);
+       g_free(slot_id);
+}
+
 void service_app_control(app_control_h app_control, void *data)
 {
        MSG_MGR_INFO("service_app_control called");
@@ -632,13 +633,9 @@ void service_app_control(app_control_h app_control, void *data)
                if (g_strcmp0(operation, APP_CONTROL_OPERATION_DEFAULT) == 0) {
                        ret = app_control_get_extra_data(app_control, "cmd", &cmd);
                        if (ret == APP_CONTROL_ERROR_NONE && cmd) {
-                               MSG_MGR_DEBUG("cmd [%s]", cmd);
+                               MSG_MGR_INFO("cmd [%s]", cmd);
 
-                               if (g_strcmp0(cmd, "incoming_msg") == 0) {
-                                       _incoming_msg_func(app_control);
-                               } else if (g_strcmp0(cmd, "outgoing_msg") == 0) {
-                                       _outgoing_msg_func(app_control);
-                               } else if (g_strcmp0(cmd, "refresh_noti") == 0) {
+                               if (g_strcmp0(cmd, "refresh_noti") == 0) {
                                        _refresh_noti_func(app_control);
                                } else if (g_strcmp0(cmd, "add_noti") == 0) {
                                        _add_noti_func(app_control);
@@ -648,6 +645,8 @@ void service_app_control(app_control_h app_control, void *data)
                                        _add_report_noti_func(app_control);
                                } else if (g_strcmp0(cmd, "del_report_noti") == 0) {
                                        _del_report_noti_func(app_control);
+                               } else if (g_strcmp0(cmd, "del_sent_read_report_noti") == 0) {
+                                       _del_sent_read_report_noti_func(app_control);
                                } else if (g_strcmp0(cmd, "insert_only_active_noti") == 0) {
                                        _insert_only_active_noti_func(app_control);
                                } else if (g_strcmp0(cmd, "insert_ticker") == 0) {
@@ -656,9 +655,14 @@ void service_app_control(app_control_h app_control, void *data)
                                        _sound_play_start_func(app_control);
                                } else if (g_strcmp0(cmd, "change_pm_state") == 0) {
                                        _change_pm_state_func(app_control);
+                               } else if (g_strcmp0(cmd, "reply_msg") == 0) {
+                                       _reply_msg_func(app_control);
                                }
 
                                g_free(cmd);
+                       } else if (ret == APP_CONTROL_ERROR_KEY_NOT_FOUND) {
+                               MSG_MGR_INFO("on boot");
+                               _on_boot_func();
                        }
                }
                g_free(operation);
@@ -674,7 +678,7 @@ void service_app_control(app_control_h app_control, void *data)
 
 int main(int argc, char* argv[])
 {
-       service_app_lifecycle_callback_s event_callback = {0,};
+       service_app_lifecycle_callback_s event_callback = {0, };
 
        event_callback.create = service_app_create;
        event_callback.terminate = service_app_terminate;