196de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
396de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
496de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
596de5490SAndrew Rist  * distributed with this work for additional information
696de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
796de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
896de5490SAndrew Rist  * "License"); you may not use this file except in compliance
996de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
1096de5490SAndrew Rist  *
1196de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1296de5490SAndrew Rist  *
1396de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1496de5490SAndrew Rist  * software distributed under the License is distributed on an
1596de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1696de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
1796de5490SAndrew Rist  * specific language governing permissions and limitations
1896de5490SAndrew Rist  * under the License.
1996de5490SAndrew Rist  *
2096de5490SAndrew Rist  *************************************************************/
2196de5490SAndrew Rist 
2296de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*b63233d8Sdamjan #include "precompiled_dbmm.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "dbmm_global.hrc"
28cdf0e10cSrcweir #include "dbmm_module.hxx"
29cdf0e10cSrcweir #include "docinteraction.hxx"
30cdf0e10cSrcweir #include "macromigration.hrc"
31cdf0e10cSrcweir #include "macromigrationdialog.hxx"
32cdf0e10cSrcweir #include "macromigrationpages.hxx"
33cdf0e10cSrcweir #include "migrationengine.hxx"
34cdf0e10cSrcweir #include "migrationerror.hxx"
35cdf0e10cSrcweir #include "migrationlog.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /** === begin UNO includes === **/
38cdf0e10cSrcweir #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
39cdf0e10cSrcweir #include <com/sun/star/frame/XModel2.hpp>
40cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
41cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
42cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
43cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
45cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
46cdf0e10cSrcweir /** === end UNO includes === **/
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
49cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
50cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
51cdf0e10cSrcweir #include <rtl/ref.hxx>
52cdf0e10cSrcweir #include <svl/filenotation.hxx>
53cdf0e10cSrcweir #include <tools/diagnose_ex.h>
54cdf0e10cSrcweir #include <ucbhelper/content.hxx>
55cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
56cdf0e10cSrcweir #include <vcl/msgbox.hxx>
57cdf0e10cSrcweir 
58cdf0e10cSrcweir #include <list>
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //........................................................................
61cdf0e10cSrcweir namespace dbmm
62cdf0e10cSrcweir {
63cdf0e10cSrcweir //........................................................................
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #define STATE_CLOSE_SUB_DOCS    0
66cdf0e10cSrcweir #define STATE_BACKUP_DBDOC      1
67cdf0e10cSrcweir #define STATE_MIGRATE           2
68cdf0e10cSrcweir #define STATE_SUMMARY           3
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #define PATH_DEFAULT            1
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	/** === begin UNO using === **/
73cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
74cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
75cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
76cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
77cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
78cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
79cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
80cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
81cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
82cdf0e10cSrcweir     using ::com::sun::star::sdb::application::XDatabaseDocumentUI;
83cdf0e10cSrcweir     using ::com::sun::star::sdb::XOfficeDatabaseDocument;
84cdf0e10cSrcweir     using ::com::sun::star::frame::XModel2;
85cdf0e10cSrcweir     using ::com::sun::star::frame::XController;
86cdf0e10cSrcweir     using ::com::sun::star::frame::XController2;
87cdf0e10cSrcweir     using ::com::sun::star::container::XEnumeration;
88cdf0e10cSrcweir     using ::com::sun::star::frame::XStorable;
89cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
90cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyValue;
91cdf0e10cSrcweir     using ::com::sun::star::frame::XFrame;
92cdf0e10cSrcweir     using ::com::sun::star::awt::XWindow;
93cdf0e10cSrcweir     using ::com::sun::star::util::XCloseable;
94cdf0e10cSrcweir     using ::com::sun::star::util::XCloseListener;
95cdf0e10cSrcweir     using ::com::sun::star::util::CloseVetoException;
96cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
97cdf0e10cSrcweir     using ::com::sun::star::frame::XComponentLoader;
98cdf0e10cSrcweir     using ::com::sun::star::util::XModifiable;
99cdf0e10cSrcweir     using ::com::sun::star::ucb::XCommandEnvironment;
100cdf0e10cSrcweir     using ::com::sun::star::ucb::XContent;
101cdf0e10cSrcweir     using ::com::sun::star::ucb::XContentIdentifier;
102cdf0e10cSrcweir     using ::com::sun::star::ucb::XContentProvider;
103cdf0e10cSrcweir 	/** === end UNO using === **/
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	//====================================================================
106cdf0e10cSrcweir 	//= helper
107cdf0e10cSrcweir 	//====================================================================
108cdf0e10cSrcweir     //--------------------------------------------------------------------
lcl_getControllers_throw(const Reference<XModel2> & _rxDocument,::std::list<Reference<XController2>> & _out_rControllers)109cdf0e10cSrcweir     static void lcl_getControllers_throw( const Reference< XModel2 >& _rxDocument,
110cdf0e10cSrcweir         ::std::list< Reference< XController2 > >& _out_rControllers )
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         _out_rControllers.clear();
113cdf0e10cSrcweir         Reference< XEnumeration > xControllerEnum( _rxDocument->getControllers(), UNO_SET_THROW );
114cdf0e10cSrcweir         while ( xControllerEnum->hasMoreElements() )
115cdf0e10cSrcweir             _out_rControllers.push_back( Reference< XController2 >( xControllerEnum->nextElement(), UNO_QUERY_THROW ) );
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	//====================================================================
119cdf0e10cSrcweir 	//= MacroMigrationDialog_Data
120cdf0e10cSrcweir 	//====================================================================
121cdf0e10cSrcweir     struct MacroMigrationDialog_Data
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         ::comphelper::ComponentContext          aContext;
124cdf0e10cSrcweir         MigrationLog                            aLogger;
125cdf0e10cSrcweir         Reference< XOfficeDatabaseDocument >    xDocument;
126cdf0e10cSrcweir         Reference< XModel2 >                    xDocumentModel;
127cdf0e10cSrcweir         ::rtl::OUString                         sSuccessfulBackupLocation;
128cdf0e10cSrcweir         bool                                    bMigrationIsRunning;
129cdf0e10cSrcweir         bool                                    bMigrationFailure;
130cdf0e10cSrcweir         bool                                    bMigrationSuccess;
131cdf0e10cSrcweir 
MacroMigrationDialog_Datadbmm::MacroMigrationDialog_Data132cdf0e10cSrcweir         MacroMigrationDialog_Data(
133cdf0e10cSrcweir                 const ::comphelper::ComponentContext& _rContext,
134cdf0e10cSrcweir                 const Reference< XOfficeDatabaseDocument >& _rxDocument )
135cdf0e10cSrcweir             :aContext( _rContext )
136cdf0e10cSrcweir             ,aLogger()
137cdf0e10cSrcweir             ,xDocument( _rxDocument )
138cdf0e10cSrcweir             ,xDocumentModel( _rxDocument, UNO_QUERY )
139cdf0e10cSrcweir             ,bMigrationIsRunning( false )
140cdf0e10cSrcweir             ,bMigrationFailure( false )
141cdf0e10cSrcweir             ,bMigrationSuccess( false )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir     };
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	//====================================================================
147cdf0e10cSrcweir 	//= MacroMigrationDialog
148cdf0e10cSrcweir 	//====================================================================
149cdf0e10cSrcweir 	//--------------------------------------------------------------------
MacroMigrationDialog(Window * _pParent,const::comphelper::ComponentContext & _rContext,const Reference<XOfficeDatabaseDocument> & _rxDocument)150cdf0e10cSrcweir     MacroMigrationDialog::MacroMigrationDialog( Window* _pParent, const ::comphelper::ComponentContext& _rContext,
151cdf0e10cSrcweir         const Reference< XOfficeDatabaseDocument >& _rxDocument )
152cdf0e10cSrcweir         :MacroMigrationDialog_Base( _pParent, MacroMigrationResId( DLG_MACRO_MIGRATION ) )
153cdf0e10cSrcweir         ,m_pData( new MacroMigrationDialog_Data( _rContext, _rxDocument ) )
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         String sTitlePrepare( MacroMigrationResId( STR_STATE_CLOSE_SUB_DOCS ) );
156cdf0e10cSrcweir         String sTitleStoreAs( MacroMigrationResId( STR_STATE_BACKUP_DBDOC ) );
157cdf0e10cSrcweir         String sTitleMigrate( MacroMigrationResId( STR_STATE_MIGRATE ) );
158cdf0e10cSrcweir         String sTitleSummary( MacroMigrationResId( STR_STATE_SUMMARY ) );
159cdf0e10cSrcweir         FreeResource();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         describeState( STATE_CLOSE_SUB_DOCS,    sTitlePrepare, &PreparationPage::Create   );
162cdf0e10cSrcweir         describeState( STATE_BACKUP_DBDOC,      sTitleStoreAs, &SaveDBDocPage::Create     );
163cdf0e10cSrcweir         describeState( STATE_MIGRATE,           sTitleMigrate, &ProgressPage::Create      );
164cdf0e10cSrcweir         describeState( STATE_SUMMARY,           sTitleSummary, &ResultPage::Create        );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         declarePath( PATH_DEFAULT, STATE_CLOSE_SUB_DOCS, STATE_BACKUP_DBDOC, STATE_MIGRATE, STATE_SUMMARY, WZS_INVALID_STATE );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         SetPageSizePixel( LogicToPixel( ::Size( TAB_PAGE_WIDTH, TAB_PAGE_HEIGHT ), MAP_APPFONT ) );
169cdf0e10cSrcweir         ShowButtonFixedLine( true );
170cdf0e10cSrcweir         SetRoadmapInteractive( true );
171cdf0e10cSrcweir         enableAutomaticNextButtonState();
172cdf0e10cSrcweir         defaultButton( WZB_NEXT );
173cdf0e10cSrcweir         enableButtons( WZB_FINISH, true );
174cdf0e10cSrcweir         ActivatePage();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         OSL_PRECOND( m_pData->xDocumentModel.is(), "MacroMigrationDialog::MacroMigrationDialog: illegal document!" );
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	//--------------------------------------------------------------------
~MacroMigrationDialog()180cdf0e10cSrcweir     MacroMigrationDialog::~MacroMigrationDialog()
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	//--------------------------------------------------------------------
getComponentContext() const185cdf0e10cSrcweir     const ::comphelper::ComponentContext& MacroMigrationDialog::getComponentContext() const
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         return m_pData->aContext;
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	//--------------------------------------------------------------------
getDocument() const191cdf0e10cSrcweir     const Reference< XOfficeDatabaseDocument >& MacroMigrationDialog::getDocument() const
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         return m_pData->xDocument;
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 	//--------------------------------------------------------------------
Execute()197cdf0e10cSrcweir     short MacroMigrationDialog::Execute()
198cdf0e10cSrcweir     {
199cdf0e10cSrcweir         short nResult = MacroMigrationDialog_Base::Execute();
200cdf0e10cSrcweir         if ( !m_pData->bMigrationFailure && !m_pData->bMigrationSuccess )
201cdf0e10cSrcweir             // migration did not even start
202cdf0e10cSrcweir             return nResult;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         OSL_ENSURE( !m_pData->bMigrationFailure || !m_pData->bMigrationSuccess,
205cdf0e10cSrcweir             "MacroMigrationDialog::Execute: success *and* failure at the same time?!" );
206cdf0e10cSrcweir         impl_reloadDocument_nothrow( m_pData->bMigrationSuccess );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         return nResult;
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	//--------------------------------------------------------------------
Close()212cdf0e10cSrcweir     sal_Bool MacroMigrationDialog::Close()
213cdf0e10cSrcweir     {
214cdf0e10cSrcweir         if ( m_pData->bMigrationIsRunning )
215cdf0e10cSrcweir             return sal_False;
216cdf0e10cSrcweir         return MacroMigrationDialog_Base::Close();
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	//--------------------------------------------------------------------
enterState(WizardState _nState)220cdf0e10cSrcweir     void MacroMigrationDialog::enterState( WizardState _nState )
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         MacroMigrationDialog_Base::enterState( _nState );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir         switch ( _nState )
225cdf0e10cSrcweir         {
226cdf0e10cSrcweir         case STATE_CLOSE_SUB_DOCS:
227cdf0e10cSrcweir             enableButtons( WZB_FINISH, false );
228cdf0e10cSrcweir             enableState( STATE_MIGRATE, false );
229cdf0e10cSrcweir             enableState( STATE_SUMMARY, false );
230cdf0e10cSrcweir             break;
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         case STATE_BACKUP_DBDOC:
233cdf0e10cSrcweir             enableState( STATE_MIGRATE, true );
234cdf0e10cSrcweir             // Note that the state is automatically disabled if the current page
235cdf0e10cSrcweir             // (SaveDBDocPage) returns false in its canAdvance, not caring that
236cdf0e10cSrcweir             // we enabled it here.
237cdf0e10cSrcweir             break;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir         case STATE_MIGRATE:
240cdf0e10cSrcweir         {
241cdf0e10cSrcweir             // disable everything. The process we will start here cannot be cancelled, the user
242cdf0e10cSrcweir             // needs to wait 'til it's finished.
243cdf0e10cSrcweir             enableState( STATE_CLOSE_SUB_DOCS, false );
244cdf0e10cSrcweir             enableState( STATE_BACKUP_DBDOC, false );
245cdf0e10cSrcweir             enableState( STATE_SUMMARY, false );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir             enableButtons( WZB_FINISH | WZB_CANCEL | WZB_PREVIOUS | WZB_NEXT, false );
248cdf0e10cSrcweir 
249cdf0e10cSrcweir             // start the migration asynchronously
250cdf0e10cSrcweir             PostUserEvent( LINK( this, MacroMigrationDialog, OnStartMigration ) );
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir         break;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         case STATE_SUMMARY:
255cdf0e10cSrcweir             // disable the previous step - we can't return to the actual migration, it already happened (or failed)
256cdf0e10cSrcweir             enableState( STATE_MIGRATE, false );
257cdf0e10cSrcweir             updateTravelUI();
258cdf0e10cSrcweir 
259cdf0e10cSrcweir             // display the results
260cdf0e10cSrcweir             dynamic_cast< ResultPage& >( *GetPage( STATE_SUMMARY ) ).displayMigrationLog(
261cdf0e10cSrcweir                 m_pData->bMigrationSuccess, m_pData->aLogger.getCompleteLog() );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir             enableButtons( WZB_FINISH, m_pData->bMigrationSuccess );
264cdf0e10cSrcweir             enableButtons( WZB_CANCEL, m_pData->bMigrationFailure );
265cdf0e10cSrcweir             defaultButton( m_pData->bMigrationSuccess ? WZB_FINISH : WZB_CANCEL );
266cdf0e10cSrcweir             break;
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         default:
269cdf0e10cSrcweir             OSL_ENSURE( false, "MacroMigrationDialog::enterState: unhandled state!" );
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 	//--------------------------------------------------------------------
prepareLeaveCurrentState(CommitPageReason _eReason)274cdf0e10cSrcweir     sal_Bool MacroMigrationDialog::prepareLeaveCurrentState( CommitPageReason _eReason )
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         if ( !MacroMigrationDialog_Base::prepareLeaveCurrentState( _eReason ) )
277cdf0e10cSrcweir             return sal_False;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir         switch ( getCurrentState() )
280cdf0e10cSrcweir         {
281cdf0e10cSrcweir         case STATE_CLOSE_SUB_DOCS:
282cdf0e10cSrcweir             if ( !impl_closeSubDocs_nothrow() )
283cdf0e10cSrcweir                 return sal_False;
284cdf0e10cSrcweir             break;
285cdf0e10cSrcweir         case STATE_BACKUP_DBDOC:
286cdf0e10cSrcweir             if ( !impl_backupDocument_nothrow() )
287cdf0e10cSrcweir                 return sal_False;
288cdf0e10cSrcweir             break;
289cdf0e10cSrcweir         case STATE_MIGRATE:
290cdf0e10cSrcweir             break;
291cdf0e10cSrcweir         case STATE_SUMMARY:
292cdf0e10cSrcweir             break;
293cdf0e10cSrcweir         default:
294cdf0e10cSrcweir             OSL_ENSURE( false, "MacroMigrationDialog::prepareLeaveCurrentState: unhandled state!" );
295cdf0e10cSrcweir         }
296cdf0e10cSrcweir 
297cdf0e10cSrcweir         return sal_True;
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	//--------------------------------------------------------------------
leaveState(WizardState _nState)301cdf0e10cSrcweir     sal_Bool MacroMigrationDialog::leaveState( WizardState _nState )
302cdf0e10cSrcweir     {
303cdf0e10cSrcweir         return MacroMigrationDialog_Base::leaveState( _nState );
304cdf0e10cSrcweir     }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	//--------------------------------------------------------------------
determineNextState(WizardState _nCurrentState) const307cdf0e10cSrcweir     MacroMigrationDialog::WizardState MacroMigrationDialog::determineNextState( WizardState _nCurrentState ) const
308cdf0e10cSrcweir     {
309cdf0e10cSrcweir         return MacroMigrationDialog_Base::determineNextState( _nCurrentState );
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	//--------------------------------------------------------------------
onFinish()313cdf0e10cSrcweir     sal_Bool MacroMigrationDialog::onFinish()
314cdf0e10cSrcweir     {
315cdf0e10cSrcweir         return MacroMigrationDialog_Base::onFinish();
316cdf0e10cSrcweir     }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	//--------------------------------------------------------------------
319cdf0e10cSrcweir     IMPL_LINK( MacroMigrationDialog, OnStartMigration, void*, /*_pNotInterestedIn*/ )
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         // prevent closing
322cdf0e10cSrcweir         m_pData->bMigrationIsRunning = true;
323cdf0e10cSrcweir 
324cdf0e10cSrcweir         // initialize migration engine and progress
325cdf0e10cSrcweir         ProgressPage& rProgressPage( dynamic_cast< ProgressPage& >( *GetPage( STATE_MIGRATE ) ) );
326cdf0e10cSrcweir         MigrationEngine aEngine( m_pData->aContext, m_pData->xDocument, rProgressPage, m_pData->aLogger );
327cdf0e10cSrcweir         rProgressPage.setDocumentCounts( aEngine.getFormCount(), aEngine.getReportCount() );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         // do the migration
330cdf0e10cSrcweir         m_pData->bMigrationSuccess = aEngine.migrateAll();
331cdf0e10cSrcweir         m_pData->bMigrationFailure = !m_pData->bMigrationSuccess;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir         // re-enable the UI
334cdf0e10cSrcweir         enableButtons( WZB_FINISH | WZB_NEXT, true );
335cdf0e10cSrcweir         enableState( STATE_SUMMARY, true );
336cdf0e10cSrcweir         updateTravelUI();
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         m_pData->bMigrationIsRunning = false;
339cdf0e10cSrcweir 
340cdf0e10cSrcweir         if ( m_pData->bMigrationSuccess )
341cdf0e10cSrcweir         {
342cdf0e10cSrcweir             rProgressPage.onFinishedSuccessfully();
343cdf0e10cSrcweir         }
344cdf0e10cSrcweir         else
345cdf0e10cSrcweir         {   // if there was an error, show the summary automatically
346cdf0e10cSrcweir             travelNext();
347cdf0e10cSrcweir         }
348cdf0e10cSrcweir 
349cdf0e10cSrcweir         // outta here
350cdf0e10cSrcweir         return 0L;
351cdf0e10cSrcweir     }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_showCloseDocsError(bool _bShow)354cdf0e10cSrcweir     void MacroMigrationDialog::impl_showCloseDocsError( bool _bShow )
355cdf0e10cSrcweir     {
356cdf0e10cSrcweir         PreparationPage* pPreparationPage = dynamic_cast< PreparationPage* >( GetPage( STATE_CLOSE_SUB_DOCS ) );
357cdf0e10cSrcweir         OSL_ENSURE( pPreparationPage, "MacroMigrationDialog::impl_showCloseDocsError: did not find the page!" );
358cdf0e10cSrcweir         if ( pPreparationPage )
359cdf0e10cSrcweir             pPreparationPage->showCloseDocsError( _bShow );
360cdf0e10cSrcweir     }
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_closeSubDocs_nothrow()363cdf0e10cSrcweir     bool MacroMigrationDialog::impl_closeSubDocs_nothrow()
364cdf0e10cSrcweir     {
365cdf0e10cSrcweir         OSL_PRECOND( m_pData->xDocument.is(), "MacroMigrationDialog::impl_closeSubDocs_nothrow: no document!" );
366cdf0e10cSrcweir         if ( !m_pData->xDocument.is() )
367cdf0e10cSrcweir             return false;
368cdf0e10cSrcweir 
369cdf0e10cSrcweir         impl_showCloseDocsError( false );
370cdf0e10cSrcweir 
371cdf0e10cSrcweir         bool bSuccess = true;
372cdf0e10cSrcweir         try
373cdf0e10cSrcweir         {
374cdf0e10cSrcweir             // collect all controllers of our document
375cdf0e10cSrcweir             ::std::list< Reference< XController2 > > aControllers;
376cdf0e10cSrcweir             lcl_getControllers_throw( m_pData->xDocumentModel, aControllers );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir             // close all sub documents of all controllers
379cdf0e10cSrcweir             for (   ::std::list< Reference< XController2 > >::const_iterator pos = aControllers.begin();
380cdf0e10cSrcweir                     pos != aControllers.end() && bSuccess;
381cdf0e10cSrcweir                     ++pos
382cdf0e10cSrcweir                 )
383cdf0e10cSrcweir             {
384cdf0e10cSrcweir                 Reference< XDatabaseDocumentUI > xController( *pos, UNO_QUERY );
385cdf0e10cSrcweir                 OSL_ENSURE( xController.is(), "MacroMigrationDialog::impl_closeSubDocs_nothrow: unexpected: controller is missing an important interface!" );
386cdf0e10cSrcweir                     // at the moment, only one implementation for a DBDoc's controller exists, which should
387cdf0e10cSrcweir                     // support this interface
388cdf0e10cSrcweir                 if ( !xController.is() )
389cdf0e10cSrcweir                     continue;
390cdf0e10cSrcweir 
391cdf0e10cSrcweir                 bSuccess = xController->closeSubComponents();
392cdf0e10cSrcweir             }
393cdf0e10cSrcweir         }
394cdf0e10cSrcweir         catch( const Exception& )
395cdf0e10cSrcweir         {
396cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
397cdf0e10cSrcweir             bSuccess = false;
398cdf0e10cSrcweir         }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir         impl_showCloseDocsError( !bSuccess );
401cdf0e10cSrcweir         return bSuccess;
402cdf0e10cSrcweir     }
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 	//--------------------------------------------------------------------
405cdf0e10cSrcweir     namespace
406cdf0e10cSrcweir     {
lcl_equalURLs_nothrow(const::rtl::OUString & _lhs,const::rtl::OUString _rhs)407cdf0e10cSrcweir         bool    lcl_equalURLs_nothrow( const ::rtl::OUString& _lhs, const ::rtl::OUString _rhs )
408cdf0e10cSrcweir         {
409cdf0e10cSrcweir             // the cheap situation: the URLs are equal
410cdf0e10cSrcweir             if ( _lhs == _rhs )
411cdf0e10cSrcweir                 return true;
412cdf0e10cSrcweir 
413cdf0e10cSrcweir             bool bEqual = true;
414cdf0e10cSrcweir             try
415cdf0e10cSrcweir             {
416cdf0e10cSrcweir 				::ucbhelper::Content aContentLHS = ::ucbhelper::Content( _lhs, Reference< XCommandEnvironment >() );
417cdf0e10cSrcweir 				::ucbhelper::Content aContentRHS = ::ucbhelper::Content( _rhs, Reference< XCommandEnvironment >() );
418cdf0e10cSrcweir 				Reference< XContent > xContentLHS( aContentLHS.get(), UNO_SET_THROW );
419cdf0e10cSrcweir 				Reference< XContent > xContentRHS( aContentRHS.get(), UNO_SET_THROW );
420cdf0e10cSrcweir 				Reference< XContentIdentifier > xID1( xContentLHS->getIdentifier(), UNO_SET_THROW );
421cdf0e10cSrcweir 				Reference< XContentIdentifier > xID2( xContentRHS->getIdentifier(), UNO_SET_THROW );
422cdf0e10cSrcweir 
423cdf0e10cSrcweir                 ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
424cdf0e10cSrcweir                 Reference< XContentProvider > xProvider(
425cdf0e10cSrcweir                     pBroker ? pBroker->getContentProviderInterface() : Reference< XContentProvider >(), UNO_SET_THROW );
426cdf0e10cSrcweir 
427cdf0e10cSrcweir                 bEqual = ( 0 == xProvider->compareContentIds( xID1, xID2 ) );
428cdf0e10cSrcweir             }
429cdf0e10cSrcweir             catch( const Exception& )
430cdf0e10cSrcweir             {
431cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
432cdf0e10cSrcweir             }
433cdf0e10cSrcweir             return bEqual;
434cdf0e10cSrcweir         }
435cdf0e10cSrcweir     }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_backupDocument_nothrow() const438cdf0e10cSrcweir     bool MacroMigrationDialog::impl_backupDocument_nothrow() const
439cdf0e10cSrcweir     {
440cdf0e10cSrcweir         if ( !m_pData->xDocumentModel.is() )
441cdf0e10cSrcweir             // should never happen, but has been reported as assertion before
442cdf0e10cSrcweir             return false;
443cdf0e10cSrcweir 
444cdf0e10cSrcweir         SaveDBDocPage& rBackupPage = dynamic_cast< SaveDBDocPage& >( *GetPage( STATE_BACKUP_DBDOC ) );
445cdf0e10cSrcweir         ::rtl::OUString sBackupLocation( rBackupPage.getBackupLocation() );
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         Any aError;
448cdf0e10cSrcweir         try
449cdf0e10cSrcweir         {
450cdf0e10cSrcweir             // check that the backup location isn't the same as the document itself
451cdf0e10cSrcweir             if ( lcl_equalURLs_nothrow( sBackupLocation, m_pData->xDocumentModel->getURL() ) )
452cdf0e10cSrcweir             {
453cdf0e10cSrcweir                 ErrorBox aErrorBox( const_cast< MacroMigrationDialog* >( this ), MacroMigrationResId( ERR_INVALID_BACKUP_LOCATION ) );
454cdf0e10cSrcweir                 aErrorBox.Execute();
455cdf0e10cSrcweir                 rBackupPage.grabLocationFocus();
456cdf0e10cSrcweir                 return false;
457cdf0e10cSrcweir             }
458cdf0e10cSrcweir 
459cdf0e10cSrcweir             // store to the backup location
460cdf0e10cSrcweir             const Reference< XStorable > xDocument( getDocument(), UNO_QUERY_THROW );
461cdf0e10cSrcweir             xDocument->storeToURL( sBackupLocation, Sequence< PropertyValue >() );
462cdf0e10cSrcweir             m_pData->sSuccessfulBackupLocation = sBackupLocation;
463cdf0e10cSrcweir         }
464cdf0e10cSrcweir         catch( const Exception& )
465cdf0e10cSrcweir         {
466cdf0e10cSrcweir             aError = ::cppu::getCaughtException();
467cdf0e10cSrcweir         }
468cdf0e10cSrcweir         if ( !aError.hasValue() )
469cdf0e10cSrcweir         {
470cdf0e10cSrcweir             ::svt::OFileNotation aFileNotation( sBackupLocation );
471cdf0e10cSrcweir             m_pData->aLogger.backedUpDocument( aFileNotation.get( ::svt::OFileNotation::N_SYSTEM ) );
472cdf0e10cSrcweir             return true;
473cdf0e10cSrcweir         }
474cdf0e10cSrcweir 
475cdf0e10cSrcweir         // display the error to the user
476cdf0e10cSrcweir         InteractionHandler aHandler( m_pData->aContext, m_pData->xDocumentModel.get() );
477cdf0e10cSrcweir         aHandler.reportError( aError );
478cdf0e10cSrcweir 
479cdf0e10cSrcweir         m_pData->aLogger.logFailure( MigrationError(
480cdf0e10cSrcweir             ERR_DOCUMENT_BACKUP_FAILED,
481cdf0e10cSrcweir             sBackupLocation,
482cdf0e10cSrcweir             aError
483cdf0e10cSrcweir         ) );
484cdf0e10cSrcweir 
485cdf0e10cSrcweir         return false;
486cdf0e10cSrcweir     }
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_reloadDocument_nothrow(bool _bMigrationSuccess)489cdf0e10cSrcweir     void MacroMigrationDialog::impl_reloadDocument_nothrow( bool _bMigrationSuccess )
490cdf0e10cSrcweir     {
491cdf0e10cSrcweir         typedef ::std::pair< Reference< XFrame >, ::rtl::OUString > ViewDescriptor;
492cdf0e10cSrcweir         ::std::list< ViewDescriptor > aViews;
493cdf0e10cSrcweir 
494cdf0e10cSrcweir         try
495cdf0e10cSrcweir         {
496cdf0e10cSrcweir             // the information which is necessary to reload the document
497cdf0e10cSrcweir             ::rtl::OUString                     sDocumentURL ( m_pData->xDocumentModel->getURL()  );
498cdf0e10cSrcweir             ::comphelper::NamedValueCollection  aDocumentArgs( m_pData->xDocumentModel->getArgs() );
499cdf0e10cSrcweir             if ( !_bMigrationSuccess )
500cdf0e10cSrcweir             {
501cdf0e10cSrcweir                 // if the migration was not successful, then reload from the backup
502cdf0e10cSrcweir                 aDocumentArgs.put( "SalvagedFile", m_pData->sSuccessfulBackupLocation );
503cdf0e10cSrcweir                 // reset the modified flag of the document, so the controller can be suspended later
504cdf0e10cSrcweir                 Reference< XModifiable > xModify( m_pData->xDocument, UNO_QUERY_THROW );
505cdf0e10cSrcweir                 xModify->setModified( sal_False );
506cdf0e10cSrcweir                 // after this reload, don't show the migration warning, again
507cdf0e10cSrcweir                 aDocumentArgs.put( "SuppressMigrationWarning", sal_Bool(sal_True) );
508cdf0e10cSrcweir             }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir             // remove anything from the args which might refer to the old document
511cdf0e10cSrcweir             aDocumentArgs.remove( "Model" );
512cdf0e10cSrcweir             aDocumentArgs.remove( "Stream" );
513cdf0e10cSrcweir             aDocumentArgs.remove( "InputStream" );
514cdf0e10cSrcweir             aDocumentArgs.remove( "FileName" );
515cdf0e10cSrcweir             aDocumentArgs.remove( "URL" );
516cdf0e10cSrcweir 
517cdf0e10cSrcweir             // collect all controllers of our document
518cdf0e10cSrcweir             ::std::list< Reference< XController2 > > aControllers;
519cdf0e10cSrcweir             lcl_getControllers_throw( m_pData->xDocumentModel, aControllers );
520cdf0e10cSrcweir 
521cdf0e10cSrcweir             // close all those controllers
522cdf0e10cSrcweir             while ( !aControllers.empty() )
523cdf0e10cSrcweir             {
524cdf0e10cSrcweir                 Reference< XController2 > xController( aControllers.front(), UNO_SET_THROW );
525cdf0e10cSrcweir                 aControllers.pop_front();
526cdf0e10cSrcweir 
527cdf0e10cSrcweir                 Reference< XFrame > xFrame( xController->getFrame(), UNO_SET_THROW );
528cdf0e10cSrcweir                 ::rtl::OUString sViewName( xController->getViewControllerName() );
529cdf0e10cSrcweir 
530cdf0e10cSrcweir                 if ( !xController->suspend( sal_True ) )
531cdf0e10cSrcweir                 {   // ouch. There shouldn't be any modal dialogs and such, so there
532cdf0e10cSrcweir                     // really is no reason why suspending shouldn't work.
533cdf0e10cSrcweir                     OSL_ENSURE( false, "MacroMigrationDialog::impl_reloadDocument_nothrow: could not suspend a controller!" );
534cdf0e10cSrcweir                     // ignoring this would be at the cost of a crash (potentially)
535cdf0e10cSrcweir                     // so, we cannot continue here.
536cdf0e10cSrcweir                     throw CloseVetoException();
537cdf0e10cSrcweir                 }
538cdf0e10cSrcweir 
539cdf0e10cSrcweir                 aViews.push_back( ViewDescriptor( xFrame, sViewName ) );
540cdf0e10cSrcweir                 xFrame->setComponent( NULL, NULL );
541cdf0e10cSrcweir                 xController->dispose();
542cdf0e10cSrcweir             }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir             // Note the document is closed now - disconnecting the last controller
545cdf0e10cSrcweir             // closes it automatically.
546cdf0e10cSrcweir 
547cdf0e10cSrcweir             Reference< XOfficeDatabaseDocument > xNewDocument;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir             // re-create the views
550cdf0e10cSrcweir             while ( !aViews.empty() )
551cdf0e10cSrcweir             {
552cdf0e10cSrcweir                 ViewDescriptor aView( aViews.front() );
553cdf0e10cSrcweir                 aViews.pop_front();
554cdf0e10cSrcweir 
555cdf0e10cSrcweir                 // load the document into this frame
556cdf0e10cSrcweir                 Reference< XComponentLoader > xLoader( aView.first, UNO_QUERY_THROW );
557cdf0e10cSrcweir                 aDocumentArgs.put( "ViewName", aView.second );
558cdf0e10cSrcweir                 Reference< XInterface > xReloaded( xLoader->loadComponentFromURL(
559cdf0e10cSrcweir                     sDocumentURL,
560cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_self" ) ),
561cdf0e10cSrcweir                     0,
562cdf0e10cSrcweir                     aDocumentArgs.getPropertyValues()
563cdf0e10cSrcweir                 ) );
564cdf0e10cSrcweir 
565cdf0e10cSrcweir                 OSL_ENSURE( xReloaded != m_pData->xDocumentModel,
566cdf0e10cSrcweir                     "MacroMigrationDialog::impl_reloadDocument_nothrow: this should have been a new instance!" );
567cdf0e10cSrcweir                     // this would be unexpected, but recoverable: The loader should at least have done
568cdf0e10cSrcweir                     // this: really *load* the document, even if it loaded it into the old document instance
569cdf0e10cSrcweir                 if ( !xNewDocument.is() )
570cdf0e10cSrcweir                 {
571cdf0e10cSrcweir                     xNewDocument.set( xReloaded, UNO_QUERY_THROW );
572cdf0e10cSrcweir                     // for subsequent loads, into different frames, put the document into the load args
573cdf0e10cSrcweir                     aDocumentArgs.put( "Model", xNewDocument );
574cdf0e10cSrcweir                 }
575cdf0e10cSrcweir                 #if OSL_DEBUG_LEVEL > 0
576cdf0e10cSrcweir                 else
577cdf0e10cSrcweir                 {
578cdf0e10cSrcweir                     OSL_ENSURE( xNewDocument == xReloaded,
579cdf0e10cSrcweir                         "MacroMigrationDialog::impl_reloadDocument_nothrow: unexpected: subsequent load attempt returned a wrong document!" );
580cdf0e10cSrcweir                 }
581cdf0e10cSrcweir                 #endif
582cdf0e10cSrcweir             }
583cdf0e10cSrcweir 
584cdf0e10cSrcweir             m_pData->xDocument = xNewDocument;
585cdf0e10cSrcweir             m_pData->xDocumentModel.set( xNewDocument, UNO_QUERY );
586cdf0e10cSrcweir 
587cdf0e10cSrcweir             // finally, now that the document has been reloaded - if the migration was not successful,
588cdf0e10cSrcweir             // then it was reloaded from the backup, but the real document still is broken. So, save
589cdf0e10cSrcweir             // the document once, which will write the content loaded from the backup to the real docfile.
590cdf0e10cSrcweir             if ( !_bMigrationSuccess )
591cdf0e10cSrcweir             {
592cdf0e10cSrcweir                 Reference< XModifiable > xModify( m_pData->xDocument, UNO_QUERY_THROW );
593cdf0e10cSrcweir                 xModify->setModified( sal_True );
594cdf0e10cSrcweir                     // this is just parnoia - in case saving the doc fails, perhaps the user is tempted to do so
595cdf0e10cSrcweir                 Reference< XStorable > xStor( m_pData->xDocument, UNO_QUERY_THROW );
596cdf0e10cSrcweir                 xStor->store();
597cdf0e10cSrcweir             }
598cdf0e10cSrcweir         }
599cdf0e10cSrcweir         catch( const Exception& )
600cdf0e10cSrcweir         {
601cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
602cdf0e10cSrcweir         }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir         // close all frames from aViews - the respective controllers have been closed, but
605cdf0e10cSrcweir         // reloading didn't work, so the frames are zombies now.
606cdf0e10cSrcweir         while ( !aViews.empty() )
607cdf0e10cSrcweir         {
608cdf0e10cSrcweir             ViewDescriptor aView( aViews.front() );
609cdf0e10cSrcweir             aViews.pop_front();
610cdf0e10cSrcweir             try
611cdf0e10cSrcweir             {
612cdf0e10cSrcweir                 Reference< XCloseable > xFrameClose( aView.first, UNO_QUERY_THROW );
613cdf0e10cSrcweir                 xFrameClose->close( sal_True );
614cdf0e10cSrcweir             }
615cdf0e10cSrcweir             catch( const Exception& )
616cdf0e10cSrcweir             {
617cdf0e10cSrcweir         	    DBG_UNHANDLED_EXCEPTION();
618cdf0e10cSrcweir             }
619cdf0e10cSrcweir         }
620cdf0e10cSrcweir     }
621cdf0e10cSrcweir 
622cdf0e10cSrcweir //........................................................................
623cdf0e10cSrcweir } // namespace dbmm
624cdf0e10cSrcweir //........................................................................
625