LCOV - code coverage report
Current view: top level - sw/source/core/unocore - swunohelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 51 98 52.0 %
Date: 2014-04-11 Functions: 4 8 50.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 <com/sun/star/uno/Sequence.h>
      21             : #include <com/sun/star/uno/Exception.hpp>
      22             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      23             : #include <com/sun/star/ucb/XContentIdentifier.hpp>
      24             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      25             : #include <com/sun/star/ucb/TransferInfo.hpp>
      26             : #include <com/sun/star/ucb/NameClash.hpp>
      27             : #include <com/sun/star/sdbc/XResultSet.hpp>
      28             : #include <com/sun/star/sdbc/XRow.hpp>
      29             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      30             : #include <comphelper/processfactory.hxx>
      31             : #include <comphelper/types.hxx>
      32             : #include <tools/urlobj.hxx>
      33             : #include <tools/datetime.hxx>
      34             : #include <rtl/ustring.hxx>
      35             : #include <ucbhelper/contentidentifier.hxx>
      36             : #include <ucbhelper/content.hxx>
      37             : #include <swunohelper.hxx>
      38             : 
      39             : namespace SWUnoHelper {
      40             : 
      41        6219 : sal_Int32 GetEnumAsInt32( const ::com::sun::star::uno::Any& rVal )
      42             : {
      43             :     sal_Int32 eVal;
      44             :     try
      45             :     {
      46        6219 :         eVal = comphelper::getEnumAsINT32( rVal );
      47             :     }
      48           0 :     catch( ::com::sun::star::uno::Exception & )
      49             :     {
      50           0 :         eVal = 0;
      51             :         OSL_FAIL( "can't get EnumAsInt32" );
      52             :     }
      53        6219 :     return eVal;
      54             : }
      55             : 
      56             : // methods for UCB actions
      57           1 : sal_Bool UCB_DeleteFile( const OUString& rURL )
      58             : {
      59             :     sal_Bool bRemoved;
      60             :     try
      61             :     {
      62             :         ucbhelper::Content aTempContent( rURL,
      63             :                                 ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
      64           1 :                                 comphelper::getProcessComponentContext() );
      65             :         aTempContent.executeCommand(OUString("delete"),
      66           1 :                         ::com::sun::star::uno::makeAny( sal_True ) );
      67           1 :         bRemoved = sal_True;
      68             :     }
      69           0 :     catch( ::com::sun::star::uno::Exception& )
      70             :     {
      71           0 :         bRemoved = sal_False;
      72             :         OSL_FAIL( "Exeception from executeCommand( delete )" );
      73             :     }
      74           1 :     return bRemoved;
      75             : }
      76             : 
      77           2 : sal_Bool UCB_CopyFile( const OUString& rURL, const OUString& rNewURL, sal_Bool bCopyIsMove )
      78             : {
      79           2 :     sal_Bool bCopyCompleted = sal_True;
      80             :     try
      81             :     {
      82           2 :         INetURLObject aURL( rNewURL );
      83           4 :         OUString sName( aURL.GetName() );
      84           2 :         aURL.removeSegment();
      85           4 :         OUString sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
      86             : 
      87             :         ucbhelper::Content aTempContent( sMainURL,
      88             :                                 ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
      89           4 :                                 comphelper::getProcessComponentContext() );
      90             : 
      91           4 :         ::com::sun::star::uno::Any aAny;
      92           4 :         ::com::sun::star::ucb::TransferInfo aInfo;
      93           2 :         aInfo.NameClash = ::com::sun::star::ucb::NameClash::ERROR;
      94           2 :         aInfo.NewTitle = sName;
      95           2 :         aInfo.SourceURL = rURL;
      96           2 :         aInfo.MoveData = bCopyIsMove;
      97           2 :         aAny <<= aInfo;
      98             :         aTempContent.executeCommand( OUString("transfer"),
      99           4 :                                      aAny );
     100             :     }
     101           0 :     catch( ::com::sun::star::uno::Exception& )
     102             :     {
     103             :         OSL_FAIL( "Exeception from executeCommand( transfer )" );
     104           0 :         bCopyCompleted = sal_False;
     105             :     }
     106           2 :     return bCopyCompleted;
     107             : }
     108             : 
     109           0 : sal_Bool UCB_IsCaseSensitiveFileName( const OUString& rURL )
     110             : {
     111             :     sal_Bool bCaseSensitive;
     112             :     try
     113             :     {
     114           0 :         INetURLObject aTempObj( rURL );
     115           0 :         aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
     116             :         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef1 = new
     117           0 :                 ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
     118             : 
     119           0 :         aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
     120             :         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef2 = new
     121           0 :                 ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
     122             : 
     123             :         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XUniversalContentBroker > xUcb =
     124           0 :               com::sun::star::ucb::UniversalContentBroker::create(comphelper::getProcessComponentContext());
     125             : 
     126           0 :         sal_Int32 nCompare = xUcb->compareContentIds( xRef1, xRef2 );
     127           0 :         bCaseSensitive = 0 != nCompare;
     128             :     }
     129           0 :     catch( ::com::sun::star::uno::Exception& )
     130             :     {
     131           0 :         bCaseSensitive = sal_False;
     132             :         OSL_FAIL( "Exeception from compareContentIds()" );
     133             :     }
     134           0 :     return bCaseSensitive;
     135             : }
     136             : 
     137           0 : sal_Bool UCB_IsReadOnlyFileName( const OUString& rURL )
     138             : {
     139           0 :     sal_Bool bIsReadOnly = sal_False;
     140             :     try
     141             :     {
     142           0 :         ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
     143             :         ::com::sun::star::uno::Any aAny = aCnt.getPropertyValue(
     144           0 :             OUString("IsReadOnly"));
     145           0 :         if(aAny.hasValue())
     146           0 :             bIsReadOnly = *(sal_Bool*)aAny.getValue();
     147             :     }
     148           0 :     catch( ::com::sun::star::uno::Exception& )
     149             :     {
     150           0 :         bIsReadOnly = sal_False;
     151             :     }
     152           0 :     return bIsReadOnly;
     153             : }
     154             : 
     155           0 : sal_Bool UCB_IsFile( const OUString& rURL )
     156             : {
     157           0 :     sal_Bool bExists = sal_False;
     158             :     try
     159             :     {
     160           0 :         ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
     161           0 :         bExists = aContent.isDocument();
     162             :     }
     163           0 :     catch (::com::sun::star::uno::Exception &)
     164             :     {
     165             :     }
     166           0 :     return bExists;
     167             : }
     168             : 
     169           0 : sal_Bool UCB_IsDirectory( const OUString& rURL )
     170             : {
     171           0 :     sal_Bool bExists = sal_False;
     172             :     try
     173             :     {
     174           0 :         ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
     175           0 :         bExists = aContent.isFolder();
     176             :     }
     177           0 :     catch (::com::sun::star::uno::Exception &)
     178             :     {
     179             :     }
     180           0 :     return bExists;
     181             : }
     182             : 
     183             :     // get a list of files from the folder of the URL
     184             :     // options: pExtension = 0 -> all, else this specific extension
     185             :     //          pDateTime != 0 -> returns also the modified date/time of
     186             :     //                       the files in a std::vector<OUString> -->
     187             :     //                       !! objects must be deleted from the caller!!
     188           2 : bool UCB_GetFileListOfFolder( const OUString& rURL,
     189             :                                 std::vector<OUString>& rList,
     190             :                                 const OUString* pExtension,
     191             :                                 std::vector< ::DateTime* >* pDateTimeList )
     192             : {
     193           2 :     bool bOk = false;
     194             :     try
     195             :     {
     196           2 :         ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
     197           4 :         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
     198             : 
     199           2 :         sal_uInt16 nSeqSize = pDateTimeList ? 2 : 1;
     200           4 :         ::com::sun::star::uno::Sequence < OUString > aProps( nSeqSize );
     201           2 :         OUString* pProps = aProps.getArray();
     202           2 :         pProps[ 0 ] = "Title";
     203           2 :         if( pDateTimeList )
     204           0 :             pProps[ 1 ] = "DateModified";
     205             : 
     206             :         try
     207             :         {
     208           2 :             xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
     209             :         }
     210           0 :         catch( ::com::sun::star::uno::Exception& )
     211             :         {
     212             :             OSL_FAIL( "create cursor failed!" );
     213             :         }
     214             : 
     215           2 :         if( xResultSet.is() )
     216             :         {
     217           2 :             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow > xRow( xResultSet, ::com::sun::star::uno::UNO_QUERY );
     218           2 :             const sal_Int32 nExtLen = pExtension ? pExtension->getLength() : 0;
     219             :             try
     220             :             {
     221           2 :                 if( xResultSet->first() )
     222             :                 {
     223           4 :                     do {
     224           4 :                         const OUString sTitle( xRow->getString( 1 ) );
     225          12 :                         if( !nExtLen ||
     226           8 :                             ( sTitle.getLength() > nExtLen &&
     227           4 :                               sTitle.endsWith( *pExtension )) )
     228             :                         {
     229           4 :                             rList.push_back( sTitle );
     230             : 
     231           4 :                             if( pDateTimeList )
     232             :                             {
     233           0 :                                 ::com::sun::star::util::DateTime aStamp = xRow->getTimestamp(2);
     234             :                                 ::DateTime* pDateTime = new ::DateTime(
     235             :                                         ::Date( aStamp.Day,
     236             :                                                 aStamp.Month,
     237             :                                                 aStamp.Year ),
     238             :                                         ::Time( aStamp.Hours,
     239             :                                                 aStamp.Minutes,
     240             :                                                 aStamp.Seconds,
     241           0 :                                                 aStamp.NanoSeconds ));
     242           0 :                                 pDateTimeList->push_back( pDateTime );
     243             :                             }
     244           4 :                         }
     245             : 
     246           4 :                     } while( xResultSet->next() );
     247             :                 }
     248           2 :                 bOk = true;
     249             :             }
     250           0 :             catch( ::com::sun::star::uno::Exception& )
     251             :             {
     252             :                 OSL_FAIL( "Exception caught!" );
     253           2 :             }
     254           2 :         }
     255             :     }
     256           0 :     catch( ::com::sun::star::uno::Exception& )
     257             :     {
     258             :         OSL_FAIL( "Exception caught!" );
     259           0 :         bOk = false;
     260             :     }
     261           2 :     return bOk;
     262             : }
     263             : 
     264             : }
     265             : 
     266             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10