2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 #include <dpl/assert.h>
23 #include <dpl/utils/mime_type_utils.h>
25 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForIcon()
27 static std::set<DPL::String> set;
28 DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
30 set.insert(s("image/gif"));
31 set.insert(s("image/png"));
32 set.insert(s("image/vnd.microsoft.icon"));
33 set.insert(s("image/svg+xml"));
34 set.insert(s("image/jpeg"));
39 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForStartFile()
41 static std::set<DPL::String> set;
42 DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
44 set.insert(s("text/html"));
45 set.insert(s("application/xhtml+xml"));
50 bool MimeTypeUtils::isMimeTypeSupportedForStartFile(const DPL::String& mimeType)
52 return getMimeTypesSupportedForStartFile().count(stripMimeParameters(
56 const MimeTypeUtils::FileIdentificationMap& MimeTypeUtils::
57 getFileIdentificationMap()
59 static FileIdentificationMap map;
60 DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
62 map[s(".html")] = s("text/html");
63 map[s(".htm")] = s("text/html");
64 map[s(".css")] = s("text/css");
65 map[s(".js")] = s("application/javascript");
66 map[s(".xml")] = s("application/xml");
67 map[s(".txt")] = s("text/plain");
68 map[s(".wav")] = s("audio/x-wav");
69 map[s(".xhtml")] = s("application/xhtml+xml");
70 map[s(".xht")] = s("application/xhtml+xml");
71 map[s(".gif")] = s("image/gif");
72 map[s(".png")] = s("image/png");
73 map[s(".ico")] = s("image/vnd.microsoft.icon");
74 map[s(".svg")] = s("image/svg+xml");
75 map[s(".jpg")] = s("image/jpeg");
80 bool MimeTypeUtils::isMimeTypeSupportedForIcon(const DPL::String& mimeType)
82 return getMimeTypesSupportedForIcon().count(stripMimeParameters(mimeType))
86 DPL::String MimeTypeUtils::stripMimeParameters(const DPL::String& mimeType)
88 size_t parametersStart = mimeType.find_first_of(L';');
89 if (parametersStart != DPL::String::npos) {
90 return mimeType.substr(0, parametersStart);
96 MimeTypeUtils::MimeAttributes MimeTypeUtils::getMimeAttributes(
97 const DPL::String& mimeType)
99 MimeAttributes attributes;
100 std::vector<DPL::String> tokens;
101 DPL::Tokenize(mimeType, L";=", std::back_inserter(tokens));
102 for (unsigned int i = 1; i < tokens.size(); i += 2) {
103 attributes[tokens[i]] = tokens[i + 1];
108 bool MimeTypeUtils::isValidIcon(const DPL::String& path)
110 return getMimeTypesSupportedForIcon().count(identifyFileMimeType(path)) > 0;
113 bool MimeTypeUtils::isValidStartFile(
114 const DPL::String& path,
115 const DPL::OptionalString&
118 DPL::String mimeType = (!!providedMimeType) ? stripMimeParameters(
119 *providedMimeType) : identifyFileMimeType(path);
120 return getMimeTypesSupportedForStartFile().count(mimeType) > 0;
123 DPL::String MimeTypeUtils::getFileNameFromPath(const DPL::String& path)
125 size_t lastSlashPos = path.find_last_of(L'/');
126 return path.substr(lastSlashPos + 1);
129 DPL::String MimeTypeUtils::identifyFileMimeType(const DPL::String& path)
131 DPL::String name = getFileNameFromPath(path); //step 4
133 if (name.size() == 0) {
134 ThrowMsg(Exception::InvalidFileName, "Path should contain a file name.");
137 size_t lastFullStop = name.find_last_of(L'.');
138 if (lastFullStop != 0 && lastFullStop != DPL::String::npos) { //step 5
139 DPL::String extension = name.substr(lastFullStop); //step 6 & 7
140 if (extension.size() > 0) { //step 8
142 std::transform(extension.begin(), extension.end(),
143 extension.begin(), ::towlower);
144 FileIdentificationMap::const_iterator it =
145 getFileIdentificationMap().find(extension);
146 if (it != getFileIdentificationMap().end()) {
151 //TODO step 10 - sniff
152 return DPL::FromASCIIString("application/sniff");