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_dbmm.hxx"
26 
27 #include "docinteraction.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/frame/XModel.hpp>
31 #include <com/sun/star/task/DocumentPasswordRequest.hpp>
32 /** === end UNO includes === **/
33 
34 #include <comphelper/componentcontext.hxx>
35 #include <comphelper/namedvaluecollection.hxx>
36 #include <comphelper/interaction.hxx>
37 #include <rtl/ref.hxx>
38 #include <tools/diagnose_ex.h>
39 
40 //........................................................................
41 namespace dbmm
42 {
43 //........................................................................
44 
45 	/** === begin UNO using === **/
46 	using ::com::sun::star::uno::Reference;
47 	using ::com::sun::star::uno::XInterface;
48 	using ::com::sun::star::uno::UNO_QUERY;
49 	using ::com::sun::star::uno::UNO_QUERY_THROW;
50 	using ::com::sun::star::uno::UNO_SET_THROW;
51 	using ::com::sun::star::uno::Exception;
52 	using ::com::sun::star::uno::RuntimeException;
53 	using ::com::sun::star::uno::Any;
54 	using ::com::sun::star::uno::makeAny;
55     using ::com::sun::star::task::XInteractionHandler;
56     using ::com::sun::star::frame::XModel;
57     using ::com::sun::star::task::DocumentPasswordRequest;
58     using ::com::sun::star::task::InteractionClassification_QUERY;
59     using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER;
60     using ::com::sun::star::task::PasswordRequestMode_PASSWORD_REENTER;
61 	/** === end UNO using === **/
62 
63 	//====================================================================
64 	//= InteractionHandler_Data
65 	//====================================================================
66     struct InteractionHandler_Data
67     {
68         Reference< XInteractionHandler >    xHandler;
69 
InteractionHandler_Datadbmm::InteractionHandler_Data70         InteractionHandler_Data( const Reference< XInteractionHandler >& _rxHandler )
71             :xHandler( _rxHandler )
72         {
73         }
74 
InteractionHandler_Datadbmm::InteractionHandler_Data75         InteractionHandler_Data( const ::comphelper::ComponentContext& _rContext )
76             :xHandler( _rContext.createComponent( "com.sun.star.task.InteractionHandler" ), UNO_QUERY_THROW )
77         {
78         }
79     };
80 
81 	//====================================================================
82 	//= InteractionHandler
83 	//====================================================================
84 	//--------------------------------------------------------------------
InteractionHandler(const::comphelper::ComponentContext & _rContext,const Reference<XModel> & _rxDocument)85     InteractionHandler::InteractionHandler( const ::comphelper::ComponentContext& _rContext, const Reference< XModel >& _rxDocument )
86         :m_pData( new InteractionHandler_Data( _rContext ) )
87     {
88         // check whether the doumentc has an own interaction handler set
89         ::comphelper::NamedValueCollection aDocArgs( _rxDocument->getArgs() );
90         m_pData->xHandler = aDocArgs.getOrDefault( "InteractionHandler", m_pData->xHandler );
91     }
92 
93 	//--------------------------------------------------------------------
~InteractionHandler()94     InteractionHandler::~InteractionHandler()
95     {
96     }
97 
98 	//--------------------------------------------------------------------
requestDocumentPassword(const::rtl::OUString & _rDocumentName,::rtl::OUString & _io_rPassword)99     bool InteractionHandler::requestDocumentPassword( const ::rtl::OUString& _rDocumentName, ::rtl::OUString& _io_rPassword )
100     {
101         // create request
102         DocumentPasswordRequest aRequest(
103             ::rtl::OUString(), NULL,
104             InteractionClassification_QUERY,
105             _io_rPassword.getLength() ? PasswordRequestMode_PASSWORD_REENTER : PasswordRequestMode_PASSWORD_ENTER,
106             _rDocumentName
107         );
108 
109         ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( makeAny( aRequest ) ) );
110         ::rtl::Reference< ::comphelper::OInteractionPassword > pPassword( new ::comphelper::OInteractionPassword( _io_rPassword ) );
111         ::rtl::Reference< ::comphelper::OInteractionAbort > pAbort( new ::comphelper::OInteractionAbort );
112         pRequest->addContinuation( pPassword.get() );
113         pRequest->addContinuation( pAbort.get() );
114 
115         // handle
116         m_pData->xHandler->handle( pRequest.get() );
117 
118         // finish up
119         if ( pAbort->wasSelected() )
120             return false;
121 
122         _io_rPassword = pPassword->getPassword();
123         return true;
124     }
125 
126 	//--------------------------------------------------------------------
reportError(const Any & _rError)127     void InteractionHandler::reportError( const Any& _rError )
128     {
129         ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( _rError ) );
130         ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
131         pRequest->addContinuation( pApprove.get() );
132 
133         m_pData->xHandler->handle( pRequest.get() );
134     }
135 
136 //........................................................................
137 } // namespace dbmm
138 //........................................................................
139