3.0 Migration
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginAppBase.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 "MsgMmsMessage.h"
18 #include "MmsPluginDebug.h"
19 #include "MmsPluginStorage.h"
20 #include "MmsPluginMessage.h"
21 #include "MmsPluginMIME.h"
22 #include "MmsPluginAppBase.h"
23 #include "MmsPluginUtil.h"
24 #include "MmsPluginTcs.h"
25 #include "MsgUtilFile.h"
26
27 msg_error_t MmsMakePreviewInfo(int msgId, MMS_MESSAGE_DATA_S *pMmsMsg, bool allow_malware, const char *raw_filepath);
28
29 MmsPluginAppBase::MmsPluginAppBase()
30 {
31         memset(&mmsMsgData, 0x00, sizeof(mmsMsgData));
32 }
33
34 MmsPluginAppBase::MmsPluginAppBase(MMS_DATA_S *pMmsData)
35 {
36         setMmsData(pMmsData);
37 }
38
39 MmsPluginAppBase::MmsPluginAppBase(MmsMsg *pMmsMsg)
40 {
41         setMmsData(pMmsMsg);
42 }
43
44 MmsPluginAppBase::~MmsPluginAppBase()
45 {
46         MsgMmsReleaseMmsLists(&mmsMsgData);
47 }
48
49 void MmsPluginAppBase::setMmsData(MmsMsg *pMmsMsg)
50 {
51         MmsConvertMsgData(pMmsMsg, &mmsMsgData);
52 }
53
54 void MmsPluginAppBase::setMmsData(MMS_DATA_S *pMmsData)
55 {
56         MsgMmsConvertMmsDataToMmsMessageData(pMmsData, &mmsMsgData);
57 }
58
59 void MmsPluginAppBase::makePreviewInfo(msg_message_id_t  msgId, bool allow_malware, const char *raw_filepath)
60 {
61         MmsMakePreviewInfo(msgId, &mmsMsgData, allow_malware, raw_filepath);
62 }
63
64 void MmsPluginAppBase::getFirstPageTextFilePath(char *textBuf, int textBufSize)
65 {
66
67         MMS_PAGE_S *pPage = NULL;
68         MMS_MEDIA_S *pMedia = NULL;
69
70         MMS_MESSAGE_DATA_S *pMmsMsgData = &mmsMsgData;
71
72         if (pMmsMsgData == NULL)
73                 return;
74
75         // Get the text data from the 1st slide.
76         if (pMmsMsgData->pageCnt > 0) {
77
78                 pPage = _MsgMmsGetPage(pMmsMsgData, 0);
79
80                 if (pPage) {
81
82                         for (int j = 0; j < pPage->mediaCnt; ++j) {
83
84                                 pMedia = _MsgMmsGetMedia(pPage, j);
85
86                                 if (pMedia && pMedia->mediatype == MMS_SMIL_MEDIA_TEXT) {
87
88                                         MimeType mimeType = MIME_UNKNOWN;
89
90                                         MmsGetMimeTypeFromFileName(MIME_MAINTYPE_UNKNOWN, pMedia->szFilePath, &mimeType, NULL);
91
92                                         if (mimeType == MIME_TEXT_X_VCALENDAR || mimeType == MIME_TEXT_X_VCARD || mimeType == MIME_TEXT_X_VTODO || mimeType == MIME_TEXT_X_VNOTE) {
93                                                 MSG_DEBUG("Media Type is Text, but Vobject file [%s]", pMedia->szFilePath);
94                                         } else {
95                                                 MSG_DEBUG("Text path : [%s]", pMedia->szFilePath);
96                                                 snprintf(textBuf, textBufSize, "%s", pMedia->szFilePath);//Set Text Filepath of First Pages
97                                         }
98                                         break;
99                                 }
100                         }
101                 }
102         }
103
104         return;
105 }
106
107
108 //FIXME::need to move AppBase
109 msg_error_t MmsMakePreviewInfo(int msgId, MMS_MESSAGE_DATA_S *pMmsMsg, bool allow_malware, const char *raw_filepath)
110 {
111         MMS_PAGE_S *pPage = NULL;
112         MMS_MEDIA_S *pMedia = NULL;
113         int bc_level = -1;
114
115         int ref_attach_count = 0;
116         const char * attachment_name = NULL;
117
118         if (pMmsMsg == NULL)
119                 return MSG_ERR_NULL_POINTER;
120
121         MmsPluginStorage::instance()->removePreviewInfo(msgId); //remove exist previnfo
122
123         //scan malware in raw file
124         if (raw_filepath && strlen(raw_filepath) > 0 && MsgAccessFile(raw_filepath, F_OK) == true) {
125                 int tcs_ret = 0; // MmsPluginTcsScanFile(raw_filepath, &bc_level);
126                 if (tcs_ret == 0) {
127                         if (bc_level > -1) {
128                                 MSG_DEBUG("malware exist, level = %d", bc_level);
129                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_MALWARE, "malware", bc_level);
130                         }
131                 }
132         }
133
134         //check ref type and increase attach count
135         if (pMmsMsg->pageCnt > 0) {
136                 for (int i = 0; i < pMmsMsg->pageCnt; i++) {
137                         pPage = _MsgMmsGetPage(pMmsMsg, i);
138                         if (pPage) {
139                                 for (int j = 0; j < pPage->mediaCnt; j++) {
140                                         pMedia = _MsgMmsGetMedia(pPage, j);
141                                         if (pMedia) { //IF Vobject type add to Attach in Preview data
142
143                                                 MimeType mimeType = MIME_UNKNOWN;
144                                                 MmsGetMimeTypeFromFileName(MIME_MAINTYPE_UNKNOWN, pMedia->szFilePath, &mimeType, NULL);
145                                                 if (mimeType == MIME_TEXT_X_VCALENDAR || mimeType == MIME_TEXT_X_VCARD) {
146
147                                                         MSG_DEBUG("Unsupported File [%s] It will be add to attach list", pMedia->szFilePath);
148
149                                                         ref_attach_count++;
150
151                                                         if (attachment_name == NULL) {
152                                                                 attachment_name = pMedia->szFileName;
153                                                         }
154                                                 }
155
156                                         }
157                                 }
158                         }
159                 }
160         }
161
162
163         if (pMmsMsg->pageCnt > 0) {
164
165                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_PAGE, (char *)"pagecount", pMmsMsg->pageCnt);
166
167                 pPage = _MsgMmsGetPage(pMmsMsg, 0);
168
169                 if (pPage) {
170                         for (int j = 0; j < pPage->mediaCnt; j++) {
171
172                                 pMedia = _MsgMmsGetMedia(pPage, j);
173
174                                 if (pMedia == NULL) {
175                                         MSG_ERR("[%d]th pMedia is NULL", j);
176                                         continue;
177                                 }
178
179                                 MSG_SEC_DEBUG("pMedia's Name: %s", pMedia->szFilePath);
180
181                                 if (allow_malware == false && bc_level > -1) {
182                                         if (pMedia->mediatype == MMS_SMIL_MEDIA_AUDIO) {
183                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_AUDIO, pMedia->szFileName);
184                                         }
185                                 } else {
186
187                                         if (j == 0) { //First Page, First Media
188                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_1ST_MEDIA, pMedia->szFilePath);
189                                         }
190
191                                         if (pMedia->mediatype == MMS_SMIL_MEDIA_IMG || pMedia->mediatype == MMS_SMIL_MEDIA_VIDEO) {
192                                                 char szFileName[MSG_FILENAME_LEN_MAX+1] = {0, };
193                                                 char thumbPath[MSG_FILEPATH_LEN_MAX+1] = {0, };
194                                                 char *pszExt = NULL;
195
196                                                 memset(szFileName, 0x00, MSG_FILENAME_LEN_MAX+1);
197                                                 memset(thumbPath, 0x00, MSG_FILEPATH_LEN_MAX);
198
199                                                 MSG_DEBUG("drm type = %d, %s", pMedia->drmType, pMedia->szFilePath);
200
201                                                 if (pMedia->drmType == MSG_DRM_TYPE_NONE) {
202
203                                                         snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%d.mms",msgId);
204
205                                                         if ((pszExt = strrchr(pMedia->szFilePath, '.')) != NULL && !strcasecmp(pszExt, ".png")) {
206                                                                 snprintf(thumbPath, MSG_FILEPATH_LEN_MAX, "%s%s.png", MSG_THUMBNAIL_PATH, szFileName);
207                                                         } else {
208                                                                 snprintf(thumbPath, MSG_FILEPATH_LEN_MAX, "%s%s.jpg", MSG_THUMBNAIL_PATH, szFileName);
209                                                         }
210
211                                                         if (MmsMakeImageThumbnail(pMedia->szFilePath, thumbPath) == true) {
212                                                                 if (pMedia->mediatype == MMS_SMIL_MEDIA_IMG) {
213                                                                         MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_IMG, thumbPath);
214                                                                 } else {
215                                                                         MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_VIDEO, thumbPath);
216                                                                 }
217                                                         } else {
218                                                                 MSG_DEBUG("Fail of generating thumbnail: %s to %s", pMedia->szFilePath, thumbPath);
219                                                         }
220                                                 }
221                                         } else if (pMedia->mediatype == MMS_SMIL_MEDIA_AUDIO) {
222                                                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_AUDIO, pMedia->szFileName);
223                                         }
224                                 }
225                         }
226                 } // end for
227         } else {
228                 MSG_DEBUG("There is no page");
229         }
230
231         int attachCnt = _MsgMmsGetAttachCount(pMmsMsg);
232         if (attachCnt > 0) {
233
234                 MMS_ATTACH_S *pAttach = _MsgMmsGetAttachment(pMmsMsg, 0);
235
236                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_ATTACH, pAttach->szFileName, attachCnt);
237                 MmsPluginStorage::instance()->updateMmsAttachCount(msgId, attachCnt); // for Get Message
238
239                 if (attachment_name == NULL) {
240                         attachment_name = pAttach->szFileName;
241                 }
242
243         } else {
244                 MSG_DEBUG("There is no attachment");
245         }
246
247         if (attachCnt + ref_attach_count > 0 && attachment_name) {
248
249                 MmsPluginStorage::instance()->insertPreviewInfo(msgId, MSG_MMS_ITEM_TYPE_ATTACH, attachment_name, attachCnt + ref_attach_count);
250                 MmsPluginStorage::instance()->updateMmsAttachCount(msgId, attachCnt + ref_attach_count);
251         }
252
253         return MSG_SUCCESS;
254 }