LCOV - code coverage report
Current view: top level - basctl/source/basicide - basobj2.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 211 0.0 %
Date: 2014-04-11 Functions: 0 15 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 "basobj.hxx"
      21             : #include "iderdll.hxx"
      22             : #include "iderdll2.hxx"
      23             : #include "iderid.hxx"
      24             : #include "macrodlg.hxx"
      25             : #include "moduldlg.hxx"
      26             : #include "basidesh.hxx"
      27             : #include "basidesh.hrc"
      28             : #include "baside2.hxx"
      29             : #include "basicmod.hxx"
      30             : #include "basdoc.hxx"
      31             : 
      32             : #include <com/sun/star/document/XEmbeddedScripts.hpp>
      33             : #include <com/sun/star/document/XScriptInvocationContext.hpp>
      34             : 
      35             : #include <basic/sbmeth.hxx>
      36             : #include <basic/sbx.hxx>
      37             : #include <boost/scoped_ptr.hpp>
      38             : #include <framework/documentundoguard.hxx>
      39             : #include <tools/diagnose_ex.h>
      40             : #include <unotools/moduleoptions.hxx>
      41             : #include <vcl/msgbox.hxx>
      42             : 
      43             : #include <vector>
      44             : #include <algorithm>
      45             : #include <basic/basmgr.hxx>
      46             : namespace basctl
      47             : {
      48             : 
      49             : using namespace ::com::sun::star;
      50             : using namespace ::com::sun::star::uno;
      51             : using namespace ::com::sun::star::container;
      52             : 
      53             : extern "C" {
      54           0 :     SAL_DLLPUBLIC_EXPORT rtl_uString* basicide_choose_macro( void* pOnlyInDocument_AsXModel, sal_Bool bChooseOnly, rtl_uString* pMacroDesc )
      55             :     {
      56           0 :         OUString aMacroDesc( pMacroDesc );
      57           0 :         Reference< frame::XModel > aDocument( static_cast< frame::XModel* >( pOnlyInDocument_AsXModel ) );
      58           0 :         OUString aScriptURL = basctl::ChooseMacro( aDocument, bChooseOnly, aMacroDesc );
      59           0 :         rtl_uString* pScriptURL = aScriptURL.pData;
      60           0 :         rtl_uString_acquire( pScriptURL );
      61             : 
      62           0 :         return pScriptURL;
      63             :     }
      64           0 :     SAL_DLLPUBLIC_EXPORT void basicide_macro_organizer( sal_Int16 nTabId )
      65             :     {
      66             :         SAL_INFO("basctl.basicide","in basicide_macro_organizer");
      67           0 :         basctl::Organize( nTabId );
      68           0 :     }
      69             : }
      70             : 
      71           0 : void Organize( sal_Int16 tabId )
      72             : {
      73           0 :     EnsureIde();
      74             : 
      75           0 :     EntryDescriptor aDesc;
      76           0 :     if (Shell* pShell = GetShell())
      77           0 :         if (BaseWindow* pCurWin = pShell->GetCurWindow())
      78           0 :             aDesc = pCurWin->CreateEntryDescriptor();
      79             : 
      80           0 :     Window* pParent = Application::GetDefDialogParent();
      81           0 :     OrganizeDialog(pParent, tabId, aDesc).Execute();
      82           0 : }
      83             : 
      84           0 : bool IsValidSbxName( const OUString& rName )
      85             : {
      86           0 :     for ( sal_uInt16 nChar = 0; nChar < rName.getLength(); nChar++ )
      87             :     {
      88           0 :         sal_Unicode c = rName[nChar];
      89             :         bool bValid = (
      90           0 :             ( c >= 'A' && c <= 'Z' ) ||
      91           0 :             ( c >= 'a' && c <= 'z' ) ||
      92           0 :             ( c >= '0' && c <= '9' && nChar ) ||
      93             :             ( c == '_' )
      94           0 :         );
      95           0 :         if ( !bValid )
      96           0 :             return false;
      97             :     }
      98           0 :     return true;
      99             : }
     100             : 
     101           0 : static bool StringCompareLessThan( const OUString& rStr1, const OUString& rStr2 )
     102             : {
     103           0 :     return rStr1.compareToIgnoreAsciiCase( rStr2 ) < 0;
     104             : }
     105             : 
     106           0 : Sequence< OUString > GetMergedLibraryNames( const Reference< script::XLibraryContainer >& xModLibContainer, const Reference< script::XLibraryContainer >& xDlgLibContainer )
     107             : {
     108             :     // create a sorted list of module library names
     109           0 :     ::std::vector<OUString> aModLibList;
     110           0 :     if ( xModLibContainer.is() )
     111             :     {
     112           0 :         Sequence< OUString > aModLibNames = xModLibContainer->getElementNames();
     113           0 :         sal_Int32 nModLibCount = aModLibNames.getLength();
     114           0 :         const OUString* pModLibNames = aModLibNames.getConstArray();
     115           0 :         for ( sal_Int32 i = 0 ; i < nModLibCount ; i++ )
     116           0 :             aModLibList.push_back( pModLibNames[ i ] );
     117           0 :         ::std::sort( aModLibList.begin() , aModLibList.end() , StringCompareLessThan );
     118             :     }
     119             : 
     120             :     // create a sorted list of dialog library names
     121           0 :     ::std::vector<OUString> aDlgLibList;
     122           0 :     if ( xDlgLibContainer.is() )
     123             :     {
     124           0 :         Sequence< OUString > aDlgLibNames = xDlgLibContainer->getElementNames();
     125           0 :         sal_Int32 nDlgLibCount = aDlgLibNames.getLength();
     126           0 :         const OUString* pDlgLibNames = aDlgLibNames.getConstArray();
     127           0 :         for ( sal_Int32 i = 0 ; i < nDlgLibCount ; i++ )
     128           0 :             aDlgLibList.push_back( pDlgLibNames[ i ] );
     129           0 :         ::std::sort( aDlgLibList.begin() , aDlgLibList.end() , StringCompareLessThan );
     130             :     }
     131             : 
     132             :     // merge both lists
     133           0 :     ::std::vector<OUString> aLibList( aModLibList.size() + aDlgLibList.size() );
     134           0 :     ::std::merge( aModLibList.begin(), aModLibList.end(), aDlgLibList.begin(), aDlgLibList.end(), aLibList.begin(), StringCompareLessThan );
     135           0 :     ::std::vector<OUString>::iterator aIterEnd = ::std::unique( aLibList.begin(), aLibList.end() );  // move unique elements to the front
     136           0 :     aLibList.erase( aIterEnd, aLibList.end() ); // remove duplicates
     137             : 
     138             :     // copy to sequence
     139           0 :     sal_Int32 nLibCount = aLibList.size();
     140           0 :     Sequence< OUString > aSeqLibNames( nLibCount );
     141           0 :     for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
     142           0 :         aSeqLibNames.getArray()[ i ] = aLibList[ i ];
     143             : 
     144           0 :     return aSeqLibNames;
     145             : }
     146             : 
     147           0 : bool RenameModule (
     148             :     Window* pErrorParent,
     149             :     const ScriptDocument& rDocument,
     150             :     const OUString& rLibName,
     151             :     const OUString& rOldName,
     152             :     const OUString& rNewName
     153             : )
     154             : {
     155           0 :     if ( !rDocument.hasModule( rLibName, rOldName ) )
     156             :     {
     157             :         SAL_WARN( "basctl.basicide","basctl::RenameModule: old module name is invalid!" );
     158           0 :         return false;
     159             :     }
     160             : 
     161           0 :     if ( rDocument.hasModule( rLibName, rNewName ) )
     162             :     {
     163           0 :         ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, IDE_RESSTR(RID_STR_SBXNAMEALLREADYUSED2) );
     164           0 :         aError.Execute();
     165           0 :         return false;
     166             :     }
     167             : 
     168             :     // #i74440
     169           0 :     if ( rNewName.isEmpty() )
     170             :     {
     171           0 :         ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, IDE_RESSTR(RID_STR_BADSBXNAME) );
     172           0 :         aError.Execute();
     173           0 :         return false;
     174             :     }
     175             : 
     176           0 :     if ( !rDocument.renameModule( rLibName, rOldName, rNewName ) )
     177           0 :         return false;
     178             : 
     179           0 :     if (Shell* pShell = GetShell())
     180             :     {
     181           0 :         if (ModulWindow* pWin = pShell->FindBasWin(rDocument, rLibName, rNewName, false, true))
     182             :         {
     183             :             // set new name in window
     184           0 :             pWin->SetName( rNewName );
     185             : 
     186             :             // set new module in module window
     187           0 :             pWin->SetSbModule( (SbModule*)pWin->GetBasic()->FindModule( rNewName ) );
     188             : 
     189             :             // update tabwriter
     190           0 :             sal_uInt16 nId = pShell->GetWindowId( pWin );
     191             :             SAL_WARN_IF( nId == 0 , "basctl.basicide", "No entry in Tabbar!");
     192           0 :             if ( nId )
     193             :             {
     194           0 :                 TabBar& rTabBar = pShell->GetTabBar();
     195           0 :                 rTabBar.SetPageText(nId, rNewName);
     196           0 :                 rTabBar.Sort();
     197           0 :                 rTabBar.MakeVisible(rTabBar.GetCurPageId());
     198             :             }
     199             :         }
     200             :     }
     201           0 :     return true;
     202             : }
     203             : 
     204             : namespace
     205             : {
     206           0 :     struct MacroExecutionData
     207             :     {
     208             :         ScriptDocument  aDocument;
     209             :         SbMethodRef     xMethod;
     210             : 
     211           0 :         MacroExecutionData()
     212             :             :aDocument( ScriptDocument::NoDocument )
     213           0 :             ,xMethod( NULL )
     214             :         {
     215           0 :         }
     216             :     };
     217             : 
     218             :     class MacroExecution
     219             :     {
     220             :     public:
     221             :         DECL_STATIC_LINK( MacroExecution, ExecuteMacroEvent, MacroExecutionData* );
     222             :     };
     223             : 
     224           0 :     IMPL_STATIC_LINK( MacroExecution, ExecuteMacroEvent, MacroExecutionData*, i_pData )
     225             :     {
     226             :         (void)pThis;
     227           0 :         ENSURE_OR_RETURN( i_pData, "wrong MacroExecutionData", 0L );
     228             :         // take ownership of the data
     229           0 :         boost::scoped_ptr< MacroExecutionData > pData( i_pData );
     230             : 
     231             :         SAL_WARN_IF( !(pData->xMethod->GetParent()->GetFlags() & SBX_EXTSEARCH), "basctl.basicide","No EXTSEARCH!" );
     232             : 
     233             :         // in case this is a document-local macro, try to protect the document's Undo Manager from
     234             :         // flawed scripts
     235           0 :         boost::scoped_ptr< ::framework::DocumentUndoGuard > pUndoGuard;
     236           0 :         if ( pData->aDocument.isDocument() )
     237           0 :             pUndoGuard.reset( new ::framework::DocumentUndoGuard( pData->aDocument.getDocument() ) );
     238             : 
     239           0 :         RunMethod(pData->xMethod);
     240             : 
     241           0 :         return 1L;
     242             :     }
     243             : }
     244             : 
     245           0 : OUString ChooseMacro( const uno::Reference< frame::XModel >& rxLimitToDocument, bool bChooseOnly, const OUString& rMacroDesc )
     246             : {
     247             :     (void)rMacroDesc;
     248             : 
     249           0 :     EnsureIde();
     250             : 
     251           0 :     GetExtraData()->ChoosingMacro() = true;
     252             : 
     253           0 :     OUString aScriptURL;
     254           0 :     bool bError = false;
     255           0 :     SbMethod* pMethod = NULL;
     256             : 
     257           0 :     boost::scoped_ptr< MacroChooser > pChooser( new MacroChooser( NULL, true ) );
     258           0 :     if ( bChooseOnly || !SvtModuleOptions().IsBasicIDE() )
     259           0 :         pChooser->SetMode(MacroChooser::ChooseOnly);
     260             : 
     261           0 :     if ( !bChooseOnly && rxLimitToDocument.is() )
     262             :         // Hack!
     263           0 :         pChooser->SetMode(MacroChooser::Recording);
     264             : 
     265           0 :     short nRetValue = pChooser->Execute();
     266             : 
     267           0 :     GetExtraData()->ChoosingMacro() = false;
     268             : 
     269           0 :     switch ( nRetValue )
     270             :     {
     271             :         case Macro_OkRun:
     272             :         {
     273           0 :             pMethod = pChooser->GetMacro();
     274           0 :             if ( !pMethod && pChooser->GetMode() == MacroChooser::Recording )
     275           0 :                 pMethod = pChooser->CreateMacro();
     276             : 
     277           0 :             if ( !pMethod )
     278           0 :                 break;
     279             : 
     280           0 :             SbModule* pModule = pMethod->GetModule();
     281           0 :             if ( !pModule )
     282             :             {
     283             :                 SAL_WARN( "basctl.basicide", "basctl::ChooseMacro: No Module found!" );
     284           0 :                 break;
     285             :             }
     286             : 
     287           0 :             StarBASIC* pBasic = dynamic_cast<StarBASIC*>(pModule->GetParent());
     288           0 :             if ( !pBasic )
     289             :             {
     290             :                 SAL_WARN( "basctl.basicide", "basctl::ChooseMacro: No Basic found!" );
     291           0 :                 break;
     292             :             }
     293             : 
     294           0 :             BasicManager* pBasMgr = FindBasicManager( pBasic );
     295           0 :             if ( !pBasMgr )
     296             :             {
     297             :                 SAL_WARN( "basctl.basicide", "basctl::ChooseMacro: No BasicManager found!" );
     298           0 :                 break;
     299             :             }
     300             : 
     301             :             // name
     302           0 :             OUString aName;
     303           0 :             aName += pBasic->GetName();
     304           0 :             aName += ".";
     305           0 :             aName += pModule->GetName();
     306           0 :             aName += ".";
     307           0 :             aName += pMethod->GetName();
     308             : 
     309             :             // language
     310           0 :             OUString aLanguage("Basic");
     311             : 
     312             :             // location
     313           0 :             OUString aLocation;
     314           0 :             ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
     315           0 :             if ( aDocument.isDocument() )
     316             :             {
     317             :                 // document basic
     318           0 :                 aLocation = "document" ;
     319             : 
     320           0 :                 if ( rxLimitToDocument.is() )
     321             :                 {
     322           0 :                     uno::Reference< frame::XModel > xLimitToDocument( rxLimitToDocument );
     323             : 
     324           0 :                     uno::Reference< document::XEmbeddedScripts > xScripts( rxLimitToDocument, UNO_QUERY );
     325           0 :                     if ( !xScripts.is() )
     326             :                     {   // the document itself does not support embedding scripts
     327           0 :                         uno::Reference< document::XScriptInvocationContext > xContext( rxLimitToDocument, UNO_QUERY );
     328           0 :                         if ( xContext.is() )
     329           0 :                             xScripts = xContext->getScriptContainer();
     330           0 :                         if ( xScripts.is() )
     331             :                         {   // but it is able to refer to a document which actually does support this
     332           0 :                             xLimitToDocument.set( xScripts, UNO_QUERY );
     333           0 :                             if ( !xLimitToDocument.is() )
     334             :                             {
     335             :                                 SAL_WARN_IF(!xLimitToDocument.is(), "basctl.basicide", "basctl::ChooseMacro: a script container which is no document!?" );
     336           0 :                                 xLimitToDocument = rxLimitToDocument;
     337             :                             }
     338           0 :                         }
     339             :                     }
     340             : 
     341           0 :                     if ( xLimitToDocument != aDocument.getDocument() )
     342             :                     {
     343             :                         // error
     344           0 :                         bError = true;
     345           0 :                         ErrorBox( NULL, WB_OK | WB_DEF_OK, IDEResId(RID_STR_ERRORCHOOSEMACRO).toString() ).Execute();
     346           0 :                     }
     347             :                 }
     348             :             }
     349             :             else
     350             :             {
     351             :                 // application basic
     352           0 :                 aLocation = "application" ;
     353             :             }
     354             : 
     355             :             // script URL
     356           0 :             if ( !bError )
     357             :             {
     358           0 :                 aScriptURL = "vnd.sun.star.script:" ;
     359           0 :                 aScriptURL += aName;
     360           0 :                 aScriptURL += "?language=" ;
     361           0 :                 aScriptURL += aLanguage;
     362           0 :                 aScriptURL += "&location=" ;
     363           0 :                 aScriptURL += aLocation;
     364             :             }
     365             : 
     366           0 :             if ( !rxLimitToDocument.is() )
     367             :             {
     368           0 :                 MacroExecutionData* pExecData = new MacroExecutionData;
     369           0 :                 pExecData->aDocument = aDocument;
     370           0 :                 pExecData->xMethod = pMethod;   // keep alive until the event has been processed
     371           0 :                 Application::PostUserEvent( STATIC_LINK( NULL, MacroExecution, ExecuteMacroEvent ), pExecData );
     372           0 :             }
     373             :         }
     374           0 :         break;
     375             :     }
     376             : 
     377           0 :     return aScriptURL;
     378             : }
     379             : 
     380           0 : Sequence< OUString > GetMethodNames( const ScriptDocument& rDocument, const OUString& rLibName, const OUString& rModName )
     381             :     throw(NoSuchElementException )
     382             : {
     383           0 :     Sequence< OUString > aSeqMethods;
     384             : 
     385             :     // get module
     386           0 :     OUString aOUSource;
     387           0 :     if ( rDocument.getModule( rLibName, rModName, aOUSource ) )
     388             :     {
     389           0 :         BasicManager* pBasMgr = rDocument.getBasicManager();
     390           0 :         StarBASIC* pSb = pBasMgr ? pBasMgr->GetLib( rLibName ) : NULL;
     391           0 :         SbModule* pMod = pSb ? pSb->FindModule( rModName ) : NULL;
     392             : 
     393           0 :         SbModuleRef xModule;
     394             :         // Only reparse modules if ScriptDocument source is out of sync
     395             :         // with basic's Module
     396           0 :         if ( !pMod || ( pMod && pMod->GetSource() != aOUSource ) )
     397             :         {
     398           0 :             xModule = new SbModule( rModName );
     399           0 :             xModule->SetSource32( aOUSource );
     400           0 :             pMod = xModule;
     401             :         }
     402             : 
     403           0 :         sal_uInt16 nCount = pMod->GetMethods()->Count();
     404           0 :         sal_uInt16 nRealCount = nCount;
     405           0 :         for ( sal_uInt16 i = 0; i < nCount; i++ )
     406             :         {
     407           0 :             SbMethod* pMethod = (SbMethod*)pMod->GetMethods()->Get( i );
     408           0 :             if( pMethod->IsHidden() )
     409           0 :                 --nRealCount;
     410             :         }
     411           0 :         aSeqMethods.realloc( nRealCount );
     412             : 
     413           0 :         sal_uInt16 iTarget = 0;
     414           0 :         for ( sal_uInt16 i = 0 ; i < nCount; ++i )
     415             :         {
     416           0 :             SbMethod* pMethod = (SbMethod*)pMod->GetMethods()->Get( i );
     417           0 :             if( pMethod->IsHidden() )
     418           0 :                 continue;
     419             :             SAL_WARN_IF( !pMethod, "basctl.basicide","Method not found! (NULL)" );
     420           0 :             aSeqMethods.getArray()[ iTarget++ ] = pMethod->GetName();
     421           0 :         }
     422             :     }
     423             : 
     424           0 :     return aSeqMethods;
     425             : }
     426             : 
     427           0 : bool HasMethod (
     428             :     ScriptDocument const& rDocument,
     429             :     OUString const& rLibName,
     430             :     OUString const& rModName,
     431             :     OUString const& rMethName
     432             : )
     433             : {
     434           0 :     bool bHasMethod = false;
     435             : 
     436           0 :     OUString aOUSource;
     437           0 :     if ( rDocument.hasModule( rLibName, rModName ) && rDocument.getModule( rLibName, rModName, aOUSource ) )
     438             :     {
     439             :         // Check if we really need to scan the source ( again )
     440           0 :         BasicManager* pBasMgr = rDocument.getBasicManager();
     441           0 :         StarBASIC* pSb = pBasMgr ? pBasMgr->GetLib( rLibName ) : NULL;
     442           0 :         SbModule* pMod = pSb ? pSb->FindModule( rModName ) : NULL;
     443           0 :         SbModuleRef xModule;
     444             :         // Only reparse modules if ScriptDocument source is out of sync
     445             :         // with basic's Module
     446           0 :         if ( !pMod || ( pMod && pMod->GetSource() != aOUSource ))
     447             :         {
     448           0 :             xModule = new SbModule( rModName );
     449           0 :             xModule->SetSource32( aOUSource );
     450           0 :             pMod = xModule;
     451             :         }
     452           0 :         SbxArray* pMethods = pMod->GetMethods();
     453           0 :         if ( pMethods )
     454             :         {
     455           0 :             SbMethod* pMethod = (SbMethod*)pMethods->Find( rMethName, SbxCLASS_METHOD );
     456           0 :             if ( pMethod && !pMethod->IsHidden() )
     457           0 :                 bHasMethod = true;
     458           0 :         }
     459             :     }
     460             : 
     461           0 :     return bHasMethod;
     462             : }
     463             : 
     464           0 : } // namespace basctl
     465             : 
     466             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10