Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "docinteraction.hxx"
21 :
22 : #include <com/sun/star/frame/XModel.hpp>
23 : #include <com/sun/star/task/InteractionHandler.hpp>
24 : #include <com/sun/star/task/DocumentPasswordRequest.hpp>
25 :
26 : #include <comphelper/namedvaluecollection.hxx>
27 : #include <comphelper/interaction.hxx>
28 : #include <rtl/ref.hxx>
29 : #include <tools/diagnose_ex.h>
30 :
31 : namespace dbmm
32 : {
33 :
34 : using ::com::sun::star::uno::Reference;
35 : using ::com::sun::star::uno::XInterface;
36 : using ::com::sun::star::uno::UNO_QUERY;
37 : using ::com::sun::star::uno::UNO_QUERY_THROW;
38 : using ::com::sun::star::uno::UNO_SET_THROW;
39 : using ::com::sun::star::uno::Exception;
40 : using ::com::sun::star::uno::RuntimeException;
41 : using ::com::sun::star::uno::Any;
42 : using ::com::sun::star::uno::makeAny;
43 : using ::com::sun::star::uno::XComponentContext;
44 : using ::com::sun::star::task::XInteractionHandler;
45 : using ::com::sun::star::frame::XModel;
46 : using ::com::sun::star::task::DocumentPasswordRequest;
47 : using ::com::sun::star::task::InteractionClassification_QUERY;
48 : using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER;
49 : using ::com::sun::star::task::PasswordRequestMode_PASSWORD_REENTER;
50 :
51 : // InteractionHandler_Data
52 0 : struct InteractionHandler_Data
53 : {
54 : Reference< XInteractionHandler > xHandler;
55 :
56 0 : InteractionHandler_Data( const Reference<XComponentContext>& _rContext )
57 0 : :xHandler( ::com::sun::star::task::InteractionHandler::createWithParent(_rContext, 0), UNO_QUERY )
58 : {
59 0 : }
60 : };
61 :
62 : // InteractionHandler
63 0 : InteractionHandler::InteractionHandler( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
64 0 : :m_pData( new InteractionHandler_Data( _rContext ) )
65 : {
66 : // check whether the doumentc has an own interaction handler set
67 0 : ::comphelper::NamedValueCollection aDocArgs( _rxDocument->getArgs() );
68 0 : m_pData->xHandler = aDocArgs.getOrDefault( "InteractionHandler", m_pData->xHandler );
69 0 : }
70 :
71 0 : InteractionHandler::~InteractionHandler()
72 : {
73 0 : }
74 :
75 0 : bool InteractionHandler::requestDocumentPassword( const OUString& _rDocumentName, OUString& _io_rPassword )
76 : {
77 : // create request
78 : DocumentPasswordRequest aRequest(
79 : OUString(), NULL,
80 : InteractionClassification_QUERY,
81 0 : _io_rPassword.isEmpty() ? PasswordRequestMode_PASSWORD_ENTER : PasswordRequestMode_PASSWORD_REENTER,
82 : _rDocumentName
83 0 : );
84 :
85 0 : ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( makeAny( aRequest ) ) );
86 0 : ::rtl::Reference< ::comphelper::OInteractionPassword > pPassword( new ::comphelper::OInteractionPassword( _io_rPassword ) );
87 0 : ::rtl::Reference< ::comphelper::OInteractionAbort > pAbort( new ::comphelper::OInteractionAbort );
88 0 : pRequest->addContinuation( pPassword.get() );
89 0 : pRequest->addContinuation( pAbort.get() );
90 :
91 : // handle
92 0 : m_pData->xHandler->handle( pRequest.get() );
93 :
94 : // finish up
95 0 : if ( pAbort->wasSelected() )
96 0 : return false;
97 :
98 0 : _io_rPassword = pPassword->getPassword();
99 0 : return true;
100 : }
101 :
102 0 : void InteractionHandler::reportError( const Any& _rError )
103 : {
104 0 : ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( _rError ) );
105 0 : ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
106 0 : pRequest->addContinuation( pApprove.get() );
107 :
108 0 : m_pData->xHandler->handle( pRequest.get() );
109 0 : }
110 :
111 : } // namespace dbmm
112 :
113 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|