summaryrefslogtreecommitdiff
path: root/desktop/source/app/officeipcthread.cxx
blob: 5bc2942a1b4069d14a323421d5af9db6006c2536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*************************************************************************
 *
 *  $RCSfile: officeipcthread.cxx,v $
 *
 *  $Revision: 1.28 $
 *
 *  last change: $Author: hr $ $Date: 2003-03-25 13:51:19 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#include "app.hxx"
#include "officeipcthread.hxx"
#include "cmdlineargs.hxx"
#include "dispatchwatcher.hxx"
#include <stdio.h>

#ifndef _VOS_PROCESS_HXX_
#include <vos/process.hxx>
#endif
#ifndef _UTL_BOOTSTRAP_HXX
#include <unotools/bootstrap.hxx>
#endif
#ifndef _SV_SVAPP_HXX
#include <vcl/svapp.hxx>
#endif
#ifndef _THREAD_HXX_
#include <osl/thread.hxx>
#endif
#ifndef _RTL_DIGEST_H_
#include <rtl/digest.h>
#endif
#ifndef _RTL_USTRBUF_HXX_
#include <rtl/ustrbuf.hxx>
#endif
#ifndef _OSL_CONDITN_HXX_
#include <osl/conditn.hxx>
#endif
#ifndef INCLUDED_SVTOOLS_MODULEOPTIONS_HXX
#include <svtools/moduleoptions.hxx>
#endif

using namespace vos;
using namespace rtl;
using namespace desktop;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::frame;

const char  *OfficeIPCThread::sc_aTerminationSequence = "InternalIPC::TerminateThread";
const int OfficeIPCThread::sc_nTSeqLength = 28;
const char  *OfficeIPCThread::sc_aShowSequence = "-show";
const int OfficeIPCThread::sc_nShSeqLength = 5;
const char  *OfficeIPCThread::sc_aConfirmationSequence = "InternalIPC::ProcessingDone";
const int OfficeIPCThread::sc_nCSeqLength = 27;

// Type of pipe we use
enum PipeMode
{
    PIPEMODE_DONTKNOW,
    PIPEMODE_CREATED,
    PIPEMODE_CONNECTED
};

