Modify flora license version.
[platform/core/messaging/msg-service.git] / framework / deliver-handler / MsgDeliverHandler.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://floralicense.org/license/
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // for sl message browser launch
18 #include <app_service.h>
19
20 #include "MsgDebug.h"
21 #include "MsgUtilFile.h"
22 #include "MsgContact.h"
23 #include "MsgUtilStorage.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgSoundPlayer.h"
26 #include "MsgSpamFilter.h"
27 #include "MsgPluginManager.h"
28 #include "MsgStorageHandler.h"
29 #include "MsgDeliverHandler.h"
30 #include "MsgNotificationWrapper.h"
31
32 /*==================================================================================================
33                                      VARIABLES
34 ==================================================================================================*/
35 extern MsgDbHandler dbHandle;
36
37
38 /*==================================================================================================
39                                      FUNCTION IMPLEMENTATION
40 ==================================================================================================*/
41 msg_error_t MsgHandleMmsConfIncomingMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_request_id_t reqID)
42 {
43         MSG_BEGIN();
44
45         msg_error_t err = MSG_SUCCESS;
46
47         MSG_DEBUG(" msgtype subtype is [%d]", pMsgInfo->msgType.subType);
48
49         if (pMsgInfo->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgInfo->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS) {
50                 // keep origianl subType
51                 MSG_SUB_TYPE_T subType = pMsgInfo->msgType.subType;
52
53                 /* If Retrieve Failed, Msg SubType is remained as Notification Ind */
54                 if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_FAIL)
55                         pMsgInfo->msgType.subType = MSG_NOTIFICATIONIND_MMS;
56
57                 err = MsgStoUpdateMMSMessage(pMsgInfo);
58
59                 if (err == MSG_SUCCESS)
60                         MSG_DEBUG("Command Handle Success : MsgStoUpdateMessage()");
61
62                 if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
63                         MSG_DEBUG("### MSG_NETWORK_RETRIEVE_SUCCESS ###");
64                         //Update Mms Message to MMS Plugin DB
65
66                         // MMS Received Ind Process Func
67                         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(pMsgInfo->msgType.mainType);
68
69                         //Contents of msg Data was removed and replaced to retrievedFilePath
70                         // NOTICE:: now it was moved to handleEvent in MsgListnerThread
71                         err = plg->updateMessage(pMsgInfo, NULL, NULL);
72                         if (err != MSG_SUCCESS)
73                                 return MSG_ERR_STORAGE_ERROR;
74                 }
75
76                 MSG_DEBUG(" ######################### MESSAGE UPDATE COMPLETED!!! ######################");
77
78                 bool readStatus = false;
79
80                 MsgStoGetReadStatus(pMsgInfo->msgId, &readStatus);
81                 MSG_DEBUG("### readStatus = %d ###", readStatus);
82
83                 //Update Read Status to Unread beacaus Noti is Read
84                 if (subType == MSG_RETRIEVE_MANUALCONF_MMS && pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
85                         MSG_DEBUG("###2. subType = %d ###", pMsgInfo->msgType.subType);
86
87                         if (readStatus == true)
88                                 MsgStoSetReadStatus(&dbHandle, pMsgInfo->msgId, false);
89                 }
90
91                 // update badge
92                 if (subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
93                         int smsCnt = 0;
94                         int mmsCnt = 0;
95
96                         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
97                         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
98
99                         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
100                 }
101
102                 if (subType == MSG_RETRIEVE_AUTOCONF_MMS) {
103                         // play message-tone when MMS retrieved
104                         MsgSoundPlayStart(false);
105
106                         // add phone log
107                         MSG_DEBUG("Enter MsgAddPhoneLog() for mms message.");
108                         MsgAddPhoneLog(pMsgInfo);
109
110                         MsgInsertNoti(pMsgInfo);
111                 }  else if (subType == MSG_RETRIEVE_MANUALCONF_MMS) {
112                         if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
113                                 MSG_DEBUG("Manual success");
114                                 MsgInsertTicker("Message Retrieved", MESSAGE_RETRIEVED);
115                         } else if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_FAIL) {
116                                 MSG_DEBUG("Manual failed");
117                                 MsgInsertTicker("Retrieving message failed", RETRIEVING_MESSAGE_FAILED);
118                         }
119                 }
120         }
121         else if (pMsgInfo->msgType.subType == MSG_SENDREQ_MMS || pMsgInfo->msgType.subType == MSG_SENDCONF_MMS)
122         {
123                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(pMsgInfo->msgType.mainType);
124                 // change subType for storage update
125                 pMsgInfo->msgType.subType = MSG_SENDCONF_MMS;
126
127                 err = MsgStoUpdateMMSMessage(pMsgInfo);
128                 if (err == MSG_SUCCESS)
129                         MSG_DEBUG("Command Handle Success : MsgStoUpdateMMSMessage()");
130
131                 err = plg->updateMessage(pMsgInfo, NULL, NULL);
132                 if (err != MSG_SUCCESS)
133                         return MSG_ERR_STORAGE_ERROR;
134
135                 MSG_DEBUG("pMsg->networkStatus : %d", pMsgInfo->networkStatus);
136                 err = MsgStoUpdateNetworkStatus(&dbHandle, pMsgInfo, pMsgInfo->networkStatus);
137                 if (err != MSG_SUCCESS)
138                         return MSG_ERR_STORAGE_ERROR;
139
140                 if (pMsgInfo->networkStatus == MSG_NETWORK_SEND_SUCCESS)
141                 {
142                         err = MsgStoMoveMessageToFolder(pMsgInfo->msgId, MSG_SENTBOX_ID);
143                         if (err != MSG_SUCCESS)
144                                 return MSG_ERR_STORAGE_ERROR;
145                 }
146
147                 // Get subject and text
148                 MsgStoGetText(pMsgInfo->msgId, pMsgInfo->subject, pMsgInfo->msgText);
149
150                 MSG_DEBUG(" ######################### MESSAGE UPDATE COMPLETED!!! ######################");
151         }
152         return err;
153 }
154
155 msg_error_t MsgHandleIncomingMsg(MSG_MESSAGE_INFO_S *pMsgInfo, bool *pSendNoti)
156 {
157         MSG_BEGIN();
158
159         msg_error_t err = MSG_SUCCESS;
160
161         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
162
163                 bool bOnlyNoti = false;
164
165                 err = MsgHandleSMS(pMsgInfo, pSendNoti, &bOnlyNoti);
166
167                 if (err == MSG_SUCCESS&& ((*pSendNoti)||bOnlyNoti)) {
168                         MsgSoundPlayStart(false);
169                         if (*pSendNoti == true) {
170                                 int smsCnt = 0, mmsCnt = 0;
171
172                                 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
173                                 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
174
175                                 MsgSettingHandleNewMsg(smsCnt, mmsCnt);
176                                 MsgInsertNoti(pMsgInfo);
177                         } else if (bOnlyNoti == true) {
178                                 MsgInsertNoti(pMsgInfo);
179                         }
180                 }
181         }
182         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
183         {
184                 err = MsgHandleMMS(pMsgInfo, pSendNoti);
185         }
186
187         //Add Phone Log Data
188         if ((err == MSG_SUCCESS) &&
189                 (pMsgInfo->folderId == MSG_INBOX_ID || pMsgInfo->folderId == MSG_SPAMBOX_ID) &&
190                 (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) &&
191                 (pMsgInfo->msgType.subType != MSG_WAP_SL_SMS)) {
192
193                 MSG_DEBUG("Enter MsgAddPhoneLog() : pMsg->folderId [%d]", pMsgInfo->folderId);
194
195                 MsgAddPhoneLog(pMsgInfo);
196         }
197
198         MSG_END();
199
200         return err;
201 }
202
203
204 msg_error_t MsgHandleSMS(MSG_MESSAGE_INFO_S *pMsgInfo, bool *pSendNoti, bool *bOnlyNoti)
205 {
206         msg_error_t err = MSG_SUCCESS;
207
208         if (pMsgInfo->msgPort.valid == true) {
209                         *pSendNoti = false;
210                         *bOnlyNoti = false;
211                         return MSG_SUCCESS;
212         }
213         if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS) {
214                 if (MsgCheckFilter(&dbHandle, pMsgInfo) == true) {
215                         // Move to SpamBox
216                         err = MsgStoMoveMessageToFolder(pMsgInfo->msgId, MSG_SPAMBOX_ID);
217
218                         if (err != MSG_SUCCESS) {
219                                 MSG_DEBUG("MsgStoMoveMessageToFolder() Error : [%d]", err);
220                         } else {
221                                 pMsgInfo->folderId = MSG_SPAMBOX_ID;
222                         }
223
224                         // Update Conversation table
225                         err = MsgStoUpdateConversation(&dbHandle, pMsgInfo->threadId);
226
227                         if (err != MSG_SUCCESS)
228                                 MSG_DEBUG("MsgStoUpdateConversation() Error : [%d]", err);
229
230                         *pSendNoti = false;
231                         *bOnlyNoti = false;
232                 }
233         } else if ((pMsgInfo->msgType.subType >= MSG_WAP_SI_SMS) && (pMsgInfo->msgType.subType <= MSG_WAP_CO_SMS)) {
234                 MSG_DEBUG("Starting WAP Message Incoming.");
235
236                 MSG_PUSH_SERVICE_TYPE_T serviceType = (MSG_PUSH_SERVICE_TYPE_T)MsgSettingGetInt(PUSH_SERVICE_TYPE);
237                 service_h svc_handle = NULL;
238
239                 switch (pMsgInfo->msgType.subType) {
240                         case MSG_WAP_SL_SMS:
241                         {
242                                 *pSendNoti = false;
243
244                                 if (serviceType == MSG_PUSH_SERVICE_ALWAYS) {
245                                         if (service_create(&svc_handle) < 0) {
246                                                 MSG_DEBUG("Fail to create service handle");
247                                                 break;
248                                         }
249                                         if (!svc_handle) {
250                                                 MSG_DEBUG("Service handle is NULL");
251                                                 break;
252                                         }
253                                         if (service_set_operation(svc_handle, SERVICE_OPERATION_VIEW) < 0) {
254                                                 MSG_DEBUG("Fail to create service handle");
255                                                 service_destroy(svc_handle);
256                                                 break;
257                                         }
258                                         if (service_set_uri(svc_handle, pMsgInfo->msgText) < 0) {
259                                                 MSG_DEBUG("Fail to set uri");
260                                                 service_destroy(svc_handle);
261                                                 break;
262                                         }
263                                         if (service_set_package(svc_handle, MSG_SVC_PKG_NAME_BROWSER) < 0) {
264                                                 MSG_DEBUG("Fail to set package");
265                                                 service_destroy(svc_handle);
266                                                 break;
267                                         }
268                                         if (service_send_launch_request(svc_handle, NULL, NULL) < 0) {
269                                                 MSG_DEBUG("Fail to launch browser");
270                                                 service_destroy(svc_handle);
271                                                 break;
272                                         }
273
274                                         service_destroy(svc_handle);
275
276                                 } else if (serviceType == MSG_PUSH_SERVICE_PROMPT) {
277                                         char urlString[MAX_COMMAND_LEN+1];
278                                         memset(urlString, 0x00, sizeof(urlString));
279
280                                         snprintf(urlString, MAX_COMMAND_LEN, "/usr/apps/org.tizen.message/bin/message-dialog -m PUSH_MSG_ALWAYS_ASK -u %s &", pMsgInfo->msgText);
281
282                                         system(urlString);
283                                 }
284
285                         }
286                         break;
287
288                         case MSG_WAP_SI_SMS:
289                                 *pSendNoti = true;
290                                 break;
291                         case MSG_WAP_CO_SMS:
292                                 *pSendNoti = false;
293                                 break;
294                 }
295         } else if (pMsgInfo->msgType.subType == MSG_STATUS_REPORT_SMS) {
296                 *pSendNoti = false;
297                 *bOnlyNoti = true;
298         } else if (pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS && pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS) {
299                 if (pMsgInfo->bStore == false) {
300                         *pSendNoti = false;
301                         *bOnlyNoti = true;
302                 }
303         } 
304
305
306         return err;
307 }
308
309
310 msg_error_t MsgHandleMMS(MSG_MESSAGE_INFO_S *pMsgInfo,  bool *pSendNoti)
311 {
312         msg_error_t err = MSG_SUCCESS;
313
314         MSG_REQUEST_INFO_S request = {0};
315         bool bReject = false;
316         bool bFiltered = false;
317
318         // MMS Received Ind Process Func
319         MSG_MAIN_TYPE_T msgMainType = pMsgInfo->msgType.mainType;
320         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
321
322         // Need to process m-delivery-ind, m-notification-ind, m-read-orig-ind
323         err = plg->processReceivedInd(pMsgInfo, &request, &bReject);
324
325         if (err == MSG_SUCCESS) {
326                 MSG_DEBUG("Process Message Success : processReceivedInd(), btextsms %d", pMsgInfo->bTextSms);
327         } else {
328                 MSG_DEBUG("Process Message Fail : processReceivedInd()");
329                 return err;
330         }
331
332         // Add into DB
333         if ((pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) && bReject == false) {
334                 bFiltered = MsgCheckFilter(&dbHandle, pMsgInfo);
335
336                 if (bFiltered == true) {
337                         pMsgInfo->networkStatus = MSG_NETWORK_RETRIEVE_FAIL;
338                         *pSendNoti = false;
339                 }
340
341                 err = MsgStoAddMessage(pMsgInfo, NULL);
342
343                 if (err != MSG_SUCCESS) {
344                         MSG_DEBUG("MsgStoAddMessage() Error: [%d]", err);
345                         return err;
346                 }
347         } else if (pMsgInfo->msgType.subType == MSG_READORGIND_MMS || pMsgInfo->msgType.subType == MSG_DELIVERYIND_MMS) {
348                 if (MsgInsertMmsReportToNoti(&dbHandle, pMsgInfo) == MSG_SUCCESS) {
349                         MsgSoundPlayStart(false);
350                         *pSendNoti = false;
351                 }
352         }
353
354         //In the case of m-notification-ind, we should decide whether to send m-notify-response-ind or http 'Get'
355         //submit request
356         if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS && bFiltered == false) {
357                 if (request.msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS && bReject == false) {
358                         MsgSoundPlayStart(false);
359
360                         int smsCnt = 0;
361                         int mmsCnt = 0;
362
363                         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
364                         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
365
366                         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
367                         MsgInsertNoti(pMsgInfo);
368                 }
369
370                 request.msgInfo.msgId = pMsgInfo->msgId;
371
372                 MSG_DEBUG("-=====================[[[ %s ]]]] =========================", pMsgInfo->msgData);
373                 err = plg->submitReq(&request);
374
375                 if (err == MSG_SUCCESS) {
376                         MSG_DEBUG("Process Message Success : processReceivedInd()");
377                 } else {
378                         MSG_DEBUG("Process Message Fail : processReceivedInd()");
379                 }
380         }
381
382         return err;
383 }
384