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