namespace desktop
{

String GetURL_Impl( const String& rName );

OfficeIPCThread*	OfficeIPCThread::pGlobalOfficeIPCThread = 0;
OSecurity			OfficeIPCThread::maSecurity;
::osl::Mutex*		OfficeIPCThread::pOfficeIPCThreadMutex = 0;


String CreateMD5FromString( const OUString& aMsg )
{
    // PRE: aStr "file"
    // BACK: Str "ababab....0f" Hexcode String

    rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
    if ( handle > 0 )
    {
        const sal_uInt8* pData = (const sal_uInt8*)aMsg.getStr();
        sal_uInt32		 nSize = ( aMsg.getLength() * sizeof( sal_Unicode ));
        sal_uInt32		 nMD5KeyLen = rtl_digest_queryLength( handle );
        sal_uInt8*		 pMD5KeyBuffer = new sal_uInt8[ nMD5KeyLen ];
        
        rtl_digest_init( handle, pData, nSize );
        rtl_digest_update( handle, pData, nSize );
        rtl_digest_get( handle, pMD5KeyBuffer, nMD5KeyLen );
        rtl_digest_destroy( handle );
        
        // Create hex-value string from the MD5 value to keep the string size minimal
        OUStringBuffer aBuffer( nMD5KeyLen * 2 + 1 );
        for ( sal_uInt32 i = 0; i < nMD5KeyLen; i++ )
            aBuffer.append( (sal_Int32)pMD5KeyBuffer[i], 16 );

        delete [] pMD5KeyBuffer;
        return aBuffer.makeStringAndClear();
    }
    
    return String();
}

class ProcessEventsClass_Impl
{
public:
    DECL_STATIC_LINK( ProcessEventsClass_Impl, CallEvent, void* pEvent );
    DECL_STATIC_LINK( ProcessEventsClass_Impl, ProcessDocumentsEvent, void* pEvent );
};

IMPL_STATIC_LINK( ProcessEventsClass_Impl, CallEvent, void*, pEvent )
{
    // Application events are processed by the Desktop::HandleAppEvent implementation.
    Desktop::HandleAppEvent( *((ApplicationEvent*)pEvent) );
    delete (ApplicationEvent*)pEvent;
    return 0;
}

IMPL_STATIC_LINK( ProcessEventsClass_Impl, ProcessDocumentsEvent, void*, pEvent )
{
    // Documents requests are processed by the OfficeIPCThread implementation
    ProcessDocumentsRequest* pDocsRequest = (ProcessDocumentsRequest*)pEvent;

    if ( pDocsRequest )
    {
        OfficeIPCThread::ExecuteCmdLineRequests( *pDocsRequest );
        delete pDocsRequest;
    }
    return 0;
}

void ImplPostForeignAppEvent( ApplicationEvent* pEvent )
{
    Application::PostUserEvent( STATIC_LINK( NULL, ProcessEventsClass_Impl, CallEvent ), pEvent );
}

void ImplPostProcessDocumentsEvent( ProcessDocumentsRequest* pEvent )
{
    Application::PostUserEvent( STATIC_LINK( NULL, ProcessEventsClass_Impl, ProcessDocumentsEvent ), pEvent );
}

OSignalHandler::TSignalAction SAL_CALL SalMainPipeExchangeSignalHandler::signal(TSignalInfo *pInfo)
{
    if( pInfo->Signal == osl_Signal_Terminate )
        OfficeIPCThread::DisableOfficeIPCThread();
    return (TAction_CallNextHandler);
}

// ----------------------------------------------------------------------------

// The OfficeIPCThreadController implementation is a bookkeeper for all pending requests
// that were created by the OfficeIPCThread. The requests are waiting to be processed by
// our framework loadComponentFromURL function (e.g. open/print request).
// During shutdown the framework is asking OfficeIPCThreadController about pending requests.
// If there are pending requests framework has to stop the shutdown process. It is waiting
// for these requests because framework is not able to handle shutdown and open a document
// concurrently.


// XServiceInfo
OUString SAL_CALL OfficeIPCThreadController::getImplementationName()
throw ( RuntimeException )
{
    return OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.OfficeIPCThreadController" ));
}

sal_Bool SAL_CALL OfficeIPCThreadController::supportsService( const OUString& ServiceName )
throw ( RuntimeException )
{
    return sal_False;
}

Sequence< OUString > SAL_CALL OfficeIPCThreadController::getSupportedServiceNames()
throw ( RuntimeException )
{
    Sequence< OUString > aSeq( 0 );
    return aSeq;
}

// XEventListener
void SAL_CALL OfficeIPCThreadController::disposing( const EventObject& Source )
throw( RuntimeException )
{
}

// XTerminateListener
void SAL_CALL OfficeIPCThreadController::queryTermination( const EventObject& aEvent )
throw( TerminationVetoException, RuntimeException )
{
    // Desktop ask about pending request through our office ipc pipe. We have to
    // be sure that no pending request is waiting because framework is not able to
    // handle shutdown and open a document concurrently.

    if ( OfficeIPCThread::AreRequestsPending() )
        throw TerminationVetoException();
    else
        OfficeIPCThread::BlockAllRequests();
}

void SAL_CALL OfficeIPCThreadController::notifyTermination( const EventObject& aEvent )
throw( RuntimeException )
{
}

// ----------------------------------------------------------------------------

::osl::Mutex&	OfficeIPCThread::GetMutex()
{
    // Get or create our mutex for thread-saftey
    if ( !pOfficeIPCThreadMutex )
    {
        ::osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
        if ( !pOfficeIPCThreadMutex )
            pOfficeIPCThreadMutex = new osl::Mutex;
    }

    return *pOfficeIPCThreadMutex;
}

OfficeIPCThread* OfficeIPCThread::GetOfficeIPCThread()
{
    // Return the one and only OfficeIPCThread pointer
    ::osl::MutexGuard	aGuard( GetMutex() );
    return pGlobalOfficeIPCThread;
}

void OfficeIPCThread::BlockAllRequests()
{
    // We have the order to block all incoming requests. Framework
    // wants to shutdown and we have to make sure that no loading/printing
    // requests are executed anymore.
    ::osl::MutexGuard	aGuard( GetMutex() );

    if ( pGlobalOfficeIPCThread )
        pGlobalOfficeIPCThread->mbBlockRequests = sal_True;
}

sal_Bool OfficeIPCThread::AreRequestsPending()
{
    // Give info about pending requests
    ::osl::MutexGuard	aGuard( GetMutex() );
    if ( pGlobalOfficeIPCThread )
        return ( pGlobalOfficeIPCThread->mnPendingRequests > 0 );
    else
        return sal_False;
}

void OfficeIPCThread::RequestsCompleted( int nCount )
{
    // Remove nCount pending requests from our internal counter
    ::osl::MutexGuard	aGuard( GetMutex() );
    if ( pGlobalOfficeIPCThread )
    {
        if ( pGlobalOfficeIPCThread->mnPendingRequests > 0 )
            pGlobalOfficeIPCThread->mnPendingRequests -= nCount;
    }
}

OfficeIPCThread::Status OfficeIPCThread::EnableOfficeIPCThread()
{
    ::osl::MutexGuard	aGuard( GetMutex() );

    if( pGlobalOfficeIPCThread )
        return IPC_STATUS_OK;

    ::rtl::OUString aUserInstallPath;
    ::rtl::OUString aDummy;

    ::vos::OStartupInfo aInfo;
    OfficeIPCThread* pThread = new OfficeIPCThread;

    pThread->maPipeIdent = OUString( RTL_CONSTASCII_USTRINGPARAM( "SingleOfficeIPC_" ) );

    // The name of the named pipe is created with the hashcode of the user installation directory (without /user). We have to retrieve
    // this information from a unotools implementation.
    ::utl::Bootstrap::PathStatus aLocateResult = ::utl::Bootstrap::locateUserInstallation( aUserInstallPath );
    if ( aLocateResult == ::utl::Bootstrap::PATH_EXISTS )
        aDummy = aUserInstallPath;
    else
    {
        delete pThread;
        return IPC_STATUS_BOOTSTRAP_ERROR;
    }

    // Try to  determine if we are the first office or not! This should prevent multiple 
    // access to the user directory !
    // First we try to create our pipe if this fails we try to connect. We have to do this
    // in a loop because the the other office can crash or shutdown between createPipe
    // and connectPipe!!
    OUString aUserInstallPathHashCode = CreateMD5FromString( aDummy );

    // Check result to create a hash code from the user install path
    if ( aUserInstallPathHashCode.getLength() == 0 )
        return IPC_STATUS_BOOTSTRAP_ERROR; // Something completely broken, we cannot create a valid hash code!

    pThread->maPipeIdent = pThread->maPipeIdent + aUserInstallPathHashCode;

    PipeMode nPipeMode = PIPEMODE_DONTKNOW;
    do
    {
        // Try to create pipe
        if ( pThread->maPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Create, maSecurity ))
        {
            // Pipe created
            nPipeMode = PIPEMODE_CREATED;
        }
        else if( pThread->maPipe.create( pThread->maPipeIdent.getStr(), OPipe::TOption_Open, maSecurity )) // Creation not successfull, now we try to connect
        {
            // Pipe connected to first office
            nPipeMode = PIPEMODE_CONNECTED;
        }
        else
        {
            // Wait for second office to be ready
            TimeValue aTimeValue;
            aTimeValue.Seconds = 0;
            aTimeValue.Nanosec = 10000000; // 10ms
            osl::Thread::wait( aTimeValue );
        }

    } while ( nPipeMode == PIPEMODE_DONTKNOW );

