LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbahyperlink.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 3 111 2.7 %
Date: 2015-06-13 12:38:46 Functions: 2 24 8.3 %
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 "vbahyperlink.hxx"
      21             : #include <vbahelper/helperdecl.hxx>
      22             : #include <com/sun/star/container/XIndexAccess.hpp>
      23             : #include <com/sun/star/text/XText.hpp>
      24             : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
      25             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      26             : #include <ooo/vba/office/MsoHyperlinkType.hpp>
      27             : #include <ooo/vba/msforms/XShape.hpp>
      28             : #include "vbarange.hxx"
      29             : 
      30             : using namespace ::ooo::vba;
      31             : using namespace ::com::sun::star;
      32             : 
      33           0 : ScVbaHyperlink::ScVbaHyperlink( const uno::Sequence< uno::Any >& rArgs,
      34             :         const uno::Reference< uno::XComponentContext >& rxContext ) throw (lang::IllegalArgumentException, uno::RuntimeException) :
      35             :     HyperlinkImpl_BASE( getXSomethingFromArgs< XHelperInterface >( rArgs, 0 ), rxContext ),
      36             :     mxCell( getXSomethingFromArgs< table::XCell >( rArgs, 1, false ) ),
      37           0 :     mnType( office::MsoHyperlinkType::msoHyperlinkRange )
      38             : {
      39           0 :     uno::Reference< text::XTextFieldsSupplier > xTextFields( mxCell, uno::UNO_QUERY_THROW );
      40           0 :     uno::Reference< container::XIndexAccess > xIndex( xTextFields->getTextFields(), uno::UNO_QUERY_THROW );
      41           0 :     mxTextField.set( xIndex->getByIndex(0), uno::UNO_QUERY_THROW );
      42           0 : }
      43             : 
      44           0 : ScVbaHyperlink::ScVbaHyperlink( const uno::Reference< XHelperInterface >& rxAnchor,
      45             :         const uno::Reference< uno::XComponentContext >& rxContext,
      46             :         const uno::Any& rAddress, const uno::Any& rSubAddress,
      47             :         const uno::Any& rScreenTip, const uno::Any& rTextToDisplay ) throw (uno::RuntimeException) :
      48           0 :     HyperlinkImpl_BASE( rxAnchor, rxContext ) // parent of Hyperlink is the anchor object
      49             : {
      50             :     // extract parameters, Address must not be empty
      51           0 :     UrlComponents aUrlComp;
      52           0 :     OUString aTextToDisplay;
      53           0 :     if( !(rAddress >>= aUrlComp.first) || aUrlComp.first.isEmpty() )
      54           0 :         throw uno::RuntimeException("Cannot get address" );
      55           0 :     rSubAddress >>= aUrlComp.second;
      56           0 :     rScreenTip >>= maScreenTip;
      57           0 :     rTextToDisplay >>= aTextToDisplay;
      58             : 
      59             :     // get anchor range or anchor shape
      60           0 :     uno::Reference< excel::XRange > xAnchorRange( rxAnchor, uno::UNO_QUERY );
      61           0 :     if( xAnchorRange.is() )
      62             :     {
      63           0 :         mnType = office::MsoHyperlinkType::msoHyperlinkRange;
      64             :         // only single ranges are allowed
      65           0 :         uno::Reference< table::XCellRange > xUnoRange( ScVbaRange::getCellRange( xAnchorRange ), uno::UNO_QUERY_THROW );
      66             :         // insert the hyperlink into the top-left cell only
      67           0 :         mxCell.set( xUnoRange->getCellByPosition( 0, 0 ), uno::UNO_SET_THROW );
      68           0 :         uno::Reference< text::XText > xText( mxCell, uno::UNO_QUERY_THROW );
      69             :         // use cell text or URL if no TextToDisplay has been passed
      70           0 :         if( aTextToDisplay.isEmpty() )
      71             :         {
      72           0 :             aTextToDisplay = xText->getString();
      73           0 :             if( aTextToDisplay.isEmpty() )
      74             :             {
      75           0 :                 OUStringBuffer aBuffer( aUrlComp.first );
      76           0 :                 if( !aUrlComp.second.isEmpty() )
      77           0 :                     aBuffer.append( " - " ).append( aUrlComp.second );
      78           0 :                 aTextToDisplay = aBuffer.makeStringAndClear();
      79             :             }
      80             :         }
      81             :         // create and initialize a new URL text field
      82           0 :         uno::Reference< lang::XMultiServiceFactory > xFactory( ScVbaRange::getUnoModel( xAnchorRange ), uno::UNO_QUERY_THROW );
      83           0 :         uno::Reference< text::XTextContent > xUrlField( xFactory->createInstance("com.sun.star.text.TextField.URL"), uno::UNO_QUERY_THROW );
      84           0 :         mxTextField.set( xUrlField, uno::UNO_QUERY_THROW );
      85           0 :         setUrlComponents( aUrlComp );
      86           0 :         setTextToDisplay( aTextToDisplay );
      87             :         // insert the text field into the document
      88           0 :         xText->setString( OUString() );
      89           0 :         uno::Reference< text::XTextRange > xRange( xText->createTextCursor(), uno::UNO_QUERY_THROW );
      90           0 :         xText->insertTextContent( xRange, xUrlField, false );
      91             :     }
      92             :     else
      93             :     {
      94           0 :         uno::Reference< msforms::XShape > xAnchorShape( rxAnchor, uno::UNO_QUERY_THROW );
      95           0 :         mnType = office::MsoHyperlinkType::msoHyperlinkShape;
      96             :         // FIXME: insert hyperlink into shape
      97           0 :         throw uno::RuntimeException();
      98           0 :     }
      99           0 : }
     100             : 
     101           0 : ScVbaHyperlink::~ScVbaHyperlink()
     102             : {
     103           0 : }
     104             : 
     105           0 : OUString ScVbaHyperlink::getName() throw (uno::RuntimeException, std::exception)
     106             : {
     107             :     // it seems this attribute is same as TextToDisplay
     108           0 :     return getTextToDisplay();
     109             : }
     110             : 
     111           0 : void ScVbaHyperlink::setName( const OUString& rName ) throw (uno::RuntimeException, std::exception)
     112             : {
     113           0 :     setTextToDisplay( rName );
     114           0 : }
     115             : 
     116           0 : OUString ScVbaHyperlink::getAddress() throw (uno::RuntimeException, std::exception)
     117             : {
     118           0 :     return getUrlComponents().first;
     119             : }
     120             : 
     121           0 : void ScVbaHyperlink::setAddress( const OUString& rAddress ) throw (uno::RuntimeException, std::exception)
     122             : {
     123           0 :     UrlComponents aUrlComp = getUrlComponents();
     124           0 :     aUrlComp.first = rAddress;
     125           0 :     setUrlComponents( aUrlComp );
     126           0 : }
     127             : 
     128           0 : OUString ScVbaHyperlink::getSubAddress() throw (uno::RuntimeException, std::exception)
     129             : {
     130           0 :     return getUrlComponents().second;
     131             : }
     132             : 
     133           0 : void ScVbaHyperlink::setSubAddress( const OUString& rSubAddress ) throw (uno::RuntimeException, std::exception)
     134             : {
     135           0 :     UrlComponents aUrlComp = getUrlComponents();
     136           0 :     aUrlComp.second = rSubAddress;
     137           0 :     setUrlComponents( aUrlComp );
     138           0 : }
     139             : 
     140           0 : OUString SAL_CALL ScVbaHyperlink::getScreenTip() throw (uno::RuntimeException, std::exception)
     141             : {
     142           0 :     return maScreenTip;
     143             : }
     144             : 
     145           0 : void SAL_CALL ScVbaHyperlink::setScreenTip( const OUString& rScreenTip ) throw (uno::RuntimeException, std::exception)
     146             : {
     147           0 :     maScreenTip = rScreenTip;
     148           0 : }
     149             : 
     150           0 : OUString ScVbaHyperlink::getTextToDisplay() throw (uno::RuntimeException, std::exception)
     151             : {
     152           0 :     ensureTextField();
     153           0 :     OUString aTextToDisplay;
     154           0 :     mxTextField->getPropertyValue("Representation") >>= aTextToDisplay;
     155           0 :     return aTextToDisplay;
     156             : }
     157             : 
     158           0 : void ScVbaHyperlink::setTextToDisplay( const OUString& rTextToDisplay ) throw (uno::RuntimeException, std::exception)
     159             : {
     160           0 :     ensureTextField();
     161           0 :     mxTextField->setPropertyValue("Representation", uno::Any( rTextToDisplay ) );
     162           0 : }
     163             : 
     164           0 : sal_Int32 SAL_CALL ScVbaHyperlink::getType() throw (uno::RuntimeException, std::exception)
     165             : {
     166           0 :     return mnType;
     167             : }
     168             : 
     169           0 : uno::Reference< excel::XRange > SAL_CALL ScVbaHyperlink::getRange() throw (uno::RuntimeException, std::exception)
     170             : {
     171           0 :     if( mnType == office::MsoHyperlinkType::msoHyperlinkRange )
     172             :     {
     173             :         // if constructed from Hyperlinks object, range has been passed as parent
     174           0 :         uno::Reference< excel::XRange > xAnchorRange( getParent(), uno::UNO_QUERY );
     175           0 :         if( !xAnchorRange.is() )
     176             :         {
     177             :             // if constructed via service c'tor, create new range based on cell
     178           0 :             uno::Reference< table::XCellRange > xRange( mxCell, uno::UNO_QUERY_THROW );
     179             :             // FIXME: need to pass current worksheet as the parent of XRange.
     180           0 :             xAnchorRange.set( new ScVbaRange( uno::Reference< XHelperInterface >(), mxContext, xRange ) );
     181             :         }
     182           0 :         return xAnchorRange;
     183             :     }
     184             :     // error if called at a shape Hyperlink object
     185           0 :     throw uno::RuntimeException();
     186             : }
     187             : 
     188           0 : uno::Reference< msforms::XShape > SAL_CALL ScVbaHyperlink::getShape() throw (uno::RuntimeException, std::exception)
     189             : {
     190             :     // error if called at a range Hyperlink object
     191           0 :     return uno::Reference< msforms::XShape >( getParent(), uno::UNO_QUERY_THROW );
     192             : }
     193             : 
     194           0 : VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaHyperlink, "ooo.vba.excel.Hyperlink" )
     195             : 
     196             : // private --------------------------------------------------------------------
     197             : 
     198           0 : void ScVbaHyperlink::ensureTextField() throw (uno::RuntimeException)
     199             : {
     200           0 :     if( !mxTextField.is() )
     201           0 :         throw uno::RuntimeException();
     202           0 : }
     203             : 
     204           0 : ScVbaHyperlink::UrlComponents ScVbaHyperlink::getUrlComponents() throw (uno::RuntimeException)
     205             : {
     206           0 :     ensureTextField();
     207           0 :     OUString aUrl;
     208           0 :     mxTextField->getPropertyValue("URL") >>= aUrl;
     209           0 :     sal_Int32 nHashPos = aUrl.indexOf( '#' );
     210           0 :     if( nHashPos < 0 )
     211           0 :         return UrlComponents( aUrl, OUString() );
     212           0 :     return UrlComponents( aUrl.copy( 0, nHashPos ), aUrl.copy( nHashPos + 1 ) );
     213             : }
     214             : 
     215           0 : void ScVbaHyperlink::setUrlComponents( const UrlComponents& rUrlComp ) throw (uno::RuntimeException)
     216             : {
     217           0 :     ensureTextField();
     218           0 :     OUStringBuffer aUrl( rUrlComp.first );
     219           0 :     if( !rUrlComp.second.isEmpty() )
     220           0 :         aUrl.append( '#' ).append( rUrlComp.second );
     221           0 :     mxTextField->setPropertyValue("URL", uno::Any( aUrl.makeStringAndClear() ) );
     222           0 : }
     223             : 
     224             : namespace hyperlink
     225             : {
     226             : namespace sdecl = comphelper::service_decl;
     227           3 : sdecl::vba_service_class_<ScVbaHyperlink, sdecl::with_args<true> > serviceImpl;
     228           3 : extern sdecl::ServiceDecl const serviceDecl(
     229             :     serviceImpl,
     230             :     "ScVbaHyperlink",
     231             :     "ooo.vba.excel.Hyperlink" );
     232           9 : }
     233             : 
     234             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11