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 <config_folders.h>
21 :
22 : #include <docrecovery.hxx>
23 : #include <com/sun/star/beans/NamedValue.hpp>
24 : #include <com/sun/star/frame/Desktop.hpp>
25 : #include <com/sun/star/frame/XFramesSupplier.hpp>
26 : #include <com/sun/star/frame/XSynchronousDispatch.hpp>
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 : #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
29 : #include <cppuhelper/implbase2.hxx>
30 : #include <osl/file.hxx>
31 : #include <rtl/bootstrap.hxx>
32 : #include <rtl/ref.hxx>
33 : #include <comphelper/processfactory.hxx>
34 : #include <cppuhelper/supportsservice.hxx>
35 :
36 : #include <vcl/svapp.hxx>
37 : #include <vcl/window.hxx>
38 :
39 : #include <boost/scoped_ptr.hpp>
40 : #include <officecfg/Office/Recovery.hxx>
41 :
42 : namespace svxdr = ::svx::DocRecovery;
43 : using namespace ::osl;
44 :
45 : namespace {
46 :
47 : class RecoveryUI : public ::cppu::WeakImplHelper2< css::lang::XServiceInfo ,
48 : css::frame::XSynchronousDispatch > // => XDispatch!
49 : {
50 :
51 : // const, types, etcpp.
52 : private:
53 :
54 : /** @short TODO */
55 : enum EJob
56 : {
57 : E_JOB_UNKNOWN,
58 : E_DO_EMERGENCY_SAVE,
59 : E_DO_RECOVERY,
60 : };
61 :
62 :
63 : // member
64 : private:
65 :
66 : /** @short TODO */
67 : css::uno::Reference< css::uno::XComponentContext > m_xContext;
68 :
69 : /** @short TODO */
70 : vcl::Window* m_pParentWindow;
71 :
72 : /** @short TODO */
73 : RecoveryUI::EJob m_eJob;
74 :
75 : /** @short TODO */
76 : css::uno::Reference< css::task::XStatusIndicatorFactory > m_xProgressFactory;
77 :
78 :
79 : // interface
80 : public:
81 :
82 :
83 : /** @short TODO */
84 : RecoveryUI(const css::uno::Reference< css::uno::XComponentContext >& xContext);
85 :
86 :
87 : /** @short TODO */
88 : virtual ~RecoveryUI();
89 :
90 :
91 : // css.lang.XServiceInfo
92 :
93 : virtual OUString SAL_CALL getImplementationName()
94 : throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
95 :
96 : virtual sal_Bool SAL_CALL supportsService(const OUString& sServiceName)
97 : throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
98 :
99 : virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
100 : throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
101 :
102 :
103 : virtual com::sun::star::uno::Any SAL_CALL dispatchWithReturnValue(const css::util::URL& aURL,
104 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
105 : throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
106 :
107 :
108 : // css.frame.XDispatch
109 :
110 : virtual void SAL_CALL dispatch(const css::util::URL& aURL ,
111 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
112 : throw(css::uno::RuntimeException);
113 :
114 : virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener >& xListener,
115 : const css::util::URL& aURL )
116 : throw(css::uno::RuntimeException);
117 : virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >& xListener,
118 : const css::util::URL& aURL )
119 : throw(css::uno::RuntimeException);
120 :
121 :
122 : // helper
123 : private:
124 :
125 : EJob impl_classifyJob(const css::util::URL& aURL);
126 :
127 : bool impl_doEmergencySave();
128 :
129 : void impl_doRecovery();
130 :
131 : void impl_showAllRecoveredDocs();
132 :
133 : };
134 :
135 0 : RecoveryUI::RecoveryUI(const css::uno::Reference< css::uno::XComponentContext >& xContext)
136 : : m_xContext (xContext )
137 : , m_pParentWindow(0 )
138 0 : , m_eJob (RecoveryUI::E_JOB_UNKNOWN)
139 : {
140 0 : }
141 :
142 0 : RecoveryUI::~RecoveryUI()
143 : {
144 0 : }
145 :
146 0 : OUString SAL_CALL RecoveryUI::getImplementationName()
147 : throw(css::uno::RuntimeException, std::exception)
148 : {
149 0 : return OUString("com.sun.star.comp.svx.RecoveryUI");
150 : }
151 :
152 0 : sal_Bool SAL_CALL RecoveryUI::supportsService(const OUString& sServiceName)
153 : throw(css::uno::RuntimeException, std::exception)
154 : {
155 0 : return cppu::supportsService(this, sServiceName);
156 : }
157 :
158 0 : css::uno::Sequence< OUString > SAL_CALL RecoveryUI::getSupportedServiceNames()
159 : throw(css::uno::RuntimeException, std::exception)
160 : {
161 0 : css::uno::Sequence< OUString > lServiceNames(1);
162 0 : lServiceNames[0] = "com.sun.star.dialog.RecoveryUI";
163 0 : return lServiceNames;
164 : }
165 :
166 0 : css::uno::Any SAL_CALL RecoveryUI::dispatchWithReturnValue(const css::util::URL& aURL,
167 : const css::uno::Sequence< css::beans::PropertyValue >& )
168 : throw(css::uno::RuntimeException, std::exception)
169 : {
170 : // Internally we use VCL ... every call into vcl based code must
171 : // be guarded by locking the global solar mutex.
172 0 : ::SolarMutexGuard aSolarLock;
173 :
174 0 : css::uno::Any aRet;
175 0 : RecoveryUI::EJob eJob = impl_classifyJob(aURL);
176 : // TODO think about outside arguments
177 :
178 0 : switch(eJob)
179 : {
180 : case RecoveryUI::E_DO_EMERGENCY_SAVE :
181 : {
182 0 : bool bRet = impl_doEmergencySave();
183 0 : aRet <<= bRet;
184 0 : break;
185 : }
186 :
187 : case RecoveryUI::E_DO_RECOVERY :
188 0 : impl_doRecovery();
189 0 : break;
190 :
191 : default :
192 0 : break;
193 : }
194 :
195 0 : return aRet;
196 : }
197 :
198 :
199 0 : void SAL_CALL RecoveryUI::dispatch(const css::util::URL& aURL ,
200 : const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
201 : throw(css::uno::RuntimeException)
202 : {
203 : // recycle this method :-)
204 0 : dispatchWithReturnValue(aURL, lArguments);
205 0 : }
206 :
207 :
208 0 : void SAL_CALL RecoveryUI::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >&, const css::util::URL& ) throw(css::uno::RuntimeException)
209 : {
210 : // TODO
211 : OSL_FAIL("RecoveryUI::addStatusListener()\nNot implemented yet!");
212 0 : }
213 :
214 :
215 0 : void SAL_CALL RecoveryUI::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >&, const css::util::URL& )
216 : throw(css::uno::RuntimeException)
217 : {
218 : // TODO
219 : OSL_FAIL("RecoveryUI::removeStatusListener()\nNot implemented yet!");
220 0 : }
221 :
222 0 : static OUString GetCrashConfigDir()
223 : {
224 :
225 : #if defined(WNT)
226 : OUString ustrValue = "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/bootstrap.ini:UserInstallation}";
227 : #elif defined(MACOSX)
228 : OUString ustrValue = "~";
229 : #else
230 0 : OUString ustrValue = "$SYSUSERCONFIG";
231 : #endif
232 0 : rtl::Bootstrap::expandMacros( ustrValue );
233 :
234 : #if defined(WNT)
235 : ustrValue += "/user/crashdata";
236 : #endif
237 0 : return ustrValue;
238 : }
239 :
240 :
241 :
242 : #if defined(WNT)
243 : #define LCKFILE "crashdat.lck"
244 : #else
245 : #define LCKFILE ".crash_report_unsent"
246 : #endif
247 :
248 :
249 0 : static OUString GetUnsentURL()
250 : {
251 0 : OUString aURL = GetCrashConfigDir() + "/" LCKFILE;
252 0 : return aURL;
253 : }
254 :
255 :
256 :
257 0 : static bool delete_pending_crash()
258 : {
259 0 : OUString aUnsentURL = GetUnsentURL();
260 0 : return ( FileBase::E_None == File::remove( aUnsentURL ) );
261 : }
262 :
263 0 : RecoveryUI::EJob RecoveryUI::impl_classifyJob(const css::util::URL& aURL)
264 : {
265 0 : m_eJob = RecoveryUI::E_JOB_UNKNOWN;
266 0 : if (aURL.Protocol.equals(RECOVERY_CMDPART_PROTOCOL))
267 : {
268 0 : if (aURL.Path.equals(RECOVERY_CMDPART_DO_EMERGENCY_SAVE))
269 0 : m_eJob = RecoveryUI::E_DO_EMERGENCY_SAVE;
270 0 : else if (aURL.Path.equals(RECOVERY_CMDPART_DO_RECOVERY))
271 0 : m_eJob = RecoveryUI::E_DO_RECOVERY;
272 : }
273 :
274 0 : return m_eJob;
275 : }
276 :
277 0 : bool RecoveryUI::impl_doEmergencySave()
278 : {
279 : // create core service, which implements the real "emergency save" algorithm.
280 0 : svxdr::RecoveryCore* pCore = new svxdr::RecoveryCore(m_xContext, true);
281 0 : css::uno::Reference< css::frame::XStatusListener > xCore(pCore);
282 :
283 : // create dialog for this operation and bind it to the used core service
284 0 : boost::scoped_ptr<Dialog> xDialog(new svxdr::SaveDialog(m_pParentWindow, pCore));
285 :
286 : // start the dialog
287 0 : short nRet = xDialog->Execute();
288 0 : return (nRet==DLG_RET_OK_AUTOLUNCH);
289 : }
290 :
291 0 : void RecoveryUI::impl_doRecovery()
292 : {
293 : // create core service, which implements the real "emergency save" algorithm.
294 0 : svxdr::RecoveryCore* pCore = new svxdr::RecoveryCore(m_xContext, false);
295 0 : css::uno::Reference< css::frame::XStatusListener > xCore(pCore);
296 :
297 : // create all needed dialogs for this operation
298 : // and bind it to the used core service
299 0 : boost::scoped_ptr<Dialog> xDialog(new svxdr::RecoveryDialog(m_pParentWindow, pCore));
300 :
301 : // start the dialog
302 0 : xDialog->Execute();
303 :
304 0 : impl_showAllRecoveredDocs();
305 :
306 0 : delete_pending_crash();
307 0 : }
308 :
309 0 : void RecoveryUI::impl_showAllRecoveredDocs()
310 : {
311 0 : css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create( m_xContext );
312 :
313 : css::uno::Reference< css::container::XIndexAccess > xTaskContainer(
314 0 : xDesktop->getFrames(),
315 0 : css::uno::UNO_QUERY_THROW);
316 :
317 0 : sal_Int32 c = xTaskContainer->getCount();
318 0 : sal_Int32 i = 0;
319 0 : for (i=0; i<c; ++i)
320 : {
321 : try
322 : {
323 0 : css::uno::Reference< css::frame::XFrame > xTask;
324 0 : xTaskContainer->getByIndex(i) >>= xTask;
325 0 : if (!xTask.is())
326 0 : continue;
327 :
328 0 : css::uno::Reference< css::awt::XWindow > xWindow = xTask->getContainerWindow();
329 0 : if (!xWindow.is())
330 0 : continue;
331 :
332 0 : xWindow->setVisible(sal_True);
333 : }
334 0 : catch(const css::uno::RuntimeException&)
335 0 : { throw; }
336 0 : catch(const css::uno::Exception&)
337 0 : { continue; }
338 0 : }
339 0 : }
340 :
341 : }
342 :
343 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
344 0 : com_sun_star_comp_svx_RecoveryUI_get_implementation(
345 : css::uno::XComponentContext *context,
346 : css::uno::Sequence<css::uno::Any> const &)
347 : {
348 0 : return cppu::acquire(new RecoveryUI(context));
349 594 : }
350 :
351 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|