LCOV - code coverage report
Current view: top level - svx/source/core - graphichelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 131 0.8 %
Date: 2015-06-13 12:38:46 Functions: 2 5 40.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             : #include <unotools/pathoptions.hxx>
      21             : #include <vcl/graphicfilter.hxx>
      22             : #include <sfx2/docfile.hxx>
      23             : #include <sfx2/filedlghelper.hxx>
      24             : #include <svx/xoutbmp.hxx>
      25             : #include <svx/dialmgr.hxx>
      26             : #include <svx/graphichelper.hxx>
      27             : #include <svx/dialogs.hrc>
      28             : 
      29             : #include <cppuhelper/exc_hlp.hxx>
      30             : #include <comphelper/anytostring.hxx>
      31             : #include <comphelper/processfactory.hxx>
      32             : 
      33             : #include <com/sun/star/beans/PropertyValues.hpp>
      34             : #include <com/sun/star/beans/PropertyValue.hpp>
      35             : #include <com/sun/star/document/XExporter.hpp>
      36             : #include <com/sun/star/document/XFilter.hpp>
      37             : #include <com/sun/star/drawing/GraphicExportFilter.hpp>
      38             : #include <com/sun/star/graphic/XGraphicProvider.hpp>
      39             : #include <com/sun/star/graphic/GraphicType.hpp>
      40             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      41             : #include <com/sun/star/lang/XComponent.hpp>
      42             : #include <com/sun/star/io/XInputStream.hpp>
      43             : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
      44             : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
      45             : #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
      46             : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
      47             : 
      48             : using namespace css::uno;
      49             : using namespace css::lang;
      50             : using namespace css::graphic;
      51             : using namespace css::ucb;
      52             : using namespace css::beans;
      53             : using namespace css::io;
      54             : using namespace css::document;
      55             : using namespace css::ui::dialogs;
      56             : 
      57             : using namespace sfx2;
      58             : 
      59             : namespace drawing = com::sun::star::drawing;
      60             : 
      61           0 : void GraphicHelper::GetPreferredExtension( OUString& rExtension, const Graphic& rGraphic )
      62             : {
      63           0 :     OUString aExtension = "png";
      64           0 :     switch( rGraphic.GetLink().GetType() )
      65             :     {
      66             :         case GFX_LINK_TYPE_NATIVE_GIF:
      67           0 :             aExtension = "gif";
      68           0 :             break;
      69             :         case GFX_LINK_TYPE_NATIVE_TIF:
      70           0 :             aExtension = "tif";
      71           0 :             break;
      72             :         case GFX_LINK_TYPE_NATIVE_WMF:
      73           0 :             aExtension = "wmf";
      74           0 :             break;
      75             :         case GFX_LINK_TYPE_NATIVE_MET:
      76           0 :             aExtension = "met";
      77           0 :             break;
      78             :         case GFX_LINK_TYPE_NATIVE_PCT:
      79           0 :             aExtension = "pct";
      80           0 :             break;
      81             :         case GFX_LINK_TYPE_NATIVE_JPG:
      82           0 :             aExtension = "jpg";
      83           0 :             break;
      84             :         case GFX_LINK_TYPE_NATIVE_BMP:
      85           0 :             aExtension = "bmp";
      86           0 :             break;
      87             :         case GFX_LINK_TYPE_NATIVE_SVG:
      88           0 :             aExtension = "svg";
      89           0 :             break;
      90             :         default:
      91           0 :             break;
      92             :     }
      93           0 :     rExtension = aExtension;
      94           0 : }
      95             : 
      96           0 : OUString GraphicHelper::ExportGraphic( const Graphic& rGraphic, const OUString& rGraphicName )
      97             : {
      98           0 :     SvtPathOptions aPathOpt;
      99           0 :     OUString sGraphicsPath( aPathOpt.GetGraphicPath() );
     100             : 
     101           0 :     FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
     102           0 :     Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker();
     103             : 
     104           0 :     INetURLObject aPath;
     105           0 :     aPath.SetSmartURL( sGraphicsPath );
     106             : 
     107             :     // fish out the graphic's name
     108           0 :     OUString aName = rGraphicName;
     109             : 
     110           0 :     aDialogHelper.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE));
     111           0 :     aDialogHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
     112           0 :     INetURLObject aURL;
     113           0 :     aURL.SetSmartURL( aName );
     114           0 :     aDialogHelper.SetFileName( aURL.GetName() );
     115             : 
     116           0 :     GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
     117           0 :     const sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount();
     118             : 
     119           0 :     OUString aExtension( aURL.GetExtension() );
     120           0 :     if( aExtension.isEmpty() )
     121             :     {
     122           0 :         GetPreferredExtension( aExtension, rGraphic );
     123             :     }
     124             : 
     125           0 :     aExtension = aExtension.toAsciiLowerCase();
     126           0 :     sal_uInt16 nDefaultFilter = USHRT_MAX;
     127             : 
     128           0 :     Reference<XFilterManager> xFilterManager(xFilePicker, UNO_QUERY);
     129             : 
     130           0 :     for ( sal_uInt16 i = 0; i < nCount; i++ )
     131             :     {
     132           0 :         xFilterManager->appendFilter( rGraphicFilter.GetExportFormatName( i ), rGraphicFilter.GetExportWildcard( i ) );
     133           0 :         OUString aFormatShortName = rGraphicFilter.GetExportFormatShortName( i );
     134           0 :         if ( aFormatShortName.equalsIgnoreAsciiCase( aExtension ) )
     135             :         {
     136           0 :             nDefaultFilter = i;
     137             :         }
     138           0 :     }
     139           0 :     if ( USHRT_MAX == nDefaultFilter )
     140             :     {
     141             :         // "wrong" extension?
     142           0 :         GetPreferredExtension( aExtension, rGraphic );
     143           0 :         for ( sal_uInt16 i = 0; i < nCount; ++i )
     144           0 :             if ( aExtension == rGraphicFilter.GetExportFormatShortName( i ).toAsciiLowerCase() )
     145             :             {
     146           0 :                 nDefaultFilter =  i;
     147           0 :                 break;
     148             :             }
     149             :     }
     150             : 
     151           0 :     if( USHRT_MAX != nDefaultFilter )
     152             :     {
     153           0 :         xFilterManager->setCurrentFilter( rGraphicFilter.GetExportFormatName( nDefaultFilter ) ) ;
     154             : 
     155           0 :         if( aDialogHelper.Execute() == ERRCODE_NONE )
     156             :         {
     157           0 :             OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
     158             :             // remember used path - please don't optimize away!
     159           0 :             aPath.SetSmartURL( sPath);
     160           0 :             sGraphicsPath = aPath.GetPath();
     161             : 
     162           0 :             if( !rGraphicName.isEmpty() &&
     163           0 :                 nDefaultFilter == rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter()))
     164             :             {
     165             :                 // try to save the original graphic
     166           0 :                 SfxMedium aIn( rGraphicName, StreamMode::READ | StreamMode::NOCREATE );
     167           0 :                 if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
     168             :                 {
     169           0 :                     SfxMedium aOut( sPath, StreamMode::WRITE | StreamMode::SHARE_DENYNONE);
     170           0 :                     if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
     171             :                     {
     172           0 :                         aOut.GetOutStream()->WriteStream( *aIn.GetInStream() );
     173           0 :                         if ( 0 == aIn.GetError() )
     174             :                         {
     175           0 :                             aOut.Close();
     176           0 :                             aOut.Commit();
     177           0 :                             if ( 0 == aOut.GetError() )
     178           0 :                                 return sPath;
     179             :                         }
     180           0 :                     }
     181           0 :                 }
     182             :             }
     183             : 
     184             :             sal_uInt16 nFilter;
     185           0 :             if ( !xFilterManager->getCurrentFilter().isEmpty() && rGraphicFilter.GetExportFormatCount() )
     186             :             {
     187           0 :                 nFilter = rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter() );
     188             :             }
     189             :             else
     190             :             {
     191           0 :                 nFilter = GRFILTER_FORMAT_DONTKNOW;
     192             :             }
     193           0 :             OUString aFilter( rGraphicFilter.GetExportFormatShortName( nFilter ) );
     194             : 
     195             :             XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter,
     196             :                                         XOUTBMP_DONT_EXPAND_FILENAME |
     197             :                                         XOUTBMP_DONT_ADD_EXTENSION |
     198           0 :                                         XOUTBMP_USE_NATIVE_IF_POSSIBLE );
     199           0 :             return sPath;
     200             :         }
     201             :     }
     202           0 :     return OUString();
     203             : }
     204             : 
     205           0 : void GraphicHelper::SaveShapeAsGraphic( const Reference< drawing::XShape >& xShape )
     206             : {
     207             :     try
     208             :     {
     209           0 :         Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
     210           0 :         Reference< XPropertySet > xShapeSet( xShape, UNO_QUERY_THROW );
     211             : 
     212           0 :         SvtPathOptions aPathOpt;
     213           0 :         OUString sGraphicPath( aPathOpt.GetGraphicPath() );
     214             : 
     215           0 :         FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
     216           0 :         Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker();
     217             : 
     218           0 :         aDialogHelper.SetTitle( "Save as Image" );
     219             : 
     220           0 :         INetURLObject aPath;
     221           0 :         aPath.SetSmartURL( sGraphicPath );
     222           0 :         xFilePicker->setDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
     223             : 
     224             :         // populate filter dialog filter list and select default filter to match graphic mime type
     225             : 
     226           0 :         GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
     227           0 :         Reference<XFilterManager> xFilterManager( xFilePicker, UNO_QUERY );
     228           0 :         const OUString aDefaultMimeType("image/png");
     229           0 :         OUString aDefaultFormatName;
     230           0 :         sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount();
     231             : 
     232           0 :         std::map< OUString, OUString > aMimeTypeMap;
     233             : 
     234           0 :         for ( sal_uInt16 i = 0; i < nCount; i++ )
     235             :         {
     236           0 :             const OUString aExportFormatName( rGraphicFilter.GetExportFormatName( i ) );
     237           0 :             const OUString aFilterMimeType( rGraphicFilter.GetExportFormatMediaType( i ) );
     238           0 :             xFilterManager->appendFilter( aExportFormatName, rGraphicFilter.GetExportWildcard( i ) );
     239           0 :             aMimeTypeMap[ aExportFormatName ] = aFilterMimeType;
     240           0 :             if( aDefaultMimeType == aFilterMimeType )
     241           0 :                 aDefaultFormatName = aExportFormatName;
     242           0 :         }
     243             : 
     244           0 :         if( !aDefaultFormatName.isEmpty() )
     245           0 :             xFilterManager->setCurrentFilter( aDefaultFormatName );
     246             : 
     247             :         // execute dialog
     248             : 
     249           0 :         if( aDialogHelper.Execute() == ERRCODE_NONE )
     250             :         {
     251           0 :             OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
     252           0 :             OUString aExportMimeType( aMimeTypeMap[xFilterManager->getCurrentFilter()] );
     253             : 
     254           0 :             Reference< XInputStream > xGraphStream;
     255             : 
     256           0 :             if( xGraphStream.is() )
     257             :             {
     258           0 :                 Reference<XSimpleFileAccess3> xFileAccess = SimpleFileAccess::create( xContext );
     259           0 :                 xFileAccess->writeFile( sPath, xGraphStream );
     260             :             }
     261             :             else
     262             :             {
     263           0 :                 Reference<css::drawing::XGraphicExportFilter> xGraphicExporter = css::drawing::GraphicExportFilter::create( xContext );
     264             : 
     265           0 :                 Sequence<PropertyValue> aDescriptor( 2 );
     266           0 :                 aDescriptor[0].Name = "MediaType";
     267           0 :                 aDescriptor[0].Value <<= aExportMimeType;
     268           0 :                 aDescriptor[1].Name = "URL";
     269           0 :                 aDescriptor[1].Value <<= sPath;
     270             : 
     271           0 :                 Reference< XComponent > xSourceDocument = Reference< XComponent >( xShape, UNO_QUERY_THROW );
     272           0 :                 if ( xSourceDocument.is() )
     273             :                 {
     274           0 :                     xGraphicExporter->setSourceDocument( xSourceDocument );
     275           0 :                     xGraphicExporter->filter( aDescriptor );
     276           0 :                 }
     277           0 :             }
     278           0 :         }
     279             :     }
     280           0 :     catch( Exception& )
     281             :     {
     282             :     }
     283         435 : }
     284             : 
     285             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11