android: Update app-specific/MIME type icons
[LibreOffice.git] / comphelper / source / misc / graphicmimetype.cxx
blob8ae3dad5619cc96f108a61b2572de5faca6b3256
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/graphicmimetype.hxx>
21 #include <comphelper/mediamimetype.hxx>
23 #include <map>
24 #include <set>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/graphic/GraphicProvider.hpp>
29 #include <com/sun/star/graphic/XGraphicProvider.hpp>
30 #include <com/sun/star/io/XInputStream.hpp>
31 #include <com/sun/star/uno/Reference.hxx>
33 #include <comphelper/processfactory.hxx>
34 #include <comphelper/propertyvalue.hxx>
36 using namespace css;
37 using namespace css::beans;
38 using namespace css::graphic;
39 using namespace css::io;
40 using namespace css::uno;
42 namespace comphelper
44 OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(std::string_view rExt)
46 struct XMLGraphicMimeTypeMapper
48 const char* pExt;
49 const char* pMimeType;
52 static const XMLGraphicMimeTypeMapper aMapper[]
53 = { { "gif", "image/gif" }, { "png", "image/png" }, { "jpg", "image/jpeg" },
54 { "tif", "image/tiff" }, { "svg", "image/svg+xml" }, { "pdf", "application/pdf" },
55 { "wmf", "image/x-wmf" }, { "emf", "image/x-emf" }, { "eps", "image/x-eps" },
56 { "bmp", "image/bmp" }, { "pct", "image/x-pict" }, { "svm", "image/x-svm" } };
58 OUString aMimeType;
60 size_t const nCount = std::size(aMapper);
61 for (size_t i = 0; (i < nCount) && aMimeType.isEmpty(); ++i)
63 if (rExt == aMapper[i].pExt)
64 aMimeType = OUString(aMapper[i].pMimeType, strlen(aMapper[i].pMimeType),
65 RTL_TEXTENCODING_ASCII_US);
68 return aMimeType;
71 OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(const Reference<XGraphic>& xGraphic)
73 OUString aSourceMimeType;
74 Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
75 if (xGraphicPropertySet.is() && // it's null if it's an external link
76 (xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
78 return aSourceMimeType;
80 return "";
83 OUString
84 GraphicMimeTypeHelper::GetMimeTypeForImageStream(const Reference<XInputStream>& xInputStream)
86 // Create the graphic to retrieve the mimetype from it
87 Reference<XGraphicProvider> xProvider
88 = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
89 Sequence<PropertyValue> aMediaProperties{ comphelper::makePropertyValue("InputStream",
90 xInputStream) };
91 Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
93 return GetMimeTypeForXGraphic(xGraphic);
96 OUString GraphicMimeTypeHelper::GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
98 switch (convertDataFormat)
100 case ConvertDataFormat::BMP:
101 return "image/bmp";
102 case ConvertDataFormat::GIF:
103 return "image/gif";
104 case ConvertDataFormat::JPG:
105 return "image/jpeg";
106 case ConvertDataFormat::PCT:
107 return "image/x-pict";
108 case ConvertDataFormat::PNG:
109 return "image/png";
110 case ConvertDataFormat::SVM:
111 return "image/x-svm";
112 case ConvertDataFormat::TIF:
113 return "image/tiff";
114 case ConvertDataFormat::WMF:
115 return "image/x-wmf";
116 case ConvertDataFormat::EMF:
117 return "image/x-emf";
118 case ConvertDataFormat::SVG:
119 return "image/svg+xml";
120 case ConvertDataFormat::MET: // What is this?
121 case ConvertDataFormat::Unknown:
122 default:
123 return "";
127 char const* GraphicMimeTypeHelper::GetExtensionForConvertDataFormat(ConvertDataFormat nFormat)
129 char const* pExt = nullptr;
130 // create extension
131 if (nFormat != ConvertDataFormat::Unknown)
133 switch (nFormat)
135 case ConvertDataFormat::BMP:
136 pExt = ".bmp";
137 break;
138 case ConvertDataFormat::GIF:
139 pExt = ".gif";
140 break;
141 case ConvertDataFormat::JPG:
142 pExt = ".jpg";
143 break;
144 case ConvertDataFormat::MET:
145 pExt = ".met";
146 break;
147 case ConvertDataFormat::PCT:
148 pExt = ".pct";
149 break;
150 case ConvertDataFormat::PNG:
151 pExt = ".png";
152 break;
153 case ConvertDataFormat::SVM:
154 pExt = ".svm";
155 break;
156 case ConvertDataFormat::TIF:
157 pExt = ".tif";
158 break;
159 case ConvertDataFormat::WMF:
160 pExt = ".wmf";
161 break;
162 case ConvertDataFormat::EMF:
163 pExt = ".emf";
164 break;
166 default:
167 pExt = ".grf";
168 break;
171 return pExt;
174 static auto GetMediaMimes() -> std::map<OString, OString> const&
176 static std::map<OString, OString> const mimes = {
177 { "mp4", "video/mp4" },
178 { "ts", "video/MP2T" },
179 { "mpeg", "video/mpeg" },
180 { "mpg", "video/mpeg" },
181 { "mkv", "video/x-matroska" },
182 { "webm", "video/webm" },
183 { "ogv", "video/ogg" },
184 { "mov", "video/quicktime" },
185 { "wmv", "video/x-ms-wmv" },
186 { "avi", "video/x-msvideo" },
187 { "m4a", "audio/mp4" },
188 { "aac", "audio/aac" },
189 { "mp3", "audio/mpeg" }, // https://bugs.chromium.org/p/chromium/issues/detail?id=227004
190 { "ogg", "audio/ogg" },
191 { "oga", "audio/ogg" },
192 { "opus", "audio/ogg" },
193 { "flac", "audio/flac" }, // missing at IANA?
194 // note there is RFC 2631 but i got the impression that vnd.wave
195 // requires specifying the codec in the container; also this page
196 // says "Historic" whatever that means:
197 // https://www.iana.org/assignments/wave-avi-codec-registry/wave-avi-codec-registry.xhtml
198 { "wav", "audio/x-wav" },
200 return mimes;
203 auto IsMediaMimeType(::std::string_view const rMimeType) -> bool
205 return IsMediaMimeType(OStringToOUString(rMimeType, RTL_TEXTENCODING_UTF8));
208 auto IsMediaMimeType(OUString const& rMimeType) -> bool
210 static std::set<OUString> mimes;
211 if (mimes.empty())
213 auto const& rMap(GetMediaMimes());
214 for (auto const& it : rMap)
216 mimes.insert(OStringToOUString(it.second, RTL_TEXTENCODING_UTF8));
219 return rMimeType == AVMEDIA_MIMETYPE_COMMON || mimes.find(rMimeType) != mimes.end();
222 auto GuessMediaMimeType(::std::u16string_view rFileName) -> OUString
224 if (auto const i = rFileName.rfind('.'); i != ::std::string_view::npos)
226 OString const ext(OUStringToOString(rFileName.substr(i + 1), RTL_TEXTENCODING_UTF8));
227 auto const& rMap(GetMediaMimes());
228 auto const it(rMap.find(ext));
229 if (it != rMap.end())
231 return OStringToOUString(it->second, RTL_TEXTENCODING_ASCII_US);
234 return OUString();
237 } // namespace comphelper
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */