xref: /AOO41X/main/svx/source/form/fmdocumentclassification.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef SVX_SOURCE_FORM_FMDOCUMENTCLASSIFICATION_HXX
32*cdf0e10cSrcweir #include "fmdocumentclassification.hxx"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include "svx/dbtoolsclient.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir /** === begin UNO includes === **/
37*cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/xforms/XFormsSupplier.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/frame/XModule.hpp>
42*cdf0e10cSrcweir /** === end UNO includes === **/
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //........................................................................
47*cdf0e10cSrcweir namespace svxform
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir //........................................................................
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     namespace
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         using ::com::sun::star::uno::Reference;
54*cdf0e10cSrcweir         using ::com::sun::star::uno::XInterface;
55*cdf0e10cSrcweir         using ::com::sun::star::container::XChild;
56*cdf0e10cSrcweir         using ::com::sun::star::frame::XModel;
57*cdf0e10cSrcweir         using ::com::sun::star::uno::UNO_QUERY;
58*cdf0e10cSrcweir         using ::com::sun::star::frame::XModule;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir         //....................................................................
61*cdf0e10cSrcweir         template< class TYPE >
62*cdf0e10cSrcweir         Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
63*cdf0e10cSrcweir         {
64*cdf0e10cSrcweir 	        Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
65*cdf0e10cSrcweir 	        if ( xTypedNode.is() )
66*cdf0e10cSrcweir 		        return xTypedNode;
67*cdf0e10cSrcweir 	        else
68*cdf0e10cSrcweir 	        {
69*cdf0e10cSrcweir 		        Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
70*cdf0e10cSrcweir 		        if ( xChild.is() )
71*cdf0e10cSrcweir 			        return getTypedModelNode< TYPE >( xChild->getParent() );
72*cdf0e10cSrcweir 		        else
73*cdf0e10cSrcweir 			        return Reference< TYPE >();
74*cdf0e10cSrcweir 	        }
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     	//....................................................................
78*cdf0e10cSrcweir         Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             return getTypedModelNode< XModel >( _rxModelNode );
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
85*cdf0e10cSrcweir     using namespace ::com::sun::star::frame;
86*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
87*cdf0e10cSrcweir     using namespace ::com::sun::star::xforms;
88*cdf0e10cSrcweir     using namespace ::com::sun::star::container;
89*cdf0e10cSrcweir     using namespace ::com::sun::star::sdbc;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     //====================================================================
92*cdf0e10cSrcweir     //====================================================================
93*cdf0e10cSrcweir     namespace
94*cdf0e10cSrcweir     {
95*cdf0e10cSrcweir         //----------------------------------------------------------------
96*cdf0e10cSrcweir         struct ModuleInfo
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             const sal_Char* pAsciiModuleOrServiceName;
99*cdf0e10cSrcweir             DocumentType    eType;
100*cdf0e10cSrcweir         };
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         //----------------------------------------------------------------
103*cdf0e10cSrcweir         const ModuleInfo* lcl_getModuleInfo()
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             static const ModuleInfo aModuleInfo[] =
106*cdf0e10cSrcweir             {
107*cdf0e10cSrcweir                 { "com.sun.star.text.TextDocument", eTextDocument },
108*cdf0e10cSrcweir                 { "com.sun.star.text.WebDocument", eWebDocument },
109*cdf0e10cSrcweir                 { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument },
110*cdf0e10cSrcweir                 { "com.sun.star.drawing.DrawingDocument", eDrawingDocument },
111*cdf0e10cSrcweir                 { "com.sun.star.presentation.PresentationDocument", ePresentationDocument },
112*cdf0e10cSrcweir                 { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm },
113*cdf0e10cSrcweir                 { "com.sun.star.sdb.FormDesign", eDatabaseForm },
114*cdf0e10cSrcweir                 { "com.sun.star.sdb.TextReportDesign", eDatabaseReport },
115*cdf0e10cSrcweir                 { "com.sun.star.text.GlobalDocument", eTextDocument },
116*cdf0e10cSrcweir                 { NULL, eUnknownDocumentType }
117*cdf0e10cSrcweir             };
118*cdf0e10cSrcweir             return aModuleInfo;
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	//====================================================================
123*cdf0e10cSrcweir 	//= DocumentClassification
124*cdf0e10cSrcweir 	//====================================================================
125*cdf0e10cSrcweir 	//--------------------------------------------------------------------
126*cdf0e10cSrcweir     DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel ) SAL_THROW(())
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         DocumentType eType( eUnknownDocumentType );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" );
131*cdf0e10cSrcweir         if ( !_rxDocumentModel.is() )
132*cdf0e10cSrcweir             return eType;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         try
135*cdf0e10cSrcweir         {
136*cdf0e10cSrcweir             // first, check whether the document has a ModuleIdentifier which we know
137*cdf0e10cSrcweir             ::rtl::OUString sModuleIdentifier;
138*cdf0e10cSrcweir             Reference< XModule > xModule( _rxDocumentModel, UNO_QUERY );
139*cdf0e10cSrcweir             if ( xModule.is() )
140*cdf0e10cSrcweir                 eType = getDocumentTypeForModuleIdentifier( xModule->getIdentifier() );
141*cdf0e10cSrcweir             if ( eType != eUnknownDocumentType )
142*cdf0e10cSrcweir                 return eType;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir             // second, check whether it supports one of the services we know
145*cdf0e10cSrcweir             Reference< XServiceInfo > xSI( _rxDocumentModel, UNO_QUERY_THROW );
146*cdf0e10cSrcweir             const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
147*cdf0e10cSrcweir             while ( pModuleInfo->pAsciiModuleOrServiceName )
148*cdf0e10cSrcweir             {
149*cdf0e10cSrcweir                 if ( xSI->supportsService( ::rtl::OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) )
150*cdf0e10cSrcweir                     return pModuleInfo->eType;
151*cdf0e10cSrcweir                 ++pModuleInfo;
152*cdf0e10cSrcweir             }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir             // last: uhm, there is no last resort
155*cdf0e10cSrcweir             OSL_ENSURE( false, "DocumentClassification::classifyDocument: unknown document!" );
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir         catch( const Exception& )
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
160*cdf0e10cSrcweir         }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         return eType;
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	//--------------------------------------------------------------------
166*cdf0e10cSrcweir     DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent ) SAL_THROW(())
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         DocumentType eType( eUnknownDocumentType );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         try
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir             Reference< XModel > xDocument( getDocument( _rxFormComponent.get() ) );
173*cdf0e10cSrcweir             if ( !xDocument.is() )
174*cdf0e10cSrcweir                 return eUnknownDocumentType;
175*cdf0e10cSrcweir             eType = classifyDocument( xDocument );
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir         catch( const Exception& )
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir         	OSL_ENSURE( sal_False, "DocumentClassification::classifyHostDocument: caught an exception!" );
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         return eType;
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	//--------------------------------------------------------------------
186*cdf0e10cSrcweir     DocumentType DocumentClassification::getDocumentTypeForModuleIdentifier( const ::rtl::OUString& _rModuleIdentifier )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
189*cdf0e10cSrcweir         while ( pModuleInfo->pAsciiModuleOrServiceName )
190*cdf0e10cSrcweir         {
191*cdf0e10cSrcweir             if ( _rModuleIdentifier.equalsAscii( pModuleInfo->pAsciiModuleOrServiceName ) )
192*cdf0e10cSrcweir                 return pModuleInfo->eType;
193*cdf0e10cSrcweir             ++pModuleInfo;
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir         return eUnknownDocumentType;
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 	//--------------------------------------------------------------------
199*cdf0e10cSrcweir     ::rtl::OUString DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType )
200*cdf0e10cSrcweir     {
201*cdf0e10cSrcweir         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
202*cdf0e10cSrcweir         while ( pModuleInfo->pAsciiModuleOrServiceName )
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             if ( pModuleInfo->eType == _eType )
205*cdf0e10cSrcweir                 return ::rtl::OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName );
206*cdf0e10cSrcweir             ++pModuleInfo;
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir         return ::rtl::OUString();
209*cdf0e10cSrcweir     }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir //........................................................................
212*cdf0e10cSrcweir } // namespace svxform
213*cdf0e10cSrcweir //........................................................................
214*cdf0e10cSrcweir 
215