Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / utils / src / mime_type_utils.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
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 #include <stddef.h>
17 #include <set>
18 #include <dpl/assert.h>
19 #include <vector>
20
21 #include <map>
22
23 #include <dpl/utils/mime_type_utils.h>
24
25 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForIcon()
26 {
27     static std::set<DPL::String> set;
28     DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
29     if (set.empty()) {
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"));
35     }
36     return set;
37 }
38
39 const std::set<DPL::String>& MimeTypeUtils::getMimeTypesSupportedForStartFile()
40 {
41     static std::set<DPL::String> set;
42     DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
43     if (set.empty()) {
44         set.insert(s("text/html"));
45         set.insert(s("application/xhtml+xml"));
46         set.insert(s("image/svg+xml"));
47     }
48     return set;
49 }
50
51 bool MimeTypeUtils::isMimeTypeSupportedForStartFile(const DPL::String& mimeType)
52 {
53     return getMimeTypesSupportedForStartFile().count(stripMimeParameters(
54                                                          mimeType)) > 0;
55 }
56
57 const MimeTypeUtils::FileIdentificationMap& MimeTypeUtils::
58     getFileIdentificationMap()
59 {
60     static FileIdentificationMap map;
61     DPL::String (*s)(const std::string&) = DPL::FromASCIIString;
62     if (map.empty()) {
63         map[s(".html")] = s("text/html");
64         map[s(".htm")] = s("text/html");
65         map[s(".css")] = s("text/css");
66         map[s(".js")] = s("application/javascript");
67         map[s(".xml")] = s("application/xml");
68         map[s(".txt")] = s("text/plain");
69         map[s(".wav")] = s("audio/x-wav");
70         map[s(".xhtml")] = s("application/xhtml+xml");
71         map[s(".xht")] = s("application/xhtml+xml");
72         map[s(".gif")] = s("image/gif");
73         map[s(".png")] = s("image/png");
74         map[s(".ico")] = s("image/vnd.microsoft.icon");
75         map[s(".svg")] = s("image/svg+xml");
76         map[s(".jpg")] = s("image/jpeg");
77     }
78     return map;
79 }
80
81 bool MimeTypeUtils::isMimeTypeSupportedForIcon(const DPL::String& mimeType)
82 {
83     return getMimeTypesSupportedForIcon().count(stripMimeParameters(mimeType))
84            > 0;
85 }
86
87 DPL::String MimeTypeUtils::stripMimeParameters(const DPL::String& mimeType)
88 {
89     size_t parametersStart = mimeType.find_first_of(L';');
90     if (parametersStart != DPL::String::npos) {
91         return mimeType.substr(0, parametersStart);
92     } else {
93         return mimeType;
94     }
95 }
96
97 MimeTypeUtils::MimeAttributes MimeTypeUtils::getMimeAttributes(
98     const DPL::String& mimeType)
99 {
100     MimeAttributes attributes;
101     std::vector<DPL::String> tokens;
102     DPL::Tokenize(mimeType, L";=", std::back_inserter(tokens));
103     for (unsigned int i = 1; i < tokens.size(); i += 2) {
104         attributes[tokens[i]] = tokens[i + 1];
105     }
106     return attributes;
107 }
108
109 bool MimeTypeUtils::isValidIcon(const DPL::String& path)
110 {
111     return getMimeTypesSupportedForIcon().count(identifyFileMimeType(path)) > 0;
112 }
113
114 bool MimeTypeUtils::isValidStartFile(
115     const DPL::String& path,
116     const DPL::OptionalString&
117     providedMimeType)
118 {
119     DPL::String mimeType = (!!providedMimeType) ? stripMimeParameters(
120             *providedMimeType) : identifyFileMimeType(path);
121     return getMimeTypesSupportedForStartFile().count(mimeType) > 0;
122 }
123
124 DPL::String MimeTypeUtils::getFileNameFromPath(const DPL::String& path)
125 {
126     size_t lastSlashPos = path.find_last_of(L'/');
127     return path.substr(lastSlashPos + 1);
128 }
129
130 DPL::String MimeTypeUtils::identifyFileMimeType(const DPL::String& path)
131 {
132     DPL::String name = getFileNameFromPath(path); //step 4
133
134     if (name.size() == 0) {
135         ThrowMsg(Exception::InvalidFileName, "Path should contain a file name.");
136     }
137
138     size_t lastFullStop = name.find_last_of(L'.');
139     if (lastFullStop != 0 && lastFullStop != DPL::String::npos) { //step 5
140         DPL::String extension = name.substr(lastFullStop); //step 6 & 7
141         if (extension.size() > 0) { //step 8
142             //step 9
143             std::transform(extension.begin(), extension.end(),
144                            extension.begin(), ::towlower);
145             FileIdentificationMap::const_iterator it =
146                 getFileIdentificationMap().find(extension);
147             if (it != getFileIdentificationMap().end()) {
148                 return it->second;
149             }
150         }
151     }
152     //TODO step 10 - sniff
153     return DPL::FromASCIIString("application/sniff");
154 }