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