LCOV - code coverage report
Current view: top level - oox/source/ole - vbaproject.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 219 0.0 %
Date: 2014-04-14 Functions: 0 32 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "oox/ole/vbaproject.hxx"
      21             : 
      22             : #include <com/sun/star/document/XStorageBasedDocument.hpp>
      23             : #include <com/sun/star/embed/ElementModes.hpp>
      24             : #include <com/sun/star/embed/XTransactedObject.hpp>
      25             : #include <com/sun/star/frame/XModel.hpp>
      26             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      27             : #include <com/sun/star/script/ModuleType.hpp>
      28             : #include <com/sun/star/script/XLibraryContainer.hpp>
      29             : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
      30             : #include <com/sun/star/script/vba/XVBAMacroResolver.hpp>
      31             : #include <com/sun/star/uno/XComponentContext.hpp>
      32             : #include <comphelper/configurationhelper.hxx>
      33             : #include <comphelper/string.hxx>
      34             : #include <rtl/tencinfo.h>
      35             : #include <rtl/ustrbuf.h>
      36             : #include "oox/helper/binaryinputstream.hxx"
      37             : #include "oox/helper/containerhelper.hxx"
      38             : #include "oox/helper/propertyset.hxx"
      39             : #include "oox/helper/textinputstream.hxx"
      40             : #include "oox/ole/olestorage.hxx"
      41             : #include "oox/ole/vbacontrol.hxx"
      42             : #include "oox/ole/vbahelper.hxx"
      43             : #include "oox/ole/vbainputstream.hxx"
      44             : #include "oox/ole/vbamodule.hxx"
      45             : #include "oox/token/properties.hxx"
      46             : 
      47             : namespace oox {
      48             : namespace ole {
      49             : 
      50             : 
      51             : 
      52             : using namespace ::com::sun::star::container;
      53             : using namespace ::com::sun::star::document;
      54             : using namespace ::com::sun::star::embed;
      55             : using namespace ::com::sun::star::frame;
      56             : using namespace ::com::sun::star::io;
      57             : using namespace ::com::sun::star::lang;
      58             : using namespace ::com::sun::star::script;
      59             : using namespace ::com::sun::star::script::vba;
      60             : using namespace ::com::sun::star::uno;
      61             : 
      62             : using ::comphelper::ConfigurationHelper;
      63             : 
      64             : 
      65             : 
      66             : namespace {
      67             : 
      68           0 : bool lclReadConfigItem( const Reference< XInterface >& rxConfigAccess, const OUString& rItemName )
      69             : {
      70             :     // some applications do not support all configuration items, assume 'false' in this case
      71             :     try
      72             :     {
      73           0 :         Any aItem = ConfigurationHelper::readRelativeKey( rxConfigAccess, "Filter/Import/VBA", rItemName );
      74           0 :         return aItem.has< bool >() && aItem.get< bool >();
      75             :     }
      76           0 :     catch(const Exception& )
      77             :     {
      78             :     }
      79           0 :     return false;
      80             : }
      81             : 
      82             : } // namespace
      83             : 
      84             : 
      85             : 
      86           0 : VbaFilterConfig::VbaFilterConfig( const Reference< XComponentContext >& rxContext, const OUString& rConfigCompName )
      87             : {
      88             :     OSL_ENSURE( rxContext.is(), "VbaFilterConfig::VbaFilterConfig - missing component context" );
      89           0 :     if( rxContext.is() ) try
      90             :     {
      91             :         OSL_ENSURE( !rConfigCompName.isEmpty(), "VbaFilterConfig::VbaFilterConfig - invalid configuration component name" );
      92           0 :         OUString aConfigPackage = "org.openoffice.Office." + rConfigCompName;
      93           0 :         mxConfigAccess = ConfigurationHelper::openConfig( rxContext, aConfigPackage, ConfigurationHelper::E_READONLY );
      94             :     }
      95           0 :     catch(const Exception& )
      96             :     {
      97             :     }
      98             :     OSL_ENSURE( mxConfigAccess.is(), "VbaFilterConfig::VbaFilterConfig - cannot open configuration" );
      99           0 : }
     100             : 
     101           0 : VbaFilterConfig::~VbaFilterConfig()
     102             : {
     103           0 : }
     104             : 
     105           0 : bool VbaFilterConfig::isImportVba() const
     106             : {
     107           0 :     return lclReadConfigItem( mxConfigAccess, "Load" );
     108             : }
     109             : 
     110           0 : bool VbaFilterConfig::isImportVbaExecutable() const
     111             : {
     112           0 :     return lclReadConfigItem( mxConfigAccess, "Executable" );
     113             : }
     114             : 
     115           0 : bool VbaFilterConfig::isExportVba() const
     116             : {
     117           0 :     return lclReadConfigItem( mxConfigAccess, "Save" );
     118             : }
     119             : 
     120             : 
     121             : 
     122           0 : VbaMacroAttacherBase::VbaMacroAttacherBase( const OUString& rMacroName ) :
     123           0 :     maMacroName( rMacroName )
     124             : {
     125             :     OSL_ENSURE( !maMacroName.isEmpty(), "VbaMacroAttacherBase::VbaMacroAttacherBase - empty macro name" );
     126           0 : }
     127             : 
     128           0 : VbaMacroAttacherBase::~VbaMacroAttacherBase()
     129             : {
     130           0 : }
     131             : 
     132           0 : void VbaMacroAttacherBase::resolveAndAttachMacro( const Reference< XVBAMacroResolver >& rxResolver )
     133             : {
     134             :     try
     135             :     {
     136           0 :         attachMacro( rxResolver->resolveVBAMacroToScriptURL( maMacroName ) );
     137             :     }
     138           0 :     catch(const Exception& )
     139             :     {
     140             :     }
     141           0 : }
     142             : 
     143             : 
     144             : 
     145           0 : VbaProject::VbaProject( const Reference< XComponentContext >& rxContext,
     146             :         const Reference< XModel >& rxDocModel, const OUString& rConfigCompName ) :
     147             :     VbaFilterConfig( rxContext, rConfigCompName ),
     148             :     mxContext( rxContext ),
     149             :     mxDocModel( rxDocModel ),
     150           0 :     maPrjName( "Standard" )
     151             : {
     152             :     OSL_ENSURE( mxContext.is(), "VbaProject::VbaProject - missing component context" );
     153             :     OSL_ENSURE( mxDocModel.is(), "VbaProject::VbaProject - missing document model" );
     154           0 : }
     155             : 
     156           0 : VbaProject::~VbaProject()
     157             : {
     158           0 : }
     159             : 
     160             : 
     161           0 : bool VbaProject::importVbaProject( StorageBase& rVbaPrjStrg )
     162             : {
     163             :    // create GraphicHelper
     164           0 :    Reference< ::com::sun::star::frame::XFrame > xFrame;
     165           0 :    if ( mxDocModel.is() )
     166             :    {
     167           0 :        Reference< ::com::sun::star::frame::XController > xController =  mxDocModel->getCurrentController();
     168           0 :        xFrame =  xController.is() ? xController->getFrame() : NULL;
     169             :    }
     170           0 :    StorageRef noStorage;
     171             :    // if the GraphicHelper tries to use noStorage it will of course crash
     172             :    // but.. this shouldn't happen as there is no reason for GraphicHelper
     173             :    // to do that when importing VBA projects
     174           0 :    GraphicHelper grfHlp( mxContext, xFrame, noStorage );
     175           0 :    importVbaProject( rVbaPrjStrg, grfHlp );
     176             :    // return true if something has been imported
     177           0 :    return hasModules() || hasDialogs();
     178             : }
     179             : 
     180           0 : void VbaProject::importVbaProject( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr )
     181             : {
     182           0 :     if( rVbaPrjStrg.isStorage() )
     183             :     {
     184             :         // load the code modules and forms
     185           0 :         if( isImportVba() )
     186           0 :             importVba( rVbaPrjStrg, rGraphicHelper, bDefaultColorBgr );
     187             :         // copy entire storage into model
     188           0 :         if( isExportVba() )
     189           0 :             copyStorage( rVbaPrjStrg );
     190             :     }
     191           0 : }
     192             : 
     193           0 : void VbaProject::registerMacroAttacher( const VbaMacroAttacherRef& rxAttacher )
     194             : {
     195             :     OSL_ENSURE( rxAttacher.get(), "VbaProject::registerMacroAttacher - unexpected empty reference" );
     196           0 :     maMacroAttachers.push_back( rxAttacher );
     197           0 : }
     198             : 
     199           0 : bool VbaProject::hasModules() const
     200             : {
     201           0 :     return mxBasicLib.is() && mxBasicLib->hasElements();
     202             : }
     203             : 
     204           0 : bool VbaProject::hasDialogs() const
     205             : {
     206           0 :     return mxDialogLib.is() && mxDialogLib->hasElements();
     207             : }
     208             : 
     209             : // protected ------------------------------------------------------------------
     210             : 
     211           0 : void VbaProject::addDummyModule( const OUString& rName, sal_Int32 nType )
     212             : {
     213             :     OSL_ENSURE( !rName.isEmpty(), "VbaProject::addDummyModule - missing module name" );
     214           0 :     maDummyModules[ rName ] = nType;
     215           0 : }
     216             : 
     217           0 : void VbaProject::prepareImport()
     218             : {
     219           0 : }
     220             : 
     221           0 : void VbaProject::finalizeImport()
     222             : {
     223           0 : }
     224             : 
     225             : // private --------------------------------------------------------------------
     226             : 
     227           0 : Reference< XLibraryContainer > VbaProject::getLibraryContainer( sal_Int32 nPropId )
     228             : {
     229           0 :     PropertySet aDocProp( mxDocModel );
     230           0 :     Reference< XLibraryContainer > xLibContainer( aDocProp.getAnyProperty( nPropId ), UNO_QUERY );
     231           0 :     return xLibContainer;
     232             : }
     233             : 
     234           0 : Reference< XNameContainer > VbaProject::openLibrary( sal_Int32 nPropId, bool bCreateMissing )
     235             : {
     236           0 :     Reference< XNameContainer > xLibrary;
     237             :     try
     238             :     {
     239           0 :         Reference< XLibraryContainer > xLibContainer( getLibraryContainer( nPropId ), UNO_SET_THROW );
     240           0 :         if( bCreateMissing && !xLibContainer->hasByName( maPrjName ) )
     241           0 :             xLibContainer->createLibrary( maPrjName );
     242           0 :         xLibrary.set( xLibContainer->getByName( maPrjName ), UNO_QUERY_THROW );
     243             :     }
     244           0 :     catch(const Exception& )
     245             :     {
     246             :     }
     247             :     OSL_ENSURE( !bCreateMissing || xLibrary.is(), "VbaProject::openLibrary - cannot create library" );
     248           0 :     return xLibrary;
     249             : }
     250             : 
     251           0 : Reference< XNameContainer > VbaProject::createBasicLibrary()
     252             : {
     253           0 :     if( !mxBasicLib.is() )
     254           0 :         mxBasicLib = openLibrary( PROP_BasicLibraries, true );
     255           0 :     return mxBasicLib;
     256             : }
     257             : 
     258           0 : Reference< XNameContainer > VbaProject::createDialogLibrary()
     259             : {
     260           0 :     if( !mxDialogLib.is() )
     261           0 :         mxDialogLib = openLibrary( PROP_DialogLibraries, true );
     262           0 :     return mxDialogLib;
     263             : }
     264             : 
     265           0 : void VbaProject::importVba( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr )
     266             : {
     267           0 :     readVbaModules( rVbaPrjStrg );
     268           0 :     importModulesAndForms(rVbaPrjStrg, rGraphicHelper, bDefaultColorBgr );
     269           0 : }
     270             : 
     271           0 : void VbaProject::readVbaModules( StorageBase& rVbaPrjStrg )
     272             : {
     273           0 :     StorageRef xVbaStrg = rVbaPrjStrg.openSubStorage( "VBA", false );
     274             :     OSL_ENSURE( xVbaStrg.get(), "VbaProject::readVbaModules - cannot open 'VBA' substorage" );
     275           0 :     if( !xVbaStrg )
     276           0 :         return;
     277             : 
     278             :     /*  Read the 'VBA/dir' stream which contains general settings of the VBA
     279             :         project such as the text encoding used throughout several streams, and
     280             :         a list of all code modules.
     281             :      */
     282           0 :     BinaryXInputStream aInStrm( xVbaStrg->openInputStream( "dir" ), true );
     283             :     // VbaInputStream implements decompression
     284           0 :     VbaInputStream aDirStrm( aInStrm );
     285             :     OSL_ENSURE( !aDirStrm.isEof(), "VbaProject::importVba - cannot open 'dir' stream" );
     286           0 :     if( aDirStrm.isEof() )
     287           0 :         return;
     288             : 
     289             :     // virtual call, derived classes may do some preparations
     290           0 :     prepareImport();
     291             : 
     292             :     // read all records of the directory
     293           0 :     rtl_TextEncoding eTextEnc = RTL_TEXTENCODING_MS_1252;
     294           0 :     sal_uInt16 nModuleCount = 0;
     295           0 :     bool bExecutable = isImportVbaExecutable();
     296             : 
     297             : 
     298           0 :     sal_uInt16 nRecId = 0;
     299           0 :     StreamDataSequence aRecData;
     300           0 :     while( VbaHelper::readDirRecord( nRecId, aRecData, aDirStrm ) && (nRecId != VBA_ID_PROJECTEND) )
     301             :     {
     302             :         // create record stream object from imported record data
     303           0 :         SequenceInputStream aRecStrm( aRecData );
     304           0 :         sal_Int32 nRecSize = aRecData.getLength();
     305           0 :         switch( nRecId )
     306             :         {
     307             : #define OOX_ENSURE_RECORDSIZE( cond ) OSL_ENSURE( cond, "VbaProject::importVba - invalid record size" )
     308             :             case VBA_ID_PROJECTCODEPAGE:
     309             :             {
     310             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 2 );
     311             :                 OSL_ENSURE( maModules.empty(), "VbaProject::importVba - unexpected PROJECTCODEPAGE record" );
     312           0 :                 rtl_TextEncoding eNewTextEnc = rtl_getTextEncodingFromWindowsCodePage( aRecStrm.readuInt16() );
     313             :                 OSL_ENSURE( eNewTextEnc != RTL_TEXTENCODING_DONTKNOW, "VbaProject::importVba - unknown text encoding" );
     314           0 :                 if( eNewTextEnc != RTL_TEXTENCODING_DONTKNOW )
     315           0 :                     eTextEnc = eNewTextEnc;
     316             :             }
     317           0 :             break;
     318             :             case VBA_ID_PROJECTNAME:
     319             :             {
     320           0 :                 OUString aPrjName = aRecStrm.readCharArrayUC( nRecSize, eTextEnc );
     321             :                 OSL_ENSURE( !aPrjName.isEmpty(), "VbaProject::importVba - invalid project name" );
     322           0 :                 if( !aPrjName.isEmpty() )
     323           0 :                     maPrjName = aPrjName;
     324             :             }
     325           0 :             break;
     326             :             case VBA_ID_PROJECTMODULES:
     327             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 2 );
     328             :                 OSL_ENSURE( maModules.empty(), "VbaProject::importVba - unexpected PROJECTMODULES record" );
     329           0 :                 aRecStrm >> nModuleCount;
     330           0 :             break;
     331             :             case VBA_ID_MODULENAME:
     332             :             {
     333           0 :                 OUString aName = aRecStrm.readCharArrayUC( nRecSize, eTextEnc );
     334             :                 OSL_ENSURE( !aName.isEmpty(), "VbaProject::importVba - invalid module name" );
     335             :                 OSL_ENSURE( !maModules.has( aName ), "VbaProject::importVba - multiple modules with the same name" );
     336           0 :                 VbaModuleMap::mapped_type& rxModule = maModules[ aName ];
     337           0 :                 rxModule.reset( new VbaModule( mxContext, mxDocModel, aName, eTextEnc, bExecutable ) );
     338             :                 // read all remaining records until the MODULEEND record
     339           0 :                 rxModule->importDirRecords( aDirStrm );
     340             :                 OSL_ENSURE( !maModulesByStrm.has( rxModule->getStreamName() ), "VbaProject::importVba - multiple modules with the same stream name" );
     341           0 :                 maModulesByStrm[ rxModule->getStreamName() ] = rxModule;
     342             :             }
     343           0 :             break;
     344             : #undef OOX_ENSURE_RECORDSIZE
     345             :         }
     346           0 :     }
     347             :     OSL_ENSURE( nModuleCount == maModules.size(), "VbaProject::importVba - invalid module count" );
     348             : 
     349             :     /*  The directory does not contain the real type of the modules, it
     350             :         distinguishes only between 'procedural' and 'document' (the latter
     351             :         includes class and form modules). Now, the exact type of all modules
     352             :         will be read from the 'PROJECT' stream. It consists of text lines in
     353             :         'key=value' format which list the code modules by type.
     354             : 
     355             :         -   The line 'document=<modulename>/&HXXXXXXXX' declares document
     356             :             modules. These are attached to the Word document (usually called
     357             :             'ThisDocument'), the Excel workbook (usually called
     358             :             'ThisWorkbook'), or single Excel worksheets or chartsheets (usually
     359             :             called 'SheetX' or 'ChartX', X being a decimal number). Of course,
     360             :             users may rename all these modules. The slash character separates
     361             :             an automation server version number (hexadecimal 'XXXXXXXX') from
     362             :             the module name.
     363             :         -   The line 'Module=<modulename>' declares common procedural code
     364             :             modules.
     365             :         -   The line 'Class=<modulename>' declares a class module.
     366             :         -   The line 'BaseClass=<modulename>' declares a code module attached
     367             :             to a user form with the same name.
     368             :      */
     369           0 :     BinaryXInputStream aPrjStrm( rVbaPrjStrg.openInputStream( "PROJECT" ), true );
     370             :     OSL_ENSURE( !aPrjStrm.isEof(), "VbaProject::importVba - cannot open 'PROJECT' stream" );
     371             :     // do not exit if this stream does not exist, but proceed to load the modules below
     372           0 :     if( !aPrjStrm.isEof() )
     373             :     {
     374           0 :         TextInputStream aPrjTextStrm( mxContext, aPrjStrm, eTextEnc );
     375           0 :         OUString aKey, aValue;
     376           0 :         bool bExitLoop = false;
     377           0 :         while( !bExitLoop && !aPrjTextStrm.isEof() )
     378             :         {
     379             :             // read a text line from the stream
     380           0 :             OUString aLine = aPrjTextStrm.readLine().trim();
     381           0 :             sal_Int32 nLineLen = aLine.getLength();
     382             :             // exit if a subsection starts (section name is given in brackets)
     383           0 :             bExitLoop = (nLineLen >= 2) && (aLine[ 0 ] == '[') && (aLine[ nLineLen - 1 ] == ']');
     384           0 :             if( !bExitLoop && VbaHelper::extractKeyValue( aKey, aValue, aLine ) )
     385             :             {
     386           0 :                 sal_Int32 nType = ModuleType::UNKNOWN;
     387           0 :                 if( aKey.equalsIgnoreAsciiCase( "Document" ) )
     388             :                 {
     389           0 :                     nType = ModuleType::DOCUMENT;
     390             :                     // strip automation server version from module names
     391           0 :                     sal_Int32 nSlashPos = aValue.indexOf( '/' );
     392           0 :                     if( nSlashPos >= 0 )
     393           0 :                         aValue = aValue.copy( 0, nSlashPos );
     394             :                 }
     395           0 :                 else if( aKey.equalsIgnoreAsciiCase( "Module" ) )
     396           0 :                     nType = ModuleType::NORMAL;
     397           0 :                 else if( aKey.equalsIgnoreAsciiCase( "Class" ) )
     398           0 :                     nType = ModuleType::CLASS;
     399           0 :                 else if( aKey.equalsIgnoreAsciiCase( "BaseClass" ) )
     400           0 :                     nType = ModuleType::FORM;
     401             : 
     402           0 :                 if( (nType != ModuleType::UNKNOWN) && !aValue.isEmpty() )
     403             :                 {
     404             :                     OSL_ENSURE( maModules.has( aValue ), "VbaProject::importVba - module not found" );
     405           0 :                     if( VbaModule* pModule = maModules.get( aValue ).get() )
     406           0 :                         pModule->setType( nType );
     407             :                 }
     408             :             }
     409           0 :         }
     410             :     }
     411           0 :     if( !maModules.empty() ) try
     412             :     {
     413             :         /*  Set library container to VBA compatibility mode. This will create
     414             :             the VBA Globals object and store it in the Basic manager of the
     415             :             document. */
     416             :         try
     417             :         {
     418           0 :             Reference< XVBACompatibility > xVBACompat( getLibraryContainer( PROP_BasicLibraries ), UNO_QUERY_THROW );
     419           0 :             xVBACompat->setVBACompatibilityMode( sal_True );
     420           0 :             xVBACompat->setProjectName( maPrjName );
     421             : 
     422             :         }
     423           0 :         catch(const Exception& )
     424             :         {
     425             :         }
     426             :     }
     427           0 :     catch(const Exception& )
     428             :     {
     429           0 :     }
     430             : }
     431             : 
     432           0 : void VbaProject::importModulesAndForms( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr )
     433             : {
     434           0 :     StorageRef xVbaStrg = rVbaPrjStrg.openSubStorage( "VBA", false );
     435             :     OSL_ENSURE( xVbaStrg.get(), "VbaProject::importModulesAndForms - cannot open 'VBA' substorage" );
     436           0 :     if( !xVbaStrg )
     437           0 :         return;
     438           0 :     rtl_TextEncoding eTextEnc = RTL_TEXTENCODING_MS_1252;
     439           0 :     bool bExecutable = isImportVbaExecutable();
     440             : 
     441             :     // create empty dummy modules
     442           0 :     VbaModuleMap aDummyModules;
     443           0 :     for( DummyModuleMap::iterator aIt = maDummyModules.begin(), aEnd = maDummyModules.end(); aIt != aEnd; ++aIt )
     444             :     {
     445             :         OSL_ENSURE( !maModules.has( aIt->first ) && !aDummyModules.has( aIt->first ), "VbaProject::importVba - multiple modules with the same name" );
     446           0 :         VbaModuleMap::mapped_type& rxModule = aDummyModules[ aIt->first ];
     447           0 :         rxModule.reset( new VbaModule( mxContext, mxDocModel, aIt->first, eTextEnc, bExecutable ) );
     448           0 :         rxModule->setType( aIt->second );
     449             :     }
     450             : 
     451             :     /*  Now it is time to load the source code. All modules will be inserted
     452             :         into the Basic library of the document specified by the 'maPrjName'
     453             :         member. Do not create the Basic library, if there are no modules
     454             :         specified. */
     455           0 :     if( !maModules.empty() || !aDummyModules.empty() ) try
     456             :     {
     457             :         // get the model factory and the basic library
     458           0 :         Reference< XMultiServiceFactory > xModelFactory( mxDocModel, UNO_QUERY_THROW );
     459           0 :         Reference< XNameContainer > xBasicLib( createBasicLibrary(), UNO_SET_THROW );
     460             : 
     461             :         // try to get access to document objects related to code modules
     462           0 :         Reference< XNameAccess > xDocObjectNA;
     463             :         try
     464             :         {
     465           0 :             xDocObjectNA.set( xModelFactory->createInstance( "ooo.vba.VBAObjectModuleObjectProvider" ), UNO_QUERY );
     466             :         }
     467           0 :         catch(const Exception& )
     468             :         {
     469             :             // not all documents support this
     470             :         }
     471             : 
     472           0 :         if( xBasicLib.is() )
     473             :         {
     474             :             // #TODO cater for mxOleOverridesSink, like I used to before
     475             :             // call Basic source code import for each module, boost::[c]ref enforces pass-by-ref
     476             :             maModules.forEachMem( &VbaModule::createAndImportModule,
     477           0 :                 ::boost::ref( *xVbaStrg ), ::boost::cref( xBasicLib ),
     478           0 :                 ::boost::cref( xDocObjectNA ) );
     479             : 
     480             :             // create empty dummy modules
     481             :             aDummyModules.forEachMem( &VbaModule::createEmptyModule,
     482           0 :                 ::boost::cref( xBasicLib ), ::boost::cref( xDocObjectNA ) );
     483           0 :         }
     484             :     }
     485           0 :     catch(const Exception& )
     486             :     {
     487             :     }
     488             : 
     489             :     /*  Load the forms. The file format specification requires that a module
     490             :         must exist for every form. We are a bit more tolerant and scan the
     491             :         project storage for all form substorages. This may 'repair' broken VBA
     492             :         storages that misses to mention a module for an existing form. */
     493           0 :     ::std::vector< OUString > aElements;
     494           0 :     rVbaPrjStrg.getElementNames( aElements );
     495           0 :     for( ::std::vector< OUString >::iterator aIt = aElements.begin(), aEnd = aElements.end(); aIt != aEnd; ++aIt )
     496             :     {
     497             :         // try to open the element as storage
     498           0 :         if( *aIt != "VBA" )
     499             :         {
     500           0 :             StorageRef xSubStrg = rVbaPrjStrg.openSubStorage( *aIt, false );
     501           0 :             if( xSubStrg.get() ) try
     502             :             {
     503             :                 // resolve module name from storage name (which equals the module stream name)
     504           0 :                 VbaModule* pModule = maModulesByStrm.get( *aIt ).get();
     505             :                 OSL_ENSURE( pModule && (pModule->getType() == ModuleType::FORM),
     506             :                     "VbaProject::importVba - form substorage without form module" );
     507           0 :                 OUString aModuleName;
     508           0 :                 if( pModule )
     509           0 :                     aModuleName = pModule->getName();
     510             : 
     511             :                 // create and import the form
     512           0 :                 Reference< XNameContainer > xDialogLib( createDialogLibrary(), UNO_SET_THROW );
     513           0 :                 VbaUserForm aForm( mxContext, mxDocModel, rGraphicHelper, bDefaultColorBgr );
     514           0 :                 aForm.importForm( xDialogLib, *xSubStrg, aModuleName, eTextEnc );
     515             :             }
     516           0 :             catch(const Exception& )
     517             :             {
     518           0 :             }
     519             :         }
     520             :     }
     521             : 
     522             :     // attach macros to registered objects
     523           0 :     attachMacros();
     524             :     // virtual call, derived classes may do some more processing
     525           0 :     finalizeImport();
     526             : }
     527             : 
     528           0 : void VbaProject::attachMacros()
     529             : {
     530           0 :     if( !maMacroAttachers.empty() && mxContext.is() ) try
     531             :     {
     532           0 :         Reference< XMultiComponentFactory > xFactory( mxContext->getServiceManager(), UNO_SET_THROW );
     533           0 :         Sequence< Any > aArgs( 2 );
     534           0 :         aArgs[ 0 ] <<= mxDocModel;
     535           0 :         aArgs[ 1 ] <<= maPrjName;
     536           0 :         Reference< XVBAMacroResolver > xResolver( xFactory->createInstanceWithArgumentsAndContext(
     537           0 :             "com.sun.star.script.vba.VBAMacroResolver", aArgs, mxContext ), UNO_QUERY_THROW );
     538           0 :         maMacroAttachers.forEachMem( &VbaMacroAttacherBase::resolveAndAttachMacro, ::boost::cref( xResolver ) );
     539             :     }
     540           0 :     catch(const Exception& )
     541             :     {
     542             :     }
     543           0 : }
     544             : 
     545           0 : void VbaProject::copyStorage( StorageBase& rVbaPrjStrg )
     546             : {
     547           0 :     if( mxContext.is() ) try
     548             :     {
     549           0 :         Reference< XStorageBasedDocument > xStorageBasedDoc( mxDocModel, UNO_QUERY_THROW );
     550           0 :         Reference< XStorage > xDocStorage( xStorageBasedDoc->getDocumentStorage(), UNO_QUERY_THROW );
     551             :         {
     552           0 :             const sal_Int32 nOpenMode = ElementModes::SEEKABLE | ElementModes::WRITE | ElementModes::TRUNCATE;
     553           0 :             Reference< XStream > xDocStream( xDocStorage->openStreamElement( "_MS_VBA_Macros", nOpenMode ), UNO_SET_THROW );
     554           0 :             OleStorage aDestStorage( mxContext, xDocStream, false );
     555           0 :             rVbaPrjStrg.copyStorageToStorage( aDestStorage );
     556           0 :             aDestStorage.commit();
     557             :         }
     558           0 :         Reference< XTransactedObject >( xDocStorage, UNO_QUERY_THROW )->commit();
     559             :     }
     560           0 :     catch(const Exception& )
     561             :     {
     562             :     }
     563           0 : }
     564             : 
     565             : 
     566             : 
     567             : } // namespace ole
     568           0 : } // namespace oox
     569             : 
     570             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10