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