1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: docfac.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <com/sun/star/registry/MergeConflictException.hpp>
34 #include <com/sun/star/registry/XSimpleRegistry.hpp>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 #include <comphelper/processfactory.hxx>
37 #include <tools/config.hxx>
38 #include <svtools/pathoptions.hxx>
39 #include <svtools/moduleoptions.hxx>
40 #include <tools/urlobj.hxx>
41 #include <unotools/ucbstreamhelper.hxx>
42 #include <comphelper/sequenceashashmap.hxx>
44 #include <sfx2/sfx.hrc>
45 #include <sfx2/docfilt.hxx>
46 #include <sfx2/docfac.hxx>
47 #include "viewfac.hxx"
49 #include "arrdecl.hxx"
50 #include <sfx2/app.hxx>
51 #include <sfx2/module.hxx>
52 #include <sfx2/mnumgr.hxx>
53 #include "sfxresid.hxx"
54 #include <sfx2/sfxuno.hxx>
57 namespace css
= ::com::sun::star
;
59 //========================================================================
61 DECL_PTRARRAY( SfxViewFactoryArr_Impl
, SfxViewFactory
*, 2, 2 )
63 //========================================================================
65 DBG_NAME(SfxObjectFactory
)
67 //static SfxObjectFactoryArr_Impl* pObjFac = 0;
69 //========================================================================
71 struct SfxObjectFactory_Impl
73 SfxViewFactoryArr_Impl aViewFactoryArr
;// Liste von <SfxViewFactory>s
74 SfxFilterArr_Impl aFilterArr
; // Liste von <SFxFilter>n
76 ::rtl::OUString aServiceName
;
77 SfxFilterContainer
* pFilterContainer
;
80 String aStandardTemplate
;
81 sal_Bool bTemplateInitialized
;
82 SvGlobalName aClassName
;
84 SfxObjectFactory_Impl() :
86 pFilterContainer ( NULL
),
89 bTemplateInitialized( sal_False
)
93 //========================================================================
95 SfxFilterContainer
* SfxObjectFactory::GetFilterContainer( sal_Bool
/*bForceLoad*/ ) const
97 return pImpl
->pFilterContainer
;
100 //--------------------------------------------------------------------
102 SfxObjectFactory::SfxObjectFactory
104 const SvGlobalName
& rName
,
105 SfxObjectShellFlags nFlagsP
,
107 ) : pShortName( pName
),
108 pImpl( new SfxObjectFactory_Impl
),
111 DBG_CTOR(SfxObjectFactory
, 0);
112 pImpl
->pFilterContainer
= new SfxFilterContainer( String::CreateFromAscii( pName
) );
114 String
aShortName( String::CreateFromAscii( pShortName
) );
115 aShortName
.ToLowerAscii();
116 pImpl
->aClassName
= rName
;
117 if ( aShortName
.EqualsAscii( "swriter" ) )
118 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_SW
);
119 else if ( aShortName
.EqualsAscii( "swriter/web" ) )
120 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_SWWEB
);
121 else if ( aShortName
.EqualsAscii( "swriter/globaldocument" ) )
122 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_SWGLOB
);
123 else if ( aShortName
.EqualsAscii( "scalc" ) )
124 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_SC
);
125 else if ( aShortName
.EqualsAscii( "simpress" ) )
126 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_SI
);
127 else if ( aShortName
.EqualsAscii( "sdraw" ) )
128 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_SD
);
129 else if ( aShortName
.EqualsAscii( "message" ) )
130 pImpl
->pNameResId
= new SfxResId( STR_DOCTYPENAME_MESSAGE
);
133 //--------------------------------------------------------------------
135 SfxObjectFactory::~SfxObjectFactory()
137 DBG_DTOR(SfxObjectFactory
, 0);
139 const sal_uInt16 nCount
= pImpl
->aFilterArr
.Count();
140 for ( sal_uInt16 i
= 0; i
< nCount
; ++i
)
141 delete pImpl
->aFilterArr
[i
];
142 delete pImpl
->pNameResId
;
146 //--------------------------------------------------------------------
148 void SfxObjectFactory::RegisterViewFactory
150 SfxViewFactory
&rFactory
155 nPos
< pImpl
->aViewFactoryArr
.Count() &&
156 pImpl
->aViewFactoryArr
[nPos
]->GetOrdinal() <= rFactory
.GetOrdinal();
159 pImpl
->aViewFactoryArr
.Insert(nPos
, &rFactory
);
162 //--------------------------------------------------------------------
164 sal_uInt16
SfxObjectFactory::GetViewFactoryCount() const
166 return pImpl
->aViewFactoryArr
.Count();
169 //--------------------------------------------------------------------
171 SfxViewFactory
& SfxObjectFactory::GetViewFactory(sal_uInt16 i
) const
173 return *pImpl
->aViewFactoryArr
[i
];
176 //--------------------------------------------------------------------
178 SfxModule
* SfxObjectFactory::GetModule() const
180 return pImpl
->pModule
;
183 void SfxObjectFactory::SetModule_Impl( SfxModule
*pMod
)
185 pImpl
->pModule
= pMod
;
188 void SfxObjectFactory::SetStandardTemplate( const String
& rServiceName
, const String
& rTemplate
)
190 SvtModuleOptions::EFactory eFac
= SvtModuleOptions::ClassifyFactoryByServiceName(rServiceName
);
191 if (eFac
== SvtModuleOptions::E_UNKNOWN_FACTORY
)
192 eFac
= SvtModuleOptions::ClassifyFactoryByShortName(rServiceName
);
193 if (eFac
!= SvtModuleOptions::E_UNKNOWN_FACTORY
)
194 SvtModuleOptions().SetFactoryStandardTemplate(eFac
, rTemplate
);
197 String
SfxObjectFactory::GetStandardTemplate( const String
& rServiceName
)
199 SvtModuleOptions::EFactory eFac
= SvtModuleOptions::ClassifyFactoryByServiceName(rServiceName
);
200 if (eFac
== SvtModuleOptions::E_UNKNOWN_FACTORY
)
201 eFac
= SvtModuleOptions::ClassifyFactoryByShortName(rServiceName
);
204 if (eFac
!= SvtModuleOptions::E_UNKNOWN_FACTORY
)
205 sTemplate
= SvtModuleOptions().GetFactoryStandardTemplate(eFac
);
211 const SfxObjectFactory* SfxObjectFactory::GetFactory( const String& rFactoryURL )
213 const SfxObjectFactory* pFactory = 0;
214 String aFact( rFactoryURL );
215 String aPrefix( DEFINE_CONST_UNICODE( "private:factory/" ) );
216 if ( aPrefix.Len() == aFact.Match( aPrefix ) )
217 // Aufruf m"oglich mit z.B. "swriter" oder "private:factory/swriter"
218 aFact.Erase( 0, aPrefix.Len() );
219 sal_uInt16 nPos = aFact.Search( '?' );
221 // Etwaige Parameter abschneiden
222 aFact.Erase( nPos, aFact.Len() );
224 SfxApplication *pApp = SFX_APP();
226 // "swriter4" durch "swriter" ersetzen, zum Vergleichen uppercase verwenden
227 WildCard aSearchedFac( aFact.EraseAllChars('4').ToUpperAscii() );
228 for( sal_uInt16 n = GetObjectFactoryCount_Impl(); !pFactory && n--; )
230 pFactory = &GetObjectFactory_Impl( n );
231 String aCompareTo = String::CreateFromAscii( pFactory->GetShortName() );
232 aCompareTo.ToUpperAscii();
233 if( !aSearchedFac.Matches( aCompareTo ) )
241 const SfxFilter
* SfxObjectFactory::GetTemplateFilter() const
244 SfxFilterMatcher
aMatcher ( String::CreateFromAscii( pShortName
) );
245 SfxFilterMatcherIter
aIter( &aMatcher
);
246 const SfxFilter
*pFilter
= 0;
247 const SfxFilter
*pTemp
= aIter
.First();
250 if( pTemp
->IsOwnFormat() && pTemp
->IsOwnTemplateFormat() && ( pTemp
->GetVersion() > nVersion
) )
253 nVersion
= (USHORT
) pTemp
->GetVersion();
256 pTemp
= aIter
.Next();
262 void SfxObjectFactory::SetDocumentTypeNameResource( const ResId
& rId
)
264 DBG_ASSERT( !pImpl
->pNameResId
, "UI-Namensresource mehrfach gesetzt!" );
265 pImpl
->pNameResId
= new ResId( rId
);
268 String
SfxObjectFactory::GetDocumentTypeName() const
270 if ( pImpl
->pNameResId
)
271 return String( *pImpl
->pNameResId
);
275 void SfxObjectFactory::SetDocumentServiceName( const ::rtl::OUString
& rServiceName
)
277 pImpl
->aServiceName
= rServiceName
;
280 const ::rtl::OUString
& SfxObjectFactory::GetDocumentServiceName() const
282 return pImpl
->aServiceName
;
285 const SvGlobalName
& SfxObjectFactory::GetClassId() const
287 return pImpl
->aClassName
;
290 String
SfxObjectFactory::GetModuleName() const
292 static ::rtl::OUString SERVICENAME_MODULEMANAGER
= ::rtl::OUString::createFromAscii("com.sun.star.frame.ModuleManager");
293 static ::rtl::OUString PROP_MODULEUINAME
= ::rtl::OUString::createFromAscii("ooSetupFactoryUIName");
297 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= ::comphelper::getProcessServiceFactory();
299 css::uno::Reference
< css::container::XNameAccess
> xModuleManager(
300 xSMGR
->createInstance(SERVICENAME_MODULEMANAGER
),
301 css::uno::UNO_QUERY_THROW
);
303 ::rtl::OUString
sDocService(GetDocumentServiceName());
304 ::comphelper::SequenceAsHashMap
aPropSet( xModuleManager
->getByName(sDocService
) );
305 ::rtl::OUString sModuleName
= aPropSet
.getUnpackedValueOrDefault(PROP_MODULEUINAME
, ::rtl::OUString());
306 return String(sModuleName
);
308 catch(const css::uno::RuntimeException
& exRun
)
310 catch(const css::uno::Exception
&)