LibreOffice Module vcl (master) 1
FilterConfigCache.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "FilterConfigCache.hxx"
21
22#include <o3tl/safeint.hxx>
23#include <vcl/graphicfilter.hxx>
25#include <tools/svlibrary.h>
26#include <com/sun/star/uno/Any.h>
29#include <com/sun/star/uno/Exception.hpp>
30#include <com/sun/star/lang/XMultiServiceFactory.hpp>
31#include <com/sun/star/beans/PropertyValue.hpp>
32#include <com/sun/star/beans/XPropertySet.hpp>
33#include <com/sun/star/configuration/theDefaultProvider.hpp>
34#include <com/sun/star/container/XNameAccess.hpp>
35
36using namespace ::com::sun::star::lang ; // XMultiServiceFactory
37using namespace ::com::sun::star::container ; // XNameAccess
38using namespace ::com::sun::star::uno ; // Reference
39using namespace ::com::sun::star::beans ; // PropertyValue
40using namespace ::com::sun::star::configuration ;
41
43{
48 nullptr
49};
50
52{
53 bIsPixelFormat = false;
54 sFilterName = rUserDataEntry;
55 const char** pPtr;
56 for ( pPtr = InternalPixelFilterNameList; *pPtr; pPtr++ )
57 {
58 if ( sFilterName.equalsIgnoreAsciiCaseAscii( *pPtr ) )
59 {
60 bIsPixelFormat = true;
61 }
62 }
63}
64
66{
67 OUString aShortName;
68 if ( !lExtensionList.empty() )
69 {
70 aShortName = lExtensionList[ 0 ];
71 if ( aShortName.startsWith( "*." ) )
72 aShortName = aShortName.replaceAt( 0, 2, u"" );
73 }
74 return aShortName;
75}
76
89static Reference< XInterface > openConfig(const char* sPackage)
90{
91 Reference< XComponentContext > xContext(
93 Reference< XInterface > xCfg;
94 try
95 {
96 // get access to config API (not to file!)
97 Reference< XMultiServiceFactory > xConfigProvider = theDefaultProvider::get( xContext );
98
99 PropertyValue aParam ;
100
101 // define cfg path for open
102 aParam.Name = "nodepath";
103 if (rtl_str_compareIgnoreAsciiCase(sPackage, "types") == 0)
104 aParam.Value <<= OUString( "/org.openoffice.TypeDetection.Types/Types" );
105 if (rtl_str_compareIgnoreAsciiCase(sPackage, "filters") == 0)
106 aParam.Value <<= OUString( "/org.openoffice.TypeDetection.GraphicFilter/Filters" );
107 Sequence< Any > lParams{ Any(aParam) };
108
109 // get access to file
110 xCfg = xConfigProvider->createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", lParams);
111 }
112 catch(const RuntimeException&)
113 { throw; }
114 catch(const Exception&)
115 { xCfg.clear(); }
116
117 return xCfg;
118}
119
121{
122 static constexpr OUStringLiteral STYPE ( u"Type" );
123 static constexpr OUStringLiteral SUINAME ( u"UIName" );
124 static constexpr OUStringLiteral SFLAGS ( u"Flags" );
125 static constexpr OUStringLiteral SMEDIATYPE ( u"MediaType" );
126 static constexpr OUStringLiteral SEXTENSIONS ( u"Extensions" );
127 static constexpr OUStringLiteral SFORMATNAME ( u"FormatName" );
128 static constexpr OUStringLiteral SREALFILTERNAME ( u"RealFilterName" );
129
130 // get access to config
131 Reference< XNameAccess > xTypeAccess ( openConfig("types" ), UNO_QUERY );
132 Reference< XNameAccess > xFilterAccess( openConfig("filters"), UNO_QUERY );
133
134 if ( !(xTypeAccess.is() && xFilterAccess.is()) )
135 return;
136
137 const Sequence< OUString > lAllFilter = xFilterAccess->getElementNames();
138
139 for ( const OUString& sInternalFilterName : lAllFilter )
140 {
141 Reference< XPropertySet > xFilterSet;
142 xFilterAccess->getByName( sInternalFilterName ) >>= xFilterSet;
143 if (!xFilterSet.is())
144 continue;
145
147
148 aEntry.sInternalFilterName = sInternalFilterName;
149 xFilterSet->getPropertyValue(STYPE) >>= aEntry.sType;
150 xFilterSet->getPropertyValue(SUINAME) >>= aEntry.sUIName;
151 xFilterSet->getPropertyValue(SREALFILTERNAME) >>= aEntry.sFilterType;
152 Sequence< OUString > lFlags;
153 xFilterSet->getPropertyValue(SFLAGS) >>= lFlags;
154 if (lFlags.getLength()!=1 || lFlags[0].isEmpty())
155 continue;
156 if (lFlags[0].equalsIgnoreAsciiCase("import"))
157 aEntry.nFlags = 1;
158 else if (lFlags[0].equalsIgnoreAsciiCase("export"))
159 aEntry.nFlags = 2;
160 else
161 aEntry.nFlags = 0;
162
163 OUString sFormatName;
164 xFilterSet->getPropertyValue(SFORMATNAME) >>= sFormatName;
165 aEntry.CreateFilterName( sFormatName );
166
167 Reference< XPropertySet > xTypeSet;
168 xTypeAccess->getByName( aEntry.sType ) >>= xTypeSet;
169 if (!xTypeSet.is())
170 continue;
171
172 xTypeSet->getPropertyValue(SMEDIATYPE) >>= aEntry.sMediaType;
173 css::uno::Sequence<OUString> tmp;
174 if (xTypeSet->getPropertyValue(SEXTENSIONS) >>= tmp)
175 aEntry.lExtensionList = comphelper::sequenceToContainer<std::vector<OUString>>(tmp);
176
177 // The first extension will be used
178 // to generate our internal FilterType ( BMP, WMF ... )
179 OUString aExtension( aEntry.GetShortName() );
180 if (aExtension.isEmpty())
181 continue;
182
183 if ( aEntry.nFlags & 1 )
184 aImport.push_back( aEntry );
185 if ( aEntry.nFlags & 2 )
186 aExport.push_back( aEntry );
187
188 // bFilterEntryCreated!?
189 if (!( aEntry.nFlags & 3 ))
190 continue; //? Entry was already inserted ... but following code will be suppressed?!
191 }
192};
193
195{
196 "bmp","1","SVBMP",
197 "bmp","2","SVBMP",
198 "dxf","1","SVDXF",
199 "eps","1","SVIEPS",
200 "eps","2","SVEEPS",
201 "gif","1","SVIGIF",
202 "gif","2","SVEGIF",
203 "jpg","1","SVIJPEG",
204 "jpg","2","SVEJPEG",
205 "mov","1","SVMOV",
206 "mov","2","SVMOV",
207 "met","1","SVMET",
208 "png","1","SVIPNG",
209 "png","2","SVEPNG",
210 "pct","1","SVPICT",
211 "pcd","1","SVPCD",
212 "psd","1","SVPSD",
213 "pcx","1","SVPCX",
214 "pbm","1","SVPBM",
215 "pgm","1","SVPBM",
216 "ppm","1","SVPBM",
217 "ras","1","SVRAS",
218 "svm","1","SVMETAFILE",
219 "svm","2","SVMETAFILE",
220 "tga","1","SVTGA",
221 "tif","1","SVTIFF",
222 "tif","2","SVTIFF",
223 "emf","1","SVEMF",
224 "emf","2","SVEMF",
225 "wmf","1","SVWMF",
226 "wmf","2","SVWMF",
227 "xbm","1","SVIXBM",
228 "xpm","1","SVIXPM",
229 "svg","1","SVISVG",
230 "svg","2","SVESVG",
231 "webp","1","SVIWEBP",
232 "webp","2","SVEWEBP",
233 nullptr
234};
235
237{
238 const char** pPtr;
239 for ( pPtr = InternalFilterListForSvxLight; *pPtr; pPtr++ )
240 {
242
243 OUString sExtension( OUString::createFromAscii( *pPtr++ ) );
244
245 aEntry.lExtensionList.push_back(sExtension);
246
247 aEntry.sType = sExtension;
248 aEntry.sUIName = sExtension;
249
250 OString sFlags( *pPtr++ );
251 aEntry.nFlags = sFlags.toInt32();
252
253 OUString sUserData( OUString::createFromAscii( *pPtr ) );
254 aEntry.CreateFilterName( sUserData );
255
256 if ( aEntry.nFlags & 1 )
257 aImport.push_back( aEntry );
258 if ( aEntry.nFlags & 2 )
259 aExport.push_back( aEntry );
260 }
261}
262
264{
265 if (bConfig)
267 if (bConfig)
268 ImplInit();
269 else
271}
272
274{
275}
276
277OUString FilterConfigCache::GetImportFilterName( sal_uInt16 nFormat )
278{
279 if( nFormat < aImport.size() )
280 return aImport[ nFormat ].sFilterName;
281 return OUString();
282}
283
284sal_uInt16 FilterConfigCache::GetImportFormatNumber( std::u16string_view rFormatName )
285{
286 sal_uInt16 nPos = 0;
287 for (auto const& elem : aImport)
288 {
289 if ( elem.sUIName.equalsIgnoreAsciiCase( rFormatName ) )
290 return nPos;
291 ++nPos;
292 }
294}
295
297sal_uInt16 FilterConfigCache::GetImportFormatNumberForExtension( std::u16string_view rExt )
298{
299 sal_uInt16 nPos = 0;
300 for (auto const& elem : aImport)
301 {
302 for ( OUString const & s : elem.lExtensionList )
303 {
304 if ( s.equalsIgnoreAsciiCase( rExt ) )
305 return nPos;
306 }
307 ++nPos;
308 }
310}
311
312sal_uInt16 FilterConfigCache::GetImportFormatNumberForShortName( std::u16string_view rShortName )
313{
314 sal_uInt16 nPos = 0;
315 for (auto & elem : aImport)
316 {
317 if ( elem.GetShortName().equalsIgnoreAsciiCase( rShortName ) )
318 return nPos;
319 ++nPos;
320 }
322}
323
324sal_uInt16 FilterConfigCache::GetImportFormatNumberForTypeName( std::u16string_view rType )
325{
326 sal_uInt16 nPos = 0;
327 for (auto const& elem : aImport)
328 {
329 if ( elem.sType.equalsIgnoreAsciiCase( rType ) )
330 return nPos;
331 ++nPos;
332 }
334}
335
336OUString FilterConfigCache::GetImportFormatName( sal_uInt16 nFormat )
337{
338 if( nFormat < aImport.size() )
339 return aImport[ nFormat ].sUIName;
340 return OUString();
341}
342
344{
345 if( nFormat < aImport.size() )
346 return aImport[ nFormat ].sMediaType;
347 return OUString();
348}
349
351{
352 if( nFormat < aImport.size() )
353 return aImport[ nFormat ].GetShortName();
354 return OUString();
355}
356
357OUString FilterConfigCache::GetImportFormatExtension( sal_uInt16 nFormat, sal_Int32 nEntry )
358{
359 if ( (nFormat < aImport.size()) && (o3tl::make_unsigned(nEntry) < aImport[ nFormat ].lExtensionList.size()) )
360 return aImport[ nFormat ].lExtensionList[ nEntry ];
361 return OUString();
362}
363
364OUString FilterConfigCache::GetImportFilterType( sal_uInt16 nFormat )
365{
366 if( nFormat < aImport.size() )
367 return aImport[ nFormat ].sType;
368 return OUString();
369}
370
371OUString FilterConfigCache::GetImportFilterTypeName( sal_uInt16 nFormat )
372{
373 if( nFormat < aImport.size() )
374 return aImport[ nFormat ].sFilterType;
375 return OUString();
376}
377
378OUString FilterConfigCache::GetImportWildcard(sal_uInt16 nFormat, sal_Int32 nEntry)
379{
380 OUString aWildcard( GetImportFormatExtension( nFormat, nEntry ) );
381 if ( !aWildcard.isEmpty() )
382 aWildcard = aWildcard.replaceAt( 0, 0, u"*." );
383 return aWildcard;
384}
385
386OUString FilterConfigCache::GetExportFilterName( sal_uInt16 nFormat )
387{
388 if( nFormat < aExport.size() )
389 return aExport[ nFormat ].sFilterName;
390 return OUString();
391}
392
393sal_uInt16 FilterConfigCache::GetExportFormatNumber(std::u16string_view rFormatName)
394{
395 sal_uInt16 nPos = 0;
396 for (auto const& elem : aExport)
397 {
398 if ( elem.sUIName.equalsIgnoreAsciiCase( rFormatName ) )
399 return nPos;
400 ++nPos;
401 }
403}
404
405sal_uInt16 FilterConfigCache::GetExportFormatNumberForMediaType( std::u16string_view rMediaType )
406{
407 sal_uInt16 nPos = 0;
408 for (auto const& elem : aExport)
409 {
410 if ( elem.sMediaType.equalsIgnoreAsciiCase( rMediaType ) )
411 return nPos;
412 ++nPos;
413 }
415}
416
417sal_uInt16 FilterConfigCache::GetExportFormatNumberForShortName( std::u16string_view rShortName )
418{
419 sal_uInt16 nPos = 0;
420 for (auto & elem : aExport)
421 {
422 if ( elem.GetShortName().equalsIgnoreAsciiCase( rShortName ) )
423 return nPos;
424 ++nPos;
425 }
427}
428
429sal_uInt16 FilterConfigCache::GetExportFormatNumberForTypeName( std::u16string_view rType )
430{
431 sal_uInt16 nPos = 0;
432 for (auto const& elem : aExport)
433 {
434 if ( elem.sType.equalsIgnoreAsciiCase( rType ) )
435 return nPos;
436 ++nPos;
437 }
439}
440
441OUString FilterConfigCache::GetExportFormatName( sal_uInt16 nFormat )
442{
443 if( nFormat < aExport.size() )
444 return aExport[ nFormat ].sUIName;
445 return OUString();
446}
447
449{
450 if( nFormat < aExport.size() )
451 return aExport[ nFormat ].sMediaType;
452 return OUString();
453}
454
456{
457 if( nFormat < aExport.size() )
458 return aExport[ nFormat ].GetShortName();
459 return OUString();
460}
461
462OUString FilterConfigCache::GetExportFormatExtension( sal_uInt16 nFormat, sal_Int32 nEntry )
463{
464 if ( (nFormat < aExport.size()) && (o3tl::make_unsigned(nEntry) < aExport[ nFormat ].lExtensionList.size()) )
465 return aExport[ nFormat ].lExtensionList[ nEntry ];
466 return OUString();
467}
468
470{
471 if( nFormat < aExport.size() )
472 return aExport[ nFormat ].sInternalFilterName;
473 return OUString();
474}
475
476OUString FilterConfigCache::GetExportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry )
477{
478 OUString aWildcard( GetExportFormatExtension( nFormat, nEntry ) );
479 if ( !aWildcard.isEmpty() )
480 aWildcard = aWildcard.replaceAt( 0, 0, u"*." );
481 return aWildcard;
482}
483
485{
486 return (nFormat < aExport.size()) && aExport[ nFormat ].bIsPixelFormat;
487}
488
489/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static Reference< XInterface > openConfig(const char *sPackage)
helper to open the configuration root of the underlying config package
bool IsExportPixelFormat(sal_uInt16 nFormat)
sal_uInt16 GetExportFormatNumberForShortName(std::u16string_view rShortName)
OUString GetImportFormatName(sal_uInt16 nFormat)
sal_uInt16 GetImportFormatNumber(std::u16string_view rFormatName)
sal_uInt16 GetExportFormatNumber(std::u16string_view rFormatName)
sal_uInt16 GetImportFormatNumberForShortName(std::u16string_view rShortName)
FilterConfigCache(bool bUseConfig)
OUString GetExportWildcard(sal_uInt16 nFormat, sal_Int32 nEntry)
OUString GetImportFormatShortName(sal_uInt16 nFormat)
sal_uInt16 GetImportFormatNumberForExtension(std::u16string_view rExt)
get the index of the filter that matches this extension
OUString GetImportWildcard(sal_uInt16 nFormat, sal_Int32 nEntry)
OUString GetImportFilterTypeName(sal_uInt16 nFormat)
OUString GetExportFilterName(sal_uInt16 nFormat)
static const char * InternalFilterListForSvxLight[]
OUString GetExportFormatShortName(sal_uInt16 nFormat)
OUString GetExportFormatName(sal_uInt16 nFormat)
OUString GetExportFormatExtension(sal_uInt16 nFormat, sal_Int32 nEntry=0)
OUString GetImportFilterName(sal_uInt16 nFormat)
sal_uInt16 GetExportFormatNumberForTypeName(std::u16string_view rType)
std::vector< FilterConfigCacheEntry > aExport
sal_uInt16 GetExportFormatNumberForMediaType(std::u16string_view rMediaType)
sal_uInt16 GetImportFormatNumberForTypeName(std::u16string_view rType)
OUString GetImportFormatExtension(sal_uInt16 nFormat, sal_Int32 nEntry=0)
OUString GetImportFilterType(sal_uInt16 nFormat)
OUString GetExportFormatMediaType(sal_uInt16 nFormat)
OUString GetImportFormatMediaType(sal_uInt16 nFormat)
std::vector< FilterConfigCacheEntry > aImport
OUString GetExportInternalFilterName(sal_uInt16 nFormat)
static bool IsFuzzing()
float u
OUString aWildcard
#define IMP_BMP
#define EXP_BMP
#define GRFILTER_FORMAT_NOTFOUND
#define IMP_TIFF
#define IMP_PICT
#define IMP_PNG
#define IMP_PCD
#define IMP_JPEG
#define IMP_XBM
#define IMP_GIF
#define IMP_WEBP
#define IMP_PSD
#define IMP_XPM
#define EXP_GIF
#define IMP_DXF
#define IMP_MET
#define IMP_TGA
#define IMP_RAS
#define EXP_TIFF
#define IMP_PCX
#define EXP_PNG
#define IMP_PBM
#define EXP_JPEG
#define EXP_WEBP
#define IMP_MOV
sal_uInt16 nPos
@ Exception
Reference< XComponentContext > getProcessComponentContext()
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
void CreateFilterName(const OUString &rUserDataEntry)