LibreOffice Module comphelper (master) 1
documentinfo.cxx
Go to the documentation of this file.
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
23
24#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
25#include <com/sun/star/document/XDocumentProperties.hpp>
26#include <com/sun/star/frame/XModel.hpp>
27#include <com/sun/star/frame/XStorable.hpp>
28#include <com/sun/star/frame/XTitle.hpp>
29
31
32#include <sal/log.hxx>
33
34namespace comphelper {
35
36
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::UNO_QUERY;
39 using ::com::sun::star::uno::UNO_QUERY_THROW;
40 using ::com::sun::star::uno::Exception;
41 using ::com::sun::star::frame::XModel;
42 using ::com::sun::star::frame::XTitle;
43 using ::com::sun::star::frame::XController;
44 using ::com::sun::star::document::XDocumentPropertiesSupplier;
45 using ::com::sun::star::document::XDocumentProperties;
46 using ::com::sun::star::frame::XStorable;
47 using ::com::sun::star::uno::XInterface;
48 using ::com::sun::star::frame::XFrame;
49
50 namespace
51 {
52 OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
53 {
54 Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
55 if ( xTitle.is() )
56 return xTitle->getTitle();
57 return OUString();
58 }
59 }
60
61 OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
62 {
63 OUString sTitle;
64
65 if ( !_rxDocument.is() )
66 return sTitle;
67
68 try
69 {
70 // 1. ask the model and the controller for their XTitle::getTitle
71 sTitle = lcl_getTitle( _rxDocument );
72 if ( !sTitle.isEmpty() )
73 return sTitle;
74
75 Reference< XController > xController( _rxDocument->getCurrentController() );
76 sTitle = lcl_getTitle( xController );
77 if ( !sTitle.isEmpty() )
78 return sTitle;
79
80 // work around a problem with embedded objects, which sometimes return
81 // private:object as URL
82 OUString sDocURL = _rxDocument->getURL();
83 if ( sDocURL.startsWithIgnoreAsciiCase( "private:" ) )
84 sDocURL.clear();
85
86 // 2. if the document is not saved, yet, check the frame title
87 if ( sDocURL.isEmpty() )
88 {
89 Reference< XFrame > xFrame;
90 if ( xController.is() )
91 xFrame.set( xController->getFrame() );
92 sTitle = lcl_getTitle( xFrame );
93 if ( !sTitle.isEmpty() )
94 return sTitle;
95 }
96
97 // 3. try the UNO XDocumentProperties
98 Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
99 if ( xDPS.is() )
100 {
101 Reference< XDocumentProperties > xDocProps (
102 xDPS->getDocumentProperties(), css::uno::UNO_SET_THROW );
103 sTitle = xDocProps->getTitle();
104 if ( !sTitle.isEmpty() )
105 return sTitle;
106 }
107
108 // 4. try model arguments
109 sTitle = NamedValueCollection::getOrDefault( _rxDocument->getArgs(), u"Title", sTitle );
110 if ( !sTitle.isEmpty() )
111 return sTitle;
112
113 // 5. try the last segment of the document URL
114 // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DecodeMechanism::WithCharset ),
115 // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
116 // This heuristics here should be sufficient - finally, we will get a UNO title API in a not
117 // too distant future (hopefully), then this complete class is superfluous)
118 if ( sDocURL.isEmpty() )
119 {
120 Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
121 sDocURL = xDocStorable->getLocation();
122 }
123 sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
124 if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
125 {
126 sDocURL = sDocURL.copy( 0, nLastSepPos );
127 nLastSepPos = sDocURL.lastIndexOf( '/' );
128 }
129 sTitle = sDocURL.copy( nLastSepPos + 1 );
130
131 if ( !sTitle.isEmpty() )
132 return sTitle;
133
134 // 5.
135 // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
136
137 Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
138 if ( xTitle.is() )
139 {
140 if ( !xTitle->getTitle().isEmpty() )
141 return xTitle->getTitle();
142 }
143 }
144 catch ( const Exception& )
145 {
146 // Cannot use tools::exceptionToString here, because the tools module depends on the comphelper module
147 css::uno::Any caught( ::cppu::getCaughtException() );
148 css::uno::Exception exception;
149 caught >>= exception;
150 SAL_WARN( "comphelper", "caught an exception!\ntype : " << caught.getValueTypeName()
151 << "\nmessage: " << exception
152 << "\nin function:\n" << __func__);
153 }
154
155 return sTitle;
156 }
157
158 void DocumentInfo::notifyMacroEventRead(const css::uno::Reference<css::frame::XModel>& rModel)
159 {
160 if (!rModel.is())
161 return;
162
163 // like BreakMacroSignature of XMLScriptContext use XModel::attachResource
164 // to propagate this notification
165 css::uno::Sequence<css::beans::PropertyValue> aMedDescr = rModel->getArgs();
166 sal_Int32 nNewLen = aMedDescr.getLength() + 1;
167 aMedDescr.realloc(nNewLen);
168 auto pMedDescr = aMedDescr.getArray();
169 pMedDescr[nNewLen-1].Name = "MacroEventRead";
170 pMedDescr[nNewLen-1].Value <<= true;
171 rModel->attachResource(rModel->getURL(), aMedDescr);
172 }
173
174} // namespace comphelper
175
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
VALUE_TYPE getOrDefault(const OUString &_rValueName, const VALUE_TYPE &_rDefault) const
retrieves a value with a given name, or defaults it to a given value, if it's not present in the coll...
#define SAL_WARN(area, stream)
COMPHELPER_DLLPUBLIC void notifyMacroEventRead(const css::uno::Reference< css::frame::XModel > &_rxDocument)
notify that this document contains a macro event handler
COMPHELPER_DLLPUBLIC OUString getDocumentTitle(const css::uno::Reference< css::frame::XModel > &_rxDocument)
retrieves the UI title of the given document
Reference< XController > xController
Reference< XFrame > xFrame