lok: register view callback also for nested sm view
[LibreOffice.git] / comphelper / source / misc / documentinfo.cxx
blob21425524e64e29b37af0e6772d4a0c91c5c46cbd
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/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>
30 #include <cppuhelper/exc_hlp.hxx>
32 #include <sal/log.hxx>
34 namespace comphelper {
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;
50 namespace
52 OUString lcl_getTitle( const Reference< XInterface >& _rxComponent )
54 Reference< XTitle > xTitle( _rxComponent, UNO_QUERY );
55 if ( xTitle.is() )
56 return xTitle->getTitle();
57 return OUString();
61 OUString DocumentInfo::getDocumentTitle( const Reference< XModel >& _rxDocument )
63 OUString sTitle;
65 if ( !_rxDocument.is() )
66 return sTitle;
68 try
70 // 1. ask the model and the controller for their XTitle::getTitle
71 sTitle = lcl_getTitle( _rxDocument );
72 if ( !sTitle.isEmpty() )
73 return sTitle;
75 Reference< XController > xController( _rxDocument->getCurrentController() );
76 sTitle = lcl_getTitle( xController );
77 if ( !sTitle.isEmpty() )
78 return sTitle;
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();
86 // 2. if the document is not saved, yet, check the frame title
87 if ( sDocURL.isEmpty() )
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;
97 // 3. try the UNO XDocumentProperties
98 Reference< XDocumentPropertiesSupplier > xDPS( _rxDocument, UNO_QUERY );
99 if ( xDPS.is() )
101 Reference< XDocumentProperties > xDocProps (
102 xDPS->getDocumentProperties(), css::uno::UNO_SET_THROW );
103 sTitle = xDocProps->getTitle();
104 if ( !sTitle.isEmpty() )
105 return sTitle;
108 // 4. try model arguments
109 sTitle = NamedValueCollection::getOrDefault( _rxDocument->getArgs(), u"Title", sTitle );
110 if ( !sTitle.isEmpty() )
111 return sTitle;
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() )
120 Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
121 sDocURL = xDocStorable->getLocation();
123 sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
124 if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
126 sDocURL = sDocURL.copy( 0, nLastSepPos );
127 nLastSepPos = sDocURL.lastIndexOf( '/' );
129 sTitle = sDocURL.copy( nLastSepPos + 1 );
131 if ( !sTitle.isEmpty() )
132 return sTitle;
134 // 5.
135 // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
137 Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
138 if ( xTitle.is() )
140 if ( !xTitle->getTitle().isEmpty() )
141 return xTitle->getTitle();
144 catch ( const Exception& )
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__);
155 return sTitle;
158 void DocumentInfo::notifyMacroEventRead(const css::uno::Reference<css::frame::XModel>& rModel)
160 if (!rModel.is())
161 return;
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);
174 } // namespace comphelper
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */