Line data Source code
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 <com/sun/star/registry/MergeConflictException.hpp>
21 : #include <com/sun/star/registry/XSimpleRegistry.hpp>
22 : #include <com/sun/star/container/XNameAccess.hpp>
23 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
24 : #include <com/sun/star/document/XTypeDetection.hpp>
25 : #include <com/sun/star/frame/ModuleManager.hpp>
26 : #include <com/sun/star/frame/XLoadable.hpp>
27 : #include <com/sun/star/frame/XStorable.hpp>
28 : #include <comphelper/processfactory.hxx>
29 : #include <unotools/pathoptions.hxx>
30 : #include <unotools/moduleoptions.hxx>
31 : #include <unotools/ucbstreamhelper.hxx>
32 : #include <unotools/localfilehelper.hxx>
33 : #include <comphelper/sequenceashashmap.hxx>
34 : #include <comphelper/configurationhelper.hxx>
35 :
36 : #include <sfx2/sfx.hrc>
37 : #include <sfx2/docfilt.hxx>
38 : #include <sfx2/docfac.hxx>
39 : #include "sfx2/viewfac.hxx"
40 : #include "fltfnc.hxx"
41 : #include "arrdecl.hxx"
42 : #include <sfx2/app.hxx>
43 : #include <sfx2/module.hxx>
44 : #include <sfx2/mnumgr.hxx>
45 : #include "sfx2/sfxresid.hxx"
46 : #include <sfx2/sfxuno.hxx>
47 : #include "syspath.hxx"
48 : #include <osl/file.hxx>
49 : #include <osl/security.hxx>
50 : #include "doc.hrc"
51 :
52 : #include <rtl/strbuf.hxx>
53 :
54 : #include <assert.h>
55 :
56 : using namespace ::com::sun::star;
57 :
58 : //========================================================================
59 :
60 1542 : DECL_PTRARRAY( SfxViewFactoryArr_Impl, SfxViewFactory*, 2, 2 )
61 :
62 : //========================================================================
63 :
64 : DBG_NAME(SfxObjectFactory)
65 :
66 : //========================================================================
67 :
68 41 : struct SfxObjectFactory_Impl
69 : {
70 : SfxViewFactoryArr_Impl aViewFactoryArr;// List of <SfxViewFactory>s
71 : ResId* pNameResId;
72 : ::rtl::OUString aServiceName;
73 : SfxFilterContainer* pFilterContainer;
74 : SfxModule* pModule;
75 : sal_uInt16 nImageId;
76 : String aStandardTemplate;
77 : sal_Bool bTemplateInitialized;
78 : SvGlobalName aClassName;
79 :
80 41 : SfxObjectFactory_Impl() :
81 : pNameResId ( NULL ),
82 : pFilterContainer ( NULL ),
83 : pModule ( NULL ),
84 : nImageId ( 0 ),
85 41 : bTemplateInitialized( sal_False )
86 41 : {}
87 : };
88 :
89 : //========================================================================
90 :
91 638 : SfxFilterContainer* SfxObjectFactory::GetFilterContainer( sal_Bool /*bForceLoad*/ ) const
92 : {
93 638 : return pImpl->pFilterContainer;
94 : }
95 :
96 : //--------------------------------------------------------------------
97 :
98 41 : SfxObjectFactory::SfxObjectFactory
99 : (
100 : const SvGlobalName& rName,
101 : SfxObjectShellFlags nFlagsP,
102 : const char* pName
103 : ) : pShortName( pName ),
104 41 : pImpl( new SfxObjectFactory_Impl ),
105 82 : nFlags( nFlagsP )
106 : {
107 : DBG_CTOR(SfxObjectFactory, 0);
108 41 : pImpl->pFilterContainer = new SfxFilterContainer( rtl::OUString::createFromAscii( pName ) );
109 :
110 41 : String aShortName( rtl::OUString::createFromAscii( pShortName ) );
111 41 : aShortName.ToLowerAscii();
112 41 : pImpl->aClassName = rName;
113 41 : if ( aShortName.EqualsAscii( "swriter" ) )
114 10 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_SW );
115 31 : else if ( aShortName.EqualsAscii( "swriter/web" ) )
116 10 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_SWWEB );
117 21 : else if ( aShortName.EqualsAscii( "swriter/globaldocument" ) )
118 8 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_SWGLOB );
119 13 : else if ( aShortName.EqualsAscii( "scalc" ) )
120 5 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_SC );
121 8 : else if ( aShortName.EqualsAscii( "simpress" ) )
122 2 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_SI );
123 6 : else if ( aShortName.EqualsAscii( "sdraw" ) )
124 2 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_SD );
125 4 : else if ( aShortName.EqualsAscii( "message" ) )
126 0 : pImpl->pNameResId = new SfxResId( STR_DOCTYPENAME_MESSAGE );
127 41 : }
128 :
129 : //--------------------------------------------------------------------
130 :
131 41 : SfxObjectFactory::~SfxObjectFactory()
132 : {
133 : DBG_DTOR(SfxObjectFactory, 0);
134 :
135 41 : delete pImpl->pNameResId;
136 41 : delete pImpl->pFilterContainer;
137 41 : delete pImpl;
138 41 : }
139 :
140 : //--------------------------------------------------------------------
141 :
142 82 : void SfxObjectFactory::RegisterViewFactory
143 : (
144 : SfxViewFactory &rFactory
145 : )
146 : {
147 : #if OSL_DEBUG_LEVEL > 0
148 : {
149 : const String sViewName( rFactory.GetAPIViewName() );
150 : for ( sal_uInt16 i = 0; i < pImpl->aViewFactoryArr.Count(); ++i )
151 : {
152 : if ( !pImpl->aViewFactoryArr[i]->GetAPIViewName().Equals( sViewName ) )
153 : continue;
154 : rtl::OStringBuffer aStr(RTL_CONSTASCII_STRINGPARAM(
155 : "SfxObjectFactory::RegisterViewFactory: duplicate view name '"));
156 : aStr.append(rtl::OUStringToOString(sViewName, RTL_TEXTENCODING_ASCII_US));
157 : aStr.append(RTL_CONSTASCII_STRINGPARAM("'!"));
158 : OSL_FAIL(aStr.getStr());
159 : break;
160 : }
161 : }
162 : #endif
163 : sal_uInt16 nPos;
164 278 : for ( nPos = 0;
165 139 : nPos < pImpl->aViewFactoryArr.Count() &&
166 57 : pImpl->aViewFactoryArr[nPos]->GetOrdinal() <= rFactory.GetOrdinal();
167 : ++nPos )
168 : /* empty loop */;
169 82 : pImpl->aViewFactoryArr.Insert(nPos, &rFactory);
170 82 : }
171 :
172 : //--------------------------------------------------------------------
173 :
174 857 : sal_uInt16 SfxObjectFactory::GetViewFactoryCount() const
175 : {
176 857 : return pImpl->aViewFactoryArr.Count();
177 : }
178 :
179 : //--------------------------------------------------------------------
180 :
181 1321 : SfxViewFactory& SfxObjectFactory::GetViewFactory(sal_uInt16 i) const
182 : {
183 1321 : return *pImpl->aViewFactoryArr[i];
184 : }
185 :
186 : //--------------------------------------------------------------------
187 :
188 10709 : SfxModule* SfxObjectFactory::GetModule() const
189 : {
190 10709 : return pImpl->pModule;
191 : }
192 :
193 39 : void SfxObjectFactory::SetModule_Impl( SfxModule *pMod )
194 : {
195 39 : pImpl->pModule = pMod;
196 39 : }
197 :
198 0 : void SfxObjectFactory::SetSystemTemplate( const String& rServiceName, const String& rTemplateName )
199 : {
200 : static const int nMaxPathSize = 16000;
201 0 : static ::rtl::OUString SERVICE_FILTER_FACTORY("com.sun.star.document.FilterFactory");
202 0 : static ::rtl::OUString SERVICE_TYPE_DECTECTION("com.sun.star.document.TypeDetection");
203 :
204 0 : static ::rtl::OUString CONF_ROOT("/org.openoffice.Setup");
205 0 : static ::rtl::OUString CONF_PATH = ::rtl::OUString("Office/Factories/" ) + ::rtl::OUString( rServiceName );
206 0 : static ::rtl::OUString PROP_DEF_TEMPL_CHANGED("ooSetupFactorySystemDefaultTemplateChanged");
207 0 : static ::rtl::OUString PROP_ACTUAL_FILTER("ooSetupFactoryActualFilter");
208 :
209 0 : static ::rtl::OUString DEF_TPL_STR("/soffice.");
210 :
211 0 : rtl::OUString sURL;
212 0 : String sPath;
213 : sal_Unicode aPathBuffer[nMaxPathSize];
214 0 : if ( SystemPath::GetUserTemplateLocation( aPathBuffer, nMaxPathSize ))
215 0 : sPath = rtl::OUString( aPathBuffer );
216 0 : ::utl::LocalFileHelper::ConvertPhysicalNameToURL( sPath, sURL );
217 :
218 0 : ::rtl::OUString aUserTemplateURL( sURL );
219 0 : if ( !aUserTemplateURL.isEmpty())
220 : {
221 : try
222 : {
223 0 : uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
224 : uno::Reference< uno::XInterface > xConfig = ::comphelper::ConfigurationHelper::openConfig(
225 0 : ::comphelper::getProcessComponentContext(), CONF_ROOT, ::comphelper::ConfigurationHelper::E_STANDARD );
226 :
227 0 : ::rtl::OUString aActualFilter;
228 0 : ::comphelper::ConfigurationHelper::readRelativeKey( xConfig, CONF_PATH, PROP_ACTUAL_FILTER ) >>= aActualFilter;
229 0 : sal_Bool bChanged(sal_False);
230 0 : ::comphelper::ConfigurationHelper::readRelativeKey( xConfig, CONF_PATH, PROP_DEF_TEMPL_CHANGED ) >>= bChanged;
231 :
232 : uno::Reference< container::XNameAccess > xFilterFactory(
233 0 : xFactory->createInstance( SERVICE_FILTER_FACTORY ), uno::UNO_QUERY_THROW );
234 : uno::Reference< container::XNameAccess > xTypeDetection(
235 0 : xFactory->createInstance( SERVICE_TYPE_DECTECTION ), uno::UNO_QUERY_THROW );
236 :
237 0 : ::rtl::OUString aActualFilterTypeName;
238 0 : uno::Sequence< beans::PropertyValue > aActuralFilterData;
239 0 : xFilterFactory->getByName( aActualFilter ) >>= aActuralFilterData;
240 0 : for ( sal_Int32 nInd = 0; nInd < aActuralFilterData.getLength(); nInd++ )
241 0 : if ( aActuralFilterData[nInd].Name == "Type" )
242 0 : aActuralFilterData[nInd].Value >>= aActualFilterTypeName;
243 0 : ::comphelper::SequenceAsHashMap aProps1( xTypeDetection->getByName( aActualFilterTypeName ) );
244 : uno::Sequence< ::rtl::OUString > aAllExt =
245 0 : aProps1.getUnpackedValueOrDefault( ::rtl::OUString("Extensions"), uno::Sequence< ::rtl::OUString >() );
246 : //To-do: check if aAllExt is empty first
247 0 : ::rtl::OUString aExt = aAllExt[0];
248 :
249 0 : aUserTemplateURL += DEF_TPL_STR;
250 0 : aUserTemplateURL += aExt;
251 :
252 : uno::Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess(
253 0 : ucb::SimpleFileAccess::create( ::comphelper::getComponentContext(xFactory) ) );
254 :
255 0 : ::rtl::OUString aBackupURL;
256 0 : ::osl::Security().getConfigDir(aBackupURL);
257 0 : aBackupURL += ::rtl::OUString("/temp");
258 :
259 0 : if ( !xSimpleFileAccess->exists( aBackupURL ) )
260 0 : xSimpleFileAccess->createFolder( aBackupURL );
261 :
262 0 : aBackupURL += DEF_TPL_STR;
263 0 : aBackupURL += aExt;
264 :
265 0 : if ( rTemplateName.Len() != 0 )
266 : {
267 0 : if ( xSimpleFileAccess->exists( aUserTemplateURL ) && !bChanged )
268 0 : xSimpleFileAccess->copy( aUserTemplateURL, aBackupURL );
269 :
270 0 : uno::Reference< document::XTypeDetection > xTypeDetector( xTypeDetection, uno::UNO_QUERY );
271 0 : ::comphelper::SequenceAsHashMap aProps2( xTypeDetection->getByName( xTypeDetector->queryTypeByURL( rTemplateName ) ) );
272 : ::rtl::OUString aFilterName =
273 0 : aProps2.getUnpackedValueOrDefault( ::rtl::OUString("PreferredFilter"), ::rtl::OUString() );
274 :
275 0 : uno::Sequence< beans::PropertyValue > aArgs( 3 );
276 0 : aArgs[0].Name = ::rtl::OUString("FilterName");
277 0 : aArgs[0].Value <<= aFilterName;
278 0 : aArgs[1].Name = ::rtl::OUString("AsTemplate");
279 0 : aArgs[1].Value <<= sal_True;
280 0 : aArgs[2].Name = ::rtl::OUString("URL");
281 0 : aArgs[2].Value <<= ::rtl::OUString( rTemplateName );
282 :
283 0 : uno::Reference< frame::XLoadable > xLoadable( xFactory->createInstance( ::rtl::OUString( rServiceName ) ), uno::UNO_QUERY );
284 0 : xLoadable->load( aArgs );
285 :
286 0 : aArgs.realloc( 2 );
287 0 : aArgs[1].Name = ::rtl::OUString("Overwrite");
288 0 : aArgs[1].Value <<= sal_True;
289 :
290 0 : uno::Reference< frame::XStorable > xStorable( xLoadable, uno::UNO_QUERY );
291 0 : xStorable->storeToURL( aUserTemplateURL, aArgs );
292 0 : ::comphelper::ConfigurationHelper::writeRelativeKey( xConfig, CONF_PATH, PROP_DEF_TEMPL_CHANGED, uno::makeAny( sal_True ));
293 0 : ::comphelper::ConfigurationHelper::flush( xConfig );
294 : }
295 : else
296 : {
297 : DBG_ASSERT( bChanged, "invalid ooSetupFactorySystemDefaultTemplateChanged value!" );
298 :
299 0 : xSimpleFileAccess->copy( aBackupURL, aUserTemplateURL );
300 0 : xSimpleFileAccess->kill( aBackupURL );
301 0 : ::comphelper::ConfigurationHelper::writeRelativeKey( xConfig, CONF_PATH, PROP_DEF_TEMPL_CHANGED, uno::makeAny( sal_False ));
302 0 : ::comphelper::ConfigurationHelper::flush( xConfig );
303 0 : }
304 : }
305 0 : catch(const uno::Exception&)
306 : {
307 : }
308 0 : }
309 0 : }
310 :
311 0 : void SfxObjectFactory::SetStandardTemplate( const String& rServiceName, const String& rTemplate )
312 : {
313 0 : SvtModuleOptions::EFactory eFac = SvtModuleOptions::ClassifyFactoryByServiceName(rServiceName);
314 0 : if (eFac == SvtModuleOptions::E_UNKNOWN_FACTORY)
315 0 : eFac = SvtModuleOptions::ClassifyFactoryByShortName(rServiceName);
316 0 : if (eFac != SvtModuleOptions::E_UNKNOWN_FACTORY)
317 : {
318 0 : SetSystemTemplate( rServiceName, rTemplate );
319 0 : SvtModuleOptions().SetFactoryStandardTemplate(eFac, rTemplate);
320 : }
321 0 : }
322 :
323 0 : String SfxObjectFactory::GetStandardTemplate( const String& rServiceName )
324 : {
325 0 : SvtModuleOptions::EFactory eFac = SvtModuleOptions::ClassifyFactoryByServiceName(rServiceName);
326 0 : if (eFac == SvtModuleOptions::E_UNKNOWN_FACTORY)
327 0 : eFac = SvtModuleOptions::ClassifyFactoryByShortName(rServiceName);
328 :
329 0 : String sTemplate;
330 0 : if (eFac != SvtModuleOptions::E_UNKNOWN_FACTORY)
331 0 : sTemplate = SvtModuleOptions().GetFactoryStandardTemplate(eFac);
332 :
333 0 : return sTemplate;
334 : }
335 :
336 0 : const SfxFilter* SfxObjectFactory::GetTemplateFilter() const
337 : {
338 0 : sal_uInt16 nVersion=0;
339 0 : SfxFilterMatcher aMatcher ( rtl::OUString::createFromAscii( pShortName ) );
340 0 : SfxFilterMatcherIter aIter( aMatcher );
341 0 : const SfxFilter *pFilter = 0;
342 0 : const SfxFilter *pTemp = aIter.First();
343 0 : while ( pTemp )
344 : {
345 0 : if( pTemp->IsOwnFormat() && pTemp->IsOwnTemplateFormat() && ( pTemp->GetVersion() > nVersion ) )
346 : {
347 0 : pFilter = pTemp;
348 0 : nVersion = (sal_uInt16) pTemp->GetVersion();
349 : }
350 :
351 0 : pTemp = aIter.Next();
352 : }
353 :
354 0 : return pFilter;
355 : }
356 :
357 39 : void SfxObjectFactory::SetDocumentServiceName( const ::rtl::OUString& rServiceName )
358 : {
359 39 : pImpl->aServiceName = rServiceName;
360 39 : }
361 :
362 31036 : const ::rtl::OUString& SfxObjectFactory::GetDocumentServiceName() const
363 : {
364 31036 : return pImpl->aServiceName;
365 : }
366 :
367 0 : const SvGlobalName& SfxObjectFactory::GetClassId() const
368 : {
369 0 : return pImpl->aClassName;
370 : }
371 :
372 0 : String SfxObjectFactory::GetFactoryURL() const
373 : {
374 0 : ::rtl::OUStringBuffer aURLComposer;
375 0 : aURLComposer.appendAscii(RTL_CONSTASCII_STRINGPARAM("private:factory/"));
376 0 : aURLComposer.appendAscii(GetShortName());
377 0 : return aURLComposer.makeStringAndClear();
378 : }
379 :
380 6 : String SfxObjectFactory::GetModuleName() const
381 : {
382 : try
383 : {
384 6 : css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
385 :
386 : css::uno::Reference< css::frame::XModuleManager2 > xModuleManager(
387 6 : css::frame::ModuleManager::create(xContext));
388 :
389 6 : ::rtl::OUString sDocService(GetDocumentServiceName());
390 6 : ::comphelper::SequenceAsHashMap aPropSet( xModuleManager->getByName(sDocService) );
391 6 : ::rtl::OUString sModuleName = aPropSet.getUnpackedValueOrDefault("ooSetupFactoryUIName", ::rtl::OUString());
392 6 : return String(sModuleName);
393 : }
394 0 : catch(const css::uno::RuntimeException&)
395 : {
396 0 : throw;
397 : }
398 0 : catch(const css::uno::Exception&)
399 : {
400 : }
401 :
402 0 : return String();
403 : }
404 :
405 :
406 480 : sal_uInt16 SfxObjectFactory::GetViewNo_Impl( const sal_uInt16 i_nViewId, const sal_uInt16 i_nFallback ) const
407 : {
408 480 : for ( sal_uInt16 curViewNo = 0; curViewNo < GetViewFactoryCount(); ++curViewNo )
409 : {
410 480 : const sal_uInt16 curViewId = GetViewFactory( curViewNo ).GetOrdinal();
411 480 : if ( i_nViewId == curViewId )
412 480 : return curViewNo;
413 : }
414 0 : return i_nFallback;
415 : }
416 :
417 377 : SfxViewFactory* SfxObjectFactory::GetViewFactoryByViewName( const String& i_rViewName ) const
418 : {
419 754 : for ( sal_uInt16 nViewNo = 0;
420 377 : nViewNo < GetViewFactoryCount();
421 : ++nViewNo
422 : )
423 : {
424 377 : SfxViewFactory& rViewFac( GetViewFactory( nViewNo ) );
425 1028 : if ( ( rViewFac.GetAPIViewName() == i_rViewName )
426 651 : || ( rViewFac.GetLegacyViewName() == i_rViewName )
427 : )
428 377 : return &rViewFac;
429 : }
430 0 : return NULL;
431 : }
432 :
433 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|