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