2.0_beta
[framework/messaging/msg-service.git] / framework / submit-handler / MsgSubmitHandler.cpp
1 /*
2 * Copyright 2012  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://www.tizenopensource.org/license
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 "MsgException.h"
23 #include "MsgGconfWrapper.h"
24 #include "MsgPluginManager.h"
25 #include "MsgStorageHandler.h"
26 #include "MsgSubmitHandler.h"
27
28
29 /*==================================================================================================
30                                      FUNCTION IMPLEMENTATION
31 ==================================================================================================*/
32 msg_error_t MsgSubmitReq(MSG_REQUEST_INFO_S *pReqInfo, bool bScheduled)
33 {
34         msg_error_t err = MSG_SUCCESS;
35
36         static int reqId = 1;
37
38         pReqInfo->reqId = reqId;
39         reqId++;
40
41         MSG_DEBUG("==== Msg ID = [%d] ====", pReqInfo->msgInfo.msgId);
42         MSG_DEBUG("==== Folder ID = [%d] ====", pReqInfo->msgInfo.folderId);
43         MSG_DEBUG("==== Main Type = [%d] ====", pReqInfo->msgInfo.msgType.mainType);
44         MSG_DEBUG("==== Sub Type = [%d] ====", pReqInfo->msgInfo.msgType.subType);
45         MSG_DEBUG("==== Class Type = [%d] ====", pReqInfo->msgInfo.msgType.classType);
46         MSG_DEBUG("==== Message Data = [%s] ====", pReqInfo->msgInfo.msgData);
47         MSG_DEBUG("==== Message Text = [%s] ====", pReqInfo->msgInfo.msgText);
48
49         MSG_DEBUG("==== bSetting = [%d] ====", pReqInfo->sendOptInfo.bSetting);
50
51         if (pReqInfo->msgInfo.msgType.mainType == MSG_SMS_TYPE)
52         {
53                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
54                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
55                 MSG_DEBUG("==== bReplyPath = [%d] ====", pReqInfo->sendOptInfo.option.smsSendOptInfo.bReplyPath);
56
57                 err = MsgSubmitReqSMS(pReqInfo);
58         }
59         else if (pReqInfo->msgInfo.msgType.mainType == MSG_MMS_TYPE)
60         {
61                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
62                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
63                 MSG_DEBUG("==== bReadReq = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.bReadReq);
64                 MSG_DEBUG("==== priority = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.priority);
65                 MSG_DEBUG("==== expiryTime = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.expiryTime.time);
66
67                 err = MsgSubmitReqMMS(pReqInfo);
68         }
69
70         return err;
71 }
72
73
74 msg_error_t MsgSubmitReqSMS(MSG_REQUEST_INFO_S *pReqInfo)
75 {
76         msg_error_t err = MSG_SUCCESS;
77
78         // submit request based on msgType;
79         MSG_MAIN_TYPE_T mainType = pReqInfo->msgInfo.msgType.mainType;
80         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
81
82         if (plg == NULL)
83                 THROW(MsgException::PLUGIN_ERROR, "No plugin for %d type", mainType);
84
85         // If MSG ID > 0 -> MSG in DRAFT
86         // Move Folder to OUTBOX
87         if (pReqInfo->msgInfo.msgPort.valid == false) {
88                 pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
89
90                 if (pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID))
91                         err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
92         }
93
94         err = plg->submitReq(pReqInfo);
95
96         return err;
97 }
98
99
100 msg_error_t MsgSubmitReqMMS(MSG_REQUEST_INFO_S *pReqInfo)
101 {
102         msg_error_t err = MSG_SUCCESS;
103
104         MSG_RECIPIENTS_LIST_S pRecipientList;
105
106         MSG_MAIN_TYPE_T msgMainType = pReqInfo->msgInfo.msgType.mainType;
107         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
108
109         if(!plg)
110         {
111                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
112                 MSG_DEBUG("No Plugin for %d type", msgMainType);
113
114                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
115         }
116
117         //      If MSG ID > 0 -> MSG in DRAFT
118         // Move Folder to OUTBOX
119         /* reject_msg_support */
120         MSG_DEBUG("Not scheduled MMS, pReqInfo->msgInfo.msgType.subType [%d]", pReqInfo->msgInfo.msgType.subType);
121
122         if(pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_JAVA_MMS)
123         {
124                 char fileName[MAX_COMMON_INFO_SIZE+1] = {0};
125
126                 // copy whole of MMS PDU filepath to msgData
127                 strncpy(fileName, pReqInfo->msgInfo.msgData, MAX_COMMON_INFO_SIZE);
128                 memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
129                 snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, MSG_IPC_DATA_PATH"%s", fileName);
130
131                 MSG_DEBUG("JAVA MMS PDU filepath:%s", pReqInfo->msgInfo.msgData);
132
133                 // submit request
134                 plg->submitReq(pReqInfo);
135
136                 if(err != MSG_SUCCESS)
137                 {
138                         MSG_DEBUG("Update Network Status : [%d]", err);
139                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo),MSG_NETWORK_SEND_FAIL);
140                 }
141
142                 return err;
143         }
144         else if((pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_MMS) || (pReqInfo->msgInfo.msgType.subType == MSG_FORWARD_MMS))
145         {
146                 // update address list in the ase of existing message
147                 if(pReqInfo->msgInfo.msgId > 0)
148                 {
149                         err = MsgStoGetOrgAddressList(&(pReqInfo->msgInfo));
150
151                         if(err != MSG_SUCCESS)
152                                 MSG_DEBUG("[WARNING]MsgStoGetOrgAddressList returned not a MSG_SUCCESS");
153                 }
154
155                 if(pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID)) {
156                         MSG_ADDRESS_INFO_S addrInfo = {0,};
157                         int addrIdx = 0;
158
159                         err = MsgStoGetAddrInfo(pReqInfo->msgInfo.msgId, &addrInfo);
160
161                         if (err == MSG_SUCCESS) {
162                                 for (int i = 0; i < pReqInfo->msgInfo.nAddressCnt; i++) {
163                                         //if (pReqInfo->msgInfo.addressList[i].threadId == addrInfo.threadId) {
164                                         if (!strcmp(pReqInfo->msgInfo.addressList[i].addressVal, addrInfo.addressVal)) {
165                                                 addrIdx = i;
166                                                 MSG_DEBUG("addrIdx = %d, address = [%s]", addrIdx, pReqInfo->msgInfo.addressList[i].addressVal);
167                                                 break;
168                                         }
169                                 }
170                         } else {
171                                 MSG_DEBUG("[Error]MsgStoGetAddrInfo is failed");
172                         }
173
174                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
175                         err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
176
177                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
178                 } else {
179                         //new message case
180                         MSG_DEBUG("New Message");
181                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
182                         err = MsgStoAddMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
183                         //pReqInfo->msgInfo.msgId = 0;
184                 }
185         }
186         else if(pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS)
187         {
188                 err = MsgStoCheckReadReportStatus(pReqInfo->msgInfo.msgId);
189                 if(err != MSG_SUCCESS)
190                         return err;
191
192                 err = MsgStoGetRecipientList(pReqInfo->msgInfo.msgId, &pRecipientList);
193                 if(err != MSG_SUCCESS)
194                         return MSG_ERR_PLUGIN_STORAGE;
195
196                 pReqInfo->msgInfo.nAddressCnt = pRecipientList.recipientCnt;
197
198                 for(int i = 0; i < pRecipientList.recipientCnt; i++)
199                 {
200                         pReqInfo->msgInfo.addressList[i].addressType = pRecipientList.recipientAddr[i].addressType;
201                         pReqInfo->msgInfo.addressList[i].recipientType = MSG_RECIPIENTS_TYPE_TO;
202                         pReqInfo->msgInfo.addressList[i].contactId = pRecipientList.recipientAddr[i].contactId;
203                         strncpy(pReqInfo->msgInfo.addressList[i].addressVal, pRecipientList.recipientAddr[i].addressVal, MAX_ADDRESS_VAL_LEN);
204                 }
205
206                 char subject[MAX_SUBJECT_LEN+1];
207
208                 err = MsgStoGetSubject(pReqInfo->msgInfo.msgId, subject);
209                 if(err != MSG_SUCCESS)
210                         MSG_DEBUG("Getting subject returned not a MSG_SUCCESS");
211
212                 strncpy(pReqInfo->msgInfo.subject, subject, MAX_SUBJECT_LEN);
213
214                 err = plg->composeReadReport(&(pReqInfo->msgInfo));
215         }
216         else if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
217         {
218                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
219         }
220
221         /* reject_msg_support */
222
223         if(err != MSG_SUCCESS)
224         {
225                 MSG_DEBUG("Fail to Add/Update Message : MsgStoMoveMessageToFolder()/MsgStoAddMessage()");
226                 return err;
227         }
228
229         switch(pReqInfo->msgInfo.msgType.subType)
230         {
231                 case MSG_SENDREQ_MMS:
232                 case MSG_FORWARD_MMS:
233                         MsgDeleteFile(pReqInfo->msgInfo.msgData);
234                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
235                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, MSG_DATA_PATH"%d.mms", pReqInfo->msgInfo.msgId);
236                         break;
237
238                 case MSG_READREPLY_MMS:
239                 case MSG_READRECIND_MMS:
240                         break;
241                 default:
242                         break;
243         }
244
245         // update content location from db
246         if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
247                 err = MsgStoGetContentLocation(&(pReqInfo->msgInfo));
248         /* reject_msg_support */
249         else if(pReqInfo->msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS)
250                 err = plg->updateRejectStatus(&(pReqInfo->msgInfo));
251         /* reject_msg_support */
252
253         // Check SIM is present or not
254         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
255
256         if(simStatus == MSG_SIM_STATUS_NOT_FOUND)
257         {
258                 MSG_DEBUG("SIM is not present...");
259                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
260
261                 return MSG_ERR_NO_SIM;
262         }
263
264         if(err == MSG_SUCCESS)
265                 err = plg->submitReq(pReqInfo);
266
267         if(err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
268                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
269
270         if (err != MSG_SUCCESS)
271         {
272                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
273                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
274                 else
275                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
276         }
277
278
279         return err;
280 }
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         {
302                 if (MsgStoGetMmsSendOpt(MsgId, &sendOpt) == MSG_SUCCESS)
303                 {
304                         bKeepCopy = sendOpt.bKeepCopy;
305                 }
306                 else
307                 {
308                         ret = MsgSettingGetBool(MSG_KEEP_COPY, &bKeepCopy);
309                 }
310         }
311 #endif
312
313         // Move Msg to SENTBOX
314         if (Status == MSG_NETWORK_SEND_SUCCESS)
315         {
316                 MSG_DEBUG(" In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
317                 if (bKeepCopy == true)
318                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
319                 else
320                         err = MsgStoDeleteMessage(MsgId, false);
321         }
322
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(pDest));
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         {
372                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
373                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
374         }
375
376         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
377         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
378
379         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
380
381         if (pDest->msgInfo.bTextSms == true)
382         {
383                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
384                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
385         }
386
387         // Set Sending Info
388         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
389
390         if (pDest->sendOptInfo.bSetting == true)
391         {
392                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
393                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
394
395                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE)
396                 {
397                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
398                 }
399                 else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE)
400                 {
401                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
402                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
403
404                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
405                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
406
407                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
408                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
409                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
410                 }
411         }
412
413         MSG_END();
414 }
415