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

Generated by: LCOV version 1.10