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