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.
17 #include <dpl/assert.h>
22 #include <mime_type_utils.h>
24 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForIcon()
26 static std::set<DPL::String> set;
27 DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
29 set.insert(s("image/gif"));
30 set.insert(s("image/png"));
31 set.insert(s("image/vnd.microsoft.icon"));
32 set.insert(s("image/svg+xml"));
33 set.insert(s("image/jpeg"));
38 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForStartFile()
40 static std::set<DPL::String> set;
41 DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
43 set.insert(s("text/html"));
44 set.insert(s("application/xhtml+xml"));
49 bool MimeTypeUtils::isMimeTypeSupportedForStartFile(const DPL::String& mimeType)
51 return getMimeTypesSupportedForStartFile().count(stripMimeParameters(
55 const MimeTypeUtils::FileIdentificationMap& MimeTypeUtils::
56 getFileIdentificationMap()
58 static FileIdentificationMap map;
59 DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
61 map[s(".html")] = s("text/html");
62 map[s(".htm")] = s("text/html");
63 map[s(".css")] = s("text/css");
64 map[s(".js")] = s("application/javascript");
65 map[s(".xml")] = s("application/xml");
66 map[s(".txt")] = s("text/plain");
67 map[s(".wav")] = s("audio/x-wav");
68 map[s(".xhtml")] = s("application/xhtml+xml");
69 map[s(".xht")] = s("application/xhtml+xml");
70 map[s(".gif")] = s("image/gif");
71 map[s(".png")] = s("image/png");
72 map[s(".ico")] = s("image/vnd.microsoft.icon");
73 map[s(".svg")] = s("image/svg+xml");
74 map[s(".jpg")] = s("image/jpeg");
79 bool MimeTypeUtils::isMimeTypeSupportedForIcon(const DPL::String& mimeType)
81 return getMimeTypesSupportedForIcon().count(stripMimeParameters(mimeType))
85 DPL::String MimeTypeUtils::stripMimeParameters(const DPL::String& mimeType)
87 size_t parametersStart = mimeType.find_first_of(L';');
88 if (parametersStart != DPL::String::npos) {
89 return mimeType.substr(0, parametersStart);
95 MimeTypeUtils::MimeAttributes MimeTypeUtils::getMimeAttributes(
96 const DPL::String& mimeType)
98 MimeAttributes attributes;
99 std::vector<DPL::String> tokens;
100 DPL::Tokenize(mimeType, L";=", std::back_inserter(tokens));
101 for (unsigned int i = 1; i < tokens.size(); i += 2) {
102 attributes[tokens[i]] = tokens[i + 1];
107 bool MimeTypeUtils::isValidIcon(const DPL::String& path)
109 return getMimeTypesSupportedForIcon().count(identifyFileMimeType(path)) > 0;
112 bool MimeTypeUtils::isValidStartFile(const DPL::String& path,
113 const DPL::OptionalString& providedMimeType)
115 DPL::String mimeType = (!!providedMimeType) ? stripMimeParameters(
116 *providedMimeType) : identifyFileMimeType(path);
117 return getMimeTypesSupportedForStartFile().count(mimeType) > 0;
120 DPL::String MimeTypeUtils::getFileNameFromPath(const DPL::String& path)
122 size_t lastSlashPos = path.find_last_of(L'/');
123 return path.substr(lastSlashPos + 1);
126 DPL::String MimeTypeUtils::identifyFileMimeType(const DPL::String& path)
128 DPL::String name = getFileNameFromPath(path); //step 4
130 if (name.size() == 0) {
131 ThrowMsg(Exception::InvalidFileName, "Path should contain a file name.");
134 size_t lastFullStop = name.find_last_of(L'.');
135 if (lastFullStop != 0 && lastFullStop != DPL::String::npos) { //step 5
136 DPL::String extension = name.substr(lastFullStop); //step 6 & 7
137 if (extension.size() > 0) { //step 8
139 std::transform(extension.begin(), extension.end(),
140 extension.begin(), ::towlower);
141 FileIdentificationMap::const_iterator it =
142 getFileIdentificationMap().find(extension);
143 if (it != getFileIdentificationMap().end()) {
148 //TODO step 10 - sniff
149 return DPL::FromASCIIString("application/sniff");