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: xdialogcreator.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_embeddedobj.hxx"
33 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
34 #include <com/sun/star/embed/XEmbeddedObject.hpp>
35 #include <com/sun/star/embed/EntryInitModes.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/datatransfer/DataFlavor.hpp>
38 #include <com/sun/star/ucb/CommandAbortedException.hpp>
41 #include <osl/thread.h>
42 #include <osl/file.hxx>
43 #include <vos/module.hxx>
44 #include <comphelper/classids.hxx>
47 #include <comphelper/mimeconfighelper.hxx>
49 #include "xdialogcreator.hxx"
50 #include "oleembobj.hxx"
52 // #include <confighelper.hxx>
53 #include <xdialogcreator.hxx>
54 #include <oleembobj.hxx>
61 class InitializedOleGuard
66 if ( !SUCCEEDED( OleInitialize( NULL
) ) )
67 throw ::com::sun::star::uno::RuntimeException();
70 ~InitializedOleGuard()
77 typedef UINT STDAPICALLTYPE
OleUIInsertObjectA_Type(LPOLEUIINSERTOBJECTA
);
83 using namespace ::com::sun::star
;
84 using namespace ::comphelper
;
85 //-------------------------------------------------------------------------
86 uno::Sequence
< sal_Int8
> GetRelatedInternalID_Impl( const uno::Sequence
< sal_Int8
>& aClassID
)
89 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_60
) )
90 || MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_8
) ) )
91 return MimeConfigurationHelper::GetSequenceClassID( SO3_SW_CLASSID_60
);
94 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_60
) )
95 || MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_8
) ) )
96 return MimeConfigurationHelper::GetSequenceClassID( SO3_SC_CLASSID_60
);
99 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_60
) )
100 || MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_8
) ) )
101 return MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_CLASSID_60
);
104 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_60
) )
105 || MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_8
) ) )
106 return MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_CLASSID_60
);
109 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_60
) )
110 || MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_8
) ) )
111 return MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_CLASSID_60
);
114 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_60
) )
115 || MimeConfigurationHelper::ClassIDsEqual( aClassID
, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_8
) ) )
116 return MimeConfigurationHelper::GetSequenceClassID( SO3_SM_CLASSID_60
);
121 //-------------------------------------------------------------------------
122 uno::Sequence
< ::rtl::OUString
> SAL_CALL
MSOLEDialogObjectCreator::impl_staticGetSupportedServiceNames()
124 uno::Sequence
< ::rtl::OUString
> aRet(2);
125 aRet
[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.MSOLEObjectSystemCreator");
126 aRet
[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.MSOLEObjectSystemCreator");
130 //-------------------------------------------------------------------------
131 ::rtl::OUString SAL_CALL
MSOLEDialogObjectCreator::impl_staticGetImplementationName()
133 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.MSOLEObjectSystemCreator");
136 //-------------------------------------------------------------------------
137 uno::Reference
< uno::XInterface
> SAL_CALL
MSOLEDialogObjectCreator::impl_staticCreateSelfInstance(
138 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
140 return uno::Reference
< uno::XInterface
>( *new MSOLEDialogObjectCreator( xServiceManager
) );
143 //-------------------------------------------------------------------------
144 embed::InsertedObjectInfo SAL_CALL
MSOLEDialogObjectCreator::createInstanceByDialog(
145 const uno::Reference
< embed::XStorage
>& xStorage
,
146 const ::rtl::OUString
& sEntName
,
147 const uno::Sequence
< beans::PropertyValue
>& aInObjArgs
)
148 throw ( lang::IllegalArgumentException
,
151 uno::RuntimeException
)
153 embed::InsertedObjectInfo aObjectInfo
;
154 uno::Sequence
< beans::PropertyValue
> aObjArgs( aInObjArgs
);
158 if ( !xStorage
.is() )
159 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
160 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
163 if ( !sEntName
.getLength() )
164 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
165 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
168 InitializedOleGuard aGuard
;
170 OLEUIINSERTOBJECT io
;
171 char szFile
[MAX_PATH
];
174 memset(&io
, 0, sizeof(io
));
176 io
.cbStruct
= sizeof(io
);
177 io
.hWndOwner
= GetActiveWindow();
180 io
.lpszFile
= szFile
;
181 io
.cchFile
= MAX_PATH
;
183 io
.dwFlags
= IOF_SELECTCREATENEW
| IOF_DISABLELINK
;
186 ::vos::OModule aOleDlgLib
;
187 if( !aOleDlgLib
.load( ::rtl::OUString::createFromAscii( "oledlg" ) ) )
188 throw uno::RuntimeException();
190 OleUIInsertObjectA_Type
* pInsertFct
= (OleUIInsertObjectA_Type
*)
191 aOleDlgLib
.getSymbol( ::rtl::OUString::createFromAscii( "OleUIInsertObjectA" ) );
193 throw uno::RuntimeException();
195 uTemp
=pInsertFct(&io
);
197 if ( OLEUI_OK
== uTemp
)
199 if (io
.dwFlags
& IOF_SELECTCREATENEW
)
201 uno::Reference
< embed::XEmbedObjectCreator
> xEmbCreator(
202 m_xFactory
->createInstance(
203 ::rtl::OUString::createFromAscii( "com.sun.star.embed.EmbeddedObjectCreator" ) ),
205 if ( !xEmbCreator
.is() )
206 throw uno::RuntimeException();
208 uno::Sequence
< sal_Int8
> aClassID
= MimeConfigurationHelper::GetSequenceClassID( io
.clsid
.Data1
,
220 aClassID
= GetRelatedInternalID_Impl( aClassID
);
222 //TODO: retrieve ClassName
223 ::rtl::OUString aClassName
;
224 aObjectInfo
.Object
= uno::Reference
< embed::XEmbeddedObject
>(
225 xEmbCreator
->createInstanceInitNew( aClassID
, aClassName
, xStorage
, sEntName
, aObjArgs
),
230 ::rtl::OUString aFileName
= ::rtl::OStringToOUString( ::rtl::OString( szFile
), osl_getThreadTextEncoding() );
231 rtl::OUString aFileURL
;
232 if ( osl::FileBase::getFileURLFromSystemPath( aFileName
, aFileURL
) != osl::FileBase::E_None
)
233 throw uno::RuntimeException();
235 uno::Sequence
< beans::PropertyValue
> aMediaDescr( 1 );
236 aMediaDescr
[0].Name
= ::rtl::OUString::createFromAscii( "URL" );
237 aMediaDescr
[0].Value
<<= aFileURL
;
239 // TODO: use config helper for type detection
240 uno::Reference
< embed::XEmbedObjectCreator
> xEmbCreator
;
241 ::comphelper::MimeConfigurationHelper
aHelper( m_xFactory
);
243 if ( aHelper
.AddFilterNameCheckOwnFile( aMediaDescr
) )
244 xEmbCreator
= uno::Reference
< embed::XEmbedObjectCreator
>(
245 m_xFactory
->createInstance(
246 ::rtl::OUString::createFromAscii( "com.sun.star.embed.EmbeddedObjectCreator" ) ),
249 xEmbCreator
= uno::Reference
< embed::XEmbedObjectCreator
>(
250 m_xFactory
->createInstance(
251 ::rtl::OUString::createFromAscii( "com.sun.star.embed.OLEEmbeddedObjectFactory" ) ),
254 if ( !xEmbCreator
.is() )
255 throw uno::RuntimeException();
257 aObjectInfo
.Object
= uno::Reference
< embed::XEmbeddedObject
>(
258 xEmbCreator
->createInstanceInitFromMediaDescriptor( xStorage
, sEntName
, aMediaDescr
, aObjArgs
),
262 if ( ( io
.dwFlags
& IOF_CHECKDISPLAYASICON
) && io
.hMetaPict
!= NULL
)
264 METAFILEPICT
* pMF
= ( METAFILEPICT
* )GlobalLock( io
.hMetaPict
);
267 sal_uInt32 nBufSize
= GetMetaFileBitsEx( pMF
->hMF
, 0, NULL
);
268 uno::Sequence
< sal_Int8
> aMetafile( nBufSize
+ 22 );
269 sal_uInt8
* pBuf
= (sal_uInt8
*)( aMetafile
.getArray() );
270 *( (long* )pBuf
) = 0x9ac6cdd7L
;
271 *( (short* )( pBuf
+6 )) = ( SHORT
) 0;
272 *( (short* )( pBuf
+8 )) = ( SHORT
) 0;
273 *( (short* )( pBuf
+10 )) = ( SHORT
) pMF
->xExt
;
274 *( (short* )( pBuf
+12 )) = ( SHORT
) pMF
->yExt
;
275 *( (short* )( pBuf
+14 )) = ( USHORT
) 2540;
277 if ( nBufSize
&& nBufSize
== GetMetaFileBitsEx( pMF
->hMF
, nBufSize
, pBuf
+22 ) )
279 datatransfer::DataFlavor
aFlavor(
280 ::rtl::OUString::createFromAscii( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
281 ::rtl::OUString::createFromAscii( "Image WMF" ),
282 getCppuType( ( const uno::Sequence
< sal_Int8
>* ) 0 ) );
284 aObjectInfo
.Options
.realloc( 2 );
285 aObjectInfo
.Options
[0].Name
= ::rtl::OUString::createFromAscii( "Icon" );
286 aObjectInfo
.Options
[0].Value
<<= aMetafile
;
287 aObjectInfo
.Options
[1].Name
= ::rtl::OUString::createFromAscii( "IconFormat" );
288 aObjectInfo
.Options
[1].Value
<<= aFlavor
;
291 GlobalUnlock( io
.hMetaPict
);
296 throw ucb::CommandAbortedException();
299 throw lang::NoSupportException(); // TODO:
302 OSL_ENSURE( aObjectInfo
.Object
.is(), "No object was created!\n" );
303 if ( !aObjectInfo
.Object
.is() )
304 throw uno::RuntimeException();
309 //-------------------------------------------------------------------------
310 embed::InsertedObjectInfo SAL_CALL
MSOLEDialogObjectCreator::createInstanceInitFromClipboard(
311 const uno::Reference
< embed::XStorage
>& xStorage
,
312 const ::rtl::OUString
& sEntryName
,
313 const uno::Sequence
< beans::PropertyValue
>& aObjectArgs
)
314 throw ( lang::IllegalArgumentException
,
317 uno::RuntimeException
)
319 embed::InsertedObjectInfo aObjectInfo
;
323 if ( !xStorage
.is() )
324 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
325 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
328 if ( !sEntryName
.getLength() )
329 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
330 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
333 uno::Reference
< embed::XEmbeddedObject
> xResult(
334 static_cast< ::cppu::OWeakObject
* > ( new OleEmbeddedObject( m_xFactory
) ),
337 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY
);
339 if ( !xPersist
.is() )
340 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
342 xPersist
->setPersistentEntry( xStorage
,
344 embed::EntryInitModes::DEFAULT_INIT
,
345 uno::Sequence
< beans::PropertyValue
>(),
348 aObjectInfo
.Object
= xResult
;
350 // TODO/LATER: in case of iconifie object the icon should be stored in aObjectInfo
352 throw lang::NoSupportException(); // TODO:
355 OSL_ENSURE( aObjectInfo
.Object
.is(), "No object was created!\n" );
356 if ( !aObjectInfo
.Object
.is() )
357 throw uno::RuntimeException();
362 //-------------------------------------------------------------------------
363 ::rtl::OUString SAL_CALL
MSOLEDialogObjectCreator::getImplementationName()
364 throw ( uno::RuntimeException
)
366 return impl_staticGetImplementationName();
369 //-------------------------------------------------------------------------
370 sal_Bool SAL_CALL
MSOLEDialogObjectCreator::supportsService( const ::rtl::OUString
& ServiceName
)
371 throw ( uno::RuntimeException
)
373 uno::Sequence
< ::rtl::OUString
> aSeq
= impl_staticGetSupportedServiceNames();
375 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
376 if ( ServiceName
.compareTo( aSeq
[nInd
] ) == 0 )
382 //-------------------------------------------------------------------------
383 uno::Sequence
< ::rtl::OUString
> SAL_CALL
MSOLEDialogObjectCreator::getSupportedServiceNames()
384 throw ( uno::RuntimeException
)
386 return impl_staticGetSupportedServiceNames();