1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fmdocumentclassification.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #ifndef SVX_SOURCE_FORM_FMDOCUMENTCLASSIFICATION_HXX
35 #include "fmdocumentclassification.hxx"
37 #include "svx/dbtoolsclient.hxx"
39 /** === begin UNO includes === **/
40 #include <com/sun/star/container/XChild.hpp>
41 #include <com/sun/star/lang/XServiceInfo.hpp>
42 #include <com/sun/star/xforms/XFormsSupplier.hpp>
43 #include <com/sun/star/sdbc/XConnection.hpp>
44 #include <com/sun/star/frame/XModule.hpp>
45 /** === end UNO includes === **/
47 #include <tools/diagnose_ex.h>
49 //........................................................................
52 //........................................................................
56 using ::com::sun::star::uno::Reference
;
57 using ::com::sun::star::uno::XInterface
;
58 using ::com::sun::star::container::XChild
;
59 using ::com::sun::star::frame::XModel
;
60 using ::com::sun::star::uno::UNO_QUERY
;
61 using ::com::sun::star::frame::XModule
;
63 //....................................................................
64 template< class TYPE
>
65 Reference
< TYPE
> getTypedModelNode( const Reference
< XInterface
>& _rxModelNode
)
67 Reference
< TYPE
> xTypedNode( _rxModelNode
, UNO_QUERY
);
68 if ( xTypedNode
.is() )
72 Reference
< XChild
> xChild( _rxModelNode
, UNO_QUERY
);
74 return getTypedModelNode
< TYPE
>( xChild
->getParent() );
76 return Reference
< TYPE
>();
80 //....................................................................
81 Reference
< XModel
> getDocument( const Reference
< XInterface
>& _rxModelNode
)
83 return getTypedModelNode
< XModel
>( _rxModelNode
);
87 using namespace ::com::sun::star::uno
;
88 using namespace ::com::sun::star::frame
;
89 using namespace ::com::sun::star::lang
;
90 using namespace ::com::sun::star::xforms
;
91 using namespace ::com::sun::star::container
;
92 using namespace ::com::sun::star::sdbc
;
94 //====================================================================
95 //====================================================================
98 //----------------------------------------------------------------
101 const sal_Char
* pAsciiModuleOrServiceName
;
105 //----------------------------------------------------------------
106 const ModuleInfo
* lcl_getModuleInfo()
108 static const ModuleInfo aModuleInfo
[] =
110 { "com.sun.star.text.TextDocument", eTextDocument
},
111 { "com.sun.star.text.WebDocument", eWebDocument
},
112 { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument
},
113 { "com.sun.star.drawing.DrawingDocument", eDrawingDocument
},
114 { "com.sun.star.presentation.PresentationDocument", ePresentationDocument
},
115 { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm
},
116 { "com.sun.star.sdb.FormDesign", eDatabaseForm
},
117 { "com.sun.star.sdb.TextReportDesign", eDatabaseReport
},
118 { "com.sun.star.text.GlobalDocument", eTextDocument
},
119 { NULL
, eUnknownDocumentType
}
125 //====================================================================
126 //= DocumentClassification
127 //====================================================================
128 //--------------------------------------------------------------------
129 DocumentType
DocumentClassification::classifyDocument( const Reference
< XModel
>& _rxDocumentModel
) SAL_THROW(())
131 DocumentType
eType( eUnknownDocumentType
);
133 OSL_ENSURE( _rxDocumentModel
.is(), "DocumentClassification::classifyDocument: invalid document!" );
134 if ( !_rxDocumentModel
.is() )
139 // first, check whether the document has a ModuleIdentifier which we know
140 ::rtl::OUString sModuleIdentifier
;
141 Reference
< XModule
> xModule( _rxDocumentModel
, UNO_QUERY
);
143 eType
= getDocumentTypeForModuleIdentifier( xModule
->getIdentifier() );
144 if ( eType
!= eUnknownDocumentType
)
147 // second, check whether it supports one of the services we know
148 Reference
< XServiceInfo
> xSI( _rxDocumentModel
, UNO_QUERY_THROW
);
149 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
150 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
152 if ( xSI
->supportsService( ::rtl::OUString::createFromAscii( pModuleInfo
->pAsciiModuleOrServiceName
) ) )
153 return pModuleInfo
->eType
;
157 // last: uhm, there is no last resort
158 OSL_ENSURE( false, "DocumentClassification::classifyDocument: unknown document!" );
160 catch( const Exception
& )
162 DBG_UNHANDLED_EXCEPTION();
168 //--------------------------------------------------------------------
169 DocumentType
DocumentClassification::classifyHostDocument( const Reference
< XInterface
>& _rxFormComponent
) SAL_THROW(())
171 DocumentType
eType( eUnknownDocumentType
);
175 Reference
< XModel
> xDocument( getDocument( _rxFormComponent
.get() ) );
176 if ( !xDocument
.is() )
177 return eUnknownDocumentType
;
178 eType
= classifyDocument( xDocument
);
180 catch( const Exception
& )
182 OSL_ENSURE( sal_False
, "DocumentClassification::classifyHostDocument: caught an exception!" );
188 //--------------------------------------------------------------------
189 DocumentType
DocumentClassification::getDocumentTypeForModuleIdentifier( const ::rtl::OUString
& _rModuleIdentifier
)
191 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
192 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
194 if ( _rModuleIdentifier
.equalsAscii( pModuleInfo
->pAsciiModuleOrServiceName
) )
195 return pModuleInfo
->eType
;
198 return eUnknownDocumentType
;
201 //--------------------------------------------------------------------
202 ::rtl::OUString
DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType
)
204 const ModuleInfo
* pModuleInfo
= lcl_getModuleInfo();
205 while ( pModuleInfo
->pAsciiModuleOrServiceName
)
207 if ( pModuleInfo
->eType
== _eType
)
208 return ::rtl::OUString::createFromAscii( pModuleInfo
->pAsciiModuleOrServiceName
);
211 return ::rtl::OUString();
214 //........................................................................
215 } // namespace svxform
216 //........................................................................