LibreOffice Module svx (master)  1
fmdocumentclassification.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 
22 
23 #include <com/sun/star/container/XChild.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/xforms/XFormsSupplier.hpp>
26 #include <com/sun/star/frame/XModule.hpp>
27 
28 #include <o3tl/string_view.hxx>
29 #include <tools/diagnose_ex.h>
30 
31 
32 namespace svxform
33 {
34 
35 
36  namespace
37  {
38  using ::com::sun::star::uno::Reference;
39  using ::com::sun::star::uno::XInterface;
40  using ::com::sun::star::container::XChild;
41  using ::com::sun::star::frame::XModel;
42  using ::com::sun::star::uno::UNO_QUERY;
43  using ::com::sun::star::frame::XModule;
44 
45 
46  template< class TYPE >
47  Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
48  {
49  Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
50  if ( xTypedNode.is() )
51  return xTypedNode;
52  else
53  {
54  Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
55  if ( xChild.is() )
56  return getTypedModelNode< TYPE >( xChild->getParent() );
57  else
58  return Reference< TYPE >();
59  }
60  }
61 
62 
63  Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
64  {
65  return getTypedModelNode< XModel >( _rxModelNode );
66  }
67  }
68 
69  using namespace ::com::sun::star::uno;
70  using namespace ::com::sun::star::frame;
71  using namespace ::com::sun::star::lang;
72  using namespace ::com::sun::star::xforms;
73  using namespace ::com::sun::star::container;
74 
75 
76  namespace
77  {
78 
79  struct ModuleInfo
80  {
83  };
84 
85 
86  const ModuleInfo* lcl_getModuleInfo()
87  {
88  static const ModuleInfo aModuleInfo[] =
89  {
90  { "com.sun.star.text.TextDocument", eTextDocument },
91  { "com.sun.star.text.WebDocument", eWebDocument },
92  { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument },
93  { "com.sun.star.drawing.DrawingDocument", eDrawingDocument },
94  { "com.sun.star.presentation.PresentationDocument", ePresentationDocument },
95  { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm },
96  { "com.sun.star.sdb.FormDesign", eDatabaseForm },
97  { "com.sun.star.sdb.TextReportDesign", eDatabaseReport },
98  { "com.sun.star.text.GlobalDocument", eTextDocument },
99  { nullptr, eUnknownDocumentType }
100  };
101  return aModuleInfo;
102  }
103  }
104 
105 
106  //= DocumentClassification
107 
108 
109  DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel )
110  {
112 
113  OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" );
114  if ( !_rxDocumentModel.is() )
115  return eType;
116 
117  try
118  {
119  // first, check whether the document has a ModuleIdentifier which we know
120  Reference< XModule > xModule( _rxDocumentModel, UNO_QUERY );
121  if ( xModule.is() )
122  eType = getDocumentTypeForModuleIdentifier( xModule->getIdentifier() );
123  if ( eType != eUnknownDocumentType )
124  return eType;
125 
126  // second, check whether it supports one of the services we know
127  Reference< XServiceInfo > xSI( _rxDocumentModel, UNO_QUERY_THROW );
128  const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
129  while ( pModuleInfo->pAsciiModuleOrServiceName )
130  {
131  if ( xSI->supportsService( OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) )
132  return pModuleInfo->eType;
133  ++pModuleInfo;
134  }
135 
136  // last: uhm, there is no last resort
137  OSL_FAIL( "DocumentClassification::classifyDocument: unknown document!" );
138  }
139  catch( const Exception& )
140  {
142  }
143 
144  return eType;
145  }
146 
147 
148  DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent )
149  {
151 
152  try
153  {
154  Reference< XModel > xDocument( getDocument( _rxFormComponent ) );
155  if ( !xDocument.is() )
156  return eUnknownDocumentType;
157  eType = classifyDocument( xDocument );
158  }
159  catch( const Exception& )
160  {
161  TOOLS_WARN_EXCEPTION( "svx", "DocumentClassification::classifyHostDocument" );
162  }
163 
164  return eType;
165  }
166 
167 
169  {
170  const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
171  while ( pModuleInfo->pAsciiModuleOrServiceName )
172  {
173  if ( o3tl::equalsAscii(_rModuleIdentifier, pModuleInfo->pAsciiModuleOrServiceName ) )
174  return pModuleInfo->eType;
175  ++pModuleInfo;
176  }
177  return eUnknownDocumentType;
178  }
179 
180 
182  {
183  const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
184  while ( pModuleInfo->pAsciiModuleOrServiceName )
185  {
186  if ( pModuleInfo->eType == _eType )
187  return OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName );
188  ++pModuleInfo;
189  }
190  return OUString();
191  }
192 
193 
194 }
195 
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static DocumentType classifyDocument(const css::uno::Reference< css::frame::XModel > &_rxDocumentModel)
classifies a document model
static DocumentType classifyHostDocument(const css::uno::Reference< css::uno::XInterface > &_rxFormComponent)
bool equalsAscii(std::u16string_view s1, const char *s2)
const char * pAsciiModuleOrServiceName
DocumentType eType
DocumentType
#define DBG_UNHANDLED_EXCEPTION(...)
#define TOOLS_WARN_EXCEPTION(area, stream)
class FmSearchEngine - Impl class for FmSearchDialog
static DocumentType getDocumentTypeForModuleIdentifier(std::u16string_view _rModuleIdentifier)
static OUString getModuleIdentifierForDocumentType(DocumentType _eType)