    if ( nPipeMode == PIPEMODE_CREATED )
    {
        // Seems we are the one and only, so start listening thread
        pGlobalOfficeIPCThread = pThread;
        pThread->create(); // starts thread
    }
    else
    {
        // Seems another office is running. Pipe arguments to it and self terminate
        pThread->maStreamPipe = pThread->maPipe;

        sal_Bool bWaitBeforeClose = sal_False;
        ByteString aArguments;
        ULONG nCount = aInfo.getCommandArgCount();		

        if ( nCount == 0 )
        {
            // Use default argument so the first office can distinguish between a real second
            // office and another program that check the existence of the the pipe!!
            
            aArguments += ByteString( "-show" );
        }
        else
        {
            sal_Bool    bPrintTo = sal_False;
            OUString    aPrintToCmd( RTL_CONSTASCII_USTRINGPARAM( "-pt" ));
            for( ULONG i=0; i < nCount; i++ )
            {
                aInfo.getCommandArg( i, aDummy );
                // Make absolute pathes from relative ones!
                // It's neccessary to use current working directory of THESE office instance and not of
                // currently running once, which get these information by using pipe.
                // Otherwhise relativ pathes are not right for his environment ...
                if( aDummy.indexOf('-',0) != 0 )
                {
                    bWaitBeforeClose = sal_True;
                    if ( !bPrintTo )
                        aDummy = GetURL_Impl( aDummy );
                    bPrintTo = sal_False;
                }
                else
                {
                    if ( aDummy.equalsIgnoreAsciiCase( aPrintToCmd ))
                        bPrintTo = sal_True;
                    else
                        bPrintTo = sal_False;
                }

                aArguments += ByteString( String( aDummy ), osl_getThreadTextEncoding() );
                aArguments += '|';
            }
        }
        // finaly, write the string onto the pipe
        pThread->maStreamPipe.write( aArguments.GetBuffer(), aArguments.Len() );
        pThread->maStreamPipe.write( "\0", 1 );

        // wait for confirmation #95361# #95425#
        ByteString aToken(sc_aConfirmationSequence);
        char *aReceiveBuffer = new char[aToken.Len()+1];
        int n = pThread->maStreamPipe.read( aReceiveBuffer, aToken.Len() );
        aReceiveBuffer[n]='\0';

        delete pThread;
        if (aToken.CompareTo(aReceiveBuffer)!= COMPARE_EQUAL) {
            // something went wrong
            delete aReceiveBuffer;
            return IPC_STATUS_BOOTSTRAP_ERROR;
        } else {
            delete aReceiveBuffer;
            return IPC_STATUS_2ND_OFFICE;
        }
    }

