Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / embeddedobj / source / msole / xdialogcreator.cxx
blobc191075076db0ef23aa8437cb7a26328b0fd20f8
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 <com/sun/star/embed/EmbeddedObjectCreator.hpp>
21 #include <com/sun/star/embed/XEmbeddedObject.hpp>
22 #include <com/sun/star/embed/EntryInitModes.hpp>
23 #include <com/sun/star/embed/OLEEmbeddedObjectFactory.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/datatransfer/DataFlavor.hpp>
26 #include <com/sun/star/ucb/CommandAbortedException.hpp>
28 #include <osl/thread.h>
29 #include <osl/file.hxx>
30 #include <osl/module.hxx>
31 #include <comphelper/classids.hxx>
33 #include "platform.h"
34 #include <comphelper/mimeconfighelper.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <cppuhelper/supportsservice.hxx>
38 #include "xdialogcreator.hxx"
39 #include "oleembobj.hxx"
40 #include <xdialogcreator.hxx>
41 #include <oleembobj.hxx>
44 #ifdef WNT
46 #include <oledlg.h>
48 class InitializedOleGuard
50 public:
51 InitializedOleGuard()
53 if ( !SUCCEEDED( OleInitialize( NULL ) ) )
54 throw ::com::sun::star::uno::RuntimeException();
57 ~InitializedOleGuard()
59 OleUninitialize();
63 extern "C" {
64 typedef UINT STDAPICALLTYPE OleUIInsertObjectA_Type(LPOLEUIINSERTOBJECTA);
67 #endif
70 using namespace ::com::sun::star;
71 using namespace ::comphelper;
72 //-------------------------------------------------------------------------
73 uno::Sequence< sal_Int8 > GetRelatedInternalID_Impl( const uno::Sequence< sal_Int8 >& aClassID )
75 // Writer
76 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_60 ) )
77 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_8 ) ) )
78 return MimeConfigurationHelper::GetSequenceClassID( SO3_SW_CLASSID_60 );
80 // Calc
81 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_60 ) )
82 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_8 ) ) )
83 return MimeConfigurationHelper::GetSequenceClassID( SO3_SC_CLASSID_60 );
85 // Impress
86 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_60 ) )
87 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_8 ) ) )
88 return MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_CLASSID_60 );
90 // Draw
91 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_60 ) )
92 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_8 ) ) )
93 return MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_CLASSID_60 );
95 // Chart
96 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_60 ) )
97 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_8 ) ) )
98 return MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_CLASSID_60 );
100 // Math
101 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_60 ) )
102 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_8 ) ) )
103 return MimeConfigurationHelper::GetSequenceClassID( SO3_SM_CLASSID_60 );
105 return aClassID;
108 //-------------------------------------------------------------------------
109 uno::Sequence< OUString > SAL_CALL MSOLEDialogObjectCreator::impl_staticGetSupportedServiceNames()
111 uno::Sequence< OUString > aRet(2);
112 aRet[0] = "com.sun.star.embed.MSOLEObjectSystemCreator";
113 aRet[1] = "com.sun.star.comp.embed.MSOLEObjectSystemCreator";
114 return aRet;
117 //-------------------------------------------------------------------------
118 OUString SAL_CALL MSOLEDialogObjectCreator::impl_staticGetImplementationName()
120 return OUString("com.sun.star.comp.embed.MSOLEObjectSystemCreator");
123 //-------------------------------------------------------------------------
124 uno::Reference< uno::XInterface > SAL_CALL MSOLEDialogObjectCreator::impl_staticCreateSelfInstance(
125 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
127 return uno::Reference< uno::XInterface >( *new MSOLEDialogObjectCreator( xServiceManager ) );
130 //-------------------------------------------------------------------------
131 embed::InsertedObjectInfo SAL_CALL MSOLEDialogObjectCreator::createInstanceByDialog(
132 const uno::Reference< embed::XStorage >& xStorage,
133 const OUString& sEntName,
134 const uno::Sequence< beans::PropertyValue >& aInObjArgs )
135 throw ( lang::IllegalArgumentException,
136 io::IOException,
137 uno::Exception,
138 uno::RuntimeException )
140 embed::InsertedObjectInfo aObjectInfo;
141 uno::Sequence< beans::PropertyValue > aObjArgs( aInObjArgs );
143 #ifdef WNT
145 if ( !xStorage.is() )
146 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
147 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
148 1 );
150 if ( !sEntName.getLength() )
151 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
152 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
153 2 );
155 InitializedOleGuard aGuard;
157 OLEUIINSERTOBJECT io;
158 char szFile[MAX_PATH];
159 UINT uTemp;
161 memset(&io, 0, sizeof(io));
163 io.cbStruct = sizeof(io);
164 io.hWndOwner = GetActiveWindow();
166 szFile[0] = 0;
167 io.lpszFile = szFile;
168 io.cchFile = MAX_PATH;
170 io.dwFlags = IOF_SELECTCREATENEW | IOF_DISABLELINK;
173 ::osl::Module aOleDlgLib;
174 if( !aOleDlgLib.load( OUString( "oledlg" ) ))
175 throw uno::RuntimeException();
177 OleUIInsertObjectA_Type * pInsertFct = (OleUIInsertObjectA_Type *)
178 aOleDlgLib.getSymbol( OUString( "OleUIInsertObjectA" ));
179 if( !pInsertFct )
180 throw uno::RuntimeException();
182 uTemp=pInsertFct(&io);
184 if ( OLEUI_OK == uTemp )
186 if (io.dwFlags & IOF_SELECTCREATENEW)
188 uno::Reference< embed::XEmbeddedObjectCreator > xEmbCreator = embed::EmbeddedObjectCreator::create( comphelper::getComponentContext(m_xFactory) );
190 uno::Sequence< sal_Int8 > aClassID = MimeConfigurationHelper::GetSequenceClassID( io.clsid.Data1,
191 io.clsid.Data2,
192 io.clsid.Data3,
193 io.clsid.Data4[0],
194 io.clsid.Data4[1],
195 io.clsid.Data4[2],
196 io.clsid.Data4[3],
197 io.clsid.Data4[4],
198 io.clsid.Data4[5],
199 io.clsid.Data4[6],
200 io.clsid.Data4[7] );
202 aClassID = GetRelatedInternalID_Impl( aClassID );
204 //TODO: retrieve ClassName
205 OUString aClassName;
206 aObjectInfo.Object = uno::Reference< embed::XEmbeddedObject >(
207 xEmbCreator->createInstanceInitNew( aClassID, aClassName, xStorage, sEntName, aObjArgs ),
208 uno::UNO_QUERY );
210 else
212 OUString aFileName = OStringToOUString( OString( szFile ), osl_getThreadTextEncoding() );
213 OUString aFileURL;
214 if ( osl::FileBase::getFileURLFromSystemPath( aFileName, aFileURL ) != osl::FileBase::E_None )
215 throw uno::RuntimeException();
217 uno::Sequence< beans::PropertyValue > aMediaDescr( 1 );
218 aMediaDescr[0].Name = "URL";
219 aMediaDescr[0].Value <<= aFileURL;
221 // TODO: use config helper for type detection
222 uno::Reference< embed::XEmbeddedObjectCreator > xEmbCreator;
223 ::comphelper::MimeConfigurationHelper aHelper( comphelper::getComponentContext(m_xFactory) );
225 if ( aHelper.AddFilterNameCheckOwnFile( aMediaDescr ) )
226 xEmbCreator = embed::EmbeddedObjectCreator::create( comphelper::getComponentContext(m_xFactory) );
227 else
228 xEmbCreator = embed::OLEEmbeddedObjectFactory::create( comphelper::getComponentContext(m_xFactory) );
230 if ( !xEmbCreator.is() )
231 throw uno::RuntimeException();
233 aObjectInfo.Object = uno::Reference< embed::XEmbeddedObject >(
234 xEmbCreator->createInstanceInitFromMediaDescriptor( xStorage, sEntName, aMediaDescr, aObjArgs ),
235 uno::UNO_QUERY );
238 if ( ( io.dwFlags & IOF_CHECKDISPLAYASICON) && io.hMetaPict != NULL )
240 METAFILEPICT* pMF = ( METAFILEPICT* )GlobalLock( io.hMetaPict );
241 if ( pMF )
243 sal_uInt32 nBufSize = GetMetaFileBitsEx( pMF->hMF, 0, NULL );
244 uno::Sequence< sal_Int8 > aMetafile( nBufSize + 22 );
245 sal_uInt8* pBuf = (sal_uInt8*)( aMetafile.getArray() );
246 *( (long* )pBuf ) = 0x9ac6cdd7L;
247 *( (short* )( pBuf+6 )) = ( SHORT ) 0;
248 *( (short* )( pBuf+8 )) = ( SHORT ) 0;
249 *( (short* )( pBuf+10 )) = ( SHORT ) pMF->xExt;
250 *( (short* )( pBuf+12 )) = ( SHORT ) pMF->yExt;
251 *( (short* )( pBuf+14 )) = ( USHORT ) 2540;
253 if ( nBufSize && nBufSize == GetMetaFileBitsEx( pMF->hMF, nBufSize, pBuf+22 ) )
255 datatransfer::DataFlavor aFlavor(
256 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
257 OUString( "Image WMF" ),
258 getCppuType( ( const uno::Sequence< sal_Int8 >* ) 0 ) );
260 aObjectInfo.Options.realloc( 2 );
261 aObjectInfo.Options[0].Name = "Icon";
262 aObjectInfo.Options[0].Value <<= aMetafile;
263 aObjectInfo.Options[1].Name = "IconFormat";
264 aObjectInfo.Options[1].Value <<= aFlavor;
267 GlobalUnlock( io.hMetaPict );
271 else
272 throw ucb::CommandAbortedException();
274 OSL_ENSURE( aObjectInfo.Object.is(), "No object was created!\n" );
275 if ( !aObjectInfo.Object.is() )
276 throw uno::RuntimeException();
278 return aObjectInfo;
279 #else
280 throw lang::NoSupportException(); // TODO:
281 #endif
284 //-------------------------------------------------------------------------
285 embed::InsertedObjectInfo SAL_CALL MSOLEDialogObjectCreator::createInstanceInitFromClipboard(
286 const uno::Reference< embed::XStorage >& xStorage,
287 const OUString& sEntryName,
288 const uno::Sequence< beans::PropertyValue >& aObjectArgs )
289 throw ( lang::IllegalArgumentException,
290 io::IOException,
291 uno::Exception,
292 uno::RuntimeException )
294 embed::InsertedObjectInfo aObjectInfo;
296 #ifdef WNT
297 if ( !xStorage.is() )
298 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
299 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
300 1 );
302 if ( !sEntryName.getLength() )
303 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
304 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
305 2 );
307 uno::Reference< embed::XEmbeddedObject > xResult(
308 static_cast< ::cppu::OWeakObject* > ( new OleEmbeddedObject( m_xFactory ) ),
309 uno::UNO_QUERY );
311 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
313 if ( !xPersist.is() )
314 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
316 xPersist->setPersistentEntry( xStorage,
317 sEntryName,
318 embed::EntryInitModes::DEFAULT_INIT,
319 uno::Sequence< beans::PropertyValue >(),
320 aObjectArgs );
322 aObjectInfo.Object = xResult;
324 // TODO/LATER: in case of iconifie object the icon should be stored in aObjectInfo
326 OSL_ENSURE( aObjectInfo.Object.is(), "No object was created!\n" );
327 if ( !aObjectInfo.Object.is() )
328 throw uno::RuntimeException();
330 return aObjectInfo;
331 #else
332 throw lang::NoSupportException(); // TODO:
333 #endif
336 //-------------------------------------------------------------------------
337 OUString SAL_CALL MSOLEDialogObjectCreator::getImplementationName()
338 throw ( uno::RuntimeException )
340 return impl_staticGetImplementationName();
343 //-------------------------------------------------------------------------
344 sal_Bool SAL_CALL MSOLEDialogObjectCreator::supportsService( const OUString& ServiceName )
345 throw ( uno::RuntimeException )
347 return cppu::supportsService(this, ServiceName);
350 //-------------------------------------------------------------------------
351 uno::Sequence< OUString > SAL_CALL MSOLEDialogObjectCreator::getSupportedServiceNames()
352 throw ( uno::RuntimeException )
354 return impl_staticGetSupportedServiceNames();
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */