1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "dbmm_global.hrc"
28cdf0e10cSrcweir #include "dbmm_module.hxx"
29cdf0e10cSrcweir #include "dbmm_types.hxx"
30cdf0e10cSrcweir #include "docinteraction.hxx"
31cdf0e10cSrcweir #include "migrationengine.hxx"
32cdf0e10cSrcweir #include "migrationerror.hxx"
33cdf0e10cSrcweir #include "migrationprogress.hxx"
34cdf0e10cSrcweir #include "migrationlog.hxx"
35cdf0e10cSrcweir #include "progresscapture.hxx"
36cdf0e10cSrcweir #include "progressmixer.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /** === begin UNO includes === **/
39cdf0e10cSrcweir #include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
41cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
42cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
43cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandProcessor.hpp>
45cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
46cdf0e10cSrcweir #include <com/sun/star/embed/XComponentSupplier.hpp>
47cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
48cdf0e10cSrcweir #include <com/sun/star/document/XStorageBasedDocument.hpp>
49cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
50cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
51cdf0e10cSrcweir #include <com/sun/star/embed/XEmbedPersist.hpp>
52cdf0e10cSrcweir #include <com/sun/star/script/DocumentScriptLibraryContainer.hpp>
53cdf0e10cSrcweir #include <com/sun/star/script/DocumentDialogLibraryContainer.hpp>
54cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedScripts.hpp>
55cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
56cdf0e10cSrcweir #include <com/sun/star/uri/UriReferenceFactory.hpp>
57cdf0e10cSrcweir #include <com/sun/star/uri/XVndSunStarScriptUrlReference.hpp>
58cdf0e10cSrcweir #include <com/sun/star/form/XFormsSupplier.hpp>
59cdf0e10cSrcweir #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
60cdf0e10cSrcweir #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
61cdf0e10cSrcweir #include <com/sun/star/script/XEventAttacherManager.hpp>
62cdf0e10cSrcweir #include <com/sun/star/script/XLibraryContainerPassword.hpp>
63cdf0e10cSrcweir #include <com/sun/star/io/WrongFormatException.hpp>
64cdf0e10cSrcweir #include <com/sun/star/script/XScriptEventsSupplier.hpp>
65cdf0e10cSrcweir #include <com/sun/star/io/XInputStreamProvider.hpp>
66cdf0e10cSrcweir /** === end UNO includes === **/
67cdf0e10cSrcweir 
68cdf0e10cSrcweir #include <comphelper/documentinfo.hxx>
69cdf0e10cSrcweir #include <comphelper/interaction.hxx>
70cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
71cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
72cdf0e10cSrcweir #include <comphelper/string.hxx>
73cdf0e10cSrcweir #include <comphelper/types.hxx>
74cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
75cdf0e10cSrcweir #include <tools/string.hxx>
76cdf0e10cSrcweir #include <tools/diagnose_ex.h>
77cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
78cdf0e10cSrcweir #include <rtl/ref.hxx>
79cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx>
80cdf0e10cSrcweir #include <xmlscript/xmldlg_imexp.hxx>
81cdf0e10cSrcweir 
82cdf0e10cSrcweir #include <vector>
83cdf0e10cSrcweir #include <set>
84cdf0e10cSrcweir 
85cdf0e10cSrcweir #define DEFAULT_DOC_PROGRESS_RANGE  100000
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //........................................................................
88cdf0e10cSrcweir namespace dbmm
89cdf0e10cSrcweir {
90cdf0e10cSrcweir //........................................................................
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	/** === begin UNO using === **/
93cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
94cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
95cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
96cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
97cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
98cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
99cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
100cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
101cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
102cdf0e10cSrcweir     using ::com::sun::star::sdb::XOfficeDatabaseDocument;
103cdf0e10cSrcweir     using ::com::sun::star::sdb::XFormDocumentsSupplier;
104cdf0e10cSrcweir     using ::com::sun::star::sdb::XReportDocumentsSupplier;
105cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
106cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
107cdf0e10cSrcweir     using ::com::sun::star::util::XCloseable;
108cdf0e10cSrcweir     using ::com::sun::star::util::CloseVetoException;
109cdf0e10cSrcweir     using ::com::sun::star::lang::XComponent;
110cdf0e10cSrcweir     using ::com::sun::star::frame::XModel;
111cdf0e10cSrcweir     using ::com::sun::star::frame::XComponentLoader;
112cdf0e10cSrcweir     using ::com::sun::star::ucb::XCommandProcessor;
113cdf0e10cSrcweir     using ::com::sun::star::ucb::XContent;
114cdf0e10cSrcweir     using ::com::sun::star::ucb::Command;
115cdf0e10cSrcweir     using ::com::sun::star::embed::XComponentSupplier;
116cdf0e10cSrcweir     using ::com::sun::star::task::XStatusIndicator;
117cdf0e10cSrcweir     using ::com::sun::star::embed::XStorage;
118cdf0e10cSrcweir     using ::com::sun::star::document::XStorageBasedDocument;
119cdf0e10cSrcweir     using ::com::sun::star::embed::XTransactedObject;
120cdf0e10cSrcweir     using ::com::sun::star::frame::XStorable;
121cdf0e10cSrcweir     using ::com::sun::star::embed::XEmbedPersist;
122cdf0e10cSrcweir     using ::com::sun::star::script::DocumentDialogLibraryContainer;
123cdf0e10cSrcweir     using ::com::sun::star::script::DocumentScriptLibraryContainer;
124cdf0e10cSrcweir     using ::com::sun::star::script::XStorageBasedLibraryContainer;
125cdf0e10cSrcweir     using ::com::sun::star::document::XEmbeddedScripts;
126cdf0e10cSrcweir     using ::com::sun::star::container::XNameContainer;
127cdf0e10cSrcweir     using ::com::sun::star::document::XEventsSupplier;
128cdf0e10cSrcweir     using ::com::sun::star::container::XNameReplace;
129cdf0e10cSrcweir     using com::sun::star::uri::UriReferenceFactory;
130cdf0e10cSrcweir     using com::sun::star::uri::XUriReferenceFactory;
131cdf0e10cSrcweir     using com::sun::star::uri::XVndSunStarScriptUrlReference;
132cdf0e10cSrcweir     using ::com::sun::star::form::XFormsSupplier;
133cdf0e10cSrcweir     using ::com::sun::star::drawing::XDrawPageSupplier;
134cdf0e10cSrcweir     using ::com::sun::star::drawing::XDrawPagesSupplier;
135cdf0e10cSrcweir     using ::com::sun::star::drawing::XDrawPage;
136cdf0e10cSrcweir     using ::com::sun::star::drawing::XDrawPages;
137cdf0e10cSrcweir     using ::com::sun::star::container::XIndexAccess;
138cdf0e10cSrcweir     using ::com::sun::star::script::XEventAttacherManager;
139cdf0e10cSrcweir     using ::com::sun::star::script::ScriptEventDescriptor;
140cdf0e10cSrcweir     using ::com::sun::star::script::XLibraryContainerPassword;
141cdf0e10cSrcweir     using ::com::sun::star::io::WrongFormatException;
142cdf0e10cSrcweir     using ::com::sun::star::script::XScriptEventsSupplier;
143cdf0e10cSrcweir     using ::com::sun::star::io::XInputStreamProvider;
144cdf0e10cSrcweir     using ::com::sun::star::io::XInputStream;
145cdf0e10cSrcweir     /** === end UNO using === **/
146cdf0e10cSrcweir     namespace ElementModes = ::com::sun::star::embed::ElementModes;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir // migration phases whose progresses are to be mixed into one progress
149cdf0e10cSrcweir #define PHASE_JAVASCRIPT    1
150cdf0e10cSrcweir #define PHASE_BEANSHELL     2
151cdf0e10cSrcweir #define PHASE_PYTHON        3
152cdf0e10cSrcweir #define PHASE_JAVA          4
153cdf0e10cSrcweir #define PHASE_BASIC         5
154cdf0e10cSrcweir #define PHASE_DIALOGS       6
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     //====================================================================
157cdf0e10cSrcweir     //= SubDocument
158cdf0e10cSrcweir     //====================================================================
159cdf0e10cSrcweir     struct SubDocument
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         Reference< XCommandProcessor >  xCommandProcessor;
162cdf0e10cSrcweir         Reference< XModel >             xDocument;          // valid only temporarily
163cdf0e10cSrcweir         ::rtl::OUString                 sHierarchicalName;
164cdf0e10cSrcweir         SubDocumentType                 eType;
165cdf0e10cSrcweir         size_t                          nNumber;
166cdf0e10cSrcweir 
SubDocumentdbmm::SubDocument167cdf0e10cSrcweir         SubDocument( const Reference< XCommandProcessor >& _rxCommandProcessor, const ::rtl::OUString& _rName,
168cdf0e10cSrcweir                 const SubDocumentType _eType, const size_t _nNumber )
169cdf0e10cSrcweir             :xCommandProcessor( _rxCommandProcessor )
170cdf0e10cSrcweir             ,xDocument()
171cdf0e10cSrcweir             ,sHierarchicalName( _rName )
172cdf0e10cSrcweir             ,eType( _eType )
173cdf0e10cSrcweir             ,nNumber( _nNumber )
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir     };
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     typedef ::std::vector< SubDocument >    SubDocuments;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     //====================================================================
181cdf0e10cSrcweir 	//= helper
182cdf0e10cSrcweir 	//====================================================================
183cdf0e10cSrcweir     //--------------------------------------------------------------------
184cdf0e10cSrcweir     typedef ::utl::SharedUNOComponent< XStorage >   SharedStorage;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     namespace
187cdf0e10cSrcweir     {
188cdf0e10cSrcweir 	    //----------------------------------------------------------------
lcl_getScriptsStorageName()189cdf0e10cSrcweir         static const ::rtl::OUString& lcl_getScriptsStorageName()
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             static const ::rtl::OUString s_sScriptsStorageName( RTL_CONSTASCII_USTRINGPARAM( "Scripts" ) );
192cdf0e10cSrcweir             return s_sScriptsStorageName;
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	    //----------------------------------------------------------------
lcl_getScriptsSubStorageName(const ScriptType _eType)196cdf0e10cSrcweir         static const ::rtl::OUString& lcl_getScriptsSubStorageName( const ScriptType _eType )
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             static const ::rtl::OUString s_sBeanShell ( RTL_CONSTASCII_USTRINGPARAM( "beanshell" ) );
199cdf0e10cSrcweir             static const ::rtl::OUString s_sJavaScript( RTL_CONSTASCII_USTRINGPARAM( "javascript" ) );
200cdf0e10cSrcweir             static const ::rtl::OUString s_sPython    ( RTL_CONSTASCII_USTRINGPARAM( "python" ) );      // TODO: is this correct?
201cdf0e10cSrcweir             static const ::rtl::OUString s_sJava      ( RTL_CONSTASCII_USTRINGPARAM( "java" ) );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             switch ( _eType )
204cdf0e10cSrcweir             {
205cdf0e10cSrcweir             case eBeanShell:    return s_sBeanShell;
206cdf0e10cSrcweir             case eJavaScript:   return s_sJavaScript;
207cdf0e10cSrcweir             case ePython:       return s_sPython;
208cdf0e10cSrcweir             case eJava:         return s_sJava;
209cdf0e10cSrcweir             default:
210cdf0e10cSrcweir                 break;
211cdf0e10cSrcweir             }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             OSL_ENSURE( false, "lcl_getScriptsSubStorageName: illegal type!" );
214cdf0e10cSrcweir             static ::rtl::OUString s_sEmpty;
215cdf0e10cSrcweir             return s_sEmpty;
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	    //----------------------------------------------------------------
lcl_getScriptTypeFromLanguage(const::rtl::OUString & _rLanguage,ScriptType & _out_rScriptType)219cdf0e10cSrcweir         static bool lcl_getScriptTypeFromLanguage( const ::rtl::OUString& _rLanguage, ScriptType& _out_rScriptType )
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             struct LanguageMapping
222cdf0e10cSrcweir             {
223cdf0e10cSrcweir                 const sal_Char*     pAsciiLanguage;
224cdf0e10cSrcweir                 const ScriptType    eScriptType;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir                 LanguageMapping( const sal_Char* _pAsciiLanguage, const ScriptType _eScriptType )
227cdf0e10cSrcweir                     :pAsciiLanguage( _pAsciiLanguage )
228cdf0e10cSrcweir                     ,eScriptType( _eScriptType )
229cdf0e10cSrcweir                 {
230cdf0e10cSrcweir                 }
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir             aLanguageMapping[] =
233cdf0e10cSrcweir             {
234cdf0e10cSrcweir                 LanguageMapping( "JavaScript", eJavaScript ),
235cdf0e10cSrcweir                 LanguageMapping( "BeanShell",  eBeanShell ),
236cdf0e10cSrcweir                 LanguageMapping( "Java",       eJava ),
237cdf0e10cSrcweir                 LanguageMapping( "Python",     ePython ),          // TODO: is this correct?
238cdf0e10cSrcweir                 LanguageMapping( "Basic",      eBasic )
239cdf0e10cSrcweir             };
240cdf0e10cSrcweir             for ( size_t i=0; i < sizeof( aLanguageMapping ) / sizeof( aLanguageMapping[0] ); ++i )
241cdf0e10cSrcweir             {
242cdf0e10cSrcweir                 if ( _rLanguage.equalsAscii( aLanguageMapping[i].pAsciiLanguage ) )
243cdf0e10cSrcweir                 {
244cdf0e10cSrcweir                     _out_rScriptType = aLanguageMapping[i].eScriptType;
245cdf0e10cSrcweir                     return true;
246cdf0e10cSrcweir                 }
247cdf0e10cSrcweir             }
248cdf0e10cSrcweir             OSL_ENSURE( false, "lcl_getScriptTypeFromLanguage: unknown language!" );
249cdf0e10cSrcweir             return false;
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_getSubDocumentDescription(const SubDocument & _rDocument)253cdf0e10cSrcweir         ::rtl::OUString lcl_getSubDocumentDescription( const SubDocument& _rDocument )
254cdf0e10cSrcweir         {
255cdf0e10cSrcweir             ::rtl::OUString sObjectName = String( MacroMigrationResId( _rDocument.eType == eForm ? STR_FORM : STR_REPORT ) );
256cdf0e10cSrcweir             ::comphelper::string::searchAndReplaceAsciiI( sObjectName, "$name$", _rDocument.sHierarchicalName );
257cdf0e10cSrcweir             return sObjectName;
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_executeCommand_throw(const Reference<XCommandProcessor> & _rxCommandProc,const sal_Char * _pAsciiCommand)261cdf0e10cSrcweir         static Any lcl_executeCommand_throw( const Reference< XCommandProcessor >& _rxCommandProc,
262cdf0e10cSrcweir             const sal_Char* _pAsciiCommand )
263cdf0e10cSrcweir         {
264cdf0e10cSrcweir             OSL_PRECOND( _rxCommandProc.is(), "lcl_executeCommand_throw: illegal object!" );
265cdf0e10cSrcweir             if ( !_rxCommandProc.is() )
266cdf0e10cSrcweir                 return Any();
267cdf0e10cSrcweir 
268cdf0e10cSrcweir             Command aCommand;
269cdf0e10cSrcweir             aCommand.Name = ::rtl::OUString::createFromAscii( _pAsciiCommand );
270cdf0e10cSrcweir             return _rxCommandProc->execute(
271cdf0e10cSrcweir                 aCommand, _rxCommandProc->createCommandIdentifier(), NULL );
272cdf0e10cSrcweir         }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_getMimeType_nothrow(const Reference<XCommandProcessor> & _rxContent)275cdf0e10cSrcweir         ::rtl::OUString lcl_getMimeType_nothrow( const Reference< XCommandProcessor >& _rxContent )
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             ::rtl::OUString sMimeType;
278cdf0e10cSrcweir             try
279cdf0e10cSrcweir             {
280cdf0e10cSrcweir                 Reference< XContent > xContent( _rxContent, UNO_QUERY_THROW );
281cdf0e10cSrcweir                 sMimeType = xContent->getContentType();
282cdf0e10cSrcweir             }
283cdf0e10cSrcweir             catch( const Exception& )
284cdf0e10cSrcweir             {
285cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
286cdf0e10cSrcweir             }
287cdf0e10cSrcweir             return sMimeType;
288cdf0e10cSrcweir         }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir         //----------------------------------------------------------------
291cdf0e10cSrcweir         enum OpenDocResult
292cdf0e10cSrcweir         {
293cdf0e10cSrcweir             eOpenedDoc,
294cdf0e10cSrcweir             eIgnoreDoc,
295cdf0e10cSrcweir             eFailure
296cdf0e10cSrcweir         };
297cdf0e10cSrcweir 
298cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_loadSubDocument_nothrow(SubDocument & _rDocument,const Reference<XStatusIndicator> & _rxProgress,MigrationLog & _rLogger)299cdf0e10cSrcweir         static OpenDocResult lcl_loadSubDocument_nothrow( SubDocument& _rDocument,
300cdf0e10cSrcweir             const Reference< XStatusIndicator >& _rxProgress, MigrationLog& _rLogger )
301cdf0e10cSrcweir         {
302cdf0e10cSrcweir             OSL_PRECOND( !_rDocument.xDocument.is(), "lcl_loadSubDocument_nothrow: already loaded!" );
303cdf0e10cSrcweir 
304cdf0e10cSrcweir             try
305cdf0e10cSrcweir             {
306cdf0e10cSrcweir                 ::comphelper::NamedValueCollection aLoadArgs;
307cdf0e10cSrcweir                 aLoadArgs.put( "Hidden", (sal_Bool)sal_True );
308cdf0e10cSrcweir                 aLoadArgs.put( "StatusIndicator", _rxProgress );
309cdf0e10cSrcweir 
310cdf0e10cSrcweir                 Reference< XCommandProcessor > xCommandProcessor( _rDocument.xCommandProcessor, UNO_SET_THROW );
311cdf0e10cSrcweir                 Command aCommand;
312cdf0e10cSrcweir                 aCommand.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "openDesign" ) );
313cdf0e10cSrcweir                 aCommand.Argument <<= aLoadArgs.getPropertyValues();
314cdf0e10cSrcweir                 Reference< XComponent > xDocComponent(
315cdf0e10cSrcweir                     xCommandProcessor->execute(
316cdf0e10cSrcweir                         aCommand, xCommandProcessor->createCommandIdentifier(), NULL
317cdf0e10cSrcweir                     ),
318cdf0e10cSrcweir                     UNO_QUERY
319cdf0e10cSrcweir                 );
320cdf0e10cSrcweir                 OSL_ENSURE( xDocComponent.is(), "lcl_loadSubDocument_nothrow: no component loaded!" );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir                 _rDocument.xDocument.set( xDocComponent, UNO_QUERY_THROW );
323cdf0e10cSrcweir             }
324cdf0e10cSrcweir             catch( const Exception& )
325cdf0e10cSrcweir             {
326cdf0e10cSrcweir                 Any aError( ::cppu::getCaughtException() );
327cdf0e10cSrcweir 
328cdf0e10cSrcweir                 bool bCausedByNewStyleReport =
329cdf0e10cSrcweir                         ( _rDocument.eType == eReport )
330cdf0e10cSrcweir                     &&  ( aError.isExtractableTo( ::cppu::UnoType< WrongFormatException >::get() ) )
331cdf0e10cSrcweir                     &&  ( lcl_getMimeType_nothrow( _rDocument.xCommandProcessor ).equalsAscii( "application/vnd.sun.xml.report" ) );
332cdf0e10cSrcweir 
333cdf0e10cSrcweir                 if ( bCausedByNewStyleReport )
334cdf0e10cSrcweir                 {
335cdf0e10cSrcweir                     _rLogger.logRecoverable( MigrationError(
336cdf0e10cSrcweir                         ERR_NEW_STYLE_REPORT,
337cdf0e10cSrcweir                         lcl_getSubDocumentDescription( _rDocument )
338cdf0e10cSrcweir                     ) );
339cdf0e10cSrcweir                     return eIgnoreDoc;
340cdf0e10cSrcweir                 }
341cdf0e10cSrcweir                 else
342cdf0e10cSrcweir                 {
343cdf0e10cSrcweir                     _rLogger.logFailure( MigrationError(
344cdf0e10cSrcweir                         ERR_OPENING_SUB_DOCUMENT_FAILED,
345cdf0e10cSrcweir                         lcl_getSubDocumentDescription( _rDocument ),
346cdf0e10cSrcweir                         aError
347cdf0e10cSrcweir                     ) );
348cdf0e10cSrcweir                 }
349cdf0e10cSrcweir             }
350cdf0e10cSrcweir             return _rDocument.xDocument.is() ? eOpenedDoc : eFailure;
351cdf0e10cSrcweir         }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 	    //----------------------------------------------------------------
lcl_unloadSubDocument_nothrow(SubDocument & _rDocument,MigrationLog & _rLogger)354cdf0e10cSrcweir         static bool lcl_unloadSubDocument_nothrow( SubDocument& _rDocument, MigrationLog& _rLogger )
355cdf0e10cSrcweir         {
356cdf0e10cSrcweir             bool bSuccess = false;
357cdf0e10cSrcweir             Any aException;
358cdf0e10cSrcweir             try
359cdf0e10cSrcweir             {
360cdf0e10cSrcweir                 OSL_VERIFY( lcl_executeCommand_throw( _rDocument.xCommandProcessor, "close" ) >>= bSuccess );
361cdf0e10cSrcweir             }
362cdf0e10cSrcweir             catch( const Exception& )
363cdf0e10cSrcweir             {
364cdf0e10cSrcweir                 aException = ::cppu::getCaughtException();
365cdf0e10cSrcweir             }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir             // log the failure, if any
368cdf0e10cSrcweir             if ( !bSuccess )
369cdf0e10cSrcweir             {
370cdf0e10cSrcweir                 _rLogger.logFailure( MigrationError(
371cdf0e10cSrcweir                     ERR_CLOSING_SUB_DOCUMENT_FAILED,
372cdf0e10cSrcweir                     lcl_getSubDocumentDescription( _rDocument ),
373cdf0e10cSrcweir                     aException
374cdf0e10cSrcweir                 ) );
375cdf0e10cSrcweir             }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir             _rDocument.xDocument.clear();
378cdf0e10cSrcweir             return bSuccess;
379cdf0e10cSrcweir         }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_commitStorage_nothrow(const Reference<XStorage> & _rxStorage)382cdf0e10cSrcweir         bool lcl_commitStorage_nothrow( const Reference< XStorage >& _rxStorage )
383cdf0e10cSrcweir         {
384cdf0e10cSrcweir             try
385cdf0e10cSrcweir             {
386cdf0e10cSrcweir                 Reference< XTransactedObject > xTrans( _rxStorage, UNO_QUERY_THROW );
387cdf0e10cSrcweir                 xTrans->commit();
388cdf0e10cSrcweir             }
389cdf0e10cSrcweir             catch( const Exception& )
390cdf0e10cSrcweir             {
391cdf0e10cSrcweir             	return false;
392cdf0e10cSrcweir             }
393cdf0e10cSrcweir             return true;
394cdf0e10cSrcweir         }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_commitDocumentStorage_nothrow(const Reference<XModel> & _rxDocument,MigrationLog & _rLogger)397cdf0e10cSrcweir         bool lcl_commitDocumentStorage_nothrow( const Reference< XModel >& _rxDocument, MigrationLog& _rLogger )
398cdf0e10cSrcweir         {
399cdf0e10cSrcweir             bool bSuccess = false;
400cdf0e10cSrcweir             Any aException;
401cdf0e10cSrcweir             try
402cdf0e10cSrcweir             {
403cdf0e10cSrcweir                 Reference< XStorageBasedDocument > xStorageDoc( _rxDocument, UNO_QUERY_THROW );
404cdf0e10cSrcweir                 Reference< XStorage > xDocStorage( xStorageDoc->getDocumentStorage(), UNO_QUERY_THROW );
405cdf0e10cSrcweir                 bSuccess = lcl_commitStorage_nothrow( xDocStorage );
406cdf0e10cSrcweir             }
407cdf0e10cSrcweir             catch( const Exception& )
408cdf0e10cSrcweir             {
409cdf0e10cSrcweir                 aException = ::cppu::getCaughtException();
410cdf0e10cSrcweir             }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir             // log the failure, if any
413cdf0e10cSrcweir             if ( !bSuccess )
414cdf0e10cSrcweir             {
415cdf0e10cSrcweir                 _rLogger.logFailure( MigrationError(
416cdf0e10cSrcweir                     ERR_STORAGE_COMMIT_FAILED,
417cdf0e10cSrcweir                     ::comphelper::DocumentInfo::getDocumentTitle( _rxDocument ),
418cdf0e10cSrcweir                     aException
419cdf0e10cSrcweir                 ) );
420cdf0e10cSrcweir             }
421cdf0e10cSrcweir         	return bSuccess;
422cdf0e10cSrcweir         }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_storeDocument_nothrow(const Reference<XModel> & _rxDocument,MigrationLog & _rLogger)425cdf0e10cSrcweir         bool lcl_storeDocument_nothrow( const Reference< XModel >& _rxDocument, MigrationLog& _rLogger )
426cdf0e10cSrcweir         {
427cdf0e10cSrcweir             bool bSuccess = false;
428cdf0e10cSrcweir             Any aException;
429cdf0e10cSrcweir             try
430cdf0e10cSrcweir             {
431cdf0e10cSrcweir                 Reference< XStorable > xStorable( _rxDocument, UNO_QUERY_THROW );
432cdf0e10cSrcweir                 xStorable->store();
433cdf0e10cSrcweir                 bSuccess = true;
434cdf0e10cSrcweir             }
435cdf0e10cSrcweir             catch( const Exception& )
436cdf0e10cSrcweir             {
437cdf0e10cSrcweir                 aException = ::cppu::getCaughtException();
438cdf0e10cSrcweir             }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir             // log the failure, if any
441cdf0e10cSrcweir             if ( !bSuccess )
442cdf0e10cSrcweir             {
443cdf0e10cSrcweir                 _rLogger.logFailure( MigrationError(
444cdf0e10cSrcweir                     ERR_STORING_DATABASEDOC_FAILED,
445cdf0e10cSrcweir                     aException
446cdf0e10cSrcweir                 ) );
447cdf0e10cSrcweir             }
448cdf0e10cSrcweir         	return bSuccess;
449cdf0e10cSrcweir         }
450cdf0e10cSrcweir 
451cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_storeEmbeddedDocument_nothrow(const SubDocument & _rDocument)452cdf0e10cSrcweir         bool lcl_storeEmbeddedDocument_nothrow( const SubDocument& _rDocument )
453cdf0e10cSrcweir         {
454cdf0e10cSrcweir             try
455cdf0e10cSrcweir             {
456cdf0e10cSrcweir                 lcl_executeCommand_throw( _rDocument.xCommandProcessor, "store" );
457cdf0e10cSrcweir             }
458cdf0e10cSrcweir             catch( const Exception& )
459cdf0e10cSrcweir             {
460cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
461cdf0e10cSrcweir                 return false;
462cdf0e10cSrcweir             }
463cdf0e10cSrcweir             return true;
464cdf0e10cSrcweir         }
465cdf0e10cSrcweir     }
466cdf0e10cSrcweir 
467cdf0e10cSrcweir     //====================================================================
468cdf0e10cSrcweir 	//= DrawPageIterator
469cdf0e10cSrcweir 	//====================================================================
470cdf0e10cSrcweir     class DrawPageIterator
471cdf0e10cSrcweir     {
472cdf0e10cSrcweir     public:
DrawPageIterator(const Reference<XModel> & _rxDocument)473cdf0e10cSrcweir         DrawPageIterator( const Reference< XModel >& _rxDocument )
474cdf0e10cSrcweir             :m_xDocument( _rxDocument )
475cdf0e10cSrcweir             ,m_nPageCount( 0 )
476cdf0e10cSrcweir             ,m_nCurrentPage( 0 )
477cdf0e10cSrcweir         {
478cdf0e10cSrcweir             Reference< XDrawPageSupplier > xSingle( _rxDocument, UNO_QUERY );
479cdf0e10cSrcweir             Reference< XDrawPagesSupplier > xMulti( _rxDocument, UNO_QUERY );
480cdf0e10cSrcweir             if ( xSingle.is() )
481cdf0e10cSrcweir             {
482cdf0e10cSrcweir                 m_xSinglePage.set( xSingle->getDrawPage(), UNO_SET_THROW );
483cdf0e10cSrcweir                 m_nPageCount = 1;
484cdf0e10cSrcweir             }
485cdf0e10cSrcweir             else if ( xMulti.is() )
486cdf0e10cSrcweir             {
487cdf0e10cSrcweir                 m_xMultiPages.set( xMulti->getDrawPages(), UNO_SET_THROW );
488cdf0e10cSrcweir                 m_nPageCount = m_xMultiPages->getCount();
489cdf0e10cSrcweir             }
490cdf0e10cSrcweir         }
491cdf0e10cSrcweir 
hasMore() const492cdf0e10cSrcweir         bool hasMore() const
493cdf0e10cSrcweir         {
494cdf0e10cSrcweir             return m_nCurrentPage < m_nPageCount;
495cdf0e10cSrcweir         }
496cdf0e10cSrcweir 
next()497cdf0e10cSrcweir         Reference< XDrawPage > next()
498cdf0e10cSrcweir         {
499cdf0e10cSrcweir             Reference< XDrawPage > xNextPage;
500cdf0e10cSrcweir 
501cdf0e10cSrcweir             if ( m_xSinglePage.is() )
502cdf0e10cSrcweir             {
503cdf0e10cSrcweir                 xNextPage = m_xSinglePage;
504cdf0e10cSrcweir             }
505cdf0e10cSrcweir             else if ( m_xMultiPages.is() )
506cdf0e10cSrcweir             {
507cdf0e10cSrcweir                 xNextPage.set( m_xMultiPages->getByIndex( m_nCurrentPage ), UNO_QUERY_THROW );
508cdf0e10cSrcweir             }
509cdf0e10cSrcweir             ++m_nCurrentPage;
510cdf0e10cSrcweir             return xNextPage;
511cdf0e10cSrcweir         }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir     private:
514cdf0e10cSrcweir         const Reference< XModel >   m_xDocument;
515cdf0e10cSrcweir         Reference< XDrawPage >      m_xSinglePage;
516cdf0e10cSrcweir         Reference< XDrawPages >     m_xMultiPages;
517cdf0e10cSrcweir         sal_Int32                   m_nPageCount;
518cdf0e10cSrcweir         sal_Int32                   m_nCurrentPage;
519cdf0e10cSrcweir     };
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     //====================================================================
522cdf0e10cSrcweir 	//= FormComponentScripts
523cdf0e10cSrcweir 	//====================================================================
524cdf0e10cSrcweir     class FormComponentScripts
525cdf0e10cSrcweir     {
526cdf0e10cSrcweir     public:
FormComponentScripts(const Reference<XInterface> & _rxComponent,const Reference<XEventAttacherManager> & _rxManager,const sal_Int32 _nIndex)527cdf0e10cSrcweir         FormComponentScripts(
528cdf0e10cSrcweir                 const Reference< XInterface >& _rxComponent,
529cdf0e10cSrcweir                 const Reference< XEventAttacherManager >& _rxManager,
530cdf0e10cSrcweir                 const sal_Int32 _nIndex
531cdf0e10cSrcweir             )
532cdf0e10cSrcweir             :m_xComponent( _rxComponent, UNO_SET_THROW )
533cdf0e10cSrcweir             ,m_xManager( _rxManager, UNO_SET_THROW )
534cdf0e10cSrcweir             ,m_nIndex( _nIndex )
535cdf0e10cSrcweir         {
536cdf0e10cSrcweir         }
537cdf0e10cSrcweir 
getEvents() const538cdf0e10cSrcweir         Sequence< ScriptEventDescriptor > getEvents() const
539cdf0e10cSrcweir         {
540cdf0e10cSrcweir             return m_xManager->getScriptEvents( m_nIndex );
541cdf0e10cSrcweir         }
542cdf0e10cSrcweir 
setEvents(const Sequence<ScriptEventDescriptor> & _rEvents) const543cdf0e10cSrcweir         void setEvents( const Sequence< ScriptEventDescriptor >& _rEvents  ) const
544cdf0e10cSrcweir         {
545cdf0e10cSrcweir             m_xManager->registerScriptEvents( m_nIndex, _rEvents );
546cdf0e10cSrcweir         }
547cdf0e10cSrcweir 
getComponent() const548cdf0e10cSrcweir         const Reference< XInterface >& getComponent() const
549cdf0e10cSrcweir         {
550cdf0e10cSrcweir             return m_xComponent;
551cdf0e10cSrcweir         }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir     private:
554cdf0e10cSrcweir         const Reference< XInterface >               m_xComponent;
555cdf0e10cSrcweir         const Reference< XEventAttacherManager >    m_xManager;
556cdf0e10cSrcweir         const sal_Int32                             m_nIndex;
557cdf0e10cSrcweir     };
558cdf0e10cSrcweir 
559cdf0e10cSrcweir     //====================================================================
560cdf0e10cSrcweir 	//= FormComponentIterator
561cdf0e10cSrcweir 	//====================================================================
562cdf0e10cSrcweir     class FormComponentIterator
563cdf0e10cSrcweir     {
564cdf0e10cSrcweir     public:
FormComponentIterator(const Reference<XIndexAccess> & _rxContainer)565cdf0e10cSrcweir         FormComponentIterator( const Reference< XIndexAccess >& _rxContainer )
566cdf0e10cSrcweir             :m_xContainer( _rxContainer, UNO_SET_THROW )
567cdf0e10cSrcweir             ,m_xEventManager( _rxContainer, UNO_QUERY_THROW )
568cdf0e10cSrcweir             ,m_nElementCount( _rxContainer->getCount() )
569cdf0e10cSrcweir             ,m_nCurrentElement( 0 )
570cdf0e10cSrcweir         {
571cdf0e10cSrcweir         }
572cdf0e10cSrcweir 
hasMore() const573cdf0e10cSrcweir         bool hasMore() const
574cdf0e10cSrcweir         {
575cdf0e10cSrcweir             return m_nCurrentElement < m_nElementCount;
576cdf0e10cSrcweir         }
577cdf0e10cSrcweir 
next()578cdf0e10cSrcweir         FormComponentScripts next()
579cdf0e10cSrcweir         {
580cdf0e10cSrcweir             FormComponentScripts aComponent(
581cdf0e10cSrcweir                 Reference< XInterface >( m_xContainer->getByIndex( m_nCurrentElement ), UNO_QUERY_THROW ),
582cdf0e10cSrcweir                 m_xEventManager,
583cdf0e10cSrcweir                 m_nCurrentElement
584cdf0e10cSrcweir             );
585cdf0e10cSrcweir             ++m_nCurrentElement;
586cdf0e10cSrcweir             return aComponent;
587cdf0e10cSrcweir         }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir     private:
590cdf0e10cSrcweir         const Reference< XIndexAccess >             m_xContainer;
591cdf0e10cSrcweir         const Reference< XEventAttacherManager >    m_xEventManager;
592cdf0e10cSrcweir         const sal_Int32                             m_nElementCount;
593cdf0e10cSrcweir         sal_Int32                                   m_nCurrentElement;
594cdf0e10cSrcweir 
595cdf0e10cSrcweir     };
596cdf0e10cSrcweir 
597cdf0e10cSrcweir     //====================================================================
598cdf0e10cSrcweir 	//= ScriptsStorage - declaration
599cdf0e10cSrcweir 	//====================================================================
600cdf0e10cSrcweir     /** a helper class which encapsulates access to the storages for Java/Script, BeanShell, and Python scripts,
601cdf0e10cSrcweir         i.e. all script types which can be manipulated on storage level.
602cdf0e10cSrcweir     */
603cdf0e10cSrcweir     class ScriptsStorage
604cdf0e10cSrcweir     {
605cdf0e10cSrcweir     public:
606cdf0e10cSrcweir         ScriptsStorage( MigrationLog& _rLogger );
607cdf0e10cSrcweir         ScriptsStorage( const Reference< XModel >& _rxDocument, MigrationLog& _rLogger );
608cdf0e10cSrcweir         ~ScriptsStorage();
609cdf0e10cSrcweir 
610cdf0e10cSrcweir         /** determines whether the instance is valid, i.e. refers to a valid root storage
611cdf0e10cSrcweir             for reading/storing scripts
612cdf0e10cSrcweir         */
isValid() const613cdf0e10cSrcweir         inline bool isValid() const { return m_xScriptsStorage.is(); }
614cdf0e10cSrcweir 
615cdf0e10cSrcweir         /** binds the instance to a new document. Only to be called when the instance is not yet
616cdf0e10cSrcweir             bound (i.e. isValid returns <FALSE/>).
617cdf0e10cSrcweir         */
618cdf0e10cSrcweir         void    bind( const Reference< XModel >& _rxDocument );
619cdf0e10cSrcweir 
620cdf0e10cSrcweir         /// determines whether scripts of the given type are present
621cdf0e10cSrcweir         bool    hasScripts( const ScriptType _eType ) const;
622cdf0e10cSrcweir 
623cdf0e10cSrcweir         /// returns the root storage for the scripts of the given type
624cdf0e10cSrcweir         SharedStorage
625cdf0e10cSrcweir                 getScriptsRoot( const ScriptType _eType ) const;
626cdf0e10cSrcweir 
627cdf0e10cSrcweir         /** returns the names of the elements in the "Scripts" storage
628cdf0e10cSrcweir         */
629cdf0e10cSrcweir         ::std::set< ::rtl::OUString >
630cdf0e10cSrcweir                 getElementNames() const;
631cdf0e10cSrcweir 
632cdf0e10cSrcweir         /** removes the sub storage for a given script type
633cdf0e10cSrcweir             @precond
634cdf0e10cSrcweir                 the respective storage is empty
635cdf0e10cSrcweir             @precond
636cdf0e10cSrcweir                 the ScriptsStorage instance was opened for writing
637cdf0e10cSrcweir         */
638cdf0e10cSrcweir         void    removeScriptTypeStorage( const ScriptType _eType ) const;
639cdf0e10cSrcweir 
640cdf0e10cSrcweir         /** commits the changes at our XStorage object
641cdf0e10cSrcweir         */
642cdf0e10cSrcweir         bool    commit();
643cdf0e10cSrcweir 
644cdf0e10cSrcweir         /** removes the "Scripts" sub storage from the given document's root storage
645cdf0e10cSrcweir             @precond
646cdf0e10cSrcweir                 the "Scripts" storage is empty
647cdf0e10cSrcweir         */
648cdf0e10cSrcweir         static bool
649cdf0e10cSrcweir                 removeFromDocument( const Reference< XModel >& _rxDocument, MigrationLog& _rLogger );
650cdf0e10cSrcweir 
651cdf0e10cSrcweir     private:
652cdf0e10cSrcweir         MigrationLog&   m_rLogger;
653cdf0e10cSrcweir         SharedStorage   m_xScriptsStorage;
654cdf0e10cSrcweir     };
655cdf0e10cSrcweir 
656cdf0e10cSrcweir     //====================================================================
657cdf0e10cSrcweir 	//= ScriptsStorage - implementation
658cdf0e10cSrcweir 	//====================================================================
659cdf0e10cSrcweir 	//--------------------------------------------------------------------
ScriptsStorage(MigrationLog & _rLogger)660cdf0e10cSrcweir     ScriptsStorage::ScriptsStorage( MigrationLog& _rLogger )
661cdf0e10cSrcweir         :m_rLogger( _rLogger )
662cdf0e10cSrcweir         ,m_xScriptsStorage()
663cdf0e10cSrcweir     {
664cdf0e10cSrcweir     }
665cdf0e10cSrcweir 
666cdf0e10cSrcweir 	//--------------------------------------------------------------------
ScriptsStorage(const Reference<XModel> & _rxDocument,MigrationLog & _rLogger)667cdf0e10cSrcweir     ScriptsStorage::ScriptsStorage( const Reference< XModel >& _rxDocument, MigrationLog& _rLogger )
668cdf0e10cSrcweir         :m_rLogger( _rLogger )
669cdf0e10cSrcweir         ,m_xScriptsStorage()
670cdf0e10cSrcweir     {
671cdf0e10cSrcweir         bind( _rxDocument );
672cdf0e10cSrcweir     }
673cdf0e10cSrcweir 
674cdf0e10cSrcweir 	//--------------------------------------------------------------------
~ScriptsStorage()675cdf0e10cSrcweir     ScriptsStorage::~ScriptsStorage()
676cdf0e10cSrcweir     {
677cdf0e10cSrcweir     }
678cdf0e10cSrcweir 
679cdf0e10cSrcweir     //--------------------------------------------------------------------
commit()680cdf0e10cSrcweir     bool ScriptsStorage::commit()
681cdf0e10cSrcweir     {
682cdf0e10cSrcweir         return lcl_commitStorage_nothrow( m_xScriptsStorage );
683cdf0e10cSrcweir     }
684cdf0e10cSrcweir 
685cdf0e10cSrcweir 	//--------------------------------------------------------------------
bind(const Reference<XModel> & _rxDocument)686cdf0e10cSrcweir     void ScriptsStorage::bind( const Reference< XModel >& _rxDocument )
687cdf0e10cSrcweir     {
688cdf0e10cSrcweir         OSL_PRECOND( !isValid(), "ScriptsStorage:bind: did not bother, yet, to check whether this is allowed!" );
689cdf0e10cSrcweir         try
690cdf0e10cSrcweir         {
691cdf0e10cSrcweir             Reference< XStorageBasedDocument > xStorageDoc( _rxDocument, UNO_QUERY_THROW );
692cdf0e10cSrcweir             Reference< XStorage > xDocStorage( xStorageDoc->getDocumentStorage(), UNO_QUERY_THROW );
693cdf0e10cSrcweir 
694cdf0e10cSrcweir             // the the "Scripts" storage exist, or if it does not (yet) exist and we are in write mode
695cdf0e10cSrcweir             // => open the storage
696cdf0e10cSrcweir             if  (   (   xDocStorage->hasByName( lcl_getScriptsStorageName() )
697cdf0e10cSrcweir                     &&  xDocStorage->isStorageElement( lcl_getScriptsStorageName() )
698cdf0e10cSrcweir                     )
699cdf0e10cSrcweir                 ||  !xDocStorage->hasByName( lcl_getScriptsStorageName() )
700cdf0e10cSrcweir                 )
701cdf0e10cSrcweir             {
702cdf0e10cSrcweir                 m_xScriptsStorage.set(
703cdf0e10cSrcweir                     xDocStorage->openStorageElement(
704cdf0e10cSrcweir                         lcl_getScriptsStorageName(), ElementModes::READWRITE
705cdf0e10cSrcweir                     ),
706cdf0e10cSrcweir                     UNO_QUERY_THROW
707cdf0e10cSrcweir                 );
708cdf0e10cSrcweir             }
709cdf0e10cSrcweir         }
710cdf0e10cSrcweir         catch( const Exception& )
711cdf0e10cSrcweir         {
712cdf0e10cSrcweir             m_rLogger.logFailure( MigrationError(
713cdf0e10cSrcweir                 ERR_BIND_SCRIPT_STORAGE_FAILED,
714cdf0e10cSrcweir                 ::comphelper::DocumentInfo::getDocumentTitle( _rxDocument ),
715cdf0e10cSrcweir                 ::cppu::getCaughtException()
716cdf0e10cSrcweir             ) );
717cdf0e10cSrcweir         }
718cdf0e10cSrcweir     }
719cdf0e10cSrcweir 
720cdf0e10cSrcweir     //--------------------------------------------------------------------
hasScripts(const ScriptType _eType) const721cdf0e10cSrcweir     bool ScriptsStorage::hasScripts( const ScriptType _eType ) const
722cdf0e10cSrcweir     {
723cdf0e10cSrcweir         OSL_PRECOND( isValid(), "ScriptsStorage::hasScripts: illegal call!" );
724cdf0e10cSrcweir         if ( !isValid() )
725cdf0e10cSrcweir             return false;
726cdf0e10cSrcweir 
727cdf0e10cSrcweir         const ::rtl::OUString& rSubStorageName( lcl_getScriptsSubStorageName( _eType ) );
728cdf0e10cSrcweir         return  m_xScriptsStorage->hasByName( rSubStorageName )
729cdf0e10cSrcweir             &&  m_xScriptsStorage->isStorageElement( rSubStorageName );
730cdf0e10cSrcweir     }
731cdf0e10cSrcweir 
732cdf0e10cSrcweir     //--------------------------------------------------------------------
getScriptsRoot(const ScriptType _eType) const733cdf0e10cSrcweir     SharedStorage ScriptsStorage::getScriptsRoot( const ScriptType _eType ) const
734cdf0e10cSrcweir     {
735cdf0e10cSrcweir         SharedStorage xStorage;
736cdf0e10cSrcweir         if ( isValid() )
737cdf0e10cSrcweir         {
738cdf0e10cSrcweir             xStorage.reset( m_xScriptsStorage->openStorageElement(
739cdf0e10cSrcweir                 lcl_getScriptsSubStorageName( _eType ), ElementModes::READWRITE
740cdf0e10cSrcweir             ) );
741cdf0e10cSrcweir         }
742cdf0e10cSrcweir         return xStorage;
743cdf0e10cSrcweir     }
744cdf0e10cSrcweir 
745cdf0e10cSrcweir     //--------------------------------------------------------------------
getElementNames() const746cdf0e10cSrcweir     ::std::set< ::rtl::OUString > ScriptsStorage::getElementNames() const
747cdf0e10cSrcweir     {
748cdf0e10cSrcweir         Sequence< ::rtl::OUString > aElementNames;
749cdf0e10cSrcweir         if ( isValid() )
750cdf0e10cSrcweir             aElementNames = m_xScriptsStorage->getElementNames();
751cdf0e10cSrcweir 
752cdf0e10cSrcweir         ::std::set< ::rtl::OUString > aNames;
753cdf0e10cSrcweir         ::std::copy(
754cdf0e10cSrcweir             aElementNames.getConstArray(),
755cdf0e10cSrcweir             aElementNames.getConstArray() + aElementNames.getLength(),
756cdf0e10cSrcweir             ::std::insert_iterator< ::std::set< ::rtl::OUString > >( aNames, aNames.end() )
757cdf0e10cSrcweir         );
758cdf0e10cSrcweir         return aNames;
759cdf0e10cSrcweir     }
760cdf0e10cSrcweir 
761cdf0e10cSrcweir     //--------------------------------------------------------------------
removeScriptTypeStorage(const ScriptType _eType) const762cdf0e10cSrcweir     void ScriptsStorage::removeScriptTypeStorage( const ScriptType _eType ) const
763cdf0e10cSrcweir     {
764cdf0e10cSrcweir         ::rtl::OUString sSubStorageName( lcl_getScriptsSubStorageName( _eType ) );
765cdf0e10cSrcweir         if ( m_xScriptsStorage->hasByName( sSubStorageName ) )
766cdf0e10cSrcweir             m_xScriptsStorage->removeElement( sSubStorageName );
767cdf0e10cSrcweir     }
768cdf0e10cSrcweir 
769cdf0e10cSrcweir     //--------------------------------------------------------------------
removeFromDocument(const Reference<XModel> & _rxDocument,MigrationLog & _rLogger)770cdf0e10cSrcweir     bool ScriptsStorage::removeFromDocument( const Reference< XModel >& _rxDocument, MigrationLog& _rLogger )
771cdf0e10cSrcweir     {
772cdf0e10cSrcweir         try
773cdf0e10cSrcweir         {
774cdf0e10cSrcweir             Reference< XStorageBasedDocument > xStorageDoc( _rxDocument, UNO_QUERY_THROW );
775cdf0e10cSrcweir             Reference< XStorage > xDocStorage( xStorageDoc->getDocumentStorage(), UNO_QUERY_THROW );
776cdf0e10cSrcweir             xDocStorage->removeElement( lcl_getScriptsStorageName() );
777cdf0e10cSrcweir         }
778cdf0e10cSrcweir         catch( const Exception& )
779cdf0e10cSrcweir         {
780cdf0e10cSrcweir             _rLogger.logFailure( MigrationError(
781cdf0e10cSrcweir                 ERR_REMOVE_SCRIPTS_STORAGE_FAILED,
782cdf0e10cSrcweir                 ::comphelper::DocumentInfo::getDocumentTitle( _rxDocument ),
783cdf0e10cSrcweir                 ::cppu::getCaughtException()
784cdf0e10cSrcweir             ) ) ;
785cdf0e10cSrcweir             return false;
786cdf0e10cSrcweir         }
787cdf0e10cSrcweir         return true;
788cdf0e10cSrcweir     }
789cdf0e10cSrcweir 
790cdf0e10cSrcweir     //====================================================================
791cdf0e10cSrcweir     //= ProgressDelegator
792cdf0e10cSrcweir     //====================================================================
793cdf0e10cSrcweir     class ProgressDelegator : public IProgressConsumer
794cdf0e10cSrcweir     {
795cdf0e10cSrcweir     public:
ProgressDelegator(IMigrationProgress & _rDelegator,const::rtl::OUString & _rObjectName,const::rtl::OUString & _rAction)796cdf0e10cSrcweir         ProgressDelegator(  IMigrationProgress& _rDelegator,
797cdf0e10cSrcweir                             const ::rtl::OUString& _rObjectName,
798cdf0e10cSrcweir                             const ::rtl::OUString& _rAction
799cdf0e10cSrcweir                           )
800cdf0e10cSrcweir             :m_rDelegator( _rDelegator )
801cdf0e10cSrcweir             ,m_sObjectName( _rObjectName )
802cdf0e10cSrcweir             ,m_sAction( _rAction )
803cdf0e10cSrcweir         {
804cdf0e10cSrcweir         }
~ProgressDelegator()805cdf0e10cSrcweir         virtual ~ProgressDelegator()
806cdf0e10cSrcweir         {
807cdf0e10cSrcweir         }
808cdf0e10cSrcweir 
809cdf0e10cSrcweir         // IProgressConsumer
start(sal_uInt32 _nRange)810cdf0e10cSrcweir         virtual void    start( sal_uInt32 _nRange )
811cdf0e10cSrcweir         {
812cdf0e10cSrcweir             m_rDelegator.startObject( m_sObjectName, m_sAction, _nRange );
813cdf0e10cSrcweir         }
advance(sal_uInt32 _nValue)814cdf0e10cSrcweir         virtual void    advance( sal_uInt32 _nValue )
815cdf0e10cSrcweir         {
816cdf0e10cSrcweir             m_rDelegator.setObjectProgressValue( _nValue );
817cdf0e10cSrcweir         }
end()818cdf0e10cSrcweir         virtual void    end()
819cdf0e10cSrcweir         {
820cdf0e10cSrcweir             m_rDelegator.endObject();
821cdf0e10cSrcweir         }
822cdf0e10cSrcweir 
823cdf0e10cSrcweir     private:
824cdf0e10cSrcweir         IMigrationProgress& m_rDelegator;
825cdf0e10cSrcweir         ::rtl::OUString     m_sObjectName;
826cdf0e10cSrcweir         ::rtl::OUString     m_sAction;
827cdf0e10cSrcweir     };
828cdf0e10cSrcweir 
829cdf0e10cSrcweir 	//====================================================================
830cdf0e10cSrcweir 	//= PhaseGuard
831cdf0e10cSrcweir 	//====================================================================
832cdf0e10cSrcweir     class PhaseGuard
833cdf0e10cSrcweir     {
834cdf0e10cSrcweir     public:
PhaseGuard(ProgressMixer & _rMixer)835cdf0e10cSrcweir         PhaseGuard( ProgressMixer& _rMixer )
836cdf0e10cSrcweir             :m_rMixer( _rMixer )
837cdf0e10cSrcweir         {
838cdf0e10cSrcweir         }
839cdf0e10cSrcweir 
PhaseGuard(ProgressMixer & _rMixer,const PhaseID _nID,const sal_uInt32 _nPhaseRange)840cdf0e10cSrcweir         PhaseGuard( ProgressMixer& _rMixer, const PhaseID _nID, const sal_uInt32 _nPhaseRange )
841cdf0e10cSrcweir             :m_rMixer( _rMixer )
842cdf0e10cSrcweir         {
843cdf0e10cSrcweir             start( _nID, _nPhaseRange );
844cdf0e10cSrcweir         }
845cdf0e10cSrcweir 
~PhaseGuard()846cdf0e10cSrcweir         ~PhaseGuard()
847cdf0e10cSrcweir         {
848cdf0e10cSrcweir             m_rMixer.endPhase();
849cdf0e10cSrcweir         }
850cdf0e10cSrcweir 
start(const PhaseID _nID,const sal_uInt32 _nPhaseRange)851cdf0e10cSrcweir         void start( const PhaseID _nID, const sal_uInt32 _nPhaseRange )
852cdf0e10cSrcweir         {
853cdf0e10cSrcweir             m_rMixer.startPhase( _nID, _nPhaseRange );
854cdf0e10cSrcweir         }
855cdf0e10cSrcweir 
856cdf0e10cSrcweir     private:
857cdf0e10cSrcweir         ProgressMixer&  m_rMixer;
858cdf0e10cSrcweir     };
859cdf0e10cSrcweir 
860cdf0e10cSrcweir 	//====================================================================
861cdf0e10cSrcweir 	//= MigrationEngine_Impl - declaration
862cdf0e10cSrcweir 	//====================================================================
863cdf0e10cSrcweir     class MigrationEngine_Impl
864cdf0e10cSrcweir     {
865cdf0e10cSrcweir     public:
866cdf0e10cSrcweir         MigrationEngine_Impl(
867cdf0e10cSrcweir             const ::comphelper::ComponentContext& _rContext,
868cdf0e10cSrcweir             const Reference< XOfficeDatabaseDocument >& _rxDocument,
869cdf0e10cSrcweir             IMigrationProgress& _rProgress,
870cdf0e10cSrcweir             MigrationLog& _rLogger
871cdf0e10cSrcweir         );
872cdf0e10cSrcweir         ~MigrationEngine_Impl();
873cdf0e10cSrcweir 
getFormCount() const874cdf0e10cSrcweir         inline  size_t      getFormCount() const    { return m_nFormCount; }
getReportCount() const875cdf0e10cSrcweir         inline  size_t      getReportCount()const   { return m_nReportCount; }
876cdf0e10cSrcweir         bool    migrateAll();
877cdf0e10cSrcweir 
878cdf0e10cSrcweir     private:
879cdf0e10cSrcweir         ::comphelper::ComponentContext              m_aContext;
880cdf0e10cSrcweir         const Reference< XOfficeDatabaseDocument >  m_xDocument;
881cdf0e10cSrcweir         const Reference< XModel >                   m_xDocumentModel;
882cdf0e10cSrcweir         IMigrationProgress&                         m_rProgress;
883cdf0e10cSrcweir         MigrationLog&                               m_rLogger;
884cdf0e10cSrcweir         mutable DocumentID                          m_nCurrentDocumentID;
885cdf0e10cSrcweir         SubDocuments                                m_aSubDocs;
886cdf0e10cSrcweir         size_t                                      m_nFormCount;
887cdf0e10cSrcweir         size_t                                      m_nReportCount;
888cdf0e10cSrcweir 
889cdf0e10cSrcweir     private:
890cdf0e10cSrcweir         /** collects a description of all sub documents of our database document
891cdf0e10cSrcweir 
892cdf0e10cSrcweir             @return
893cdf0e10cSrcweir                 <TRUE/> if and only if collecting the documents was successful
894cdf0e10cSrcweir         */
895cdf0e10cSrcweir         bool    impl_collectSubDocuments_nothrow();
896cdf0e10cSrcweir 
897cdf0e10cSrcweir         /** migrates the macros/scripts of the given sub document
898cdf0e10cSrcweir         */
899cdf0e10cSrcweir         bool    impl_handleDocument_nothrow( const SubDocument& _rDocument ) const;
900cdf0e10cSrcweir 
901cdf0e10cSrcweir         /** checks the structure of the 'Scripts' folder of a sub document
902cdf0e10cSrcweir             for unknown elements
903cdf0e10cSrcweir 
904cdf0e10cSrcweir             @return
905cdf0e10cSrcweir                 <TRUE/> if and only if the 'Scripts' folder contains known elements only.
906cdf0e10cSrcweir         */
907cdf0e10cSrcweir         bool    impl_checkScriptStorageStructure_nothrow( const SubDocument& _rDocument ) const;
908cdf0e10cSrcweir 
909cdf0e10cSrcweir         /** migrates the scripts of the given "storage-based" script type
910cdf0e10cSrcweir         */
911cdf0e10cSrcweir         bool    impl_migrateScriptStorage_nothrow(
912cdf0e10cSrcweir                     const SubDocument& _rDocument,
913cdf0e10cSrcweir                     const ScriptType _eScriptType,
914cdf0e10cSrcweir                     ProgressMixer& _rProgress,
915cdf0e10cSrcweir                     const PhaseID _nPhaseID
916cdf0e10cSrcweir                 ) const;
917cdf0e10cSrcweir 
918cdf0e10cSrcweir         /** migrates the content of the given "container based" libraries (Basic/Dialogs)
919cdf0e10cSrcweir         */
920cdf0e10cSrcweir         bool    impl_migrateContainerLibraries_nothrow(
921cdf0e10cSrcweir                     const SubDocument& _rDocument,
922cdf0e10cSrcweir                     const ScriptType _eScriptType,
923cdf0e10cSrcweir                     ProgressMixer& _rProgress,
924cdf0e10cSrcweir                     const PhaseID _nPhaseID
925cdf0e10cSrcweir                 ) const;
926cdf0e10cSrcweir 
927cdf0e10cSrcweir         /** adjusts the events for the given dialog/element, taking into account the new names
928cdf0e10cSrcweir             of the moved libraries
929cdf0e10cSrcweir         */
930cdf0e10cSrcweir         void    impl_adjustDialogElementEvents_throw(
931cdf0e10cSrcweir                     const Reference< XInterface >& _rxElement
932cdf0e10cSrcweir                 ) const;
933cdf0e10cSrcweir 
934cdf0e10cSrcweir         /** adjusts the events in the given dialog, and its controls, taking into account the new names
935cdf0e10cSrcweir             of the moved libraries
936cdf0e10cSrcweir         */
937cdf0e10cSrcweir         bool    impl_adjustDialogEvents_nothrow(
938cdf0e10cSrcweir                     Any& _inout_rDialogLibraryElement,
939cdf0e10cSrcweir                     const ::rtl::OUString& _rDocName,
940cdf0e10cSrcweir                     const ::rtl::OUString& _rDialogLibName,
941cdf0e10cSrcweir                     const ::rtl::OUString& _rDialogName
942cdf0e10cSrcweir                 ) const;
943cdf0e10cSrcweir 
944cdf0e10cSrcweir         /** adjust the document-events which refer to macros/scripts in the document, taking into
945cdf0e10cSrcweir             account the new names of the moved libraries
946cdf0e10cSrcweir         */
947cdf0e10cSrcweir         bool    impl_adjustDocumentEvents_nothrow(
948cdf0e10cSrcweir                     const SubDocument& _rDocument
949cdf0e10cSrcweir                 ) const;
950cdf0e10cSrcweir 
951cdf0e10cSrcweir         /** adjusts the script references bound to form component events
952cdf0e10cSrcweir         */
953cdf0e10cSrcweir         bool    impl_adjustFormComponentEvents_nothrow(
954cdf0e10cSrcweir                     const SubDocument& _rDocument
955cdf0e10cSrcweir                 ) const;
956cdf0e10cSrcweir 
957cdf0e10cSrcweir         /** adjusts the script references for the elements of the given form component container
958cdf0e10cSrcweir         */
959cdf0e10cSrcweir         void    impl_adjustFormComponentEvents_throw(
960cdf0e10cSrcweir                     const Reference< XIndexAccess >& _rxComponentContainer
961cdf0e10cSrcweir                 ) const;
962cdf0e10cSrcweir 
963cdf0e10cSrcweir         /** adjusts the library name in the given script URL, so that it reflects
964cdf0e10cSrcweir             the new name of the library
965cdf0e10cSrcweir 
966cdf0e10cSrcweir             @return <TRUE/>
967cdf0e10cSrcweir                 if and only if adjustments to the script code have been made
968cdf0e10cSrcweir         */
969cdf0e10cSrcweir         bool    impl_adjustScriptLibrary_nothrow(
970cdf0e10cSrcweir                     const ::rtl::OUString& _rScriptType,
971cdf0e10cSrcweir                     ::rtl::OUString& _inout_rScriptCode
972cdf0e10cSrcweir                 ) const;
973cdf0e10cSrcweir 
974cdf0e10cSrcweir         bool    impl_adjustScriptLibrary_nothrow( Any& _inout_rScriptDescriptor ) const;
975cdf0e10cSrcweir         bool    impl_adjustScriptLibrary_nothrow( ScriptEventDescriptor& _inout_rScriptEvent ) const;
976cdf0e10cSrcweir 
977cdf0e10cSrcweir         /** asks the user for a password for the given library, and unprotects the library
978cdf0e10cSrcweir 
979cdf0e10cSrcweir             @return <TRUE/>
980cdf0e10cSrcweir                 if and only if the library could be successfully unprotected
981cdf0e10cSrcweir         */
982cdf0e10cSrcweir         bool    impl_unprotectPasswordLibrary_throw(
983cdf0e10cSrcweir                     const Reference< XLibraryContainerPassword >& _rxPasswordManager,
984cdf0e10cSrcweir                     const ScriptType _eScriptType,
985cdf0e10cSrcweir                     const ::rtl::OUString& _rLibraryName
986cdf0e10cSrcweir                 ) const;
987cdf0e10cSrcweir     };
988cdf0e10cSrcweir 
989cdf0e10cSrcweir 	//====================================================================
990cdf0e10cSrcweir 	//= MigrationEngine_Impl - implementation
991cdf0e10cSrcweir 	//====================================================================
992cdf0e10cSrcweir 	//--------------------------------------------------------------------
MigrationEngine_Impl(const::comphelper::ComponentContext & _rContext,const Reference<XOfficeDatabaseDocument> & _rxDocument,IMigrationProgress & _rProgress,MigrationLog & _rLogger)993cdf0e10cSrcweir     MigrationEngine_Impl::MigrationEngine_Impl( const ::comphelper::ComponentContext& _rContext,
994cdf0e10cSrcweir             const Reference< XOfficeDatabaseDocument >& _rxDocument, IMigrationProgress& _rProgress, MigrationLog& _rLogger )
995cdf0e10cSrcweir         :m_aContext( _rContext )
996cdf0e10cSrcweir         ,m_xDocument( _rxDocument )
997cdf0e10cSrcweir         ,m_xDocumentModel( _rxDocument, UNO_QUERY_THROW )
998cdf0e10cSrcweir         ,m_rProgress( _rProgress )
999cdf0e10cSrcweir         ,m_rLogger( _rLogger )
1000cdf0e10cSrcweir         ,m_nCurrentDocumentID( - 1 )
1001cdf0e10cSrcweir         ,m_aSubDocs()
1002cdf0e10cSrcweir         ,m_nFormCount( 0 )
1003cdf0e10cSrcweir         ,m_nReportCount( 0 )
1004cdf0e10cSrcweir     {
1005cdf0e10cSrcweir         OSL_VERIFY( impl_collectSubDocuments_nothrow() );
1006cdf0e10cSrcweir     }
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir 	//--------------------------------------------------------------------
~MigrationEngine_Impl()1009cdf0e10cSrcweir     MigrationEngine_Impl::~MigrationEngine_Impl()
1010cdf0e10cSrcweir     {
1011cdf0e10cSrcweir     }
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir 	//--------------------------------------------------------------------
migrateAll()1014cdf0e10cSrcweir     bool MigrationEngine_Impl::migrateAll()
1015cdf0e10cSrcweir     {
1016cdf0e10cSrcweir         if  ( m_aSubDocs.empty() )
1017cdf0e10cSrcweir         {
1018cdf0e10cSrcweir             OSL_ENSURE( false, "MigrationEngine_Impl::migrateAll: no forms/reports found!" );
1019cdf0e10cSrcweir             // The whole migration wizard is not expected to be called when there are no forms/reports
1020cdf0e10cSrcweir             // with macros, not to mention when there are no forms/reports at all.
1021cdf0e10cSrcweir             return false;
1022cdf0e10cSrcweir         }
1023cdf0e10cSrcweir 
1024cdf0e10cSrcweir         // initialize global progress
1025cdf0e10cSrcweir         sal_Int32 nOverallRange( m_aSubDocs.size() );
1026cdf0e10cSrcweir         String sProgressSkeleton = String( MacroMigrationResId( STR_OVERALL_PROGRESS ) );
1027cdf0e10cSrcweir         sProgressSkeleton.SearchAndReplaceAscii( "$overall$", String::CreateFromInt32( nOverallRange ) );
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir         m_rProgress.start( nOverallRange );
1030cdf0e10cSrcweir 
1031cdf0e10cSrcweir         for (   SubDocuments::const_iterator doc = m_aSubDocs.begin();
1032cdf0e10cSrcweir                 doc != m_aSubDocs.end();
1033cdf0e10cSrcweir                 ++doc
1034cdf0e10cSrcweir             )
1035cdf0e10cSrcweir         {
1036cdf0e10cSrcweir             sal_Int32 nOverallProgressValue( doc - m_aSubDocs.begin() + 1 );
1037cdf0e10cSrcweir             // update overall progress text
1038cdf0e10cSrcweir             ::rtl::OUString sOverallProgress( sProgressSkeleton );
1039cdf0e10cSrcweir             ::comphelper::string::searchAndReplaceAsciiI( sOverallProgress, "$current$", ::rtl::OUString::valueOf( nOverallProgressValue ) );
1040cdf0e10cSrcweir             m_rProgress.setOverallProgressText( sOverallProgress );
1041cdf0e10cSrcweir 
1042cdf0e10cSrcweir             // migrate document
1043cdf0e10cSrcweir             if ( !impl_handleDocument_nothrow( *doc ) )
1044cdf0e10cSrcweir                 return false;
1045cdf0e10cSrcweir 
1046cdf0e10cSrcweir             // update overall progress vallue
1047cdf0e10cSrcweir             m_rProgress.setOverallProgressValue( nOverallProgressValue );
1048cdf0e10cSrcweir         }
1049cdf0e10cSrcweir 
1050cdf0e10cSrcweir         // commit the root storage of the database document, for all changes made so far to take effect
1051cdf0e10cSrcweir         if ( !lcl_commitDocumentStorage_nothrow( m_xDocumentModel, m_rLogger ) )
1052cdf0e10cSrcweir             return false;
1053cdf0e10cSrcweir 
1054cdf0e10cSrcweir         // save the document
1055cdf0e10cSrcweir         if ( !lcl_storeDocument_nothrow( m_xDocumentModel, m_rLogger ) )
1056cdf0e10cSrcweir             return false;
1057cdf0e10cSrcweir 
1058cdf0e10cSrcweir         return true;
1059cdf0e10cSrcweir     }
1060cdf0e10cSrcweir 
1061cdf0e10cSrcweir 	//--------------------------------------------------------------------
1062cdf0e10cSrcweir     namespace
1063cdf0e10cSrcweir     {
lcl_collectHierarchicalElementNames_throw(const Reference<XNameAccess> & _rxContainer,const::rtl::OUString & _rContainerLoc,SubDocuments & _out_rDocs,const SubDocumentType _eType,size_t & _io_counter)1064cdf0e10cSrcweir         void lcl_collectHierarchicalElementNames_throw(
1065cdf0e10cSrcweir             const Reference< XNameAccess >& _rxContainer, const ::rtl::OUString& _rContainerLoc,
1066cdf0e10cSrcweir             SubDocuments& _out_rDocs, const SubDocumentType _eType, size_t& _io_counter )
1067cdf0e10cSrcweir         {
1068cdf0e10cSrcweir             const ::rtl::OUString sHierarhicalBase(
1069cdf0e10cSrcweir                 _rContainerLoc.getLength()  ?   ::rtl::OUStringBuffer( _rContainerLoc ).appendAscii( "/" ).makeStringAndClear()
1070cdf0e10cSrcweir                                             :   ::rtl::OUString() );
1071cdf0e10cSrcweir 
1072cdf0e10cSrcweir             Sequence< ::rtl::OUString > aElementNames( _rxContainer->getElementNames() );
1073cdf0e10cSrcweir             for (   const ::rtl::OUString* elementName = aElementNames.getConstArray();
1074cdf0e10cSrcweir                     elementName != aElementNames.getConstArray() + aElementNames.getLength();
1075cdf0e10cSrcweir                     ++elementName
1076cdf0e10cSrcweir                 )
1077cdf0e10cSrcweir             {
1078cdf0e10cSrcweir                 Any aElement( _rxContainer->getByName( *elementName ) );
1079cdf0e10cSrcweir                 ::rtl::OUString sElementName( ::rtl::OUStringBuffer( sHierarhicalBase ).append( *elementName ) );
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir                 Reference< XNameAccess > xSubContainer( aElement, UNO_QUERY );
1082cdf0e10cSrcweir                 if ( xSubContainer.is() )
1083cdf0e10cSrcweir                 {
1084cdf0e10cSrcweir                     lcl_collectHierarchicalElementNames_throw( xSubContainer, sElementName, _out_rDocs, _eType, _io_counter );
1085cdf0e10cSrcweir                 }
1086cdf0e10cSrcweir                 else
1087cdf0e10cSrcweir                 {
1088cdf0e10cSrcweir                     Reference< XCommandProcessor > xCommandProcessor( aElement, UNO_QUERY );
1089cdf0e10cSrcweir                     OSL_ENSURE( xCommandProcessor.is(), "lcl_collectHierarchicalElementNames_throw: no container, and no comand processor? What *is* it, then?!" );
1090cdf0e10cSrcweir                     if ( xCommandProcessor.is() )
1091cdf0e10cSrcweir                     {
1092cdf0e10cSrcweir                         _out_rDocs.push_back( SubDocument( xCommandProcessor, sElementName, _eType, ++_io_counter ) );
1093cdf0e10cSrcweir                     }
1094cdf0e10cSrcweir                 }
1095cdf0e10cSrcweir             }
1096cdf0e10cSrcweir         }
1097cdf0e10cSrcweir     }
1098cdf0e10cSrcweir 
1099cdf0e10cSrcweir     //--------------------------------------------------------------------
impl_collectSubDocuments_nothrow()1100cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_collectSubDocuments_nothrow()
1101cdf0e10cSrcweir     {
1102cdf0e10cSrcweir         OSL_PRECOND( m_xDocument.is(), "MigrationEngine_Impl::impl_collectSubDocuments_nothrow: invalid document!" );
1103cdf0e10cSrcweir         if ( !m_xDocument.is() )
1104cdf0e10cSrcweir             return false;
1105cdf0e10cSrcweir 
1106cdf0e10cSrcweir         try
1107cdf0e10cSrcweir         {
1108cdf0e10cSrcweir             Reference< XNameAccess > xDocContainer( m_xDocument->getFormDocuments(), UNO_SET_THROW );
1109cdf0e10cSrcweir             m_nFormCount = 0;
1110cdf0e10cSrcweir             lcl_collectHierarchicalElementNames_throw( xDocContainer, ::rtl::OUString(), m_aSubDocs, eForm, m_nFormCount );
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir             xDocContainer.set( m_xDocument->getReportDocuments(), UNO_SET_THROW );
1113cdf0e10cSrcweir             m_nReportCount = 0;
1114cdf0e10cSrcweir             lcl_collectHierarchicalElementNames_throw( xDocContainer, ::rtl::OUString(), m_aSubDocs, eReport, m_nReportCount );
1115cdf0e10cSrcweir         }
1116cdf0e10cSrcweir         catch( const Exception& )
1117cdf0e10cSrcweir         {
1118cdf0e10cSrcweir             m_rLogger.logFailure( MigrationError(
1119cdf0e10cSrcweir                 ERR_COLLECTING_DOCUMENTS_FAILED,
1120cdf0e10cSrcweir                 ::cppu::getCaughtException()
1121cdf0e10cSrcweir             ) );
1122cdf0e10cSrcweir             return false;
1123cdf0e10cSrcweir         }
1124cdf0e10cSrcweir         return true;
1125cdf0e10cSrcweir     }
1126cdf0e10cSrcweir 
1127cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_handleDocument_nothrow(const SubDocument & _rDocument) const1128cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_handleDocument_nothrow( const SubDocument& _rDocument ) const
1129cdf0e10cSrcweir     {
1130cdf0e10cSrcweir         OSL_ENSURE( m_nCurrentDocumentID == -1,
1131cdf0e10cSrcweir             "MigrationEngine_Impl::impl_handleDocument_nothrow: there already is a current document!");
1132cdf0e10cSrcweir         m_nCurrentDocumentID = m_rLogger.startedDocument( _rDocument.eType, _rDocument.sHierarchicalName );
1133cdf0e10cSrcweir 
1134cdf0e10cSrcweir         // start the progress
1135cdf0e10cSrcweir         ::rtl::OUString sObjectName( lcl_getSubDocumentDescription( _rDocument ) );
1136cdf0e10cSrcweir         m_rProgress.startObject( sObjectName, ::rtl::OUString(), DEFAULT_DOC_PROGRESS_RANGE );
1137cdf0e10cSrcweir 
1138cdf0e10cSrcweir         // -----------------
1139cdf0e10cSrcweir         // load the document
1140cdf0e10cSrcweir         ::rtl::Reference< ProgressCapture > pStatusIndicator( new ProgressCapture( sObjectName, m_rProgress ) );
1141cdf0e10cSrcweir         SubDocument aSubDocument( _rDocument );
1142cdf0e10cSrcweir         OpenDocResult eResult = lcl_loadSubDocument_nothrow( aSubDocument, pStatusIndicator.get(), m_rLogger );
1143cdf0e10cSrcweir         if ( eResult != eOpenedDoc )
1144cdf0e10cSrcweir         {
1145cdf0e10cSrcweir             pStatusIndicator->dispose();
1146cdf0e10cSrcweir             m_rProgress.endObject();
1147cdf0e10cSrcweir             m_rLogger.finishedDocument( m_nCurrentDocumentID );
1148cdf0e10cSrcweir 			m_nCurrentDocumentID = -1;
1149cdf0e10cSrcweir             return ( eResult == eIgnoreDoc );
1150cdf0e10cSrcweir         }
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir         // -----------------
1153cdf0e10cSrcweir         // migrate the libraries
1154cdf0e10cSrcweir         ProgressDelegator aDelegator( m_rProgress, sObjectName, String( MacroMigrationResId( STR_MIGRATING_LIBS ) ) );
1155cdf0e10cSrcweir         ProgressMixer aProgressMixer( aDelegator );
1156cdf0e10cSrcweir         aProgressMixer.registerPhase( PHASE_JAVASCRIPT, 1 );
1157cdf0e10cSrcweir         aProgressMixer.registerPhase( PHASE_BEANSHELL, 1 );
1158cdf0e10cSrcweir         aProgressMixer.registerPhase( PHASE_PYTHON, 1 );
1159cdf0e10cSrcweir         aProgressMixer.registerPhase( PHASE_JAVA, 1 );
1160cdf0e10cSrcweir         aProgressMixer.registerPhase( PHASE_BASIC, 5 );
1161cdf0e10cSrcweir             // more weight than then others, assuming that usually, there are much more Basic macros than any other scripts
1162cdf0e10cSrcweir         aProgressMixer.registerPhase( PHASE_DIALOGS, 1 );
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir         bool bSuccess = impl_checkScriptStorageStructure_nothrow( aSubDocument );
1165cdf0e10cSrcweir 
1166cdf0e10cSrcweir         // migrate storage-based script libraries (which can be handled by mere storage operations)
1167cdf0e10cSrcweir         bSuccess = bSuccess
1168cdf0e10cSrcweir             &&  impl_migrateScriptStorage_nothrow( aSubDocument, eJavaScript, aProgressMixer, PHASE_JAVASCRIPT )
1169cdf0e10cSrcweir             &&  impl_migrateScriptStorage_nothrow( aSubDocument, eBeanShell, aProgressMixer, PHASE_BEANSHELL )
1170cdf0e10cSrcweir             &&  impl_migrateScriptStorage_nothrow( aSubDocument, ePython, aProgressMixer, PHASE_PYTHON )
1171cdf0e10cSrcweir             &&  impl_migrateScriptStorage_nothrow( aSubDocument, eJava, aProgressMixer, PHASE_JAVA );
1172cdf0e10cSrcweir 
1173cdf0e10cSrcweir         // migrate Basic and dialog libraries
1174cdf0e10cSrcweir         bSuccess =  bSuccess
1175cdf0e10cSrcweir                 &&  impl_migrateContainerLibraries_nothrow( aSubDocument, eBasic, aProgressMixer, PHASE_BASIC )
1176cdf0e10cSrcweir                 &&  impl_migrateContainerLibraries_nothrow( aSubDocument, eDialog, aProgressMixer, PHASE_DIALOGS );
1177cdf0e10cSrcweir                 // order matters: First Basic scripts, then dialogs. So we can adjust references from the latter
1178cdf0e10cSrcweir                 // to the former
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir         // adjust the events in the document
1181cdf0e10cSrcweir         // (note that errors are ignored here - failure to convert a script reference
1182cdf0e10cSrcweir         // is not considered a critical error)
1183cdf0e10cSrcweir         if ( bSuccess )
1184cdf0e10cSrcweir         {
1185cdf0e10cSrcweir             impl_adjustDocumentEvents_nothrow( aSubDocument );
1186cdf0e10cSrcweir             impl_adjustFormComponentEvents_nothrow( aSubDocument );
1187cdf0e10cSrcweir         }
1188cdf0e10cSrcweir 
1189cdf0e10cSrcweir         // -----------------
1190cdf0e10cSrcweir         // clean up
1191cdf0e10cSrcweir         // store the sub document, including removal of the (now obsolete) "Scripts" sub folder
1192cdf0e10cSrcweir         if ( m_rLogger.movedAnyLibrary( m_nCurrentDocumentID ) )
1193cdf0e10cSrcweir         {
1194cdf0e10cSrcweir             bSuccess =  bSuccess
1195cdf0e10cSrcweir                     &&  ScriptsStorage::removeFromDocument( aSubDocument.xDocument, m_rLogger )
1196cdf0e10cSrcweir                     &&  lcl_commitDocumentStorage_nothrow( aSubDocument.xDocument, m_rLogger )
1197cdf0e10cSrcweir                     &&  lcl_storeEmbeddedDocument_nothrow( aSubDocument );
1198cdf0e10cSrcweir         }
1199cdf0e10cSrcweir 
1200cdf0e10cSrcweir         // unload in any case, even if we were not successful
1201cdf0e10cSrcweir         bSuccess =  lcl_unloadSubDocument_nothrow( aSubDocument, m_rLogger )
1202cdf0e10cSrcweir                 &&  bSuccess;
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir         pStatusIndicator->dispose();
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir         // end the progress, just in case the ProgressCapture didn't receive the XStatusIndicator::end event
1207cdf0e10cSrcweir         m_rProgress.endObject();
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir         m_rLogger.finishedDocument( m_nCurrentDocumentID );
1210cdf0e10cSrcweir         m_nCurrentDocumentID = -1;
1211cdf0e10cSrcweir         return bSuccess;
1212cdf0e10cSrcweir     }
1213cdf0e10cSrcweir 
1214cdf0e10cSrcweir 	//--------------------------------------------------------------------
1215cdf0e10cSrcweir     namespace
1216cdf0e10cSrcweir     {
lcl_createTargetLibName(const SubDocument & _rDocument,const::rtl::OUString & _rSourceLibName,const Reference<XNameAccess> & _rxTargetContainer)1217cdf0e10cSrcweir         static ::rtl::OUString lcl_createTargetLibName( const SubDocument& _rDocument,
1218cdf0e10cSrcweir             const ::rtl::OUString& _rSourceLibName, const Reference< XNameAccess >& _rxTargetContainer )
1219cdf0e10cSrcweir         {
1220cdf0e10cSrcweir             // The new library name is composed from the prefix, the base name, and the old library name.
1221cdf0e10cSrcweir             const ::rtl::OUString sPrefix( ::rtl::OUString::createFromAscii( _rDocument.eType == eForm ? "Form_" : "Report_" ) );
1222cdf0e10cSrcweir 
1223cdf0e10cSrcweir             ::rtl::OUString sBaseName( _rDocument.sHierarchicalName.copy(
1224cdf0e10cSrcweir                 _rDocument.sHierarchicalName.lastIndexOf( '/' ) + 1 ) );
1225cdf0e10cSrcweir             // Normalize this name. In our current storage implementation (and script containers in a document
1226cdf0e10cSrcweir             // are finally mapped to sub storages of the document storage), not all characters are allowed.
1227cdf0e10cSrcweir             // The bug requesting to change this is #i95409#.
1228cdf0e10cSrcweir             // Unfortunately, the storage implementation does not complain if you use invalid characters/names, but instead
1229cdf0e10cSrcweir             // it silently accepts them, and produces garbage in the file (#i95408).
1230cdf0e10cSrcweir             // So, until especially the former is fixed, we need to strip the name from all invalid characters.
1231cdf0e10cSrcweir             // #i95865# / 2008-11-06 / frank.schoenheit@sun.com
1232cdf0e10cSrcweir 
1233cdf0e10cSrcweir             // The general idea is to replace invalid characters with '_'. However, since "valid" essentially means
1234cdf0e10cSrcweir             // ASCII only, this implies that for a lot of languages, we would simply replace everything with '_',
1235cdf0e10cSrcweir             // which of course is not desired.
1236cdf0e10cSrcweir             // So, we use a heuristics: If the name contains at most 3 invalid characters, and as many valid as invalid
1237cdf0e10cSrcweir             // characters, then we use the replacement. Otherwise, we just use a unambiguous number for the sub document.
1238cdf0e10cSrcweir             sal_Int32 nValid=0, nInvalid=0;
1239cdf0e10cSrcweir             const sal_Unicode* pBaseName = sBaseName.getStr();
1240cdf0e10cSrcweir             const sal_Int32 nBaseNameLen = sBaseName.getLength();
1241cdf0e10cSrcweir             for ( sal_Int32 i=0; i<nBaseNameLen; ++i )
1242cdf0e10cSrcweir             {
1243cdf0e10cSrcweir                 if ( ::comphelper::OStorageHelper::IsValidZipEntryFileName( pBaseName + i, 1, sal_False ) )
1244cdf0e10cSrcweir                     ++nValid;
1245cdf0e10cSrcweir                 else
1246cdf0e10cSrcweir                     ++nInvalid;
1247cdf0e10cSrcweir             }
1248cdf0e10cSrcweir             if ( ( nInvalid <= 3 ) && ( nInvalid * 2 <= nValid ) )
1249cdf0e10cSrcweir             {   // not "too many" invalid => replace them
1250cdf0e10cSrcweir                 ::rtl::OUStringBuffer aReplacement;
1251cdf0e10cSrcweir                 aReplacement.ensureCapacity( nBaseNameLen );
1252cdf0e10cSrcweir                 aReplacement.append( sBaseName );
1253cdf0e10cSrcweir                 const sal_Unicode* pReplacement = aReplacement.getStr();
1254cdf0e10cSrcweir                 for ( sal_Int32 i=0; i<nBaseNameLen; ++i )
1255cdf0e10cSrcweir                 {
1256cdf0e10cSrcweir                     if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( pReplacement + i, 1, sal_False ) )
1257cdf0e10cSrcweir                         aReplacement.setCharAt( i, '_' );
1258cdf0e10cSrcweir                 }
1259cdf0e10cSrcweir                 sBaseName = aReplacement.makeStringAndClear();
1260cdf0e10cSrcweir 
1261cdf0e10cSrcweir                 ::rtl::OUStringBuffer aNewLibNameAttempt;
1262cdf0e10cSrcweir                 aNewLibNameAttempt.append( sPrefix );
1263cdf0e10cSrcweir                 aNewLibNameAttempt.append( sBaseName );
1264cdf0e10cSrcweir                 aNewLibNameAttempt.appendAscii( "_" );
1265cdf0e10cSrcweir                 aNewLibNameAttempt.append( _rSourceLibName );
1266cdf0e10cSrcweir                 ::rtl::OUString sTargetName( aNewLibNameAttempt.makeStringAndClear() );
1267cdf0e10cSrcweir                 if ( !_rxTargetContainer->hasByName( sTargetName ) )
1268cdf0e10cSrcweir                     return sTargetName;
1269cdf0e10cSrcweir             }
1270cdf0e10cSrcweir 
1271cdf0e10cSrcweir             // "too many" invalid characters, or the name composed with the base name was already used.
1272cdf0e10cSrcweir             // (The latter is valid, since there can be multiple sub documents with the same base name,
1273cdf0e10cSrcweir             // in different levels in the hierarchy.)
1274cdf0e10cSrcweir             // In this case, just use the umambiguous sub document number.
1275cdf0e10cSrcweir             ::rtl::OUStringBuffer aNewLibName;
1276cdf0e10cSrcweir             aNewLibName.append( sPrefix );
1277cdf0e10cSrcweir             aNewLibName.append( ::rtl::OUString::valueOf( sal_Int64( _rDocument.nNumber ) ) );
1278cdf0e10cSrcweir             aNewLibName.appendAscii( "_" );
1279cdf0e10cSrcweir             aNewLibName.append( _rSourceLibName );
1280cdf0e10cSrcweir             return aNewLibName.makeStringAndClear();
1281cdf0e10cSrcweir         }
1282cdf0e10cSrcweir     }
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir     //--------------------------------------------------------------------
impl_checkScriptStorageStructure_nothrow(const SubDocument & _rDocument) const1285cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_checkScriptStorageStructure_nothrow( const SubDocument& _rDocument ) const
1286cdf0e10cSrcweir     {
1287cdf0e10cSrcweir         OSL_PRECOND( _rDocument.xDocument.is(), "MigrationEngine_Impl::impl_checkScriptStorageStructure_nothrow: invalid document!" );
1288cdf0e10cSrcweir         if ( !_rDocument.xDocument.is() )
1289cdf0e10cSrcweir             return false;
1290cdf0e10cSrcweir 
1291cdf0e10cSrcweir         try
1292cdf0e10cSrcweir         {
1293cdf0e10cSrcweir             // the root storage of the document whose scripts are to be migrated
1294cdf0e10cSrcweir             ScriptsStorage aDocStorage( _rDocument.xDocument, m_rLogger );
1295cdf0e10cSrcweir             if  ( !aDocStorage.isValid() )
1296cdf0e10cSrcweir             {   // no scripts at all, or no scripts of the given type
1297cdf0e10cSrcweir                 return !m_rLogger.hadFailure();
1298cdf0e10cSrcweir             }
1299cdf0e10cSrcweir             ::std::set< ::rtl::OUString > aElementNames( aDocStorage.getElementNames() );
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir             ScriptType aKnownStorageBasedTypes[] = {
1302cdf0e10cSrcweir                 eBeanShell, eJavaScript, ePython, eJava
1303cdf0e10cSrcweir             };
1304cdf0e10cSrcweir             for ( size_t i=0; i<sizeof( aKnownStorageBasedTypes ) / sizeof( aKnownStorageBasedTypes[0] ); ++i )
1305cdf0e10cSrcweir                 aElementNames.erase( lcl_getScriptsSubStorageName( aKnownStorageBasedTypes[i] ) );
1306cdf0e10cSrcweir 
1307cdf0e10cSrcweir             if ( !aElementNames.empty() )
1308cdf0e10cSrcweir             {
1309cdf0e10cSrcweir                 m_rLogger.logFailure( MigrationError(
1310cdf0e10cSrcweir                     ERR_UNKNOWN_SCRIPT_FOLDER,
1311cdf0e10cSrcweir                     lcl_getSubDocumentDescription( _rDocument ),
1312cdf0e10cSrcweir                     *aElementNames.begin()
1313cdf0e10cSrcweir                 ) );
1314cdf0e10cSrcweir                 return false;
1315cdf0e10cSrcweir             }
1316cdf0e10cSrcweir         }
1317cdf0e10cSrcweir         catch( const Exception& )
1318cdf0e10cSrcweir         {
1319cdf0e10cSrcweir             m_rLogger.logFailure( MigrationError(
1320cdf0e10cSrcweir                 ERR_EXAMINING_SCRIPTS_FOLDER_FAILED,
1321cdf0e10cSrcweir                 lcl_getSubDocumentDescription( _rDocument ),
1322cdf0e10cSrcweir                 ::cppu::getCaughtException()
1323cdf0e10cSrcweir             ) );
1324cdf0e10cSrcweir             return false;
1325cdf0e10cSrcweir         }
1326cdf0e10cSrcweir         return true;
1327cdf0e10cSrcweir     }
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir     //--------------------------------------------------------------------
impl_migrateScriptStorage_nothrow(const SubDocument & _rDocument,const ScriptType _eScriptType,ProgressMixer & _rProgress,const PhaseID _nPhaseID) const1330cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_migrateScriptStorage_nothrow( const SubDocument& _rDocument,
1331cdf0e10cSrcweir         const ScriptType _eScriptType, ProgressMixer& _rProgress, const PhaseID _nPhaseID ) const
1332cdf0e10cSrcweir     {
1333cdf0e10cSrcweir         OSL_PRECOND( _rDocument.xDocument.is(), "MigrationEngine_Impl::impl_migrateScriptStorage_nothrow: invalid document!" );
1334cdf0e10cSrcweir         if ( !_rDocument.xDocument.is() )
1335cdf0e10cSrcweir             return false;
1336cdf0e10cSrcweir 
1337cdf0e10cSrcweir         ScriptsStorage aDatabaseScripts( m_rLogger );
1338cdf0e10cSrcweir             // the scripts of our complete database document - created on demand only
1339cdf0e10cSrcweir         SharedStorage xTargetStorage;
1340cdf0e10cSrcweir             // the target for moving the scripts storages - created on demand only
1341cdf0e10cSrcweir 
1342cdf0e10cSrcweir         PhaseGuard aPhase( _rProgress );
1343cdf0e10cSrcweir         bool bSuccess = false;
1344cdf0e10cSrcweir         Any aException;
1345cdf0e10cSrcweir         try
1346cdf0e10cSrcweir         {
1347cdf0e10cSrcweir             // the root storage of the document whose scripts are to be migrated
1348cdf0e10cSrcweir             ScriptsStorage aDocStorage( _rDocument.xDocument, m_rLogger );
1349cdf0e10cSrcweir             if  (   !aDocStorage.isValid()
1350cdf0e10cSrcweir                 ||  !aDocStorage.hasScripts( _eScriptType )
1351cdf0e10cSrcweir                 )
1352cdf0e10cSrcweir             {
1353cdf0e10cSrcweir                 // no scripts at all, or no scripts of the given type
1354cdf0e10cSrcweir                 _rProgress.startPhase( _nPhaseID, 1 );
1355cdf0e10cSrcweir                 _rProgress.endPhase();
1356cdf0e10cSrcweir                 return !m_rLogger.hadFailure();
1357cdf0e10cSrcweir             }
1358cdf0e10cSrcweir 
1359cdf0e10cSrcweir             SharedStorage xScriptsRoot( aDocStorage.getScriptsRoot( _eScriptType ) );
1360cdf0e10cSrcweir             if ( !xScriptsRoot.is() )
1361cdf0e10cSrcweir                 throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "internal error" ) ), NULL );
1362cdf0e10cSrcweir 
1363cdf0e10cSrcweir             // loop through the script libraries
1364cdf0e10cSrcweir             Sequence< ::rtl::OUString > aStorageElements( xScriptsRoot->getElementNames() );
1365cdf0e10cSrcweir             aPhase.start( _nPhaseID, aStorageElements.getLength() );
1366cdf0e10cSrcweir 
1367cdf0e10cSrcweir             for (   const ::rtl::OUString* element = aStorageElements.getConstArray();
1368cdf0e10cSrcweir                     element != aStorageElements.getConstArray() + aStorageElements.getLength();
1369cdf0e10cSrcweir                     ++element
1370cdf0e10cSrcweir                 )
1371cdf0e10cSrcweir             {
1372cdf0e10cSrcweir                 bool bIsScriptLibrary = xScriptsRoot->isStorageElement( *element );
1373cdf0e10cSrcweir                 OSL_ENSURE( bIsScriptLibrary,
1374cdf0e10cSrcweir                     "MigrationEngine_Impl::impl_migrateScriptStorage_nothrow: warning: unknown scripts storage structure!" );
1375cdf0e10cSrcweir                     // we cannot handle this. We would need to copy this stream to the respective scripts storage
1376cdf0e10cSrcweir                     // of the database document, but we cannot guarantee that the name is not used, yet, and we cannot
1377cdf0e10cSrcweir                     // simply rename the thing.
1378cdf0e10cSrcweir                 if ( !bIsScriptLibrary )
1379cdf0e10cSrcweir                 {
1380cdf0e10cSrcweir                     m_rLogger.logFailure( MigrationError(
1381cdf0e10cSrcweir                         ERR_UNEXPECTED_LIBSTORAGE_ELEMENT,
1382cdf0e10cSrcweir                         lcl_getSubDocumentDescription( _rDocument ),
1383cdf0e10cSrcweir                         getScriptTypeDisplayName( _eScriptType ),
1384cdf0e10cSrcweir                         *element
1385cdf0e10cSrcweir                     ) );
1386cdf0e10cSrcweir                     return false;
1387cdf0e10cSrcweir                 }
1388cdf0e10cSrcweir 
1389cdf0e10cSrcweir                 // ensure we have access to the DBDoc's scripts storage
1390cdf0e10cSrcweir                 if ( !aDatabaseScripts.isValid() )
1391cdf0e10cSrcweir                 {   // not needed 'til now
1392cdf0e10cSrcweir                     aDatabaseScripts.bind( m_xDocumentModel );
1393cdf0e10cSrcweir                     if ( aDatabaseScripts.isValid() )
1394cdf0e10cSrcweir                         xTargetStorage = aDatabaseScripts.getScriptsRoot( _eScriptType );
1395cdf0e10cSrcweir 
1396cdf0e10cSrcweir                     if ( !xTargetStorage.is() )
1397cdf0e10cSrcweir                     {
1398cdf0e10cSrcweir                         m_rLogger.logFailure( MigrationError(
1399cdf0e10cSrcweir                             ERR_CREATING_DBDOC_SCRIPT_STORAGE_FAILED,
1400cdf0e10cSrcweir                             getScriptTypeDisplayName( _eScriptType )
1401cdf0e10cSrcweir                         ) );
1402cdf0e10cSrcweir                         return false;
1403cdf0e10cSrcweir                     }
1404cdf0e10cSrcweir                 }
1405cdf0e10cSrcweir 
1406cdf0e10cSrcweir                 // move the library to the DBDoc's scripts library, under the new name
1407cdf0e10cSrcweir                 ::rtl::OUString sNewLibName( lcl_createTargetLibName( _rDocument, *element, xTargetStorage.getTyped().get() ) );
1408cdf0e10cSrcweir                 xScriptsRoot->moveElementTo( *element, xTargetStorage, sNewLibName );
1409cdf0e10cSrcweir 
1410cdf0e10cSrcweir                 // log the fact that we moved the library
1411cdf0e10cSrcweir                 m_rLogger.movedLibrary( m_nCurrentDocumentID, _eScriptType, *element, sNewLibName );
1412cdf0e10cSrcweir 
1413cdf0e10cSrcweir                 // progress
1414cdf0e10cSrcweir                 _rProgress.advancePhase( element - aStorageElements.getConstArray() );
1415cdf0e10cSrcweir             }
1416cdf0e10cSrcweir 
1417cdf0e10cSrcweir             // commit the storages, so the changes we made persist
1418cdf0e10cSrcweir             if  (   !lcl_commitStorage_nothrow( xScriptsRoot )
1419cdf0e10cSrcweir                 ||  ( xTargetStorage.is() && !lcl_commitStorage_nothrow( xTargetStorage ) )
1420cdf0e10cSrcweir                 )
1421cdf0e10cSrcweir             {
1422cdf0e10cSrcweir                 m_rLogger.logFailure( MigrationError(
1423cdf0e10cSrcweir                     ERR_COMMITTING_SCRIPT_STORAGES_FAILED,
1424cdf0e10cSrcweir                     getScriptTypeDisplayName( _eScriptType ),
1425cdf0e10cSrcweir                     lcl_getSubDocumentDescription( _rDocument )
1426cdf0e10cSrcweir                 ) );
1427cdf0e10cSrcweir                 return false;
1428cdf0e10cSrcweir             }
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir             // now that the concrete scripts storage does not have any elements anymore,
1431cdf0e10cSrcweir             // remove it
1432cdf0e10cSrcweir             xScriptsRoot.reset( NULL ); // need to reset the storage to be allowed to remove it
1433cdf0e10cSrcweir             aDocStorage.removeScriptTypeStorage( _eScriptType );
1434cdf0e10cSrcweir 
1435cdf0e10cSrcweir             // done so far
1436cdf0e10cSrcweir             bSuccess =  aDocStorage.commit()
1437cdf0e10cSrcweir                     &&  aDatabaseScripts.commit();
1438cdf0e10cSrcweir         }
1439cdf0e10cSrcweir         catch( const Exception& )
1440cdf0e10cSrcweir         {
1441cdf0e10cSrcweir             aException = ::cppu::getCaughtException();
1442cdf0e10cSrcweir             bSuccess = false;
1443cdf0e10cSrcweir         }
1444cdf0e10cSrcweir 
1445cdf0e10cSrcweir         // log the error, if any
1446cdf0e10cSrcweir         if ( !bSuccess )
1447cdf0e10cSrcweir         {
1448cdf0e10cSrcweir             m_rLogger.logFailure( MigrationError(
1449cdf0e10cSrcweir                 ERR_GENERAL_SCRIPT_MIGRATION_FAILURE,
1450cdf0e10cSrcweir                 getScriptTypeDisplayName( _eScriptType ),
1451cdf0e10cSrcweir                 lcl_getSubDocumentDescription( _rDocument ),
1452cdf0e10cSrcweir                 aException
1453cdf0e10cSrcweir             ) );
1454cdf0e10cSrcweir         }
1455cdf0e10cSrcweir 
1456cdf0e10cSrcweir         return bSuccess;
1457cdf0e10cSrcweir     }
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_migrateContainerLibraries_nothrow(const SubDocument & _rDocument,const ScriptType _eScriptType,ProgressMixer & _rProgress,const PhaseID _nPhaseID) const1460cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_migrateContainerLibraries_nothrow( const SubDocument& _rDocument,
1461cdf0e10cSrcweir             const ScriptType _eScriptType, ProgressMixer& _rProgress, const PhaseID _nPhaseID ) const
1462cdf0e10cSrcweir     {
1463cdf0e10cSrcweir 		OSL_PRECOND( ( _eScriptType == eBasic ) || ( _eScriptType == eDialog ),
1464cdf0e10cSrcweir             "MigrationEngine_Impl::impl_migrateContainerLibraries_nothrow: illegal script type!" );
1465cdf0e10cSrcweir 
1466cdf0e10cSrcweir         bool bSuccess = false;
1467cdf0e10cSrcweir         PhaseGuard aPhase( _rProgress );
1468cdf0e10cSrcweir         Any aException;
1469cdf0e10cSrcweir         do  // artificial loop for flow control only
1470cdf0e10cSrcweir         {
1471cdf0e10cSrcweir         try
1472cdf0e10cSrcweir         {
1473cdf0e10cSrcweir             // access library container of the sub document
1474cdf0e10cSrcweir             Reference< XEmbeddedScripts > xSubDocScripts( _rDocument.xDocument, UNO_QUERY );
1475cdf0e10cSrcweir             if ( !xSubDocScripts.is() )
1476cdf0e10cSrcweir             {   // no script support in the sub document -> nothing to migrate
1477cdf0e10cSrcweir                 // (though ... this is suspicious, at least ...)
1478cdf0e10cSrcweir                 bSuccess = true;
1479cdf0e10cSrcweir                 break;
1480cdf0e10cSrcweir             }
1481cdf0e10cSrcweir 
1482cdf0e10cSrcweir             Reference< XStorageBasedLibraryContainer > xSourceLibraries(
1483cdf0e10cSrcweir                 _eScriptType == eBasic ? xSubDocScripts->getBasicLibraries() : xSubDocScripts->getDialogLibraries(),
1484cdf0e10cSrcweir                 UNO_QUERY_THROW
1485cdf0e10cSrcweir             );
1486cdf0e10cSrcweir             Reference< XLibraryContainerPassword > xSourcePasswords( xSourceLibraries, UNO_QUERY );
1487cdf0e10cSrcweir             OSL_ENSURE( xSourcePasswords.is(),
1488cdf0e10cSrcweir                 "MigrationEngine_Impl::impl_migrateContainerLibraries_nothrow: suspicious: no password management for the source libraries!" );
1489cdf0e10cSrcweir 
1490cdf0e10cSrcweir             Sequence< ::rtl::OUString > aSourceLibNames( xSourceLibraries->getElementNames() );
1491cdf0e10cSrcweir             aPhase.start( _nPhaseID, aSourceLibNames.getLength() );
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir             if ( !xSourceLibraries->hasElements() )
1494cdf0e10cSrcweir             {
1495cdf0e10cSrcweir                 bSuccess = true;
1496cdf0e10cSrcweir                 break;
1497cdf0e10cSrcweir             }
1498cdf0e10cSrcweir 
1499cdf0e10cSrcweir             // create library containers for the document - those will be the target for the migration
1500cdf0e10cSrcweir             Reference< XStorageBasedDocument > xStorageDoc( m_xDocument, UNO_QUERY_THROW );
1501cdf0e10cSrcweir             Reference< XStorageBasedLibraryContainer > xTargetLibraries;
1502cdf0e10cSrcweir             if ( _eScriptType == eBasic )
1503cdf0e10cSrcweir             {
1504cdf0e10cSrcweir                 xTargetLibraries.set( DocumentScriptLibraryContainer::create(
1505cdf0e10cSrcweir                     m_aContext.getUNOContext(), xStorageDoc ), UNO_QUERY_THROW );
1506cdf0e10cSrcweir             }
1507cdf0e10cSrcweir             else
1508cdf0e10cSrcweir             {
1509cdf0e10cSrcweir                 xTargetLibraries.set( DocumentDialogLibraryContainer::create(
1510cdf0e10cSrcweir                     m_aContext.getUNOContext(), xStorageDoc ), UNO_QUERY_THROW );
1511cdf0e10cSrcweir             }
1512cdf0e10cSrcweir 
1513cdf0e10cSrcweir             // copy all libs to the target, with potentially renaming them
1514cdf0e10cSrcweir             const ::rtl::OUString* pSourceLibBegin = aSourceLibNames.getConstArray();
1515cdf0e10cSrcweir             const ::rtl::OUString* pSourceLibEnd = pSourceLibBegin + aSourceLibNames.getLength();
1516cdf0e10cSrcweir             for (   const ::rtl::OUString* pSourceLibName = pSourceLibBegin;
1517cdf0e10cSrcweir                     pSourceLibName != pSourceLibEnd;
1518cdf0e10cSrcweir                     ++pSourceLibName
1519cdf0e10cSrcweir                 )
1520cdf0e10cSrcweir             {
1521cdf0e10cSrcweir                 // if the library is password-protected, ask the user to unprotect it
1522cdf0e10cSrcweir                 if  (   xSourcePasswords.is()
1523cdf0e10cSrcweir                     &&  xSourcePasswords->isLibraryPasswordProtected( *pSourceLibName )
1524cdf0e10cSrcweir                     &&  !xSourcePasswords->isLibraryPasswordVerified( *pSourceLibName )
1525cdf0e10cSrcweir                     )
1526cdf0e10cSrcweir                 {
1527cdf0e10cSrcweir                     if ( !impl_unprotectPasswordLibrary_throw( xSourcePasswords, _eScriptType, *pSourceLibName ) )
1528cdf0e10cSrcweir                     {
1529cdf0e10cSrcweir                         m_rLogger.logFailure( MigrationError(
1530cdf0e10cSrcweir                             ERR_PASSWORD_VERIFICATION_FAILED,
1531cdf0e10cSrcweir                             _rDocument.sHierarchicalName,
1532cdf0e10cSrcweir                             getScriptTypeDisplayName( _eScriptType ),
1533cdf0e10cSrcweir                             *pSourceLibName
1534cdf0e10cSrcweir                         ) );
1535cdf0e10cSrcweir                         return false;
1536cdf0e10cSrcweir                     }
1537cdf0e10cSrcweir                 }
1538cdf0e10cSrcweir 
1539cdf0e10cSrcweir                 ::rtl::OUString sNewLibName( lcl_createTargetLibName( _rDocument, *pSourceLibName, xTargetLibraries.get() ) );
1540cdf0e10cSrcweir 
1541cdf0e10cSrcweir                 if ( xSourceLibraries->isLibraryLink( *pSourceLibName ) )
1542cdf0e10cSrcweir                 {
1543cdf0e10cSrcweir                     // just re-create the link in the target library
1544cdf0e10cSrcweir                     xTargetLibraries->createLibraryLink(
1545cdf0e10cSrcweir                         sNewLibName,
1546cdf0e10cSrcweir                         xSourceLibraries->getLibraryLinkURL( *pSourceLibName ),
1547cdf0e10cSrcweir                         xSourceLibraries->isLibraryReadOnly( *pSourceLibName )
1548cdf0e10cSrcweir                     );
1549cdf0e10cSrcweir                 }
1550cdf0e10cSrcweir                 else
1551cdf0e10cSrcweir                 {
1552cdf0e10cSrcweir                     if ( !xSourceLibraries->isLibraryLoaded( *pSourceLibName ) )
1553cdf0e10cSrcweir                         xSourceLibraries->loadLibrary( *pSourceLibName );
1554cdf0e10cSrcweir 
1555cdf0e10cSrcweir                     // copy the content of this particular libary
1556cdf0e10cSrcweir                     Reference< XNameAccess > xSourceLib( xSourceLibraries->getByName( *pSourceLibName ), UNO_QUERY_THROW );
1557cdf0e10cSrcweir                     Reference< XNameContainer > xTargetLib( xTargetLibraries->createLibrary( sNewLibName ), UNO_QUERY_THROW );
1558cdf0e10cSrcweir 
1559cdf0e10cSrcweir                     Sequence< ::rtl::OUString > aLibElementNames( xSourceLib->getElementNames() );
1560cdf0e10cSrcweir                     for (   const ::rtl::OUString* pSourceElementName = aLibElementNames.getConstArray();
1561cdf0e10cSrcweir                             pSourceElementName != aLibElementNames.getConstArray() + aLibElementNames.getLength();
1562cdf0e10cSrcweir                             ++pSourceElementName
1563cdf0e10cSrcweir                         )
1564cdf0e10cSrcweir                     {
1565cdf0e10cSrcweir                         Any aElement = xSourceLib->getByName( *pSourceElementName );
1566cdf0e10cSrcweir                         OSL_ENSURE( aElement.hasValue(),
1567cdf0e10cSrcweir                             "MigrationEngine_Impl::impl_migrateContainerLibraries_nothrow: invalid (empty) lib element!" );
1568cdf0e10cSrcweir 
1569cdf0e10cSrcweir                         // if this is a dialog, adjust the references to scripts
1570cdf0e10cSrcweir                         if ( _eScriptType == eDialog )
1571cdf0e10cSrcweir                         {
1572cdf0e10cSrcweir                             impl_adjustDialogEvents_nothrow( aElement, lcl_getSubDocumentDescription( _rDocument ),
1573cdf0e10cSrcweir                                 *pSourceLibName, *pSourceElementName );
1574cdf0e10cSrcweir                         }
1575cdf0e10cSrcweir 
1576cdf0e10cSrcweir                         xTargetLib->insertByName( *pSourceElementName, aElement );
1577cdf0e10cSrcweir                     }
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir                     // transfer the read-only flag
1580cdf0e10cSrcweir                     xTargetLibraries->setLibraryReadOnly(
1581cdf0e10cSrcweir                         sNewLibName, xSourceLibraries->isLibraryReadOnly( *pSourceLibName ) );
1582cdf0e10cSrcweir                 }
1583cdf0e10cSrcweir 
1584cdf0e10cSrcweir                 // remove the source lib
1585cdf0e10cSrcweir                 xSourceLibraries->removeLibrary( *pSourceLibName );
1586cdf0e10cSrcweir 
1587cdf0e10cSrcweir                 // tell the logger
1588cdf0e10cSrcweir                 m_rLogger.movedLibrary( m_nCurrentDocumentID, _eScriptType, *pSourceLibName, sNewLibName );
1589cdf0e10cSrcweir 
1590cdf0e10cSrcweir                 // tell the progress
1591cdf0e10cSrcweir                 _rProgress.advancePhase( pSourceLibName - pSourceLibBegin );
1592cdf0e10cSrcweir             }
1593cdf0e10cSrcweir 
1594cdf0e10cSrcweir             // clean up
1595cdf0e10cSrcweir             xSourceLibraries->storeLibraries();
1596cdf0e10cSrcweir 
1597cdf0e10cSrcweir             xTargetLibraries->storeLibraries();
1598cdf0e10cSrcweir             Reference< XStorage > xTargetRoot( xTargetLibraries->getRootLocation(), UNO_QUERY_THROW );
1599cdf0e10cSrcweir             bSuccess = lcl_commitStorage_nothrow( xTargetRoot );
1600cdf0e10cSrcweir         }
1601cdf0e10cSrcweir         catch( const Exception& )
1602cdf0e10cSrcweir         {
1603cdf0e10cSrcweir             aException = ::cppu::getCaughtException();
1604cdf0e10cSrcweir             bSuccess = false;
1605cdf0e10cSrcweir         }
1606cdf0e10cSrcweir         } while ( false );
1607cdf0e10cSrcweir 
1608cdf0e10cSrcweir         // log the error, if any
1609cdf0e10cSrcweir         if ( !bSuccess )
1610cdf0e10cSrcweir         {
1611cdf0e10cSrcweir             m_rLogger.logFailure( MigrationError(
1612cdf0e10cSrcweir                 ERR_GENERAL_MACRO_MIGRATION_FAILURE,
1613cdf0e10cSrcweir                 lcl_getSubDocumentDescription( _rDocument ),
1614cdf0e10cSrcweir                 aException
1615cdf0e10cSrcweir             ) );
1616cdf0e10cSrcweir         }
1617cdf0e10cSrcweir 
1618cdf0e10cSrcweir         return bSuccess;
1619cdf0e10cSrcweir     }
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustScriptLibrary_nothrow(const::rtl::OUString & _rScriptType,::rtl::OUString & _inout_rScriptCode) const1622cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow( const ::rtl::OUString& _rScriptType,
1623cdf0e10cSrcweir             ::rtl::OUString& _inout_rScriptCode ) const
1624cdf0e10cSrcweir     {
1625cdf0e10cSrcweir         OSL_PRECOND( _inout_rScriptCode.getLength(), "MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow: invalid script!" );
1626cdf0e10cSrcweir         if ( !_inout_rScriptCode.getLength() )
1627cdf0e10cSrcweir             return false;
1628cdf0e10cSrcweir 
1629cdf0e10cSrcweir         bool bSuccess = false;
1630cdf0e10cSrcweir         Any aException;
1631cdf0e10cSrcweir         try
1632cdf0e10cSrcweir         {
1633cdf0e10cSrcweir             if  (   !_rScriptType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Script" ) )
1634cdf0e10cSrcweir                 ||  !_rScriptType.getLength()
1635cdf0e10cSrcweir                 )
1636cdf0e10cSrcweir             {
1637cdf0e10cSrcweir                 OSL_ENSURE( false,
1638cdf0e10cSrcweir                     "MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow: no or unknown script type!" );
1639cdf0e10cSrcweir                 m_rLogger.logRecoverable( MigrationError(
1640cdf0e10cSrcweir                     ERR_UNKNOWN_SCRIPT_TYPE,
1641cdf0e10cSrcweir                     _rScriptType
1642cdf0e10cSrcweir                 ) );
1643cdf0e10cSrcweir                 return false;
1644cdf0e10cSrcweir             }
1645cdf0e10cSrcweir 
1646cdf0e10cSrcweir             // analyze the script URI
1647cdf0e10cSrcweir             Reference< XUriReferenceFactory > xUriRefFac = UriReferenceFactory::create( m_aContext.getUNOContext() );
1648cdf0e10cSrcweir             Reference< XVndSunStarScriptUrlReference > xUri( xUriRefFac->parse( _inout_rScriptCode ), UNO_QUERY_THROW );
1649cdf0e10cSrcweir 
1650cdf0e10cSrcweir             ::rtl::OUString sScriptLanguage = xUri->getParameter(
1651cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "language" ) ) );
1652cdf0e10cSrcweir             ScriptType eScriptType = eBasic;
1653cdf0e10cSrcweir             if ( !lcl_getScriptTypeFromLanguage( sScriptLanguage, eScriptType ) )
1654cdf0e10cSrcweir             {
1655cdf0e10cSrcweir                 OSL_ENSURE( false,
1656cdf0e10cSrcweir                     "MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow: unknown script language!" );
1657cdf0e10cSrcweir                 m_rLogger.logRecoverable( MigrationError(
1658cdf0e10cSrcweir                     ERR_UNKNOWN_SCRIPT_LANGUAGE,
1659cdf0e10cSrcweir                     sScriptLanguage
1660cdf0e10cSrcweir                 ) );
1661cdf0e10cSrcweir                 return false;
1662cdf0e10cSrcweir             }
1663cdf0e10cSrcweir 
1664cdf0e10cSrcweir             ::rtl::OUString sLocation = xUri->getParameter(
1665cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "location" ) ) );
1666cdf0e10cSrcweir             if ( !sLocation.equalsAscii( "document" ) )
1667cdf0e10cSrcweir             {
1668cdf0e10cSrcweir                 // only document libraries must be migrated, of course
1669cdf0e10cSrcweir                 return false;
1670cdf0e10cSrcweir             }
1671cdf0e10cSrcweir 
1672cdf0e10cSrcweir             ::rtl::OUString sScriptName = xUri->getName();
1673cdf0e10cSrcweir             sal_Int32 nLibModuleSeparator = sScriptName.indexOf( '.' );
1674cdf0e10cSrcweir             if ( nLibModuleSeparator < 0 )
1675cdf0e10cSrcweir             {
1676cdf0e10cSrcweir                 OSL_ENSURE( false,
1677cdf0e10cSrcweir                     "MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow: invalid/unknown location format!" );
1678cdf0e10cSrcweir                 m_rLogger.logRecoverable( MigrationError(
1679cdf0e10cSrcweir                     ERR_UNKNOWN_SCRIPT_NAME_FORMAT,
1680cdf0e10cSrcweir                     sScriptName
1681cdf0e10cSrcweir                 ) );
1682cdf0e10cSrcweir                 return false;
1683cdf0e10cSrcweir             }
1684cdf0e10cSrcweir 
1685cdf0e10cSrcweir             // replace the library name
1686cdf0e10cSrcweir             ::rtl::OUString sLibrary = sScriptName.copy( 0, nLibModuleSeparator );
1687cdf0e10cSrcweir             ::rtl::OUString sNewLibName = m_rLogger.getNewLibraryName(
1688cdf0e10cSrcweir                 m_nCurrentDocumentID, eScriptType, sLibrary );
1689cdf0e10cSrcweir             OSL_ENSURE( sLibrary != sNewLibName,
1690cdf0e10cSrcweir                 "MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow: a library which has not been migrated?" );
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir             ::rtl::OUStringBuffer aNewLocation;
1693cdf0e10cSrcweir             aNewLocation.append( sNewLibName );
1694cdf0e10cSrcweir             aNewLocation.append( sScriptName.copy( nLibModuleSeparator ) );
1695cdf0e10cSrcweir             xUri->setName( aNewLocation.makeStringAndClear() );
1696cdf0e10cSrcweir 
1697cdf0e10cSrcweir             // update the new script URL
1698cdf0e10cSrcweir             _inout_rScriptCode = xUri->getUriReference();
1699cdf0e10cSrcweir             bSuccess = true;
1700cdf0e10cSrcweir         }
1701cdf0e10cSrcweir         catch( const Exception& )
1702cdf0e10cSrcweir         {
1703cdf0e10cSrcweir             aException = ::cppu::getCaughtException();
1704cdf0e10cSrcweir             bSuccess = false;
1705cdf0e10cSrcweir         }
1706cdf0e10cSrcweir 
1707cdf0e10cSrcweir         // log the failure, if any
1708cdf0e10cSrcweir         if ( !bSuccess )
1709cdf0e10cSrcweir         {
1710cdf0e10cSrcweir             m_rLogger.logRecoverable( MigrationError(
1711cdf0e10cSrcweir                 ERR_SCRIPT_TRANSLATION_FAILURE,
1712cdf0e10cSrcweir                 _rScriptType,
1713cdf0e10cSrcweir                 _inout_rScriptCode,
1714cdf0e10cSrcweir                 aException
1715cdf0e10cSrcweir             ) );
1716cdf0e10cSrcweir         }
1717cdf0e10cSrcweir 
1718cdf0e10cSrcweir         return bSuccess;
1719cdf0e10cSrcweir     }
1720cdf0e10cSrcweir 
1721cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustScriptLibrary_nothrow(ScriptEventDescriptor & _inout_rScriptEvent) const1722cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow( ScriptEventDescriptor& _inout_rScriptEvent ) const
1723cdf0e10cSrcweir     {
1724cdf0e10cSrcweir         if ( _inout_rScriptEvent.ScriptType.getLength() && _inout_rScriptEvent.ScriptCode.getLength() )
1725cdf0e10cSrcweir             return impl_adjustScriptLibrary_nothrow( _inout_rScriptEvent.ScriptType, _inout_rScriptEvent.ScriptCode );
1726cdf0e10cSrcweir         return false;
1727cdf0e10cSrcweir     }
1728cdf0e10cSrcweir 
1729cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustScriptLibrary_nothrow(Any & _inout_rScriptDescriptor) const1730cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_adjustScriptLibrary_nothrow( Any& _inout_rScriptDescriptor ) const
1731cdf0e10cSrcweir     {
1732cdf0e10cSrcweir         ::comphelper::NamedValueCollection aScriptDesc( _inout_rScriptDescriptor );
1733cdf0e10cSrcweir 
1734cdf0e10cSrcweir         ::rtl::OUString sScriptType;
1735cdf0e10cSrcweir         ::rtl::OUString sScript;
1736cdf0e10cSrcweir         try
1737cdf0e10cSrcweir         {
1738cdf0e10cSrcweir             OSL_VERIFY( aScriptDesc.get_ensureType( "EventType", sScriptType ) );
1739cdf0e10cSrcweir             OSL_VERIFY( aScriptDesc.get_ensureType( "Script", sScript ) );
1740cdf0e10cSrcweir         }
1741cdf0e10cSrcweir         catch( const Exception& )
1742cdf0e10cSrcweir         {
1743cdf0e10cSrcweir             m_rLogger.logRecoverable( MigrationError(
1744cdf0e10cSrcweir                 ERR_INVALID_SCRIPT_DESCRIPTOR_FORMAT,
1745cdf0e10cSrcweir                 ::cppu::getCaughtException()
1746cdf0e10cSrcweir             ) );
1747cdf0e10cSrcweir         }
1748cdf0e10cSrcweir 
1749cdf0e10cSrcweir         if ( sScriptType.getLength() && sScript.getLength() )
1750cdf0e10cSrcweir             if ( !impl_adjustScriptLibrary_nothrow( sScriptType, sScript ) )
1751cdf0e10cSrcweir                 return false;
1752cdf0e10cSrcweir 
1753cdf0e10cSrcweir         aScriptDesc.put( "Script", sScript );
1754cdf0e10cSrcweir         _inout_rScriptDescriptor <<= aScriptDesc.getPropertyValues();
1755cdf0e10cSrcweir         return true;
1756cdf0e10cSrcweir     }
1757cdf0e10cSrcweir 
1758cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustDocumentEvents_nothrow(const SubDocument & _rDocument) const1759cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_adjustDocumentEvents_nothrow( const SubDocument& _rDocument ) const
1760cdf0e10cSrcweir     {
1761cdf0e10cSrcweir         try
1762cdf0e10cSrcweir         {
1763cdf0e10cSrcweir             Reference< XEventsSupplier > xSuppEvents( _rDocument.xDocument, UNO_QUERY );
1764cdf0e10cSrcweir             if ( !xSuppEvents.is() )
1765cdf0e10cSrcweir                 // this is allowed. E.g. new-style reports currently do not support this
1766cdf0e10cSrcweir                 return true;
1767cdf0e10cSrcweir 
1768cdf0e10cSrcweir             Reference< XNameReplace > xEvents( xSuppEvents->getEvents(), UNO_SET_THROW );
1769cdf0e10cSrcweir             Sequence< ::rtl::OUString > aEventNames = xEvents->getElementNames();
1770cdf0e10cSrcweir 
1771cdf0e10cSrcweir             Any aEvent;
1772cdf0e10cSrcweir             for (   const ::rtl::OUString* eventName = aEventNames.getConstArray();
1773cdf0e10cSrcweir                     eventName != aEventNames.getConstArray() + aEventNames.getLength();
1774cdf0e10cSrcweir                     ++eventName
1775cdf0e10cSrcweir                 )
1776cdf0e10cSrcweir             {
1777cdf0e10cSrcweir                 aEvent = xEvents->getByName( *eventName );
1778cdf0e10cSrcweir                 if ( !aEvent.hasValue() )
1779cdf0e10cSrcweir                     continue;
1780cdf0e10cSrcweir 
1781cdf0e10cSrcweir                 // translate
1782cdf0e10cSrcweir                 if ( !impl_adjustScriptLibrary_nothrow( aEvent ) )
1783cdf0e10cSrcweir                     continue;
1784cdf0e10cSrcweir 
1785cdf0e10cSrcweir                 // put back
1786cdf0e10cSrcweir                 xEvents->replaceByName( *eventName, aEvent );
1787cdf0e10cSrcweir             }
1788cdf0e10cSrcweir         }
1789cdf0e10cSrcweir         catch( const Exception& )
1790cdf0e10cSrcweir         {
1791cdf0e10cSrcweir             m_rLogger.logRecoverable( MigrationError(
1792cdf0e10cSrcweir                 ERR_ADJUSTING_DOCUMENT_EVENTS_FAILED,
1793cdf0e10cSrcweir                 lcl_getSubDocumentDescription( _rDocument ),
1794cdf0e10cSrcweir                 ::cppu::getCaughtException()
1795cdf0e10cSrcweir             ) );
1796cdf0e10cSrcweir             return false;
1797cdf0e10cSrcweir         }
1798cdf0e10cSrcweir         return true;
1799cdf0e10cSrcweir     }
1800cdf0e10cSrcweir 
1801cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustDialogElementEvents_throw(const Reference<XInterface> & _rxElement) const1802cdf0e10cSrcweir     void MigrationEngine_Impl::impl_adjustDialogElementEvents_throw( const Reference< XInterface >& _rxElement ) const
1803cdf0e10cSrcweir     {
1804cdf0e10cSrcweir         Reference< XScriptEventsSupplier > xEventsSupplier( _rxElement, UNO_QUERY_THROW );
1805cdf0e10cSrcweir         Reference< XNameReplace > xEvents( xEventsSupplier->getEvents(), UNO_QUERY_THROW );
1806cdf0e10cSrcweir         Sequence< ::rtl::OUString > aEventNames( xEvents->getElementNames() );
1807cdf0e10cSrcweir 
1808cdf0e10cSrcweir         const ::rtl::OUString* eventName = aEventNames.getArray();
1809cdf0e10cSrcweir         const ::rtl::OUString* eventNamesEnd = eventName + aEventNames.getLength();
1810cdf0e10cSrcweir 
1811cdf0e10cSrcweir         ScriptEventDescriptor aScriptEvent;
1812cdf0e10cSrcweir         for ( ; eventName != eventNamesEnd; ++eventName )
1813cdf0e10cSrcweir         {
1814cdf0e10cSrcweir             OSL_VERIFY( xEvents->getByName( *eventName ) >>= aScriptEvent );
1815cdf0e10cSrcweir 
1816cdf0e10cSrcweir             if ( !impl_adjustScriptLibrary_nothrow( aScriptEvent ) )
1817cdf0e10cSrcweir                 continue;
1818cdf0e10cSrcweir 
1819cdf0e10cSrcweir             xEvents->replaceByName( *eventName, makeAny( aScriptEvent ) );
1820cdf0e10cSrcweir         }
1821cdf0e10cSrcweir     }
1822cdf0e10cSrcweir 
1823cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustDialogEvents_nothrow(Any & _inout_rDialogLibraryElement,const::rtl::OUString & _rDocName,const::rtl::OUString & _rDialogLibName,const::rtl::OUString & _rDialogName) const1824cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_adjustDialogEvents_nothrow( Any& _inout_rDialogLibraryElement,
1825cdf0e10cSrcweir         const ::rtl::OUString& _rDocName, const ::rtl::OUString& _rDialogLibName, const ::rtl::OUString& _rDialogName ) const
1826cdf0e10cSrcweir     {
1827cdf0e10cSrcweir         try
1828cdf0e10cSrcweir         {
1829cdf0e10cSrcweir             // load a dialog model from the stream describing it
1830cdf0e10cSrcweir             Reference< XInputStreamProvider > xISP( _inout_rDialogLibraryElement, UNO_QUERY_THROW );
1831cdf0e10cSrcweir             Reference< XInputStream > xInput( xISP->createInputStream(), UNO_QUERY_THROW );
1832cdf0e10cSrcweir 
1833cdf0e10cSrcweir             Reference< XNameContainer > xDialogModel( m_aContext.createComponent( "com.sun.star.awt.UnoControlDialogModel" ), UNO_QUERY_THROW );
1834cdf0e10cSrcweir             ::xmlscript::importDialogModel( xInput, xDialogModel, m_aContext.getUNOContext() );
1835cdf0e10cSrcweir 
1836cdf0e10cSrcweir             // adjust the events of the dialog
1837cdf0e10cSrcweir             impl_adjustDialogElementEvents_throw( xDialogModel );
1838cdf0e10cSrcweir 
1839cdf0e10cSrcweir             // adjust the events of the controls
1840cdf0e10cSrcweir             Sequence< ::rtl::OUString > aControlNames( xDialogModel->getElementNames() );
1841cdf0e10cSrcweir             const ::rtl::OUString* controlName = aControlNames.getConstArray();
1842cdf0e10cSrcweir             const ::rtl::OUString* controlNamesEnd = controlName + aControlNames.getLength();
1843cdf0e10cSrcweir             for ( ; controlName != controlNamesEnd; ++controlName )
1844cdf0e10cSrcweir             {
1845cdf0e10cSrcweir                 impl_adjustDialogElementEvents_throw( Reference< XInterface >( xDialogModel->getByName( *controlName ), UNO_QUERY ) );
1846cdf0e10cSrcweir             }
1847cdf0e10cSrcweir 
1848cdf0e10cSrcweir             // export dialog model
1849cdf0e10cSrcweir             xISP = ::xmlscript::exportDialogModel( xDialogModel, m_aContext.getUNOContext() );
1850cdf0e10cSrcweir             _inout_rDialogLibraryElement <<= xISP;
1851cdf0e10cSrcweir         }
1852cdf0e10cSrcweir         catch( const Exception& )
1853cdf0e10cSrcweir         {
1854cdf0e10cSrcweir             m_rLogger.logRecoverable( MigrationError(
1855cdf0e10cSrcweir                 ERR_ADJUSTING_DIALOG_EVENTS_FAILED,
1856cdf0e10cSrcweir                 _rDocName,
1857cdf0e10cSrcweir                 _rDialogLibName,
1858cdf0e10cSrcweir                 _rDialogName,
1859cdf0e10cSrcweir                 ::cppu::getCaughtException()
1860cdf0e10cSrcweir             ) );
1861cdf0e10cSrcweir             return false;
1862cdf0e10cSrcweir         }
1863cdf0e10cSrcweir         return true;
1864cdf0e10cSrcweir     }
1865cdf0e10cSrcweir 
1866cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustFormComponentEvents_throw(const Reference<XIndexAccess> & _rxComponentContainer) const1867cdf0e10cSrcweir     void MigrationEngine_Impl::impl_adjustFormComponentEvents_throw( const Reference< XIndexAccess >& _rxComponentContainer ) const
1868cdf0e10cSrcweir     {
1869cdf0e10cSrcweir         FormComponentIterator aCompIter( _rxComponentContainer );
1870cdf0e10cSrcweir         while ( aCompIter.hasMore() )
1871cdf0e10cSrcweir         {
1872cdf0e10cSrcweir             // 1. adjust the component's scripts of the current component
1873cdf0e10cSrcweir             FormComponentScripts aComponent( aCompIter.next() );
1874cdf0e10cSrcweir             Sequence< ScriptEventDescriptor > aEvents( aComponent.getEvents() );
1875cdf0e10cSrcweir 
1876cdf0e10cSrcweir             bool bChangedComponentEvents = false;
1877cdf0e10cSrcweir             for (   ScriptEventDescriptor* scriptEvent = aEvents.getArray();
1878cdf0e10cSrcweir                     scriptEvent != aEvents.getArray() + aEvents.getLength();
1879cdf0e10cSrcweir                     ++scriptEvent
1880cdf0e10cSrcweir                 )
1881cdf0e10cSrcweir             {
1882cdf0e10cSrcweir                 if ( !impl_adjustScriptLibrary_nothrow( *scriptEvent ) )
1883cdf0e10cSrcweir                     continue;
1884cdf0e10cSrcweir 
1885cdf0e10cSrcweir                 bChangedComponentEvents = true;
1886cdf0e10cSrcweir             }
1887cdf0e10cSrcweir 
1888cdf0e10cSrcweir             if ( bChangedComponentEvents )
1889cdf0e10cSrcweir                 aComponent.setEvents( aEvents );
1890cdf0e10cSrcweir 
1891cdf0e10cSrcweir             // 2. step down if the component is a container itself
1892cdf0e10cSrcweir             Reference< XIndexAccess > xContainer( aComponent.getComponent(), UNO_QUERY );
1893cdf0e10cSrcweir             if ( xContainer.is() )
1894cdf0e10cSrcweir                 impl_adjustFormComponentEvents_throw( xContainer );
1895cdf0e10cSrcweir         }
1896cdf0e10cSrcweir     }
1897cdf0e10cSrcweir 
1898cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_adjustFormComponentEvents_nothrow(const SubDocument & _rDocument) const1899cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_adjustFormComponentEvents_nothrow( const SubDocument& _rDocument ) const
1900cdf0e10cSrcweir     {
1901cdf0e10cSrcweir         try
1902cdf0e10cSrcweir         {
1903cdf0e10cSrcweir             DrawPageIterator aPageIter( _rDocument.xDocument );
1904cdf0e10cSrcweir             while ( aPageIter.hasMore() )
1905cdf0e10cSrcweir             {
1906cdf0e10cSrcweir                 Reference< XFormsSupplier > xSuppForms( aPageIter.next(), UNO_QUERY_THROW );
1907cdf0e10cSrcweir                 Reference< XIndexAccess > xForms( xSuppForms->getForms(), UNO_QUERY_THROW );
1908cdf0e10cSrcweir                 impl_adjustFormComponentEvents_throw( xForms );
1909cdf0e10cSrcweir             }
1910cdf0e10cSrcweir         }
1911cdf0e10cSrcweir         catch( const Exception& )
1912cdf0e10cSrcweir         {
1913cdf0e10cSrcweir             m_rLogger.logRecoverable( MigrationError(
1914cdf0e10cSrcweir                 ERR_ADJUSTING_FORMCOMP_EVENTS_FAILED,
1915cdf0e10cSrcweir                 lcl_getSubDocumentDescription( _rDocument ),
1916cdf0e10cSrcweir                 ::cppu::getCaughtException()
1917cdf0e10cSrcweir             ) );
1918cdf0e10cSrcweir             return false;
1919cdf0e10cSrcweir         }
1920cdf0e10cSrcweir         return true;
1921cdf0e10cSrcweir     }
1922cdf0e10cSrcweir 
1923cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_unprotectPasswordLibrary_throw(const Reference<XLibraryContainerPassword> & _rxPasswordManager,const ScriptType _eScriptType,const::rtl::OUString & _rLibraryName) const1924cdf0e10cSrcweir     bool MigrationEngine_Impl::impl_unprotectPasswordLibrary_throw( const Reference< XLibraryContainerPassword >& _rxPasswordManager,
1925cdf0e10cSrcweir             const ScriptType _eScriptType, const ::rtl::OUString& _rLibraryName ) const
1926cdf0e10cSrcweir     {
1927cdf0e10cSrcweir         // a human-readable description of the affected library
1928cdf0e10cSrcweir         ::rtl::OUString sLibraryDescription( String(
1929cdf0e10cSrcweir             MacroMigrationResId( STR_LIBRARY_TYPE_AND_NAME ) ) );
1930cdf0e10cSrcweir         ::comphelper::string::searchAndReplaceAsciiI( sLibraryDescription, "$type$",
1931cdf0e10cSrcweir             getScriptTypeDisplayName( _eScriptType ) );
1932cdf0e10cSrcweir         ::comphelper::string::searchAndReplaceAsciiI( sLibraryDescription, "$library$",
1933cdf0e10cSrcweir             _rLibraryName );
1934cdf0e10cSrcweir 
1935cdf0e10cSrcweir         InteractionHandler aHandler( m_aContext, m_xDocumentModel );
1936cdf0e10cSrcweir         ::rtl::OUString sPassword;
1937cdf0e10cSrcweir         while ( true )
1938cdf0e10cSrcweir         {
1939cdf0e10cSrcweir             if ( !aHandler.requestDocumentPassword( sLibraryDescription, sPassword ) )
1940cdf0e10cSrcweir                 // aborted by the user
1941cdf0e10cSrcweir                 return false;
1942cdf0e10cSrcweir 
1943cdf0e10cSrcweir             bool bSuccessVerification = _rxPasswordManager->verifyLibraryPassword( _rLibraryName, sPassword );
1944cdf0e10cSrcweir             if ( bSuccessVerification )
1945cdf0e10cSrcweir                 return true;
1946cdf0e10cSrcweir         }
1947cdf0e10cSrcweir 
1948cdf0e10cSrcweir     }
1949cdf0e10cSrcweir 
1950cdf0e10cSrcweir 	//====================================================================
1951cdf0e10cSrcweir 	//= MigrationEngine
1952cdf0e10cSrcweir 	//====================================================================
1953cdf0e10cSrcweir 	//--------------------------------------------------------------------
MigrationEngine(const::comphelper::ComponentContext & _rContext,const Reference<XOfficeDatabaseDocument> & _rxDocument,IMigrationProgress & _rProgress,MigrationLog & _rLogger)1954cdf0e10cSrcweir     MigrationEngine::MigrationEngine( const ::comphelper::ComponentContext& _rContext,
1955cdf0e10cSrcweir             const Reference< XOfficeDatabaseDocument >& _rxDocument, IMigrationProgress& _rProgress,
1956cdf0e10cSrcweir             MigrationLog& _rLogger )
1957cdf0e10cSrcweir         :m_pImpl( new MigrationEngine_Impl( _rContext, _rxDocument, _rProgress, _rLogger ) )
1958cdf0e10cSrcweir     {
1959cdf0e10cSrcweir     }
1960cdf0e10cSrcweir 
1961cdf0e10cSrcweir 	//--------------------------------------------------------------------
~MigrationEngine()1962cdf0e10cSrcweir     MigrationEngine::~MigrationEngine()
1963cdf0e10cSrcweir     {
1964cdf0e10cSrcweir     }
1965cdf0e10cSrcweir 
1966cdf0e10cSrcweir 	//--------------------------------------------------------------------
getFormCount() const1967cdf0e10cSrcweir     sal_Int32 MigrationEngine::getFormCount() const
1968cdf0e10cSrcweir     {
1969cdf0e10cSrcweir         return m_pImpl->getFormCount();
1970cdf0e10cSrcweir     }
1971cdf0e10cSrcweir 
1972cdf0e10cSrcweir 	//--------------------------------------------------------------------
getReportCount() const1973cdf0e10cSrcweir     sal_Int32 MigrationEngine::getReportCount() const
1974cdf0e10cSrcweir     {
1975cdf0e10cSrcweir         return m_pImpl->getReportCount();
1976cdf0e10cSrcweir     }
1977cdf0e10cSrcweir 
1978cdf0e10cSrcweir 	//--------------------------------------------------------------------
migrateAll()1979cdf0e10cSrcweir     bool MigrationEngine::migrateAll()
1980cdf0e10cSrcweir     {
1981cdf0e10cSrcweir         return m_pImpl->migrateAll();
1982cdf0e10cSrcweir     }
1983cdf0e10cSrcweir 
1984cdf0e10cSrcweir //........................................................................
1985cdf0e10cSrcweir } // namespace dbmm
1986cdf0e10cSrcweir //........................................................................
1987