    return IPC_STATUS_OK;
}

void OfficeIPCThread::DisableOfficeIPCThread()
{
    osl::ClearableMutexGuard aMutex( GetMutex() );

    if( pGlobalOfficeIPCThread && !( pGlobalOfficeIPCThread->mbShutdownInProgress ))
    {
        pGlobalOfficeIPCThread->mbShutdownInProgress = sal_True;

        // send thread a termination message
        // this is done so the subsequent join will not hang
        // because the thread hangs in accept of pipe
        OPipe Pipe( pGlobalOfficeIPCThread->maPipeIdent, OPipe::TOption_Open, maSecurity );
        //Pipe.send( TERMINATION_SEQUENCE, TERMINATION_LENGTH );
        Pipe.send( sc_aTerminationSequence, sc_nTSeqLength+1 ); // also send 0-byte

        // close the pipe so that the streampipe on the other
        // side produces EOF
        Pipe.close();

        // release mutex to avoid deadlocks
        aMutex.clear();

        // exit gracefully and join
        pGlobalOfficeIPCThread->join();

        {
            // acquire mutex again to delete and reset global pointer threadsafe!
            osl::MutexGuard aGuard( GetMutex() );
            delete pGlobalOfficeIPCThread;
            pGlobalOfficeIPCThread = 0;
        }
    }
}

OfficeIPCThread::OfficeIPCThread() :
    mbBlockRequests( sal_False ),
    mnPendingRequests( 0 ),
    mpDispatchWatcher( 0 ),
    mbShutdownInProgress( sal_False )
{
}

OfficeIPCThread::~OfficeIPCThread()
{
    ::osl::ClearableMutexGuard	aGuard( GetMutex() );

    if ( mpDispatchWatcher )
        mpDispatchWatcher->release();
    maPipe.close();
    maStreamPipe.close();
    pGlobalOfficeIPCThread = 0;
}

static void AddURLToStringList( const rtl::OUString& aURL, rtl::OUString& aStringList )
{
    if ( aStringList.getLength() )
        aStringList += ::rtl::OUString::valueOf( (sal_Unicode)APPEVENT_PARAM_DELIMITER );
    aStringList += aURL;
}

