xref: /aoo41x/main/uui/source/iahndl-locking.cxx (revision cdf0e10c)
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 #include <memory>
29 
30 #include "com/sun/star/document/ChangedByOthersRequest.hpp"
31 #include "com/sun/star/document/LockedDocumentRequest.hpp"
32 #include "com/sun/star/document/LockedOnSavingRequest.hpp"
33 #include "com/sun/star/document/LockFileIgnoreRequest.hpp"
34 #include "com/sun/star/document/OwnLockOnDocumentRequest.hpp"
35 #include "com/sun/star/task/XInteractionApprove.hpp"
36 #include "com/sun/star/task/XInteractionDisapprove.hpp"
37 #include "com/sun/star/task/XInteractionAbort.hpp"
38 #include "com/sun/star/task/XInteractionRequest.hpp"
39 
40 #include "vos/mutex.hxx"
41 #include "vcl/svapp.hxx"
42 #include "vcl/msgbox.hxx"
43 
44 #include "ids.hrc"
45 #include "getcontinuations.hxx"
46 #include "openlocked.hxx"
47 #include "trylater.hxx"
48 #include "alreadyopen.hxx"
49 #include "filechanged.hxx"
50 #include "lockfailed.hxx"
51 
52 #include "iahndl.hxx"
53 
54 #define UUI_DOC_LOAD_LOCK       0
55 #define UUI_DOC_OWN_LOAD_LOCK   1
56 #define UUI_DOC_SAVE_LOCK       2
57 #define UUI_DOC_OWN_SAVE_LOCK   3
58 
59 using namespace com::sun::star;
60 
61 namespace {
62 
63 void
64 handleLockedDocumentRequest_(
65     Window * pParent,
66     const ::rtl::OUString& aDocumentURL,
67     const ::rtl::OUString& aInfo,
68     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
69         rContinuations,
70     sal_uInt16 nMode )
71     SAL_THROW((uno::RuntimeException))
72 {
73     uno::Reference< task::XInteractionApprove > xApprove;
74     uno::Reference< task::XInteractionDisapprove > xDisapprove;
75     uno::Reference< task::XInteractionAbort > xAbort;
76     getContinuations(rContinuations, &xApprove, &xDisapprove, &xAbort);
77 
78     if ( !xApprove.is() || !xDisapprove.is() || !xAbort.is() )
79         return;
80 
81     try
82     {
83         vos::OGuard aGuard(Application::GetSolarMutex());
84         std::auto_ptr< ResMgr > xManager(
85             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
86         if (!xManager.get())
87             return;
88 
89         ::rtl::OUString aMessage;
90         std::vector< rtl::OUString > aArguments;
91         aArguments.push_back( aDocumentURL );
92 
93         sal_Int32 nResult = RET_CANCEL;
94         if ( nMode == UUI_DOC_LOAD_LOCK )
95         {
96             aArguments.push_back( aInfo.getLength()
97                                   ? aInfo
98                                   : ::rtl::OUString( String(
99                                         ResId( STR_UNKNOWNUSER,
100                                                *xManager.get() ) ) ) );
101             aMessage = String( ResId( STR_OPENLOCKED_MSG, *xManager.get() ) );
102             aMessage = UUIInteractionHelper::replaceMessageWithArguments(
103                 aMessage, aArguments );
104 
105             std::auto_ptr< OpenLockedQueryBox > xDialog(new OpenLockedQueryBox(
106                             pParent, xManager.get(), aMessage ) );
107             nResult = xDialog->Execute();
108         }
109         else if ( nMode == UUI_DOC_SAVE_LOCK )
110         {
111             aArguments.push_back( aInfo.getLength()
112                                   ? aInfo
113                                   : ::rtl::OUString( String(
114                                         ResId( STR_UNKNOWNUSER,
115                                                *xManager.get() ) ) ) );
116             aMessage = String( ResId( STR_TRYLATER_MSG, *xManager.get() ) );
117             aMessage = UUIInteractionHelper::replaceMessageWithArguments(
118                 aMessage, aArguments );
119 
120             std::auto_ptr< TryLaterQueryBox > xDialog(
121                 new TryLaterQueryBox( pParent, xManager.get(), aMessage ) );
122             nResult = xDialog->Execute();
123         }
124         else if ( nMode == UUI_DOC_OWN_LOAD_LOCK ||
125                   nMode == UUI_DOC_OWN_SAVE_LOCK )
126         {
127             aArguments.push_back( aInfo );
128             aMessage = String( ResId( nMode == UUI_DOC_OWN_SAVE_LOCK
129                                           ? STR_ALREADYOPEN_SAVE_MSG
130                                           : STR_ALREADYOPEN_MSG,
131                                       *xManager.get() ) );
132             aMessage = UUIInteractionHelper::replaceMessageWithArguments(
133                 aMessage, aArguments );
134 
135             std::auto_ptr< AlreadyOpenQueryBox > xDialog(
136                 new AlreadyOpenQueryBox( pParent,
137                                          xManager.get(),
138                                          aMessage,
139                                          nMode == UUI_DOC_OWN_SAVE_LOCK ) );
140             nResult = xDialog->Execute();
141         }
142 
143         if ( nResult == RET_YES )
144             xApprove->select();
145         else if ( nResult == RET_NO )
146             xDisapprove->select();
147         else
148             xAbort->select();
149     }
150     catch (std::bad_alloc const &)
151     {
152         throw uno::RuntimeException(
153                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
154                   uno::Reference< uno::XInterface >());
155     }
156 }
157 
158 void
159 handleChangedByOthersRequest_(
160     Window * pParent,
161     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
162         rContinuations )
163     SAL_THROW((uno::RuntimeException))
164 {
165     uno::Reference< task::XInteractionApprove > xApprove;
166     uno::Reference< task::XInteractionAbort > xAbort;
167     getContinuations(rContinuations, &xApprove, &xAbort);
168 
169     if ( !xApprove.is() || !xAbort.is() )
170         return;
171 
172     try
173     {
174         vos::OGuard aGuard(Application::GetSolarMutex());
175         std::auto_ptr< ResMgr > xManager(
176             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
177         if (!xManager.get())
178             return;
179 
180         std::auto_ptr< FileChangedQueryBox > xDialog(
181             new FileChangedQueryBox( pParent, xManager.get() ) );
182         sal_Int32 nResult = xDialog->Execute();
183 
184         if ( nResult == RET_YES )
185             xApprove->select();
186         else
187             xAbort->select();
188     }
189     catch (std::bad_alloc const &)
190     {
191         throw uno::RuntimeException(
192                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
193                   uno::Reference< uno::XInterface >());
194     }
195 }
196 
197 void
198 handleLockFileIgnoreRequest_(
199     Window * pParent,
200     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
201         rContinuations )
202     SAL_THROW((uno::RuntimeException))
203 {
204     uno::Reference< task::XInteractionApprove > xApprove;
205     uno::Reference< task::XInteractionAbort > xAbort;
206     getContinuations(rContinuations, &xApprove, &xAbort);
207 
208     if ( !xApprove.is() || !xAbort.is() )
209         return;
210 
211     try
212     {
213         vos::OGuard aGuard(Application::GetSolarMutex());
214         std::auto_ptr< ResMgr > xManager(
215             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
216         if (!xManager.get())
217             return;
218 
219         std::auto_ptr< LockFailedQueryBox > xDialog(
220             new LockFailedQueryBox( pParent, xManager.get() ) );
221         sal_Int32 nResult = xDialog->Execute();
222 
223         if ( nResult == RET_OK )
224             xApprove->select();
225         else
226             xAbort->select();
227     }
228     catch (std::bad_alloc const &)
229     {
230         throw uno::RuntimeException(
231                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
232                   uno::Reference< uno::XInterface >());
233     }
234 }
235 
236 } // namespace
237 
238 bool
239 UUIInteractionHelper::handleLockedDocumentRequest(
240     uno::Reference< task::XInteractionRequest > const & rRequest)
241     SAL_THROW((::com::sun::star::uno::RuntimeException))
242 {
243     uno::Any aAnyRequest(rRequest->getRequest());
244 
245     document::LockedDocumentRequest aLockedDocumentRequest;
246     if (aAnyRequest >>= aLockedDocumentRequest )
247     {
248         handleLockedDocumentRequest_( getParentProperty(),
249                                       aLockedDocumentRequest.DocumentURL,
250                                       aLockedDocumentRequest.UserInfo,
251                                       rRequest->getContinuations(),
252                                       UUI_DOC_LOAD_LOCK );
253         return true;
254     }
255 
256     document::OwnLockOnDocumentRequest aOwnLockOnDocumentRequest;
257     if (aAnyRequest >>= aOwnLockOnDocumentRequest )
258     {
259         handleLockedDocumentRequest_( getParentProperty(),
260                                       aOwnLockOnDocumentRequest.DocumentURL,
261                                       aOwnLockOnDocumentRequest.TimeInfo,
262                                       rRequest->getContinuations(),
263                                       aOwnLockOnDocumentRequest.IsStoring
264                                           ? UUI_DOC_OWN_SAVE_LOCK
265                                           : UUI_DOC_OWN_LOAD_LOCK );
266         return true;
267     }
268 
269     document::LockedOnSavingRequest aLockedOnSavingRequest;
270     if (aAnyRequest >>= aLockedOnSavingRequest )
271     {
272         handleLockedDocumentRequest_( getParentProperty(),
273                                       aLockedOnSavingRequest.DocumentURL,
274                                       aLockedOnSavingRequest.UserInfo,
275                                       rRequest->getContinuations(),
276                                       UUI_DOC_SAVE_LOCK );
277         return true;
278     }
279     return false;
280 }
281 
282 bool
283 UUIInteractionHelper::handleChangedByOthersRequest(
284     uno::Reference< task::XInteractionRequest > const & rRequest)
285     SAL_THROW((uno::RuntimeException))
286 {
287     uno::Any aAnyRequest(rRequest->getRequest());
288 
289     document::ChangedByOthersRequest aChangedByOthersRequest;
290     if (aAnyRequest >>= aChangedByOthersRequest )
291     {
292         handleChangedByOthersRequest_( getParentProperty(),
293                                        rRequest->getContinuations() );
294         return true;
295     }
296     return false;
297 }
298 
299 bool
300 UUIInteractionHelper::handleLockFileIgnoreRequest(
301     uno::Reference< task::XInteractionRequest > const & rRequest)
302     SAL_THROW((uno::RuntimeException))
303 {
304     uno::Any aAnyRequest(rRequest->getRequest());
305 
306     document::LockFileIgnoreRequest aLockFileIgnoreRequest;
307     if (aAnyRequest >>= aLockFileIgnoreRequest )
308     {
309         handleLockFileIgnoreRequest_( getParentProperty(),
310                                       rRequest->getContinuations() );
311         return true;
312     }
313     return false;
314 }
315 
316 
317