LCOV - code coverage report
Current view: top level - comphelper/source/misc - documentinfo.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 12 66 18.2 %
Date: 2015-06-13 12:38:46 Functions: 2 2 100.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             : 
      21             : #include <comphelper/documentinfo.hxx>
      22             : #include <comphelper/namedvaluecollection.hxx>
      23             : 
      24             : #include <com/sun/star/beans/XPropertySet.hpp>
      25             : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
      26             : #include <com/sun/star/document/XDocumentProperties.hpp>
      27             : #include <com/sun/star/frame/XStorable.hpp>
      28             : #include <com/sun/star/frame/XTitle.hpp>
      29             : 
      30             : #include <cppuhelper/exc_hlp.hxx>
      31             : 
      32             : #include <osl/diagnose.h>
      33             : #include <osl/thread.h>
      34             : 
      35             : #include <boost/current_function.hpp>
      36             : 
      37             : 
      38             : namespace comphelper {
      39             : 
      40             : 
      41             :     using ::com::sun::star::uno::Reference;
      42             :     using ::com::sun::star::uno::UNO_QUERY;
      43             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      44             :     using ::com::sun::star::uno::Exception;
      45             :     using ::com::sun::star::uno::RuntimeException;
      46             :     using ::com::sun::star::frame::XModel;
      47             :     using ::com::sun::star::frame::XTitle;
      48             :     using ::com::sun::star::frame::XController;
      49             :     using ::com::sun::star::beans::XPropertySet;
      50             :     using ::com::sun::star::document::XDocumentPropertiesSupplier;
      51             :     using ::com::sun::star::document::XDocumentProperties;
      52             :     using ::com::sun::star::frame::XStorable;
      53             :     using ::com::sun::star::beans::XPropertySetInfo;
      54             :     using ::com::sun::star::uno::XInterface;
      55             :     using ::com::sun::star::frame::XFrame;
      56             : 
      57             :     namespace
      58             :     {
      59        5640 :         OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
      60             :         {
      61        5640 :             Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
      62        5640 :             if ( xTitle.is() )
      63        5640 :                 return xTitle->getTitle();
      64           0 :             return OUString();
      65             :         }
      66             :     }
      67             : 
      68        5640 :     OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
      69             :     {
      70        5640 :         OUString sTitle;
      71             : 
      72        5640 :         if ( !_rxDocument.is() )
      73           0 :             return sTitle;
      74             : 
      75       11280 :         OUString sDocURL;
      76             :         try
      77             :         {
      78             :             // 1. ask the model and the controller for their XTitle::getTitle
      79        5640 :             sTitle = lcl_getTitle( _rxDocument );
      80        5640 :             if ( !sTitle.isEmpty() )
      81       11280 :                 return sTitle;
      82             : 
      83           0 :             Reference< XController > xController( _rxDocument->getCurrentController() );
      84           0 :             sTitle = lcl_getTitle( xController );
      85           0 :             if ( !sTitle.isEmpty() )
      86           0 :                 return sTitle;
      87             : 
      88             :             // work around a problem with embedded objects, which sometimes return
      89             :             // private:object as URL
      90           0 :             sDocURL = _rxDocument->getURL();
      91           0 :             if ( sDocURL.startsWithIgnoreAsciiCase( "private:" ) )
      92           0 :                 sDocURL.clear();
      93             : 
      94             :             // 2. if the document is not saved, yet, check the frame title
      95           0 :             if ( sDocURL.isEmpty() )
      96             :             {
      97           0 :                 Reference< XFrame > xFrame;
      98           0 :                 if ( xController.is() )
      99           0 :                     xFrame.set( xController->getFrame() );
     100           0 :                 sTitle = lcl_getTitle( xFrame );
     101           0 :                 if ( !sTitle.isEmpty() )
     102           0 :                     return sTitle;
     103             :             }
     104             : 
     105             :             // 3. try the UNO XDocumentProperties
     106           0 :             Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
     107           0 :             if ( xDPS.is() )
     108             :             {
     109             :                 Reference< XDocumentProperties > xDocProps (
     110           0 :                     xDPS->getDocumentProperties(), UNO_QUERY_THROW );
     111             :                 OSL_ENSURE(xDocProps.is(), "no DocumentProperties");
     112           0 :                 sTitle = xDocProps->getTitle();
     113           0 :                 if ( !sTitle.isEmpty() )
     114           0 :                     return sTitle;
     115             :             }
     116             : 
     117             :             // 4. try model arguments
     118           0 :             NamedValueCollection aModelArgs( _rxDocument->getArgs() );
     119           0 :             sTitle = aModelArgs.getOrDefault( "Title", sTitle );
     120           0 :             if ( !sTitle.isEmpty() )
     121           0 :                 return sTitle;
     122             : 
     123             :             // 5. try the last segment of the document URL
     124             :             // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DECODE_WITH_CHARSET ),
     125             :             // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
     126             :             // This heuristics here should be sufficient - finally, we will get an UNO title API in a not
     127             :             // too distant future (hopefully), then  this complete class is superfluous)
     128           0 :             if ( sDocURL.isEmpty() )
     129             :             {
     130           0 :                 Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
     131           0 :                 sDocURL = xDocStorable->getLocation();
     132             :             }
     133           0 :             sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
     134           0 :             if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
     135             :             {
     136           0 :                 sDocURL = sDocURL.copy( 0, nLastSepPos );
     137           0 :                 nLastSepPos = sDocURL.lastIndexOf( '/' );
     138             :             }
     139           0 :             sTitle = sDocURL.copy( nLastSepPos + 1 );
     140             : 
     141           0 :             if ( !sTitle.isEmpty() )
     142           0 :                 return sTitle;
     143             : 
     144             :             // 5.
     145             :             // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
     146             : 
     147           0 :             Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
     148           0 :             if ( xTitle.is() )
     149             :             {
     150           0 :                 if ( !xTitle->getTitle().isEmpty() )
     151           0 :                     return xTitle->getTitle();
     152           0 :             }
     153             :         }
     154           0 :         catch ( const Exception& )
     155             :         {
     156           0 :             ::com::sun::star::uno::Any caught( ::cppu::getCaughtException() );
     157           0 :             OString sMessage( "caught an exception!" );
     158           0 :             sMessage += "\ntype   : ";
     159           0 :             sMessage += OString( caught.getValueTypeName().getStr(), caught.getValueTypeName().getLength(), osl_getThreadTextEncoding() );
     160           0 :             sMessage += "\nmessage: ";
     161           0 :             ::com::sun::star::uno::Exception exception;
     162           0 :             caught >>= exception;
     163           0 :             sMessage += OString( exception.Message.getStr(), exception.Message.getLength(), osl_getThreadTextEncoding() );
     164           0 :             sMessage += "\nin function:\n";
     165           0 :             sMessage += BOOST_CURRENT_FUNCTION;
     166           0 :             sMessage += "\n";
     167           0 :             OSL_FAIL( sMessage.getStr() );
     168             :         }
     169             : 
     170        5640 :         return sTitle;
     171             :     }
     172             : 
     173             : 
     174             : } // namespace comphelper
     175             : 
     176             : 
     177             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11