1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 #include "interaction/quietinteraction.hxx"
32 
33 //_________________________________________________________________________________________________________________
34 //	my own includes
35 //_________________________________________________________________________________________________________________
36 #include <threadhelp/readguard.hxx>
37 #include <threadhelp/writeguard.hxx>
38 #include <macros/generic.hxx>
39 #include <macros/debug.hxx>
40 
41 //_________________________________________________________________________________________________________________
42 //	interface includes
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/task/XInteractionAbort.hpp>
45 #include <com/sun/star/task/XInteractionApprove.hpp>
46 #include <com/sun/star/document/XInteractionFilterSelect.hpp>
47 #include <com/sun/star/document/XInteractionFilterOptions.hpp>
48 #include <com/sun/star/document/AmbigousFilterRequest.hpp>
49 #include <com/sun/star/document/FilterOptionsRequest.hpp>
50 #include <com/sun/star/task/ErrorCodeRequest.hpp>
51 
52 #ifndef _COM_SUN_STAR_DOCUMENT_LOCKEDDOCUMENTREQUEST_HPP_
53 #include <com/sun/star/document/LockedDocumentRequest.hpp>
54 #endif
55 
56 //_________________________________________________________________________________________________________________
57 //	other includes
58 //_________________________________________________________________________________________________________________
59 #include <vcl/svapp.hxx>
60 
61 #ifndef __RSC
62 #include <tools/errinf.hxx>
63 #endif
64 
65 //_________________________________________________________________________________________________________________
66 //	namespace
67 //_________________________________________________________________________________________________________________
68 
69 namespace framework{
70 
71 //_________________________________________________________________________________________________________________
72 //	exported const
73 //_________________________________________________________________________________________________________________
74 
75 //_________________________________________________________________________________________________________________
76 //	exported definitions
77 //_________________________________________________________________________________________________________________
78 
79 DEFINE_XINTERFACE_2( QuietInteraction                                 ,
80                      OWeakObject                                      ,
81                      DIRECT_INTERFACE(css::lang::XTypeProvider      ) ,
82                      DIRECT_INTERFACE(css::task::XInteractionHandler) )
83 
84 DEFINE_XTYPEPROVIDER_2( QuietInteraction               ,
85                         css::lang::XTypeProvider       ,
86                         css::task::XInteractionHandler )
87 
88 //_________________________________________________________________________________________________________________
89 
90 QuietInteraction::QuietInteraction()
91     : ThreadHelpBase     ( &Application::GetSolarMutex() )
92     , ::cppu::OWeakObject(                               )
93     , m_aRequest         (                               )
94 {
95 }
96 
97 //_________________________________________________________________________________________________________________
98 
99 void SAL_CALL QuietInteraction::handle( const css::uno::Reference< css::task::XInteractionRequest >& xRequest ) throw( css::uno::RuntimeException )
100 {
101     // safe the request for outside analyzing everytime!
102     css::uno::Any aRequest = xRequest->getRequest();
103     /* SAFE { */
104     WriteGuard aWriteLock(m_aLock);
105     m_aRequest = aRequest;
106     aWriteLock.unlock();
107     /* } SAFE */
108 
109     // analyze the request
110     // We need XAbort as possible continuation as minimum!
111     // An optional filter selection we can handle too.
112     css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
113     css::uno::Reference< css::task::XInteractionAbort >                              xAbort     ;
114     css::uno::Reference< css::task::XInteractionApprove >                            xApprove   ;
115     css::uno::Reference< css::document::XInteractionFilterSelect >                   xFilter    ;
116     css::uno::Reference< css::document::XInteractionFilterOptions >                  xFOptions  ;
117 
118     sal_Int32 nCount=lContinuations.getLength();
119     for (sal_Int32 i=0; i<nCount; ++i)
120     {
121         if ( ! xAbort.is() )
122             xAbort = css::uno::Reference< css::task::XInteractionAbort >( lContinuations[i], css::uno::UNO_QUERY );
123 
124         if( ! xApprove.is() )
125             xApprove  = css::uno::Reference< css::task::XInteractionApprove >( lContinuations[i], css::uno::UNO_QUERY );
126 
127         if ( ! xFilter.is() )
128             xFilter = css::uno::Reference< css::document::XInteractionFilterSelect >( lContinuations[i], css::uno::UNO_QUERY );
129 
130         if ( ! xFOptions.is() )
131             xFOptions = css::uno::Reference< css::document::XInteractionFilterOptions >( lContinuations[i], css::uno::UNO_QUERY );
132     }
133 
134     // differ between abortable interactions (error, unknown filter ...)
135     // and other ones (ambigous but not unknown filter ...)
136     css::task::ErrorCodeRequest          aErrorCodeRequest     ;
137     css::document::AmbigousFilterRequest aAmbigousFilterRequest;
138     css::document::LockedDocumentRequest aLockedDocumentRequest;
139     css::document::FilterOptionsRequest  aFilterOptionsRequest;
140 
141     if (aRequest>>=aAmbigousFilterRequest)
142     {
143         if (xFilter.is())
144         {
145             // user selected filter wins everytime!
146             xFilter->setFilter( aAmbigousFilterRequest.SelectedFilter );
147             xFilter->select();
148         }
149     }
150     else
151     if( aRequest >>= aErrorCodeRequest )
152     {
153         // warnings can be ignored   => approve
154         // errors must break loading => abort
155         sal_Bool bWarning = (aErrorCodeRequest.ErrCode & ERRCODE_WARNING_MASK) == ERRCODE_WARNING_MASK;
156         if (xApprove.is() && bWarning)
157             xApprove->select();
158         else
159         if (xAbort.is())
160             xAbort->select();
161     }
162     else
163     if( aRequest >>= aLockedDocumentRequest )
164     {
165         // the locked document should be opened readonly by default
166         if (xApprove.is())
167             xApprove->select();
168         else
169         if (xAbort.is())
170             xAbort->select();
171     }
172     else
173     if (aRequest>>=aFilterOptionsRequest)
174     {
175         if (xFOptions.is())
176         {
177             // let the default filter options be used
178             xFOptions->select();
179         }
180     }
181     else
182     if (xAbort.is())
183         xAbort->select();
184 }
185 
186 //_________________________________________________________________________________________________________________
187 
188 css::uno::Any QuietInteraction::getRequest() const
189 {
190     /* SAFE { */
191     ReadGuard aReadLock(m_aLock);
192     return m_aRequest;
193     /* } SAFE */
194 }
195 
196 //_________________________________________________________________________________________________________________
197 
198 sal_Bool QuietInteraction::wasUsed() const
199 {
200     /* SAFE { */
201     ReadGuard aReadLock(m_aLock);
202     return m_aRequest.hasValue();
203     /* } SAFE */
204 }
205 
206 } // namespace framework
207