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

Generated by: LCOV version 1.10