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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #include "interaction/quietinteraction.hxx"
28 
29 //_________________________________________________________________________________________________________________
30 //	my own includes
31 //_________________________________________________________________________________________________________________
32 #include <threadhelp/readguard.hxx>
33 #include <threadhelp/writeguard.hxx>
34 #include <macros/generic.hxx>
35 #include <macros/debug.hxx>
36 
37 //_________________________________________________________________________________________________________________
38 //	interface includes
39 //_________________________________________________________________________________________________________________
40 #include <com/sun/star/task/XInteractionAbort.hpp>
41 #include <com/sun/star/task/XInteractionApprove.hpp>
42 #include <com/sun/star/document/XInteractionFilterSelect.hpp>
43 #include <com/sun/star/document/XInteractionFilterOptions.hpp>
44 #include <com/sun/star/document/AmbigousFilterRequest.hpp>
45 #include <com/sun/star/document/FilterOptionsRequest.hpp>
46 #include <com/sun/star/task/ErrorCodeRequest.hpp>
47 
48 #ifndef _COM_SUN_STAR_DOCUMENT_LOCKEDDOCUMENTREQUEST_HPP_
49 #include <com/sun/star/document/LockedDocumentRequest.hpp>
50 #endif
51 
52 //_________________________________________________________________________________________________________________
53 //	other includes
54 //_________________________________________________________________________________________________________________
55 #include <vcl/svapp.hxx>
56 
57 #ifndef __RSC
58 #include <tools/errinf.hxx>
59 #endif
60 
61 //_________________________________________________________________________________________________________________
62 //	namespace
63 //_________________________________________________________________________________________________________________
64 
65 namespace framework{
66 
67 //_________________________________________________________________________________________________________________
68 //	exported const
69 //_________________________________________________________________________________________________________________
70 
71 //_________________________________________________________________________________________________________________
72 //	exported definitions
73 //_________________________________________________________________________________________________________________
74 
75 DEFINE_XINTERFACE_2( QuietInteraction                                 ,
76                      OWeakObject                                      ,
77                      DIRECT_INTERFACE(css::lang::XTypeProvider      ) ,
78                      DIRECT_INTERFACE(css::task::XInteractionHandler) )
79 
80 DEFINE_XTYPEPROVIDER_2( QuietInteraction               ,
81                         css::lang::XTypeProvider       ,
82                         css::task::XInteractionHandler )
83 
84 //_________________________________________________________________________________________________________________
85 
86 QuietInteraction::QuietInteraction()
87     : ThreadHelpBase     ( &Application::GetSolarMutex() )
88     , ::cppu::OWeakObject(                               )
89     , m_aRequest         (                               )
90 {
91 }
92 
93 //_________________________________________________________________________________________________________________
94 
95 void SAL_CALL QuietInteraction::handle( const css::uno::Reference< css::task::XInteractionRequest >& xRequest ) throw( css::uno::RuntimeException )
96 {
97     // safe the request for outside analyzing everytime!
98     css::uno::Any aRequest = xRequest->getRequest();
99     /* SAFE { */
100     WriteGuard aWriteLock(m_aLock);
101     m_aRequest = aRequest;
102     aWriteLock.unlock();
103     /* } SAFE */
104 
105     // analyze the request
106     // We need XAbort as possible continuation as minimum!
107     // An optional filter selection we can handle too.
108     css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
109     css::uno::Reference< css::task::XInteractionAbort >                              xAbort     ;
110     css::uno::Reference< css::task::XInteractionApprove >                            xApprove   ;
111     css::uno::Reference< css::document::XInteractionFilterSelect >                   xFilter    ;
112     css::uno::Reference< css::document::XInteractionFilterOptions >                  xFOptions  ;
113 
114     sal_Int32 nCount=lContinuations.getLength();
115     for (sal_Int32 i=0; i<nCount; ++i)
116     {
117         if ( ! xAbort.is() )
118             xAbort = css::uno::Reference< css::task::XInteractionAbort >( lContinuations[i], css::uno::UNO_QUERY );
119 
120         if( ! xApprove.is() )
121             xApprove  = css::uno::Reference< css::task::XInteractionApprove >( lContinuations[i], css::uno::UNO_QUERY );
122 
123         if ( ! xFilter.is() )
124             xFilter = css::uno::Reference< css::document::XInteractionFilterSelect >( lContinuations[i], css::uno::UNO_QUERY );
125 
126         if ( ! xFOptions.is() )
127             xFOptions = css::uno::Reference< css::document::XInteractionFilterOptions >( lContinuations[i], css::uno::UNO_QUERY );
128     }
129 
130     // differ between abortable interactions (error, unknown filter ...)
131     // and other ones (ambigous but not unknown filter ...)
132     css::task::ErrorCodeRequest          aErrorCodeRequest     ;
133     css::document::AmbigousFilterRequest aAmbigousFilterRequest;
134     css::document::LockedDocumentRequest aLockedDocumentRequest;
135     css::document::FilterOptionsRequest  aFilterOptionsRequest;
136 
137     if (aRequest>>=aAmbigousFilterRequest)
138     {
139         if (xFilter.is())
140         {
141             // user selected filter wins everytime!
142             xFilter->setFilter( aAmbigousFilterRequest.SelectedFilter );
143             xFilter->select();
144         }
145     }
146     else
147     if( aRequest >>= aErrorCodeRequest )
148     {
149         // warnings can be ignored   => approve
150         // errors must break loading => abort
151         sal_Bool bWarning = (aErrorCodeRequest.ErrCode & ERRCODE_WARNING_MASK) == ERRCODE_WARNING_MASK;
152         if (xApprove.is() && bWarning)
153             xApprove->select();
154         else
155         if (xAbort.is())
156             xAbort->select();
157     }
158     else
159     if( aRequest >>= aLockedDocumentRequest )
160     {
161         // the locked document should be opened readonly by default
162         if (xApprove.is())
163             xApprove->select();
164         else
165         if (xAbort.is())
166             xAbort->select();
167     }
168     else
169     if (aRequest>>=aFilterOptionsRequest)
170     {
171         if (xFOptions.is())
172         {
173             // let the default filter options be used
174             xFOptions->select();
175         }
176     }
177     else
178     if (xAbort.is())
179         xAbort->select();
180 }
181 
182 //_________________________________________________________________________________________________________________
183 
184 css::uno::Any QuietInteraction::getRequest() const
185 {
186     /* SAFE { */
187     ReadGuard aReadLock(m_aLock);
188     return m_aRequest;
189     /* } SAFE */
190 }
191 
192 //_________________________________________________________________________________________________________________
193 
194 sal_Bool QuietInteraction::wasUsed() const
195 {
196     /* SAFE { */
197     ReadGuard aReadLock(m_aLock);
198     return m_aRequest.hasValue();
199     /* } SAFE */
200 }
201 
202 } // namespace framework
203