merge the formfield patch from ooo-build
[ooovba.git] / svx / source / unodraw / recoveryui.cxx
blob98137146fc61f35d86abfadb3da060a721bba637
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: recoveryui.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 //===============================================
35 // includes
36 #include "recoveryui.hxx"
37 #include "docrecovery.hxx"
38 #include <com/sun/star/lang/XInitialization.hpp>
39 #include <com/sun/star/frame/XFramesSupplier.hpp>
40 #include <com/sun/star/beans/NamedValue.hpp>
41 #include <osl/file.hxx>
42 #include <rtl/bootstrap.hxx>
43 #include <comphelper/configurationhelper.hxx>
45 #include <vcl/svapp.hxx>
47 //===============================================
48 // const
50 #define IMPLEMENTATIONNAME_RECOVERYUI ::rtl::OUString::createFromAscii("com.sun.star.comp.svx.RecoveryUI")
51 #define SERVICENAME_RECOVERYUI ::rtl::OUString::createFromAscii("com.sun.star.dialog.RecoveryUI")
53 //===============================================
54 // namespace
56 namespace svx
59 namespace css = ::com::sun::star;
60 namespace svxdr = ::svx::DocRecovery;
62 using namespace ::rtl;
63 using namespace ::osl;
65 //===============================================
66 RecoveryUI::RecoveryUI(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
67 : m_xSMGR (xSMGR )
68 , m_pParentWindow(0 )
69 , m_eJob (RecoveryUI::E_JOB_UNKNOWN)
73 //===============================================
74 RecoveryUI::~RecoveryUI()
78 //===============================================
79 ::rtl::OUString SAL_CALL RecoveryUI::getImplementationName()
80 throw(css::uno::RuntimeException)
82 return RecoveryUI::st_getImplementationName();
85 //===============================================
86 sal_Bool SAL_CALL RecoveryUI::supportsService(const ::rtl::OUString& sServiceName)
87 throw(css::uno::RuntimeException)
89 const css::uno::Sequence< ::rtl::OUString > lServices = RecoveryUI::st_getSupportedServiceNames();
90 sal_Int32 c = lServices.getLength();
91 sal_Int32 i = 0;
92 for (i=0; i<c; ++i)
94 const ::rtl::OUString& sSupportedService = lServices[i];
95 if (sSupportedService.equals(sServiceName))
96 return sal_True;
98 return sal_False;
101 //===============================================
102 css::uno::Sequence< ::rtl::OUString > SAL_CALL RecoveryUI::getSupportedServiceNames()
103 throw(css::uno::RuntimeException)
105 return RecoveryUI::st_getSupportedServiceNames();
108 //===============================================
109 css::uno::Any SAL_CALL RecoveryUI::dispatchWithReturnValue(const css::util::URL& aURL,
110 const css::uno::Sequence< css::beans::PropertyValue >& )
111 throw(css::uno::RuntimeException)
113 // Internaly we use VCL ... every call into vcl based code must
114 // be guarded by locking the global solar mutex.
115 ::vos::OGuard aSolarLock(&Application::GetSolarMutex());
117 css::uno::Any aRet;
118 RecoveryUI::EJob eJob = impl_classifyJob(aURL);
119 // TODO think about outside arguments
121 switch(eJob)
123 case RecoveryUI::E_DO_EMERGENCY_SAVE :
125 sal_Bool bRet = impl_doEmergencySave();
126 aRet <<= bRet;
127 break;
130 case RecoveryUI::E_DO_RECOVERY :
131 impl_doRecovery();
132 break;
134 case RecoveryUI::E_DO_CRASHREPORT :
135 impl_doCrashReport();
136 break;
138 default :
139 break;
142 return aRet;
145 //===============================================
146 void SAL_CALL RecoveryUI::dispatch(const css::util::URL& aURL ,
147 const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
148 throw(css::uno::RuntimeException)
150 // recycle this method :-)
151 dispatchWithReturnValue(aURL, lArguments);
154 //===============================================
155 void SAL_CALL RecoveryUI::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >&, const css::util::URL& ) throw(css::uno::RuntimeException)
157 // TODO
158 OSL_ENSURE(sal_False, "RecoveryUI::addStatusListener()\nNot implemented yet!");
161 //===============================================
162 void SAL_CALL RecoveryUI::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >&, const css::util::URL& )
163 throw(css::uno::RuntimeException)
165 // TODO
166 OSL_ENSURE(sal_False, "RecoveryUI::removeStatusListener()\nNot implemented yet!");
169 //===============================================
170 ::rtl::OUString RecoveryUI::st_getImplementationName()
172 static ::rtl::OUString IMPLEMENTATIONNAME = IMPLEMENTATIONNAME_RECOVERYUI;
173 return IMPLEMENTATIONNAME;
176 //===============================================
177 css::uno::Sequence< ::rtl::OUString > RecoveryUI::st_getSupportedServiceNames()
179 css::uno::Sequence< ::rtl::OUString > lServiceNames(1); lServiceNames.getArray() [0] = SERVICENAME_RECOVERYUI;
180 return lServiceNames;
183 //===============================================
184 css::uno::Reference< css::uno::XInterface > SAL_CALL RecoveryUI::st_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
186 RecoveryUI* pNew = new RecoveryUI(xSMGR);
187 return css::uno::Reference< css::uno::XInterface >(static_cast< css::lang::XServiceInfo* >(pNew));
190 //===============================================
192 static OUString GetCrashConfigDir()
195 #if defined(WNT) || defined(OS2)
196 OUString ustrValue = OUString::createFromAscii("${$BRAND_BASE_DIR/program/bootstrap.ini:UserInstallation}");
197 #elif defined(MACOSX)
198 OUString ustrValue = OUString::createFromAscii("~");
199 #else
200 OUString ustrValue = OUString::createFromAscii("$SYSUSERCONFIG");
201 #endif
202 Bootstrap::expandMacros( ustrValue );
204 #if defined(WNT) || defined(OS2)
205 ustrValue += OUString::createFromAscii("/user/crashdata");
206 #endif
207 return ustrValue;
210 //===============================================
212 #if defined(WNT) || defined(OS2)
213 #define LCKFILE "crashdat.lck"
214 #else
215 #define LCKFILE ".crash_report_unsent"
216 #endif
219 static OUString GetUnsentURL()
221 OUString aURL = GetCrashConfigDir();
223 aURL += OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
224 aURL += OUString( RTL_CONSTASCII_USTRINGPARAM( LCKFILE ) );
226 return aURL;
229 //===============================================
231 static bool new_crash_pending()
233 OUString aUnsentURL = GetUnsentURL();
234 File aFile( aUnsentURL );
236 if ( FileBase::E_None == aFile.open( OpenFlag_Read ) )
238 aFile.close();
239 return true;
242 return false;
244 //===============================================
246 static bool delete_pending_crash()
248 OUString aUnsentURL = GetUnsentURL();
249 return ( FileBase::E_None == File::remove( aUnsentURL ) );
252 RecoveryUI::EJob RecoveryUI::impl_classifyJob(const css::util::URL& aURL)
254 m_eJob = RecoveryUI::E_JOB_UNKNOWN;
255 if (aURL.Protocol.equals(RECOVERY_CMDPART_PROTOCOL))
257 if (aURL.Path.equals(RECOVERY_CMDPART_DO_EMERGENCY_SAVE))
258 m_eJob = RecoveryUI::E_DO_EMERGENCY_SAVE;
259 else
260 if (aURL.Path.equals(RECOVERY_CMDPART_DO_RECOVERY))
261 m_eJob = RecoveryUI::E_DO_RECOVERY;
262 else
263 if (aURL.Path.equals(RECOVERY_CMDPART_DO_CRASHREPORT))
264 m_eJob = RecoveryUI::E_DO_CRASHREPORT;
267 return m_eJob;
270 //===============================================
271 sal_Bool RecoveryUI::impl_doEmergencySave()
273 // create core service, which implements the real "emergency save" algorithm.
274 svxdr::RecoveryCore* pCore = new svxdr::RecoveryCore(m_xSMGR, sal_True);
275 css::uno::Reference< css::frame::XStatusListener > xCore(pCore);
277 // create all needed dialogs for this operation
278 // and bind it to the used core service
279 svxdr::TabDialog4Recovery* pWizard = new svxdr::TabDialog4Recovery(m_pParentWindow);
280 svxdr::IExtendedTabPage* pPage1 = new svxdr::SaveDialog (pWizard, pCore );
281 pWizard->addTabPage(pPage1);
283 // start the wizard
284 short nRet = pWizard->Execute();
286 delete pPage1 ;
287 delete pWizard;
289 return (nRet==DLG_RET_OK_AUTOLUNCH);
292 //===============================================
293 void RecoveryUI::impl_doRecovery()
295 sal_Bool bRecoveryOnly( sal_False );
297 ::rtl::OUString CFG_PACKAGE_RECOVERY( RTL_CONSTASCII_USTRINGPARAM ( "org.openoffice.Office.Recovery/" ));
298 ::rtl::OUString CFG_PATH_CRASHREPORTER( RTL_CONSTASCII_USTRINGPARAM( "CrashReporter" ));
299 ::rtl::OUString CFG_ENTRY_ENABLED( RTL_CONSTASCII_USTRINGPARAM ( "Enabled" ));
301 sal_Bool bCrashRepEnabled( sal_True );
302 css::uno::Any aVal = ::comphelper::ConfigurationHelper::readDirectKey(
303 m_xSMGR,
304 CFG_PACKAGE_RECOVERY,
305 CFG_PATH_CRASHREPORTER,
306 CFG_ENTRY_ENABLED,
307 ::comphelper::ConfigurationHelper::E_READONLY);
308 aVal >>= bCrashRepEnabled;
309 bRecoveryOnly = !bCrashRepEnabled;
311 // create core service, which implements the real "emergency save" algorithm.
312 svxdr::RecoveryCore* pCore = new svxdr::RecoveryCore(m_xSMGR, sal_False);
313 css::uno::Reference< css::frame::XStatusListener > xCore(pCore);
315 // create all needed dialogs for this operation
316 // and bind it to the used core service
317 svxdr::TabDialog4Recovery* pWizard = new svxdr::TabDialog4Recovery (m_pParentWindow);
318 svxdr::IExtendedTabPage* pPage1 = new svxdr::RecoveryDialog (pWizard, pCore );
319 svxdr::IExtendedTabPage* pPage2 = 0;
320 svxdr::IExtendedTabPage* pPage3 = 0;
322 pWizard->addTabPage(pPage1);
323 if ( !bRecoveryOnly && new_crash_pending() )
325 pPage2 = new svxdr::ErrorRepWelcomeDialog(pWizard );
326 pPage3 = new svxdr::ErrorRepSendDialog (pWizard );
327 pWizard->addTabPage(pPage2);
328 pWizard->addTabPage(pPage3);
331 // start the wizard
332 pWizard->Execute();
334 impl_showAllRecoveredDocs();
336 delete pPage3 ;
337 delete pPage2 ;
338 delete pPage1 ;
339 delete pWizard;
341 delete_pending_crash();
344 //===============================================
346 void RecoveryUI::impl_doCrashReport()
348 if ( new_crash_pending() )
350 svxdr::TabDialog4Recovery* pWizard = new svxdr::TabDialog4Recovery (m_pParentWindow );
351 svxdr::IExtendedTabPage* pPage1 = new svxdr::ErrorRepWelcomeDialog(pWizard, sal_False);
352 svxdr::IExtendedTabPage* pPage2 = new svxdr::ErrorRepSendDialog (pWizard );
353 pWizard->addTabPage(pPage1);
354 pWizard->addTabPage(pPage2);
356 // start the wizard
357 pWizard->Execute();
359 delete pPage2 ;
360 delete pPage1 ;
361 delete pWizard;
363 delete_pending_crash();
367 //===============================================
368 void RecoveryUI::impl_showAllRecoveredDocs()
370 css::uno::Reference< css::frame::XFramesSupplier > xDesktop(
371 m_xSMGR->createInstance(SERVICENAME_DESKTOP),
372 css::uno::UNO_QUERY_THROW);
374 css::uno::Reference< css::container::XIndexAccess > xTaskContainer(
375 xDesktop->getFrames(),
376 css::uno::UNO_QUERY_THROW);
378 sal_Int32 c = xTaskContainer->getCount();
379 sal_Int32 i = 0;
380 for (i=0; i<c; ++i)
384 css::uno::Reference< css::frame::XFrame > xTask;
385 xTaskContainer->getByIndex(i) >>= xTask;
386 if (!xTask.is())
387 continue;
389 css::uno::Reference< css::awt::XWindow > xWindow = xTask->getContainerWindow();
390 if (!xWindow.is())
391 continue;
393 xWindow->setVisible(sal_True);
395 catch(const css::uno::RuntimeException& exRun)
396 { throw exRun; }
397 catch(const css::uno::Exception&)
398 { continue; }
402 } // namespace svx