LibreOffice Module embeddedobj (master) 1
xdialogcreator.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <string_view>
23
24#include <com/sun/star/embed/EmbeddedObjectCreator.hpp>
25#include <com/sun/star/embed/XEmbeddedObject.hpp>
26#include <com/sun/star/embed/EntryInitModes.hpp>
27#include <com/sun/star/embed/OLEEmbeddedObjectFactory.hpp>
28#include <com/sun/star/beans/PropertyValue.hpp>
29#include <com/sun/star/datatransfer/DataFlavor.hpp>
30#include <com/sun/star/lang/IllegalArgumentException.hpp>
31#include <com/sun/star/ucb/CommandAbortedException.hpp>
32#include <com/sun/star/task/XStatusIndicatorFactory.hpp>
33
34#include <osl/thread.h>
35#include <osl/file.hxx>
36#include <osl/module.hxx>
38
39#include "platform.h"
44#include <cppuhelper/weak.hxx>
46
47#include "xdialogcreator.hxx"
48#include <oleembobj.hxx>
49
50
51#ifdef _WIN32
52
53#include <oledlg.h>
54#include <vcl/winscheduler.hxx>
55
56namespace {
57
58class InitializedOleGuard
59{
60public:
61 InitializedOleGuard()
62 {
63 if ( !SUCCEEDED( OleInitialize( nullptr ) ) )
64 throw css::uno::RuntimeException();
65 }
66
67 ~InitializedOleGuard()
68 {
69 OleUninitialize();
70 }
71};
72
73}
74
75extern "C" {
76typedef UINT STDAPICALLTYPE OleUIInsertObjectA_Type(LPOLEUIINSERTOBJECTA);
77}
78
79#endif
80
81
82using namespace ::com::sun::star;
83using namespace ::comphelper;
84
85static uno::Sequence< sal_Int8 > GetRelatedInternalID_Impl( const uno::Sequence< sal_Int8 >& aClassID )
86{
87 // Writer
88 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_60 ) )
89 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SW_OLE_EMBED_CLASSID_8 ) ) )
90 return MimeConfigurationHelper::GetSequenceClassID( SO3_SW_CLASSID_60 );
91
92 // Calc
93 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_60 ) )
94 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SC_OLE_EMBED_CLASSID_8 ) ) )
95 return MimeConfigurationHelper::GetSequenceClassID( SO3_SC_CLASSID_60 );
96
97 // Impress
98 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_60 ) )
99 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_OLE_EMBED_CLASSID_8 ) ) )
100 return MimeConfigurationHelper::GetSequenceClassID( SO3_SIMPRESS_CLASSID_60 );
101
102 // Draw
103 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_60 ) )
104 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_OLE_EMBED_CLASSID_8 ) ) )
105 return MimeConfigurationHelper::GetSequenceClassID( SO3_SDRAW_CLASSID_60 );
106
107 // Chart
108 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_60 ) )
109 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_OLE_EMBED_CLASSID_8 ) ) )
110 return MimeConfigurationHelper::GetSequenceClassID( SO3_SCH_CLASSID_60 );
111
112 // Math
113 if ( MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_60 ) )
114 || MimeConfigurationHelper::ClassIDsEqual( aClassID, MimeConfigurationHelper::GetSequenceClassID( SO3_SM_OLE_EMBED_CLASSID_8 ) ) )
115 return MimeConfigurationHelper::GetSequenceClassID( SO3_SM_CLASSID_60 );
116
117 return aClassID;
118}
119
120
121embed::InsertedObjectInfo SAL_CALL MSOLEDialogObjectCreator::createInstanceByDialog(
122 const uno::Reference< embed::XStorage >& xStorage,
123 const OUString& sEntName,
124 const uno::Sequence< beans::PropertyValue >& aInObjArgs )
125{
126 embed::InsertedObjectInfo aObjectInfo;
127 uno::Sequence< beans::PropertyValue > aObjArgs( aInObjArgs );
128
129#ifdef _WIN32
130
131 if ( !xStorage.is() )
132 throw lang::IllegalArgumentException( "No parent storage is provided!",
133 static_cast< ::cppu::OWeakObject* >(this),
134 1 );
135
136 if ( !sEntName.getLength() )
137 throw lang::IllegalArgumentException( "Empty element name is provided!",
138 static_cast< ::cppu::OWeakObject* >(this),
139 2 );
140
141 InitializedOleGuard aGuard;
142
143 OLEUIINSERTOBJECT io = {};
144 char szFile[MAX_PATH];
145 UINT uTemp;
146
147 io.cbStruct = sizeof(io);
148 io.hWndOwner = GetActiveWindow();
149
150 szFile[0] = 0;
151 io.lpszFile = szFile;
152 io.cchFile = MAX_PATH;
153
154 io.dwFlags = IOF_SELECTCREATENEW | IOF_DISABLELINK;
155
156
157 ::osl::Module aOleDlgLib;
158 if( !aOleDlgLib.load( "oledlg" ))
159 throw uno::RuntimeException();
160
161 OleUIInsertObjectA_Type * pInsertFct = reinterpret_cast<OleUIInsertObjectA_Type *>(
162 aOleDlgLib.getSymbol( "OleUIInsertObjectA" ));
163 if( !pInsertFct )
164 throw uno::RuntimeException();
165
166 // Disable any event loop shortcuts by enabling a real timer.
167 // This way the native windows dialog won't block our own processing.
168 WinScheduler::SetForceRealTimer();
169
170 uTemp=pInsertFct(&io);
171
172 if ( OLEUI_OK != uTemp )
173 throw ucb::CommandAbortedException();
174
175 if (io.dwFlags & IOF_SELECTCREATENEW)
176 {
177 uno::Reference< embed::XEmbeddedObjectCreator > xEmbCreator = embed::EmbeddedObjectCreator::create( m_xContext );
178
179 uno::Sequence< sal_Int8 > aClassID = MimeConfigurationHelper::GetSequenceClassID( io.clsid.Data1,
180 io.clsid.Data2,
181 io.clsid.Data3,
182 io.clsid.Data4[0],
183 io.clsid.Data4[1],
184 io.clsid.Data4[2],
185 io.clsid.Data4[3],
186 io.clsid.Data4[4],
187 io.clsid.Data4[5],
188 io.clsid.Data4[6],
189 io.clsid.Data4[7] );
190
192
193 //TODO: retrieve ClassName
194 aObjectInfo.Object.set( xEmbCreator->createInstanceInitNew( aClassID, OUString(), xStorage, sEntName, aObjArgs ),
195 uno::UNO_QUERY );
196 }
197 else
198 {
199 OUString aFileName
200 = OStringToOUString( std::string_view( szFile ), osl_getThreadTextEncoding() );
201 OUString aFileURL;
202 if ( osl::FileBase::getFileURLFromSystemPath( aFileName, aFileURL ) != osl::FileBase::E_None )
203 throw uno::RuntimeException();
204
205 uno::Sequence< beans::PropertyValue > aMediaDescr{ comphelper::makePropertyValue("URL",
206 aFileURL) };
207
208 // TODO: use config helper for type detection
209 uno::Reference< embed::XEmbeddedObjectCreator > xEmbCreator;
211
212 if ( aHelper.AddFilterNameCheckOwnFile( aMediaDescr ) )
213 xEmbCreator = embed::EmbeddedObjectCreator::create( m_xContext );
214 else
215 xEmbCreator = embed::OLEEmbeddedObjectFactory::create( m_xContext );
216
217 if ( !xEmbCreator.is() )
218 throw uno::RuntimeException();
219
220 uno::Reference<task::XStatusIndicator> xProgress;
221 OUString aProgressText;
223 auto it = aMap.find("StatusIndicator");
224 if (it != aMap.end())
225 {
226 it->second >>= xProgress;
227 }
228 it = aMap.find("StatusIndicatorText");
229 if (it != aMap.end())
230 {
231 it->second >>= aProgressText;
232 }
233 if (xProgress.is())
234 {
235 xProgress->start(aProgressText, 100);
236 }
237
238 aObjectInfo.Object.set( xEmbCreator->createInstanceInitFromMediaDescriptor( xStorage, sEntName, aMediaDescr, aObjArgs ),
239 uno::UNO_QUERY );
240
241 if (xProgress.is())
242 {
243 xProgress->end();
244 }
245 }
246
247 if ( ( io.dwFlags & IOF_CHECKDISPLAYASICON) && io.hMetaPict != nullptr )
248 {
249 METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock( io.hMetaPict ));
250 if ( pMF )
251 {
252 sal_uInt32 nBufSize = GetMetaFileBitsEx( pMF->hMF, 0, nullptr );
253 uno::Sequence< sal_Int8 > aMetafile( nBufSize + 22 );
254 sal_Int8* pBuf = aMetafile.getArray();
255 *reinterpret_cast<long*>( pBuf ) = 0x9ac6cdd7L;
256 *reinterpret_cast<short*>( pBuf+6 ) = SHORT(0);
257 *reinterpret_cast<short*>( pBuf+8 ) = SHORT(0);
258 *reinterpret_cast<short*>( pBuf+10 ) = static_cast<SHORT>(pMF->xExt);
259 *reinterpret_cast<short*>( pBuf+12 ) = static_cast<SHORT>(pMF->yExt);
260 *reinterpret_cast<short*>( pBuf+14 ) = USHORT(2540);
261
262 if ( nBufSize && nBufSize == GetMetaFileBitsEx( pMF->hMF, nBufSize, pBuf+22 ) )
263 {
264 datatransfer::DataFlavor aFlavor(
265 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
266 "Image WMF",
267 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
268
269 aObjectInfo.Options = { { "Icon", css::uno::Any(aMetafile) },
270 { "IconFormat", css::uno::Any(aFlavor) } };
271 }
272
273 GlobalUnlock( io.hMetaPict );
274 }
275 }
276
277 OSL_ENSURE( aObjectInfo.Object.is(), "No object was created!" );
278 if ( !aObjectInfo.Object.is() )
279 throw uno::RuntimeException();
280
281 return aObjectInfo;
282#else
283 throw lang::NoSupportException(); // TODO:
284#endif
285}
286
287
289 const uno::Reference< embed::XStorage >& xStorage,
290 const OUString& sEntryName,
291 const uno::Sequence< beans::PropertyValue >& aObjectArgs )
292{
293 embed::InsertedObjectInfo aObjectInfo;
294
295#ifdef _WIN32
296 if ( !xStorage.is() )
297 throw lang::IllegalArgumentException( "No parent storage is provided!",
298 static_cast< ::cppu::OWeakObject* >(this),
299 1 );
300
301 if ( !sEntryName.getLength() )
302 throw lang::IllegalArgumentException( "Empty element name is provided!",
303 static_cast< ::cppu::OWeakObject* >(this),
304 2 );
305
306 uno::Reference< embed::XEmbeddedObject > xResult(
308
309 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
310 xPersist->setPersistentEntry( xStorage,
311 sEntryName,
312 embed::EntryInitModes::DEFAULT_INIT,
313 uno::Sequence< beans::PropertyValue >(),
314 aObjectArgs );
315
316 aObjectInfo.Object = xResult;
317
318 // TODO/LATER: in case of iconify object the icon should be stored in aObjectInfo
319
320 OSL_ENSURE( aObjectInfo.Object.is(), "No object was created!" );
321 if ( !aObjectInfo.Object.is() )
322 throw uno::RuntimeException();
323
324 return aObjectInfo;
325#else
326 throw lang::NoSupportException(); // TODO:
327#endif
328}
329
330
332{
333 return "com.sun.star.comp.embed.MSOLEObjectSystemCreator";
334}
335
336
337sal_Bool SAL_CALL MSOLEDialogObjectCreator::supportsService( const OUString& ServiceName )
338{
340}
341
342
343uno::Sequence< OUString > SAL_CALL MSOLEDialogObjectCreator::getSupportedServiceNames()
344{
345 return { "com.sun.star.embed.MSOLEObjectSystemCreator",
346 "com.sun.star.comp.embed.MSOLEObjectSystemCreator" };
347}
348
349extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
351 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
352{
353 return cppu::acquire(new MSOLEDialogObjectCreator(context));
354}
355
356/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::embed::InsertedObjectInfo SAL_CALL createInstanceInitFromClipboard(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntryName, const css::uno::Sequence< css::beans::PropertyValue > &aObjectArgs) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::embed::InsertedObjectInfo SAL_CALL createInstanceByDialog(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Represents an OLE object that has native data and we try to let an external application handle that d...
Definition: oleembobj.hxx:120
#define SO3_SDRAW_OLE_EMBED_CLASSID_60
#define SO3_SIMPRESS_OLE_EMBED_CLASSID_60
#define SO3_SDRAW_CLASSID_60
#define SO3_SC_CLASSID_60
#define SO3_SM_CLASSID_60
#define SO3_SW_OLE_EMBED_CLASSID_8
#define SO3_SW_OLE_EMBED_CLASSID_60
#define SO3_SW_CLASSID_60
#define SO3_SCH_OLE_EMBED_CLASSID_60
#define SO3_SCH_CLASSID_60
#define SO3_SM_OLE_EMBED_CLASSID_60
#define SO3_SC_OLE_EMBED_CLASSID_60
#define SO3_SIMPRESS_CLASSID_60
#define SO3_SM_OLE_EMBED_CLASSID_8
#define SO3_SDRAW_OLE_EMBED_CLASSID_8
#define SO3_SCH_OLE_EMBED_CLASSID_8
#define SO3_SC_OLE_EMBED_CLASSID_8
#define SO3_SIMPRESS_OLE_EMBED_CLASSID_8
Any aHelper
#define MAX_PATH
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
HashMap_OWString_Interface aMap
const char *const aClassID
unsigned char sal_Bool
signed char sal_Int8
static uno::Sequence< sal_Int8 > GetRelatedInternalID_Impl(const uno::Sequence< sal_Int8 > &aClassID)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * embeddedobj_MSOLEDialogObjectCreator_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)