10a1e2f0eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
30a1e2f0eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
40a1e2f0eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
50a1e2f0eSAndrew Rist  * distributed with this work for additional information
60a1e2f0eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
70a1e2f0eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
80a1e2f0eSAndrew Rist  * "License"); you may not use this file except in compliance
90a1e2f0eSAndrew Rist  * with the License.  You may obtain a copy of the License at
100a1e2f0eSAndrew Rist  *
110a1e2f0eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
120a1e2f0eSAndrew Rist  *
130a1e2f0eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
140a1e2f0eSAndrew Rist  * software distributed under the License is distributed on an
150a1e2f0eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
160a1e2f0eSAndrew Rist  * KIND, either express or implied.  See the License for the
170a1e2f0eSAndrew Rist  * specific language governing permissions and limitations
180a1e2f0eSAndrew Rist  * under the License.
190a1e2f0eSAndrew Rist  *
200a1e2f0eSAndrew Rist  *************************************************************/
210a1e2f0eSAndrew Rist 
220a1e2f0eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _DESKTOP_OFFICEIPCTHREAD_HXX_
25cdf0e10cSrcweir #define _DESKTOP_OFFICEIPCTHREAD_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
28cdf0e10cSrcweir #include <com/sun/star/frame/XTerminateListener.hpp>
29cdf0e10cSrcweir #include <vos/pipe.hxx>
30cdf0e10cSrcweir #include <vos/security.hxx>
31cdf0e10cSrcweir #include <vos/thread.hxx>
32cdf0e10cSrcweir #include <vos/signal.hxx>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir #ifndef _CPPUHELPER_WEAKBASE2_HXX_
35cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #include <osl/conditn.hxx>
38cdf0e10cSrcweir #include "boost/optional.hpp"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace desktop
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir class SalMainPipeExchangeSignalHandler : public vos::OSignalHandler
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     virtual TSignalAction SAL_CALL signal(TSignalInfo *pInfo);
46cdf0e10cSrcweir };
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // A request for the current office
49cdf0e10cSrcweir // that was given by command line or by IPC pipe communication.
50cdf0e10cSrcweir struct ProcessDocumentsRequest
51cdf0e10cSrcweir {
ProcessDocumentsRequestdesktop::ProcessDocumentsRequest52cdf0e10cSrcweir     ProcessDocumentsRequest(boost::optional< rtl::OUString > const & cwdUrl):
53cdf0e10cSrcweir         aCwdUrl(cwdUrl), pcProcessed( NULL ) {}
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     boost::optional< ::rtl::OUString > aCwdUrl;
56cdf0e10cSrcweir     ::rtl::OUString aModule;
57cdf0e10cSrcweir 	::rtl::OUString aOpenList;		// Documents that should be opened in the default way
58cdf0e10cSrcweir 	::rtl::OUString aViewList;      // Documents that should be opened in viewmode
59cdf0e10cSrcweir     ::rtl::OUString aStartList;     // Documents/Presentations that should be started
60cdf0e10cSrcweir 	::rtl::OUString aPrintList;		// Documents that should be printed on default printer
61cdf0e10cSrcweir 	::rtl::OUString aForceOpenList; // Documents that should be forced to open for editing (even templates)
62cdf0e10cSrcweir 	::rtl::OUString aForceNewList;	// Documents that should be forced to create a new document
63cdf0e10cSrcweir 	::rtl::OUString aPrinterName;	// The printer name that should be used for printing
64cdf0e10cSrcweir 	::rtl::OUString aPrintToList;	// Documents that should be printed on the given printer
65cdf0e10cSrcweir 	::osl::Condition *pcProcessed;  // pointer condition to be set when the request has been processed
66cdf0e10cSrcweir };
67cdf0e10cSrcweir 
68cdf0e10cSrcweir class DispatchWatcher;
69cdf0e10cSrcweir class OfficeIPCThread : public vos::OThread
70cdf0e10cSrcweir {
71cdf0e10cSrcweir   private:
72cdf0e10cSrcweir 	static OfficeIPCThread*		pGlobalOfficeIPCThread;
73cdf0e10cSrcweir 	static ::osl::Mutex*		pOfficeIPCThreadMutex;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     vos::OPipe					maPipe;
76cdf0e10cSrcweir     vos::OStreamPipe			maStreamPipe;
77cdf0e10cSrcweir 	rtl::OUString				maPipeIdent;
78cdf0e10cSrcweir 	bool                        mbDowning;
79cdf0e10cSrcweir     bool                        mbRequestsEnabled;
80cdf0e10cSrcweir 	int							mnPendingRequests;
81cdf0e10cSrcweir 	DispatchWatcher*			mpDispatchWatcher;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     /* condition to be set when the request has been processed */
84cdf0e10cSrcweir     ::osl::Condition cProcessed;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /* condition to be set when the main event loop is ready
87cdf0e10cSrcweir        otherwise an error dialogs event loop could eat away
88cdf0e10cSrcweir        requests from a 2nd office */
89cdf0e10cSrcweir     ::osl::Condition cReady;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	static ::osl::Mutex&		GetMutex();
92cdf0e10cSrcweir 	static const char *sc_aTerminationSequence;
93cdf0e10cSrcweir 	static const int sc_nTSeqLength;
94cdf0e10cSrcweir 	static const char *sc_aShowSequence;
95cdf0e10cSrcweir 	static const int sc_nShSeqLength;
96cdf0e10cSrcweir 	static const char *sc_aConfirmationSequence;
97cdf0e10cSrcweir 	static const int sc_nCSeqLength;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	OfficeIPCThread();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir   protected:
102cdf0e10cSrcweir 	/// Working method which should be overridden
103cdf0e10cSrcweir     virtual void SAL_CALL run();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir   public:
106cdf0e10cSrcweir     enum Status
107cdf0e10cSrcweir 	{
108cdf0e10cSrcweir 		IPC_STATUS_OK,
109cdf0e10cSrcweir 		IPC_STATUS_2ND_OFFICE,
110*aa94018dSJürgen Schmidt 		IPC_STATUS_BOOTSTRAP_ERROR,
111*aa94018dSJürgen Schmidt 		IPC_STATUS_MULTI_TS_ERROR
112cdf0e10cSrcweir 	};
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	virtual ~OfficeIPCThread();
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	// controlling pipe communication during shutdown
117cdf0e10cSrcweir 	static void					SetDowning();
118cdf0e10cSrcweir     static void                 EnableRequests( bool i_bEnable = true );
119cdf0e10cSrcweir 	static sal_Bool				AreRequestsPending();
120cdf0e10cSrcweir 	static void					RequestsCompleted( int n = 1 );
121cdf0e10cSrcweir 	static sal_Bool				ExecuteCmdLineRequests( ProcessDocumentsRequest& );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	// return sal_False if second office
124cdf0e10cSrcweir 	static Status				EnableOfficeIPCThread();
125cdf0e10cSrcweir 	static void					DisableOfficeIPCThread();
126cdf0e10cSrcweir     // start dispatching events...
127cdf0e10cSrcweir     static void                 SetReady(OfficeIPCThread* pThread = NULL);
128cdf0e10cSrcweir 
AreRequestsEnabled() const129cdf0e10cSrcweir     bool                        AreRequestsEnabled() const { return mbRequestsEnabled && ! mbDowning; }
130cdf0e10cSrcweir };
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
133cdf0e10cSrcweir class OfficeIPCThreadController : public ::cppu::WeakImplHelper2<
134cdf0e10cSrcweir 											::com::sun::star::lang::XServiceInfo,
135cdf0e10cSrcweir 											::com::sun::star::frame::XTerminateListener >
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	public:
OfficeIPCThreadController()138cdf0e10cSrcweir 		OfficeIPCThreadController() {}
~OfficeIPCThreadController()139cdf0e10cSrcweir 		virtual ~OfficeIPCThreadController() {}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		// XServiceInfo
142cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getImplementationName()
143cdf0e10cSrcweir 			throw ( ::com::sun::star::uno::RuntimeException );
144cdf0e10cSrcweir 		virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
145cdf0e10cSrcweir 			throw ( ::com::sun::star::uno::RuntimeException );
146cdf0e10cSrcweir 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
147cdf0e10cSrcweir 			throw ( ::com::sun::star::uno::RuntimeException );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		// XEventListener
150cdf0e10cSrcweir 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source )
151cdf0e10cSrcweir 			throw( ::com::sun::star::uno::RuntimeException );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 		// XTerminateListener
154cdf0e10cSrcweir 		virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& aEvent )
155cdf0e10cSrcweir 			throw( ::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException );
156cdf0e10cSrcweir 		virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& aEvent )
157cdf0e10cSrcweir 			throw( ::com::sun::star::uno::RuntimeException );
158cdf0e10cSrcweir };
159cdf0e10cSrcweir 
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir #endif // _DESKTOP_OFFICEIPCTHREAD_HXX_
163