Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / form / fmdocumentclassification.cxx
blob4878403fd2ccddefda4dc2e34a98a71c8bbdcf6f
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 <fmdocumentclassification.hxx>
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>
28 #include <o3tl/string_view.hxx>
29 #include <comphelper/diagnose_ex.hxx>
32 namespace svxform
36 namespace
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;
46 template< class TYPE >
47 Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
49 Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
50 if ( xTypedNode.is() )
51 return xTypedNode;
52 else
54 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
55 if ( xChild.is() )
56 return getTypedModelNode< TYPE >( xChild->getParent() );
57 else
58 return Reference< TYPE >();
63 Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
65 return getTypedModelNode< XModel >( _rxModelNode );
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;
76 namespace
79 struct ModuleInfo
81 const char* pAsciiModuleOrServiceName;
82 DocumentType eType;
86 const ModuleInfo* lcl_getModuleInfo()
88 static const ModuleInfo aModuleInfo[] =
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 }
101 return aModuleInfo;
106 //= DocumentClassification
109 DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel )
111 DocumentType eType( eUnknownDocumentType );
113 OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" );
114 if ( !_rxDocumentModel.is() )
115 return eType;
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;
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 )
131 if ( xSI->supportsService( OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) )
132 return pModuleInfo->eType;
133 ++pModuleInfo;
136 // last: uhm, there is no last resort
137 OSL_FAIL( "DocumentClassification::classifyDocument: unknown document!" );
139 catch( const Exception& )
141 DBG_UNHANDLED_EXCEPTION("svx");
144 return eType;
148 DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent )
150 DocumentType eType( eUnknownDocumentType );
154 Reference< XModel > xDocument( getDocument( _rxFormComponent ) );
155 if ( !xDocument.is() )
156 return eUnknownDocumentType;
157 eType = classifyDocument( xDocument );
159 catch( const Exception& )
161 TOOLS_WARN_EXCEPTION( "svx", "DocumentClassification::classifyHostDocument" );
164 return eType;
168 DocumentType DocumentClassification::getDocumentTypeForModuleIdentifier( std::u16string_view _rModuleIdentifier )
170 const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
171 while ( pModuleInfo->pAsciiModuleOrServiceName )
173 if ( o3tl::equalsAscii(_rModuleIdentifier, pModuleInfo->pAsciiModuleOrServiceName ) )
174 return pModuleInfo->eType;
175 ++pModuleInfo;
177 return eUnknownDocumentType;
181 OUString DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType )
183 const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
184 while ( pModuleInfo->pAsciiModuleOrServiceName )
186 if ( pModuleInfo->eType == _eType )
187 return OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName );
188 ++pModuleInfo;
190 return OUString();
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */