LCOV - code coverage report
Current view: top level - writerperfect/source/common - DirectoryStream.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 68 83 81.9 %
Date: 2014-11-03 Functions: 14 18 77.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /* writerperfect
       3             :  * Version: MPL 2.0 / LGPLv2.1+
       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             :  * Major Contributor(s):
      10             :  * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
      11             :  *
      12             :  * For minor contributions see the git repository.
      13             :  *
      14             :  * Alternatively, the contents of this file may be used under the terms
      15             :  * of the GNU Lesser General Public License Version 2.1 or later
      16             :  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
      17             :  * applicable instead of those above.
      18             :  *
      19             :  * For further information visit http://libwpd.sourceforge.net
      20             :  */
      21             : 
      22             : #include <com/sun/star/container/XChild.hpp>
      23             : #include <com/sun/star/io/XInputStream.hpp>
      24             : #include <com/sun/star/sdbc/XResultSet.hpp>
      25             : #include <com/sun/star/sdbc/XRow.hpp>
      26             : #include <com/sun/star/ucb/XContent.hpp>
      27             : #include <com/sun/star/ucb/XContentAccess.hpp>
      28             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      29             : 
      30             : #include <com/sun/star/uno/Reference.hxx>
      31             : #include <com/sun/star/uno/Sequence.hxx>
      32             : 
      33             : #include <comphelper/processfactory.hxx>
      34             : 
      35             : #include <rtl/ustring.hxx>
      36             : 
      37             : #include <ucbhelper/content.hxx>
      38             : 
      39             : #include <writerperfect/DirectoryStream.hxx>
      40             : #include <writerperfect/WPXSvInputStream.hxx>
      41             : 
      42             : namespace container = com::sun::star::container;
      43             : namespace io = com::sun::star::io;
      44             : namespace sdbc = com::sun::star::sdbc;
      45             : namespace ucb = com::sun::star::ucb;
      46             : namespace uno = com::sun::star::uno;
      47             : 
      48             : namespace writerperfect
      49             : {
      50             : 
      51             : namespace
      52             : {
      53             : 
      54           4 : uno::Reference<io::XInputStream> findStream(ucbhelper::Content &rContent, const rtl::OUString &rName)
      55             : {
      56           4 :     uno::Reference<io::XInputStream> xInputStream;
      57             : 
      58           8 :     uno::Sequence<rtl::OUString> lPropNames(1);
      59           4 :     lPropNames[0] = "Title";
      60             :     try
      61             :     {
      62             :         const uno::Reference<sdbc::XResultSet> xResultSet(
      63           4 :             rContent.createCursor(lPropNames, ucbhelper::INCLUDE_DOCUMENTS_ONLY));
      64           4 :         if (xResultSet->first())
      65             :         {
      66           4 :             const uno::Reference<ucb::XContentAccess> xContentAccess(xResultSet, uno::UNO_QUERY_THROW);
      67           8 :             const uno::Reference<sdbc::XRow> xRow(xResultSet, uno::UNO_QUERY_THROW);
      68           8 :             do
      69             :             {
      70          12 :                 const rtl::OUString aTitle(xRow->getString(1));
      71          12 :                 if (aTitle == rName)
      72             :                 {
      73           4 :                     const uno::Reference<ucb::XContent> xSubContent(xContentAccess->queryContent());
      74           8 :                     ucbhelper::Content aSubContent(xSubContent, uno::Reference<ucb::XCommandEnvironment>(), comphelper::getProcessComponentContext());
      75           4 :                     xInputStream = aSubContent.openStream();
      76           8 :                     break;
      77           8 :                 }
      78             :             }
      79          12 :             while (xResultSet->next());
      80           4 :         }
      81             :     }
      82           0 :     catch (const uno::RuntimeException &)
      83             :     {
      84             :         // ignore
      85             :     }
      86           0 :     catch (const uno::Exception &)
      87             :     {
      88             :         // ignore
      89             :     }
      90             : 
      91           8 :     return xInputStream;
      92             : }
      93             : 
      94             : }
      95             : 
      96          10 : struct DirectoryStream::Impl
      97             : {
      98             :     Impl(const uno::Reference<ucb::XContent> &rxContent);
      99             : 
     100             :     uno::Reference<ucb::XContent> xContent;
     101             : };
     102             : 
     103          10 : DirectoryStream::Impl::Impl(const uno::Reference<ucb::XContent> &rxContent)
     104          10 :     : xContent(rxContent)
     105             : {
     106          10 : }
     107             : 
     108          16 : DirectoryStream::DirectoryStream(const com::sun::star::uno::Reference<com::sun::star::ucb::XContent> &xContent)
     109          16 :     : m_pImpl(isDirectory(xContent) ? new Impl(xContent) : 0)
     110             : {
     111          16 : }
     112             : 
     113          40 : DirectoryStream::~DirectoryStream()
     114             : {
     115          16 :     delete m_pImpl;
     116          24 : }
     117             : 
     118           8 : DirectoryStream *DirectoryStream::createForParent(const com::sun::star::uno::Reference<com::sun::star::ucb::XContent> &xContent) try
     119             : {
     120           8 :     if (!xContent.is())
     121           0 :         return 0;
     122             : 
     123           8 :     DirectoryStream *pDir(0);
     124             : 
     125           8 :     const uno::Reference<container::XChild> xChild(xContent, uno::UNO_QUERY);
     126           8 :     if (xChild.is())
     127             :     {
     128           8 :         const uno::Reference<ucb::XContent> xDirContent(xChild->getParent(), uno::UNO_QUERY);
     129           8 :         if (xDirContent.is())
     130             :         {
     131           8 :             pDir = new writerperfect::DirectoryStream(xDirContent);
     132           8 :             if (!pDir->isStructured())
     133             :             {
     134           2 :                 delete pDir;
     135           2 :                 pDir = 0;
     136             :             }
     137           8 :         }
     138             :     }
     139             : 
     140           8 :     return pDir;
     141             : }
     142           0 : catch (...)
     143             : {
     144           0 :     return 0;
     145             : }
     146             : 
     147          22 : bool DirectoryStream::isDirectory(const com::sun::star::uno::Reference<com::sun::star::ucb::XContent> &xContent) try
     148             : {
     149          22 :     if (!xContent.is())
     150           0 :         return false;
     151             : 
     152          22 :     ucbhelper::Content aContent(xContent, uno::Reference<ucb::XCommandEnvironment>(), comphelper::getProcessComponentContext());
     153          28 :     return aContent.isFolder();
     154             : }
     155          12 : catch (...)
     156             : {
     157           6 :     return false;
     158             : }
     159             : 
     160          18 : bool DirectoryStream::isStructured()
     161             : {
     162          18 :     if (!m_pImpl)
     163           4 :         return false;
     164             : 
     165          14 :     return true;
     166             : }
     167             : 
     168           0 : unsigned DirectoryStream::subStreamCount()
     169             : {
     170             :     // TODO: implement me
     171           0 :     return 0;
     172             : }
     173             : 
     174           0 : const char *DirectoryStream::subStreamName(unsigned /* id */)
     175             : {
     176             :     // TODO: implement me
     177           0 :     return 0;
     178             : }
     179             : 
     180           0 : bool DirectoryStream::existsSubStream(const char * /* name */)
     181             : {
     182             :     // TODO: implement me
     183           0 :     return false;
     184             : }
     185             : 
     186           4 : librevenge::RVNGInputStream *DirectoryStream::getSubStreamByName(const char *const pName)
     187             : {
     188           4 :     if (!m_pImpl)
     189           0 :         return 0;
     190             : 
     191           4 :     ucbhelper::Content aContent(m_pImpl->xContent, uno::Reference<ucb::XCommandEnvironment>(), comphelper::getProcessComponentContext());
     192           8 :     const uno::Reference<io::XInputStream> xInputStream(findStream(aContent, rtl::OUString::createFromAscii(pName)));
     193           4 :     if (xInputStream.is())
     194           4 :         return new WPXSvInputStream(xInputStream);
     195             : 
     196           4 :     return 0;
     197             : }
     198             : 
     199           0 : librevenge::RVNGInputStream *DirectoryStream::getSubStreamById(unsigned /* id */)
     200             : {
     201             :     // TODO: implement me
     202           0 :     return 0;
     203             : }
     204             : 
     205           4 : const unsigned char *DirectoryStream::read(unsigned long, unsigned long &nNumBytesRead)
     206             : {
     207           4 :     nNumBytesRead = 0;
     208           4 :     return 0;
     209             : }
     210             : 
     211           4 : int DirectoryStream::seek(long, librevenge::RVNG_SEEK_TYPE)
     212             : {
     213           4 :     return -1;
     214             : }
     215             : 
     216           4 : long DirectoryStream::tell()
     217             : {
     218           4 :     return 0;
     219             : }
     220             : 
     221           4 : bool DirectoryStream::isEnd()
     222             : {
     223           4 :     return true;
     224             : }
     225             : 
     226             : }
     227             : 
     228             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10