1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #include "comphelper/documentinfo.hxx"
32 #include "comphelper/namedvaluecollection.hxx"
34 /** === begin UNO includes === **/
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
37 #include <com/sun/star/document/XDocumentProperties.hpp>
38 #include <com/sun/star/frame/XStorable.hpp>
39 #include <com/sun/star/frame/XTitle.hpp>
40 /** === end UNO includes === **/
42 #include <cppuhelper/exc_hlp.hxx>
44 #include <osl/diagnose.h>
45 #include <osl/thread.h>
47 #include <boost/current_function.hpp>
49 //........................................................................
50 namespace comphelper
{
51 //........................................................................
53 /** === begin UNO using === **/
54 using ::com::sun::star::uno::Reference
;
55 using ::com::sun::star::uno::UNO_QUERY
;
56 using ::com::sun::star::uno::UNO_QUERY_THROW
;
57 using ::com::sun::star::uno::Exception
;
58 using ::com::sun::star::uno::RuntimeException
;
59 using ::com::sun::star::frame::XModel
;
60 using ::com::sun::star::frame::XTitle
;
61 using ::com::sun::star::frame::XController
;
62 using ::com::sun::star::beans::XPropertySet
;
63 using ::com::sun::star::document::XDocumentPropertiesSupplier
;
64 using ::com::sun::star::document::XDocumentProperties
;
65 using ::com::sun::star::frame::XStorable
;
66 using ::com::sun::star::beans::XPropertySetInfo
;
67 using ::com::sun::star::frame::XTitle
;
68 using ::com::sun::star::uno::XInterface
;
69 using ::com::sun::star::frame::XFrame
;
70 /** === end UNO using === **/
72 //====================================================================
74 //====================================================================
77 ::rtl::OUString
lcl_getTitle( const Reference
< XInterface
>& _rxComponent
)
79 Reference
< XTitle
> xTitle( _rxComponent
, UNO_QUERY
);
81 return xTitle
->getTitle();
82 return ::rtl::OUString();
86 //====================================================================
88 //====================================================================
89 //--------------------------------------------------------------------
90 ::rtl::OUString
DocumentInfo::getDocumentTitle( const Reference
< XModel
>& _rxDocument
)
92 ::rtl::OUString sTitle
;
94 if ( !_rxDocument
.is() )
97 ::rtl::OUString sDocURL
;
100 // 1. ask the model and the controller for their XTitle::getTitle
101 sTitle
= lcl_getTitle( _rxDocument
);
102 if ( sTitle
.getLength() )
105 Reference
< XController
> xController( _rxDocument
->getCurrentController() );
106 sTitle
= lcl_getTitle( xController
);
107 if ( sTitle
.getLength() )
110 // work around a problem with embedded objects, which sometimes return
111 // private:object as URL
112 sDocURL
= _rxDocument
->getURL();
113 if ( sDocURL
.matchAsciiL( "private:", 8 ) )
114 sDocURL
= ::rtl::OUString();
116 // 2. if the document is not saved, yet, check the frame title
117 if ( sDocURL
.getLength() == 0 )
119 Reference
< XFrame
> xFrame
;
120 if ( xController
.is() )
121 xFrame
.set( xController
->getFrame() );
122 sTitle
= lcl_getTitle( xFrame
);
123 if ( sTitle
.getLength() )
127 // 3. try the UNO DocumentInfo
128 Reference
< XDocumentPropertiesSupplier
> xDPS( _rxDocument
, UNO_QUERY
);
131 Reference
< XDocumentProperties
> xDocProps (
132 xDPS
->getDocumentProperties(), UNO_QUERY_THROW
);
133 OSL_ENSURE(xDocProps
.is(), "no DocumentProperties");
134 sTitle
= xDocProps
->getTitle();
135 if ( sTitle
.getLength() )
139 // 4. try model arguments
140 NamedValueCollection
aModelArgs( _rxDocument
->getArgs() );
141 sTitle
= aModelArgs
.getOrDefault( "Title", sTitle
);
142 if ( sTitle
.getLength() )
145 // 5. try the last segment of the document URL
146 // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DECODE_WITH_CHARSET ),
147 // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
148 // This heuristics here should be sufficient - finally, we will get an UNO title API in a not
149 // too distant future (hopefully), then this complete class is superfluous)
150 if ( sDocURL
.getLength() == 0 )
152 Reference
< XStorable
> xDocStorable( _rxDocument
, UNO_QUERY_THROW
);
153 sDocURL
= xDocStorable
->getLocation();
155 sal_Int32 nLastSepPos
= sDocURL
.lastIndexOf( '/' );
156 if ( ( nLastSepPos
!= -1 ) && ( nLastSepPos
== sDocURL
.getLength() - 1 ) )
158 sDocURL
= sDocURL
.copy( 0, nLastSepPos
);
159 nLastSepPos
= sDocURL
.lastIndexOf( '/' );
161 sTitle
= sDocURL
.copy( nLastSepPos
+ 1 );
163 if ( sTitle
.getLength() != 0 )
167 // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
169 Reference
< XTitle
> xTitle( _rxDocument
, UNO_QUERY
);
172 if ( xTitle
->getTitle().getLength() != 0 )
173 return xTitle
->getTitle();
176 catch ( const Exception
& )
178 ::com::sun::star::uno::Any
caught( ::cppu::getCaughtException() );
179 ::rtl::OString
sMessage( "caught an exception!" );
180 sMessage
+= "\ntype : ";
181 sMessage
+= ::rtl::OString( caught
.getValueTypeName().getStr(), caught
.getValueTypeName().getLength(), osl_getThreadTextEncoding() );
182 sMessage
+= "\nmessage: ";
183 ::com::sun::star::uno::Exception exception
;
184 caught
>>= exception
;
185 sMessage
+= ::rtl::OString( exception
.Message
.getStr(), exception
.Message
.getLength(), osl_getThreadTextEncoding() );
186 sMessage
+= "\nin function:\n";
187 sMessage
+= BOOST_CURRENT_FUNCTION
;
189 OSL_ENSURE( false, sMessage
);
195 //........................................................................
196 } // namespace comphelper
197 //........................................................................