Merge from master.
[framework/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginDrm.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 <string.h>
18 #include <drm_client_types.h>
19 #include <drm_client.h>
20 #include "MsgMmsTypes.h"
21 #include "MsgDrmWrapper.h"
22 #include "MmsPluginDrm.h"
23 #include "MmsPluginCodec.h"
24 #include "MmsPluginMIME.h"
25 #include "MmsPluginDebug.h"
26 #include "MmsPluginUtil.h"
27
28 #ifdef __SUPPORT_DRM__
29
30 MmsDrm2ConvertState     mmsDrm2ConvertState;
31
32 MsgDrmType MsgGetDRMType(MsgType *pMsgType, MsgBody *pMsgBody)
33 {
34         MsgDrmType drmType = MSG_DRM_TYPE_NONE;
35         MsgMultipart *pMultipart = NULL;
36
37         if (MsgIsMultipart(pMsgType->type)) {
38                 pMultipart = pMsgBody->body.pMultipart;
39                 while (pMultipart) {
40                         if (drmType < pMultipart->type.drmInfo.drmType)
41                                 drmType = pMultipart->type.drmInfo.drmType;
42
43                         pMultipart = pMultipart->pNext;
44                 }
45         } else {
46                 drmType = pMsgType->drmInfo.drmType;
47         }
48
49         return drmType;
50 }
51
52 void MmsDrm2SetConvertState(MmsDrm2ConvertState newConvertState)
53 {
54         mmsDrm2ConvertState = newConvertState;
55         MSG_DEBUG("MmsDrm2SetConvertState: mmsDrm2ConvertState = %d\n", mmsDrm2ConvertState);
56 }
57
58 MmsDrm2ConvertState MmsDrm2GetConvertState(void)
59 {
60         return mmsDrm2ConvertState;
61 }
62
63 bool MsgDRM2GetDRMInfo(char *szFilePath, MsgType *pMsgType)
64 {
65         if (szFilePath == NULL || pMsgType == NULL) {
66                 MSG_DEBUG("Param is NULL szFilePath = %d, pMsgType = %d", szFilePath, pMsgType);
67                 return false;
68         }
69
70         char szMimeType[DRM_MAX_LEN_MIME + 1];
71         char szContentID[DRM_MAX_LEN_CID + 1];
72         MSG_DRM_TYPE drmType = MSG_DRM_NONE;
73         int ret = 0;
74
75         MsgDrmGetDrmType(szFilePath, &drmType);
76         MsgDrmGetMimeTypeEx(szFilePath, szMimeType, sizeof(szMimeType));
77         MsgDrmGetContentID(szFilePath, szContentID, sizeof(szContentID));
78         MSG_DEBUG("drmType: %d", drmType);
79
80         switch (drmType) {
81         case MSG_DRM_FORWARD_LOCK:
82                 pMsgType->drmInfo.drmType = MSG_DRM_TYPE_FL;
83                 pMsgType->drmInfo.contentType = (MimeType)_MsgGetCode(MSG_TYPE, szMimeType);
84                 if (MsgCopyDrmInfo(pMsgType) == false) {
85                         MSG_DEBUG("MsgDRM2GetDRMInfo : MsgCopyDrmInfo failed");
86                         return false;
87                 }
88                 break;
89
90         case MSG_DRM_COMBINED_DELIVERY:
91                 pMsgType->drmInfo.drmType = MSG_DRM_TYPE_CD;
92                 pMsgType->drmInfo.szContentURI = MsgResolveContentURI(szContentID);
93                 break;
94
95         case MSG_DRM_SEPARATE_DELIVERY:
96                 pMsgType->drmInfo.drmType = MSG_DRM_TYPE_SD;
97
98                 pMsgType->drmInfo.contentType = (MimeType)_MsgGetCode(MSG_TYPE, szMimeType);
99
100                 drm_content_info_s dcfHdrInfo;
101                 bzero(&dcfHdrInfo, sizeof(drm_content_info_s));
102                 ret = drm_get_content_info(szFilePath, &dcfHdrInfo);
103                 MSG_DEBUG("drm_get_content_info is failed, ret=[%d]", ret);
104
105                 drm_file_info_s fileInfo;
106                 bzero(&fileInfo, sizeof(drm_file_info_s));
107                 ret = drm_get_file_info(szFilePath, &fileInfo);
108                 MSG_DEBUG("drm_get_file_info is failed, ret=[%d]", ret);
109
110                 if (fileInfo.oma_info.version == DRM_OMA_DRMV1_RIGHTS) {
111                         pMsgType->drmInfo.szContentName = MsgRemoveQuoteFromFilename(dcfHdrInfo.title);
112                         pMsgType->drmInfo.szContentDescription = MsgStrCopy(dcfHdrInfo.description);
113                 }
114                 break;
115
116         default:
117                 pMsgType->drmInfo.drmType = MSG_DRM_TYPE_NONE;
118                 break;
119         }
120
121         pMsgType->drmInfo.szDrm2FullPath = MsgStrCopy(szFilePath);
122         MSG_DEBUG("pMsgType->drmInfo.szDrm2FullPath: %s", pMsgType->drmInfo.szDrm2FullPath);
123
124         return true;
125 }
126
127 bool MsgDRMIsForwardLockType(MsgDrmType drmType)
128 {
129         switch (drmType) {
130         case MSG_DRM_TYPE_FL:
131         case MSG_DRM_TYPE_CD:
132                 return true;
133
134         case MSG_DRM_TYPE_NONE:         //SD & plain can be forwarded
135         case MSG_DRM_TYPE_SD:
136         default:
137                 return false;
138         }
139 }
140
141 bool MsgChangeDrm2FileName(char *szFileName)
142 {
143         char szTempFileName[MSG_FILENAME_LEN_MAX] = {0,};
144
145         if (szFileName == NULL || szFileName[0] == '\0')
146                 return false;
147
148         MsgGetFileNameWithoutExtension(szTempFileName, szFileName);
149
150         if (strrchr(szTempFileName, '.'))
151                 return true;
152
153         strcat(szTempFileName, ".dcf");
154         strcpy(szFileName, szTempFileName);
155
156         MSG_DEBUG("MsgChangeDrm2FileName: made szFileName = %s \n", szFileName);
157
158         return true;
159 }
160
161 bool MsgIsDCFFile(char *szFilePath)
162 {
163         int length = 0;
164
165         MSG_DEBUG("MsgIsDCFFile: szFilePath = %s \n", szFilePath);
166
167         length = MsgStrlen(szFilePath);
168         if (szFilePath[length - 4] == '.' &&
169                 (szFilePath[length - 3] == 'd' || szFilePath[length - 3] == 'D') &&
170                 (szFilePath[length - 2] == 'c' || szFilePath[length - 2] == 'C') &&
171                 (szFilePath[length - 1] == 'f' || szFilePath[length - 1] == 'F'))
172                 return true;
173
174         return false;
175 }
176
177 #endif