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_DISPATCHWATCHER_HXX
25cdf0e10cSrcweir #define _DESKTOP_DISPATCHWATCHER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
28cdf0e10cSrcweir #include <com/sun/star/frame/XNotifyingDispatch.hpp>
29cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchResultListener.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "officeipcthread.hxx"
32cdf0e10cSrcweir #include <hash_map>
33cdf0e10cSrcweir #include <vector>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace desktop
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /*
39*07a3d7f1SPedro Giffuni 	Class for controls dispatching of command URL through office command line. There
40cdf0e10cSrcweir 	are "dangerous" command URLs, that can result in a running office without UI. To prevent
41cdf0e10cSrcweir 	this situation the implementation surveile all dispatches and looks for an open task if
42cdf0e10cSrcweir 	there is arose a problem. If there is none the office will be shutdown to prevent a
43cdf0e10cSrcweir 	running office without UI.
44cdf0e10cSrcweir */
45cdf0e10cSrcweir 
46cdf0e10cSrcweir struct OUStringHashCode
47cdf0e10cSrcweir {
operator ()desktop::OUStringHashCode48cdf0e10cSrcweir     size_t operator()( const ::rtl::OUString& sString ) const
49cdf0e10cSrcweir 	{
50cdf0e10cSrcweir 		return sString.hashCode();
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir };
53cdf0e10cSrcweir 
54cdf0e10cSrcweir class DispatchWatcherHashMap : public ::std::hash_map< ::rtl::OUString, sal_Int32, OUStringHashCode, ::std::equal_to< ::rtl::OUString >	>
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	public:
free()57cdf0e10cSrcweir 		inline void free()
58cdf0e10cSrcweir 		{
59cdf0e10cSrcweir 			DispatchWatcherHashMap().swap( *this );
60cdf0e10cSrcweir 		}
61cdf0e10cSrcweir };
62cdf0e10cSrcweir 
63cdf0e10cSrcweir class DispatchWatcher : public ::cppu::WeakImplHelper1< ::com::sun::star::frame::XDispatchResultListener >
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	public:
66cdf0e10cSrcweir 		enum RequestType
67cdf0e10cSrcweir 		{
68cdf0e10cSrcweir 			REQUEST_OPEN,
69cdf0e10cSrcweir 			REQUEST_VIEW,
70cdf0e10cSrcweir             REQUEST_START,
71cdf0e10cSrcweir 			REQUEST_PRINT,
72cdf0e10cSrcweir 			REQUEST_PRINTTO,
73cdf0e10cSrcweir 			REQUEST_FORCEOPEN,
74cdf0e10cSrcweir 			REQUEST_FORCENEW
75cdf0e10cSrcweir 		};
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 		struct DispatchRequest
78cdf0e10cSrcweir 		{
DispatchRequestdesktop::DispatchWatcher::DispatchRequest79cdf0e10cSrcweir             DispatchRequest( RequestType aType, const ::rtl::OUString& aFile, boost::optional< rtl::OUString > const & cwdUrl, const ::rtl::OUString& aPrinter, const ::rtl::OUString& aFact ) :
80cdf0e10cSrcweir                 aRequestType( aType ), aURL( aFile ), aCwdUrl( cwdUrl ), aPrinterName( aPrinter ), aPreselectedFactory( aFact ) {}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 			RequestType		aRequestType;
83cdf0e10cSrcweir 			rtl::OUString	aURL;
84cdf0e10cSrcweir             boost::optional< rtl::OUString > aCwdUrl;
85cdf0e10cSrcweir 			rtl::OUString	aPrinterName;
86cdf0e10cSrcweir             rtl::OUString   aPreselectedFactory;
87cdf0e10cSrcweir 		};
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 		typedef std::vector< DispatchRequest > DispatchList;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 		virtual ~DispatchWatcher();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 		// XEventListener
94cdf0e10cSrcweir 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source )
95cdf0e10cSrcweir 			throw(::com::sun::star::uno::RuntimeException);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         // XDispachResultListener
98cdf0e10cSrcweir         virtual void SAL_CALL dispatchFinished( const com::sun::star::frame::DispatchResultEvent& aEvent ) throw( com::sun::star::uno::RuntimeException );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 		// Access function to get a dispatcher watcher reference. There must be a global reference holder
101cdf0e10cSrcweir 		static DispatchWatcher* GetDispatchWatcher();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 		// execute new dispatch request
104cdf0e10cSrcweir 		sal_Bool executeDispatchRequests( const DispatchList& aDispatches, bool bNoTerminate = false );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	private:
107cdf0e10cSrcweir 		DispatchWatcher();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		static ::osl::Mutex&		GetMutex();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 		DispatchWatcherHashMap		m_aRequestContainer;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 		static ::osl::Mutex*		pWatcherMutex;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         sal_Int16                   m_nRequestCount;
116cdf0e10cSrcweir };
117cdf0e10cSrcweir 
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir #endif // _DESKTOP_DISPATCHWATCHER_HXX
121