xref: /trunk/main/embeddedobj/source/msole/xdialogcreator.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_embeddedobj.hxx"
26 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
27 #include <com/sun/star/embed/XEmbeddedObject.hpp>
28 #include <com/sun/star/embed/EntryInitModes.hpp>
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 #include <com/sun/star/datatransfer/DataFlavor.hpp>
31 #include <com/sun/star/ucb/CommandAbortedException.hpp>
32 
33 
34 #include <osl/thread.h>
35 #include <osl/file.hxx>
36 #include <vos/module.hxx>
37 #include <comphelper/classids.hxx>
38 
39 #include "platform.h"
40 #include <comphelper/mimeconfighelper.hxx>
41 
42 #include "xdialogcreator.hxx"
43 #include "oleembobj.hxx"
44 // LLA: tip from FS
45 // #include <confighelper.hxx>
46 #include <xdialogcreator.hxx>
47 #include <oleembobj.hxx>
48 
49 
50 #ifdef WNT
51 
52 #include <oledlg.h>
53 
54 class InitializedOleGuard
55 {
56 public:
InitializedOleGuard()57     InitializedOleGuard()
58     {
59         if ( !SUCCEEDED( OleInitialize( NULL ) ) )
60             throw ::com::sun::star::uno::RuntimeException();
61     }
62 
~InitializedOleGuard()63     ~InitializedOleGuard()
64     {
65         OleUninitialize();
66     }
67 };
68 
69 extern "C" {
70 typedef UINT STDAPICALLTYPE OleUIInsertObjectA_Type(LPOLEUIINSERTOBJECTA);
71 }
72 
73 #endif
74 
75 
76 using namespace ::com::sun::star;
77 using namespace ::comphelper;
78 //-------------------------------------------------------------------------
GetRelatedInternalID_Impl(const uno::Sequence<sal_Int8> & aClassID)79 uno::Sequence< sal_Int8 > GetRelatedInternalID_Impl( const uno::Sequence< sal_Int8 >& aClassID )
80 {
81     // Writer
82     if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_60 ) )
83       || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_8 ) ) )
84         return MimeConfigurationHelper::GetSequenceClassID( SO3_SW_CLASSID_60 );
85 
86     // Calc
87     if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_60 ) )
88       || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_8 ) ) )
89         return MimeConfigurationHelper::GetSequenceClassID( SO3_SC_CLASSID_60 );
90 
91     // Impress
92     if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_60 ) )
93       || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_8 ) ) )
94         return MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_CLASSID_60 );
95 
96     // Draw
97     if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_60 ) )
98       || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_8 ) ) )
99         return MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_CLASSID_60 );
100 
101     // Chart
102     if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_60 ) )
103       || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_8 ) ) )
104         return MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_CLASSID_60 );
105 
106     // Math
107     if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_60 ) )
108       || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_8 ) ) )
109         return MimeConfigurationHelper::GetSequenceClassID( SO3_SM_CLASSID_60 );
110 
111     return aClassID;
112 }
113 
114 //-------------------------------------------------------------------------
impl_staticGetSupportedServiceNames()115 uno::Sequence< ::rtl::OUString > SAL_CALL MSOLEDialogObjectCreator::impl_staticGetSupportedServiceNames()
116 {
117     uno::Sequence< ::rtl::OUString > aRet(2);
118     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.MSOLEObjectSystemCreator");
119     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.MSOLEObjectSystemCreator");
120     return aRet;
121 }
122 
123 //-------------------------------------------------------------------------
impl_staticGetImplementationName()124 ::rtl::OUString SAL_CALL MSOLEDialogObjectCreator::impl_staticGetImplementationName()
125 {
126     return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.MSOLEObjectSystemCreator");
127 }
128 
129 //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)130 uno::Reference< uno::XInterface > SAL_CALL MSOLEDialogObjectCreator::impl_staticCreateSelfInstance(
131             const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
132 {
133     return uno::Reference< uno::XInterface >( *new MSOLEDialogObjectCreator( xServiceManager ) );
134 }
135 
136 //-------------------------------------------------------------------------
createInstanceByDialog(const uno::Reference<embed::XStorage> & xStorage,const::rtl::OUString & sEntName,const uno::Sequence<beans::PropertyValue> & aInObjArgs)137 embed::InsertedObjectInfo SAL_CALL MSOLEDialogObjectCreator::createInstanceByDialog(
138             const uno::Reference< embed::XStorage >& xStorage,
139             const ::rtl::OUString& sEntName,
140             const uno::Sequence< beans::PropertyValue >& aInObjArgs )
141 {
142     embed::InsertedObjectInfo aObjectInfo;
143     uno::Sequence< beans::PropertyValue > aObjArgs( aInObjArgs );
144 
145 #ifdef WNT
146 
147     if ( !xStorage.is() )
148         throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
149                                             uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
150                                             1 );
151 
152     if ( !sEntName.getLength() )
153         throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
154                                             uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
155                                             2 );
156 
157     InitializedOleGuard aGuard;
158 
159     OLEUIINSERTOBJECT io;
160     char szFile[MAX_PATH];
161     UINT uTemp;
162 
163     memset(&io, 0, sizeof(io));
164 
165     io.cbStruct = sizeof(io);
166     io.hWndOwner = GetActiveWindow();
167 
168     szFile[0] = 0;
169     io.lpszFile = szFile;
170     io.cchFile = MAX_PATH;
171 
172     io.dwFlags = IOF_SELECTCREATENEW | IOF_DISABLELINK;
173 
174 
175     ::vos::OModule aOleDlgLib;
176     if( !aOleDlgLib.load( ::rtl::OUString::createFromAscii( "oledlg" ) ) )
177         throw uno::RuntimeException();
178 
179     OleUIInsertObjectA_Type * pInsertFct = (OleUIInsertObjectA_Type *)
180                                 aOleDlgLib.getSymbol( ::rtl::OUString::createFromAscii( "OleUIInsertObjectA" ) );
181     if( !pInsertFct )
182         throw uno::RuntimeException();
183 
184     uTemp=pInsertFct(&io);
185 
186     if ( OLEUI_OK == uTemp )
187     {
188         if (io.dwFlags & IOF_SELECTCREATENEW)
189         {
190             uno::Reference< embed::XEmbedObjectCreator > xEmbCreator(
191                         m_xFactory->createInstance(
192                                 ::rtl::OUString::createFromAscii( "com.sun.star.embed.EmbeddedObjectCreator" ) ),
193                         uno::UNO_QUERY );
194             if ( !xEmbCreator.is() )
195                 throw uno::RuntimeException();
196 
197             uno::Sequence< sal_Int8 > aClassID = MimeConfigurationHelper::GetSequenceClassID( io.clsid.Data1,
198                                                                      io.clsid.Data2,
199                                                                      io.clsid.Data3,
200                                                                      io.clsid.Data4[0],
201                                                                      io.clsid.Data4[1],
202                                                                      io.clsid.Data4[2],
203                                                                      io.clsid.Data4[3],
204                                                                      io.clsid.Data4[4],
205                                                                      io.clsid.Data4[5],
206                                                                      io.clsid.Data4[6],
207                                                                      io.clsid.Data4[7] );
208 
209             aClassID = GetRelatedInternalID_Impl( aClassID );
210 
211             //TODO: retrieve ClassName
212             ::rtl::OUString aClassName;
213             aObjectInfo.Object = uno::Reference< embed::XEmbeddedObject >(
214                             xEmbCreator->createInstanceInitNew( aClassID, aClassName, xStorage, sEntName, aObjArgs ),
215                             uno::UNO_QUERY );
216         }
217         else
218         {
219             ::rtl::OUString aFileName = ::rtl::OStringToOUString( ::rtl::OString( szFile ), osl_getThreadTextEncoding() );
220             rtl::OUString aFileURL;
221             if ( osl::FileBase::getFileURLFromSystemPath( aFileName, aFileURL ) != osl::FileBase::E_None )
222                 throw uno::RuntimeException();
223 
224             uno::Sequence< beans::PropertyValue > aMediaDescr( 1 );
225             aMediaDescr[0].Name = ::rtl::OUString::createFromAscii( "URL" );
226             aMediaDescr[0].Value <<= aFileURL;
227 
228             // TODO: use config helper for type detection
229             uno::Reference< embed::XEmbedObjectCreator > xEmbCreator;
230             ::comphelper::MimeConfigurationHelper aHelper( m_xFactory );
231 
232             if ( aHelper.AddFilterNameCheckOwnFile( aMediaDescr ) )
233                 xEmbCreator = uno::Reference< embed::XEmbedObjectCreator >(
234                         m_xFactory->createInstance(
235                                 ::rtl::OUString::createFromAscii( "com.sun.star.embed.EmbeddedObjectCreator" ) ),
236                         uno::UNO_QUERY );
237             else
238                 xEmbCreator = uno::Reference< embed::XEmbedObjectCreator >(
239                         m_xFactory->createInstance(
240                                 ::rtl::OUString::createFromAscii( "com.sun.star.embed.OLEEmbeddedObjectFactory" ) ),
241                         uno::UNO_QUERY );
242 
243             if ( !xEmbCreator.is() )
244                 throw uno::RuntimeException();
245 
246             aObjectInfo.Object = uno::Reference< embed::XEmbeddedObject >(
247                             xEmbCreator->createInstanceInitFromMediaDescriptor( xStorage, sEntName, aMediaDescr, aObjArgs ),
248                             uno::UNO_QUERY );
249         }
250 
251         if ( ( io.dwFlags & IOF_CHECKDISPLAYASICON) && io.hMetaPict != NULL )
252         {
253             METAFILEPICT* pMF = ( METAFILEPICT* )GlobalLock( io.hMetaPict );
254             if ( pMF )
255             {
256                 sal_uInt32 nBufSize = GetMetaFileBitsEx( pMF->hMF, 0, NULL );
257                 uno::Sequence< sal_Int8 > aMetafile( nBufSize + 22 );
258                 sal_uInt8* pBuf = (sal_uInt8*)( aMetafile.getArray() );
259                 *( (long* )pBuf ) = 0x9ac6cdd7L;
260                 *( (short* )( pBuf+6 )) = ( SHORT ) 0;
261                 *( (short* )( pBuf+8 )) = ( SHORT ) 0;
262                 *( (short* )( pBuf+10 )) = ( SHORT ) pMF->xExt;
263                 *( (short* )( pBuf+12 )) = ( SHORT ) pMF->yExt;
264                 *( (short* )( pBuf+14 )) = ( USHORT ) 2540;
265 
266                 if ( nBufSize && nBufSize == GetMetaFileBitsEx( pMF->hMF, nBufSize, pBuf+22 ) )
267                 {
268                     datatransfer::DataFlavor aFlavor(
269                         ::rtl::OUString::createFromAscii( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
270                         ::rtl::OUString::createFromAscii( "Image WMF" ),
271                         getCppuType( ( const uno::Sequence< sal_Int8 >* ) 0 ) );
272 
273                     aObjectInfo.Options.realloc( 2 );
274                     aObjectInfo.Options[0].Name = ::rtl::OUString::createFromAscii( "Icon" );
275                     aObjectInfo.Options[0].Value <<= aMetafile;
276                     aObjectInfo.Options[1].Name = ::rtl::OUString::createFromAscii( "IconFormat" );
277                     aObjectInfo.Options[1].Value <<= aFlavor;
278                 }
279 
280                 GlobalUnlock( io.hMetaPict );
281             }
282         }
283     }
284     else
285         throw ucb::CommandAbortedException();
286 
287 #else
288     throw lang::NoSupportException(); // TODO:
289 #endif
290 
291     OSL_ENSURE( aObjectInfo.Object.is(), "No object was created!\n" );
292     if ( !aObjectInfo.Object.is() )
293         throw uno::RuntimeException();
294 
295     return aObjectInfo;
296 }
297 
298 //-------------------------------------------------------------------------
createInstanceInitFromClipboard(const uno::Reference<embed::XStorage> & xStorage,const::rtl::OUString & sEntryName,const uno::Sequence<beans::PropertyValue> & aObjectArgs)299 embed::InsertedObjectInfo SAL_CALL MSOLEDialogObjectCreator::createInstanceInitFromClipboard(
300                 const uno::Reference< embed::XStorage >& xStorage,
301                 const ::rtl::OUString& sEntryName,
302                 const uno::Sequence< beans::PropertyValue >& aObjectArgs )
303 {
304     embed::InsertedObjectInfo aObjectInfo;
305 
306 #ifdef WNT
307 
308     if ( !xStorage.is() )
309         throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
310                                             uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
311                                             1 );
312 
313     if ( !sEntryName.getLength() )
314         throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
315                                             uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
316                                             2 );
317 
318     uno::Reference< embed::XEmbeddedObject > xResult(
319                     static_cast< ::cppu::OWeakObject* > ( new OleEmbeddedObject( m_xFactory ) ),
320                     uno::UNO_QUERY );
321 
322     uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
323 
324     if ( !xPersist.is() )
325         throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
326 
327     xPersist->setPersistentEntry( xStorage,
328                                     sEntryName,
329                                     embed::EntryInitModes::DEFAULT_INIT,
330                                     uno::Sequence< beans::PropertyValue >(),
331                                     aObjectArgs );
332 
333     aObjectInfo.Object = xResult;
334 
335     // TODO/LATER: in case of iconifie object the icon should be stored in aObjectInfo
336 #else
337     throw lang::NoSupportException(); // TODO:
338 #endif
339 
340     OSL_ENSURE( aObjectInfo.Object.is(), "No object was created!\n" );
341     if ( !aObjectInfo.Object.is() )
342         throw uno::RuntimeException();
343 
344     return aObjectInfo;
345 }
346 
347 //-------------------------------------------------------------------------
getImplementationName()348 ::rtl::OUString SAL_CALL MSOLEDialogObjectCreator::getImplementationName()
349 {
350     return impl_staticGetImplementationName();
351 }
352 
353 //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)354 sal_Bool SAL_CALL MSOLEDialogObjectCreator::supportsService( const ::rtl::OUString& ServiceName )
355 {
356     uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
357 
358     for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
359         if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
360             return sal_True;
361 
362     return sal_False;
363 }
364 
365 //-------------------------------------------------------------------------
getSupportedServiceNames()366 uno::Sequence< ::rtl::OUString > SAL_CALL MSOLEDialogObjectCreator::getSupportedServiceNames()
367 {
368     return impl_staticGetSupportedServiceNames();
369 }
370