1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _DESKTOP_DISPATCHWATCHER_HXX
25 #define _DESKTOP_DISPATCHWATCHER_HXX
26 
27 #include <cppuhelper/implbase1.hxx>
28 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
29 #include <com/sun/star/frame/XDispatchResultListener.hpp>
30 
31 #include "officeipcthread.hxx"
32 #include <hash_map>
33 #include <vector>
34 
35 namespace desktop
36 {
37 
38 /*
39 	Class for controls dispatching of command URL through office command line. There
40 	are "dangerous" command URLs, that can result in a running office without UI. To prevent
41 	this situation the implementation surveile all dispatches and looks for an open task if
42 	there is arose a problem. If there is none the office will be shutdown to prevent a
43 	running office without UI.
44 */
45 
46 struct OUStringHashCode
47 {
operator ()desktop::OUStringHashCode48     size_t operator()( const ::rtl::OUString& sString ) const
49 	{
50 		return sString.hashCode();
51 	}
52 };
53 
54 class DispatchWatcherHashMap : public ::std::hash_map< ::rtl::OUString, sal_Int32, OUStringHashCode, ::std::equal_to< ::rtl::OUString >	>
55 {
56 	public:
free()57 		inline void free()
58 		{
59 			DispatchWatcherHashMap().swap( *this );
60 		}
61 };
62 
63 class DispatchWatcher : public ::cppu::WeakImplHelper1< ::com::sun::star::frame::XDispatchResultListener >
64 {
65 	public:
66 		enum RequestType
67 		{
68 			REQUEST_OPEN,
69 			REQUEST_VIEW,
70             REQUEST_START,
71 			REQUEST_PRINT,
72 			REQUEST_PRINTTO,
73 			REQUEST_FORCEOPEN,
74 			REQUEST_FORCENEW
75 		};
76 
77 		struct DispatchRequest
78 		{
DispatchRequestdesktop::DispatchWatcher::DispatchRequest79             DispatchRequest( RequestType aType, const ::rtl::OUString& aFile, boost::optional< rtl::OUString > const & cwdUrl, const ::rtl::OUString& aPrinter, const ::rtl::OUString& aFact ) :
80                 aRequestType( aType ), aURL( aFile ), aCwdUrl( cwdUrl ), aPrinterName( aPrinter ), aPreselectedFactory( aFact ) {}
81 
82 			RequestType		aRequestType;
83 			rtl::OUString	aURL;
84             boost::optional< rtl::OUString > aCwdUrl;
85 			rtl::OUString	aPrinterName;
86             rtl::OUString   aPreselectedFactory;
87 		};
88 
89 		typedef std::vector< DispatchRequest > DispatchList;
90 
91 		virtual ~DispatchWatcher();
92 
93 		// XEventListener
94 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source )
95 			throw(::com::sun::star::uno::RuntimeException);
96 
97         // XDispachResultListener
98         virtual void SAL_CALL dispatchFinished( const com::sun::star::frame::DispatchResultEvent& aEvent ) throw( com::sun::star::uno::RuntimeException );
99 
100 		// Access function to get a dispatcher watcher reference. There must be a global reference holder
101 		static DispatchWatcher* GetDispatchWatcher();
102 
103 		// execute new dispatch request
104 		sal_Bool executeDispatchRequests( const DispatchList& aDispatches, bool bNoTerminate = false );
105 
106 	private:
107 		DispatchWatcher();
108 
109 		static ::osl::Mutex&		GetMutex();
110 
111 		DispatchWatcherHashMap		m_aRequestContainer;
112 
113 		static ::osl::Mutex*		pWatcherMutex;
114 
115         sal_Int16                   m_nRequestCount;
116 };
117 
118 }
119 
120 #endif // _DESKTOP_DISPATCHWATCHER_HXX
121