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