Tag: cws_dev300_odbmacros3 User: fs Date: 2008-05-07 08:38:21+0000 Modified: dba/dbaccess/source/ext/macromigration/macromigrationdialog.cxx
Log: #i49133# reload the document if migration finished (successfully or not) File Changes: Directory: /dba/dbaccess/source/ext/macromigration/ =================================================== File [changed]: macromigrationdialog.cxx Url: http://dba.openoffice.org/source/browse/dba/dbaccess/source/ext/macromigration/macromigrationdialog.cxx?r1=1.3.2.3&r2=1.3.2.4 Delta lines: +154 -10 ---------------------- --- macromigrationdialog.cxx 2008-04-30 09:29:25+0000 1.3.2.3 +++ macromigrationdialog.cxx 2008-05-07 08:38:18+0000 1.3.2.4 @@ -7,7 +7,7 @@ * OpenOffice.org - a multi-platform office productivity suite * * $RCSfile: macromigrationdialog.cxx,v $ - * $Revision: 1.3.2.3 $ + * $Revision: 1.3.2.4 $ * * This file is part of OpenOffice.org. * @@ -46,12 +46,20 @@ #include <com/sun/star/frame/XModel2.hpp> #include <com/sun/star/frame/XStorable.hpp> #include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/util/XCloseable.hpp> +#include <com/sun/star/frame/XComponentLoader.hpp> +#include <com/sun/star/util/XModifiable.hpp> /** === end UNO includes === **/ +#include <comphelper/namedvaluecollection.hxx> #include <cppuhelper/exc_hlp.hxx> +#include <cppuhelper/implbase1.hxx> +#include <rtl/ref.hxx> #include <svtools/filenotation.hxx> #include <tools/diagnose_ex.h> +#include <list> + //........................................................................ namespace dbmm { @@ -78,14 +86,37 @@ using ::com::sun::star::sdb::XOfficeDatabaseDocument; using ::com::sun::star::frame::XModel2; using ::com::sun::star::frame::XController; + using ::com::sun::star::frame::XController2; using ::com::sun::star::container::XEnumeration; using ::com::sun::star::frame::XStorable; using ::com::sun::star::uno::Sequence; using ::com::sun::star::beans::PropertyValue; using ::com::sun::star::frame::XModel; + using ::com::sun::star::frame::XFrame; + using ::com::sun::star::awt::XWindow; + using ::com::sun::star::util::XCloseable; + using ::com::sun::star::util::XCloseListener; + using ::com::sun::star::util::CloseVetoException; + using ::com::sun::star::lang::EventObject; + using ::com::sun::star::frame::XComponentLoader; + using ::com::sun::star::util::XModifiable; /** === end UNO using === **/ //==================================================================== + //= helper + //==================================================================== + //-------------------------------------------------------------------- + static void lcl_getControllers_throw( const Reference< XOfficeDatabaseDocument >& _rxDocument, + ::std::list< Reference< XController2 > >& _out_rControllers ) + { + _out_rControllers.clear(); + Reference< XModel2 > xDocument( _rxDocument, UNO_QUERY_THROW ); + Reference< XEnumeration > xControllerEnum( xDocument->getControllers(), UNO_SET_THROW ); + while ( xControllerEnum->hasMoreElements() ) + _out_rControllers.push_back( Reference< XController2 >( xControllerEnum->nextElement(), UNO_QUERY_THROW ) ); + } + + //==================================================================== //= MacroMigrationDialog_Data //==================================================================== struct MacroMigrationDialog_Data @@ -93,6 +124,7 @@ ::comphelper::ComponentContext aContext; MigrationLog aLogger; Reference< XOfficeDatabaseDocument > xDocument; + ::rtl::OUString sSuccessfulBackupLocation; bool bMigrationIsRunning; bool bMigrationFailure; bool bMigrationSuccess; @@ -172,6 +204,7 @@ OSL_ENSURE( !m_pData->bMigrationFailure || !m_pData->bMigrationSuccess, "MacroMigrationDialog::Execute: success *and* failure at the same time?!" ); impl_reloadDocument_nothrow( m_pData->bMigrationSuccess ); + return nResult; } @@ -332,16 +365,12 @@ bool bSuccess = true; try { - ::std::vector< Reference< XController > > aControllers; - // collect all controllers of our document - Reference< XModel2 > xDocument( m_pData->xDocument, UNO_QUERY_THROW ); - Reference< XEnumeration > xControllerEnum( xDocument->getControllers(), UNO_SET_THROW ); - while ( xControllerEnum->hasMoreElements() ) - aControllers.push_back( Reference< XController >( xControllerEnum->nextElement(), UNO_QUERY_THROW ) ); + ::std::list< Reference< XController2 > > aControllers; + lcl_getControllers_throw( m_pData->xDocument, aControllers ); // close all sub documents of all controllers - for ( ::std::vector< Reference< XController > >::const_iterator pos = aControllers.begin(); + for ( ::std::list< Reference< XController2 > >::const_iterator pos = aControllers.begin(); pos != aControllers.end() && bSuccess; ++pos ) @@ -377,6 +406,7 @@ { const Reference< XStorable > xDocument( getDocument(), UNO_QUERY_THROW ); xDocument->storeToURL( sBackupLocation, Sequence< PropertyValue >() ); + m_pData->sSuccessfulBackupLocation = sBackupLocation; } catch( const Exception& ) { @@ -404,8 +434,122 @@ //-------------------------------------------------------------------- void MacroMigrationDialog::impl_reloadDocument_nothrow( bool _bMigrationSuccess ) { - // TODO - (void)_bMigrationSuccess; + typedef ::std::pair< Reference< XFrame >, ::rtl::OUString > ViewDescriptor; + ::std::list< ViewDescriptor > aViews; + + try + { + Reference< XModel2 > xDocument( m_pData->xDocument, UNO_QUERY_THROW ); + + // the information which is necessary to reload the document + ::rtl::OUString sDocumentURL ( xDocument->getURL() ); + ::comphelper::NamedValueCollection aDocumentArgs( xDocument->getArgs() ); + if ( !_bMigrationSuccess ) + { + // if the migration was not successful, then reload from the backup + aDocumentArgs.put( "SalvagedFile", m_pData->sSuccessfulBackupLocation ); + // reset the modified flag of the document, so the controller can be suspended later + Reference< XModifiable > xModify( xDocument, UNO_QUERY_THROW ); + xModify->setModified( sal_False ); + } + + // remove anything from the args which might refer to the old document + aDocumentArgs.remove( "Model" ); + aDocumentArgs.remove( "Stream" ); + aDocumentArgs.remove( "InputStream" ); + aDocumentArgs.remove( "FileName" ); + aDocumentArgs.remove( "URL" ); + + // collect all controllers of our document + ::std::list< Reference< XController2 > > aControllers; + lcl_getControllers_throw( m_pData->xDocument, aControllers ); + + // close all those controllers + while ( !aControllers.empty() ) + { + Reference< XController2 > xController( aControllers.front(), UNO_SET_THROW ); + aControllers.pop_front(); + + Reference< XFrame > xFrame( xController->getFrame(), UNO_SET_THROW ); + ::rtl::OUString sViewName( xController->getViewControllerName() ); + + if ( !xController->suspend( sal_True ) ) + { // ouch. There shouldn't be any modal dialogs and such, so there + // really is no reason why suspending shouldn't work. + OSL_ENSURE( false, "MacroMigrationDialog::impl_reloadDocument_nothrow: could not suspend a controller!" ); + // ignoring this would be at the cost of a crash (potentially) + // so, we cannot continue here. + throw CloseVetoException(); + } + + aViews.push_back( ViewDescriptor( xFrame, sViewName ) ); + xFrame->setComponent( NULL, NULL ); + xController->dispose(); + } + + // Note the document is closed now - disconnecting the last controller + // closes it automatically. + + Reference< XOfficeDatabaseDocument > xNewDocument; + + // re-create the views + while ( !aViews.empty() ) + { + ViewDescriptor aView( aViews.front() ); + aViews.pop_front(); + + // load the document into this frame + Reference< XComponentLoader > xLoader( aView.first, UNO_QUERY_THROW ); + aDocumentArgs.put( "ViewName", aView.second ); + Reference< XInterface > xReloaded( xLoader->loadComponentFromURL( + sDocumentURL, + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_self" ) ), + 0, + aDocumentArgs.getPropertyValues() + ) ); + + OSL_ENSURE( xReloaded != xDocument, + "MacroMigrationDialog::impl_reloadDocument_nothrow: this should have been a new instance!" ); + // this would be unexpected, but recoverable: The loader should at least have done + // this: really *load* the document, even if it loaded it into the old document instance + if ( !xNewDocument.is() ) + { + xNewDocument.set( xReloaded, UNO_QUERY_THROW ); + // for subsequent loads, into different frames, put the document into the load args + aDocumentArgs.put( "Model", xNewDocument ); + } + #if OSL_DEBUG_LEVEL > 0 + else + { + OSL_ENSURE( xNewDocument == xReloaded, + "MacroMigrationDialog::impl_reloadDocument_nothrow: unexpected: subsequent load attempt returned a wrong document!" ); + } + #endif + } + + m_pData->xDocument = xNewDocument; + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + + // close all frames from aViews - the respective controllers have been closed, but + // reloading didn't work, so the frames are zombies now. + while ( !aViews.empty() ) + { + ViewDescriptor aView( aViews.front() ); + aViews.pop_front(); + try + { + Reference< XCloseable > xFrameClose( aView.first, UNO_QUERY_THROW ); + xFrameClose->close( sal_True ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } } //........................................................................ --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
