LCOV - code coverage report
Current view: top level - svx/source/form - fmdocumentclassification.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 41 53 77.4 %
Date: 2014-11-03 Functions: 7 7 100.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         800 :         Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
      49             :         {
      50         800 :             Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
      51         800 :             if ( xTypedNode.is() )
      52         502 :                 return xTypedNode;
      53             :             else
      54             :             {
      55         298 :                 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
      56         298 :                 if ( xChild.is() )
      57         296 :                     return getTypedModelNode< TYPE >( xChild->getParent() );
      58             :                 else
      59           2 :                     return Reference< TYPE >();
      60         800 :             }
      61             :         }
      62             : 
      63             : 
      64         504 :         Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
      65             :         {
      66         504 :             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        6412 :         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        6412 :             return aModuleInfo;
     105             :         }
     106             :     }
     107             : 
     108             : 
     109             :     //= DocumentClassification
     110             : 
     111             : 
     112        5908 :     DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel )
     113             :     {
     114        5908 :         DocumentType eType( eUnknownDocumentType );
     115             : 
     116             :         OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" );
     117        5908 :         if ( !_rxDocumentModel.is() )
     118           0 :             return eType;
     119             : 
     120             :         try
     121             :         {
     122             :             // first, check whether the document has a ModuleIdentifier which we know
     123        5908 :             Reference< XModule > xModule( _rxDocumentModel, UNO_QUERY );
     124        5908 :             if ( xModule.is() )
     125        5908 :                 eType = getDocumentTypeForModuleIdentifier( xModule->getIdentifier() );
     126        5908 :             if ( eType != eUnknownDocumentType )
     127        5908 :                 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         504 :     DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent )
     152             :     {
     153         504 :         DocumentType eType( eUnknownDocumentType );
     154             : 
     155             :         try
     156             :         {
     157         504 :             Reference< XModel > xDocument( getDocument( _rxFormComponent.get() ) );
     158         504 :             if ( !xDocument.is() )
     159           2 :                 return eUnknownDocumentType;
     160         502 :             eType = classifyDocument( xDocument );
     161             :         }
     162           0 :         catch( const Exception& )
     163             :         {
     164             :             OSL_FAIL( "DocumentClassification::classifyHostDocument: caught an exception!" );
     165             :         }
     166             : 
     167         502 :         return eType;
     168             :     }
     169             : 
     170             : 
     171        5908 :     DocumentType DocumentClassification::getDocumentTypeForModuleIdentifier( const OUString& _rModuleIdentifier )
     172             :     {
     173        5908 :         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
     174       14006 :         while ( pModuleInfo->pAsciiModuleOrServiceName )
     175             :         {
     176        8098 :             if ( _rModuleIdentifier.equalsAscii( pModuleInfo->pAsciiModuleOrServiceName ) )
     177        5908 :                 return pModuleInfo->eType;
     178        2190 :             ++pModuleInfo;
     179             :         }
     180           0 :         return eUnknownDocumentType;
     181             :     }
     182             : 
     183             : 
     184         504 :     OUString DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType )
     185             :     {
     186         504 :         const ModuleInfo* pModuleInfo = lcl_getModuleInfo();
     187        1440 :         while ( pModuleInfo->pAsciiModuleOrServiceName )
     188             :         {
     189         934 :             if ( pModuleInfo->eType == _eType )
     190         502 :                 return OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName );
     191         432 :             ++pModuleInfo;
     192             :         }
     193           2 :         return OUString();
     194             :     }
     195             : 
     196             : 
     197             : } // namespace svxform
     198             : 
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10