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