LCOV - code coverage report
Current view: top level - writerperfect/source/writer - MSWorksImportFilter.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 33 44 75.0 %
Date: 2015-06-13 12:38:46 Functions: 8 10 80.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             : /* MSWorksImportFilter: Sets up the filter, and calls DocumentCollector
       3             :  * to do the actual filtering
       4             :  *
       5             :  * This file is part of the LibreOffice project.
       6             :  *
       7             :  * This Source Code Form is subject to the terms of the Mozilla Public
       8             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       9             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      10             :  */
      11             : 
      12             : #include <boost/scoped_ptr.hpp>
      13             : #include <com/sun/star/uno/Reference.h>
      14             : #include <cppuhelper/supportsservice.hxx>
      15             : 
      16             : #include <libwps/libwps.h>
      17             : 
      18             : #include "WPFTEncodingDialog.hxx"
      19             : #include "MSWorksImportFilter.hxx"
      20             : 
      21             : using com::sun::star::uno::Sequence;
      22             : using com::sun::star::uno::Reference;
      23             : using com::sun::star::uno::Any;
      24             : using com::sun::star::uno::XInterface;
      25             : using com::sun::star::uno::Exception;
      26             : using com::sun::star::uno::RuntimeException;
      27             : using com::sun::star::uno::XComponentContext;
      28             : 
      29           0 : static bool handleEmbeddedWKSObject(const librevenge::RVNGBinaryData &data, OdfDocumentHandler *pHandler,  const OdfStreamType streamType)
      30             : {
      31           0 :     OdsGenerator exporter;
      32           0 :     exporter.addDocumentHandler(pHandler, streamType);
      33           0 :     return libwps::WPSDocument::parse(data.getDataStream(), &exporter)==libwps::WPS_OK;
      34             : }
      35             : 
      36           5 : bool MSWorksImportFilter::doImportDocument(librevenge::RVNGInputStream &rInput, OdtGenerator &rGenerator, utl::MediaDescriptor &)
      37             : {
      38           5 :     libwps::WPSKind kind = libwps::WPS_TEXT;
      39             :     libwps::WPSCreator creator;
      40           5 :     bool needEncoding = false;
      41           5 :     const libwps::WPSConfidence confidence = libwps::WPSDocument::isFileFormatSupported(&rInput, kind, creator, needEncoding);
      42             : 
      43           5 :     std::string fileEncoding("");
      44             :     try
      45             :     {
      46           5 :         if ((kind == libwps::WPS_TEXT) && (confidence == libwps::WPS_CONFIDENCE_EXCELLENT) && needEncoding)
      47             :         {
      48             :             const ScopedVclPtrInstance<writerperfect::WPFTEncodingDialog> pDlg(
      49           1 :                 "Import MsWorks files(libwps)", "CP850");
      50           0 :             if (pDlg->Execute() == RET_OK)
      51             :             {
      52           0 :                 if (!pDlg->GetEncoding().isEmpty())
      53           0 :                     fileEncoding=pDlg->GetEncoding().toUtf8().getStr();
      54             :             }
      55             :             // we can fail because we are in headless mode, the user has cancelled conversion, ...
      56           0 :             else if (pDlg->hasUserCalledCancel())
      57           0 :                 return false;
      58             :         }
      59             :     }
      60           1 :     catch (css::uno::Exception &e)
      61             :     {
      62             :         SAL_WARN("writerperfect", "ignoring Exception " << e.Message);
      63             :     }
      64           5 :     return libwps::WPS_OK == libwps::WPSDocument::parse(&rInput, &rGenerator, "", fileEncoding.c_str());
      65             : }
      66             : 
      67          89 : bool MSWorksImportFilter::doDetectFormat(librevenge::RVNGInputStream &rInput, OUString &rTypeName)
      68             : {
      69          89 :     libwps::WPSKind kind = libwps::WPS_TEXT;
      70             :     libwps::WPSCreator creator;
      71             :     bool needEncoding;
      72          89 :     const libwps::WPSConfidence confidence = libwps::WPSDocument::isFileFormatSupported(&rInput, kind, creator, needEncoding);
      73             : 
      74          89 :     if ((kind == libwps::WPS_TEXT) && (confidence == libwps::WPS_CONFIDENCE_EXCELLENT))
      75             :     {
      76           5 :         rTypeName = "writer_MS_Works_Document";
      77           5 :         return true;
      78             :     }
      79             : 
      80          84 :     return false;
      81             : }
      82             : 
      83           5 : void MSWorksImportFilter::doRegisterHandlers(OdtGenerator &rGenerator)
      84             : {
      85           5 :     rGenerator.registerEmbeddedObjectHandler("image/wks-ods", &handleEmbeddedWKSObject);
      86           5 : }
      87             : 
      88          33 : OUString MSWorksImportFilter_getImplementationName()
      89             : throw (RuntimeException)
      90             : {
      91          33 :     return OUString("com.sun.star.comp.Writer.MSWorksImportFilter");
      92             : }
      93             : 
      94           5 : Sequence< OUString > SAL_CALL MSWorksImportFilter_getSupportedServiceNames()
      95             : throw (RuntimeException)
      96             : {
      97           5 :     Sequence < OUString > aRet(2);
      98           5 :     OUString *pArray = aRet.getArray();
      99           5 :     pArray[0] =  "com.sun.star.document.ImportFilter";
     100           5 :     pArray[1] =  "com.sun.star.document.ExtendedTypeDetection";
     101           5 :     return aRet;
     102             : }
     103             : #undef SERVICE_NAME2
     104             : #undef SERVICE_NAME1
     105             : 
     106          86 : Reference< XInterface > SAL_CALL MSWorksImportFilter_createInstance(const Reference< XComponentContext > &rContext)
     107             : throw(Exception)
     108             : {
     109          86 :     return static_cast<cppu::OWeakObject *>(new MSWorksImportFilter(rContext));
     110             : }
     111             : 
     112             : // XServiceInfo
     113           1 : OUString SAL_CALL MSWorksImportFilter::getImplementationName()
     114             : throw (RuntimeException, std::exception)
     115             : {
     116           1 :     return MSWorksImportFilter_getImplementationName();
     117             : }
     118           0 : sal_Bool SAL_CALL MSWorksImportFilter::supportsService(const OUString &rServiceName)
     119             : throw (RuntimeException, std::exception)
     120             : {
     121           0 :     return cppu::supportsService(this, rServiceName);
     122             : }
     123           1 : Sequence< OUString > SAL_CALL MSWorksImportFilter::getSupportedServiceNames()
     124             : throw (RuntimeException, std::exception)
     125             : {
     126           1 :     return MSWorksImportFilter_getSupportedServiceNames();
     127             : }
     128             : 
     129             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11