Resolve the bug about the name of edc
[apps/web/download-manager.git] / src / download-manager-util.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 /*
18  * @file        download-manager-util.cpp
19  * @author      Jungki Kwak (jungki.kwak@samsung.com)
20  * @brief   Utility APIs and interface with content player
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <dlfcn.h>
26 #include "aul.h"
27 #include "xdgmime.h"
28 #include "app_service.h"
29 #include "media_content.h"
30 #include "media_info.h"
31
32 #include "download-manager-util.h"
33
34 struct MimeTableType
35 {
36         const char *mime;
37         int contentType;
38 };
39
40 #define MAX_MIME_TABLE_NUM 22
41 const char *ambiguousMIMETypeList[] = {
42                 "text/plain",
43                 "application/octet-stream"
44 };
45
46 struct MimeTableType MimeTable[]={
47                 // PDF
48                 {"application/pdf",DP_CONTENT_PDF},
49                 // word
50                 {"application/msword",DP_CONTENT_WORD},
51                 {"application/vnd.openxmlformats-officedocument.wordprocessingml.document",DP_CONTENT_WORD},
52                 // ppt
53                 {"application/vnd.ms-powerpoint",DP_CONTENT_PPT},
54                 {"application/vnd.openxmlformats-officedocument.presentationml.presentation",DP_CONTENT_PPT},
55                 // excel
56                 {"application/vnd.ms-excel",DP_CONTENT_EXCEL},
57                 {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",DP_CONTENT_EXCEL},
58                 // html
59                 {"text/html",DP_CONTENT_HTML},
60                 // txt
61                 {"text/txt",DP_CONTENT_TEXT},
62                 {"text/palin",DP_CONTENT_TEXT},
63                 // ringtone
64                 {"text/x-iMelody",DP_CONTENT_RINGTONE},//10
65                 {"application/x-smaf",DP_CONTENT_RINGTONE},
66                 {"audio/midi",DP_CONTENT_RINGTONE},
67                 {"audio/AMR",DP_CONTENT_RINGTONE},
68                 {"audio/AMR-WB",DP_CONTENT_RINGTONE},
69                 {"audio/x-xmf",DP_CONTENT_RINGTONE},
70                 // DRM
71                 {"application/vnd.oma.drm.content",DP_CONTENT_DRM},
72                 {"application/vnd.oma.drm.message",DP_CONTENT_DRM},
73                 // JAVA
74                 {"application/x-java-archive",DP_CONTENT_JAVA},
75                 {"application/java-archive",DP_CONTENT_JAVA},
76                 // SVG
77                 {"image/svg+xml",DP_CONTENT_SVG}, //20
78                 // FLASH
79                 {"application/x-shockwave-flash",DP_CONTENT_FLASH}
80 };
81
82 bool FileOpener::openFile(string &path, int contentType)
83 {
84         service_h handle = NULL;
85         string filePath;
86         DP_LOG_FUNC();
87
88         if (path.empty())
89                 return false;
90
91         DP_LOG("path [%s]", path.c_str());
92         if (service_create(&handle) < 0) {
93                 DP_LOGE("Fail to create service handle");
94                 return false;
95         }
96
97         if (!handle) {
98                 DP_LOGE("service handle is null");
99                 return false;
100         }
101
102         if (service_set_operation(handle, SERVICE_OPERATION_VIEW) < 0) {
103                 DP_LOGE("Fail to set service operation");
104                 service_destroy(handle);
105                 return false;
106         }
107
108         if (contentType == DP_CONTENT_HTML) {
109                 filePath = "file://";
110                 filePath.append(path.c_str());
111         } else {
112                 filePath = path;
113         }
114         if (service_set_uri(handle, filePath.c_str()) < 0) {
115                 DP_LOGE("Fail to set uri");
116                 service_destroy(handle);
117                 return false;
118         }
119
120         if (service_send_launch_request(handle, NULL, NULL) < 0) {
121                 DP_LOGE("Fail to launch service");
122                 service_destroy(handle);
123                 return false;
124         }
125
126         service_destroy(handle);
127
128         return true;
129 }
130
131 DownloadUtil::DownloadUtil()
132 {
133 }
134
135 int DownloadUtil::getContentType(const char *mime, const char *filePath)
136 {
137         int i = 0;
138         int type = DP_CONTENT_UNKOWN;
139         int ret = 0;
140         char tempMime[MAX_FILE_PATH_LEN] = {0,};
141         DP_LOGD_FUNC();
142         if (mime == NULL || strlen(mime) < 1)
143                 return DP_CONTENT_UNKOWN;
144
145         DP_LOG("mime[%s]",mime);
146         strncpy(tempMime, mime, MAX_FILE_PATH_LEN-1);
147         if (isAmbiguousMIMEType(mime)) {
148                 if (filePath) {
149                         memset(tempMime, 0x00, MAX_FILE_PATH_LEN);
150                         ret = aul_get_mime_from_file(filePath,tempMime,sizeof(tempMime));
151                         if (ret < AUL_R_OK )
152                                 strncpy(tempMime, mime, MAX_FILE_PATH_LEN-1);
153                         else
154                                 DP_LOG("mime from extension name[%s]",tempMime);
155                 }
156         }
157
158         /* Search a content type from mime table. */
159         for (i = 0; i < MAX_MIME_TABLE_NUM; i++)
160         {
161                 //DP_LOG("TableMime[%d][%s]",i,MimeTable[i].mime);
162                 if (strncmp(MimeTable[i].mime, tempMime, strlen(tempMime)) == 0){
163                         type = MimeTable[i].contentType;
164                         break;
165                 }
166         }
167         /* If there is no mime at mime table, check the category with the first
168          *   domain of mime string
169          * ex) video/... => video type */
170         if (type == DP_CONTENT_UNKOWN)
171         {
172                 const char *unaliasedMime = NULL;
173                 /* unaliased_mimetype means representative mime among similar types */
174                 unaliasedMime = xdg_mime_unalias_mime_type(tempMime);
175
176                 if (unaliasedMime != NULL) {
177                         DP_LOG("unaliased mime type[%s]\n",unaliasedMime);
178                         if (strstr(unaliasedMime,"video/") != NULL)
179                                 type = DP_CONTENT_VIDEO;
180                         else if (strstr(unaliasedMime,"audio/") != NULL)
181                                 type = DP_CONTENT_MUSIC;
182                         else if (strstr(unaliasedMime,"image/") != NULL)
183                                 type = DP_CONTENT_IMAGE;
184                 }
185         }
186         DP_LOG("type[%d]\n",type);
187         return type;
188 }
189
190 bool DownloadUtil::isAmbiguousMIMEType(const char *mimeType)
191 {
192
193         if (!mimeType)
194                 return false;
195
196         int index = 0;
197         int listSize = sizeof(ambiguousMIMETypeList) / sizeof(const char *);
198         for (index = 0; index < listSize; index++) {
199                 if (0 == strncmp(mimeType, ambiguousMIMETypeList[index],
200                                 strlen(ambiguousMIMETypeList[index]))) {
201                         DP_LOG("It is ambiguous! [%s]", ambiguousMIMETypeList[index]);
202                         return true;
203                 }
204         }
205
206         return false;
207 }
208
209 void DownloadUtil::registerContent(string filePath)
210 {
211         int ret = -1;
212         media_info_h info = NULL;
213
214         if (filePath.empty()) {
215                 DP_LOGE("file path is NULL");
216                 return;
217         }
218
219         DP_LOG("Register file [%s]", filePath.c_str());
220
221         ret = media_content_connect();
222         if (ret != MEDIA_CONTENT_ERROR_NONE) {
223                 DP_LOGE("Fail to connect media db");
224                 return;
225         }
226
227         ret = media_info_insert_to_db(filePath.c_str(), &info);
228         if (ret != MEDIA_CONTENT_ERROR_NONE) {
229                 DP_LOGE("Fail to insert media db [%d]", ret);
230                 media_content_disconnect();
231                 return;
232         }
233
234         ret = media_content_disconnect();
235         if (ret != MEDIA_CONTENT_ERROR_NONE) {
236                 DP_LOGE("Fail to connect media db");
237         }
238         return;
239 }
240