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