3.0 Migration
[platform/core/messaging/msg-service.git] / framework / submit-handler / MsgSubmitHandler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include <stdio.h>
18 #include <string.h>
19
20 #include "MsgDebug.h"
21 #include "MsgUtilFile.h"
22 #include "MsgUtilStorage.h"
23 #include "MsgException.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgPluginManager.h"
26 #include "MsgStorageHandler.h"
27 #include "MsgSubmitHandler.h"
28
29
30
31 /*==================================================================================================
32                                      FUNCTION IMPLEMENTATION
33 ==================================================================================================*/
34 msg_error_t MsgSubmitReq(MSG_REQUEST_INFO_S *pReqInfo, bool bScheduled)
35 {
36         msg_error_t err = MSG_SUCCESS;
37         static int reqId = 1;
38
39         pReqInfo->reqId = reqId;
40         reqId++;
41
42         MSG_DEBUG("==== Msg ID = [%d] ====", pReqInfo->msgInfo.msgId);
43         MSG_DEBUG("==== Folder ID = [%d] ====", pReqInfo->msgInfo.folderId);
44         MSG_INFO("==== Main Type = [%d] ====", pReqInfo->msgInfo.msgType.mainType);
45         MSG_INFO("==== Sub Type = [%d] ====", pReqInfo->msgInfo.msgType.subType);
46         MSG_DEBUG("==== Class Type = [%d] ====", pReqInfo->msgInfo.msgType.classType);
47         MSG_DEBUG("==== Message Data = [%s] ====", pReqInfo->msgInfo.msgData);
48         MSG_SEC_DEBUG("==== Message Text = [%s] ====", pReqInfo->msgInfo.msgText);
49         MSG_INFO("==== SIM Index = [%d] ====", pReqInfo->msgInfo.sim_idx);
50
51         MSG_DEBUG("==== bSetting = [%d] ====", pReqInfo->sendOptInfo.bSetting);
52
53         if (pReqInfo->msgInfo.msgType.mainType == MSG_SMS_TYPE)
54         {
55                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
56                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
57                 MSG_DEBUG("==== bReplyPath = [%d] ====", pReqInfo->sendOptInfo.option.smsSendOptInfo.bReplyPath);
58
59                 err = MsgSubmitReqSMS(pReqInfo);
60         }
61         else if (pReqInfo->msgInfo.msgType.mainType == MSG_MMS_TYPE)
62         {
63                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
64                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
65                 MSG_DEBUG("==== bReadReq = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.bReadReq);
66                 MSG_DEBUG("==== priority = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.priority);
67                 MSG_DEBUG("==== expiryTime = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.expiryTime.time);
68
69                 err = MsgSubmitReqMMS(pReqInfo);
70         }
71
72         return err;
73 }
74
75
76 msg_error_t MsgSubmitReqSMS(MSG_REQUEST_INFO_S *pReqInfo)
77 {
78         msg_error_t err = MSG_SUCCESS;
79
80         // submit request based on msgType;
81         MSG_MAIN_TYPE_T mainType = pReqInfo->msgInfo.msgType.mainType;
82         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
83
84         if (plg == NULL)
85                 return MSG_ERR_NULL_POINTER;
86
87         // If MSG ID > 0 -> MSG in DRAFT
88         // Move Folder to OUTBOX
89         if (pReqInfo->msgInfo.msgPort.valid == false) {
90                 pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
91
92         if (pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID))
93                 err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
94                 if (err != MSG_SUCCESS)
95                         return err;
96         }
97
98         err = plg->submitReq(pReqInfo);
99
100         return err;
101 }
102
103 msg_error_t MsgSubmitReqMMS(MSG_REQUEST_INFO_S *pReqInfo)
104 {
105         msg_error_t err = MSG_SUCCESS;
106         MsgDbHandler *dbHandle = getDbHandle();
107         MSG_RECIPIENTS_LIST_S pRecipientList;
108
109         MsgPlugin *sms_plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
110
111         if (sms_plg == NULL){
112                 return MSG_ERR_NULL_POINTER;
113         }
114
115         int defaultNetworkSimId = 0;
116
117         err = sms_plg->getDefaultNetworkSimId(&defaultNetworkSimId);
118         if (err != MSG_SUCCESS) {
119                 MSG_ERR("getDefaultNetworkSimId is failed=[%d]", err);
120                 return err;
121         } else if (pReqInfo->msgInfo.sim_idx != defaultNetworkSimId) {
122                 MSG_ERR("It is not default network SIM ID, request SIM=[%d], activated SIM=[%d]", pReqInfo->msgInfo.sim_idx, defaultNetworkSimId);
123                 return MSG_ERR_INVALID_PARAMETER;
124
125         }
126
127         MSG_MAIN_TYPE_T msgMainType = pReqInfo->msgInfo.msgType.mainType;
128         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
129
130         if(!plg)
131         {
132                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
133                 MSG_DEBUG("No Plugin for %d type", msgMainType);
134
135                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
136         }
137
138         //      If MSG ID > 0 -> MSG in DRAFT
139         // Move Folder to OUTBOX
140         /* reject_msg_support */
141         MSG_DEBUG("Not scheduled MMS, pReqInfo->msgInfo.msgType.subType [%d]", pReqInfo->msgInfo.msgType.subType);
142
143         if(pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_JAVA_MMS)
144         {
145                 char fileName[MAX_COMMON_INFO_SIZE+1] = {0};
146
147                 // copy whole of MMS PDU filepath to msgData
148                 strncpy(fileName, pReqInfo->msgInfo.msgData, MAX_COMMON_INFO_SIZE);
149                 memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
150                 snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s%s", MSG_IPC_DATA_PATH, fileName);
151
152                 MSG_SEC_DEBUG("JAVA MMS PDU filepath:%s", pReqInfo->msgInfo.msgData);
153
154                 // submit request
155                 err = plg->submitReq(pReqInfo);
156
157                 if(err != MSG_SUCCESS)
158                 {
159                         MSG_DEBUG("Update Network Status : [%d]", err);
160                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo),MSG_NETWORK_SEND_FAIL);
161                 }
162
163                 return err;
164         }
165         else if((pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_MMS) || (pReqInfo->msgInfo.msgType.subType == MSG_FORWARD_MMS))
166         {
167                 if(pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID)) {
168                         MSG_DEBUG("Not New Message.");
169                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
170                         err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
171                 } else {
172                         MSG_DEBUG("New Message.");
173                         pReqInfo->msgInfo.msgId = 0;
174                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
175                         err = MsgStoAddMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
176                 }
177         }
178         else if(pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS)
179         {
180                 msg_read_report_status_t readStatus;
181
182                 err = MsgStoCheckReadReportStatus(pReqInfo->msgInfo.msgId);
183                 if(err != MSG_SUCCESS) {
184
185                         MSG_INFO("msg id [%d], read report could NOT send [%d]", pReqInfo->msgInfo.msgId, err);
186                         return err;
187                 }
188
189                 memcpy(&readStatus, pReqInfo->msgInfo.msgData, sizeof(msg_read_report_status_t));
190                 if (readStatus == MSG_READ_REPORT_REJECT_BY_USER) {
191
192                         MSG_INFO("msg id [%d], read report reject by user", pReqInfo->msgInfo.msgId);
193
194                         err = MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_NO_SENT);
195
196                         return err;
197                 }
198
199                 err = MsgStoGetRecipientList(pReqInfo->msgInfo.msgId, &pRecipientList);
200                 if(err != MSG_SUCCESS)
201                         return MSG_ERR_PLUGIN_STORAGE;
202
203                 pReqInfo->msgInfo.nAddressCnt = pRecipientList.recipientCnt;
204                 pReqInfo->msgInfo.addressList = pRecipientList.recipientAddr;
205
206 //                      for(int i = 0; i < pRecipientList.recipientCnt; i++)
207 //                      {
208 //                              pReqInfo->msgInfo.addressList[i].addressType = pRecipientList.recipientAddr[i].addressType;
209 //                              pReqInfo->msgInfo.addressList[i].recipientType = MSG_RECIPIENTS_TYPE_TO;
210 //                              pReqInfo->msgInfo.addressList[i].contactId = pRecipientList.recipientAddr[i].contactId;
211 //                              strncpy(pReqInfo->msgInfo.addressList[i].addressVal, pRecipientList.recipientAddr[i].addressVal, MAX_ADDRESS_VAL_LEN);
212 //                      }
213
214                 char subject[MAX_SUBJECT_LEN+1];
215
216                 err = MsgStoGetSubject(pReqInfo->msgInfo.msgId, subject);
217                 if(err != MSG_SUCCESS)
218                         MSG_DEBUG("Getting subject returned not a MSG_SUCCESS");
219
220                 strncpy(pReqInfo->msgInfo.subject, subject, MAX_SUBJECT_LEN);
221
222                 err = plg->composeReadReport(&(pReqInfo->msgInfo));
223         }
224         else if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
225         {
226                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
227         }
228
229         /* reject_msg_support */
230
231         if(err != MSG_SUCCESS)
232         {
233                 MSG_DEBUG("Fail to Add/Update Message : MsgStoMoveMessageToFolder()/MsgStoAddMessage()");
234                 return err;
235         }
236
237         switch(pReqInfo->msgInfo.msgType.subType)
238         {
239                 case MSG_SENDREQ_MMS:
240                 case MSG_FORWARD_MMS:
241                         MsgDeleteFile(pReqInfo->msgInfo.msgData);
242                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
243                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s%d.mms", MSG_DATA_PATH, pReqInfo->msgInfo.msgId);
244                         break;
245
246                 case MSG_READREPLY_MMS:
247                 case MSG_READRECIND_MMS:
248                         break;
249                 default:
250                         break;
251         }
252
253         // update content location from db
254         if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
255                 err = MsgStoGetContentLocation(&(pReqInfo->msgInfo));
256         /* reject_msg_support */
257         else if(pReqInfo->msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS)
258                 err = plg->updateRejectStatus(&(pReqInfo->msgInfo));
259         /* reject_msg_support */
260
261         // Check SIM is present or not
262         char keyName[MAX_VCONFKEY_NAME_LEN];
263         memset(keyName, 0x00, sizeof(keyName));
264         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, pReqInfo->msgInfo.sim_idx);
265         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(keyName);
266
267         if(simStatus == MSG_SIM_STATUS_NOT_FOUND)
268         {
269                 MSG_DEBUG("SIM is not present...");
270                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
271
272                 return MSG_ERR_NO_SIM;
273         }
274
275         if(err == MSG_SUCCESS)
276                 err = plg->submitReq(pReqInfo);
277
278         if(err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
279                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
280
281         if (err != MSG_SUCCESS)
282         {
283                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
284                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
285                 else
286                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
287         }
288
289
290         return err;
291 }
292
293 msg_error_t MsgCancelReq(msg_request_id_t reqId)
294 {
295         msg_error_t err = MSG_SUCCESS;
296
297         return err;
298 }
299
300
301 msg_error_t MsgUpdateSentMsg(msg_message_id_t MsgId, msg_network_status_t Status)
302 {
303         msg_error_t err = MSG_SUCCESS;
304
305         bool bKeepCopy = true;
306
307 #ifdef MSG_MMS_KEEPCOPY
308         MSG_SENDINGOPT_INFO_S sendOpt = {};
309
310         if (msgType.mainType == MSG_MMS_TYPE)
311         {
312                 if (MsgStoGetMmsSendOpt(MsgId, &sendOpt) == MSG_SUCCESS)
313                 {
314                         bKeepCopy = sendOpt.bKeepCopy;
315                 }
316                 else
317                 {
318                         ret = MsgSettingGetBool(MSG_KEEP_COPY, &bKeepCopy);
319                 }
320         }
321
322         // Move Msg to SENTBOX
323         if (Status == MSG_NETWORK_SEND_SUCCESS)
324         {
325                 MSG_DEBUG("In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
326                 if (bKeepCopy == true)
327                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
328                 else
329                         err = MsgStoDeleteMessage(MsgId, false);
330         }
331 #else
332         // Move Msg to SENTBOX
333         if (Status == MSG_NETWORK_SEND_SUCCESS)
334         {
335                 MSG_DEBUG("In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
336                 if (bKeepCopy == true)
337                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
338         }
339 #endif
340         return err;
341 }
342
343
344 void MsgCopyReqInfo(MSG_REQUEST_INFO_S *pSrc, int addrIdx, MSG_REQUEST_INFO_S *pDest)
345 {
346         MSG_BEGIN();
347
348         memset(pDest, 0x00, sizeof(MSG_REQUEST_INFO_S));
349
350         // Copy Request ID
351         pDest->reqId = pSrc->reqId;
352
353         // Copy Msg Info
354         pDest->msgInfo.msgId = pSrc->msgInfo.msgId;
355
356         pDest->msgInfo.threadId = pSrc->msgInfo.threadId;
357
358         pDest->msgInfo.folderId = pSrc->msgInfo.folderId;
359
360         pDest->msgInfo.msgType.mainType = pSrc->msgInfo.msgType.mainType;
361         pDest->msgInfo.msgType.subType = pSrc->msgInfo.msgType.subType;
362         pDest->msgInfo.msgType.classType = pSrc->msgInfo.msgType.classType;
363
364         pDest->msgInfo.storageId = pSrc->msgInfo.storageId;
365
366         pDest->msgInfo.nAddressCnt = 1;
367
368         pDest->msgInfo.addressList[0].addressType = pSrc->msgInfo.addressList[addrIdx].addressType;
369         pDest->msgInfo.addressList[0].recipientType = pSrc->msgInfo.addressList[addrIdx].recipientType;
370         pDest->msgInfo.addressList[0].contactId = pSrc->msgInfo.addressList[addrIdx].contactId;
371         strncpy(pDest->msgInfo.addressList[0].addressVal, pSrc->msgInfo.addressList[addrIdx].addressVal, MAX_ADDRESS_VAL_LEN);
372         strncpy(pDest->msgInfo.addressList[0].displayName, pSrc->msgInfo.addressList[addrIdx].displayName, MAX_DISPLAY_NAME_LEN);
373
374         strncpy(pDest->msgInfo.replyAddress, pSrc->msgInfo.replyAddress, MAX_PHONE_NUMBER_LEN);
375         strncpy(pDest->msgInfo.subject, pSrc->msgInfo.subject, MAX_SUBJECT_LEN);
376
377         pDest->msgInfo.displayTime = pSrc->msgInfo.displayTime;
378         pDest->msgInfo.networkStatus = pSrc->msgInfo.networkStatus;
379         pDest->msgInfo.encodeType = pSrc->msgInfo.encodeType;
380         pDest->msgInfo.bRead = pSrc->msgInfo.bRead;
381         pDest->msgInfo.bProtected = pSrc->msgInfo.bProtected;
382         pDest->msgInfo.priority = pSrc->msgInfo.priority;
383         pDest->msgInfo.direction = pSrc->msgInfo.direction;
384
385         pDest->msgInfo.msgPort.valid = pSrc->msgInfo.msgPort.valid;
386
387         if (pDest->msgInfo.msgPort.valid == true)
388         {
389                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
390                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
391         }
392
393         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
394         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
395
396         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
397
398         if (pDest->msgInfo.bTextSms == true)
399         {
400                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
401                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
402         }
403
404         // Set Sending Info
405         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
406
407         if (pDest->sendOptInfo.bSetting == true)
408         {
409                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
410                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
411
412                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE)
413                 {
414                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
415                 }
416                 else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE)
417                 {
418                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
419                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
420
421                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
422                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
423
424                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
425                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
426                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
427                 }
428         }
429
430         MSG_END();
431 }
432