1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "comphelper/documentinfo.hxx"
22 #include "comphelper/namedvaluecollection.hxx"
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>
30 #include <cppuhelper/exc_hlp.hxx>
32 #include <osl/diagnose.h>
33 #include <osl/thread.h>
35 #include <boost/current_function.hpp>
37 //........................................................................
38 namespace comphelper
{
39 //........................................................................
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
;
57 //====================================================================
59 //====================================================================
62 OUString
lcl_getTitle( const Reference
< XInterface
>& _rxComponent
)
64 Reference
< XTitle
> xTitle( _rxComponent
, UNO_QUERY
);
66 return xTitle
->getTitle();
71 //====================================================================
73 //====================================================================
74 //--------------------------------------------------------------------
75 OUString
DocumentInfo::getDocumentTitle( const Reference
< XModel
>& _rxDocument
)
79 if ( !_rxDocument
.is() )
85 // 1. ask the model and the controller for their XTitle::getTitle
86 sTitle
= lcl_getTitle( _rxDocument
);
87 if ( !sTitle
.isEmpty() )
90 Reference
< XController
> xController( _rxDocument
->getCurrentController() );
91 sTitle
= lcl_getTitle( xController
);
92 if ( !sTitle
.isEmpty() )
95 // work around a problem with embedded objects, which sometimes return
96 // private:object as URL
97 sDocURL
= _rxDocument
->getURL();
98 if ( sDocURL
.matchAsciiL( "private:", 8 ) )
101 // 2. if the document is not saved, yet, check the frame title
102 if ( sDocURL
.isEmpty() )
104 Reference
< XFrame
> xFrame
;
105 if ( xController
.is() )
106 xFrame
.set( xController
->getFrame() );
107 sTitle
= lcl_getTitle( xFrame
);
108 if ( !sTitle
.isEmpty() )
112 // 3. try the UNO XDocumentProperties
113 Reference
< XDocumentPropertiesSupplier
> xDPS( _rxDocument
, UNO_QUERY
);
116 Reference
< XDocumentProperties
> xDocProps (
117 xDPS
->getDocumentProperties(), UNO_QUERY_THROW
);
118 OSL_ENSURE(xDocProps
.is(), "no DocumentProperties");
119 sTitle
= xDocProps
->getTitle();
120 if ( !sTitle
.isEmpty() )
124 // 4. try model arguments
125 NamedValueCollection
aModelArgs( _rxDocument
->getArgs() );
126 sTitle
= aModelArgs
.getOrDefault( "Title", sTitle
);
127 if ( !sTitle
.isEmpty() )
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 if ( sDocURL
.isEmpty() )
137 Reference
< XStorable
> xDocStorable( _rxDocument
, UNO_QUERY_THROW
);
138 sDocURL
= xDocStorable
->getLocation();
140 sal_Int32 nLastSepPos
= sDocURL
.lastIndexOf( '/' );
141 if ( ( nLastSepPos
!= -1 ) && ( nLastSepPos
== sDocURL
.getLength() - 1 ) )
143 sDocURL
= sDocURL
.copy( 0, nLastSepPos
);
144 nLastSepPos
= sDocURL
.lastIndexOf( '/' );
146 sTitle
= sDocURL
.copy( nLastSepPos
+ 1 );
148 if ( !sTitle
.isEmpty() )
152 // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
154 Reference
< XTitle
> xTitle( _rxDocument
, UNO_QUERY
);
157 if ( !xTitle
->getTitle().isEmpty() )
158 return xTitle
->getTitle();
161 catch ( const Exception
& )
163 ::com::sun::star::uno::Any
caught( ::cppu::getCaughtException() );
164 OString
sMessage( "caught an exception!" );
165 sMessage
+= "\ntype : ";
166 sMessage
+= OString( caught
.getValueTypeName().getStr(), caught
.getValueTypeName().getLength(), osl_getThreadTextEncoding() );
167 sMessage
+= "\nmessage: ";
168 ::com::sun::star::uno::Exception exception
;
169 caught
>>= exception
;
170 sMessage
+= OString( exception
.Message
.getStr(), exception
.Message
.getLength(), osl_getThreadTextEncoding() );
171 sMessage
+= "\nin function:\n";
172 sMessage
+= BOOST_CURRENT_FUNCTION
;
174 OSL_FAIL( sMessage
.getStr() );
180 //........................................................................
181 } // namespace comphelper
182 //........................................................................
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */