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

Generated by: LCOV version 1.11