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 
30 #include <cppuhelper/exc_hlp.hxx>
31 
32 #include <sal/log.hxx>
33 
34 namespace 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  {
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  NamedValueCollection aModelArgs( _rxDocument->getArgs() );
110  sTitle = aModelArgs.getOrDefault( "Title", sTitle );
111  if ( !sTitle.isEmpty() )
112  return sTitle;
113 
114  // 5. try the last segment of the document URL
115  // this formerly was an INetURLObject::getName( LAST_SEGMENT, true, DecodeMechanism::WithCharset ),
116  // but since we moved this code to comphelper, we do not have access to an INetURLObject anymore
117  // This heuristics here should be sufficient - finally, we will get a UNO title API in a not
118  // too distant future (hopefully), then this complete class is superfluous)
119  if ( sDocURL.isEmpty() )
120  {
121  Reference< XStorable > xDocStorable( _rxDocument, UNO_QUERY_THROW );
122  sDocURL = xDocStorable->getLocation();
123  }
124  sal_Int32 nLastSepPos = sDocURL.lastIndexOf( '/' );
125  if ( ( nLastSepPos != -1 ) && ( nLastSepPos == sDocURL.getLength() - 1 ) )
126  {
127  sDocURL = sDocURL.copy( 0, nLastSepPos );
128  nLastSepPos = sDocURL.lastIndexOf( '/' );
129  }
130  sTitle = sDocURL.copy( nLastSepPos + 1 );
131 
132  if ( !sTitle.isEmpty() )
133  return sTitle;
134 
135  // 5.
136  // <-- #i88104# (05-16-08) TKR: use the new XTitle Interface to get the Title -->
137 
138  Reference< XTitle > xTitle( _rxDocument, UNO_QUERY );
139  if ( xTitle.is() )
140  {
141  if ( !xTitle->getTitle().isEmpty() )
142  return xTitle->getTitle();
143  }
144  }
145  catch ( const Exception& )
146  {
147  // Cannot use tools::exceptionToString here, because the tools module depends on the comphelper module
148  css::uno::Any caught( ::cppu::getCaughtException() );
149  css::uno::Exception exception;
150  caught >>= exception;
151  SAL_WARN( "comphelper", "caught an exception!\ntype : " << caught.getValueTypeName()
152  << "\nmessage: " << exception
153  << "\nin function:\n" << __func__);
154  }
155 
156  return sTitle;
157  }
158 
159  void DocumentInfo::notifyMacroEventRead(const css::uno::Reference<css::frame::XModel>& rModel)
160  {
161  if (!rModel.is())
162  return;
163 
164  // like BreakMacroSignature of XMLScriptContext use XModel::attachResource
165  // to propagate this notification
166  css::uno::Sequence<css::beans::PropertyValue> aMedDescr = rModel->getArgs();
167  sal_Int32 nNewLen = aMedDescr.getLength() + 1;
168  aMedDescr.realloc(nNewLen);
169  aMedDescr[nNewLen-1].Name = "MacroEventRead";
170  aMedDescr[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: */
Reference< XFrame > xFrame
Reference< XController > xController
Any SAL_CALL getCaughtException()
COMPHELPER_DLLPUBLIC OUString getDocumentTitle(const css::uno::Reference< css::frame::XModel > &_rxDocument)
retrieves the UI title of the given document
#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