Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / uui / source / iahndl-locking.cxx
blob57faaa11e6cd4373bfbe3ae7a5460e3f1df7d4aa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
22 #include "com/sun/star/document/ChangedByOthersRequest.hpp"
23 #include "com/sun/star/document/LockedDocumentRequest.hpp"
24 #include "com/sun/star/document/LockedOnSavingRequest.hpp"
25 #include "com/sun/star/document/LockFileIgnoreRequest.hpp"
26 #include "com/sun/star/document/OwnLockOnDocumentRequest.hpp"
27 #include "com/sun/star/task/XInteractionApprove.hpp"
28 #include "com/sun/star/task/XInteractionDisapprove.hpp"
29 #include "com/sun/star/task/XInteractionAbort.hpp"
30 #include "com/sun/star/task/XInteractionRequest.hpp"
32 #include "osl/mutex.hxx"
33 #include "vcl/svapp.hxx"
34 #include "vcl/msgbox.hxx"
36 #include "ids.hrc"
37 #include "getcontinuations.hxx"
38 #include "openlocked.hxx"
39 #include "trylater.hxx"
40 #include "alreadyopen.hxx"
41 #include "filechanged.hxx"
42 #include "lockfailed.hxx"
44 #include "iahndl.hxx"
46 #include <boost/scoped_ptr.hpp>
48 #define UUI_DOC_LOAD_LOCK 0
49 #define UUI_DOC_OWN_LOAD_LOCK 1
50 #define UUI_DOC_SAVE_LOCK 2
51 #define UUI_DOC_OWN_SAVE_LOCK 3
53 using namespace com::sun::star;
55 namespace {
57 void
58 handleLockedDocumentRequest_(
59 Window * pParent,
60 const ::rtl::OUString& aDocumentURL,
61 const ::rtl::OUString& aInfo,
62 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
63 rContinuations,
64 sal_uInt16 nMode )
65 SAL_THROW((uno::RuntimeException))
67 uno::Reference< task::XInteractionApprove > xApprove;
68 uno::Reference< task::XInteractionDisapprove > xDisapprove;
69 uno::Reference< task::XInteractionAbort > xAbort;
70 getContinuations(rContinuations, &xApprove, &xDisapprove, &xAbort);
72 if ( !xApprove.is() || !xDisapprove.is() || !xAbort.is() )
73 return;
75 try
77 SolarMutexGuard aGuard;
78 boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
79 if (!xManager.get())
80 return;
82 ::rtl::OUString aMessage;
83 std::vector< rtl::OUString > aArguments;
84 aArguments.push_back( aDocumentURL );
86 sal_Int32 nResult = RET_CANCEL;
87 if ( nMode == UUI_DOC_LOAD_LOCK )
89 aArguments.push_back( !aInfo.isEmpty()
90 ? aInfo
91 : ResId( STR_UNKNOWNUSER,
92 *xManager.get() ).toString() );
93 aMessage = ResId(STR_OPENLOCKED_MSG, *xManager.get()).toString();
94 aMessage = UUIInteractionHelper::replaceMessageWithArguments(
95 aMessage, aArguments );
97 boost::scoped_ptr< OpenLockedQueryBox > xDialog(new OpenLockedQueryBox(
98 pParent, xManager.get(), aMessage ) );
99 nResult = xDialog->Execute();
101 else if ( nMode == UUI_DOC_SAVE_LOCK )
103 aArguments.push_back( !aInfo.isEmpty()
104 ? aInfo
105 : ResId( STR_UNKNOWNUSER,
106 *xManager.get() ).toString() );
107 aMessage = ResId(STR_TRYLATER_MSG, *xManager.get()).toString();
108 aMessage = UUIInteractionHelper::replaceMessageWithArguments(
109 aMessage, aArguments );
111 boost::scoped_ptr< TryLaterQueryBox > xDialog(
112 new TryLaterQueryBox( pParent, xManager.get(), aMessage ) );
113 nResult = xDialog->Execute();
115 else if ( nMode == UUI_DOC_OWN_LOAD_LOCK ||
116 nMode == UUI_DOC_OWN_SAVE_LOCK )
118 aArguments.push_back( aInfo );
119 aMessage = ResId(nMode == UUI_DOC_OWN_SAVE_LOCK
120 ? STR_ALREADYOPEN_SAVE_MSG
121 : STR_ALREADYOPEN_MSG,
122 *xManager.get() ).toString();
123 aMessage = UUIInteractionHelper::replaceMessageWithArguments(
124 aMessage, aArguments );
126 boost::scoped_ptr< AlreadyOpenQueryBox > xDialog(
127 new AlreadyOpenQueryBox( pParent,
128 xManager.get(),
129 aMessage,
130 nMode == UUI_DOC_OWN_SAVE_LOCK ) );
131 nResult = xDialog->Execute();
134 if ( nResult == RET_YES )
135 xApprove->select();
136 else if ( nResult == RET_NO )
137 xDisapprove->select();
138 else
139 xAbort->select();
141 catch (std::bad_alloc const &)
143 throw uno::RuntimeException(
144 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
145 uno::Reference< uno::XInterface >());
149 void
150 handleChangedByOthersRequest_(
151 Window * pParent,
152 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
153 rContinuations )
154 SAL_THROW((uno::RuntimeException))
156 uno::Reference< task::XInteractionApprove > xApprove;
157 uno::Reference< task::XInteractionAbort > xAbort;
158 getContinuations(rContinuations, &xApprove, &xAbort);
160 if ( !xApprove.is() || !xAbort.is() )
161 return;
165 SolarMutexGuard aGuard;
166 boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
167 if (!xManager.get())
168 return;
170 boost::scoped_ptr< FileChangedQueryBox > xDialog(
171 new FileChangedQueryBox( pParent, xManager.get() ) );
172 sal_Int32 nResult = xDialog->Execute();
174 if ( nResult == RET_YES )
175 xApprove->select();
176 else
177 xAbort->select();
179 catch (std::bad_alloc const &)
181 throw uno::RuntimeException(
182 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
183 uno::Reference< uno::XInterface >());
187 void
188 handleLockFileIgnoreRequest_(
189 Window * pParent,
190 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
191 rContinuations )
192 SAL_THROW((uno::RuntimeException))
194 uno::Reference< task::XInteractionApprove > xApprove;
195 uno::Reference< task::XInteractionAbort > xAbort;
196 getContinuations(rContinuations, &xApprove, &xAbort);
198 if ( !xApprove.is() || !xAbort.is() )
199 return;
203 SolarMutexGuard aGuard;
204 boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
205 if (!xManager.get())
206 return;
208 boost::scoped_ptr< LockFailedQueryBox > xDialog(
209 new LockFailedQueryBox( pParent, xManager.get() ) );
210 sal_Int32 nResult = xDialog->Execute();
212 if ( nResult == RET_OK )
213 xApprove->select();
214 else
215 xAbort->select();
217 catch (std::bad_alloc const &)
219 throw uno::RuntimeException(
220 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
221 uno::Reference< uno::XInterface >());
225 } // namespace
227 bool
228 UUIInteractionHelper::handleLockedDocumentRequest(
229 uno::Reference< task::XInteractionRequest > const & rRequest)
230 SAL_THROW((::com::sun::star::uno::RuntimeException))
232 uno::Any aAnyRequest(rRequest->getRequest());
234 document::LockedDocumentRequest aLockedDocumentRequest;
235 if (aAnyRequest >>= aLockedDocumentRequest )
237 handleLockedDocumentRequest_( getParentProperty(),
238 aLockedDocumentRequest.DocumentURL,
239 aLockedDocumentRequest.UserInfo,
240 rRequest->getContinuations(),
241 UUI_DOC_LOAD_LOCK );
242 return true;
245 document::OwnLockOnDocumentRequest aOwnLockOnDocumentRequest;
246 if (aAnyRequest >>= aOwnLockOnDocumentRequest )
248 handleLockedDocumentRequest_( getParentProperty(),
249 aOwnLockOnDocumentRequest.DocumentURL,
250 aOwnLockOnDocumentRequest.TimeInfo,
251 rRequest->getContinuations(),
252 aOwnLockOnDocumentRequest.IsStoring
253 ? UUI_DOC_OWN_SAVE_LOCK
254 : UUI_DOC_OWN_LOAD_LOCK );
255 return true;
258 document::LockedOnSavingRequest aLockedOnSavingRequest;
259 if (aAnyRequest >>= aLockedOnSavingRequest )
261 handleLockedDocumentRequest_( getParentProperty(),
262 aLockedOnSavingRequest.DocumentURL,
263 aLockedOnSavingRequest.UserInfo,
264 rRequest->getContinuations(),
265 UUI_DOC_SAVE_LOCK );
266 return true;
268 return false;
271 bool
272 UUIInteractionHelper::handleChangedByOthersRequest(
273 uno::Reference< task::XInteractionRequest > const & rRequest)
274 SAL_THROW((uno::RuntimeException))
276 uno::Any aAnyRequest(rRequest->getRequest());
278 document::ChangedByOthersRequest aChangedByOthersRequest;
279 if (aAnyRequest >>= aChangedByOthersRequest )
281 handleChangedByOthersRequest_( getParentProperty(),
282 rRequest->getContinuations() );
283 return true;
285 return false;
288 bool
289 UUIInteractionHelper::handleLockFileIgnoreRequest(
290 uno::Reference< task::XInteractionRequest > const & rRequest)
291 SAL_THROW((uno::RuntimeException))
293 uno::Any aAnyRequest(rRequest->getRequest());
295 document::LockFileIgnoreRequest aLockFileIgnoreRequest;
296 if (aAnyRequest >>= aLockFileIgnoreRequest )
298 handleLockFileIgnoreRequest_( getParentProperty(),
299 rRequest->getContinuations() );
300 return true;
302 return false;
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */