1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include "comphelper/documentinfo.hxx"
32 #include "comphelper/namedvaluecollection.hxx"
33 
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 === **/
41 
42 #include <cppuhelper/exc_hlp.hxx>
43 
44 #include <osl/diagnose.h>
45 #include <osl/thread.h>
46 
47 #include <boost/current_function.hpp>
48 
49 //........................................................................
50 namespace comphelper {
51 //........................................................................
52 
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 === **/
71 
72 	//====================================================================
73 	//= helper
74 	//====================================================================
75     namespace
76     {
77         ::rtl::OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
78         {
79             Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
80             if ( xTitle.is() )
81                 return xTitle->getTitle();
82             return ::rtl::OUString();
83         }
84     }
85 
86     //====================================================================
87 	//= DocumentInfo
88 	//====================================================================
89 	//--------------------------------------------------------------------
90     ::rtl::OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
91     {
92         ::rtl::OUString sTitle;
93 
94 	    if ( !_rxDocument.is() )
95             return sTitle;
96 
97         ::rtl::OUString sDocURL;
98 	    try
99 	    {
100             // 1. ask the model and the controller for their XTitle::getTitle
101             sTitle = lcl_getTitle( _rxDocument );
102             if ( sTitle.getLength() )
103                 return sTitle;
104 
105             Reference< XController > xController( _rxDocument->getCurrentController() );
106             sTitle = lcl_getTitle( xController );
107             if ( sTitle.getLength() )
108                 return sTitle;
109 
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();
115 
116             // 2. if the document is not saved, yet, check the frame title
117             if ( sDocURL.getLength() == 0 )
118             {
119                 Reference< XFrame > xFrame;
120                 if ( xController.is() )
121                     xFrame.set( xController->getFrame() );
122                 sTitle = lcl_getTitle( xFrame );
123                 if ( sTitle.getLength() )
124                     return sTitle;
125             }
126 
127             // 3. try the UNO DocumentInfo
128 			Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
129 			if ( xDPS.is() )
130 			{
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() )
136                     return sTitle;
137 			}
138 
139             // 4. try model arguments
140             NamedValueCollection aModelArgs( _rxDocument->getArgs() );
141             sTitle = aModelArgs.getOrDefault( "Title", sTitle );
142             if ( sTitle.getLength() )
143                 return sTitle;
144 
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 )
151             {
152                 Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
153                 sDocURL = xDocStorable->getLocation();
154             }
155             sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
156             if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
157             {
158                 sDocURL = sDocURL.copy( 0, nLastSepPos );
159                 nLastSepPos = sDocURL.lastIndexOf( '/' );
160             }
161             sTitle = sDocURL.copy( nLastSepPos + 1 );
162 
163 			if ( sTitle.getLength() != 0 )
164                 return sTitle;
165 
166 			// 5.
167 			// <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
168 
169 		    Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
170 			if ( xTitle.is() )
171 			{
172 				if ( xTitle->getTitle().getLength() != 0 )
173 					return xTitle->getTitle();
174 			}
175 	    }
176 	    catch ( const Exception& )
177 	    {
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;
188             sMessage += "\n";
189 	        OSL_ENSURE( false, sMessage );
190 	    }
191 
192         return sTitle;
193     }
194 
195 //........................................................................
196 } // namespace comphelper
197 //........................................................................
198