2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
18 * @file download-manager-util.cpp
19 * @author Jungki Kwak (jungki.kwak@samsung.com)
20 * @brief Utility APIs and interface with content player
28 #include "app_service.h"
29 #include "media_content.h"
30 #include "media_info.h"
32 #include "download-manager-util.h"
40 #define MAX_MIME_TABLE_NUM 22
41 const char *ambiguousMIMETypeList[] = {
43 "application/octet-stream"
46 struct MimeTableType MimeTable[]={
48 {"application/pdf",DP_CONTENT_PDF},
50 {"application/msword",DP_CONTENT_WORD},
51 {"application/vnd.openxmlformats-officedocument.wordprocessingml.document",DP_CONTENT_WORD},
53 {"application/vnd.ms-powerpoint",DP_CONTENT_PPT},
54 {"application/vnd.openxmlformats-officedocument.presentationml.presentation",DP_CONTENT_PPT},
56 {"application/vnd.ms-excel",DP_CONTENT_EXCEL},
57 {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",DP_CONTENT_EXCEL},
59 {"text/html",DP_CONTENT_HTML},
61 {"text/txt",DP_CONTENT_TEXT},
62 {"text/palin",DP_CONTENT_TEXT},
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},
71 {"application/vnd.oma.drm.content",DP_CONTENT_DRM},
72 {"application/vnd.oma.drm.message",DP_CONTENT_DRM},
74 {"application/x-java-archive",DP_CONTENT_JAVA},
75 {"application/java-archive",DP_CONTENT_JAVA},
77 {"image/svg+xml",DP_CONTENT_SVG}, //20
79 {"application/x-shockwave-flash",DP_CONTENT_FLASH}
82 bool FileOpener::openFile(string &path, int contentType)
84 service_h handle = NULL;
91 DP_LOG("path [%s]", path.c_str());
92 if (service_create(&handle) < 0) {
93 DP_LOGE("Fail to create service handle");
98 DP_LOGE("service handle is null");
102 if (service_set_operation(handle, SERVICE_OPERATION_VIEW) < 0) {
103 DP_LOGE("Fail to set service operation");
104 service_destroy(handle);
108 if (contentType == DP_CONTENT_HTML) {
109 filePath = "file://";
110 filePath.append(path.c_str());
114 if (service_set_uri(handle, filePath.c_str()) < 0) {
115 DP_LOGE("Fail to set uri");
116 service_destroy(handle);
120 if (service_send_launch_request(handle, NULL, NULL) < 0) {
121 DP_LOGE("Fail to launch service");
122 service_destroy(handle);
126 service_destroy(handle);
131 DownloadUtil::DownloadUtil()
135 int DownloadUtil::getContentType(const char *mime, const char *filePath)
138 int type = DP_CONTENT_UNKOWN;
140 char tempMime[MAX_FILE_PATH_LEN] = {0,};
142 if (mime == NULL || strlen(mime) < 1)
143 return DP_CONTENT_UNKOWN;
145 DP_LOG("mime[%s]",mime);
146 strncpy(tempMime, mime, MAX_FILE_PATH_LEN-1);
147 if (isAmbiguousMIMEType(mime)) {
149 memset(tempMime, 0x00, MAX_FILE_PATH_LEN);
150 ret = aul_get_mime_from_file(filePath,tempMime,sizeof(tempMime));
152 strncpy(tempMime, mime, MAX_FILE_PATH_LEN-1);
154 DP_LOG("mime from extension name[%s]",tempMime);
158 /* Search a content type from mime table. */
159 for (i = 0; i < MAX_MIME_TABLE_NUM; i++)
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;
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)
172 const char *unaliasedMime = NULL;
173 /* unaliased_mimetype means representative mime among similar types */
174 unaliasedMime = xdg_mime_unalias_mime_type(tempMime);
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;
186 DP_LOG("type[%d]\n",type);
190 bool DownloadUtil::isAmbiguousMIMEType(const char *mimeType)
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]);
209 void DownloadUtil::registerContent(string filePath)
212 media_info_h info = NULL;
214 if (filePath.empty()) {
215 DP_LOGE("file path is NULL");
219 DP_LOG("Register file [%s]", filePath.c_str());
221 ret = media_content_connect();
222 if (ret != MEDIA_CONTENT_ERROR_NONE) {
223 DP_LOGE("Fail to connect media db");
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();
234 ret = media_content_disconnect();
235 if (ret != MEDIA_CONTENT_ERROR_NONE) {
236 DP_LOGE("Fail to connect media db");