LCOV - code coverage report
Current view: top level - libreoffice/basctl/source/basicide - basobj3.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 232 0.0 %
Date: 2012-12-17 Functions: 0 14 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 <vcl/msgbox.hxx>
      21             : #include <vector>
      22             : #include <algorithm>
      23             : #include <basic/basmgr.hxx>
      24             : #include <basic/sbmeth.hxx>
      25             : #include <basic/sbx.hxx>
      26             : #include <unotools/moduleoptions.hxx>
      27             : 
      28             : #include <iderdll.hxx>
      29             : #include <iderdll2.hxx>
      30             : #include <basobj.hxx>
      31             : #include <basidesh.hxx>
      32             : #include <objdlg.hxx>
      33             : #include <bastypes.hxx>
      34             : #include <basdoc.hxx>
      35             : #include <basidesh.hrc>
      36             : 
      37             : #include <baside2.hxx>
      38             : #include <baside3.hxx>
      39             : #include <basicmod.hxx>
      40             : #include <localizationmgr.hxx>
      41             : #include "dlged.hxx"
      42             : #include <dlgeddef.hxx>
      43             : #include <comphelper/processfactory.hxx>
      44             : #include <com/sun/star/script/XLibraryContainer.hpp>
      45             : #include <com/sun/star/script/XLibraryContainerPassword.hpp>
      46             : #include <com/sun/star/container/XNameContainer.hpp>
      47             : #include <xmlscript/xmldlg_imexp.hxx>
      48             : #include <sfx2/app.hxx>
      49             : #include <sfx2/dispatch.hxx>
      50             : #include <sfx2/request.hxx>
      51             : #include <rtl/uri.hxx>
      52             : #include <osl/process.h>
      53             : #include <osl/file.hxx>
      54             : 
      55             : namespace basctl
      56             : {
      57             : 
      58             : using namespace comphelper;
      59             : using namespace ::com::sun::star;
      60             : using namespace ::com::sun::star::uno;
      61             : using namespace ::com::sun::star::container;
      62             : 
      63             : 
      64             : //----------------------------------------------------------------------------
      65             : 
      66             : extern "C" {
      67           0 :     SAL_DLLPUBLIC_EXPORT long basicide_handle_basic_error( void* pPtr )
      68             :     {
      69           0 :         return HandleBasicError( (StarBASIC*)pPtr );
      70             :     }
      71             : }
      72             : 
      73             : //----------------------------------------------------------------------------
      74             : 
      75           0 : SbMethod* CreateMacro( SbModule* pModule, const String& rMacroName )
      76             : {
      77           0 :     SfxDispatcher* pDispatcher = GetDispatcher();
      78           0 :     if( pDispatcher )
      79             :     {
      80           0 :         pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
      81             :     }
      82             : 
      83           0 :     if ( pModule->GetMethods()->Find( rMacroName, SbxCLASS_METHOD ) )
      84           0 :         return 0;
      85             : 
      86           0 :     OUString aMacroName( rMacroName );
      87           0 :     if ( aMacroName.isEmpty() )
      88             :     {
      89           0 :         if ( !pModule->GetMethods()->Count() )
      90           0 :             aMacroName = "Main" ;
      91             :         else
      92             :         {
      93           0 :             bool bValid = false;
      94           0 :             OUString aStdMacroText( "Macro" );
      95           0 :             sal_Int32 nMacro = 1;
      96           0 :             while ( !bValid )
      97             :             {
      98           0 :                 aMacroName = aStdMacroText;
      99           0 :                 aMacroName += OUString::valueOf( nMacro );
     100             :                 // test whether existing...
     101           0 :                 bValid = pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD ) ? false : true;
     102           0 :                 nMacro++;
     103           0 :             }
     104             :         }
     105             :     }
     106             : 
     107           0 :     OUString aOUSource( pModule->GetSource32() );
     108             : 
     109             :     // don't produce too many empty lines...
     110           0 :     sal_Int32 nSourceLen = aOUSource.getLength();
     111           0 :     if ( nSourceLen > 2 )
     112             :     {
     113           0 :         const sal_Unicode* pStr = aOUSource.getStr();
     114           0 :         if ( pStr[ nSourceLen - 1 ]  != LINE_SEP )
     115           0 :             aOUSource += "\n\n" ;
     116           0 :         else if ( pStr[ nSourceLen - 2 ] != LINE_SEP )
     117           0 :             aOUSource += "\n" ;
     118           0 :         else if ( pStr[ nSourceLen - 3 ] == LINE_SEP )
     119           0 :             aOUSource = aOUSource.copy( 0, nSourceLen-1 );
     120             :     }
     121             : 
     122           0 :     OUString aSubStr;
     123           0 :     aSubStr = "Sub " ;
     124           0 :     aSubStr += aMacroName;
     125           0 :     aSubStr += "\n\nEnd Sub" ;
     126             : 
     127           0 :     aOUSource += aSubStr;
     128             : 
     129             :     // update module in library
     130           0 :     ScriptDocument aDocument( ScriptDocument::NoDocument );
     131           0 :     StarBASIC* pBasic = dynamic_cast<StarBASIC*>(pModule->GetParent());
     132             :     DBG_ASSERT(pBasic, "basctl::CreateMacro: No Basic found!");
     133           0 :     if ( pBasic )
     134             :     {
     135           0 :         BasicManager* pBasMgr = FindBasicManager( pBasic );
     136             :         DBG_ASSERT(pBasMgr, "basctl::CreateMacro: No BasicManager found!");
     137           0 :         if ( pBasMgr )
     138             :         {
     139           0 :             aDocument = ScriptDocument::getDocumentForBasicManager( pBasMgr );
     140             :             OSL_ENSURE( aDocument.isValid(), "basctl::CreateMacro: no document for the given BasicManager!" );
     141           0 :             if ( aDocument.isValid() )
     142             :             {
     143           0 :                 String aLibName = pBasic->GetName();
     144           0 :                 String aModName = pModule->GetName();
     145           0 :                 OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aOUSource ) );
     146             :             }
     147             :         }
     148             :     }
     149             : 
     150           0 :     SbMethod* pMethod = (SbMethod*)pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD );
     151             : 
     152           0 :     if( pDispatcher )
     153             :     {
     154           0 :         pDispatcher->Execute( SID_BASICIDE_UPDATEALLMODULESOURCES );
     155             :     }
     156             : 
     157           0 :     if ( aDocument.isAlive() )
     158           0 :         MarkDocumentModified( aDocument );
     159             : 
     160           0 :     return pMethod;
     161             : }
     162             : 
     163             : //----------------------------------------------------------------------------
     164             : 
     165           0 : bool RenameDialog (
     166             :     Window* pErrorParent,
     167             :     ScriptDocument const& rDocument,
     168             :     OUString const& rLibName,
     169             :     OUString const& rOldName,
     170             :     OUString const& rNewName
     171             : )
     172             :     throw(ElementExistException, NoSuchElementException)
     173             : {
     174           0 :     if ( !rDocument.hasDialog( rLibName, rOldName ) )
     175             :     {
     176             :         OSL_FAIL( "basctl::RenameDialog: old module name is invalid!" );
     177           0 :         return false;
     178             :     }
     179             : 
     180           0 :     if ( rDocument.hasDialog( rLibName, rNewName ) )
     181             :     {
     182           0 :         ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, IDE_RESSTR(RID_STR_SBXNAMEALLREADYUSED2) );
     183           0 :         aError.Execute();
     184           0 :         return false;
     185             :     }
     186             : 
     187             :     // #i74440
     188           0 :     if ( rNewName.isEmpty() )
     189             :     {
     190           0 :         ErrorBox aError( pErrorParent, WB_OK | WB_DEF_OK, IDE_RESSTR(RID_STR_BADSBXNAME) );
     191           0 :         aError.Execute();
     192           0 :         return false;
     193             :     }
     194             : 
     195           0 :     Shell* pShell = GetShell();
     196           0 :     DialogWindow* pWin = pShell ? pShell->FindDlgWin(rDocument, rLibName, rOldName) : 0;
     197           0 :     Reference< XNameContainer > xExistingDialog;
     198           0 :     if ( pWin )
     199           0 :         xExistingDialog = pWin->GetEditor().GetDialog();
     200             : 
     201           0 :     if ( xExistingDialog.is() )
     202           0 :         LocalizationMgr::renameStringResourceIDs( rDocument, rLibName, rNewName, xExistingDialog );
     203             : 
     204           0 :     if ( !rDocument.renameDialog( rLibName, rOldName, rNewName, xExistingDialog ) )
     205           0 :         return false;
     206             : 
     207           0 :     if ( pWin )
     208             :     {
     209             :         // set new name in window
     210           0 :         pWin->SetName( rNewName );
     211             : 
     212             :         // update property browser
     213           0 :         pWin->UpdateBrowser();
     214             : 
     215             :         // update tabwriter
     216           0 :         sal_uInt16 nId = pShell->GetWindowId( pWin );
     217             :         DBG_ASSERT( nId, "No entry in Tabbar!" );
     218           0 :         if ( nId )
     219             :         {
     220           0 :             TabBar& rTabBar = pShell->GetTabBar();
     221           0 :             rTabBar.SetPageText( nId, rNewName );
     222           0 :             rTabBar.Sort();
     223           0 :             rTabBar.MakeVisible( rTabBar.GetCurPageId() );
     224             :         }
     225             :     }
     226           0 :     return true;
     227             : }
     228             : 
     229             : //----------------------------------------------------------------------------
     230             : 
     231           0 : bool RemoveDialog( const ScriptDocument& rDocument, const OUString& rLibName, const OUString& rDlgName )
     232             : {
     233           0 :     if (Shell* pShell = GetShell())
     234             :     {
     235           0 :         if (DialogWindow* pDlgWin = pShell->FindDlgWin(rDocument, rLibName, rDlgName))
     236             :         {
     237           0 :             Reference< container::XNameContainer > xDialogModel = pDlgWin->GetDialog();
     238           0 :             LocalizationMgr::removeResourceForDialog( rDocument, rLibName, rDlgName, xDialogModel );
     239             :         }
     240             :     }
     241             : 
     242           0 :     return rDocument.removeDialog( rLibName, rDlgName );
     243             : }
     244             : 
     245             : //----------------------------------------------------------------------------
     246             : 
     247           0 : StarBASIC* FindBasic( const SbxVariable* pVar )
     248             : {
     249           0 :     SbxVariable const* pSbx = pVar;
     250           0 :     while (pSbx && !dynamic_cast<StarBASIC const*>(pSbx))
     251           0 :         pSbx = pSbx->GetParent();
     252           0 :     return (StarBASIC*)pSbx;
     253             : }
     254             : 
     255             : //----------------------------------------------------------------------------
     256             : 
     257           0 : BasicManager* FindBasicManager( StarBASIC* pLib )
     258             : {
     259           0 :     ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::AllWithApplication ) );
     260           0 :     for (   ScriptDocuments::const_iterator doc = aDocuments.begin();
     261           0 :             doc != aDocuments.end();
     262             :             ++doc
     263             :         )
     264             :     {
     265           0 :         BasicManager* pBasicMgr = doc->getBasicManager();
     266             :         OSL_ENSURE( pBasicMgr, "basctl::FindBasicManager: no basic manager for the document!" );
     267           0 :         if ( !pBasicMgr )
     268           0 :             continue;
     269             : 
     270           0 :         Sequence< OUString > aLibNames( doc->getLibraryNames() );
     271           0 :         sal_Int32 nLibCount = aLibNames.getLength();
     272           0 :         const OUString* pLibNames = aLibNames.getConstArray();
     273             : 
     274           0 :         for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
     275             :         {
     276           0 :             StarBASIC* pL = pBasicMgr->GetLib( pLibNames[ i ] );
     277           0 :             if ( pL == pLib )
     278           0 :                 return pBasicMgr;
     279             :         }
     280           0 :     }
     281           0 :     return NULL;
     282             : }
     283             : 
     284             : //----------------------------------------------------------------------------
     285             : 
     286           0 : void MarkDocumentModified( const ScriptDocument& rDocument )
     287             : {
     288             :     // does not have to come from a document...
     289           0 :     if ( rDocument.isApplication() )
     290             :     {
     291           0 :         if (Shell* pShell = GetShell())
     292             :         {
     293           0 :             pShell->SetAppBasicModified();
     294           0 :             pShell->UpdateObjectCatalog();
     295             :         }
     296             :     }
     297             :     else
     298             :     {
     299           0 :         rDocument.setDocumentModified();
     300             :     }
     301             : 
     302           0 :     if (SfxBindings* pBindings = GetBindingsPtr())
     303             :     {
     304           0 :         pBindings->Invalidate( SID_SIGNATURE );
     305           0 :         pBindings->Invalidate( SID_SAVEDOC );
     306           0 :         pBindings->Update( SID_SAVEDOC );
     307             :     }
     308           0 : }
     309             : 
     310             : //----------------------------------------------------------------------------
     311             : 
     312           0 : void RunMethod( SbMethod* pMethod )
     313             : {
     314           0 :     SbxValues aRes;
     315           0 :     aRes.eType = SbxVOID;
     316           0 :     pMethod->Get( aRes );
     317           0 : }
     318             : 
     319             : //----------------------------------------------------------------------------
     320             : 
     321           0 : void StopBasic()
     322             : {
     323           0 :     StarBASIC::Stop();
     324           0 :     if (Shell* pShell = GetShell())
     325             :     {
     326           0 :         Shell::WindowTable& rWindows = pShell->GetWindowTable();
     327           0 :         for (Shell::WindowTableIt it = rWindows.begin(); it != rWindows.end(); ++it )
     328             :         {
     329           0 :             BaseWindow* pWin = it->second;
     330             :             // call BasicStopped manually because the Stop-Notify
     331             :             // might not get through otherwise
     332           0 :             pWin->BasicStopped();
     333             :         }
     334             :     }
     335           0 :     BasicStopped();
     336           0 : }
     337             : 
     338             : //----------------------------------------------------------------------------
     339             : 
     340           0 : void BasicStopped(
     341             :     bool* pbAppWindowDisabled,
     342             :     bool* pbDispatcherLocked,
     343             :     sal_uInt16* pnWaitCount,
     344             :     SfxUInt16Item** ppSWActionCount, SfxUInt16Item** ppSWLockViewCount
     345             : )
     346             : {
     347             :     // maybe there are some locks to be removed after an error
     348             :     // or an explicit cancelling of the basic...
     349             : 
     350           0 :     if ( pbAppWindowDisabled )
     351           0 :         *pbAppWindowDisabled = false;
     352           0 :     if ( pbDispatcherLocked )
     353           0 :         *pbDispatcherLocked = false;
     354           0 :     if ( pnWaitCount )
     355           0 :         *pnWaitCount = 0;
     356           0 :     if ( ppSWActionCount )
     357           0 :         *ppSWActionCount = 0;
     358           0 :     if ( ppSWLockViewCount )
     359           0 :         *ppSWLockViewCount = 0;
     360             : 
     361             :     // AppWait ?
     362           0 :     if (Shell* pShell = GetShell())
     363             :     {
     364           0 :         sal_uInt16 nWait = 0;
     365           0 :         while ( pShell->GetViewFrame()->GetWindow().IsWait() )
     366             :         {
     367           0 :             pShell->GetViewFrame()->GetWindow().LeaveWait();
     368           0 :             nWait++;
     369             :         }
     370           0 :         if ( pnWaitCount )
     371           0 :             *pnWaitCount = nWait;
     372             :     }
     373             : 
     374           0 :     Window* pDefParent = Application::GetDefDialogParent();
     375           0 :     if ( pDefParent && !pDefParent->IsEnabled() )
     376             :     {
     377           0 :         pDefParent->Enable(true);
     378           0 :         if ( pbAppWindowDisabled )
     379           0 :             *pbAppWindowDisabled = true;
     380             :     }
     381             : 
     382           0 : }
     383             : 
     384             : //----------------------------------------------------------------------------
     385             : 
     386           0 : void InvalidateDebuggerSlots()
     387             : {
     388           0 :     if (SfxBindings* pBindings = GetBindingsPtr())
     389             :     {
     390           0 :         pBindings->Invalidate( SID_BASICSTOP );
     391           0 :         pBindings->Update( SID_BASICSTOP );
     392           0 :         pBindings->Invalidate( SID_BASICRUN );
     393           0 :         pBindings->Update( SID_BASICRUN );
     394           0 :         pBindings->Invalidate( SID_BASICCOMPILE );
     395           0 :         pBindings->Update( SID_BASICCOMPILE );
     396           0 :         pBindings->Invalidate( SID_BASICSTEPOVER );
     397           0 :         pBindings->Update( SID_BASICSTEPOVER );
     398           0 :         pBindings->Invalidate( SID_BASICSTEPINTO );
     399           0 :         pBindings->Update( SID_BASICSTEPINTO );
     400           0 :         pBindings->Invalidate( SID_BASICSTEPOUT );
     401           0 :         pBindings->Update( SID_BASICSTEPOUT );
     402           0 :         pBindings->Invalidate( SID_BASICIDE_TOGGLEBRKPNT );
     403           0 :         pBindings->Update( SID_BASICIDE_TOGGLEBRKPNT );
     404           0 :         pBindings->Invalidate( SID_BASICIDE_STAT_POS );
     405           0 :         pBindings->Update( SID_BASICIDE_STAT_POS );
     406             :     }
     407           0 : }
     408             : 
     409             : //----------------------------------------------------------------------------
     410             : 
     411           0 : long HandleBasicError( StarBASIC* pBasic )
     412             : {
     413           0 :     EnsureIde();
     414           0 :     BasicStopped();
     415             : 
     416             :     // no error output during macro choosing
     417           0 :     if (GetExtraData()->ChoosingMacro())
     418           0 :         return 1;
     419           0 :     if (GetExtraData()->ShellInCriticalSection())
     420           0 :         return 2;
     421             : 
     422           0 :     long nRet = 0;
     423           0 :     Shell* pShell = 0;
     424           0 :     if ( SvtModuleOptions().IsBasicIDE() )
     425             :     {
     426           0 :         BasicManager* pBasMgr = FindBasicManager( pBasic );
     427           0 :         if ( pBasMgr )
     428             :         {
     429           0 :             bool bProtected = false;
     430           0 :             ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
     431             :             OSL_ENSURE( aDocument.isValid(), "basctl::HandleBasicError: no document for the given BasicManager!" );
     432           0 :             if ( aDocument.isValid() )
     433             :             {
     434           0 :                 OUString aOULibName( pBasic->GetName() );
     435           0 :                 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
     436           0 :                 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
     437             :                 {
     438           0 :                     Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
     439           0 :                     if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
     440             :                     {
     441           0 :                         bProtected = true;
     442           0 :                     }
     443           0 :                 }
     444             :             }
     445             : 
     446           0 :             if ( !bProtected )
     447             :             {
     448           0 :                 pShell = GetShell();
     449           0 :                 if ( !pShell )
     450             :                 {
     451           0 :                     SfxAllItemSet aArgs( SFX_APP()->GetPool() );
     452           0 :                     SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
     453           0 :                     SFX_APP()->ExecuteSlot( aRequest );
     454           0 :                     pShell = GetShell();
     455             :                 }
     456           0 :             }
     457             :         }
     458             :     }
     459             : 
     460           0 :     if ( pShell )
     461           0 :         nRet = pShell->CallBasicErrorHdl( pBasic );
     462             :     else
     463           0 :         ErrorHandler::HandleError( StarBASIC::GetErrorCode() );
     464             : 
     465           0 :     return nRet;
     466             : }
     467             : 
     468             : //----------------------------------------------------------------------------
     469             : 
     470           0 : SfxBindings* GetBindingsPtr()
     471             : {
     472           0 :     SfxBindings* pBindings = NULL;
     473             : 
     474           0 :     SfxViewFrame* pFrame = NULL;
     475           0 :     if (Shell* pShell = GetShell())
     476             :     {
     477           0 :         pFrame = pShell->GetViewFrame();
     478             :     }
     479             :     else
     480             :     {
     481           0 :         SfxViewFrame* pView = SfxViewFrame::GetFirst();
     482           0 :         while ( pView )
     483             :         {
     484           0 :             if (dynamic_cast<DocShell*>(pView->GetObjectShell()))
     485             :             {
     486           0 :                 pFrame = pView;
     487           0 :                 break;
     488             :             }
     489           0 :             pView = SfxViewFrame::GetNext( *pView );
     490             :         }
     491             :     }
     492           0 :     if ( pFrame != NULL )
     493           0 :         pBindings = &pFrame->GetBindings();
     494             : 
     495           0 :     return pBindings;
     496             : }
     497             : 
     498             : //----------------------------------------------------------------------------
     499             : 
     500           0 : SfxDispatcher* GetDispatcher ()
     501             : {
     502           0 :     if (Shell* pShell = GetShell())
     503           0 :         if (SfxViewFrame* pViewFrame = pShell->GetViewFrame())
     504           0 :             if (SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher())
     505           0 :                 return pDispatcher;
     506           0 :     return 0;
     507             : }
     508             : 
     509             : //----------------------------------------------------------------------------
     510             : } // namespace basctl
     511             : 
     512             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10