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