Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : *
6 : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : *
8 : * OpenOffice.org - a multi-platform office productivity suite
9 : *
10 : * This file is part of OpenOffice.org.
11 : *
12 : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU Lesser General Public License version 3
14 : * only, as published by the Free Software Foundation.
15 : *
16 : * OpenOffice.org is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU Lesser General Public License version 3 for more details
20 : * (a copy is included in the LICENSE file that accompanied this code).
21 : *
22 : * You should have received a copy of the GNU Lesser General Public License
23 : * version 3 along with OpenOffice.org. If not, see
24 : * <http://www.openoffice.org/license.html>
25 : * for a copy of the LGPLv3 License.
26 : *
27 : ************************************************************************/
28 :
29 :
30 : #include <com/sun/star/beans/XPropertySet.hpp>
31 : #include <com/sun/star/frame/Desktop.hpp>
32 : #include <com/sun/star/frame/DispatchHelper.hpp>
33 : #include <com/sun/star/frame/XDispatchProviderInterception.hpp>
34 : #include <com/sun/star/lang/SystemDependent.hpp>
35 : #include <com/sun/star/awt/Toolkit.hpp>
36 : #include <com/sun/star/awt/XVclWindowPeer.hpp>
37 : #include <com/sun/star/util/XCloseable.hpp>
38 : #include <com/sun/star/lang/DisposedException.hpp>
39 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
40 : #include <com/sun/star/presentation/XPresentation.hpp>
41 : #include <com/sun/star/presentation/XPresentationSupplier.hpp>
42 : #include <comphelper/processfactory.hxx>
43 : #include <tools/color.hxx>
44 : #include <rtl/textenc.h>
45 : #include <rtl/locale.h>
46 : #include <osl/nlsupport.h>
47 :
48 : #include <stdio.h>
49 : #include <stdlib.h>
50 : #include <string.h>
51 : #include <unistd.h>
52 : #include <errno.h>
53 :
54 : #include "so_instance.hxx"
55 : #include "ns_debug.hxx"
56 : #include "so_env.hxx"
57 : #include "nsp_windows.hxx"
58 : #include "so_closelistener.hxx"
59 : #include <osl/process.h>
60 :
61 : using namespace com::sun::star::uno;
62 : using namespace com::sun::star::lang;
63 : using namespace com::sun::star::connection;
64 : using namespace cppu;
65 : using namespace com::sun::star;
66 :
67 : using ::rtl::OUString;
68 : using ::rtl::OString;
69 :
70 : char SoPluginInstance::sSO_Dir[] = {0};
71 0 : Reference< XMultiServiceFactory > SoPluginInstance::mxRemoteMSF = Reference< XMultiServiceFactory >(NULL);
72 :
73 0 : SoPluginInstance::SoPluginInstance(long pParent, Reference< XMultiServiceFactory > xMSF):
74 : m_xUnoWin(NULL),
75 : m_xComponent(NULL),
76 : m_xFrame(NULL),
77 : m_xFrames(NULL),
78 : m_xDispatcher(NULL),
79 : m_xDispatchProvider(NULL),
80 0 : m_pCloseListener(NULL)
81 : {
82 0 : m_nWidth = 0;
83 0 : m_nHeight = 0;
84 0 : m_nX = 0;
85 0 : m_nY = 0;
86 0 : m_nFlag = 15;
87 0 : m_bInit = sal_False;
88 0 : m_hParent = 0;
89 0 : m_pParent = pParent;
90 0 : m_dParentStyl = 0;
91 0 : mxRemoteMSF = xMSF;
92 0 : }
93 :
94 0 : SoPluginInstance::~SoPluginInstance()
95 : {
96 0 : }
97 :
98 0 : sal_Bool SoPluginInstance::SetURL(char* aURL)
99 : {
100 0 : debug_fprintf(NSP_LOG_APPEND, "SoPluginInstance::SetURL %s\n", aURL);
101 0 : sal_uInt16 encoding = 11;
102 :
103 0 : rtl_Locale* pLocale = NULL;
104 0 : osl_getProcessLocale(&pLocale);
105 0 : encoding = osl_getTextEncodingFromLocale(pLocale);
106 :
107 0 : m_sURL = ::rtl::OUString(aURL, strlen(aURL), encoding);
108 : debug_fprintf(NSP_LOG_APPEND, "SetURL %s\nencoding is: %d\n", ::rtl::OUStringToOString(m_sURL,
109 0 : RTL_TEXTENCODING_GB_18030).getStr(), m_sURL.getLength(), encoding);
110 0 : return sal_True;
111 : }
112 :
113 : // plugin window UI part: create window, load document
114 0 : sal_Bool SoPluginInstance::LoadDocument(NSP_HWND hParent)
115 : {
116 : // If doc has been loaded, we just resize the window and return
117 0 : if(m_bInit)
118 : {
119 0 : debug_fprintf(NSP_LOG_APPEND, "only reset windows size\n");
120 0 : m_xUnoWin->setPosSize( m_nX, m_nY, m_nWidth, m_nHeight, m_nFlag );
121 : debug_fprintf(NSP_LOG_APPEND, "set windows to x:%d y:%d w:%d h%d falg:%d\n",
122 0 : m_nX, m_nY, m_nWidth, m_nHeight, m_nFlag);
123 0 : return sal_True;
124 : }
125 :
126 : // If mxRemoteMSF is not initialized, we assert and return sal_False
127 0 : if(!mxRemoteMSF.is())
128 : {
129 0 : debug_fprintf(NSP_LOG_APPEND, "Remote StarOfiice ServiceManager is not initilzed correctly!\n");
130 0 : return sal_False;
131 : }
132 :
133 : try
134 : {
135 0 : Reference< beans::XPropertySet > xFactoryProperties( mxRemoteMSF, uno::UNO_QUERY );
136 0 : Reference< uno::XComponentContext > xContext( xFactoryProperties->getPropertyValue( "DefaultContext" ), UNO_QUERY );
137 :
138 : // try to create netscape plugin window
139 0 : Reference< awt::XToolkit2 > xToolkit( awt::Toolkit::create(xContext) );
140 :
141 : // prepare parameters for plugin window
142 0 : css::uno::Any hwndParent = css::uno::makeAny((sal_Int32)hParent);
143 : #ifdef UNIX
144 0 : sal_Int16 nWindowType = css::lang::SystemDependent::SYSTEM_XWINDOW;
145 : #endif //end of UNIX
146 : #ifdef WNT
147 : sal_Int16 nWindowType = css::lang::SystemDependent::SYSTEM_WIN32;
148 : #endif //end of WNT
149 :
150 0 : debug_fprintf(NSP_LOG_APPEND, "print by Nsplugin, try to create plugin container window HWIN:%ld.\n", hParent);
151 :
152 : // create the plugin window
153 : Reference< awt::XWindowPeer > xNewWinPeer =
154 0 : xToolkit->createSystemChild( hwndParent,
155 0 : css::uno::Sequence<sal_Int8>(), nWindowType );
156 0 : if ( !xNewWinPeer.is() )
157 : {
158 0 : debug_fprintf(NSP_LOG_APPEND, "can not create first window\n", hParent);
159 0 : return sal_False;
160 : }
161 :
162 : // get interface of first window
163 0 : m_xUnoWin = Reference< awt::XWindow >( xNewWinPeer, uno::UNO_QUERY );
164 0 : if( !m_xUnoWin.is() )
165 : {
166 0 : debug_fprintf(NSP_LOG_APPEND, "can not get interface of first window\n", hParent);
167 0 : return sal_False;
168 : }
169 :
170 : // initialize window
171 0 : xNewWinPeer->setBackground( COL_GRAY );
172 0 : m_xUnoWin->setVisible( sal_True );
173 0 : m_xUnoWin->setPosSize( m_nX, m_nY, m_nWidth, m_nHeight, m_nFlag );
174 : debug_fprintf(NSP_LOG_APPEND, "set windows to x:%d y:%d w:%d h%d falg:%d\n",
175 0 : m_nX, m_nY, m_nWidth, m_nHeight, m_nFlag);
176 :
177 : // create frame
178 : m_xFrame = Reference< frame::XFrame >(
179 0 : mxRemoteMSF->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Frame") )),
180 0 : uno::UNO_QUERY );
181 0 : if (!m_xFrame.is())
182 : {
183 0 : debug_fprintf(NSP_LOG_APPEND, "can not create frame\n");
184 0 : return sal_False;
185 : }
186 :
187 : // initialize frame
188 0 : m_xFrame->initialize( m_xUnoWin );
189 :
190 : try
191 : {
192 : // currently ignore errors in this code
193 0 : uno::Reference< beans::XPropertySet > xFrameProps( m_xFrame, uno::UNO_QUERY_THROW );
194 0 : uno::Reference< beans::XPropertySet > xLMProps;
195 0 : xFrameProps->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("LayoutManager")) ) >>= xLMProps;
196 0 : if ( xLMProps.is() )
197 0 : xLMProps->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AutomaticToolbars")), uno::makeAny( (sal_Bool)sal_False ) );
198 : }
199 0 : catch( const uno::Exception& )
200 : {}
201 :
202 : // get frames supplier
203 0 : Reference< frame::XDesktop2 > xFramesSupplier = frame::Desktop::create( xContext );
204 :
205 : // get frames
206 0 : m_xFrames = xFramesSupplier->getFrames();
207 0 : if ( !m_xFrames.is() )
208 : {
209 0 : debug_fprintf(NSP_LOG_APPEND, "can not get frames from FramesSupplier\n");
210 0 : return sal_False;
211 : }
212 :
213 : // append m_xFrame to m_xFrames
214 0 : m_xFrames->append( m_xFrame );
215 :
216 : // create XComponentLoader
217 0 : Reference< frame::XComponentLoader > xLoader( m_xFrame, uno::UNO_QUERY );
218 0 : if ( !xLoader.is() )
219 : {
220 0 : debug_fprintf(NSP_LOG_APPEND, "can not get ComponentLoader to load URL\n");
221 0 : return sal_False;
222 : }
223 :
224 : //create stream for the document
225 0 : Reference< ucb::XSimpleFileAccess3 > xSimpleFileAccess( ucb::SimpleFileAccess::create(xContext) );
226 0 : Reference<io::XInputStream> xInputStream = xSimpleFileAccess->openFileRead( m_sURL );
227 :
228 0 : if(!xInputStream.is())
229 : {
230 0 : debug_fprintf(NSP_LOG_APPEND, "can not create XInputStream for URL\n");
231 0 : return sal_False;
232 : }
233 :
234 : // prepare to load document
235 0 : debug_fprintf(NSP_LOG_APPEND, "try to load document\n");
236 0 : Sequence< ::com::sun::star::beans::PropertyValue > setPropValues(3);
237 0 : setPropValues[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("ViewOnly") );
238 0 : setPropValues[0].Value <<= sal_True;
239 0 : setPropValues[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("ReadOnly") );
240 0 : setPropValues[1].Value <<= sal_True;
241 0 : setPropValues[2].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream"));
242 0 : setPropValues[2].Value <<= xInputStream;
243 : /*
244 : setPropValues[ 3 ].Name = "FilterName";
245 : setPropValues[ 3 ].Value = "Flat XML File";*/
246 :
247 : debug_fprintf(NSP_LOG_APPEND, "try to load copy of URL from local file %s:%d\n",
248 : ::rtl::OUStringToOString( m_sURL, RTL_TEXTENCODING_ASCII_US ).getStr( ),
249 0 : m_sURL.getLength() );
250 :
251 : // load document
252 0 : Sequence< ::com::sun::star::beans::PropertyValue > setPropValues2(3);
253 0 : setPropValues2[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("ViewOnly") );
254 0 : setPropValues2[0].Value <<= sal_True;
255 0 : setPropValues2[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("ReadOnly") );
256 0 : setPropValues2[1].Value <<= sal_True;
257 0 : setPropValues2[2].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("AsTemplate") );
258 0 : setPropValues2[2].Value <<= sal_False;
259 0 : m_xComponent = xLoader->loadComponentFromURL(
260 : m_sURL,
261 0 : m_xFrame->getName(), 0, setPropValues2 );
262 :
263 0 : if ( !m_xComponent.is() )
264 : {
265 0 : debug_fprintf(NSP_LOG_APPEND, "print by Nsplugin, Load Componment error\n");
266 0 : return sal_False;
267 : }
268 :
269 : // register the closelistener that will prevent closing of the component
270 0 : Reference< util::XCloseable > xCloseable( m_xFrame, uno::UNO_QUERY );
271 0 : if ( xCloseable.is() )
272 : {
273 0 : m_pCloseListener = new PluginDocumentClosePreventer();
274 0 : m_xCloseListener = Reference< util::XCloseListener >( m_pCloseListener );
275 0 : xCloseable->addCloseListener( m_xCloseListener );
276 : }
277 :
278 : //free the input stream, it is hold by the m_xComponent
279 0 : xInputStream = Reference<io::XInputStream>();
280 :
281 0 : debug_fprintf(NSP_LOG_APPEND, "load document success\n");
282 :
283 : // create frame::XDispatchHelper and frame::XDispatchProvider
284 0 : m_xDispatcher = Reference< frame::XDispatchHelper >( frame::DispatchHelper::create( xContext ) );
285 0 : m_xDispatchProvider = Reference< frame::XDispatchProvider >(m_xFrame, uno::UNO_QUERY);
286 0 : if(!m_xDispatchProvider.is())
287 : {
288 0 : debug_fprintf(NSP_LOG_APPEND, "m_xDispatchProvider can not be getten\n");
289 0 : return sal_False;
290 : }
291 :
292 : //try to enable toolbar and tool windows
293 0 : Sequence< ::com::sun::star::beans::PropertyValue > propertyValue(1);
294 0 : propertyValue[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM("FunctionBarVisible") );
295 0 : propertyValue[0].Value <<= sal_True;
296 0 : m_xDispatcher->executeDispatch(m_xDispatchProvider,
297 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:FunctionBarVisible")),
298 0 : m_xFrame->getName(), 0,
299 0 : propertyValue );
300 :
301 : // reset window style of parent window
302 : #ifdef WNT
303 : m_dParentStyl = ::NSP_ResetWinStyl (m_hParent);
304 : #endif
305 0 : m_bInit = sal_True;
306 :
307 : try
308 : {
309 : // in case of presentation try to set the mode of slide-show, and start it
310 0 : uno::Reference< presentation::XPresentationSupplier > xPresSuppl( m_xComponent, uno::UNO_QUERY_THROW );
311 0 : uno::Reference< presentation::XPresentation > xPres( xPresSuppl->getPresentation(), uno::UNO_SET_THROW );
312 0 : uno::Reference< beans::XPropertySet > xProps( xPresSuppl->getPresentation(), uno::UNO_QUERY_THROW );
313 0 : xProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsFullScreen" ) ), uno::makeAny( sal_False ) );
314 0 : xPres->start();
315 : }
316 0 : catch( const uno::Exception& )
317 0 : {}
318 : }
319 0 : catch( const uno::Exception& e )
320 : {
321 0 : debug_fprintf(NSP_LOG_APPEND, "Unknown exception while loading document in netscape plugin windows\n");
322 0 : OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
323 0 : debug_fprintf(NSP_LOG_APPEND, "error: %s \n", o.pData->buffer );
324 0 : return sal_False;
325 : }
326 0 : return sal_True;
327 : }
328 :
329 0 : sal_Bool SoPluginInstance::SetWindow(NSP_HWND hParent, int x, int y, int w, int h)
330 : {
331 0 : sal_Bool bRetval(sal_True);
332 :
333 0 : if ( m_hParent && !hParent )
334 : {
335 : // the plugin instance has the parent window set already and now it becomes NULL as parent window
336 : // that means that the instance should be closed
337 0 : Destroy();
338 0 : m_hParent = hParent;
339 : }
340 : else
341 : {
342 0 : m_hParent = hParent;
343 :
344 : debug_fprintf(NSP_LOG_APPEND, "SoPluginInstance::SetWindow %s : %d\n",
345 : ::rtl::OUStringToOString(m_sURL, RTL_TEXTENCODING_ASCII_US).getStr(),
346 0 : m_sURL.getLength() );
347 0 : m_nWidth = w;
348 0 : m_nHeight =h;
349 0 : m_nX = x;
350 0 : m_nY = y;
351 :
352 0 : if ( m_sURL.isEmpty() )
353 0 : return sal_True;
354 :
355 0 : if(!mxRemoteMSF.is())
356 : {
357 0 : debug_fprintf(NSP_LOG_APPEND, "Remote StarOfiice ServiceManager is not initilzed correctly!\n");
358 0 : return sal_False;
359 : }
360 0 : debug_fprintf(NSP_LOG_APPEND, "in SoPluginInstance::SetWindow, begin LoadDocument(hParent)\n");
361 0 : bRetval = LoadDocument(hParent); // Load document into current window
362 0 : if(!bRetval){
363 : // try to reload document again
364 0 : debug_fprintf(NSP_LOG_APPEND, "load document error, try to reload it once again\n");
365 0 : mxRemoteMSF = Reference< XMultiServiceFactory >();
366 0 : m_bInit = sal_False;
367 0 : bRetval = LoadDocument(hParent);
368 0 : debug_fprintf(NSP_LOG_APPEND, "load document again, return %d\n", bRetval);
369 : }
370 : }
371 :
372 0 : return bRetval;
373 : }
374 :
375 0 : sal_Bool SoPluginInstance::Destroy(void)
376 : {
377 : #ifdef WNT
378 : if(m_dParentStyl != 0)
379 : NSP_RestoreWinStyl(m_hParent, m_dParentStyl);
380 : #endif
381 :
382 0 : uno::Reference< util::XCloseable > xCloseable( m_xFrame, uno::UNO_QUERY );
383 :
384 : try
385 : {
386 0 : if ( m_xCloseListener.is() )
387 : {
388 0 : if ( m_pCloseListener )
389 0 : m_pCloseListener->StopPreventClose();
390 :
391 0 : if ( xCloseable.is() )
392 0 : xCloseable->removeCloseListener( m_xCloseListener );
393 : }
394 : }
395 0 : catch( const uno::Exception& )
396 : {}
397 :
398 : try
399 : {
400 0 : uno::Sequence< uno::Any > aArgs( 1 );
401 0 : aArgs[0] <<= m_xFrame;
402 : uno::Reference< lang::XComponent > xDocumentCloser(
403 0 : mxRemoteMSF->createInstanceWithArguments(
404 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.embed.DocumentCloser" ) ),
405 0 : aArgs ),
406 0 : uno::UNO_QUERY_THROW );
407 :
408 0 : xDocumentCloser->dispose(); // this call should close the document
409 : }
410 0 : catch( const uno::Exception& )
411 : {
412 0 : debug_fprintf(NSP_LOG_APPEND, "print by Nsplugin.exe, could not close the document correctly!\n");
413 : try
414 : {
415 0 : if ( xCloseable.is() )
416 0 : xCloseable->close( sal_True );
417 : else
418 : {
419 0 : uno::Reference< lang::XComponent > xDisposable( m_xFrame, uno::UNO_QUERY );
420 0 : if ( xDisposable.is() )
421 0 : xDisposable->dispose();
422 : }
423 : }
424 0 : catch (const uno::Exception&)
425 : {
426 : }
427 : }
428 :
429 0 : m_xUnoWin = Reference< awt::XWindow >();
430 0 : m_xComponent = Reference< XComponent >();
431 0 : m_xFrame = Reference< frame::XFrame >();
432 0 : m_xFrames = Reference< frame::XFrames >();
433 0 : m_xDispatcher = Reference< frame::XDispatchHelper >();
434 0 : m_xDispatchProvider = Reference< frame::XDispatchProvider >();
435 0 : return sal_True;
436 : }
437 :
438 0 : sal_Bool SoPluginInstance::Print(void)
439 : {
440 0 : if(!m_xDispatcher.is())
441 0 : return sal_False;
442 :
443 0 : Sequence< ::com::sun::star::beans::PropertyValue > propertyValue(1);
444 0 : m_xDispatcher->executeDispatch(m_xDispatchProvider,
445 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".uno:PrintDefault")),
446 0 : m_xFrame->getName(), 0,
447 0 : propertyValue );
448 0 : return sal_True;
449 0 : }
450 :
451 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|