void SAL_CALL OfficeIPCThread::run()
{
    while (schedule())
    {
        OPipe::TPipeError
            nError = maPipe.accept( maStreamPipe );


        if( nError == OStreamPipe::E_None )
        {
            osl::ClearableMutexGuard aGuard( GetMutex() );
            ByteString aArguments;
            // test byte by byte
            const int nBufSz = 2048;
            char pBuf[nBufSz];
            int nBytes = 0;
            int nResult = 0;
            // read into pBuf until '\0' is read or read-error
            while ((nResult=maStreamPipe.recv( pBuf+nBytes, nBufSz-nBytes))>0) {
                nBytes += nResult;
                if (pBuf[nBytes-1]=='\0') {
                    aArguments += pBuf;
                    break;
                }
            }
            // don't close pipe ...

            // #90717# Is this a lookup message from another application? if so, ignore
            if ( aArguments.Len() == 0 )
                continue;

            // is this a termination message ? if so, terminate
            if(( aArguments.CompareTo( sc_aTerminationSequence, sc_nTSeqLength ) == COMPARE_EQUAL ) ||
                    mbBlockRequests ) return;
            String           aEmpty;
            CommandLineArgs  aCmdLineArgs( OUString( aArguments.GetBuffer(), 
                aArguments.Len(), gsl_getSystemTextEncoding() ));
            CommandLineArgs	*pCurrentCmdLineArgs = Desktop::GetCommandLineArgs();

            if ( aCmdLineArgs.IsQuickstart() )
            {
                // we have to use application event, because we have to start quickstart service in main thread!!
                ApplicationEvent* pAppEvent =
                    new ApplicationEvent( aEmpty, aEmpty,
                                            "QUICKSTART", aEmpty );
                ImplPostForeignAppEvent( pAppEvent );
            }

            // handle request for acceptor
            sal_Bool bAcceptorRequest = sal_False;
            OUString aAcceptString;
            if ( aCmdLineArgs.GetAcceptString(aAcceptString) ) {				
                ApplicationEvent* pAppEvent =
                    new ApplicationEvent( aEmpty, aEmpty,
                                          "ACCEPT", aAcceptString );
                ImplPostForeignAppEvent( pAppEvent );
                bAcceptorRequest = sal_True;
            }
            // handle acceptor removal
            OUString aUnAcceptString;				
            if ( aCmdLineArgs.GetUnAcceptString(aUnAcceptString) ) {
                ApplicationEvent* pAppEvent =
                    new ApplicationEvent( aEmpty, aEmpty,
                                         "UNACCEPT", aUnAcceptString );
                ImplPostForeignAppEvent( pAppEvent );
                bAcceptorRequest = sal_True;
            }

#ifndef UNX
            // only in non-unix version, we need to handle a -help request
            // in a running instance in order to display  the command line help
            if ( aCmdLineArgs.IsHelp() ) {
                ApplicationEvent* pAppEvent =
                    new ApplicationEvent( aEmpty, aEmpty, "HELP", aEmpty );
                ImplPostForeignAppEvent( pAppEvent );
            }
#endif
            
            sal_Bool bDocRequestSent = sal_False;
            ProcessDocumentsRequest* pRequest = new ProcessDocumentsRequest;

            // Print requests are not dependent on the -invisible cmdline argument as they are
            // loaded with the "hidden" flag! So they are always checked.
            bDocRequestSent |= aCmdLineArgs.GetPrintList( pRequest->aPrintList );
            bDocRequestSent |= ( aCmdLineArgs.GetPrintToList( pRequest->aPrintToList ) && 
                                    aCmdLineArgs.GetPrinterName( pRequest->aPrinterName )		);
            
            if ( !pCurrentCmdLineArgs->IsInvisible() )
            {
                // Read cmdline args that can open/create documents. As they would open a window
                // they are only allowed if the "-invisible" is currently not used!
                bDocRequestSent |= aCmdLineArgs.GetOpenList( pRequest->aOpenList );
                bDocRequestSent |= aCmdLineArgs.GetViewList( pRequest->aViewList );
                bDocRequestSent |= aCmdLineArgs.GetForceOpenList( pRequest->aForceOpenList );
                bDocRequestSent |= aCmdLineArgs.GetForceNewList( pRequest->aForceNewList );
                
                // Special command line args to create an empty document for a given module
                if ( aCmdLineArgs.HasModuleParam() )
                {
                    SvtModuleOptions aOpt;
                    SvtModuleOptions::EFactory eFactory = SvtModuleOptions::E_WRITER;
                    if ( aCmdLineArgs.IsWriter() )
                        eFactory = SvtModuleOptions::E_WRITER;
                    else if ( aCmdLineArgs.IsCalc() )
                        eFactory = SvtModuleOptions::E_CALC;
                    else if ( aCmdLineArgs.IsDraw() )
                        eFactory = SvtModuleOptions::E_DRAW;
                    else if ( aCmdLineArgs.IsImpress() )
                        eFactory = SvtModuleOptions::E_IMPRESS;
                    else if ( aCmdLineArgs.IsMath() )
                        eFactory = SvtModuleOptions::E_MATH;
                    else if ( aCmdLineArgs.IsGlobal() )
                        eFactory = SvtModuleOptions::E_WRITERGLOBAL;
                    else if ( aCmdLineArgs.IsWeb() )
                        eFactory = SvtModuleOptions::E_WRITERWEB;
                    AddURLToStringList( aOpt.GetFactoryEmptyDocumentURL( eFactory ), pRequest->aOpenList );
                    bDocRequestSent = sal_True;
                }
            }
                
            if ( bDocRequestSent )
             {
                // Send requests to dispatch watcher if we have at least one. The receiver
                // is responsible to delete the request after processing it.
                ImplPostProcessDocumentsEvent( pRequest );
            }
            else
            {
                // delete not used request again
                delete pRequest;
                pRequest = NULL;
            }
            if ((( aArguments.CompareTo( sc_aShowSequence, sc_nShSeqLength ) == COMPARE_EQUAL ) ||
                !bDocRequestSent ) && !bAcceptorRequest )
            {
                // no document was sent, just bring Office to front
                ApplicationEvent* pAppEvent =
                        new ApplicationEvent( aEmpty, aEmpty, "APPEAR", aEmpty );
                ImplPostForeignAppEvent( pAppEvent );
                if (pRequest != NULL) pRequest->cProcessed.set();
            }

            // we don't need the mutex any longer...
            aGuard.clear();
            // wait for processing to finish
            if (pRequest != NULL) pRequest->cProcessed.wait();
            // processing finished, inform the requesting end
            nBytes = 0;
            while (
                (nResult = maStreamPipe.send(sc_aConfirmationSequence+nBytes, sc_nCSeqLength-nBytes))>0 &&
                ((nBytes += nResult) < sc_nCSeqLength) );
            // now we can close, don't we?
            // maStreamPipe.close();

    }
    else
    {
#if defined DEBUG || defined DBG_UTIL
            fprintf( stderr, "Error on accept: %d\n", (int)nError );
#endif
            TimeValue tval;
            tval.Seconds = 1;
            tval.Nanosec = 0;
            sleep( tval );
        }
    }
}

