LCOV - code coverage report
Current view: top level - desktop/source/deployment/misc - dp_ucb.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 66 133 49.6 %
Date: 2014-11-03 Functions: 5 6 83.3 %
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 "deployment.hrc"
      22             : #include "dp_misc.h"
      23             : #include "dp_ucb.h"
      24             : #include <rtl/uri.hxx>
      25             : #include <rtl/ustrbuf.hxx>
      26             : #include <ucbhelper/content.hxx>
      27             : #include <xmlscript/xml_helper.hxx>
      28             : #include <com/sun/star/io/XInputStream.hpp>
      29             : #include <com/sun/star/ucb/CommandFailedException.hpp>
      30             : #include <com/sun/star/ucb/ContentInfo.hpp>
      31             : #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
      32             : #include <comphelper/processfactory.hxx>
      33             : 
      34             : using namespace ::com::sun::star;
      35             : using namespace ::com::sun::star::uno;
      36             : using namespace ::com::sun::star::ucb;
      37             : 
      38             : namespace dp_misc
      39             : {
      40             : 
      41             : 
      42        8864 : bool create_ucb_content(
      43             :     ::ucbhelper::Content * ret_ucbContent, OUString const & url,
      44             :     Reference<XCommandEnvironment> const & xCmdEnv,
      45             :     bool throw_exc )
      46             : {
      47             :     try {
      48             :         // Existense check...
      49             :         // content ctor/isFolder() will throw exception in case the resource
      50             :         // does not exist.
      51             : 
      52             :         // dilemma: no chance to use the given iahandler here, because it would
      53             :         //          raise no such file dialogs, else no interaction for
      54             :         //          passwords, ...? xxx todo
      55             :         ::ucbhelper::Content ucbContent(
      56             :             url, Reference<XCommandEnvironment>(),
      57        8994 :             comphelper::getProcessComponentContext() );
      58             : 
      59        8734 :         ucbContent.isFolder();
      60             : 
      61        4760 :         if (ret_ucbContent != 0)
      62             :         {
      63        4632 :             ucbContent.setCommandEnvironment( xCmdEnv );
      64        4632 :             *ret_ucbContent = ucbContent;
      65             :         }
      66        8734 :         return true;
      67             :     }
      68           0 :     catch (const RuntimeException &) {
      69           0 :         throw;
      70             :     }
      71        8208 :     catch (const Exception &) {
      72        4104 :         if (throw_exc)
      73           0 :             throw;
      74             :     }
      75        4104 :     return false;
      76             : }
      77             : 
      78             : 
      79        7516 : bool create_folder(
      80             :     ::ucbhelper::Content * ret_ucb_content, OUString const & url_,
      81             :     Reference<XCommandEnvironment> const & xCmdEnv, bool throw_exc )
      82             : {
      83        7516 :     ::ucbhelper::Content ucb_content;
      84        7516 :     if (create_ucb_content(
      85             :             &ucb_content, url_, xCmdEnv, false /* no throw */ ))
      86             :     {
      87        4526 :         if (ucb_content.isFolder()) {
      88        4526 :             if (ret_ucb_content != 0)
      89        2718 :                 *ret_ucb_content = ucb_content;
      90        4526 :             return true;
      91             :         }
      92             :     }
      93             : 
      94        5980 :     OUString url( url_ );
      95             :     // xxx todo: find parent
      96        2990 :     sal_Int32 slash = url.lastIndexOf( '/' );
      97        2990 :     if (slash < 0) {
      98             :         // fallback:
      99         180 :         url = expandUnoRcUrl( url );
     100         180 :         slash = url.lastIndexOf( '/' );
     101             :     }
     102        2990 :     if (slash < 0) {
     103             :         // invalid: has to be at least "auth:/..."
     104           0 :         if (throw_exc)
     105             :             throw ContentCreationException(
     106           0 :                 "Cannot create folder (invalid path): " + url,
     107           0 :                 Reference<XInterface>(), ContentCreationError_UNKNOWN );
     108           0 :         return false;
     109             :     }
     110        5980 :     ::ucbhelper::Content parentContent;
     111        5876 :     if (! create_folder(
     112        5980 :             &parentContent, url.copy( 0, slash ), xCmdEnv, throw_exc ))
     113          24 :         return false;
     114             :     const Any title( ::rtl::Uri::decode( url.copy( slash + 1 ),
     115             :                                          rtl_UriDecodeWithCharset,
     116        5828 :                                          RTL_TEXTENCODING_UTF8 ) );
     117             :     const Sequence<ContentInfo> infos(
     118        5828 :         parentContent.queryCreatableContentsInfo() );
     119        5828 :     for ( sal_Int32 pos = 0; pos < infos.getLength(); ++pos )
     120             :     {
     121             :         // look KIND_FOLDER:
     122        5828 :         ContentInfo const & info = infos[ pos ];
     123        5828 :         if ((info.Attributes & ContentInfoAttribute::KIND_FOLDER) != 0)
     124             :         {
     125             :             // make sure the only required bootstrap property is "Title":
     126        2914 :             Sequence<beans::Property> const & rProps = info.Properties;
     127        2914 :             if ( rProps.getLength() != 1 || rProps[ 0 ].Name != "Title" )
     128           0 :                 continue;
     129             : 
     130             :             try {
     131        5778 :                 if (parentContent.insertNewContent(
     132             :                         info.Type,
     133             :                         StrTitle::getTitleSequence(),
     134             :                         Sequence<Any>( &title, 1 ),
     135        5828 :                         ucb_content )) {
     136        2864 :                     if (ret_ucb_content != 0)
     137         202 :                         *ret_ucb_content = ucb_content;
     138        2864 :                     return true;
     139             :                 }
     140             :             }
     141           0 :             catch (const RuntimeException &) {
     142           0 :                 throw;
     143             :             }
     144           0 :             catch (const CommandFailedException &) {
     145             :                 // Interaction Handler already handled the error
     146             :                 // that has occurred...
     147             :             }
     148         100 :             catch (const Exception &) {
     149          50 :                 if (throw_exc)
     150          26 :                     throw;
     151          24 :                 return false;
     152             :             }
     153             :         }
     154             :     }
     155           0 :     if (throw_exc)
     156             :         throw ContentCreationException(
     157           0 :             "Cannot create folder: " + url,
     158           0 :             Reference<XInterface>(), ContentCreationError_UNKNOWN );
     159        7516 :     return false;
     160             : }
     161             : 
     162             : 
     163         192 : bool erase_path( OUString const & url,
     164             :                  Reference<XCommandEnvironment> const & xCmdEnv,
     165             :                  bool throw_exc )
     166             : {
     167         192 :     ::ucbhelper::Content ucb_content;
     168         192 :     if (create_ucb_content( &ucb_content, url, xCmdEnv, false /* no throw */ ))
     169             :     {
     170             :         try {
     171             :             ucb_content.executeCommand(
     172          64 :                 "delete", Any( true /* delete physically */ ) );
     173             :         }
     174           0 :         catch (const RuntimeException &) {
     175           0 :             throw;
     176             :         }
     177           0 :         catch (const Exception &) {
     178           0 :             if (throw_exc)
     179           0 :                 throw;
     180           0 :             return false;
     181             :         }
     182             :     }
     183         192 :     return true;
     184             : }
     185             : 
     186             : 
     187           2 : ::rtl::ByteSequence readFile( ::ucbhelper::Content & ucb_content )
     188             : {
     189           2 :     ::rtl::ByteSequence bytes;
     190             :     Reference<io::XOutputStream> xStream(
     191           4 :         ::xmlscript::createOutputStream( &bytes ) );
     192           2 :     if (! ucb_content.openStream( xStream ))
     193             :         throw RuntimeException(
     194             :             "::ucbhelper::Content::openStream( XOutputStream ) failed!",
     195           0 :             0 );
     196           4 :     return bytes;
     197             : }
     198             : 
     199             : 
     200           2 : bool readLine( OUString * res, OUString const & startingWith,
     201             :                ::ucbhelper::Content & ucb_content, rtl_TextEncoding textenc )
     202             : {
     203             :     // read whole file:
     204           2 :     ::rtl::ByteSequence bytes( readFile( ucb_content ) );
     205           2 :     OUString file( reinterpret_cast<sal_Char const *>(bytes.getConstArray()),
     206           6 :                    bytes.getLength(), textenc );
     207           2 :     sal_Int32 pos = 0;
     208             :     for (;;)
     209             :     {
     210          14 :         if (file.match( startingWith, pos ))
     211             :         {
     212           0 :             OUStringBuffer buf;
     213           0 :             sal_Int32 start = pos;
     214           0 :             pos += startingWith.getLength();
     215             :             for (;;)
     216             :             {
     217           0 :                 pos = file.indexOf( LF, pos );
     218           0 :                 if (pos < 0) { // EOF
     219           0 :                     buf.append( file.copy( start ) );
     220             :                 }
     221             :                 else
     222             :                 {
     223           0 :                     if (pos > 0 && file[ pos - 1 ] == CR)
     224             :                     {
     225             :                         // consume extra CR
     226           0 :                         buf.append( file.copy( start, pos - start - 1 ) );
     227           0 :                         ++pos;
     228             :                     }
     229             :                     else
     230           0 :                         buf.append( file.copy( start, pos - start ) );
     231           0 :                     ++pos; // consume LF
     232             :                     // check next line:
     233           0 :                     if (pos < file.getLength() &&
     234           0 :                         (file[ pos ] == ' ' || file[ pos ] == '\t'))
     235             :                     {
     236           0 :                         buf.append( ' ' );
     237           0 :                         ++pos;
     238           0 :                         start = pos;
     239           0 :                         continue;
     240             :                     }
     241             :                 }
     242           0 :                 break;
     243             :             }
     244           0 :             *res = buf.makeStringAndClear();
     245           0 :             return true;
     246             :         }
     247             :         // next line:
     248          14 :         sal_Int32 next_lf = file.indexOf( LF, pos );
     249          14 :         if (next_lf < 0) // EOF
     250           2 :             break;
     251          12 :         pos = next_lf + 1;
     252          12 :     }
     253           4 :     return false;
     254             : }
     255             : 
     256           0 : bool readProperties( ::std::list< ::std::pair< OUString, OUString> > & out_result,
     257             :                      ::ucbhelper::Content & ucb_content )
     258             : {
     259             :     // read whole file:
     260           0 :     ::rtl::ByteSequence bytes( readFile( ucb_content ) );
     261           0 :     OUString file( reinterpret_cast<sal_Char const *>(bytes.getConstArray()),
     262           0 :                    bytes.getLength(), RTL_TEXTENCODING_UTF8);
     263           0 :     sal_Int32 pos = 0;
     264             : 
     265             :     for (;;)
     266             :     {
     267             : 
     268           0 :         OUStringBuffer buf;
     269           0 :         sal_Int32 start = pos;
     270             : 
     271           0 :         bool bEOF = false;
     272           0 :         pos = file.indexOf( LF, pos );
     273           0 :         if (pos < 0) { // EOF
     274           0 :             buf.append( file.copy( start ) );
     275           0 :             bEOF = true;
     276             :         }
     277             :         else
     278             :         {
     279           0 :             if (pos > 0 && file[ pos - 1 ] == CR)
     280             :                 // consume extra CR
     281           0 :                 buf.append( file.copy( start, pos - start - 1 ) );
     282             :             else
     283           0 :                 buf.append( file.copy( start, pos - start ) );
     284           0 :             pos++;
     285             :         }
     286           0 :         OUString aLine = buf.makeStringAndClear();
     287             : 
     288           0 :         sal_Int32 posEqual = aLine.indexOf('=');
     289           0 :         if (posEqual > 0 && (posEqual + 1) <  aLine.getLength())
     290             :         {
     291           0 :             OUString name = aLine.copy(0, posEqual);
     292           0 :             OUString value = aLine.copy(posEqual + 1);
     293           0 :             out_result.push_back(::std::make_pair(name, value));
     294             :         }
     295             : 
     296           0 :         if (bEOF)
     297           0 :             break;
     298           0 :     }
     299           0 :     return false;
     300             : }
     301             : 
     302             : }
     303             : 
     304             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10