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