static void AddToDispatchList( 
    DispatchWatcher::DispatchList& rDispatchList, 
    const OUString& aRequestList, 
    DispatchWatcher::RequestType nType,
    const OUString& aParam )
{
    if ( aRequestList.getLength() > 0 )
    {
        sal_Int32 nIndex = 0;
        do
        {
            OUString aToken = aRequestList.getToken( 0, APPEVENT_PARAM_DELIMITER, nIndex );
            if ( aToken.getLength() > 0 )
                rDispatchList.push_back(
                    DispatchWatcher::DispatchRequest( nType, aToken, aParam ));
        }
        while ( nIndex >= 0 );
    }
}

void OfficeIPCThread::ExecuteCmdLineRequests( ProcessDocumentsRequest& aRequest )
{
    ::rtl::OUString					aEmpty;
    DispatchWatcher::DispatchList	aDispatchList;

    // Create dispatch list for dispatch watcher
    AddToDispatchList( aDispatchList, aRequest.aOpenList, DispatchWatcher::REQUEST_OPEN, aEmpty );
    AddToDispatchList( aDispatchList, aRequest.aViewList, DispatchWatcher::REQUEST_VIEW, aEmpty );
    AddToDispatchList( aDispatchList, aRequest.aPrintList, DispatchWatcher::REQUEST_PRINT, aEmpty );
    AddToDispatchList( aDispatchList, aRequest.aPrintToList, DispatchWatcher::REQUEST_PRINTTO, aRequest.aPrinterName );
    AddToDispatchList( aDispatchList, aRequest.aForceOpenList, DispatchWatcher::REQUEST_FORCEOPEN, aEmpty );
    AddToDispatchList( aDispatchList, aRequest.aForceNewList, DispatchWatcher::REQUEST_FORCENEW, aEmpty );

    osl::ClearableMutexGuard aGuard( GetMutex() );

    if ( pGlobalOfficeIPCThread )
    {
        pGlobalOfficeIPCThread->mnPendingRequests += aDispatchList.size();
        if ( !pGlobalOfficeIPCThread->mpDispatchWatcher )
        {
            pGlobalOfficeIPCThread->mpDispatchWatcher = DispatchWatcher::GetDispatchWatcher();
            pGlobalOfficeIPCThread->mpDispatchWatcher->acquire();
        }

        aGuard.clear();
        
        // Execute dispatch requests
        pGlobalOfficeIPCThread->mpDispatchWatcher->executeDispatchRequests( aDispatchList );

        // set processed flag
        aRequest.cProcessed.set();

    }
}

}