LCOV - code coverage report
Current view: top level - svx/source/form - fmdocumentclassification.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 53 0.0 %
Date: 2014-04-14 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include "fmdocumentclassification.hxx"
      22             : #include "svx/dbtoolsclient.hxx"
      23             : 
      24             : #include <com/sun/star/container/XChild.hpp>
      25             : #include <com/sun/star/lang/XServiceInfo.hpp>
      26             : #include <com/sun/star/xforms/XFormsSupplier.hpp>
      27             : #include <com/sun/star/sdbc/XConnection.hpp>
      28             : #include <com/sun/star/frame/XModule.hpp>
      29             : 
      30             : #include <tools/diagnose_ex.h>
      31             : 
      32             : 
      33             : namespace svxform
      34             : {
      35             : 
      36             : 
      37             :     namespace
      38             :     {
      39             :         using ::com::sun::star::uno::Reference;
      40             :         using ::com::sun::star::uno::XInterface;
      41             :         using ::com::sun::star::container::XChild;
      42             :         using ::com::sun::star::frame::XModel;
      43             :         using ::com::sun::star::uno::UNO_QUERY;
      44             :         using ::com::sun::star::frame::XModule;
      45             : 
      46             : 
      47             :         template< class TYPE >
      48           0 :         Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
      49             :         {
      50           0 :             Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
      51           0 :             if ( xTypedNode.is() )
      52           0 :                 return xTypedNode;
      53             :             else
      54             :             {
      55           0 :                 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
      56           0 :                 if ( xChild.is() )
      57           0 :                     return getTypedModelNode< TYPE >( xChild->getParent() );
      58             :                 else
      59           0 :                     return Reference< TYPE >();
      60           0 :             }
      61             :         }
      62             : 
      63             : 
      64           0 :         Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
      65             :         {
      66           0 :             return getTypedModelNode< XModel >( _rxModelNode );
      67             :         }
      68             :     }
      69             : 
      70             :     using namespace ::com::sun::star::uno;
      71             :     using namespace ::com::sun::star::frame;
      72             :     using namespace ::com::sun::star::lang;
      73             :     using namespace ::com::sun::star::xforms;
      74             :     using namespace ::com::sun::star::container;
      75             :     using namespace ::com::sun::star::sdbc;
      76             : 
      77             : 
      78             : 
      79             :     namespace
      80             :     {
      81             : 
      82             :         struct ModuleInfo
      83             :         {
      84             :             const sal_Char* pAsciiModuleOrServiceName;
      85             :             DocumentType    eType;
      86             :         };
      87             : 
      88             : 
      89           0 :         const ModuleInfo* lcl_getModuleInfo()
      90             :         {
      91             :             static const ModuleInfo aModuleInfo[] =
      92             :             {
      93             :                 { "com.sun.star.text.TextDocument", eTextDocument },
      94             :                 { "com.sun.star.text.WebDocument", eWebDocument },
      95             :                 { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument },
      96             :                 { "com.sun.star.drawing.DrawingDocument", eDrawingDocument },
      97             :                 { "com.sun.star.presentation.PresentationDocument", ePresentationDocument },
      98             :                 { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm },
      99             :                 { "com.sun.star.sdb.FormDesign", eDatabaseForm },
     100             :                 { "com.sun.star.sdb.TextReportDesign", eDatabaseReport },
     101             :                 { "com.sun.star.text.GlobalDocument", eTextDocument },
     102             :                 { NULL, eUnknownDocumentType }
     103             :             };
     104           0 :             return aModuleInfo;
     105             :         }
     106             :     }
     107             : 
     108             : 
     109             :     //= DocumentClassification
     110             : 
     111             : 
     112           0 :     DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel ) SAL_THROW(())
     113             :     {
     114           0 :         DocumentType eType( eUnknownDocumentType );
     115             : 
     116             :         OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" );
     117           0 :         if ( !_rxDocumentModel.is() )
     118           0 :             return eType;
     119             : 
     120             :         try
     121             :         {
     122             :             // first, check whether the document has a ModuleIdentifier which we know
     123           0 :             Reference< XModule > xModule( _rxDocumentModel, UNO_QUERY );
     124           0 :             if ( xModule.is() )
     125           0 :                 eType = getDocumentTypeForModuleIdentifier( xModule->getIdentifier() );
     126           0 :             if ( eType != eUnknownDocumentType )
     127           0 :                 return eType;
     128             : 
     129             :             // second, check whether it supports one of the services we know
     130           0 :             Reference< XServiceInfo > xSI( _rxDocumentModel, UNO_QUERY_THROW );
     131           0 :             const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
     132           0 :             while ( pModuleInfo->pAsciiModuleOrServiceName )
     133             :             {
     134           0 :                 if ( xSI->supportsService( OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) )
     135           0 :                     return pModuleInfo->eType;
     136           0 :                 ++pModuleInfo;
     137             :             }
     138             : 
     139             :             // last: uhm, there is no last resort
     140           0 :             OSL_FAIL( "DocumentClassification::classifyDocument: unknown document!" );
     141             :         }
     142           0 :         catch( const Exception& )
     143             :         {
     144             :             DBG_UNHANDLED_EXCEPTION();
     145             :         }
     146             : 
     147           0 :         return eType;
     148             :     }
     149             : 
     150             : 
     151           0 :     DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent ) SAL_THROW(())
     152             :     {
     153           0 :         DocumentType eType( eUnknownDocumentType );
     154             : 
     155             :         try
     156             :         {
     157           0 :             Reference< XModel > xDocument( getDocument( _rxFormComponent.get() ) );
     158           0 :             if ( !xDocument.is() )
     159           0 :                 return eUnknownDocumentType;
     160           0 :             eType = classifyDocument( xDocument );
     161             :         }
     162           0 :         catch( const Exception& )
     163             :         {
     164             :             OSL_FAIL( "DocumentClassification::classifyHostDocument: caught an exception!" );
     165             :         }
     166             : 
     167           0 :         return eType;
     168             :     }
     169             : 
     170             : 
     171           0 :     DocumentType DocumentClassification::getDocumentTypeForModuleIdentifier( const OUString& _rModuleIdentifier )
     172             :     {
     173           0 :         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
     174           0 :         while ( pModuleInfo->pAsciiModuleOrServiceName )
     175             :         {
     176           0 :             if ( _rModuleIdentifier.equalsAscii( pModuleInfo->pAsciiModuleOrServiceName ) )
     177           0 :                 return pModuleInfo->eType;
     178           0 :             ++pModuleInfo;
     179             :         }
     180           0 :         return eUnknownDocumentType;
     181             :     }
     182             : 
     183             : 
     184           0 :     OUString DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType )
     185             :     {
     186           0 :         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
     187           0 :         while ( pModuleInfo->pAsciiModuleOrServiceName )
     188             :         {
     189           0 :             if ( pModuleInfo->eType == _eType )
     190           0 :                 return OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName );
     191           0 :             ++pModuleInfo;
     192             :         }
     193           0 :         return OUString();
     194             :     }
     195             : 
     196             : 
     197             : } // namespace svxform
     198             : 
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10