1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <config_folders.h>
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>
36 #include <vcl/svapp.hxx>
37 #include <vcl/window.hxx>
39 #include <boost/scoped_ptr.hpp>
40 #include <officecfg/Office/Recovery.hxx>
42 namespace svxdr
= svx::DocRecovery
;
43 using namespace ::osl
;
47 class RecoveryUI
: public ::cppu::WeakImplHelper2
< css::lang::XServiceInfo
,
48 css::frame::XSynchronousDispatch
> // => XDispatch!
51 // const, types, etcpp.
67 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
70 VclPtr
<vcl::Window
> m_pParentWindow
;
73 RecoveryUI::EJob m_eJob
;
76 css::uno::Reference
< css::task::XStatusIndicatorFactory
> m_xProgressFactory
;
84 RecoveryUI(const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
88 virtual ~RecoveryUI();
91 // css.lang.XServiceInfo
93 virtual OUString SAL_CALL
getImplementationName()
94 throw(css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
96 virtual sal_Bool SAL_CALL
supportsService(const OUString
& sServiceName
)
97 throw(css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
99 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
100 throw(css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
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
;
111 EJob
impl_classifyJob(const css::util::URL
& aURL
);
113 bool impl_doEmergencySave();
115 void impl_doRecovery();
117 void impl_showAllRecoveredDocs();
121 RecoveryUI::RecoveryUI(const css::uno::Reference
< css::uno::XComponentContext
>& xContext
)
122 : m_xContext (xContext
)
123 , m_pParentWindow(0 )
124 , m_eJob (RecoveryUI::E_JOB_UNKNOWN
)
128 RecoveryUI::~RecoveryUI()
132 OUString SAL_CALL
RecoveryUI::getImplementationName()
133 throw(css::uno::RuntimeException
, std::exception
)
135 return OUString("com.sun.star.comp.svx.RecoveryUI");
138 sal_Bool SAL_CALL
RecoveryUI::supportsService(const OUString
& sServiceName
)
139 throw(css::uno::RuntimeException
, std::exception
)
141 return cppu::supportsService(this, sServiceName
);
144 css::uno::Sequence
< OUString
> SAL_CALL
RecoveryUI::getSupportedServiceNames()
145 throw(css::uno::RuntimeException
, std::exception
)
147 css::uno::Sequence
< OUString
> lServiceNames(1);
148 lServiceNames
[0] = "com.sun.star.dialog.RecoveryUI";
149 return lServiceNames
;
152 css::uno::Any SAL_CALL
RecoveryUI::dispatchWithReturnValue(const css::util::URL
& aURL
,
153 const css::uno::Sequence
< css::beans::PropertyValue
>& )
154 throw(css::uno::RuntimeException
, std::exception
)
156 // Internally we use VCL ... every call into vcl based code must
157 // be guarded by locking the global solar mutex.
158 ::SolarMutexGuard aSolarLock
;
161 RecoveryUI::EJob eJob
= impl_classifyJob(aURL
);
162 // TODO think about outside arguments
166 case RecoveryUI::E_DO_EMERGENCY_SAVE
:
168 bool bRet
= impl_doEmergencySave();
173 case RecoveryUI::E_DO_RECOVERY
:
186 static OUString
GetCrashConfigDir()
190 OUString ustrValue
= "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/bootstrap.ini:UserInstallation}";
191 #elif defined(MACOSX)
192 OUString ustrValue
= "~";
194 OUString ustrValue
= "$SYSUSERCONFIG";
196 rtl::Bootstrap::expandMacros( ustrValue
);
199 ustrValue
+= "/user/crashdata";
207 #define LCKFILE "crashdat.lck"
209 #define LCKFILE ".crash_report_unsent"
213 static OUString
GetUnsentURL()
215 OUString aURL
= GetCrashConfigDir() + "/" LCKFILE
;
221 static bool delete_pending_crash()
223 OUString aUnsentURL
= GetUnsentURL();
224 return ( FileBase::E_None
== File::remove( aUnsentURL
) );
227 RecoveryUI::EJob
RecoveryUI::impl_classifyJob(const css::util::URL
& aURL
)
229 m_eJob
= RecoveryUI::E_JOB_UNKNOWN
;
230 if (aURL
.Protocol
== RECOVERY_CMDPART_PROTOCOL
)
232 if (aURL
.Path
== RECOVERY_CMDPART_DO_EMERGENCY_SAVE
)
233 m_eJob
= RecoveryUI::E_DO_EMERGENCY_SAVE
;
234 else if (aURL
.Path
== RECOVERY_CMDPART_DO_RECOVERY
)
235 m_eJob
= RecoveryUI::E_DO_RECOVERY
;
241 bool RecoveryUI::impl_doEmergencySave()
243 // create core service, which implements the real "emergency save" algorithm.
244 svxdr::RecoveryCore
* pCore
= new svxdr::RecoveryCore(m_xContext
, true);
245 css::uno::Reference
< css::frame::XStatusListener
> xCore(pCore
);
247 // create dialog for this operation and bind it to the used core service
248 ScopedVclPtrInstance
<svxdr::SaveDialog
> xDialog(m_pParentWindow
, pCore
);
251 short nRet
= xDialog
->Execute();
252 return (nRet
==DLG_RET_OK_AUTOLUNCH
);
255 void RecoveryUI::impl_doRecovery()
257 // create core service, which implements the real "emergency save" algorithm.
258 svxdr::RecoveryCore
* pCore
= new svxdr::RecoveryCore(m_xContext
, false);
259 css::uno::Reference
< css::frame::XStatusListener
> xCore(pCore
);
261 // create all needed dialogs for this operation
262 // and bind it to the used core service
263 ScopedVclPtrInstance
<svxdr::RecoveryDialog
> xDialog(m_pParentWindow
, pCore
);
268 impl_showAllRecoveredDocs();
270 delete_pending_crash();
273 void RecoveryUI::impl_showAllRecoveredDocs()
275 css::uno::Reference
< css::frame::XDesktop2
> xDesktop
= css::frame::Desktop::create( m_xContext
);
277 css::uno::Reference
< css::container::XIndexAccess
> xTaskContainer(
278 xDesktop
->getFrames(),
279 css::uno::UNO_QUERY_THROW
);
281 sal_Int32 c
= xTaskContainer
->getCount();
287 css::uno::Reference
< css::frame::XFrame
> xTask
;
288 xTaskContainer
->getByIndex(i
) >>= xTask
;
292 css::uno::Reference
< css::awt::XWindow
> xWindow
= xTask
->getContainerWindow();
296 xWindow
->setVisible(sal_True
);
298 catch(const css::uno::RuntimeException
&)
300 catch(const css::uno::Exception
&)
307 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
308 com_sun_star_comp_svx_RecoveryUI_get_implementation(
309 css::uno::XComponentContext
*context
,
310 css::uno::Sequence
<css::uno::Any
> const &)
312 return cppu::acquire(new RecoveryUI(context
));
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */