1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2722ceddSAndrew Rist  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2722ceddSAndrew Rist  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19*2722ceddSAndrew Rist  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew 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();
499cdf0e10cSrcweir 		// Try to create pipe
500cdf0e10cSrcweir 		if ( pThread->maPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Create, rSecurity ))
501cdf0e10cSrcweir 		{
502cdf0e10cSrcweir 			// Pipe created
503cdf0e10cSrcweir 			nPipeMode = PIPEMODE_CREATED;
504cdf0e10cSrcweir 		}
505cdf0e10cSrcweir 		else if( pThread->maPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Open, rSecurity )) // Creation not successfull, now we try to connect
506cdf0e10cSrcweir 		{
507cdf0e10cSrcweir 			// Pipe connected to first office
508cdf0e10cSrcweir 			nPipeMode = PIPEMODE_CONNECTED;
509cdf0e10cSrcweir 		}
510cdf0e10cSrcweir 		else
511cdf0e10cSrcweir 		{
512cdf0e10cSrcweir 			OPipe::TPipeError eReason = pThread->maPipe.getError();
513cdf0e10cSrcweir 			if ((eReason == OPipe::E_ConnectionRefused) || (eReason == OPipe::E_invalidError))
514cdf0e10cSrcweir 				return IPC_STATUS_BOOTSTRAP_ERROR;
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 			// Wait for second office to be ready
517cdf0e10cSrcweir 			TimeValue aTimeValue;
518cdf0e10cSrcweir 			aTimeValue.Seconds = 0;
519cdf0e10cSrcweir 			aTimeValue.Nanosec = 10000000; // 10ms
520cdf0e10cSrcweir 			osl::Thread::wait( aTimeValue );
521cdf0e10cSrcweir 		}
522cdf0e10cSrcweir 
523cdf0e10cSrcweir 	} while ( nPipeMode == PIPEMODE_DONTKNOW );
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 	if ( nPipeMode == PIPEMODE_CREATED )
526cdf0e10cSrcweir 	{
527cdf0e10cSrcweir 		// Seems we are the one and only, so start listening thread
528cdf0e10cSrcweir 		pGlobalOfficeIPCThread = pThread;
529cdf0e10cSrcweir 		pThread->create(); // starts thread
530cdf0e10cSrcweir 	}
531cdf0e10cSrcweir 	else
532cdf0e10cSrcweir 	{
533cdf0e10cSrcweir 		// Seems another office is running. Pipe arguments to it and self terminate
534cdf0e10cSrcweir 		pThread->maStreamPipe = pThread->maPipe;
535cdf0e10cSrcweir 
536cdf0e10cSrcweir 		sal_Bool bWaitBeforeClose = sal_False;
537cdf0e10cSrcweir 		ByteString aArguments(RTL_CONSTASCII_STRINGPARAM(ARGUMENT_PREFIX));
538cdf0e10cSrcweir         rtl::OUString cwdUrl;
539cdf0e10cSrcweir         if (!(tools::getProcessWorkingDir(&cwdUrl) &&
540cdf0e10cSrcweir               addArgument(&aArguments, '1', cwdUrl)))
541cdf0e10cSrcweir         {
542cdf0e10cSrcweir             aArguments += '0';
543cdf0e10cSrcweir         }
544cdf0e10cSrcweir         sal_uInt32 nCount = rtl_getAppCommandArgCount();
545cdf0e10cSrcweir         for( sal_uInt32 i=0; i < nCount; i++ )
546cdf0e10cSrcweir 		{
547cdf0e10cSrcweir 			rtl_getAppCommandArg( i, &aDummy.pData );
548cdf0e10cSrcweir 			if( aDummy.indexOf('-',0) != 0 )
549cdf0e10cSrcweir 			{
550cdf0e10cSrcweir                 bWaitBeforeClose = sal_True;
551cdf0e10cSrcweir 			}
552cdf0e10cSrcweir             if (!addArgument(&aArguments, ',', aDummy)) {
553cdf0e10cSrcweir                 return IPC_STATUS_BOOTSTRAP_ERROR;
554cdf0e10cSrcweir             }
555cdf0e10cSrcweir 		}
556cdf0e10cSrcweir 		// finaly, write the string onto the pipe
557cdf0e10cSrcweir 		pThread->maStreamPipe.write( aArguments.GetBuffer(), aArguments.Len() );
558cdf0e10cSrcweir 		pThread->maStreamPipe.write( "\0", 1 );
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 		// wait for confirmation #95361# #95425#
561cdf0e10cSrcweir 		ByteString aToken(sc_aConfirmationSequence);
562cdf0e10cSrcweir 		char *aReceiveBuffer = new char[aToken.Len()+1];
563cdf0e10cSrcweir 		int n = pThread->maStreamPipe.read( aReceiveBuffer, aToken.Len() );
564cdf0e10cSrcweir 		aReceiveBuffer[n]='\0';
565cdf0e10cSrcweir 
566cdf0e10cSrcweir 		delete pThread;
567cdf0e10cSrcweir 		if (aToken.CompareTo(aReceiveBuffer)!= COMPARE_EQUAL) {
568cdf0e10cSrcweir 			// something went wrong
569cdf0e10cSrcweir 			delete[] aReceiveBuffer;
570cdf0e10cSrcweir 			return IPC_STATUS_BOOTSTRAP_ERROR;
571cdf0e10cSrcweir 		} else {
572cdf0e10cSrcweir 			delete[] aReceiveBuffer;
573cdf0e10cSrcweir 			return IPC_STATUS_2ND_OFFICE;
574cdf0e10cSrcweir 		}
575cdf0e10cSrcweir 	}
576cdf0e10cSrcweir 
577cdf0e10cSrcweir 	return IPC_STATUS_OK;
578cdf0e10cSrcweir }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir void OfficeIPCThread::DisableOfficeIPCThread()
581cdf0e10cSrcweir {
582cdf0e10cSrcweir 	osl::ClearableMutexGuard aMutex( GetMutex() );
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 	if( pGlobalOfficeIPCThread )
585cdf0e10cSrcweir 	{
586cdf0e10cSrcweir         OfficeIPCThread *pOfficeIPCThread = pGlobalOfficeIPCThread;
587cdf0e10cSrcweir 		pGlobalOfficeIPCThread = 0;
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 		// send thread a termination message
590cdf0e10cSrcweir 		// this is done so the subsequent join will not hang
591cdf0e10cSrcweir 		// because the thread hangs in accept of pipe
592cdf0e10cSrcweir         OPipe Pipe( pOfficeIPCThread->maPipeIdent, OPipe::TOption_Open, Security::get() );
593cdf0e10cSrcweir 		//Pipe.send( TERMINATION_SEQUENCE, TERMINATION_LENGTH );
594cdf0e10cSrcweir         if (Pipe.isValid())
595cdf0e10cSrcweir         {
596cdf0e10cSrcweir     		Pipe.send( sc_aTerminationSequence, sc_nTSeqLength+1 ); // also send 0-byte
597cdf0e10cSrcweir 
598cdf0e10cSrcweir 	    	// close the pipe so that the streampipe on the other
599cdf0e10cSrcweir     		// side produces EOF
600cdf0e10cSrcweir 	    	Pipe.close();
601cdf0e10cSrcweir         }
602cdf0e10cSrcweir 
603cdf0e10cSrcweir 		// release mutex to avoid deadlocks
604cdf0e10cSrcweir 		aMutex.clear();
605cdf0e10cSrcweir 
606cdf0e10cSrcweir         OfficeIPCThread::SetReady(pOfficeIPCThread);
607cdf0e10cSrcweir 
608cdf0e10cSrcweir 		// exit gracefully and join
609cdf0e10cSrcweir 		pOfficeIPCThread->join();
610cdf0e10cSrcweir 		delete pOfficeIPCThread;
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 
613cdf0e10cSrcweir 	}
614cdf0e10cSrcweir }
615cdf0e10cSrcweir 
616cdf0e10cSrcweir OfficeIPCThread::OfficeIPCThread() :
617cdf0e10cSrcweir 	mbDowning( false ),
618cdf0e10cSrcweir     mbRequestsEnabled( false ),
619cdf0e10cSrcweir 	mnPendingRequests( 0 ),
620cdf0e10cSrcweir 	mpDispatchWatcher( 0 )
621cdf0e10cSrcweir {
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
624cdf0e10cSrcweir OfficeIPCThread::~OfficeIPCThread()
625cdf0e10cSrcweir {
626cdf0e10cSrcweir 	::osl::ClearableMutexGuard	aGuard( GetMutex() );
627cdf0e10cSrcweir 
628cdf0e10cSrcweir 	if ( mpDispatchWatcher )
629cdf0e10cSrcweir 		mpDispatchWatcher->release();
630cdf0e10cSrcweir 	maPipe.close();
631cdf0e10cSrcweir 	maStreamPipe.close();
632cdf0e10cSrcweir 	pGlobalOfficeIPCThread = 0;
633cdf0e10cSrcweir }
634cdf0e10cSrcweir 
635cdf0e10cSrcweir static void AddURLToStringList( const rtl::OUString& aURL, rtl::OUString& aStringList )
636cdf0e10cSrcweir {
637cdf0e10cSrcweir 	if ( aStringList.getLength() )
638cdf0e10cSrcweir 		aStringList += ::rtl::OUString::valueOf( (sal_Unicode)APPEVENT_PARAM_DELIMITER );
639cdf0e10cSrcweir 	aStringList += aURL;
640cdf0e10cSrcweir }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir void OfficeIPCThread::SetReady(OfficeIPCThread* pThread)
643cdf0e10cSrcweir {
644cdf0e10cSrcweir     if (pThread == NULL) pThread = pGlobalOfficeIPCThread;
645cdf0e10cSrcweir     if (pThread != NULL)
646cdf0e10cSrcweir     {
647cdf0e10cSrcweir         pThread->cReady.set();
648cdf0e10cSrcweir     }
649cdf0e10cSrcweir }
650cdf0e10cSrcweir 
651cdf0e10cSrcweir void SAL_CALL OfficeIPCThread::run()
652cdf0e10cSrcweir {
653cdf0e10cSrcweir     do
654cdf0e10cSrcweir 	{
655cdf0e10cSrcweir         OPipe::TPipeError
656cdf0e10cSrcweir 			nError = maPipe.accept( maStreamPipe );
657cdf0e10cSrcweir 
658cdf0e10cSrcweir 
659cdf0e10cSrcweir 		if( nError == OStreamPipe::E_None )
660cdf0e10cSrcweir 		{
661cdf0e10cSrcweir 
662cdf0e10cSrcweir             // #111143# and others:
663cdf0e10cSrcweir             // if we receive a request while the office is displaying some dialog or error during
664cdf0e10cSrcweir             // bootstrap, that dialogs event loop might get events that are dispatched by this thread
665cdf0e10cSrcweir             // we have to wait for cReady to be set by the real main loop.
666cdf0e10cSrcweir             // only reqests that dont dispatch events may be processed before cReady is set.
667cdf0e10cSrcweir             cReady.wait();
668cdf0e10cSrcweir 
669cdf0e10cSrcweir             // we might have decided to shutdown while we were sleeping
670cdf0e10cSrcweir             if (!pGlobalOfficeIPCThread) return;
671cdf0e10cSrcweir 
672cdf0e10cSrcweir             // only lock the mutex when processing starts, othewise we deadlock when the office goes
673cdf0e10cSrcweir             // down during wait
674cdf0e10cSrcweir             osl::ClearableMutexGuard aGuard( GetMutex() );
675cdf0e10cSrcweir 
676cdf0e10cSrcweir             ByteString aArguments;
677cdf0e10cSrcweir             // test byte by byte
678cdf0e10cSrcweir             const int nBufSz = 2048;
679cdf0e10cSrcweir             char pBuf[nBufSz];
680cdf0e10cSrcweir             int nBytes = 0;
681cdf0e10cSrcweir             int nResult = 0;
682cdf0e10cSrcweir             // read into pBuf until '\0' is read or read-error
683cdf0e10cSrcweir             while ((nResult=maStreamPipe.recv( pBuf+nBytes, nBufSz-nBytes))>0) {
684cdf0e10cSrcweir 				nBytes += nResult;
685cdf0e10cSrcweir 				if (pBuf[nBytes-1]=='\0') {
686cdf0e10cSrcweir 					aArguments += pBuf;
687cdf0e10cSrcweir 			        break;
688cdf0e10cSrcweir 		        }
689cdf0e10cSrcweir 	        }
690cdf0e10cSrcweir 			// don't close pipe ...
691cdf0e10cSrcweir 
692cdf0e10cSrcweir 			// #90717# Is this a lookup message from another application? if so, ignore
693cdf0e10cSrcweir 			if ( aArguments.Len() == 0 )
694cdf0e10cSrcweir 				continue;
695cdf0e10cSrcweir 
696cdf0e10cSrcweir             // is this a termination message ? if so, terminate
697cdf0e10cSrcweir             if(( aArguments.CompareTo( sc_aTerminationSequence, sc_nTSeqLength ) == COMPARE_EQUAL ) ||
698cdf0e10cSrcweir                     mbDowning ) return;
699cdf0e10cSrcweir             String           aEmpty;
700cdf0e10cSrcweir             std::auto_ptr< CommandLineArgs > aCmdLineArgs;
701cdf0e10cSrcweir             try
702cdf0e10cSrcweir             {
703cdf0e10cSrcweir                 Parser p( aArguments );
704cdf0e10cSrcweir                 aCmdLineArgs.reset( new CommandLineArgs( p ) );
705cdf0e10cSrcweir             }
706cdf0e10cSrcweir             catch ( CommandLineArgs::Supplier::Exception & )
707cdf0e10cSrcweir             {
708cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
709cdf0e10cSrcweir                 fprintf( stderr, "Error in received command line arguments\n" );
710cdf0e10cSrcweir #endif
711cdf0e10cSrcweir                 continue;
712cdf0e10cSrcweir             }
713cdf0e10cSrcweir             CommandLineArgs	*pCurrentCmdLineArgs = Desktop::GetCommandLineArgs();
714cdf0e10cSrcweir 
715cdf0e10cSrcweir 			if ( aCmdLineArgs->IsQuickstart() )
716cdf0e10cSrcweir 			{
717cdf0e10cSrcweir 				// we have to use application event, because we have to start quickstart service in main thread!!
718cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
719cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty,
720cdf0e10cSrcweir 											"QUICKSTART", aEmpty );
721cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
722cdf0e10cSrcweir 			}
723cdf0e10cSrcweir 
724cdf0e10cSrcweir 			// handle request for acceptor
725cdf0e10cSrcweir 			sal_Bool bAcceptorRequest = sal_False;
726cdf0e10cSrcweir 			OUString aAcceptString;
727cdf0e10cSrcweir             if ( aCmdLineArgs->GetAcceptString(aAcceptString) && Desktop::CheckOEM()) {
728cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
729cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty,
730cdf0e10cSrcweir 										  "ACCEPT", aAcceptString );
731cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
732cdf0e10cSrcweir 				bAcceptorRequest = sal_True;
733cdf0e10cSrcweir 			}
734cdf0e10cSrcweir 			// handle acceptor removal
735cdf0e10cSrcweir 			OUString aUnAcceptString;
736cdf0e10cSrcweir 			if ( aCmdLineArgs->GetUnAcceptString(aUnAcceptString) ) {
737cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
738cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty,
739cdf0e10cSrcweir 										 "UNACCEPT", aUnAcceptString );
740cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
741cdf0e10cSrcweir 				bAcceptorRequest = sal_True;
742cdf0e10cSrcweir 			}
743cdf0e10cSrcweir 
744cdf0e10cSrcweir #ifndef UNX
745cdf0e10cSrcweir 			// only in non-unix version, we need to handle a -help request
746cdf0e10cSrcweir 			// in a running instance in order to display  the command line help
747cdf0e10cSrcweir 			if ( aCmdLineArgs->IsHelp() ) {
748cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
749cdf0e10cSrcweir 					new ApplicationEvent( aEmpty, aEmpty, "HELP", aEmpty );
750cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
751cdf0e10cSrcweir 			}
752cdf0e10cSrcweir #endif
753cdf0e10cSrcweir 
754cdf0e10cSrcweir 			sal_Bool bDocRequestSent = sal_False;
755cdf0e10cSrcweir 			ProcessDocumentsRequest* pRequest = new ProcessDocumentsRequest(
756cdf0e10cSrcweir                 aCmdLineArgs->getCwdUrl());
757cdf0e10cSrcweir             cProcessed.reset();
758cdf0e10cSrcweir             pRequest->pcProcessed = &cProcessed;
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 			// Print requests are not dependent on the -invisible cmdline argument as they are
761cdf0e10cSrcweir 			// loaded with the "hidden" flag! So they are always checked.
762cdf0e10cSrcweir 			bDocRequestSent |= aCmdLineArgs->GetPrintList( pRequest->aPrintList );
763cdf0e10cSrcweir 			bDocRequestSent |= ( aCmdLineArgs->GetPrintToList( pRequest->aPrintToList ) &&
764cdf0e10cSrcweir 									aCmdLineArgs->GetPrinterName( pRequest->aPrinterName )		);
765cdf0e10cSrcweir 
766cdf0e10cSrcweir 			if ( !pCurrentCmdLineArgs->IsInvisible() )
767cdf0e10cSrcweir 			{
768cdf0e10cSrcweir 				// Read cmdline args that can open/create documents. As they would open a window
769cdf0e10cSrcweir 				// they are only allowed if the "-invisible" is currently not used!
770cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetOpenList( pRequest->aOpenList );
771cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetViewList( pRequest->aViewList );
772cdf0e10cSrcweir                 bDocRequestSent |= aCmdLineArgs->GetStartList( pRequest->aStartList );
773cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetForceOpenList( pRequest->aForceOpenList );
774cdf0e10cSrcweir 				bDocRequestSent |= aCmdLineArgs->GetForceNewList( pRequest->aForceNewList );
775cdf0e10cSrcweir 
776cdf0e10cSrcweir 				// Special command line args to create an empty document for a given module
777cdf0e10cSrcweir 
778cdf0e10cSrcweir                 // #i18338# (lo)
779cdf0e10cSrcweir                 // we only do this if no document was specified on the command line,
780cdf0e10cSrcweir                 // since this would be inconsistent with the the behaviour of
781cdf0e10cSrcweir                 // the first process, see OpenClients() (call to OpenDefault()) in app.cxx
782cdf0e10cSrcweir                 if ( aCmdLineArgs->HasModuleParam() && Desktop::CheckOEM() && (!bDocRequestSent))
783cdf0e10cSrcweir 				{
784cdf0e10cSrcweir 					SvtModuleOptions aOpt;
785cdf0e10cSrcweir 					SvtModuleOptions::EFactory eFactory = SvtModuleOptions::E_WRITER;
786cdf0e10cSrcweir 					if ( aCmdLineArgs->IsWriter() )
787cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_WRITER;
788cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsCalc() )
789cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_CALC;
790cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsDraw() )
791cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_DRAW;
792cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsImpress() )
793cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_IMPRESS;
794cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsBase() )
795cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_DATABASE;
796cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsMath() )
797cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_MATH;
798cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsGlobal() )
799cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_WRITERGLOBAL;
800cdf0e10cSrcweir 					else if ( aCmdLineArgs->IsWeb() )
801cdf0e10cSrcweir 						eFactory = SvtModuleOptions::E_WRITERWEB;
802cdf0e10cSrcweir 
803cdf0e10cSrcweir                     if ( pRequest->aOpenList.getLength() )
804cdf0e10cSrcweir                         pRequest->aModule = aOpt.GetFactoryName( eFactory );
805cdf0e10cSrcweir                     else
806cdf0e10cSrcweir                         AddURLToStringList( aOpt.GetFactoryEmptyDocumentURL( eFactory ), pRequest->aOpenList );
807cdf0e10cSrcweir 					bDocRequestSent = sal_True;
808cdf0e10cSrcweir 				}
809cdf0e10cSrcweir             }
810cdf0e10cSrcweir 
811cdf0e10cSrcweir             if (!aCmdLineArgs->IsQuickstart() && Desktop::CheckOEM()) {
812cdf0e10cSrcweir                 sal_Bool bShowHelp = sal_False;
813cdf0e10cSrcweir                 rtl::OUStringBuffer aHelpURLBuffer;
814cdf0e10cSrcweir                 if (aCmdLineArgs->IsHelpWriter()) {
815cdf0e10cSrcweir                     bShowHelp = sal_True;
816cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://swriter/start");
817cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpCalc()) {
818cdf0e10cSrcweir                     bShowHelp = sal_True;
819cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://scalc/start");
820cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpDraw()) {
821cdf0e10cSrcweir                     bShowHelp = sal_True;
822cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://sdraw/start");
823cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpImpress()) {
824cdf0e10cSrcweir                     bShowHelp = sal_True;
825cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://simpress/start");
826cdf0e10cSrcweir 				} else if (aCmdLineArgs->IsHelpBase()) {
827cdf0e10cSrcweir                     bShowHelp = sal_True;
828cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://sdatabase/start");
829cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpBasic()) {
830cdf0e10cSrcweir                     bShowHelp = sal_True;
831cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://sbasic/start");
832cdf0e10cSrcweir                 } else if (aCmdLineArgs->IsHelpMath()) {
833cdf0e10cSrcweir                     bShowHelp = sal_True;
834cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("vnd.sun.star.help://smath/start");
835cdf0e10cSrcweir                 }
836cdf0e10cSrcweir                 if (bShowHelp) {
837cdf0e10cSrcweir                     Any aRet = ::utl::ConfigManager::GetDirectConfigProperty( ::utl::ConfigManager::LOCALE );
838cdf0e10cSrcweir                     rtl::OUString aTmp;
839cdf0e10cSrcweir                     aRet >>= aTmp;
840cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("?Language=");
841cdf0e10cSrcweir                     aHelpURLBuffer.append(aTmp);
842cdf0e10cSrcweir #if defined UNX
843cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("&System=UNX");
844cdf0e10cSrcweir #elif defined WNT
845cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("&System=WIN");
846cdf0e10cSrcweir #elif defined OS2
847cdf0e10cSrcweir                     aHelpURLBuffer.appendAscii("&System=OS2");
848cdf0e10cSrcweir #endif
849cdf0e10cSrcweir                     ApplicationEvent* pAppEvent =
850cdf0e10cSrcweir                         new ApplicationEvent( aEmpty, aEmpty,
851cdf0e10cSrcweir                                               "OPENHELPURL", aHelpURLBuffer.makeStringAndClear());
852cdf0e10cSrcweir                     ImplPostForeignAppEvent( pAppEvent );
853cdf0e10cSrcweir                 }
854cdf0e10cSrcweir             }
855cdf0e10cSrcweir 
856cdf0e10cSrcweir             if ( bDocRequestSent && Desktop::CheckOEM())
857cdf0e10cSrcweir  			{
858cdf0e10cSrcweir 				// Send requests to dispatch watcher if we have at least one. The receiver
859cdf0e10cSrcweir 				// is responsible to delete the request after processing it.
860cdf0e10cSrcweir                 if ( aCmdLineArgs->HasModuleParam() )
861cdf0e10cSrcweir                 {
862cdf0e10cSrcweir                     SvtModuleOptions    aOpt;
863cdf0e10cSrcweir 
864cdf0e10cSrcweir                     // Support command line parameters to start a module (as preselection)
865cdf0e10cSrcweir                     if ( aCmdLineArgs->IsWriter() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SWRITER ) )
866cdf0e10cSrcweir                         pRequest->aModule = aOpt.GetFactoryName( SvtModuleOptions::E_WRITER );
867cdf0e10cSrcweir                     else if ( aCmdLineArgs->IsCalc() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SCALC ) )
868cdf0e10cSrcweir                         pRequest->aModule = aOpt.GetFactoryName( SvtModuleOptions::E_CALC );
869cdf0e10cSrcweir                     else if ( aCmdLineArgs->IsImpress() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SIMPRESS ) )
870cdf0e10cSrcweir                         pRequest->aModule= aOpt.GetFactoryName( SvtModuleOptions::E_IMPRESS );
871cdf0e10cSrcweir                     else if ( aCmdLineArgs->IsDraw() && aOpt.IsModuleInstalled( SvtModuleOptions::E_SDRAW ) )
872cdf0e10cSrcweir                         pRequest->aModule= aOpt.GetFactoryName( SvtModuleOptions::E_DRAW );
873cdf0e10cSrcweir                 }
874cdf0e10cSrcweir 
875cdf0e10cSrcweir 
876cdf0e10cSrcweir 				ImplPostProcessDocumentsEvent( pRequest );
877cdf0e10cSrcweir 			}
878cdf0e10cSrcweir 			else
879cdf0e10cSrcweir 			{
880cdf0e10cSrcweir 				// delete not used request again
881cdf0e10cSrcweir 				delete pRequest;
882cdf0e10cSrcweir 				pRequest = NULL;
883cdf0e10cSrcweir 			}
884cdf0e10cSrcweir 			if (( aArguments.CompareTo( sc_aShowSequence, sc_nShSeqLength ) == COMPARE_EQUAL ) ||
885cdf0e10cSrcweir 				aCmdLineArgs->IsEmpty() )
886cdf0e10cSrcweir 			{
887cdf0e10cSrcweir 				// no document was sent, just bring Office to front
888cdf0e10cSrcweir 				ApplicationEvent* pAppEvent =
889cdf0e10cSrcweir 						new ApplicationEvent( aEmpty, aEmpty, "APPEAR", aEmpty );
890cdf0e10cSrcweir 				ImplPostForeignAppEvent( pAppEvent );
891cdf0e10cSrcweir 			}
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 			// we don't need the mutex any longer...
894cdf0e10cSrcweir 			aGuard.clear();
895cdf0e10cSrcweir 			// wait for processing to finish
896cdf0e10cSrcweir             if (bDocRequestSent)
897cdf0e10cSrcweir     			cProcessed.wait();
898cdf0e10cSrcweir 			// processing finished, inform the requesting end
899cdf0e10cSrcweir 			nBytes = 0;
900cdf0e10cSrcweir 			while (
901cdf0e10cSrcweir                    (nResult = maStreamPipe.send(sc_aConfirmationSequence+nBytes, sc_nCSeqLength-nBytes))>0 &&
902cdf0e10cSrcweir                    ((nBytes += nResult) < sc_nCSeqLength) ) ;
903cdf0e10cSrcweir 			// now we can close, don't we?
904cdf0e10cSrcweir 			// maStreamPipe.close();
905cdf0e10cSrcweir 
906cdf0e10cSrcweir         }
907cdf0e10cSrcweir         else
908cdf0e10cSrcweir         {
909cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
910cdf0e10cSrcweir 			fprintf( stderr, "Error on accept: %d\n", (int)nError );
911cdf0e10cSrcweir #endif
912cdf0e10cSrcweir 			TimeValue tval;
913cdf0e10cSrcweir 			tval.Seconds = 1;
914cdf0e10cSrcweir 			tval.Nanosec = 0;
915cdf0e10cSrcweir 			sleep( tval );
916cdf0e10cSrcweir 		}
917cdf0e10cSrcweir 	} while( schedule() );
918cdf0e10cSrcweir }
919cdf0e10cSrcweir 
920cdf0e10cSrcweir static void AddToDispatchList(
921cdf0e10cSrcweir 	DispatchWatcher::DispatchList& rDispatchList,
922cdf0e10cSrcweir     boost::optional< rtl::OUString > const & cwdUrl,
923cdf0e10cSrcweir 	const OUString& aRequestList,
924cdf0e10cSrcweir 	DispatchWatcher::RequestType nType,
925cdf0e10cSrcweir     const OUString& aParam,
926cdf0e10cSrcweir     const OUString& aFactory )
927cdf0e10cSrcweir {
928cdf0e10cSrcweir 	if ( aRequestList.getLength() > 0 )
929cdf0e10cSrcweir 	{
930cdf0e10cSrcweir 		sal_Int32 nIndex = 0;
931cdf0e10cSrcweir 		do
932cdf0e10cSrcweir 		{
933cdf0e10cSrcweir 			OUString aToken = aRequestList.getToken( 0, APPEVENT_PARAM_DELIMITER, nIndex );
934cdf0e10cSrcweir 			if ( aToken.getLength() > 0 )
935cdf0e10cSrcweir 				rDispatchList.push_back(
936cdf0e10cSrcweir                     DispatchWatcher::DispatchRequest( nType, aToken, cwdUrl, aParam, aFactory ));
937cdf0e10cSrcweir 		}
938cdf0e10cSrcweir 		while ( nIndex >= 0 );
939cdf0e10cSrcweir 	}
940cdf0e10cSrcweir }
941cdf0e10cSrcweir 
942cdf0e10cSrcweir sal_Bool OfficeIPCThread::ExecuteCmdLineRequests( ProcessDocumentsRequest& aRequest )
943cdf0e10cSrcweir {
944cdf0e10cSrcweir     // protect the dispatch list
945cdf0e10cSrcweir     osl::ClearableMutexGuard aGuard( GetMutex() );
946cdf0e10cSrcweir 
947cdf0e10cSrcweir 	static DispatchWatcher::DispatchList	aDispatchList;
948cdf0e10cSrcweir 
949cdf0e10cSrcweir 	rtl::OUString aEmpty;
950cdf0e10cSrcweir 	// Create dispatch list for dispatch watcher
951cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aOpenList, DispatchWatcher::REQUEST_OPEN, aEmpty, aRequest.aModule );
952cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aViewList, DispatchWatcher::REQUEST_VIEW, aEmpty, aRequest.aModule );
953cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aStartList, DispatchWatcher::REQUEST_START, aEmpty, aRequest.aModule );
954cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aPrintList, DispatchWatcher::REQUEST_PRINT, aEmpty, aRequest.aModule );
955cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aPrintToList, DispatchWatcher::REQUEST_PRINTTO, aRequest.aPrinterName, aRequest.aModule );
956cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aForceOpenList, DispatchWatcher::REQUEST_FORCEOPEN, aEmpty, aRequest.aModule );
957cdf0e10cSrcweir     AddToDispatchList( aDispatchList, aRequest.aCwdUrl, aRequest.aForceNewList, DispatchWatcher::REQUEST_FORCENEW, aEmpty, aRequest.aModule );
958cdf0e10cSrcweir 
959cdf0e10cSrcweir 	sal_Bool bShutdown( sal_False );
960cdf0e10cSrcweir 
961cdf0e10cSrcweir 	if ( pGlobalOfficeIPCThread )
962cdf0e10cSrcweir 	{
963cdf0e10cSrcweir         if( ! pGlobalOfficeIPCThread->AreRequestsEnabled() )
964cdf0e10cSrcweir             return bShutdown;
965cdf0e10cSrcweir 
966cdf0e10cSrcweir 		pGlobalOfficeIPCThread->mnPendingRequests += aDispatchList.size();
967cdf0e10cSrcweir 		if ( !pGlobalOfficeIPCThread->mpDispatchWatcher )
968cdf0e10cSrcweir 		{
969cdf0e10cSrcweir 			pGlobalOfficeIPCThread->mpDispatchWatcher = DispatchWatcher::GetDispatchWatcher();
970cdf0e10cSrcweir 			pGlobalOfficeIPCThread->mpDispatchWatcher->acquire();
971cdf0e10cSrcweir 		}
972cdf0e10cSrcweir 
973cdf0e10cSrcweir         // copy for execute
974cdf0e10cSrcweir         DispatchWatcher::DispatchList aTempList( aDispatchList );
975cdf0e10cSrcweir         aDispatchList.clear();
976cdf0e10cSrcweir 
977cdf0e10cSrcweir 		aGuard.clear();
978cdf0e10cSrcweir 
979cdf0e10cSrcweir 		// Execute dispatch requests
980cdf0e10cSrcweir 		bShutdown = pGlobalOfficeIPCThread->mpDispatchWatcher->executeDispatchRequests( aTempList, s_bInEnableRequests );
981cdf0e10cSrcweir 
982cdf0e10cSrcweir 		// set processed flag
983cdf0e10cSrcweir 		if (aRequest.pcProcessed != NULL)
984cdf0e10cSrcweir 			aRequest.pcProcessed->set();
985cdf0e10cSrcweir 	}
986cdf0e10cSrcweir 
987cdf0e10cSrcweir 	return bShutdown;
988cdf0e10cSrcweir }
989cdf0e10cSrcweir 
990cdf0e10cSrcweir }
991