1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef SVX_SOURCE_FORM_FMDOCUMENTCLASSIFICATION_HXX
28cdf0e10cSrcweir #include "fmdocumentclassification.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include "svx/dbtoolsclient.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /** === begin UNO includes === **/
33cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
34cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
35cdf0e10cSrcweir #include <com/sun/star/xforms/XFormsSupplier.hpp>
36cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp>
37cdf0e10cSrcweir #include <com/sun/star/frame/XModule.hpp>
38cdf0e10cSrcweir /** === end UNO includes === **/
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <tools/diagnose_ex.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //........................................................................
43cdf0e10cSrcweir namespace svxform
44cdf0e10cSrcweir {
45cdf0e10cSrcweir //........................................................................
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     namespace
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         using ::com::sun::star::uno::Reference;
50cdf0e10cSrcweir         using ::com::sun::star::uno::XInterface;
51cdf0e10cSrcweir         using ::com::sun::star::container::XChild;
52cdf0e10cSrcweir         using ::com::sun::star::frame::XModel;
53cdf0e10cSrcweir         using ::com::sun::star::uno::UNO_QUERY;
54cdf0e10cSrcweir         using ::com::sun::star::frame::XModule;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir         //....................................................................
57cdf0e10cSrcweir         template< class TYPE >
getTypedModelNode(const Reference<XInterface> & _rxModelNode)58cdf0e10cSrcweir         Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir 	        Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
61cdf0e10cSrcweir 	        if ( xTypedNode.is() )
62cdf0e10cSrcweir 		        return xTypedNode;
63cdf0e10cSrcweir 	        else
64cdf0e10cSrcweir 	        {
65cdf0e10cSrcweir 		        Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
66cdf0e10cSrcweir 		        if ( xChild.is() )
67cdf0e10cSrcweir 			        return getTypedModelNode< TYPE >( xChild->getParent() );
68cdf0e10cSrcweir 		        else
69cdf0e10cSrcweir 			        return Reference< TYPE >();
70cdf0e10cSrcweir 	        }
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     	//....................................................................
getDocument(const Reference<XInterface> & _rxModelNode)74cdf0e10cSrcweir         Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             return getTypedModelNode< XModel >( _rxModelNode );
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
81cdf0e10cSrcweir     using namespace ::com::sun::star::frame;
82cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
83cdf0e10cSrcweir     using namespace ::com::sun::star::xforms;
84cdf0e10cSrcweir     using namespace ::com::sun::star::container;
85cdf0e10cSrcweir     using namespace ::com::sun::star::sdbc;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     //====================================================================
88cdf0e10cSrcweir     //====================================================================
89cdf0e10cSrcweir     namespace
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         //----------------------------------------------------------------
92cdf0e10cSrcweir         struct ModuleInfo
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             const sal_Char* pAsciiModuleOrServiceName;
95cdf0e10cSrcweir             DocumentType    eType;
96cdf0e10cSrcweir         };
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         //----------------------------------------------------------------
lcl_getModuleInfo()99cdf0e10cSrcweir         const ModuleInfo* lcl_getModuleInfo()
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             static const ModuleInfo aModuleInfo[] =
102cdf0e10cSrcweir             {
103cdf0e10cSrcweir                 { "com.sun.star.text.TextDocument", eTextDocument },
104cdf0e10cSrcweir                 { "com.sun.star.text.WebDocument", eWebDocument },
105cdf0e10cSrcweir                 { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument },
106cdf0e10cSrcweir                 { "com.sun.star.drawing.DrawingDocument", eDrawingDocument },
107cdf0e10cSrcweir                 { "com.sun.star.presentation.PresentationDocument", ePresentationDocument },
108cdf0e10cSrcweir                 { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm },
109cdf0e10cSrcweir                 { "com.sun.star.sdb.FormDesign", eDatabaseForm },
110cdf0e10cSrcweir                 { "com.sun.star.sdb.TextReportDesign", eDatabaseReport },
111cdf0e10cSrcweir                 { "com.sun.star.text.GlobalDocument", eTextDocument },
112cdf0e10cSrcweir                 { NULL, eUnknownDocumentType }
113cdf0e10cSrcweir             };
114cdf0e10cSrcweir             return aModuleInfo;
115cdf0e10cSrcweir         }
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	//====================================================================
119cdf0e10cSrcweir 	//= DocumentClassification
120cdf0e10cSrcweir 	//====================================================================
121cdf0e10cSrcweir 	//--------------------------------------------------------------------
classifyDocument(const Reference<XModel> & _rxDocumentModel)122cdf0e10cSrcweir     DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel ) SAL_THROW(())
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         DocumentType eType( eUnknownDocumentType );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" );
127cdf0e10cSrcweir         if ( !_rxDocumentModel.is() )
128cdf0e10cSrcweir             return eType;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         try
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir             // first, check whether the document has a ModuleIdentifier which we know
133cdf0e10cSrcweir             ::rtl::OUString sModuleIdentifier;
134cdf0e10cSrcweir             Reference< XModule > xModule( _rxDocumentModel, UNO_QUERY );
135cdf0e10cSrcweir             if ( xModule.is() )
136cdf0e10cSrcweir                 eType = getDocumentTypeForModuleIdentifier( xModule->getIdentifier() );
137cdf0e10cSrcweir             if ( eType != eUnknownDocumentType )
138cdf0e10cSrcweir                 return eType;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             // second, check whether it supports one of the services we know
141cdf0e10cSrcweir             Reference< XServiceInfo > xSI( _rxDocumentModel, UNO_QUERY_THROW );
142cdf0e10cSrcweir             const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
143cdf0e10cSrcweir             while ( pModuleInfo->pAsciiModuleOrServiceName )
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 if ( xSI->supportsService( ::rtl::OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) )
146cdf0e10cSrcweir                     return pModuleInfo->eType;
147cdf0e10cSrcweir                 ++pModuleInfo;
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             // last: uhm, there is no last resort
151cdf0e10cSrcweir             OSL_ENSURE( false, "DocumentClassification::classifyDocument: unknown document!" );
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir         catch( const Exception& )
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         return eType;
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	//--------------------------------------------------------------------
classifyHostDocument(const Reference<XInterface> & _rxFormComponent)162cdf0e10cSrcweir     DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent ) SAL_THROW(())
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         DocumentType eType( eUnknownDocumentType );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         try
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             Reference< XModel > xDocument( getDocument( _rxFormComponent.get() ) );
169cdf0e10cSrcweir             if ( !xDocument.is() )
170cdf0e10cSrcweir                 return eUnknownDocumentType;
171cdf0e10cSrcweir             eType = classifyDocument( xDocument );
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir         catch( const Exception& )
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir         	OSL_ENSURE( sal_False, "DocumentClassification::classifyHostDocument: caught an exception!" );
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         return eType;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	//--------------------------------------------------------------------
getDocumentTypeForModuleIdentifier(const::rtl::OUString & _rModuleIdentifier)182cdf0e10cSrcweir     DocumentType DocumentClassification::getDocumentTypeForModuleIdentifier( const ::rtl::OUString& _rModuleIdentifier )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
185cdf0e10cSrcweir         while ( pModuleInfo->pAsciiModuleOrServiceName )
186cdf0e10cSrcweir         {
187cdf0e10cSrcweir             if ( _rModuleIdentifier.equalsAscii( pModuleInfo->pAsciiModuleOrServiceName ) )
188cdf0e10cSrcweir                 return pModuleInfo->eType;
189cdf0e10cSrcweir             ++pModuleInfo;
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         return eUnknownDocumentType;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	//--------------------------------------------------------------------
getModuleIdentifierForDocumentType(DocumentType _eType)195cdf0e10cSrcweir     ::rtl::OUString DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType )
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
198cdf0e10cSrcweir         while ( pModuleInfo->pAsciiModuleOrServiceName )
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             if ( pModuleInfo->eType == _eType )
201cdf0e10cSrcweir                 return ::rtl::OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName );
202cdf0e10cSrcweir             ++pModuleInfo;
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir         return ::rtl::OUString();
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir //........................................................................
208cdf0e10cSrcweir } // namespace svxform
209cdf0e10cSrcweir //........................................................................
210cdf0e10cSrcweir 
211