merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / misc / documentinfo.cxx
blobb66a73d5c7f199f540a61380bbb0e34f080d289d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: documentinfo.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper/documentinfo.hxx"
35 #include "comphelper/namedvaluecollection.hxx"
37 /** === begin UNO includes === **/
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
40 #include <com/sun/star/document/XDocumentProperties.hpp>
41 #include <com/sun/star/frame/XStorable.hpp>
42 #include <com/sun/star/frame/XTitle.hpp>
43 /** === end UNO includes === **/
45 #include <cppuhelper/exc_hlp.hxx>
47 #include <osl/diagnose.h>
48 #include <osl/thread.h>
50 #include <boost/current_function.hpp>
52 //........................................................................
53 namespace comphelper {
54 //........................................................................
56 /** === begin UNO using === **/
57 using ::com::sun::star::uno::Reference;
58 using ::com::sun::star::uno::UNO_QUERY;
59 using ::com::sun::star::uno::UNO_QUERY_THROW;
60 using ::com::sun::star::uno::Exception;
61 using ::com::sun::star::uno::RuntimeException;
62 using ::com::sun::star::frame::XModel;
63 using ::com::sun::star::frame::XTitle;
64 using ::com::sun::star::frame::XController;
65 using ::com::sun::star::beans::XPropertySet;
66 using ::com::sun::star::document::XDocumentPropertiesSupplier;
67 using ::com::sun::star::document::XDocumentProperties;
68 using ::com::sun::star::frame::XStorable;
69 using ::com::sun::star::beans::XPropertySetInfo;
70 using ::com::sun::star::frame::XTitle;
71 using ::com::sun::star::uno::XInterface;
72 using ::com::sun::star::frame::XFrame;
73 /** === end UNO using === **/
75 //====================================================================
76 //= helper
77 //====================================================================
78 namespace
80 ::rtl::OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
82 Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
83 if ( xTitle.is() )
84 return xTitle->getTitle();
85 return ::rtl::OUString();
89 //====================================================================
90 //= DocumentInfo
91 //====================================================================
92 //--------------------------------------------------------------------
93 ::rtl::OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
95 ::rtl::OUString sTitle;
97 if ( !_rxDocument.is() )
98 return sTitle;
100 ::rtl::OUString sDocURL;
103 // 1. ask the model and the controller for their XTitle::getTitle
104 sTitle = lcl_getTitle( _rxDocument );
105 if ( sTitle.getLength() )
106 return sTitle;
108 Reference< XController > xController( _rxDocument->getCurrentController() );
109 sTitle = lcl_getTitle( xController );
110 if ( sTitle.getLength() )
111 return sTitle;
113 // work around a problem with embedded objects, which sometimes return
114 // private:object as URL
115 sDocURL = _rxDocument->getURL();
116 if ( sDocURL.matchAsciiL( "private:", 8 ) )
117 sDocURL = ::rtl::OUString();
119 // 2. if the document is not saved, yet, check the frame title
120 if ( sDocURL.getLength() == 0 )
122 Reference< XFrame > xFrame;
123 if ( xController.is() )
124 xFrame.set( xController->getFrame() );
125 sTitle = lcl_getTitle( xFrame );
126 if ( sTitle.getLength() )
127 return sTitle;
130 // 3. try the UNO DocumentInfo
131 Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
132 if ( xDPS.is() )
134 Reference< XDocumentProperties > xDocProps (
135 xDPS->getDocumentProperties(), UNO_QUERY_THROW );
136 OSL_ENSURE(xDocProps.is(), "no DocumentProperties");
137 sTitle = xDocProps->getTitle();
138 if ( sTitle.getLength() )
139 return sTitle;
142 // 4. try model arguments
143 NamedValueCollection aModelArgs( _rxDocument->getArgs() );
144 sTitle = aModelArgs.getOrDefault( "Title", sTitle );
145 if ( sTitle.getLength() )
146 return sTitle;
148 // 5. try the last segment of the document URL
149 // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DECODE_WITH_CHARSET ),
150 // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
151 // This heuristics here should be sufficient - finally, we will get an UNO title API in a not
152 // too distant future (hopefully), then this complete class is superfluous)
153 if ( sDocURL.getLength() == 0 )
155 Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
156 sDocURL = xDocStorable->getLocation();
158 sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
159 if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
161 sDocURL = sDocURL.copy( 0, nLastSepPos );
162 nLastSepPos = sDocURL.lastIndexOf( '/' );
164 sTitle = sDocURL.copy( nLastSepPos + 1 );
166 if ( sTitle.getLength() != 0 )
167 return sTitle;
169 // 5.
170 // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
172 Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
173 if ( xTitle.is() )
175 if ( xTitle->getTitle().getLength() != 0 )
176 return xTitle->getTitle();
179 catch ( const Exception& )
181 ::com::sun::star::uno::Any caught( ::cppu::getCaughtException() );
182 ::rtl::OString sMessage( "caught an exception!" );
183 sMessage += "\ntype : ";
184 sMessage += ::rtl::OString( caught.getValueTypeName().getStr(), caught.getValueTypeName().getLength(), osl_getThreadTextEncoding() );
185 sMessage += "\nmessage: ";
186 ::com::sun::star::uno::Exception exception;
187 caught >>= exception;
188 sMessage += ::rtl::OString( exception.Message.getStr(), exception.Message.getLength(), osl_getThreadTextEncoding() );
189 sMessage += "\nin function:\n";
190 sMessage += BOOST_CURRENT_FUNCTION;
191 sMessage += "\n";
192 OSL_ENSURE( false, sMessage );
195 return sTitle;
198 //........................................................................
199 } // namespace comphelper
200 //........................................................................