Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "comphelper/documentinfo.hxx"
22 : #include "comphelper/namedvaluecollection.hxx"
23 :
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>
29 :
30 : #include <cppuhelper/exc_hlp.hxx>
31 :
32 : #include <osl/diagnose.h>
33 : #include <osl/thread.h>
34 :
35 : #include <boost/current_function.hpp>
36 :
37 : //........................................................................
38 : namespace comphelper {
39 : //........................................................................
40 :
41 : /** === begin UNO using === **/
42 : using ::com::sun::star::uno::Reference;
43 : using ::com::sun::star::uno::UNO_QUERY;
44 : using ::com::sun::star::uno::UNO_QUERY_THROW;
45 : using ::com::sun::star::uno::Exception;
46 : using ::com::sun::star::uno::RuntimeException;
47 : using ::com::sun::star::frame::XModel;
48 : using ::com::sun::star::frame::XTitle;
49 : using ::com::sun::star::frame::XController;
50 : using ::com::sun::star::beans::XPropertySet;
51 : using ::com::sun::star::document::XDocumentPropertiesSupplier;
52 : using ::com::sun::star::document::XDocumentProperties;
53 : using ::com::sun::star::frame::XStorable;
54 : using ::com::sun::star::beans::XPropertySetInfo;
55 : using ::com::sun::star::frame::XTitle;
56 : using ::com::sun::star::uno::XInterface;
57 : using ::com::sun::star::frame::XFrame;
58 : /** === end UNO using === **/
59 :
60 : //====================================================================
61 : //= helper
62 : //====================================================================
63 : namespace
64 : {
65 343 : ::rtl::OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
66 : {
67 343 : Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
68 343 : if ( xTitle.is() )
69 343 : return xTitle->getTitle();
70 0 : return ::rtl::OUString();
71 : }
72 : }
73 :
74 : //====================================================================
75 : //= DocumentInfo
76 : //====================================================================
77 : //--------------------------------------------------------------------
78 343 : ::rtl::OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
79 : {
80 343 : ::rtl::OUString sTitle;
81 :
82 343 : if ( !_rxDocument.is() )
83 0 : return sTitle;
84 :
85 343 : ::rtl::OUString sDocURL;
86 : try
87 : {
88 : // 1. ask the model and the controller for their XTitle::getTitle
89 343 : sTitle = lcl_getTitle( _rxDocument );
90 343 : if ( !sTitle.isEmpty() )
91 343 : return sTitle;
92 :
93 0 : Reference< XController > xController( _rxDocument->getCurrentController() );
94 0 : sTitle = lcl_getTitle( xController );
95 0 : if ( !sTitle.isEmpty() )
96 0 : return sTitle;
97 :
98 : // work around a problem with embedded objects, which sometimes return
99 : // private:object as URL
100 0 : sDocURL = _rxDocument->getURL();
101 0 : if ( sDocURL.matchAsciiL( "private:", 8 ) )
102 0 : sDocURL = ::rtl::OUString();
103 :
104 : // 2. if the document is not saved, yet, check the frame title
105 0 : if ( sDocURL.isEmpty() )
106 : {
107 0 : Reference< XFrame > xFrame;
108 0 : if ( xController.is() )
109 0 : xFrame.set( xController->getFrame() );
110 0 : sTitle = lcl_getTitle( xFrame );
111 0 : if ( !sTitle.isEmpty() )
112 0 : return sTitle;
113 : }
114 :
115 : // 3. try the UNO XDocumentProperties
116 0 : Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
117 0 : if ( xDPS.is() )
118 : {
119 : Reference< XDocumentProperties > xDocProps (
120 0 : xDPS->getDocumentProperties(), UNO_QUERY_THROW );
121 : OSL_ENSURE(xDocProps.is(), "no DocumentProperties");
122 0 : sTitle = xDocProps->getTitle();
123 0 : if ( !sTitle.isEmpty() )
124 0 : return sTitle;
125 : }
126 :
127 : // 4. try model arguments
128 0 : NamedValueCollection aModelArgs( _rxDocument->getArgs() );
129 0 : sTitle = aModelArgs.getOrDefault( "Title", sTitle );
130 0 : if ( !sTitle.isEmpty() )
131 0 : return sTitle;
132 :
133 : // 5. try the last segment of the document URL
134 : // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DECODE_WITH_CHARSET ),
135 : // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
136 : // This heuristics here should be sufficient - finally, we will get an UNO title API in a not
137 : // too distant future (hopefully), then this complete class is superfluous)
138 0 : if ( sDocURL.isEmpty() )
139 : {
140 0 : Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
141 0 : sDocURL = xDocStorable->getLocation();
142 : }
143 0 : sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
144 0 : if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
145 : {
146 0 : sDocURL = sDocURL.copy( 0, nLastSepPos );
147 0 : nLastSepPos = sDocURL.lastIndexOf( '/' );
148 : }
149 0 : sTitle = sDocURL.copy( nLastSepPos + 1 );
150 :
151 0 : if ( !sTitle.isEmpty() )
152 0 : return sTitle;
153 :
154 : // 5.
155 : // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
156 :
157 0 : Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
158 0 : if ( xTitle.is() )
159 : {
160 0 : if ( !xTitle->getTitle().isEmpty() )
161 0 : return xTitle->getTitle();
162 0 : }
163 : }
164 0 : catch ( const Exception& )
165 : {
166 0 : ::com::sun::star::uno::Any caught( ::cppu::getCaughtException() );
167 0 : ::rtl::OString sMessage( "caught an exception!" );
168 0 : sMessage += "\ntype : ";
169 0 : sMessage += ::rtl::OString( caught.getValueTypeName().getStr(), caught.getValueTypeName().getLength(), osl_getThreadTextEncoding() );
170 0 : sMessage += "\nmessage: ";
171 0 : ::com::sun::star::uno::Exception exception;
172 0 : caught >>= exception;
173 0 : sMessage += ::rtl::OString( exception.Message.getStr(), exception.Message.getLength(), osl_getThreadTextEncoding() );
174 0 : sMessage += "\nin function:\n";
175 0 : sMessage += BOOST_CURRENT_FUNCTION;
176 0 : sMessage += "\n";
177 0 : OSL_FAIL( sMessage.getStr() );
178 : }
179 :
180 0 : return sTitle;
181 : }
182 :
183 : //........................................................................
184 : } // namespace comphelper
185 : //........................................................................
186 :
187 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|