LCOV - code coverage report
Current view: top level - oox/source/ole - vbaproject.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 184 219 84.0 %
Date: 2014-04-11 Functions: 25 32 78.1 %
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         116 : 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         116 :         Any aItem = ConfigurationHelper::readRelativeKey( rxConfigAccess, "Filter/Import/VBA", rItemName );
      74         116 :         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          29 : VbaFilterConfig::VbaFilterConfig( const Reference< XComponentContext >& rxContext, const OUString& rConfigCompName )
      87             : {
      88             :     OSL_ENSURE( rxContext.is(), "VbaFilterConfig::VbaFilterConfig - missing component context" );
      89          29 :     if( rxContext.is() ) try
      90             :     {
      91             :         OSL_ENSURE( !rConfigCompName.isEmpty(), "VbaFilterConfig::VbaFilterConfig - invalid configuration component name" );
      92          29 :         OUString aConfigPackage = "org.openoffice.Office." + rConfigCompName;
      93          29 :         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          29 : }
     100             : 
     101          29 : VbaFilterConfig::~VbaFilterConfig()
     102             : {
     103          29 : }
     104             : 
     105          29 : bool VbaFilterConfig::isImportVba() const
     106             : {
     107          29 :     return lclReadConfigItem( mxConfigAccess, "Load" );
     108             : }
     109             : 
     110          58 : bool VbaFilterConfig::isImportVbaExecutable() const
     111             : {
     112          58 :     return lclReadConfigItem( mxConfigAccess, "Executable" );
     113             : }
     114             : 
     115          29 : bool VbaFilterConfig::isExportVba() const
     116             : {
     117          29 :     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          29 : 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          29 :     maPrjName( "Standard" )
     151             : {
     152             :     OSL_ENSURE( mxContext.is(), "VbaProject::VbaProject - missing component context" );
     153             :     OSL_ENSURE( mxDocModel.is(), "VbaProject::VbaProject - missing document model" );
     154          29 : }
     155             : 
     156          29 : VbaProject::~VbaProject()
     157             : {
     158          29 : }
     159             : 
     160             : 
     161          29 : bool VbaProject::importVbaProject( StorageBase& rVbaPrjStrg )
     162             : {
     163             :    // create GraphicHelper
     164          29 :    Reference< ::com::sun::star::frame::XFrame > xFrame;
     165          29 :    if ( mxDocModel.is() )
     166             :    {
     167          29 :        Reference< ::com::sun::star::frame::XController > xController =  mxDocModel->getCurrentController();
     168          29 :        xFrame =  xController.is() ? xController->getFrame() : NULL;
     169             :    }
     170          58 :    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          58 :    GraphicHelper grfHlp( mxContext, xFrame, noStorage );
     175          29 :    importVbaProject( rVbaPrjStrg, grfHlp );
     176             :    // return true if something has been imported
     177          58 :    return hasModules() || hasDialogs();
     178             : }
     179             : 
     180          29 : void VbaProject::importVbaProject( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr )
     181             : {
     182          29 :     if( rVbaPrjStrg.isStorage() )
     183             :     {
     184             :         // load the code modules and forms
     185          29 :         if( isImportVba() )
     186          29 :             importVba( rVbaPrjStrg, rGraphicHelper, bDefaultColorBgr );
     187             :         // copy entire storage into model
     188          29 :         if( isExportVba() )
     189          29 :             copyStorage( rVbaPrjStrg );
     190             :     }
     191          29 : }
     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          29 : bool VbaProject::hasModules() const
     200             : {
     201          29 :     return mxBasicLib.is() && mxBasicLib->hasElements();
     202             : }
     203             : 
     204           1 : bool VbaProject::hasDialogs() const
     205             : {
     206           1 :     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          29 : void VbaProject::prepareImport()
     218             : {
     219          29 : }
     220             : 
     221          29 : void VbaProject::finalizeImport()
     222             : {
     223          29 : }
     224             : 
     225             : // private --------------------------------------------------------------------
     226             : 
     227          60 : Reference< XLibraryContainer > VbaProject::getLibraryContainer( sal_Int32 nPropId )
     228             : {
     229          60 :     PropertySet aDocProp( mxDocModel );
     230          60 :     Reference< XLibraryContainer > xLibContainer( aDocProp.getAnyProperty( nPropId ), UNO_QUERY );
     231          60 :     return xLibContainer;
     232             : }
     233             : 
     234          31 : Reference< XNameContainer > VbaProject::openLibrary( sal_Int32 nPropId, bool bCreateMissing )
     235             : {
     236          31 :     Reference< XNameContainer > xLibrary;
     237             :     try
     238             :     {
     239          32 :         Reference< XLibraryContainer > xLibContainer( getLibraryContainer( nPropId ), UNO_SET_THROW );
     240          30 :         if( bCreateMissing && !xLibContainer->hasByName( maPrjName ) )
     241          30 :             xLibContainer->createLibrary( maPrjName );
     242          30 :         xLibrary.set( xLibContainer->getByName( maPrjName ), UNO_QUERY_THROW );
     243             :     }
     244           1 :     catch(const Exception& )
     245             :     {
     246             :     }
     247             :     OSL_ENSURE( !bCreateMissing || xLibrary.is(), "VbaProject::openLibrary - cannot create library" );
     248          31 :     return xLibrary;
     249             : }
     250             : 
     251          29 : Reference< XNameContainer > VbaProject::createBasicLibrary()
     252             : {
     253          29 :     if( !mxBasicLib.is() )
     254          29 :         mxBasicLib = openLibrary( PROP_BasicLibraries, true );
     255          29 :     return mxBasicLib;
     256             : }
     257             : 
     258           2 : Reference< XNameContainer > VbaProject::createDialogLibrary()
     259             : {
     260           2 :     if( !mxDialogLib.is() )
     261           2 :         mxDialogLib = openLibrary( PROP_DialogLibraries, true );
     262           2 :     return mxDialogLib;
     263             : }
     264             : 
     265          29 : void VbaProject::importVba( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr )
     266             : {
     267          29 :     readVbaModules( rVbaPrjStrg );
     268          29 :     importModulesAndForms(rVbaPrjStrg, rGraphicHelper, bDefaultColorBgr );
     269          29 : }
     270             : 
     271          29 : void VbaProject::readVbaModules( StorageBase& rVbaPrjStrg )
     272             : {
     273          29 :     StorageRef xVbaStrg = rVbaPrjStrg.openSubStorage( "VBA", false );
     274             :     OSL_ENSURE( xVbaStrg.get(), "VbaProject::readVbaModules - cannot open 'VBA' substorage" );
     275          29 :     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          58 :     BinaryXInputStream aInStrm( xVbaStrg->openInputStream( "dir" ), true );
     283             :     // VbaInputStream implements decompression
     284          58 :     VbaInputStream aDirStrm( aInStrm );
     285             :     OSL_ENSURE( !aDirStrm.isEof(), "VbaProject::importVba - cannot open 'dir' stream" );
     286          29 :     if( aDirStrm.isEof() )
     287           0 :         return;
     288             : 
     289             :     // virtual call, derived classes may do some preparations
     290          29 :     prepareImport();
     291             : 
     292             :     // read all records of the directory
     293          29 :     rtl_TextEncoding eTextEnc = RTL_TEXTENCODING_MS_1252;
     294          29 :     sal_uInt16 nModuleCount = 0;
     295          29 :     bool bExecutable = isImportVbaExecutable();
     296             : 
     297             : 
     298          29 :     sal_uInt16 nRecId = 0;
     299          58 :     StreamDataSequence aRecData;
     300        1013 :     while( VbaHelper::readDirRecord( nRecId, aRecData, aDirStrm ) && (nRecId != VBA_ID_PROJECTEND) )
     301             :     {
     302             :         // create record stream object from imported record data
     303         955 :         SequenceInputStream aRecStrm( aRecData );
     304         955 :         sal_Int32 nRecSize = aRecData.getLength();
     305         955 :         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          29 :                 rtl_TextEncoding eNewTextEnc = rtl_getTextEncodingFromWindowsCodePage( aRecStrm.readuInt16() );
     313             :                 OSL_ENSURE( eNewTextEnc != RTL_TEXTENCODING_DONTKNOW, "VbaProject::importVba - unknown text encoding" );
     314          29 :                 if( eNewTextEnc != RTL_TEXTENCODING_DONTKNOW )
     315          29 :                     eTextEnc = eNewTextEnc;
     316             :             }
     317          29 :             break;
     318             :             case VBA_ID_PROJECTNAME:
     319             :             {
     320          29 :                 OUString aPrjName = aRecStrm.readCharArrayUC( nRecSize, eTextEnc );
     321             :                 OSL_ENSURE( !aPrjName.isEmpty(), "VbaProject::importVba - invalid project name" );
     322          29 :                 if( !aPrjName.isEmpty() )
     323          29 :                     maPrjName = aPrjName;
     324             :             }
     325          29 :             break;
     326             :             case VBA_ID_PROJECTMODULES:
     327             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 2 );
     328             :                 OSL_ENSURE( maModules.empty(), "VbaProject::importVba - unexpected PROJECTMODULES record" );
     329          29 :                 aRecStrm >> nModuleCount;
     330          29 :             break;
     331             :             case VBA_ID_MODULENAME:
     332             :             {
     333         147 :                 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         147 :                 VbaModuleMap::mapped_type& rxModule = maModules[ aName ];
     337         147 :                 rxModule.reset( new VbaModule( mxContext, mxDocModel, aName, eTextEnc, bExecutable ) );
     338             :                 // read all remaining records until the MODULEEND record
     339         147 :                 rxModule->importDirRecords( aDirStrm );
     340             :                 OSL_ENSURE( !maModulesByStrm.has( rxModule->getStreamName() ), "VbaProject::importVba - multiple modules with the same stream name" );
     341         147 :                 maModulesByStrm[ rxModule->getStreamName() ] = rxModule;
     342             :             }
     343         147 :             break;
     344             : #undef OOX_ENSURE_RECORDSIZE
     345             :         }
     346         955 :     }
     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          29 :     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          29 :     if( !aPrjStrm.isEof() )
     373             :     {
     374          29 :         TextInputStream aPrjTextStrm( mxContext, aPrjStrm, eTextEnc );
     375          58 :         OUString aKey, aValue;
     376          29 :         bool bExitLoop = false;
     377         471 :         while( !bExitLoop && !aPrjTextStrm.isEof() )
     378             :         {
     379             :             // read a text line from the stream
     380         413 :             OUString aLine = aPrjTextStrm.readLine().trim();
     381         413 :             sal_Int32 nLineLen = aLine.getLength();
     382             :             // exit if a subsection starts (section name is given in brackets)
     383         413 :             bExitLoop = (nLineLen >= 2) && (aLine[ 0 ] == '[') && (aLine[ nLineLen - 1 ] == ']');
     384         413 :             if( !bExitLoop && VbaHelper::extractKeyValue( aKey, aValue, aLine ) )
     385             :             {
     386         355 :                 sal_Int32 nType = ModuleType::UNKNOWN;
     387         355 :                 if( aKey.equalsIgnoreAsciiCase( "Document" ) )
     388             :                 {
     389         118 :                     nType = ModuleType::DOCUMENT;
     390             :                     // strip automation server version from module names
     391         118 :                     sal_Int32 nSlashPos = aValue.indexOf( '/' );
     392         118 :                     if( nSlashPos >= 0 )
     393         118 :                         aValue = aValue.copy( 0, nSlashPos );
     394             :                 }
     395         237 :                 else if( aKey.equalsIgnoreAsciiCase( "Module" ) )
     396          27 :                     nType = ModuleType::NORMAL;
     397         210 :                 else if( aKey.equalsIgnoreAsciiCase( "Class" ) )
     398           0 :                     nType = ModuleType::CLASS;
     399         210 :                 else if( aKey.equalsIgnoreAsciiCase( "BaseClass" ) )
     400           2 :                     nType = ModuleType::FORM;
     401             : 
     402         355 :                 if( (nType != ModuleType::UNKNOWN) && !aValue.isEmpty() )
     403             :                 {
     404             :                     OSL_ENSURE( maModules.has( aValue ), "VbaProject::importVba - module not found" );
     405         147 :                     if( VbaModule* pModule = maModules.get( aValue ).get() )
     406         146 :                         pModule->setType( nType );
     407             :                 }
     408             :             }
     409         442 :         }
     410             :     }
     411          29 :     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          30 :             Reference< XVBACompatibility > xVBACompat( getLibraryContainer( PROP_BasicLibraries ), UNO_QUERY_THROW );
     419          28 :             xVBACompat->setVBACompatibilityMode( sal_True );
     420          28 :             xVBACompat->setProjectName( maPrjName );
     421             : 
     422             :         }
     423           1 :         catch(const Exception& )
     424             :         {
     425             :         }
     426             :     }
     427           0 :     catch(const Exception& )
     428             :     {
     429          58 :     }
     430             : }
     431             : 
     432          29 : void VbaProject::importModulesAndForms( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr )
     433             : {
     434          29 :     StorageRef xVbaStrg = rVbaPrjStrg.openSubStorage( "VBA", false );
     435             :     OSL_ENSURE( xVbaStrg.get(), "VbaProject::importModulesAndForms - cannot open 'VBA' substorage" );
     436          29 :     if( !xVbaStrg )
     437          29 :         return;
     438          29 :     rtl_TextEncoding eTextEnc = RTL_TEXTENCODING_MS_1252;
     439          29 :     bool bExecutable = isImportVbaExecutable();
     440             : 
     441             :     // create empty dummy modules
     442          58 :     VbaModuleMap aDummyModules;
     443          29 :     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          29 :     if( !maModules.empty() || !aDummyModules.empty() ) try
     456             :     {
     457             :         // get the model factory and the basic library
     458          29 :         Reference< XMultiServiceFactory > xModelFactory( mxDocModel, UNO_QUERY_THROW );
     459          58 :         Reference< XNameContainer > xBasicLib( createBasicLibrary(), UNO_SET_THROW );
     460             : 
     461             :         // try to get access to document objects related to code modules
     462          56 :         Reference< XNameAccess > xDocObjectNA;
     463             :         try
     464             :         {
     465          28 :             xDocObjectNA.set( xModelFactory->createInstance( "ooo.vba.VBAObjectModuleObjectProvider" ), UNO_QUERY );
     466             :         }
     467           3 :         catch(const Exception& )
     468             :         {
     469             :             // not all documents support this
     470             :         }
     471             : 
     472          28 :         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          28 :                 ::boost::ref( *xVbaStrg ), ::boost::cref( xBasicLib ),
     478          56 :                 ::boost::cref( xDocObjectNA ) );
     479             : 
     480             :             // create empty dummy modules
     481             :             aDummyModules.forEachMem( &VbaModule::createEmptyModule,
     482          28 :                 ::boost::cref( xBasicLib ), ::boost::cref( xDocObjectNA ) );
     483          29 :         }
     484             :     }
     485           1 :     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          58 :     ::std::vector< OUString > aElements;
     494          29 :     rVbaPrjStrg.getElementNames( aElements );
     495         118 :     for( ::std::vector< OUString >::iterator aIt = aElements.begin(), aEnd = aElements.end(); aIt != aEnd; ++aIt )
     496             :     {
     497             :         // try to open the element as storage
     498          89 :         if( *aIt != "VBA" )
     499             :         {
     500          60 :             StorageRef xSubStrg = rVbaPrjStrg.openSubStorage( *aIt, false );
     501          60 :             if( xSubStrg.get() ) try
     502             :             {
     503             :                 // resolve module name from storage name (which equals the module stream name)
     504           2 :                 VbaModule* pModule = maModulesByStrm.get( *aIt ).get();
     505             :                 OSL_ENSURE( pModule && (pModule->getType() == ModuleType::FORM),
     506             :                     "VbaProject::importVba - form substorage without form module" );
     507           2 :                 OUString aModuleName;
     508           2 :                 if( pModule )
     509           2 :                     aModuleName = pModule->getName();
     510             : 
     511             :                 // create and import the form
     512           4 :                 Reference< XNameContainer > xDialogLib( createDialogLibrary(), UNO_SET_THROW );
     513           4 :                 VbaUserForm aForm( mxContext, mxDocModel, rGraphicHelper, bDefaultColorBgr );
     514           4 :                 aForm.importForm( xDialogLib, *xSubStrg, aModuleName, eTextEnc );
     515             :             }
     516           0 :             catch(const Exception& )
     517             :             {
     518          60 :             }
     519             :         }
     520             :     }
     521             : 
     522             :     // attach macros to registered objects
     523          29 :     attachMacros();
     524             :     // virtual call, derived classes may do some more processing
     525          58 :     finalizeImport();
     526             : }
     527             : 
     528          29 : void VbaProject::attachMacros()
     529             : {
     530          29 :     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          29 : }
     544             : 
     545          29 : void VbaProject::copyStorage( StorageBase& rVbaPrjStrg )
     546             : {
     547          29 :     if( mxContext.is() ) try
     548             :     {
     549          29 :         Reference< XStorageBasedDocument > xStorageBasedDoc( mxDocModel, UNO_QUERY_THROW );
     550          58 :         Reference< XStorage > xDocStorage( xStorageBasedDoc->getDocumentStorage(), UNO_QUERY_THROW );
     551             :         {
     552          29 :             const sal_Int32 nOpenMode = ElementModes::SEEKABLE | ElementModes::WRITE | ElementModes::TRUNCATE;
     553          29 :             Reference< XStream > xDocStream( xDocStorage->openStreamElement( "_MS_VBA_Macros", nOpenMode ), UNO_SET_THROW );
     554          58 :             OleStorage aDestStorage( mxContext, xDocStream, false );
     555          29 :             rVbaPrjStrg.copyStorageToStorage( aDestStorage );
     556          58 :             aDestStorage.commit();
     557             :         }
     558          58 :         Reference< XTransactedObject >( xDocStorage, UNO_QUERY_THROW )->commit();
     559             :     }
     560           0 :     catch(const Exception& )
     561             :     {
     562             :     }
     563          29 : }
     564             : 
     565             : 
     566             : 
     567             : } // namespace ole
     568         177 : } // namespace oox
     569             : 
     570             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10