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

Generated by: LCOV version 1.11