1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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() )
54 Reference
< XChild
> xChild( _rxModelNode
, UNO_QUERY
);
56 return getTypedModelNode
< TYPE
>( xChild
->getParent() );
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::container
;
80 const char* pAsciiModuleOrServiceName
;
85 const ModuleInfo
* lcl_getModuleInfo()
87 static const ModuleInfo aModuleInfo
[] =
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
}
105 //= DocumentClassification
108 DocumentType
DocumentClassification::classifyDocument( const Reference
< XModel
>& _rxDocumentModel
)
110 DocumentType
eType( eUnknownDocumentType
);
112 OSL_ENSURE( _rxDocumentModel
.is(), "DocumentClassification::classifyDocument: invalid document!" );
113 if ( !_rxDocumentModel
.is() )
118 // first, check whether the document has a ModuleIdentifier which we know
119 Reference
< XModule
> xModule( _rxDocumentModel
, UNO_QUERY
);
121 eType
= getDocumentTypeForModuleIdentifier( xModule
->getIdentifier() );
122 if ( eType
!= eUnknownDocumentType
)
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
)
130 if ( xSI
->supportsService( OUString::createFromAscii( pModuleInfo
->pAsciiModuleOrServiceName
) ) )
131 return pModuleInfo
->eType
;
135 // last: uhm, there is no last resort
136 OSL_FAIL( "DocumentClassification::classifyDocument: unknown document!" );
138 catch( const Exception
& )
140 DBG_UNHANDLED_EXCEPTION("svx");
147 DocumentType
DocumentClassification::classifyHostDocument( const Reference
< XInterface
>& _rxFormComponent
)
149 DocumentType
eType( eUnknownDocumentType
);
153 Reference
< XModel
> xDocument( getDocument( _rxFormComponent
) );
154 if ( !xDocument
.is() )
155 return eUnknownDocumentType
;
156 eType
= classifyDocument( xDocument
);
158 catch( const Exception
& )
160 TOOLS_WARN_EXCEPTION( "svx", "DocumentClassification::classifyHostDocument" );
167 DocumentType
DocumentClassification::getDocumentTypeForModuleIdentifier( std::u16string_view _rModuleIdentifier
)
169 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
170 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
172 if ( o3tl::equalsAscii(_rModuleIdentifier
, pModuleInfo
->pAsciiModuleOrServiceName
) )
173 return pModuleInfo
->eType
;
176 return eUnknownDocumentType
;
180 OUString
DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType
)
182 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
183 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
185 if ( pModuleInfo
->eType
== _eType
)
186 return OUString::createFromAscii( pModuleInfo
->pAsciiModuleOrServiceName
);
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */