Sync with tizen 2.4
[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                 THROW(MsgException::PLUGIN_ERROR, "No plugin for %d type", mainType);
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         }
95
96         err = plg->submitReq(pReqInfo);
97
98         return err;
99 }
100
101 msg_error_t MsgSubmitReqMMS(MSG_REQUEST_INFO_S *pReqInfo)
102 {
103         msg_error_t err = MSG_SUCCESS;
104         MsgDbHandler *dbHandle = getDbHandle();
105         MSG_RECIPIENTS_LIST_S pRecipientList;
106
107         MsgPlugin *sms_plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
108
109         if (sms_plg == NULL){
110                 THROW(MsgException::PLUGIN_ERROR, "No plugin for %d type", MSG_SMS_TYPE);
111         }
112
113         int defaultNetworkSimId = 0;
114
115         err = sms_plg->getDefaultNetworkSimId(&defaultNetworkSimId);
116         if (err != MSG_SUCCESS) {
117                 MSG_ERR("getDefaultNetworkSimId is failed=[%d]", err);
118                 return err;
119         } else if (pReqInfo->msgInfo.sim_idx != defaultNetworkSimId) {
120                 MSG_ERR("It is not default network SIM ID, request SIM=[%d], activated SIM=[%d]", pReqInfo->msgInfo.sim_idx, defaultNetworkSimId);
121                 return MSG_ERR_INVALID_PARAMETER;
122
123         }
124
125         MSG_MAIN_TYPE_T msgMainType = pReqInfo->msgInfo.msgType.mainType;
126         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
127
128         if(!plg)
129         {
130                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
131                 MSG_DEBUG("No Plugin for %d type", msgMainType);
132
133                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
134         }
135
136         //      If MSG ID > 0 -> MSG in DRAFT
137         // Move Folder to OUTBOX
138         /* reject_msg_support */
139         MSG_DEBUG("Not scheduled MMS, pReqInfo->msgInfo.msgType.subType [%d]", pReqInfo->msgInfo.msgType.subType);
140
141         if(pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_JAVA_MMS)
142         {
143                 char fileName[MAX_COMMON_INFO_SIZE+1] = {0};
144
145                 // copy whole of MMS PDU filepath to msgData
146                 strncpy(fileName, pReqInfo->msgInfo.msgData, MAX_COMMON_INFO_SIZE);
147                 memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
148                 snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s%s", MSG_IPC_DATA_PATH, fileName);
149
150                 MSG_SEC_DEBUG("JAVA MMS PDU filepath:%s", pReqInfo->msgInfo.msgData);
151
152                 // submit request
153                 err = plg->submitReq(pReqInfo);
154
155                 if(err != MSG_SUCCESS)
156                 {
157                         MSG_DEBUG("Update Network Status : [%d]", err);
158                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo),MSG_NETWORK_SEND_FAIL);
159                 }
160
161                 return err;
162         }
163         else if((pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_MMS) || (pReqInfo->msgInfo.msgType.subType == MSG_FORWARD_MMS))
164         {
165                 if(pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID)) {
166                         MSG_DEBUG("Not New Message.");
167                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
168                         err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
169                 } else {
170                         MSG_DEBUG("New Message.");
171                         pReqInfo->msgInfo.msgId = 0;
172                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
173                         err = MsgStoAddMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
174                 }
175         }
176         else if(pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS)
177         {
178                 msg_read_report_status_t readStatus;
179
180                 err = MsgStoCheckReadReportStatus(pReqInfo->msgInfo.msgId);
181                 if(err != MSG_SUCCESS) {
182
183                         MSG_INFO("msg id [%d], read report could NOT send [%d]", pReqInfo->msgInfo.msgId, err);
184                         return err;
185                 }
186
187                 memcpy(&readStatus, pReqInfo->msgInfo.msgData, sizeof(msg_read_report_status_t));
188                 if (readStatus == MSG_READ_REPORT_REJECT_BY_USER) {
189
190                         MSG_INFO("msg id [%d], read report reject by user", pReqInfo->msgInfo.msgId);
191
192                         err = MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_NO_SENT);
193
194                         return err;
195                 }
196
197                 err = MsgStoGetRecipientList(pReqInfo->msgInfo.msgId, &pRecipientList);
198                 if(err != MSG_SUCCESS)
199                         return MSG_ERR_PLUGIN_STORAGE;
200
201                 pReqInfo->msgInfo.nAddressCnt = pRecipientList.recipientCnt;
202                 pReqInfo->msgInfo.addressList = pRecipientList.recipientAddr;
203
204 //                      for(int i = 0; i < pRecipientList.recipientCnt; i++)
205 //                      {
206 //                              pReqInfo->msgInfo.addressList[i].addressType = pRecipientList.recipientAddr[i].addressType;
207 //                              pReqInfo->msgInfo.addressList[i].recipientType = MSG_RECIPIENTS_TYPE_TO;
208 //                              pReqInfo->msgInfo.addressList[i].contactId = pRecipientList.recipientAddr[i].contactId;
209 //                              strncpy(pReqInfo->msgInfo.addressList[i].addressVal, pRecipientList.recipientAddr[i].addressVal, MAX_ADDRESS_VAL_LEN);
210 //                      }
211
212                 char subject[MAX_SUBJECT_LEN+1];
213
214                 err = MsgStoGetSubject(pReqInfo->msgInfo.msgId, subject);
215                 if(err != MSG_SUCCESS)
216                         MSG_DEBUG("Getting subject returned not a MSG_SUCCESS");
217
218                 strncpy(pReqInfo->msgInfo.subject, subject, MAX_SUBJECT_LEN);
219
220                 err = plg->composeReadReport(&(pReqInfo->msgInfo));
221         }
222         else if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
223         {
224                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
225         }
226
227         /* reject_msg_support */
228
229         if(err != MSG_SUCCESS)
230         {
231                 MSG_DEBUG("Fail to Add/Update Message : MsgStoMoveMessageToFolder()/MsgStoAddMessage()");
232                 return err;
233         }
234
235         switch(pReqInfo->msgInfo.msgType.subType)
236         {
237                 case MSG_SENDREQ_MMS:
238                 case MSG_FORWARD_MMS:
239                         MsgDeleteFile(pReqInfo->msgInfo.msgData);
240                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
241                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s%d.mms", MSG_DATA_PATH, pReqInfo->msgInfo.msgId);
242                         break;
243
244                 case MSG_READREPLY_MMS:
245                 case MSG_READRECIND_MMS:
246                         break;
247                 default:
248                         break;
249         }
250
251         // update content location from db
252         if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
253                 err = MsgStoGetContentLocation(&(pReqInfo->msgInfo));
254         /* reject_msg_support */
255         else if(pReqInfo->msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS)
256                 err = plg->updateRejectStatus(&(pReqInfo->msgInfo));
257         /* reject_msg_support */
258
259         // Check SIM is present or not
260         char keyName[MAX_VCONFKEY_NAME_LEN];
261         memset(keyName, 0x00, sizeof(keyName));
262         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, pReqInfo->msgInfo.sim_idx);
263         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(keyName);
264
265         if(simStatus == MSG_SIM_STATUS_NOT_FOUND)
266         {
267                 MSG_DEBUG("SIM is not present...");
268                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
269
270                 return MSG_ERR_NO_SIM;
271         }
272
273         if(err == MSG_SUCCESS)
274                 err = plg->submitReq(pReqInfo);
275
276         if(err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
277                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
278
279         if (err != MSG_SUCCESS)
280         {
281                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
282                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
283                 else
284                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
285         }
286
287
288         return err;
289 }
290
291 msg_error_t MsgCancelReq(msg_request_id_t reqId)
292 {
293         msg_error_t err = MSG_SUCCESS;
294
295         return err;
296 }
297
298
299 msg_error_t MsgUpdateSentMsg(msg_message_id_t MsgId, msg_network_status_t Status)
300 {
301         msg_error_t err = MSG_SUCCESS;
302
303         bool bKeepCopy = true;
304
305 #ifdef MSG_MMS_KEEPCOPY
306         MSG_SENDINGOPT_INFO_S sendOpt = {};
307
308         if (msgType.mainType == MSG_MMS_TYPE)
309         {
310                 if (MsgStoGetMmsSendOpt(MsgId, &sendOpt) == MSG_SUCCESS)
311                 {
312                         bKeepCopy = sendOpt.bKeepCopy;
313                 }
314                 else
315                 {
316                         ret = MsgSettingGetBool(MSG_KEEP_COPY, &bKeepCopy);
317                 }
318         }
319 #endif
320
321         // Move Msg to SENTBOX
322         if (Status == MSG_NETWORK_SEND_SUCCESS)
323         {
324                 MSG_DEBUG(" In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
325                 if (bKeepCopy == true)
326                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
327                 else
328                         err = MsgStoDeleteMessage(MsgId, false);
329         }
330
331         return err;
332 }
333
334
335 void MsgCopyReqInfo(MSG_REQUEST_INFO_S *pSrc, int addrIdx, MSG_REQUEST_INFO_S *pDest)
336 {
337         MSG_BEGIN();
338
339         memset(pDest, 0x00, sizeof(MSG_REQUEST_INFO_S));
340
341         // Copy Request ID
342         pDest->reqId = pSrc->reqId;
343
344         // Copy Msg Info
345         pDest->msgInfo.msgId = pSrc->msgInfo.msgId;
346
347         pDest->msgInfo.threadId = pSrc->msgInfo.threadId;
348
349         pDest->msgInfo.folderId = pSrc->msgInfo.folderId;
350
351         pDest->msgInfo.msgType.mainType = pSrc->msgInfo.msgType.mainType;
352         pDest->msgInfo.msgType.subType = pSrc->msgInfo.msgType.subType;
353         pDest->msgInfo.msgType.classType = pSrc->msgInfo.msgType.classType;
354
355         pDest->msgInfo.storageId = pSrc->msgInfo.storageId;
356
357         pDest->msgInfo.nAddressCnt = 1;
358
359         pDest->msgInfo.addressList[0].addressType = pSrc->msgInfo.addressList[addrIdx].addressType;
360         pDest->msgInfo.addressList[0].recipientType = pSrc->msgInfo.addressList[addrIdx].recipientType;
361         pDest->msgInfo.addressList[0].contactId = pSrc->msgInfo.addressList[addrIdx].contactId;
362         strncpy(pDest->msgInfo.addressList[0].addressVal, pSrc->msgInfo.addressList[addrIdx].addressVal, MAX_ADDRESS_VAL_LEN);
363         strncpy(pDest->msgInfo.addressList[0].displayName, pSrc->msgInfo.addressList[addrIdx].displayName, MAX_DISPLAY_NAME_LEN);
364
365         strncpy(pDest->msgInfo.replyAddress, pSrc->msgInfo.replyAddress, MAX_PHONE_NUMBER_LEN);
366         strncpy(pDest->msgInfo.subject, pSrc->msgInfo.subject, MAX_SUBJECT_LEN);
367
368         pDest->msgInfo.displayTime = pSrc->msgInfo.displayTime;
369         pDest->msgInfo.networkStatus = pSrc->msgInfo.networkStatus;
370         pDest->msgInfo.encodeType = pSrc->msgInfo.encodeType;
371         pDest->msgInfo.bRead = pSrc->msgInfo.bRead;
372         pDest->msgInfo.bProtected = pSrc->msgInfo.bProtected;
373         pDest->msgInfo.priority = pSrc->msgInfo.priority;
374         pDest->msgInfo.direction = pSrc->msgInfo.direction;
375
376         pDest->msgInfo.msgPort.valid = pSrc->msgInfo.msgPort.valid;
377
378         if (pDest->msgInfo.msgPort.valid == true)
379         {
380                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
381                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
382         }
383
384         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
385         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
386
387         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
388
389         if (pDest->msgInfo.bTextSms == true)
390         {
391                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
392                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
393         }
394
395         // Set Sending Info
396         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
397
398         if (pDest->sendOptInfo.bSetting == true)
399         {
400                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
401                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
402
403                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE)
404                 {
405                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
406                 }
407                 else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE)
408                 {
409                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
410                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
411
412                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
413                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
414
415                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
416                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
417                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
418                 }
419         }
420
421         MSG_END();
422 }
423