Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / vcl / source / filter / FilterConfigCache.cxx
blob217888ee1b6c3c0adee00b133c35942a682a882c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "FilterConfigCache.hxx"
22 #include <vcl/graphicfilter.hxx>
23 #include <com/sun/star/uno/Any.h>
24 #include <comphelper/processfactory.hxx>
25 #include <com/sun/star/uno/Exception.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/configuration/theDefaultProvider.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
31 using namespace ::com::sun::star::lang ; // XMultiServiceFactory
32 using namespace ::com::sun::star::container ; // XNameAccess
33 using namespace ::com::sun::star::uno ; // Reference
34 using namespace ::com::sun::star::beans ; // PropertyValue
35 using namespace ::com::sun::star::configuration ;
37 const char* FilterConfigCache::FilterConfigCacheEntry::InternalPixelFilterNameList[] =
39 IMP_BMP, IMP_GIF, IMP_PNG,IMP_JPEG, IMP_XBM, IMP_XPM,
40 EXP_BMP, EXP_JPEG, EXP_PNG, NULL
43 const char* FilterConfigCache::FilterConfigCacheEntry::InternalVectorFilterNameList[] =
45 IMP_SVMETAFILE, IMP_WMF, IMP_EMF, IMP_SVSGF, IMP_SVSGV, IMP_SVG,
46 EXP_SVMETAFILE, EXP_WMF, EXP_EMF, EXP_SVG, NULL
49 const char* FilterConfigCache::FilterConfigCacheEntry::ExternalPixelFilterNameList[] =
51 "egi", "icd", "ipd", "ipx", "ipb", "epb", "epg",
52 "epp", "ira", "era", "itg", "iti", "eti", "exp", NULL
55 sal_Bool FilterConfigCache::bInitialized = sal_False;
56 sal_Int32 FilterConfigCache::nIndType = -1;
57 sal_Int32 FilterConfigCache::nIndUIName = -1;
58 sal_Int32 FilterConfigCache::nIndDocumentService = -1;
59 sal_Int32 FilterConfigCache::nIndFilterService = -1;
60 sal_Int32 FilterConfigCache::nIndFlags = -1;
61 sal_Int32 FilterConfigCache::nIndUserData = -1;
62 sal_Int32 FilterConfigCache::nIndFileFormatVersion = -1;
63 sal_Int32 FilterConfigCache::nIndTemplateName = -1;
65 sal_Bool FilterConfigCache::FilterConfigCacheEntry::CreateFilterName( const OUString& rUserDataEntry )
67 bIsPixelFormat = bIsInternalFilter = sal_False;
68 sFilterName = rUserDataEntry;
69 const char** pPtr;
70 for ( pPtr = InternalPixelFilterNameList; *pPtr && ( bIsInternalFilter == sal_False ); pPtr++ )
72 if ( sFilterName.equalsIgnoreAsciiCase( OUString(*pPtr, strlen(*pPtr), RTL_TEXTENCODING_ASCII_US) ) )
74 bIsInternalFilter = sal_True;
75 bIsPixelFormat = sal_True;
78 for ( pPtr = InternalVectorFilterNameList; *pPtr && ( bIsInternalFilter == sal_False ); pPtr++ )
80 if ( sFilterName.equalsIgnoreAsciiCase( OUString(*pPtr, strlen(*pPtr), RTL_TEXTENCODING_ASCII_US) ) )
81 bIsInternalFilter = sal_True;
83 if ( !bIsInternalFilter )
85 for ( pPtr = ExternalPixelFilterNameList; *pPtr && ( bIsPixelFormat == sal_False ); pPtr++ )
87 if ( sFilterName.equalsIgnoreAsciiCase( OUString(*pPtr, strlen(*pPtr), RTL_TEXTENCODING_ASCII_US) ) )
88 bIsPixelFormat = sal_True;
90 OUString sTemp(SVLIBRARY("?"));
91 sFilterName = sTemp.replaceFirst("?", sFilterName);
93 return ! sFilterName.isEmpty();
96 OUString FilterConfigCache::FilterConfigCacheEntry::GetShortName()
98 OUString aShortName;
99 if ( lExtensionList.getLength() )
101 aShortName = lExtensionList[ 0 ];
102 if ( aShortName.startsWith( "*." ) )
103 aShortName = aShortName.replaceAt( 0, 2, "" );
105 return aShortName;
108 /** helper to open the configuration root of the underlying
109 config package
111 @param sPackage
112 specify, which config package should be opened.
113 Must be one of "types" or "filters"
115 @return A valid object if open was successful. The access on opened
116 data will be readonly. It returns NULL in case open failed.
118 @throws It let pass RuntimeExceptions only.
120 Reference< XInterface > openConfig(const char* sPackage)
121 throw(RuntimeException)
123 Reference< XComponentContext > xContext(
124 comphelper::getProcessComponentContext() );
125 Reference< XInterface > xCfg;
128 // get access to config API (not to file!)
129 Reference< XMultiServiceFactory > xConfigProvider = theDefaultProvider::get( xContext );
131 Sequence< Any > lParams(1);
132 PropertyValue aParam ;
134 // define cfg path for open
135 aParam.Name = "nodepath";
136 if (rtl_str_compareIgnoreAsciiCase(sPackage, "types") == 0)
137 aParam.Value <<= OUString( "/org.openoffice.TypeDetection.Types/Types" );
138 if (rtl_str_compareIgnoreAsciiCase(sPackage, "filters") == 0)
139 aParam.Value <<= OUString( "/org.openoffice.TypeDetection.GraphicFilter/Filters" );
140 lParams[0] = makeAny(aParam);
142 // get access to file
143 xCfg = xConfigProvider->createInstanceWithArguments(
144 OUString( "com.sun.star.configuration.ConfigurationAccess" ), lParams);
146 catch(const RuntimeException&)
147 { throw; }
148 catch(const Exception&)
149 { xCfg.clear(); }
151 return xCfg;
154 void FilterConfigCache::ImplInit()
156 OUString STYPE ( "Type" );
157 OUString SUINAME ( "UIName" );
158 OUString SFLAGS ( "Flags" );
159 OUString SMEDIATYPE ( "MediaType" );
160 OUString SEXTENSIONS ( "Extensions" );
161 OUString SFORMATNAME ( "FormatName" );
162 OUString SREALFILTERNAME ( "RealFilterName" );
164 // get access to config
165 Reference< XNameAccess > xTypeAccess ( openConfig("types" ), UNO_QUERY );
166 Reference< XNameAccess > xFilterAccess( openConfig("filters"), UNO_QUERY );
168 if ( xTypeAccess.is() && xFilterAccess.is() )
170 Sequence< OUString > lAllFilter = xFilterAccess->getElementNames();
171 sal_Int32 nAllFilterCount = lAllFilter.getLength();
173 for ( sal_Int32 i = 0; i < nAllFilterCount; i++ )
175 OUString sInternalFilterName = lAllFilter[ i ];
176 Reference< XPropertySet > xFilterSet;
177 xFilterAccess->getByName( sInternalFilterName ) >>= xFilterSet;
178 if (!xFilterSet.is())
179 continue;
181 FilterConfigCacheEntry aEntry;
183 aEntry.sInternalFilterName = sInternalFilterName;
184 xFilterSet->getPropertyValue(STYPE) >>= aEntry.sType;
185 xFilterSet->getPropertyValue(SUINAME) >>= aEntry.sUIName;
186 xFilterSet->getPropertyValue(SREALFILTERNAME) >>= aEntry.sFilterType;
187 Sequence< OUString > lFlags;
188 xFilterSet->getPropertyValue(SFLAGS) >>= lFlags;
189 if (lFlags.getLength()!=1 || lFlags[0].isEmpty())
190 continue;
191 if (lFlags[0].equalsIgnoreAsciiCase("import"))
192 aEntry.nFlags = 1;
193 else if (lFlags[0].equalsIgnoreAsciiCase("export"))
194 aEntry.nFlags = 2;
196 OUString sFormatName;
197 xFilterSet->getPropertyValue(SFORMATNAME) >>= sFormatName;
198 aEntry.CreateFilterName( sFormatName );
200 Reference< XPropertySet > xTypeSet;
201 xTypeAccess->getByName( aEntry.sType ) >>= xTypeSet;
202 if (!xTypeSet.is())
203 continue;
205 xTypeSet->getPropertyValue(SMEDIATYPE) >>= aEntry.sMediaType;
206 xTypeSet->getPropertyValue(SEXTENSIONS) >>= aEntry.lExtensionList;
208 // The first extension will be used
209 // to generate our internal FilterType ( BMP, WMF ... )
210 OUString aExtension( aEntry.GetShortName() );
211 if (aExtension.getLength() != 3)
212 continue;
214 if ( aEntry.nFlags & 1 )
215 aImport.push_back( aEntry );
216 if ( aEntry.nFlags & 2 )
217 aExport.push_back( aEntry );
219 // bFilterEntryCreated!?
220 if (!( aEntry.nFlags & 3 ))
221 continue; //? Entry was already inserted ... but following code will be supressed?!
226 const char* FilterConfigCache::InternalFilterListForSvxLight[] =
228 "bmp","1","SVBMP",
229 "bmp","2","SVBMP",
230 "dxf","1","idx",
231 "eps","1","ips",
232 "eps","2","eps",
233 "gif","1","SVIGIF",
234 "gif","2","egi",
235 "jpg","1","SVIJPEG",
236 "jpg","2","SVEJPEG",
237 "sgv","1","SVSGV",
238 "sgf","1","SVSGF",
239 "met","1","ime",
240 "met","2","eme",
241 "png","1","SVIPNG",
242 "png","2","SVEPNG",
243 "pct","1","ipt",
244 "pct","2","ept",
245 "pcd","1","icd",
246 "psd","1","ipd",
247 "pcx","1","ipx",
248 "pbm","1","ipb",
249 "pbm","2","epb",
250 "pgm","1","ipb",
251 "pgm","2","epg",
252 "ppm","1","ipb",
253 "ppm","2","epp",
254 "ras","1","ira",
255 "ras","2","era",
256 "svm","1","SVMETAFILE",
257 "svm","2","SVMETAFILE",
258 "tga","1","itg",
259 "tif","1","iti",
260 "tif","2","eti",
261 "emf","1","SVEMF",
262 "emf","2","SVEMF",
263 "wmf","1","SVWMF",
264 "wmf","2","SVWMF",
265 "xbm","1","SVIXBM",
266 "xpm","1","SVIXPM",
267 "xpm","2","exp",
268 "svg","1","SVISVG",
269 "svg","2","SVESVG",
270 NULL
273 void FilterConfigCache::ImplInitSmart()
275 const char** pPtr;
276 for ( pPtr = InternalFilterListForSvxLight; *pPtr; pPtr++ )
278 FilterConfigCacheEntry aEntry;
280 OUString sExtension( OUString::createFromAscii( *pPtr++ ) );
282 aEntry.lExtensionList.realloc( 1 );
283 aEntry.lExtensionList[ 0 ] = sExtension;
285 aEntry.sType = sExtension;
286 aEntry.sUIName = sExtension;
288 OString sFlags( *pPtr++ );
289 aEntry.nFlags = sFlags.toInt32();
291 OUString sUserData( OUString::createFromAscii( *pPtr ) );
292 aEntry.CreateFilterName( sUserData );
294 if ( aEntry.nFlags & 1 )
295 aImport.push_back( aEntry );
296 if ( aEntry.nFlags & 2 )
297 aExport.push_back( aEntry );
301 // ------------------------------------------------------------------------
303 FilterConfigCache::FilterConfigCache( sal_Bool bConfig ) :
304 bUseConfig ( bConfig )
306 if ( bUseConfig )
307 ImplInit();
308 else
309 ImplInitSmart();
312 FilterConfigCache::~FilterConfigCache()
316 OUString FilterConfigCache::GetImportFilterName( sal_uInt16 nFormat )
318 if( nFormat < aImport.size() )
319 return aImport[ nFormat ].sFilterName;
320 return OUString("");
323 sal_uInt16 FilterConfigCache::GetImportFormatNumber( const OUString& rFormatName )
325 CacheVector::const_iterator aIter, aEnd;
326 for (aIter = aImport.begin(), aEnd = aImport.end(); aIter != aEnd; ++aIter)
328 if ( aIter->sUIName.equalsIgnoreAsciiCase( rFormatName ) )
329 break;
331 return sal::static_int_cast< sal_uInt16 >(aIter == aImport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aImport.begin());
334 /// get the index of the filter that matches this extension
335 sal_uInt16 FilterConfigCache::GetImportFormatNumberForExtension( const OUString& rExt )
337 CacheVector::const_iterator aIter, aEnd;
338 for (aIter = aImport.begin(), aEnd = aImport.end(); aIter != aEnd; ++aIter)
340 for ( sal_Int32 i = 0; i < aIter->lExtensionList.getLength(); i++ )
342 if ( aIter->lExtensionList[i].equalsIgnoreAsciiCase( rExt ) )
343 return sal::static_int_cast< sal_uInt16 >( aIter - aImport.begin() );
346 return GRFILTER_FORMAT_NOTFOUND;
349 sal_uInt16 FilterConfigCache::GetImportFormatNumberForMediaType( const OUString& rMediaType )
351 CacheVector::const_iterator aIter, aEnd;
352 for (aIter = aImport.begin(), aEnd = aImport.end(); aIter != aEnd; ++aIter)
354 if ( aIter->sMediaType.equalsIgnoreAsciiCase( rMediaType ) )
355 break;
357 return sal::static_int_cast< sal_uInt16 >(aIter == aImport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aImport.begin());
360 sal_uInt16 FilterConfigCache::GetImportFormatNumberForShortName( const OUString& rShortName )
362 CacheVector::const_iterator aEnd;
363 CacheVector::iterator aIter;
364 for (aIter = aImport.begin(), aEnd = aImport.end(); aIter != aEnd; ++aIter)
366 if ( aIter->GetShortName().equalsIgnoreAsciiCase( rShortName ) )
367 break;
369 return sal::static_int_cast< sal_uInt16 >(aIter == aImport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aImport.begin());
372 sal_uInt16 FilterConfigCache::GetImportFormatNumberForTypeName( const OUString& rType )
374 CacheVector::const_iterator aIter, aEnd;
375 for (aIter = aImport.begin(), aEnd = aImport.end(); aIter != aEnd; ++aIter)
377 if ( aIter->sType.equalsIgnoreAsciiCase( rType ) )
378 break;
380 return sal::static_int_cast< sal_uInt16 >(aIter == aImport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aImport.begin());
383 OUString FilterConfigCache::GetImportFormatName( sal_uInt16 nFormat )
385 if( nFormat < aImport.size() )
386 return aImport[ nFormat ].sUIName;
387 return OUString("");
390 OUString FilterConfigCache::GetImportFormatMediaType( sal_uInt16 nFormat )
392 if( nFormat < aImport.size() )
393 return aImport[ nFormat ].sMediaType;
394 return OUString("");
397 OUString FilterConfigCache::GetImportFormatShortName( sal_uInt16 nFormat )
399 if( nFormat < aImport.size() )
400 return aImport[ nFormat ].GetShortName();
401 return OUString("");
404 OUString FilterConfigCache::GetImportFormatExtension( sal_uInt16 nFormat, sal_Int32 nEntry )
406 if ( (nFormat < aImport.size()) && (nEntry < aImport[ nFormat ].lExtensionList.getLength()) )
407 return aImport[ nFormat ].lExtensionList[ nEntry ];
408 return OUString("");
411 OUString FilterConfigCache::GetImportFilterType( sal_uInt16 nFormat )
413 if( nFormat < aImport.size() )
414 return aImport[ nFormat ].sType;
415 return OUString("");
418 OUString FilterConfigCache::GetImportFilterTypeName( sal_uInt16 nFormat )
420 if( nFormat < aImport.size() )
421 return aImport[ nFormat ].sFilterType;
422 return OUString("");
425 OUString FilterConfigCache::GetImportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry )
427 OUString aWildcard( GetImportFormatExtension( nFormat, nEntry ) );
428 if ( !aWildcard.isEmpty() )
429 aWildcard = aWildcard.replaceAt( 0, 0, "*." );
430 return aWildcard;
433 sal_Bool FilterConfigCache::IsImportInternalFilter( sal_uInt16 nFormat )
435 return (nFormat < aImport.size()) && aImport[ nFormat ].bIsInternalFilter;
438 sal_Bool FilterConfigCache::IsImportPixelFormat( sal_uInt16 nFormat )
440 return (nFormat < aImport.size()) && aImport[ nFormat ].bIsPixelFormat;
443 // ------------------------------------------------------------------------
445 OUString FilterConfigCache::GetExportFilterName( sal_uInt16 nFormat )
447 if( nFormat < aExport.size() )
448 return aExport[ nFormat ].sFilterName;
449 return OUString("");
452 sal_uInt16 FilterConfigCache::GetExportFormatNumber( const OUString& rFormatName )
454 CacheVector::const_iterator aIter, aEnd;
455 for (aIter = aExport.begin(), aEnd = aExport.end(); aIter != aEnd; ++aIter)
457 if ( aIter->sUIName.equalsIgnoreAsciiCase( rFormatName ) )
458 break;
460 return sal::static_int_cast< sal_uInt16 >(aIter == aExport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aExport.begin());
463 sal_uInt16 FilterConfigCache::GetExportFormatNumberForMediaType( const OUString& rMediaType )
465 CacheVector::const_iterator aIter, aEnd;
466 for (aIter = aExport.begin(), aEnd = aExport.end(); aIter != aEnd; ++aIter)
468 if ( aIter->sMediaType.equalsIgnoreAsciiCase( rMediaType ) )
469 break;
471 return sal::static_int_cast< sal_uInt16 >(aIter == aExport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aExport.begin());
474 sal_uInt16 FilterConfigCache::GetExportFormatNumberForShortName( const OUString& rShortName )
476 CacheVector::const_iterator aEnd;
477 CacheVector::iterator aIter;
478 for (aIter = aExport.begin(), aEnd = aExport.end(); aIter != aEnd; ++aIter)
480 if ( aIter->GetShortName().equalsIgnoreAsciiCase( rShortName ) )
481 break;
483 return sal::static_int_cast< sal_uInt16 >(aIter == aExport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aExport.begin());
486 sal_uInt16 FilterConfigCache::GetExportFormatNumberForTypeName( const OUString& rType )
488 CacheVector::const_iterator aIter, aEnd;
489 for (aIter = aExport.begin(), aEnd = aExport.end(); aIter != aEnd; ++aIter)
491 if ( aIter->sType.equalsIgnoreAsciiCase( rType ) )
492 break;
494 return sal::static_int_cast< sal_uInt16 >(aIter == aExport.end() ? GRFILTER_FORMAT_NOTFOUND : aIter - aExport.begin());
497 OUString FilterConfigCache::GetExportFormatName( sal_uInt16 nFormat )
499 if( nFormat < aExport.size() )
500 return aExport[ nFormat ].sUIName;
501 return OUString("");
504 OUString FilterConfigCache::GetExportFormatMediaType( sal_uInt16 nFormat )
506 if( nFormat < aExport.size() )
507 return aExport[ nFormat ].sMediaType;
508 return OUString("");
511 OUString FilterConfigCache::GetExportFormatShortName( sal_uInt16 nFormat )
513 if( nFormat < aExport.size() )
514 return aExport[ nFormat ].GetShortName();
515 return OUString("");
518 OUString FilterConfigCache::GetExportFormatExtension( sal_uInt16 nFormat, sal_Int32 nEntry )
520 if ( (nFormat < aExport.size()) && (nEntry < aExport[ nFormat ].lExtensionList.getLength()) )
521 return aExport[ nFormat ].lExtensionList[ nEntry ];
522 return OUString("");
525 OUString FilterConfigCache::GetExportFilterTypeName( sal_uInt16 nFormat )
527 if( nFormat < aExport.size() )
528 return aExport[ nFormat ].sFilterType;
529 return OUString("");
532 OUString FilterConfigCache::GetExportInternalFilterName( sal_uInt16 nFormat )
534 if( nFormat < aExport.size() )
535 return aExport[ nFormat ].sInternalFilterName;
536 return OUString("");
539 OUString FilterConfigCache::GetExportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry )
541 OUString aWildcard( GetExportFormatExtension( nFormat, nEntry ) );
542 if ( !aWildcard.isEmpty() )
543 aWildcard = aWildcard.replaceAt( 0, 0, "*." );
544 return aWildcard;
547 sal_Bool FilterConfigCache::IsExportInternalFilter( sal_uInt16 nFormat )
549 return (nFormat < aExport.size()) && aExport[ nFormat ].bIsInternalFilter;
552 sal_Bool FilterConfigCache::IsExportPixelFormat( sal_uInt16 nFormat )
554 return (nFormat < aExport.size()) && aExport[ nFormat ].bIsPixelFormat;
557 // ------------------------------------------------------------------------
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */