bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / documentinfo.cxx
blob9a75a6fbf85bc702d41e461d06c31593d160d406
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
38 namespace comphelper {
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 namespace
59 OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
61 Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
62 if ( xTitle.is() )
63 return xTitle->getTitle();
64 return OUString();
68 OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
70 OUString sTitle;
72 if ( !_rxDocument.is() )
73 return sTitle;
75 OUString sDocURL;
76 try
78 // 1. ask the model and the controller for their XTitle::getTitle
79 sTitle = lcl_getTitle( _rxDocument );
80 if ( !sTitle.isEmpty() )
81 return sTitle;
83 Reference< XController > xController( _rxDocument->getCurrentController() );
84 sTitle = lcl_getTitle( xController );
85 if ( !sTitle.isEmpty() )
86 return sTitle;
88 // work around a problem with embedded objects, which sometimes return
89 // private:object as URL
90 sDocURL = _rxDocument->getURL();
91 if ( sDocURL.startsWithIgnoreAsciiCase( "private:" ) )
92 sDocURL.clear();
94 // 2. if the document is not saved, yet, check the frame title
95 if ( sDocURL.isEmpty() )
97 Reference< XFrame > xFrame;
98 if ( xController.is() )
99 xFrame.set( xController->getFrame() );
100 sTitle = lcl_getTitle( xFrame );
101 if ( !sTitle.isEmpty() )
102 return sTitle;
105 // 3. try the UNO XDocumentProperties
106 Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
107 if ( xDPS.is() )
109 Reference< XDocumentProperties > xDocProps (
110 xDPS->getDocumentProperties(), UNO_QUERY_THROW );
111 OSL_ENSURE(xDocProps.is(), "no DocumentProperties");
112 sTitle = xDocProps->getTitle();
113 if ( !sTitle.isEmpty() )
114 return sTitle;
117 // 4. try model arguments
118 NamedValueCollection aModelArgs( _rxDocument->getArgs() );
119 sTitle = aModelArgs.getOrDefault( "Title", sTitle );
120 if ( !sTitle.isEmpty() )
121 return sTitle;
123 // 5. try the last segment of the document URL
124 // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DECODE_WITH_CHARSET ),
125 // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
126 // This heuristics here should be sufficient - finally, we will get an UNO title API in a not
127 // too distant future (hopefully), then this complete class is superfluous)
128 if ( sDocURL.isEmpty() )
130 Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
131 sDocURL = xDocStorable->getLocation();
133 sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
134 if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
136 sDocURL = sDocURL.copy( 0, nLastSepPos );
137 nLastSepPos = sDocURL.lastIndexOf( '/' );
139 sTitle = sDocURL.copy( nLastSepPos + 1 );
141 if ( !sTitle.isEmpty() )
142 return sTitle;
144 // 5.
145 // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
147 Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
148 if ( xTitle.is() )
150 if ( !xTitle->getTitle().isEmpty() )
151 return xTitle->getTitle();
154 catch ( const Exception& )
156 ::com::sun::star::uno::Any caught( ::cppu::getCaughtException() );
157 OString sMessage( "caught an exception!" );
158 sMessage += "\ntype : ";
159 sMessage += OString( caught.getValueTypeName().getStr(), caught.getValueTypeName().getLength(), osl_getThreadTextEncoding() );
160 sMessage += "\nmessage: ";
161 ::com::sun::star::uno::Exception exception;
162 caught >>= exception;
163 sMessage += OString( exception.Message.getStr(), exception.Message.getLength(), osl_getThreadTextEncoding() );
164 sMessage += "\nin function:\n";
165 sMessage += BOOST_CURRENT_FUNCTION;
166 sMessage += "\n";
167 OSL_FAIL( sMessage.getStr() );
170 return sTitle;
174 } // namespace comphelper
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */