12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52722ceddSAndrew Rist  * distributed with this work for additional information
62722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
102722ceddSAndrew Rist  *
112722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122722ceddSAndrew Rist  *
132722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist  * software distributed under the License is distributed on an
152722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
172722ceddSAndrew Rist  * specific language governing permissions and limitations
182722ceddSAndrew Rist  * under the License.
192722ceddSAndrew Rist  *
202722ceddSAndrew Rist  *************************************************************/
212722ceddSAndrew Rist 
222722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "app.hxx"
28cdf0e10cSrcweir #include "officeipcthread.hxx"
29cdf0e10cSrcweir #include "cmdlineargs.hxx"
30cdf0e10cSrcweir #include "dispatchwatcher.hxx"
31cdf0e10cSrcweir #include <memory>
32cdf0e10cSrcweir #include <stdio.h>
33cdf0e10cSrcweir #include <vos/process.hxx>
34cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
35cdf0e10cSrcweir #include <vcl/svapp.hxx>
36cdf0e10cSrcweir #include <vcl/help.hxx>
37cdf0e10cSrcweir #include <unotools/configmgr.hxx>
38cdf0e10cSrcweir #include <osl/thread.hxx>
39cdf0e10cSrcweir #include <rtl/digest.h>
40cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
41cdf0e10cSrcweir #include <rtl/instance.hxx>
42cdf0e10cSrcweir #include <osl/conditn.hxx>
43cdf0e10cSrcweir #include <unotools/moduleoptions.hxx>
44cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
45cdf0e10cSrcweir #include <rtl/strbuf.hxx>
46cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
47cdf0e10cSrcweir #include "osl/file.hxx"
48cdf0e10cSrcweir #include "rtl/process.h"
49cdf0e10cSrcweir #include "tools/getprocessworkingdir.hxx"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir using namespace vos;
52cdf0e10cSrcweir using namespace rtl;
53cdf0e10cSrcweir using namespace desktop;
54cdf0e10cSrcweir using namespace ::com::sun::star::uno;
55cdf0e10cSrcweir using namespace ::com::sun::star::lang;
56cdf0e10cSrcweir using namespace ::com::sun::star::frame;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir const char  *OfficeIPCThread::sc_aTerminationSequence = "InternalIPC::TerminateThread";
59cdf0e10cSrcweir const int OfficeIPCThread::sc_nTSeqLength = 28;
60cdf0e10cSrcweir const char  *OfficeIPCThread::sc_aShowSequence = "-tofront";
61cdf0e10cSrcweir const int OfficeIPCThread::sc_nShSeqLength = 5;
62cdf0e10cSrcweir const char  *OfficeIPCThread::sc_aConfirmationSequence = "InternalIPC::ProcessingDone";
63cdf0e10cSrcweir const int OfficeIPCThread::sc_nCSeqLength = 27;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir namespace { static char const ARGUMENT_PREFIX[] = "InternalIPC::Arguments"; }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir // Type of pipe we use
68cdf0e10cSrcweir enum PipeMode
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	PIPEMODE_DONTKNOW,
71cdf0e10cSrcweir 	PIPEMODE_CREATED,
72cdf0e10cSrcweir 	PIPEMODE_CONNECTED
73cdf0e10cSrcweir };
74cdf0e10cSrcweir 
75cdf0e10cSrcweir namespace desktop
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 
78cdf0e10cSrcweir namespace {
79cdf0e10cSrcweir 
80cdf0e10cSrcweir class Parser: public CommandLineArgs::Supplier {
81cdf0e10cSrcweir public:
82cdf0e10cSrcweir     explicit Parser(rtl::OString const & input): m_input(input) {
83cdf0e10cSrcweir         if (!m_input.match(ARGUMENT_PREFIX) ||
84cdf0e10cSrcweir             m_input.getLength() == RTL_CONSTASCII_LENGTH(ARGUMENT_PREFIX))
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             throw CommandLineArgs::Supplier::Exception();
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         m_index = RTL_CONSTASCII_LENGTH(ARGUMENT_PREFIX);
89cdf0e10cSrcweir         switch (m_input[m_index++]) {
90cdf0e10cSrcweir         case '0':
91cdf0e10cSrcweir             break;
92cdf0e10cSrcweir         case '1':
93cdf0e10cSrcweir             {
94cdf0e10cSrcweir                 rtl::OUString url;
95cdf0e10cSrcweir                 if (!next(&url, false)) {
96cdf0e10cSrcweir                     throw CommandLineArgs::Supplier::Exception();
97cdf0e10cSrcweir                 }
98cdf0e10cSrcweir                 m_cwdUrl.reset(url);
99cdf0e10cSrcweir                 break;
100cdf0e10cSrcweir             }
101cdf0e10cSrcweir         case '2':
102cdf0e10cSrcweir             {
103cdf0e10cSrcweir                 rtl::OUString path;
104cdf0e10cSrcweir                 if (!next(&path, false)) {
105cdf0e10cSrcweir                     throw CommandLineArgs::Supplier::Exception();
106cdf0e10cSrcweir                 }
107cdf0e10cSrcweir                 rtl::OUString url;
108cdf0e10cSrcweir                 if (osl::FileBase::getFileURLFromSystemPath(path, url) ==
109cdf0e10cSrcweir                     osl::FileBase::E_None)
110cdf0e10cSrcweir                 {
111cdf0e10cSrcweir                     m_cwdUrl.reset(url);
112cdf0e10cSrcweir                 }
113cdf0e10cSrcweir                 break;
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir         default:
116cdf0e10cSrcweir             throw CommandLineArgs::Supplier::Exception();
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     virtual ~Parser() {}
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     virtual boost::optional< rtl::OUString > getCwdUrl() { return m_cwdUrl; }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     virtual bool next(rtl::OUString * argument) { return next(argument, true); }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir private:
127cdf0e10cSrcweir     virtual bool next(rtl::OUString * argument, bool prefix) {
128cdf0e10cSrcweir         OSL_ASSERT(argument != NULL);
129cdf0e10cSrcweir         if (m_index < m_input.getLength()) {
130cdf0e10cSrcweir             if (prefix) {
131cdf0e10cSrcweir                 if (m_input[m_index] != ',') {
132cdf0e10cSrcweir                     throw CommandLineArgs::Supplier::Exception();
133cdf0e10cSrcweir                 }
134cdf0e10cSrcweir                 ++m_index;
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir             rtl::OStringBuffer b;
137cdf0e10cSrcweir             while (m_index < m_input.getLength()) {
138cdf0e10cSrcweir                 char c = m_input[m_index];
139cdf0e10cSrcweir                 if (c == ',') {
140cdf0e10cSrcweir                     break;
141cdf0e10cSrcweir                 }
142cdf0e10cSrcweir                 ++m_index;
143cdf0e10cSrcweir                 if (c == '\\') {
144cdf0e10cSrcweir                     if (m_index < m_input.getLength()) {
145cdf0e10cSrcweir                         c = m_input[m_index++];
146cdf0e10cSrcweir                         switch (c) {
147cdf0e10cSrcweir                         case '0':
148cdf0e10cSrcweir                             c = '\0';
149cdf0e10cSrcweir                             break;
150cdf0e10cSrcweir                         case ',':
151cdf0e10cSrcweir                         case '\\':
152cdf0e10cSrcweir                             break;
153cdf0e10cSrcweir                         default:
154cdf0e10cSrcweir                             throw CommandLineArgs::Supplier::Exception();
155cdf0e10cSrcweir                         }
156cdf0e10cSrcweir                     } else {
157cdf0e10cSrcweir                         throw CommandLineArgs::Supplier::Exception();
158cdf0e10cSrcweir                     }
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir                 b.append(c);
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir             rtl::OString b2(b.makeStringAndClear());
163cdf0e10cSrcweir             if (!rtl_convertStringToUString(
164cdf0e10cSrcweir                     &argument->pData, b2.getStr(), b2.getLength(),
165cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8,
166cdf0e10cSrcweir                     (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
167cdf0e10cSrcweir                      RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
168cdf0e10cSrcweir                      RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
169cdf0e10cSrcweir             {
170cdf0e10cSrcweir                 throw CommandLineArgs::Supplier::Exception();
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir             return true;
173cdf0e10cSrcweir         } else {
174cdf0e10cSrcweir             return false;
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     boost::optional< rtl::OUString > m_cwdUrl;
179cdf0e10cSrcweir     rtl::OString m_input;
180cdf0e10cSrcweir     sal_Int32 m_index;
181cdf0e10cSrcweir };
182cdf0e10cSrcweir 
183cdf0e10cSrcweir bool addArgument(
184cdf0e10cSrcweir     ByteString * arguments, char prefix, rtl::OUString const & argument)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     rtl::OString utf8;
187cdf0e10cSrcweir     if (!argument.convertToString(
188cdf0e10cSrcweir             &utf8, RTL_TEXTENCODING_UTF8,
189cdf0e10cSrcweir             (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
190cdf0e10cSrcweir              RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         return false;
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir     *arguments += prefix;
195cdf0e10cSrcweir     for (sal_Int32 i = 0; i < utf8.getLength(); ++i) {
196cdf0e10cSrcweir         char c = utf8[i];
197cdf0e10cSrcweir         switch (c) {
198cdf0e10cSrcweir         case '\0':
199cdf0e10cSrcweir             *arguments += "\\0";
200cdf0e10cSrcweir             break;
201cdf0e10cSrcweir         case ',':
202cdf0e10cSrcweir             *arguments += "\\,";
203cdf0e10cSrcweir             break;
204cdf0e10cSrcweir         case '\\':
205cdf0e10cSrcweir             *arguments += "\\\\";
206cdf0e10cSrcweir             break;
207cdf0e10cSrcweir         default:
208cdf0e10cSrcweir             *arguments += c;
209cdf0e10cSrcweir             break;
210cdf0e10cSrcweir         }
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir     return true;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir OfficeIPCThread*	OfficeIPCThread::pGlobalOfficeIPCThread = 0;
218cdf0e10cSrcweir namespace { struct Security : public rtl::Static<OSecurity, Security> {}; }
219cdf0e10cSrcweir ::osl::Mutex*		OfficeIPCThread::pOfficeIPCThreadMutex = 0;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 
222cdf0e10cSrcweir String CreateMD5FromString( const OUString& aMsg )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	// PRE: aStr "file"
225cdf0e10cSrcweir 	// BACK: Str "ababab....0f" Hexcode String
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
228cdf0e10cSrcweir 	if ( handle > 0 )
229cdf0e10cSrcweir 	{
230cdf0e10cSrcweir 		const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr();
231cdf0e10cSrcweir 		sal_uInt32		 nSize = ( aMsg.getLength() * sizeof( sal_Unicode ));
232cdf0e10cSrcweir 		sal_uInt32		 nMD5KeyLen = rtl_digest_queryLength( handle );
233cdf0e10cSrcweir 		sal_uInt8*		 pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ];
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 		rtl_digest_init( handle, pData, nSize );
236cdf0e10cSrcweir 		rtl_digest_update( handle, pData, nSize );
237cdf0e10cSrcweir 		rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen );
238cdf0e10cSrcweir 		rtl_digest_destroy( handle );
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 		// Create hex-value string from the MD5 value to keep the string size minimal
241cdf0e10cSrcweir 		OUStringBuffer aBuffer( nMD5KeyLen * 2 + 1 );
242cdf0e10cSrcweir 		for ( sal_uInt32 i = 0; i < nMD5KeyLen; i++ )
243cdf0e10cSrcweir 			aBuffer.append( (sal_Int32)pMD5KeyBuffer[i], 16 );
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 		delete [] pMD5KeyBuffer;
246cdf0e10cSrcweir 		return aBuffer.makeStringAndClear();
247cdf0e10cSrcweir 	}
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	return String();
250cdf0e10cSrcweir }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir class ProcessEventsClass_Impl
253cdf0e10cSrcweir {
254cdf0e10cSrcweir public:
255cdf0e10cSrcweir 	DECL_STATIC_LINK( ProcessEventsClass_Impl, CallEvent, void* pEvent );
256cdf0e10cSrcweir 	DECL_STATIC_LINK( ProcessEventsClass_Impl, ProcessDocumentsEvent, void* pEvent );
257cdf0e10cSrcweir };
258cdf0e10cSrcweir 
259cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( ProcessEventsClass_Impl, CallEvent, void*, pEvent )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir 	// Application events are processed by the Desktop::HandleAppEvent implementation.
262cdf0e10cSrcweir 	Desktop::HandleAppEvent( *((ApplicationEvent*)pEvent) );
263cdf0e10cSrcweir 	delete (ApplicationEvent*)pEvent;
264cdf0e10cSrcweir 	return 0;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( ProcessEventsClass_Impl, ProcessDocumentsEvent, void*, pEvent )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir 	// Documents requests are processed by the OfficeIPCThread implementation
270cdf0e10cSrcweir 	ProcessDocumentsRequest* pDocsRequest = (ProcessDocumentsRequest*)pEvent;
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	if ( pDocsRequest )
273cdf0e10cSrcweir 	{
274cdf0e10cSrcweir 		OfficeIPCThread::ExecuteCmdLineRequests( *pDocsRequest );
275cdf0e10cSrcweir 		delete pDocsRequest;
276cdf0e10cSrcweir 	}
277cdf0e10cSrcweir 	return 0;
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
280cdf0e10cSrcweir void ImplPostForeignAppEvent( ApplicationEvent* pEvent )
281cdf0e10cSrcweir {
282cdf0e10cSrcweir 	Application::PostUserEvent( STATIC_LINK( NULL, ProcessEventsClass_Impl, CallEvent ), pEvent );
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir void ImplPostProcessDocumentsEvent( ProcessDocumentsRequest* pEvent )
286cdf0e10cSrcweir {
287cdf0e10cSrcweir 	Application::PostUserEvent( STATIC_LINK( NULL, ProcessEventsClass_Impl, ProcessDocumentsEvent ), pEvent );
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir OSignalHandler::TSignalAction SAL_CALL SalMainPipeExchangeSignalHandler::signal(TSignalInfo *pInfo)
291cdf0e10cSrcweir {
292cdf0e10cSrcweir     if( pInfo->Signal == osl_Signal_Terminate )
293cdf0e10cSrcweir 		OfficeIPCThread::DisableOfficeIPCThread();
294cdf0e10cSrcweir 	return (TAction_CallNextHandler);
295cdf0e10cSrcweir }
296cdf0e10cSrcweir 
297cdf0e10cSrcweir // ----------------------------------------------------------------------------
298cdf0e10cSrcweir 
299cdf0e10cSrcweir // The OfficeIPCThreadController implementation is a bookkeeper for all pending requests
300cdf0e10cSrcweir // that were created by the OfficeIPCThread. The requests are waiting to be processed by
301cdf0e10cSrcweir // our framework loadComponentFromURL function (e.g. open/print request).
302cdf0e10cSrcweir // During shutdown the framework is asking OfficeIPCThreadController about pending requests.
303cdf0e10cSrcweir // If there are pending requests framework has to stop the shutdown process. It is waiting
304cdf0e10cSrcweir // for these requests because framework is not able to handle shutdown and open a document
305cdf0e10cSrcweir // concurrently.
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 
308cdf0e10cSrcweir // XServiceInfo
309cdf0e10cSrcweir OUString SAL_CALL OfficeIPCThreadController::getImplementationName()
310cdf0e10cSrcweir throw ( RuntimeException )
311cdf0e10cSrcweir {
312cdf0e10cSrcweir 	return OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.OfficeIPCThreadController" ));
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir sal_Bool SAL_CALL OfficeIPCThreadController::supportsService( const OUString& )
316cdf0e10cSrcweir throw ( RuntimeException )
317cdf0e10cSrcweir {
318cdf0e10cSrcweir 	return sal_False;
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir Sequence< OUString > SAL_CALL OfficeIPCThreadController::getSupportedServiceNames()
322cdf0e10cSrcweir throw ( RuntimeException )
323cdf0e10cSrcweir {
324cdf0e10cSrcweir 	Sequence< OUString > aSeq( 0 );
325cdf0e10cSrcweir 	return aSeq;
326cdf0e10cSrcweir }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir // XEventListener
329cdf0e10cSrcweir void SAL_CALL OfficeIPCThreadController::disposing( const EventObject& )
330cdf0e10cSrcweir throw( RuntimeException )
331cdf0e10cSrcweir {
332cdf0e10cSrcweir }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir // XTerminateListener
335cdf0e10cSrcweir void SAL_CALL OfficeIPCThreadController::queryTermination( const EventObject& )
336cdf0e10cSrcweir throw( TerminationVetoException, RuntimeException )
337cdf0e10cSrcweir {
338cdf0e10cSrcweir 	// Desktop ask about pending request through our office ipc pipe. We have to
339cdf0e10cSrcweir 	// be sure that no pending request is waiting because framework is not able to
340cdf0e10cSrcweir 	// handle shutdown and open a document concurrently.
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 	if ( OfficeIPCThread::AreRequestsPending() )
343cdf0e10cSrcweir 		throw TerminationVetoException();
344cdf0e10cSrcweir 	else
345cdf0e10cSrcweir 		OfficeIPCThread::SetDowning();
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir void SAL_CALL OfficeIPCThreadController::notifyTermination( const EventObject& )
349cdf0e10cSrcweir throw( RuntimeException )
350cdf0e10cSrcweir {
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir // ----------------------------------------------------------------------------
354cdf0e10cSrcweir 
355cdf0e10cSrcweir ::osl::Mutex&	OfficeIPCThread::GetMutex()
356cdf0e10cSrcweir {
357cdf0e10cSrcweir 	// Get or create our mutex for thread-saftey
358cdf0e10cSrcweir 	if ( !pOfficeIPCThreadMutex )
359cdf0e10cSrcweir 	{
360cdf0e10cSrcweir 		::osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
361cdf0e10cSrcweir 		if ( !pOfficeIPCThreadMutex )
362cdf0e10cSrcweir 			pOfficeIPCThreadMutex = new osl::Mutex;
363cdf0e10cSrcweir 	}
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 	return *pOfficeIPCThreadMutex;
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir void OfficeIPCThread::SetDowning()
369cdf0e10cSrcweir {
370cdf0e10cSrcweir 	// We have the order to block all incoming requests. Framework
371cdf0e10cSrcweir 	// wants to shutdown and we have to make sure that no loading/printing
372cdf0e10cSrcweir 	// requests are executed anymore.
373cdf0e10cSrcweir 	::osl::MutexGuard	aGuard( GetMutex() );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 	if ( pGlobalOfficeIPCThread )
376cdf0e10cSrcweir 		pGlobalOfficeIPCThread->mbDowning = true;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir static bool s_bInEnableRequests = false;
380cdf0e10cSrcweir 
381cdf0e10cSrcweir void OfficeIPCThread::EnableRequests( bool i_bEnable )
382cdf0e10cSrcweir {
383cdf0e10cSrcweir     // switch between just queueing the requests and executing them
384cdf0e10cSrcweir 	::osl::MutexGuard	aGuard( GetMutex() );
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 	if ( pGlobalOfficeIPCThread )
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir         s_bInEnableRequests = true;
389cdf0e10cSrcweir 		pGlobalOfficeIPCThread->mbRequestsEnabled = i_bEnable;
390cdf0e10cSrcweir         if( i_bEnable )
391cdf0e10cSrcweir         {
392cdf0e10cSrcweir             // hit the compiler over the head
393cdf0e10cSrcweir             ProcessDocumentsRequest aEmptyReq = ProcessDocumentsRequest( boost::optional< rtl::OUString >() );
394cdf0e10cSrcweir             // trigger already queued requests
395cdf0e10cSrcweir             OfficeIPCThread::ExecuteCmdLineRequests( aEmptyReq );
396cdf0e10cSrcweir         }
397cdf0e10cSrcweir         s_bInEnableRequests = false;
398cdf0e10cSrcweir     }
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir sal_Bool OfficeIPCThread::AreRequestsPending()
402cdf0e10cSrcweir {
403cdf0e10cSrcweir 	// Give info about pending requests
404cdf0e10cSrcweir 	::osl::MutexGuard	aGuard( GetMutex() );
405cdf0e10cSrcweir 	if ( pGlobalOfficeIPCThread )
406cdf0e10cSrcweir 		return ( pGlobalOfficeIPCThread->mnPendingRequests > 0 );
407cdf0e10cSrcweir 	else
408cdf0e10cSrcweir 		return sal_False;
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir void OfficeIPCThread::RequestsCompleted( int nCount )
412cdf0e10cSrcweir {
413cdf0e10cSrcweir 	// Remove nCount pending requests from our internal counter
414cdf0e10cSrcweir 	::osl::MutexGuard	aGuard( GetMutex() );
415cdf0e10cSrcweir 	if ( pGlobalOfficeIPCThread )
416cdf0e10cSrcweir 	{
417cdf0e10cSrcweir 		if ( pGlobalOfficeIPCThread->mnPendingRequests > 0 )
418cdf0e10cSrcweir 			pGlobalOfficeIPCThread->mnPendingRequests -= nCount;
419cdf0e10cSrcweir 	}
420cdf0e10cSrcweir }
421cdf0e10cSrcweir 
422cdf0e10cSrcweir OfficeIPCThread::Status OfficeIPCThread::EnableOfficeIPCThread()
423cdf0e10cSrcweir {
424cdf0e10cSrcweir 	::osl::MutexGuard	aGuard( GetMutex() );
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 	if( pGlobalOfficeIPCThread )
427cdf0e10cSrcweir 		return IPC_STATUS_OK;
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 	::rtl::OUString aUserInstallPath;
430cdf0e10cSrcweir     ::rtl::OUString aDummy;
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 	::vos::OStartupInfo aInfo;
433cdf0e10cSrcweir 	OfficeIPCThread* pThread = new OfficeIPCThread;
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 	pThread->maPipeIdent = OUString( RTL_CONSTASCII_USTRINGPARAM( "SingleOfficeIPC_" ) );
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 	// The name of the named pipe is created with the hashcode of the user installation directory (without /user). We have to retrieve
438cdf0e10cSrcweir 	// this information from a unotools implementation.
439cdf0e10cSrcweir 	::utl::Bootstrap::PathStatus aLocateResult = ::utl::Bootstrap::locateUserInstallation( aUserInstallPath );
440cdf0e10cSrcweir 	if ( aLocateResult == ::utl::Bootstrap::PATH_EXISTS || aLocateResult == ::utl::Bootstrap::PATH_VALID)
441cdf0e10cSrcweir 		aDummy = aUserInstallPath;
442cdf0e10cSrcweir 	else
443cdf0e10cSrcweir 	{
444cdf0e10cSrcweir 		delete pThread;
445cdf0e10cSrcweir 		return IPC_STATUS_BOOTSTRAP_ERROR;
446cdf0e10cSrcweir 	}
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 	// Try to  determine if we are the first office or not! This should prevent multiple
449cdf0e10cSrcweir 	// access to the user directory !
450cdf0e10cSrcweir 	// First we try to create our pipe if this fails we try to connect. We have to do this
451cdf0e10cSrcweir 	// in a loop because the the other office can crash or shutdown between createPipe
452cdf0e10cSrcweir 	// and connectPipe!!
453cdf0e10cSrcweir 
454cdf0e10cSrcweir     OUString            aIniName;
455cdf0e10cSrcweir 
456cdf0e10cSrcweir     aInfo.getExecutableFile( aIniName );
457cdf0e10cSrcweir     sal_uInt32     lastIndex = aIniName.lastIndexOf('/');
458cdf0e10cSrcweir     if ( lastIndex > 0 )
459cdf0e10cSrcweir     {
460cdf0e10cSrcweir         aIniName    = aIniName.copy( 0, lastIndex+1 );
461cdf0e10cSrcweir         aIniName    += OUString( RTL_CONSTASCII_USTRINGPARAM( "perftune" ));
462cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
463cdf0e10cSrcweir         aIniName    += OUString( RTL_CONSTASCII_USTRINGPARAM( ".ini" ));
464cdf0e10cSrcweir #else
465cdf0e10cSrcweir         aIniName    += OUString( RTL_CONSTASCII_USTRINGPARAM( "rc" ));
466cdf0e10cSrcweir #endif
467cdf0e10cSrcweir     }
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 	::rtl::Bootstrap aPerfTuneIniFile( aIniName );
470cdf0e10cSrcweir 
471cdf0e10cSrcweir     OUString aDefault( RTL_CONSTASCII_USTRINGPARAM( "0" ));
472cdf0e10cSrcweir     OUString aPreloadData;
473cdf0e10cSrcweir 
474cdf0e10cSrcweir     aPerfTuneIniFile.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "FastPipeCommunication" )), aPreloadData, aDefault );
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	OUString aUserInstallPathHashCode;
478cdf0e10cSrcweir 
479cdf0e10cSrcweir     if ( aPreloadData.equalsAscii( "1" ))
480cdf0e10cSrcweir     {
481cdf0e10cSrcweir 		sal_Char	szBuffer[32];
482cdf0e10cSrcweir 		sprintf( szBuffer, "%d", SUPD );
483cdf0e10cSrcweir 		aUserInstallPathHashCode = OUString( szBuffer, strlen(szBuffer), osl_getThreadTextEncoding() );
484cdf0e10cSrcweir     }
485cdf0e10cSrcweir 	else
486cdf0e10cSrcweir 		aUserInstallPathHashCode = CreateMD5FromString( aDummy );
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 	// Check result to create a hash code from the user install path
490cdf0e10cSrcweir 	if ( aUserInstallPathHashCode.getLength() == 0 )
491cdf0e10cSrcweir 		return IPC_STATUS_BOOTSTRAP_ERROR; // Something completely broken, we cannot create a valid hash code!
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 	pThread->maPipeIdent = pThread->maPipeIdent + aUserInstallPathHashCode;
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 	PipeMode nPipeMode = PIPEMODE_DONTKNOW;
496cdf0e10cSrcweir 	do
497cdf0e10cSrcweir 	{
498cdf0e10cSrcweir 		OSecurity &rSecurity = Security::get();
499*aa94018dSJürgen Schmidt 		// #119950# Try to connect pipe first. If connected, means another instance already launched.
500*aa94018dSJürgen Schmidt 		if( pThread->maPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Open, rSecurity ))
501cdf0e10cSrcweir 		{
502*aa94018dSJürgen Schmidt 			// #119950# Test if launched in a new terminal session for same user. On Windows platform, normally a user is resticted
503*aa94018dSJürgen Schmidt 			// to have only one terminal session. But if mutiple terminal session for one user is allowed, crash will happen if launched
504*aa94018dSJürgen Schmidt 			// OpenOffice from more than one terminal session. So need to detect and prevent this happen.
505*aa94018dSJürgen Schmidt 
506*aa94018dSJürgen Schmidt 			// Will try to create a same name pipe. If creation is successfully, means current instance is launched in a new session.
507*aa94018dSJürgen Schmidt 			vos::OPipe	maSessionPipe;
508*aa94018dSJürgen Schmidt 			if ( maSessionPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Create, rSecurity )) {
509*aa94018dSJürgen Schmidt 				// Can create a pipe with same name. This can only happen in multiple terminal session environment on Windows platform.
510*aa94018dSJürgen Schmidt 				// Will display a warning dialog and exit.
511*aa94018dSJürgen Schmidt 				return IPC_STATUS_MULTI_TS_ERROR;
512*aa94018dSJürgen Schmidt 			} else {
513*aa94018dSJürgen Schmidt 				// Pipe connected to first office
514*aa94018dSJürgen Schmidt 				nPipeMode = PIPEMODE_CONNECTED;
515*aa94018dSJürgen Schmidt 			}
516*aa94018dSJürgen Schmidt 
517cdf0e10cSrcweir 		}
518*aa94018dSJürgen Schmidt 		else if ( pThread->maPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Create, rSecurity )) // Connection not successfull, now we try to create
519cdf0e10cSrcweir 		{
520*aa94018dSJürgen Schmidt 			// Pipe created
521*aa94018dSJürgen Schmidt 			nPipeMode = PIPEMODE_CREATED;
522cdf0e10cSrcweir 		}
523cdf0e10cSrcweir 		else
524cdf0e10cSrcweir 		{
525cdf0e10cSrcweir 			OPipe::TPipeError eReason = pThread->maPipe.getError();
526cdf0e10cSrcweir 			if ((eReason == OPipe::E_ConnectionRefused) || (eReason == OPipe::E_invalidError))
527cdf0e10cSrcweir 				return IPC_STATUS_BOOTSTRAP_ERROR;
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 			// Wait for second office to be ready
530cdf0e10cSrcweir 			TimeValue aTimeValue;
531cdf0e10cSrcweir 			aTimeValue.Seconds = 0;
532cdf0e10cSrcweir 			aTimeValue.Nanosec = 10000000; // 10ms
533cdf0e10cSrcweir 			osl::Thread::wait( aTimeValue );
534cdf0e10cSrcweir 		}
535cdf0e10cSrcweir 
536cdf0e10cSrcweir 	} while ( nPipeMode == PIPEMODE_DONTKNOW );
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 	if ( nPipeMode == PIPEMODE_CREATED )
539cdf0e10cSrcweir 	{
540cdf0e10cSrcweir 		// Seems we are the one and only, so start listening thread
541cdf0e10cSrcweir 		pGlobalOfficeIPCThread = pThread;
542cdf0e10cSrcweir 		pThread->create(); // starts thread
543cdf0e10cSrcweir 	}
544cdf0e10cSrcweir 	else
545cdf0e10cSrcweir 	{
546cdf0e10cSrcweir 		// Seems another office is running. Pipe arguments to it and self terminate
547cdf0e10cSrcweir 		pThread->maStreamPipe = pThread->maPipe;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 		sal_Bool bWaitBeforeClose = sal_False;
550cdf0e10cSrcweir 		ByteString aArguments(RTL_CONSTASCII_STRINGPARAM(ARGUMENT_PREFIX));
551cdf0e10cSrcweir         rtl::OUString cwdUrl;
552cdf0e10cSrcweir         if (!(tools::getProcessWorkingDir(&cwdUrl) &&
553cdf0e10cSrcweir               addArgument(&aArguments, '1', cwdUrl)))
554cdf0e10cSrcweir         {
555cdf0e10cSrcweir             aArguments += '0';
556cdf0e10cSrcweir         }
557cdf0e10cSrcweir         sal_uInt32 nCount = rtl_getAppCommandArgCount();
558cdf0e10cSrcweir         for( sal_uInt32 i=0; i < nCount; i++ )
559cdf0e10cSrcweir 		{
560cdf0e10cSrcweir 			rtl_getAppCommandArg( i, &aDummy.pData );
561cdf0e10cSrcweir 			if( aDummy.indexOf('-',0) != 0 )
562cdf0e10cSrcweir 			{
563cdf0e10cSrcweir                 bWaitBeforeClose = sal_True;
564cdf0e10cSrcweir 			}
565cdf0e10cSrcweir             if (!addArgument(&aArguments, ',', aDummy)) {
566cdf0e10cSrcweir                 return IPC_STATUS_BOOTSTRAP_ERROR;
567cdf0e10cSrcweir             }
568cdf0e10cSrcweir 		}
569cdf0e10cSrcweir 		// finaly, write the string onto the pipe
570cdf0e10cSrcweir 		pThread->maStreamPipe.write( aArguments.GetBuffer(), aArguments.Len() );
571cdf0e10cSrcweir 		pThread->maStreamPipe.write( "\0", 1 );
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 		// wait for confirmation #95361# #95425#
574cdf0e10cSrcweir 		ByteString aToken(sc_aConfirmationSequence);
575cdf0e10cSrcweir 		char *aReceiveBuffer = new char[aToken.Len()+1];
576cdf0e10cSrcweir 		int n = pThread->maStreamPipe.read( aReceiveBuffer, aToken.Len() );
577cdf0e10cSrcweir 		aReceiveBuffer[n]='\0';
578cdf0e10cSrcweir 
579cdf0e10cSrcweir 		delete pThread;
580cdf0e10cSrcweir 		if (aToken.CompareTo(aReceiveBuffer)!= COMPARE_EQUAL) {
581cdf0e10cSrcweir 			// something went wrong
582cdf0e10cSrcweir 			delete[] aReceiveBuffer;
583cdf0e10cSrcweir 			return IPC_STATUS_BOOTSTRAP_ERROR;
584cdf0e10cSrcweir 		} else {
585cdf0e10cSrcweir 			delete[] aReceiveBuffer;
586cdf0e10cSrcweir 			return IPC_STATUS_2ND_OFFICE;
587cdf0e10cSrcweir 		}
588cdf0e10cSrcweir 	}
589cdf0e10cSrcweir 
590cdf0e10cSrcweir 	return IPC_STATUS_OK;
591cdf0e10cSrcweir }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir void OfficeIPCThread::DisableOfficeIPCThread()
594cdf0e10cSrcweir {
595cdf0e10cSrcweir 	osl::ClearableMutexGuard aMutex( GetMutex() );
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 	if( pGlobalOfficeIPCThread )
598cdf0e10cSrcweir 	{
599cdf0e10cSrcweir         OfficeIPCThread *pOfficeIPCThread = pGlobalOfficeIPCThread;
600cdf0e10cSrcweir 		pGlobalOfficeIPCThread = 0;
601cdf0e10cSrcweir 
602cdf0e10cSrcweir 		// send thread a termination message
603cdf0e10cSrcweir 		// this is done so the subsequent join will not hang
604cdf0e10cSrcweir 		// because the thread hangs in accept of pipe
605cdf0e10cSrcweir         OPipe Pipe( pOfficeIPCThread->maPipeIdent, OPipe::TOption_Open, Security::get() );
606cdf0e10cSrcweir 		//Pipe.send( TERMINATION_SEQUENCE, TERMINATION_LENGTH );
607cdf0e10cSrcweir         if (Pipe.isValid())
608cdf0e10cSrcweir         {
609cdf0e10cSrcweir     		Pipe.send( sc_aTerminationSequence, sc_nTSeqLength+1 ); // also send 0-byte
610cdf0e10cSrcweir 
611cdf0e10cSrcweir 	    	// close the pipe so that the streampipe on the other
612cdf0e10cSrcweir     		// side produces EOF
613cdf0e10cSrcweir 	    	Pipe.close();
614cdf0e10cSrcweir         }
615cdf0e10cSrcweir 
616cdf0e10cSrcweir 		// release mutex to avoid deadlocks
617cdf0e10cSrcweir 		aMutex.clear();
618cdf0e10cSrcweir 
619cdf0e10cSrcweir         OfficeIPCThread::SetReady(pOfficeIPCThread);
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 		// exit gracefully and join
622cdf0e10cSrcweir 		pOfficeIPCThread->join();
623cdf0e10cSrcweir 		delete pOfficeIPCThread;
624cdf0e10cSrcweir 
625cdf0e10cSrcweir 
626cdf0e10cSrcweir 	}
627cdf0e10cSrcweir }
628cdf0e10cSrcweir 
629cdf0e10cSrcweir OfficeIPCThread::OfficeIPCThread() :
630cdf0e10cSrcweir 	mbDowning( false ),
631cdf0e10cSrcweir     mbRequestsEnabled( false ),
632cdf0e10cSrcweir 	mnPendingRequests( 0 ),
633cdf0e10cSrcweir 	mpDispatchWatcher( 0 )
634cdf0e10cSrcweir {
635cdf0e10cSrcweir }
636cdf0e10cSrcweir 
637cdf0e10cSrcweir OfficeIPCThread::~OfficeIPCThread()
638cdf0e10cSrcweir {
639cdf0e10cSrcweir 	::osl::ClearableMutexGuard	aGuard( GetMutex() );
640cdf0e10cSrcweir 
641cdf0e10cSrcweir 	if ( mpDispatchWatcher )
642cdf0e10cSrcweir 		mpDispatchWatcher->release();
643cdf0e10cSrcweir 	maPipe.close();
644cdf0e10cSrcweir 	maStreamPipe.close();
645cdf0e10cSrcweir 	pGlobalOfficeIPCThread = 0;
646cdf0e10cSrcweir }
647cdf0e10cSrcweir 
648cdf0e10cSrcweir static void AddURLToStringList( const rtl::OUString& aURL, rtl::OUString& aStringList )
649cdf0e10cSrcweir {
650cdf0e10cSrcweir 	if ( aStringList.getLength() )
651cdf0e10cSrcweir 		aStringList += ::rtl::OUString::valueOf( (sal_Unicode)APPEVENT_PARAM_DELIMITER );
652cdf0e10cSrcweir 	aStringList += aURL;
653cdf0e10cSrcweir }
654cdf0e10cSrcweir 
655cdf0e10cSrcweir void OfficeIPCThread::SetReady(OfficeIPCThread* pThread)
656cdf0e10cSrcweir {
657cdf0e10cSrcweir     if (pThread == NULL) pThread = pGlobalOfficeIPCThread;
658cdf0e10cSrcweir     if (pThread != NULL)
659cdf0e10cSrcweir     {
660cdf0e10cSrcweir         pThread->cReady.set();
661cdf0e10cSrcweir     }
662cdf0e10cSrcweir }
663cdf0e10cSrcweir 
664cdf0e10cSrcweir void SAL_CALL OfficeIPCThread::run()
665cdf0e10cSrcweir {
666cdf0e10cSrcweir     do
667cdf0e10cSrcweir 	{
668cdf0e10cSrcweir         OPipe::TPipeError
669cdf0e10cSrcweir 			nError = maPipe.accept( maStreamPipe );
670cdf0e10cSrcweir 
671cdf0e10cSrcweir 
672cdf0e10cSrcweir 		if( nError == OStreamPipe::E_None )
673cdf0e10cSrcweir 		{
674cdf0e10cSrcweir 
675cdf0e10cSrcweir             // #111143# and others:
676cdf0e10cSrcweir             // if we receive a request while the office is displaying some dialog or error during
677cdf0e10cSrcweir             // bootstrap, that dialogs event loop might get events that are dispatched by this thread
678cdf0e10cSrcweir             // we have to wait for cReady to be set by the real main loop.
679cdf0e10cSrcweir             // only reqests that dont dispatch events may be processed before cReady is set.
680cdf0e10cSrcweir             cReady.wait();
681cdf0e10cSrcweir 
682cdf0e10cSrcweir             // we might have decided to shutdown while we were sleeping
683cdf0e10cSrcweir             if (!pGlobalOfficeIPCThread) return;
684cdf0e10cSrcweir 
685cdf0e10cSrcweir             // only lock the mutex when processing starts, othewise we deadlock when the office goes
686cdf0e10cSrcweir             // down during wait
687cdf0e10cSrcweir             osl::ClearableMutexGuard aGuard( GetMutex() );
688cdf0e10cSrcweir 
689cdf0e10cSrcweir             ByteString aArguments;
690cdf0e10cSrcweir             // test byte by byte
691cdf0e10cSrcweir             const int nBufSz = 2048;
692cdf0e10cSrcweir             char pBuf[nBufSz];
693cdf0e10cSrcweir             int nBytes = 0;
694cdf0e10cSrcweir             int nResult = 0;
695cdf0e10cSrcweir             // read into pBuf until '\0' is read or read-error
696cdf0e10cSrcweir             while ((nResult=maStreamPipe.recv( pBuf+nBytes, nBufSz-nBytes))>0) {
697cdf0e10cSrcweir 				nBytes += nResult;
698cdf0e10cSrcweir 				if (pBuf[nBytes-1]=='\0') {
699cdf0e10cSrcweir 					aArguments += pBuf;
700cdf0e10cSrcweir 			        break;
701cdf0e10cSrcweir 		        }
702cdf0e10cSrcweir 	        }
703cdf0e10cSrcweir 			// don't close pipe ...
704cdf0e10cSrcweir 
705cdf0e10cSrcweir 			// #90717# Is this a lookup message from another application? if so, ignore
706cdf0e10cSrcweir 			if ( aArguments.Len() == 0 )
707cdf0e10cSrcweir 				continue;
708cdf0e10cSrcweir 
709cdf0e10cSrcweir             // is this a termination message ? if so, terminate
710cdf0e10cSrcweir             if(( aArguments.CompareTo( sc_aTerminationSequence, sc_nTSeqLength ) == COMPARE_EQUAL ) ||
711cdf0e10cSrcweir                     mbDowning ) return;
712cdf0e10cSrcweir             String           aEmpty;
713cdf0e10cSrcweir             std::auto_ptr< CommandLineArgs > aCmdLineArgs;
714cdf0e10cSrcweir             try
715cdf0e10cSrcweir             {
716cdf0e10cSrcweir                 Parser p( aArguments );
717cdf0e10cSrcweir                 aCmdLineArgs.reset( new CommandLineArgs( p ) );
718cdf0e10cSrcweir             }
719cdf0e10cSrcweir             catch ( CommandLineArgs::Supplier::Exception & )
720cdf0e10cSrcweir             {
721cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
722cdf0e10cSrcweir                 fprintf( stderr, "Error in received command line arguments\n" );
723cdf0e10cSrcweir #endif
724cdf0e10cSrcweir                 continue;
725cdf0e10cSrcweir             }
726cdf0e10cSrcweir             CommandLineArgs	*pCurrentCmdLineArgs = Desktop::GetCommandLineArgs();
727cdf0e10cSrcweir 
728cdf0e10cSrcweir 			if ( aCmdLineArgs->IsQuickstart() )
729cdf0e10cSrcweir 			{
730cdf0e10cSrcweir 				// we have to use application event, because we have to start quickstart service in main thread!!
731cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
732cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty,
733cdf0e10cSrcweir 											"QUICKSTART", aEmpty );
734cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
735cdf0e10cSrcweir 			}
736cdf0e10cSrcweir 
737cdf0e10cSrcweir 			// handle request for acceptor
738cdf0e10cSrcweir 			sal_Bool bAcceptorRequest = sal_False;
739cdf0e10cSrcweir 			OUString aAcceptString;
740cdf0e10cSrcweir             if ( aCmdLineArgs->GetAcceptString(aAcceptString) && Desktop::CheckOEM()) {
741cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
742cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty,
743cdf0e10cSrcweir 										  "ACCEPT", aAcceptString );
744cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
745cdf0e10cSrcweir 				bAcceptorRequest = sal_True;
746cdf0e10cSrcweir 			}
747cdf0e10cSrcweir 			// handle acceptor removal
748cdf0e10cSrcweir 			OUString aUnAcceptString;
749cdf0e10cSrcweir 			if ( aCmdLineArgs->GetUnAcceptString(aUnAcceptString) ) {
750cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
751cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty,
752cdf0e10cSrcweir 										 "UNACCEPT", aUnAcceptString );
753cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
754cdf0e10cSrcweir 				bAcceptorRequest = sal_True;
755cdf0e10cSrcweir 			}
756cdf0e10cSrcweir 
757cdf0e10cSrcweir #ifndef UNX
758cdf0e10cSrcweir 			// only in non-unix version, we need to handle a -help request
759cdf0e10cSrcweir 			// in a running instance in order to display  the command line help
760cdf0e10cSrcweir 			if ( aCmdLineArgs->IsHelp() ) {
761cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
762cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty, "HELP", aEmpty );
763cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
764cdf0e10cSrcweir 			}
765cdf0e10cSrcweir #endif
766cdf0e10cSrcweir 
767cdf0e10cSrcweir 			sal_Bool bDocRequestSent = sal_False;
768cdf0e10cSrcweir 			ProcessDocumentsRequest* pRequest = new ProcessDocumentsRequest(
769cdf0e10cSrcweir                 aCmdLineArgs->getCwdUrl());
770cdf0e10cSrcweir             cProcessed.reset();
771cdf0e10cSrcweir             pRequest->pcProcessed = &cProcessed;
772cdf0e10cSrcweir 
773cdf0e10cSrcweir 			// Print requests are not dependent on the -invisible cmdline argument as they are
774cdf0e10cSrcweir 			// loaded with the "hidden" flag! So they are always checked.
775cdf0e10cSrcweir 			bDocRequestSent |= aCmdLineArgs->GetPrintList( pRequest->aPrintList );
776cdf0e10cSrcweir 			bDocRequestSent |= ( aCmdLineArgs->GetPrintToList( pRequest->aPrintToList ) &&
777cdf0e10cSrcweir 									aCmdLineArgs->GetPrinterName( pRequest->aPrinterName )		);
778cdf0e10cSrcweir 
779cdf0e10cSrcweir 			if ( !pCurrentCmdLineArgs->IsInvisible() )
780cdf0e10cSrcweir 			{
781cdf0e10cSrcweir 				// Read cmdline args that can open/create documents. As they would open a window
782cdf0e10cSrcweir 				// they are only allowed if the "-invisible" is currently not used!
783cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetOpenList( pRequest->aOpenList );
784cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetViewList( pRequest->aViewList );
785cdf0e10cSrcweir                 bDocRequestSent |= aCmdLineArgs->GetStartList( pRequest->aStartList );
786cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetForceOpenList( pRequest->aForceOpenList );
787cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetForceNewList( pRequest->aForceNewList );
788cdf0e10cSrcweir 
789cdf0e10cSrcweir 				// Special command line args to create an empty document for a given module
790cdf0e10cSrcweir 
791cdf0e10cSrcweir                 // #i18338# (lo)
792cdf0e10cSrcweir                 // we only do this if no document was specified on the command line,
793cdf0e10cSrcweir                 // since this would be inconsistent with the the behaviour of
794cdf0e10cSrcweir                 // the first process, see OpenClients() (call to OpenDefault()) in app.cxx
795cdf0e10cSrcweir                 if ( aCmdLineArgs->HasModuleParam() && Desktop::CheckOEM() && (!bDocRequestSent))
796cdf0e10cSrcweir 				{
797cdf0e10cSrcweir 					SvtModuleOptions aOpt;
798cdf0e10cSrcweir 					SvtModuleOptions::EFactory eFactory = SvtModuleOptions::E_WRITER;
799cdf0e10cSrcweir 					if ( aCmdLineArgs->IsWriter() )
800cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_WRITER;
801cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsCalc() )
802cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_CALC;
803cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsDraw() )
804cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_DRAW;
805cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsImpress() )
806cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_IMPRESS;
807cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsBase() )
808cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_DATABASE;
809cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsMath() )
810cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_MATH;
811cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsGlobal() )
812cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_WRITERGLOBAL;
813cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsWeb() )
814cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_WRITERWEB;
815cdf0e10cSrcweir 
816cdf0e10cSrcweir                     if ( pRequest->aOpenList.getLength() )
817cdf0e10cSrcweir                         pRequest->aModule = aOpt.GetFactoryName( eFactory );
818cdf0e10cSrcweir                     else
819cdf0e10cSrcweir                         AddURLToStringList( aOpt.GetFactoryEmptyDocumentURL( eFactory ), pRequest->aOpenList );
820cdf0e10cSrcweir 					bDocRequestSent = sal_True;
821cdf0e10cSrcweir 				}
822cdf0e10cSrcweir             }
823cdf0e10cSrcweir 
824cdf0e10cSrcweir             if (!aCmdLineArgs->IsQuickstart() && Desktop::CheckOEM()) {
825cdf0e10cSrcweir                 sal_Bool bShowHelp = sal_False;
826cdf0e10cSrcweir                 rtl::OUStringBuffer aHelpURLBuffer;
827cdf0e10cSrcweir                 if (aCmdLineArgs->IsHelpWriter()) {
828cdf0e10cSrcweir                     bShowHelp = sal_True;
829cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://swriter/start");
830cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpCalc()) {
831cdf0e10cSrcweir                     bShowHelp = sal_True;
832cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://scalc/start");
833cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpDraw()) {
834cdf0e10cSrcweir                     bShowHelp = sal_True;
835cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://sdraw/start");
836cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpImpress()) {
837cdf0e10cSrcweir                     bShowHelp = sal_True;
838cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://simpress/start");
839cdf0e10cSrcweir 				} else if (aCmdLineArgs->IsHelpBase()) {
840cdf0e10cSrcweir                     bShowHelp = sal_True;
841cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://sdatabase/start");
842cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpBasic()) {
843cdf0e10cSrcweir                     bShowHelp = sal_True;
844cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://sbasic/start");
845cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpMath()) {
846cdf0e10cSrcweir                     bShowHelp = sal_True;
847cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://smath/start");
848cdf0e10cSrcweir                 }
849cdf0e10cSrcweir                 if (bShowHelp) {
850cdf0e10cSrcweir                     Any aRet = ::utl::ConfigManager::GetDirectConfigProperty( ::utl::ConfigManager::LOCALE );
851cdf0e10cSrcweir                     rtl::OUString aTmp;
852cdf0e10cSrcweir                     aRet >>= aTmp;
853cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("?Language=");
854cdf0e10cSrcweir                     aHelpURLBuffer.append(aTmp);
855cdf0e10cSrcweir #if defined UNX
856cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("&System=UNX");
857cdf0e10cSrcweir #elif defined WNT
858cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("&System=WIN");
859cdf0e10cSrcweir #elif defined OS2
860cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("&System=OS2");
861cdf0e10cSrcweir #endif
862cdf0e10cSrcweir                     ApplicationEvent* pAppEvent =
863cdf0e10cSrcweir                         new ApplicationEvent( aEmpty, aEmpty,
864cdf0e10cSrcweir                                               "OPENHELPURL", aHelpURLBuffer.makeStringAndClear());
865cdf0e10cSrcweir                     ImplPostForeignAppEvent( pAppEvent );
866cdf0e10cSrcweir                 }
867cdf0e10cSrcweir             }
868cdf0e10cSrcweir 
869cdf0e10cSrcweir             if ( bDocRequestSent && Desktop::CheckOEM())
870cdf0e10cSrcweir  			{
871cdf0e10cSrcweir 				// Send requests to dispatch watcher if we have at least one. The receiver
872cdf0e10cSrcweir 				// is responsible to delete the request after processing it.
873cdf0e10cSrcweir                 if ( aCmdLineArgs->HasModuleParam() )
874cdf0e10cSrcweir                 {
875cdf0e10cSrcweir                     SvtModuleOptions    aOpt;
876cdf0e10cSrcweir 
877cdf0e10cSrcweir                     // Support command line parameters to start a module (as preselection)
878cdf0e10cSrcweir                     if ( aCmdLineArgs->IsWriter() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SWRITER ) )
879cdf0e10cSrcweir                         pRequest->aModule = aOpt.GetFactoryName( SvtModuleOptions::E_WRITER );
880cdf0e10cSrcweir                     else if ( aCmdLineArgs->IsCalc() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SCALC ) )
881cdf0e10cSrcweir                         pRequest->aModule = aOpt.GetFactoryName( SvtModuleOptions::E_CALC );
882cdf0e10cSrcweir                     else if ( aCmdLineArgs->IsImpress() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SIMPRESS ) )
883cdf0e10cSrcweir                         pRequest->aModule= aOpt.GetFactoryName( SvtModuleOptions::E_IMPRESS );
884cdf0e10cSrcweir                     else if ( aCmdLineArgs->IsDraw() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SDRAW ) )
885cdf0e10cSrcweir                         pRequest->aModule= aOpt.GetFactoryName( SvtModuleOptions::E_DRAW );
886cdf0e10cSrcweir                 }
887cdf0e10cSrcweir 
888cdf0e10cSrcweir 
889cdf0e10cSrcweir 				ImplPostProcessDocumentsEvent( pRequest );
890cdf0e10cSrcweir 			}
891cdf0e10cSrcweir 			else
892cdf0e10cSrcweir 			{
893cdf0e10cSrcweir 				// delete not used request again
894cdf0e10cSrcweir 				delete pRequest;
895cdf0e10cSrcweir 				pRequest = NULL;
896cdf0e10cSrcweir 			}
897cdf0e10cSrcweir 			if (( aArguments.CompareTo( sc_aShowSequence, sc_nShSeqLength ) == COMPARE_EQUAL ) ||
898cdf0e10cSrcweir 				aCmdLineArgs->IsEmpty() )
899cdf0e10cSrcweir 			{
900cdf0e10cSrcweir 				// no document was sent, just bring Office to front
901cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
902cdf0e10cSrcweir 						new ApplicationEvent( aEmpty, aEmpty, "APPEAR", aEmpty );
903cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
904cdf0e10cSrcweir 			}
905cdf0e10cSrcweir 
906cdf0e10cSrcweir 			// we don't need the mutex any longer...
907cdf0e10cSrcweir 			aGuard.clear();
908cdf0e10cSrcweir 			// wait for processing to finish
909cdf0e10cSrcweir             if (bDocRequestSent)
910cdf0e10cSrcweir     			cProcessed.wait();
911cdf0e10cSrcweir 			// processing finished, inform the requesting end
912cdf0e10cSrcweir 			nBytes = 0;
913cdf0e10cSrcweir 			while (
914cdf0e10cSrcweir                    (nResult = maStreamPipe.send(sc_aConfirmationSequence+nBytes, sc_nCSeqLength-nBytes))>0 &&
915cdf0e10cSrcweir                    ((nBytes += nResult) < sc_nCSeqLength) ) ;
916cdf0e10cSrcweir 			// now we can close, don't we?
917cdf0e10cSrcweir 			// maStreamPipe.close();
918cdf0e10cSrcweir 
919cdf0e10cSrcweir         }
920cdf0e10cSrcweir         else
921cdf0e10cSrcweir         {
922cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
923cdf0e10cSrcweir 			fprintf( stderr, "Error on accept: %d\n", (int)nError );
924cdf0e10cSrcweir #endif
925cdf0e10cSrcweir 			TimeValue tval;
926cdf0e10cSrcweir 			tval.Seconds = 1;
927cdf0e10cSrcweir 			tval.Nanosec = 0;
928cdf0e10cSrcweir 			sleep( tval );
929cdf0e10cSrcweir 		}
930cdf0e10cSrcweir 	} while( schedule() );
931cdf0e10cSrcweir }
932cdf0e10cSrcweir 
933cdf0e10cSrcweir static void AddToDispatchList(
934cdf0e10cSrcweir 	DispatchWatcher::DispatchList& rDispatchList,
935cdf0e10cSrcweir     boost::optional< rtl::OUString > const & cwdUrl,
936cdf0e10cSrcweir 	const OUString& aRequestList,
937cdf0e10cSrcweir 	DispatchWatcher::RequestType nType,
938cdf0e10cSrcweir     const OUString& aParam,
939cdf0e10cSrcweir     const OUString& aFactory )
940cdf0e10cSrcweir {
941cdf0e10cSrcweir 	if ( aRequestList.getLength() > 0 )
942cdf0e10cSrcweir 	{
943cdf0e10cSrcweir 		sal_Int32 nIndex = 0;
944cdf0e10cSrcweir 		do
945cdf0e10cSrcweir 		{
946cdf0e10cSrcweir 			OUString aToken = aRequestList.getToken( 0, APPEVENT_PARAM_DELIMITER, nIndex );
947cdf0e10cSrcweir 			if ( aToken.getLength() > 0 )
948cdf0e10cSrcweir 				rDispatchList.push_back(
949cdf0e10cSrcweir                     DispatchWatcher::DispatchRequest( nType, aToken, cwdUrl, aParam, aFactory ));
950cdf0e10cSrcweir 		}
951cdf0e10cSrcweir 		while ( nIndex >= 0 );
952cdf0e10cSrcweir 	}
953cdf0e10cSrcweir }
954cdf0e10cSrcweir 
955cdf0e10cSrcweir sal_Bool OfficeIPCThread::ExecuteCmdLineRequests( ProcessDocumentsRequest& aRequest )
956cdf0e10cSrcweir {
957cdf0e10cSrcweir     // protect the dispatch list
958cdf0e10cSrcweir     osl::ClearableMutexGuard aGuard( GetMutex() );
959cdf0e10cSrcweir 
960cdf0e10cSrcweir 	static DispatchWatcher::DispatchList	aDispatchList;
961cdf0e10cSrcweir 
962cdf0e10cSrcweir 	rtl::OUString aEmpty;
963cdf0e10cSrcweir 	// Create dispatch list for dispatch watcher
964cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aOpenList, DispatchWatcher::REQUEST_OPEN, aEmpty, aRequest.aModule );
965cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aViewList, DispatchWatcher::REQUEST_VIEW, aEmpty, aRequest.aModule );
966cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aStartList, DispatchWatcher::REQUEST_START, aEmpty, aRequest.aModule );
967cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aPrintList, DispatchWatcher::REQUEST_PRINT, aEmpty, aRequest.aModule );
968cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aPrintToList, DispatchWatcher::REQUEST_PRINTTO, aRequest.aPrinterName, aRequest.aModule );
969cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aForceOpenList, DispatchWatcher::REQUEST_FORCEOPEN, aEmpty, aRequest.aModule );
970cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aForceNewList, DispatchWatcher::REQUEST_FORCENEW, aEmpty, aRequest.aModule );
971cdf0e10cSrcweir 
972cdf0e10cSrcweir 	sal_Bool bShutdown( sal_False );
973cdf0e10cSrcweir 
974cdf0e10cSrcweir 	if ( pGlobalOfficeIPCThread )
975cdf0e10cSrcweir 	{
976cdf0e10cSrcweir         if( ! pGlobalOfficeIPCThread->AreRequestsEnabled() )
977cdf0e10cSrcweir             return bShutdown;
978cdf0e10cSrcweir 
979cdf0e10cSrcweir 		pGlobalOfficeIPCThread->mnPendingRequests += aDispatchList.size();
980cdf0e10cSrcweir 		if ( !pGlobalOfficeIPCThread->mpDispatchWatcher )
981cdf0e10cSrcweir 		{
982cdf0e10cSrcweir 			pGlobalOfficeIPCThread->mpDispatchWatcher = DispatchWatcher::GetDispatchWatcher();
983cdf0e10cSrcweir 			pGlobalOfficeIPCThread->mpDispatchWatcher->acquire();
984cdf0e10cSrcweir 		}
985cdf0e10cSrcweir 
986cdf0e10cSrcweir         // copy for execute
987cdf0e10cSrcweir         DispatchWatcher::DispatchList aTempList( aDispatchList );
988cdf0e10cSrcweir         aDispatchList.clear();
989cdf0e10cSrcweir 
990cdf0e10cSrcweir 		aGuard.clear();
991cdf0e10cSrcweir 
992cdf0e10cSrcweir 		// Execute dispatch requests
993cdf0e10cSrcweir 		bShutdown = pGlobalOfficeIPCThread->mpDispatchWatcher->executeDispatchRequests( aTempList, s_bInEnableRequests );
994cdf0e10cSrcweir 
995cdf0e10cSrcweir 		// set processed flag
996cdf0e10cSrcweir 		if (aRequest.pcProcessed != NULL)
997cdf0e10cSrcweir 			aRequest.pcProcessed->set();
998cdf0e10cSrcweir 	}
999cdf0e10cSrcweir 
1000cdf0e10cSrcweir 	return bShutdown;
1001cdf0e10cSrcweir }
1002cdf0e10cSrcweir 
1003cdf0e10cSrcweir }
1004