remove unused code
[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         int tmpval = 0;
259         if (MsgSettingGetInt(keyName, &tmpval) != MSG_SUCCESS) {
260                 MSG_INFO("MsgSettingGetInt() is failed");
261         }
262         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpval;
263
264         if(simStatus == MSG_SIM_STATUS_NOT_FOUND) {
265                 MSG_DEBUG("SIM is not present...");
266                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
267
268                 return MSG_ERR_NO_SIM;
269         }
270
271         if (err == MSG_SUCCESS)
272                 err = plg->submitReq(pReqInfo);
273
274         if (err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
275                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
276
277         if (err != MSG_SUCCESS) {
278                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
279                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
280                 else
281                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
282         }
283
284         return err;
285 }
286
287
288 msg_error_t MsgUpdateSentMsg(msg_message_id_t MsgId, msg_network_status_t Status)
289 {
290         msg_error_t err = MSG_SUCCESS;
291
292         bool bKeepCopy = true;
293
294 #ifdef MSG_MMS_KEEPCOPY
295         MSG_SENDINGOPT_INFO_S sendOpt = {};
296
297         if (msgType.mainType == MSG_MMS_TYPE) {
298                 if (MsgStoGetMmsSendOpt(MsgId, &sendOpt) == MSG_SUCCESS)
299                         bKeepCopy = sendOpt.bKeepCopy;
300                 else
301                         err = MsgSettingGetBool(MSG_KEEP_COPY, &bKeepCopy);
302         }
303
304         /* Move Msg to SENTBOX */
305         if (Status == MSG_NETWORK_SEND_SUCCESS) {
306                 MSG_DEBUG("In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
307                 if (bKeepCopy == true)
308                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
309                 else
310                         err = MsgStoDeleteMessage(MsgId, false);
311         }
312 #else
313         /* Move Msg to SENTBOX */
314         if (Status == MSG_NETWORK_SEND_SUCCESS) {
315                 MSG_DEBUG("In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
316                 if (bKeepCopy == true)
317                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
318         }
319 #endif
320         return err;
321 }
322
323
324 void MsgCopyReqInfo(MSG_REQUEST_INFO_S *pSrc, int addrIdx, MSG_REQUEST_INFO_S *pDest)
325 {
326         MSG_BEGIN();
327
328         memset(pDest, 0x00, sizeof(MSG_REQUEST_INFO_S));
329
330         /* Copy Request ID */
331         pDest->reqId = pSrc->reqId;
332
333         /* Copy Msg Info */
334         pDest->msgInfo.msgId = pSrc->msgInfo.msgId;
335
336         pDest->msgInfo.threadId = pSrc->msgInfo.threadId;
337
338         pDest->msgInfo.folderId = pSrc->msgInfo.folderId;
339
340         pDest->msgInfo.msgType.mainType = pSrc->msgInfo.msgType.mainType;
341         pDest->msgInfo.msgType.subType = pSrc->msgInfo.msgType.subType;
342         pDest->msgInfo.msgType.classType = pSrc->msgInfo.msgType.classType;
343
344         pDest->msgInfo.storageId = pSrc->msgInfo.storageId;
345
346         pDest->msgInfo.nAddressCnt = 1;
347
348         pDest->msgInfo.addressList[0].addressType = pSrc->msgInfo.addressList[addrIdx].addressType;
349         pDest->msgInfo.addressList[0].recipientType = pSrc->msgInfo.addressList[addrIdx].recipientType;
350         pDest->msgInfo.addressList[0].contactId = pSrc->msgInfo.addressList[addrIdx].contactId;
351         strncpy(pDest->msgInfo.addressList[0].addressVal, pSrc->msgInfo.addressList[addrIdx].addressVal, MAX_ADDRESS_VAL_LEN);
352         strncpy(pDest->msgInfo.addressList[0].displayName, pSrc->msgInfo.addressList[addrIdx].displayName, MAX_DISPLAY_NAME_LEN);
353
354         strncpy(pDest->msgInfo.replyAddress, pSrc->msgInfo.replyAddress, MAX_PHONE_NUMBER_LEN);
355         strncpy(pDest->msgInfo.subject, pSrc->msgInfo.subject, MAX_SUBJECT_LEN);
356
357         pDest->msgInfo.displayTime = pSrc->msgInfo.displayTime;
358         pDest->msgInfo.networkStatus = pSrc->msgInfo.networkStatus;
359         pDest->msgInfo.encodeType = pSrc->msgInfo.encodeType;
360         pDest->msgInfo.bRead = pSrc->msgInfo.bRead;
361         pDest->msgInfo.bProtected = pSrc->msgInfo.bProtected;
362         pDest->msgInfo.priority = pSrc->msgInfo.priority;
363         pDest->msgInfo.direction = pSrc->msgInfo.direction;
364
365         pDest->msgInfo.msgPort.valid = pSrc->msgInfo.msgPort.valid;
366
367         if (pDest->msgInfo.msgPort.valid == true) {
368                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
369                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
370         }
371
372         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
373         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
374
375         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
376
377         if (pDest->msgInfo.bTextSms == true) {
378                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
379                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
380         }
381
382         /* Set Sending Info */
383         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
384
385         if (pDest->sendOptInfo.bSetting == true) {
386                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
387                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
388
389                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE) {
390                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
391                 } else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE) {
392                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
393                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
394
395                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
396                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
397
398                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
399                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
400                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
401                 }
402         }
403
404         MSG_END();
405 }
406