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/sdbc/XConnection.hpp>
27 #include <com/sun/star/frame/XModule.hpp>
29 #include <tools/diagnose_ex.h>
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::xforms
;
73 using namespace ::com::sun::star::container
;
74 using namespace ::com::sun::star::sdbc
;
82 const sal_Char
* pAsciiModuleOrServiceName
;
83 DocumentType
const eType
;
87 const ModuleInfo
* lcl_getModuleInfo()
89 static const ModuleInfo aModuleInfo
[] =
91 { "com.sun.star.text.TextDocument", eTextDocument
},
92 { "com.sun.star.text.WebDocument", eWebDocument
},
93 { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument
},
94 { "com.sun.star.drawing.DrawingDocument", eDrawingDocument
},
95 { "com.sun.star.presentation.PresentationDocument", ePresentationDocument
},
96 { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm
},
97 { "com.sun.star.sdb.FormDesign", eDatabaseForm
},
98 { "com.sun.star.sdb.TextReportDesign", eDatabaseReport
},
99 { "com.sun.star.text.GlobalDocument", eTextDocument
},
100 { nullptr, eUnknownDocumentType
}
107 //= DocumentClassification
110 DocumentType
DocumentClassification::classifyDocument( const Reference
< XModel
>& _rxDocumentModel
)
112 DocumentType
eType( eUnknownDocumentType
);
114 OSL_ENSURE( _rxDocumentModel
.is(), "DocumentClassification::classifyDocument: invalid document!" );
115 if ( !_rxDocumentModel
.is() )
120 // first, check whether the document has a ModuleIdentifier which we know
121 Reference
< XModule
> xModule( _rxDocumentModel
, UNO_QUERY
);
123 eType
= getDocumentTypeForModuleIdentifier( xModule
->getIdentifier() );
124 if ( eType
!= eUnknownDocumentType
)
127 // second, check whether it supports one of the services we know
128 Reference
< XServiceInfo
> xSI( _rxDocumentModel
, UNO_QUERY_THROW
);
129 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
130 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
132 if ( xSI
->supportsService( OUString::createFromAscii( pModuleInfo
->pAsciiModuleOrServiceName
) ) )
133 return pModuleInfo
->eType
;
137 // last: uhm, there is no last resort
138 OSL_FAIL( "DocumentClassification::classifyDocument: unknown document!" );
140 catch( const Exception
& )
142 DBG_UNHANDLED_EXCEPTION("svx");
149 DocumentType
DocumentClassification::classifyHostDocument( const Reference
< XInterface
>& _rxFormComponent
)
151 DocumentType
eType( eUnknownDocumentType
);
155 Reference
< XModel
> xDocument( getDocument( _rxFormComponent
.get() ) );
156 if ( !xDocument
.is() )
157 return eUnknownDocumentType
;
158 eType
= classifyDocument( xDocument
);
160 catch( const Exception
& )
162 OSL_FAIL( "DocumentClassification::classifyHostDocument: caught an exception!" );
169 DocumentType
DocumentClassification::getDocumentTypeForModuleIdentifier( const OUString
& _rModuleIdentifier
)
171 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
172 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
174 if ( _rModuleIdentifier
.equalsAscii( pModuleInfo
->pAsciiModuleOrServiceName
) )
175 return pModuleInfo
->eType
;
178 return eUnknownDocumentType
;
182 OUString
DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType
)
184 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
185 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
187 if ( pModuleInfo
->eType
== _eType
)
188 return OUString::createFromAscii( pModuleInfo
->pAsciiModuleOrServiceName
);
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */