1dde7d3faSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file 5dde7d3faSAndrew Rist * distributed with this work for additional information 6dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the 8dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance 9dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at 10dde7d3faSAndrew Rist * 11dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12dde7d3faSAndrew Rist * 13dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing, 14dde7d3faSAndrew Rist * software distributed under the License is distributed on an 15dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the 17dde7d3faSAndrew Rist * specific language governing permissions and limitations 18dde7d3faSAndrew Rist * under the License. 19dde7d3faSAndrew Rist * 20dde7d3faSAndrew Rist *************************************************************/ 21dde7d3faSAndrew Rist 22dde7d3faSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "comphelper/documentinfo.hxx" 28cdf0e10cSrcweir #include "comphelper/namedvaluecollection.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** === begin UNO includes === **/ 31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 32cdf0e10cSrcweir #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp> 33cdf0e10cSrcweir #include <com/sun/star/document/XDocumentProperties.hpp> 34cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp> 35cdf0e10cSrcweir #include <com/sun/star/frame/XTitle.hpp> 36cdf0e10cSrcweir /** === end UNO includes === **/ 37cdf0e10cSrcweir 38cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx> 39cdf0e10cSrcweir 40cdf0e10cSrcweir #include <osl/diagnose.h> 41cdf0e10cSrcweir #include <osl/thread.h> 42cdf0e10cSrcweir 43cdf0e10cSrcweir #include <boost/current_function.hpp> 44cdf0e10cSrcweir 45cdf0e10cSrcweir //........................................................................ 46cdf0e10cSrcweir namespace comphelper { 47cdf0e10cSrcweir //........................................................................ 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** === begin UNO using === **/ 50cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 51cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 52cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 53cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 54cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 55cdf0e10cSrcweir using ::com::sun::star::frame::XModel; 56cdf0e10cSrcweir using ::com::sun::star::frame::XTitle; 57cdf0e10cSrcweir using ::com::sun::star::frame::XController; 58cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet; 59cdf0e10cSrcweir using ::com::sun::star::document::XDocumentPropertiesSupplier; 60cdf0e10cSrcweir using ::com::sun::star::document::XDocumentProperties; 61cdf0e10cSrcweir using ::com::sun::star::frame::XStorable; 62cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySetInfo; 63cdf0e10cSrcweir using ::com::sun::star::frame::XTitle; 64cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 65cdf0e10cSrcweir using ::com::sun::star::frame::XFrame; 66cdf0e10cSrcweir /** === end UNO using === **/ 67cdf0e10cSrcweir 68cdf0e10cSrcweir //==================================================================== 69cdf0e10cSrcweir //= helper 70cdf0e10cSrcweir //==================================================================== 71cdf0e10cSrcweir namespace 72cdf0e10cSrcweir { lcl_getTitle(const Reference<XInterface> & _rxComponent)73cdf0e10cSrcweir ::rtl::OUString lcl_getTitle( const Reference< XInterface >& _rxComponent ) 74cdf0e10cSrcweir { 75cdf0e10cSrcweir Reference< XTitle > xTitle( _rxComponent, UNO_QUERY ); 76cdf0e10cSrcweir if ( xTitle.is() ) 77cdf0e10cSrcweir return xTitle->getTitle(); 78cdf0e10cSrcweir return ::rtl::OUString(); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir //==================================================================== 83cdf0e10cSrcweir //= DocumentInfo 84cdf0e10cSrcweir //==================================================================== 85cdf0e10cSrcweir //-------------------------------------------------------------------- getDocumentTitle(const Reference<XModel> & _rxDocument)86cdf0e10cSrcweir ::rtl::OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument ) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir ::rtl::OUString sTitle; 89cdf0e10cSrcweir 90cdf0e10cSrcweir if ( !_rxDocument.is() ) 91cdf0e10cSrcweir return sTitle; 92cdf0e10cSrcweir 93cdf0e10cSrcweir ::rtl::OUString sDocURL; 94cdf0e10cSrcweir try 95cdf0e10cSrcweir { 96cdf0e10cSrcweir // 1. ask the model and the controller for their XTitle::getTitle 97cdf0e10cSrcweir sTitle = lcl_getTitle( _rxDocument ); 98*49b34792SHerbert Dürr if ( !sTitle.isEmpty() ) 99cdf0e10cSrcweir return sTitle; 100cdf0e10cSrcweir 101cdf0e10cSrcweir Reference< XController > xController( _rxDocument->getCurrentController() ); 102cdf0e10cSrcweir sTitle = lcl_getTitle( xController ); 103*49b34792SHerbert Dürr if ( !sTitle.isEmpty() ) 104cdf0e10cSrcweir return sTitle; 105cdf0e10cSrcweir 106cdf0e10cSrcweir // work around a problem with embedded objects, which sometimes return 107cdf0e10cSrcweir // private:object as URL 108cdf0e10cSrcweir sDocURL = _rxDocument->getURL(); 109cdf0e10cSrcweir if ( sDocURL.matchAsciiL( "private:", 8 ) ) 110cdf0e10cSrcweir sDocURL = ::rtl::OUString(); 111cdf0e10cSrcweir 112cdf0e10cSrcweir // 2. if the document is not saved, yet, check the frame title 113*49b34792SHerbert Dürr if ( sDocURL.isEmpty() ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir Reference< XFrame > xFrame; 116cdf0e10cSrcweir if ( xController.is() ) 117cdf0e10cSrcweir xFrame.set( xController->getFrame() ); 118cdf0e10cSrcweir sTitle = lcl_getTitle( xFrame ); 119*49b34792SHerbert Dürr if ( !sTitle.isEmpty() ) 120cdf0e10cSrcweir return sTitle; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir // 3. try the UNO DocumentInfo 124cdf0e10cSrcweir Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY ); 125cdf0e10cSrcweir if ( xDPS.is() ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir Reference< XDocumentProperties > xDocProps ( 128cdf0e10cSrcweir xDPS->getDocumentProperties(), UNO_QUERY_THROW ); 129cdf0e10cSrcweir OSL_ENSURE(xDocProps.is(), "no DocumentProperties"); 130cdf0e10cSrcweir sTitle = xDocProps->getTitle(); 131*49b34792SHerbert Dürr if ( !sTitle.isEmpty() ) 132cdf0e10cSrcweir return sTitle; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir // 4. try model arguments 136cdf0e10cSrcweir NamedValueCollection aModelArgs( _rxDocument->getArgs() ); 137cdf0e10cSrcweir sTitle = aModelArgs.getOrDefault( "Title", sTitle ); 138*49b34792SHerbert Dürr if ( !sTitle.isEmpty() ) 139cdf0e10cSrcweir return sTitle; 140cdf0e10cSrcweir 141cdf0e10cSrcweir // 5. try the last segment of the document URL 142cdf0e10cSrcweir // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DECODE_WITH_CHARSET ), 143cdf0e10cSrcweir // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore 144cdf0e10cSrcweir // This heuristics here should be sufficient - finally, we will get an UNO title API in a not 145cdf0e10cSrcweir // too distant future (hopefully), then this complete class is superfluous) 146*49b34792SHerbert Dürr if ( sDocURL.isEmpty() ) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW ); 149cdf0e10cSrcweir sDocURL = xDocStorable->getLocation(); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' ); 152cdf0e10cSrcweir if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) ) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir sDocURL = sDocURL.copy( 0, nLastSepPos ); 155cdf0e10cSrcweir nLastSepPos = sDocURL.lastIndexOf( '/' ); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir sTitle = sDocURL.copy( nLastSepPos + 1 ); 158cdf0e10cSrcweir 159*49b34792SHerbert Dürr if ( !sTitle.isEmpty() ) 160cdf0e10cSrcweir return sTitle; 161cdf0e10cSrcweir 162cdf0e10cSrcweir // 5. 163cdf0e10cSrcweir // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title --> 164cdf0e10cSrcweir 165cdf0e10cSrcweir Reference< XTitle > xTitle( _rxDocument, UNO_QUERY ); 166cdf0e10cSrcweir if ( xTitle.is() ) 167cdf0e10cSrcweir { 168*49b34792SHerbert Dürr if ( !xTitle->getTitle().isEmpty() ) 169cdf0e10cSrcweir return xTitle->getTitle(); 170cdf0e10cSrcweir } 171cdf0e10cSrcweir } 172cdf0e10cSrcweir catch ( const Exception& ) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir ::com::sun::star::uno::Any caught( ::cppu::getCaughtException() ); 175cdf0e10cSrcweir ::rtl::OString sMessage( "caught an exception!" ); 176cdf0e10cSrcweir sMessage += "\ntype : "; 177cdf0e10cSrcweir sMessage += ::rtl::OString( caught.getValueTypeName().getStr(), caught.getValueTypeName().getLength(), osl_getThreadTextEncoding() ); 178cdf0e10cSrcweir sMessage += "\nmessage: "; 179cdf0e10cSrcweir ::com::sun::star::uno::Exception exception; 180cdf0e10cSrcweir caught >>= exception; 181cdf0e10cSrcweir sMessage += ::rtl::OString( exception.Message.getStr(), exception.Message.getLength(), osl_getThreadTextEncoding() ); 182cdf0e10cSrcweir sMessage += "\nin function:\n"; 183cdf0e10cSrcweir sMessage += BOOST_CURRENT_FUNCTION; 184cdf0e10cSrcweir sMessage += "\n"; 185cdf0e10cSrcweir OSL_ENSURE( false, sMessage ); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir return sTitle; 189cdf0e10cSrcweir } 190cdf0e10cSrcweir 191cdf0e10cSrcweir //........................................................................ 192cdf0e10cSrcweir } // namespace comphelper 193cdf0e10cSrcweir //........................................................................ 194