LCOV - code coverage report
Current view: top level - unotools/source/ucbhelper - ucbhelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 70 195 35.9 %
Date: 2015-06-13 12:38:46 Functions: 10 18 55.6 %
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 <sal/config.h>
      21             : 
      22             : #include <cassert>
      23             : #include <vector>
      24             : 
      25             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      26             : #include <com/sun/star/sdbc/XResultSet.hpp>
      27             : #include <com/sun/star/task/XInteractionHandler.hpp>
      28             : #include <com/sun/star/ucb/CommandAbortedException.hpp>
      29             : #include <com/sun/star/ucb/ContentInfo.hpp>
      30             : #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
      31             : #include <com/sun/star/ucb/IOErrorCode.hpp>
      32             : #include <com/sun/star/ucb/InteractiveIOException.hpp>
      33             : #include <com/sun/star/ucb/NameClashException.hpp>
      34             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      35             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      36             : #include <com/sun/star/ucb/XContentAccess.hpp>
      37             : #include <com/sun/star/ucb/XContentIdentifier.hpp>
      38             : #include <com/sun/star/ucb/XProgressHandler.hpp>
      39             : #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
      40             : #include <com/sun/star/uno/Any.hxx>
      41             : #include <com/sun/star/uno/Exception.hpp>
      42             : #include <com/sun/star/uno/Reference.hxx>
      43             : #include <com/sun/star/uno/RuntimeException.hpp>
      44             : #include <com/sun/star/uno/Sequence.hxx>
      45             : #include <com/sun/star/uno/XComponentContext.hpp>
      46             : #include <com/sun/star/util/DateTime.hpp>
      47             : #include <comphelper/processfactory.hxx>
      48             : #include <cppuhelper/exc_hlp.hxx>
      49             : #include <osl/file.hxx>
      50             : #include <rtl/string.h>
      51             : #include <rtl/ustring.h>
      52             : #include <rtl/ustring.hxx>
      53             : #include <sal/log.hxx>
      54             : #include <sal/types.h>
      55             : #include <tools/datetime.hxx>
      56             : #include <tools/urlobj.hxx>
      57             : #include <ucbhelper/commandenvironment.hxx>
      58             : #include <ucbhelper/content.hxx>
      59             : #include <unotools/localfilehelper.hxx>
      60             : #include <unotools/ucbhelper.hxx>
      61             : 
      62             : namespace {
      63             : 
      64       11220 : OUString canonic(OUString const & url) {
      65       11220 :     INetURLObject o(url);
      66             :     SAL_WARN_IF(o.HasError(), "unotools.ucbhelper", "Invalid URL \"" << url << '"');
      67       11220 :     return o.GetMainURL(INetURLObject::NO_DECODE);
      68             : }
      69             : 
      70        9498 : ucbhelper::Content content(OUString const & url) {
      71             :     return ucbhelper::Content(
      72             :         canonic(url),
      73             :         css::uno::Reference<css::ucb::XCommandEnvironment>(),
      74        9498 :         comphelper::getProcessComponentContext());
      75             : }
      76             : 
      77         932 : ucbhelper::Content content(INetURLObject const & url) {
      78             :     return ucbhelper::Content(
      79             :         url.GetMainURL(INetURLObject::NO_DECODE),
      80             :         css::uno::Reference<css::ucb::XCommandEnvironment>(),
      81         932 :         comphelper::getProcessComponentContext());
      82             : }
      83             : 
      84           0 : std::vector<OUString> getContents(OUString const & url) {
      85             :     try {
      86           0 :         std::vector<OUString> cs;
      87           0 :         ucbhelper::Content c(content(url));
      88           0 :         css::uno::Sequence<OUString> args(1);
      89           0 :         args[0] = "Title";
      90             :         css::uno::Reference<css::sdbc::XResultSet> res(
      91             :             c.createCursor(args, ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS),
      92           0 :             css::uno::UNO_SET_THROW);
      93             :         css::uno::Reference<com::sun::star::ucb::XContentAccess> acc(
      94           0 :             res, css::uno::UNO_QUERY_THROW);
      95           0 :         while (res->next()) {
      96           0 :             cs.push_back(acc->queryContentIdentifierString());
      97             :         }
      98           0 :         return cs;
      99           0 :     } catch (css::uno::RuntimeException const &) {
     100           0 :         throw;
     101           0 :     } catch (css::ucb::CommandAbortedException const &) {
     102             :         assert(false && "this cannot happen");
     103           0 :         throw;
     104           0 :     } catch (css::uno::Exception const &) {
     105           0 :         css::uno::Any e(cppu::getCaughtException());
     106             :         SAL_INFO(
     107             :             "unotools.ucbhelper",
     108             :             "getContents(" << url << ") " << e.getValueType().getTypeName()
     109             :                 << " \"" << e.get<css::uno::Exception>().Message << '"');
     110           0 :         return std::vector<OUString>();
     111             :     }
     112             : }
     113             : 
     114           0 : OUString getCasePreservingUrl(const INetURLObject& url) {
     115             :     return
     116             :         content(url).executeCommand(
     117             :             OUString("getCasePreservingURL"),
     118             :             css::uno::Any()).
     119           0 :         get<OUString>();
     120             : }
     121             : 
     122           0 : DateTime convert(css::util::DateTime const & dt) {
     123           0 :     return DateTime(dt);
     124             : }
     125             : 
     126             : }
     127             : 
     128        4994 : bool utl::UCBContentHelper::IsDocument(OUString const & url) {
     129             :     try {
     130        4994 :         return content(url).isDocument();
     131           0 :     } catch (css::uno::RuntimeException const &) {
     132           0 :         throw;
     133           0 :     } catch (css::ucb::CommandAbortedException const &) {
     134             :         assert(false && "this cannot happen");
     135           0 :         throw;
     136        2028 :     } catch (css::uno::Exception const &) {
     137        1014 :         css::uno::Any e(cppu::getCaughtException());
     138             :         SAL_INFO(
     139             :             "unotools.ucbhelper",
     140             :             "UCBContentHelper::IsDocument(" << url << ") "
     141             :                 << e.getValueType().getTypeName() << " \""
     142             :                 << e.get<css::uno::Exception>().Message << '"');
     143        1014 :         return false;
     144             :     }
     145             : }
     146             : 
     147           0 : css::uno::Any utl::UCBContentHelper::GetProperty(
     148             :     OUString const & url, OUString const & property)
     149             : {
     150             :     try {
     151           0 :         return content(url).getPropertyValue(property);
     152           0 :     } catch (css::uno::RuntimeException const &) {
     153           0 :         throw;
     154           0 :     } catch (css::ucb::CommandAbortedException const &) {
     155             :         assert(false && "this cannot happen");
     156           0 :         throw;
     157           0 :     } catch (css::uno::Exception const &) {
     158           0 :         css::uno::Any e(cppu::getCaughtException());
     159             :         SAL_INFO(
     160             :             "unotools.ucbhelper",
     161             :             "UCBContentHelper::GetProperty(" << url << ", " << property << ") "
     162             :                 << e.getValueType().getTypeName() << " \""
     163             :                 << e.get<css::uno::Exception>().Message << '"');
     164           0 :         return css::uno::Any();
     165             :     }
     166             : }
     167             : 
     168        3568 : bool utl::UCBContentHelper::IsFolder(OUString const & url) {
     169             :     try {
     170        3568 :         return content(url).isFolder();
     171           0 :     } catch (css::uno::RuntimeException const &) {
     172           0 :         throw;
     173           0 :     } catch (css::ucb::CommandAbortedException const &) {
     174             :         assert(false && "this cannot happen");
     175           0 :         throw;
     176        1920 :     } catch (css::uno::Exception const &) {
     177         960 :         css::uno::Any e(cppu::getCaughtException());
     178             :         SAL_INFO(
     179             :             "unotools.ucbhelper",
     180             :             "UCBContentHelper::IsFolder(" << url << ") "
     181             :                 << e.getValueType().getTypeName() << " \""
     182             :                 << e.get<css::uno::Exception>().Message << '"');
     183         960 :         return false;
     184             :     }
     185             : }
     186             : 
     187           0 : bool utl::UCBContentHelper::GetTitle(
     188             :     OUString const & url, OUString * title)
     189             : {
     190             :     assert(title != 0);
     191             :     try {
     192           0 :         return content(url).getPropertyValue("Title") >>= *title;
     193           0 :     } catch (css::uno::RuntimeException const &) {
     194           0 :         throw;
     195           0 :     } catch (css::ucb::CommandAbortedException const &) {
     196             :         assert(false && "this cannot happen");
     197           0 :         throw;
     198           0 :     } catch (css::uno::Exception const &) {
     199           0 :         css::uno::Any e(cppu::getCaughtException());
     200             :         SAL_INFO(
     201             :             "unotools.ucbhelper",
     202             :             "UCBContentHelper::GetTitle(" << url << ") "
     203             :                 << e.getValueType().getTypeName() << " \""
     204             :                 << e.get<css::uno::Exception>().Message << '"');
     205           0 :         return false;
     206             :     }
     207             : }
     208             : 
     209         936 : bool utl::UCBContentHelper::Kill(OUString const & url) {
     210             :     try {
     211             :         content(url).executeCommand(
     212             :             OUString("delete"),
     213         936 :             css::uno::makeAny(true));
     214         936 :         return true;
     215           0 :     } catch (css::uno::RuntimeException const &) {
     216           0 :         throw;
     217           0 :     } catch (css::ucb::CommandAbortedException const &) {
     218             :         assert(false && "this cannot happen");
     219           0 :         throw;
     220           0 :     } catch (css::uno::Exception const &) {
     221           0 :         css::uno::Any e(cppu::getCaughtException());
     222             :         SAL_INFO(
     223             :             "unotools.ucbhelper",
     224             :             "UCBContentHelper::Kill(" << url << ") "
     225             :                 << e.getValueType().getTypeName() << " \""
     226             :                 << e.get<css::uno::Exception>().Message << '"');
     227           0 :         return false;
     228             :     }
     229             : }
     230             : 
     231        1353 : bool utl::UCBContentHelper::MakeFolder(
     232             :     ucbhelper::Content & parent, OUString const & title,
     233             :     ucbhelper::Content & result, bool exclusive)
     234             : {
     235        1353 :     bool exists = false;
     236             :     try {
     237             :         css::uno::Sequence<css::ucb::ContentInfo> info(
     238        1353 :             parent.queryCreatableContentsInfo());
     239        2690 :         for (sal_Int32 i = 0; i < info.getLength(); ++i) {
     240             :             // Simply look for the first KIND_FOLDER:
     241        5380 :             if ((info[i].Attributes
     242        2690 :                  & css::ucb::ContentInfoAttribute::KIND_FOLDER)
     243             :                 != 0)
     244             :             {
     245             :                 // Make sure the only required bootstrap property is "Title":
     246        1345 :                 if ( info[i].Properties.getLength() != 1 || info[i].Properties[0].Name != "Title" )
     247             :                 {
     248           0 :                     continue;
     249             :                 }
     250        1345 :                 css::uno::Sequence<OUString> keys(1);
     251        1345 :                 keys[0] = "Title";
     252        2277 :                 css::uno::Sequence<css::uno::Any> values(1);
     253        1345 :                 values[0] <<= title;
     254        1345 :                 if (parent.insertNewContent(info[i].Type, keys, values, result))
     255             :                 {
     256         413 :                     return true;
     257         932 :                 }
     258             :             }
     259         932 :         }
     260          16 :     } catch (css::ucb::InteractiveIOException const & e) {
     261           8 :         if (e.Code == css::ucb::IOErrorCode_ALREADY_EXISTING) {
     262           0 :             exists = true;
     263             :         } else {
     264             :             SAL_INFO(
     265             :                 "unotools.ucbhelper",
     266             :                 "UCBContentHelper::MakeFolder(" << title
     267             :                     << ") InteractiveIOException \"" << e.Message
     268             :                     << "\", code " << +e.Code);
     269             :         }
     270        1864 :     } catch (css::ucb::NameClashException const &) {
     271         932 :         exists = true;
     272           0 :     } catch (css::uno::RuntimeException const &) {
     273           0 :         throw;
     274           0 :     } catch (css::ucb::CommandAbortedException const &) {
     275             :         assert(false && "this cannot happen");
     276           0 :         throw;
     277           0 :     } catch (css::uno::Exception const &) {
     278           0 :         css::uno::Any e(cppu::getCaughtException());
     279             :         SAL_INFO(
     280             :             "unotools.ucbhelper",
     281             :             "UCBContentHelper::MakeFolder(" << title << ") "
     282             :                 << e.getValueType().getTypeName() << " \""
     283           0 :                 << e.get<css::uno::Exception>().Message << '"');
     284             :     }
     285         940 :     if (exists && !exclusive) {
     286         932 :         INetURLObject o(parent.getURL());
     287         932 :         o.Append(title);
     288         932 :         result = content(o);
     289         932 :         return true;
     290             :     } else {
     291           8 :         return false;
     292             :     }
     293             : }
     294             : 
     295           0 : sal_Int64 utl::UCBContentHelper::GetSize(OUString const & url) {
     296             :     try {
     297           0 :         sal_Int64 n = 0;
     298           0 :         bool ok = (content(url).getPropertyValue("Size") >>= n);
     299             :         SAL_INFO_IF(
     300             :             !ok, "unotools.ucbhelper",
     301             :             "UCBContentHelper::GetSize(" << url
     302             :                 << "): Size cannot be determined");
     303           0 :         return n;
     304           0 :     } catch (css::uno::RuntimeException const &) {
     305           0 :         throw;
     306           0 :     } catch (css::ucb::CommandAbortedException const &) {
     307             :         assert(false && "this cannot happen");
     308           0 :         throw;
     309           0 :     } catch (css::uno::Exception const &) {
     310           0 :         css::uno::Any e(cppu::getCaughtException());
     311             :         SAL_INFO(
     312             :             "unotools.ucbhelper",
     313             :             "UCBContentHelper::GetSize(" << url << ") "
     314             :                 << e.getValueType().getTypeName() << " \""
     315             :                 << e.get<css::uno::Exception>().Message << '"');
     316           0 :         return 0;
     317             :     }
     318             : }
     319             : 
     320           0 : bool utl::UCBContentHelper::IsYounger(
     321             :     OUString const & younger, OUString const & older)
     322             : {
     323             :     try {
     324             :         return
     325             :             convert(
     326             :                 content(younger).getPropertyValue(
     327             :                     OUString("DateModified")).
     328           0 :                 get<css::util::DateTime>())
     329           0 :             > convert(
     330             :                 content(older).getPropertyValue(
     331             :                     OUString("DateModified")).
     332           0 :                 get<css::util::DateTime>());
     333           0 :     } catch (css::uno::RuntimeException const &) {
     334           0 :         throw;
     335           0 :     } catch (css::ucb::CommandAbortedException const &) {
     336             :         assert(false && "this cannot happen");
     337           0 :         throw;
     338           0 :     } catch (css::uno::Exception const &) {
     339           0 :         css::uno::Any e(cppu::getCaughtException());
     340             :         SAL_INFO(
     341             :             "unotools.ucbhelper",
     342             :             "UCBContentHelper::IsYounger(" << younger << ", " << older << ") "
     343             :                 << e.getValueType().getTypeName() << " \""
     344             :                 << e.get<css::uno::Exception>().Message << '"');
     345           0 :         return false;
     346             :     }
     347             : }
     348             : 
     349         409 : bool utl::UCBContentHelper::Exists(OUString const & url) {
     350         409 :     OUString pathname;
     351         409 :     if (utl::LocalFileHelper::ConvertURLToPhysicalName(url, pathname)) {
     352             :         // Try to create a directory entry for the given URL:
     353         409 :         OUString url2;
     354         409 :         if (osl::FileBase::getFileURLFromSystemPath(pathname, url2)
     355             :             == osl::FileBase::E_None)
     356             :         {
     357             :             // #106526 osl_getDirectoryItem is an existence check, no further
     358             :             // osl_getFileStatus call necessary:
     359         409 :             osl::DirectoryItem item;
     360         409 :             return osl::DirectoryItem::get(url2, item) == osl::FileBase::E_None;
     361             :         } else {
     362           0 :             return false;
     363         409 :         }
     364             :     } else {
     365             :         // Divide URL into folder and name part:
     366           0 :         INetURLObject o(url);
     367             :         OUString name(
     368             :             o.getName(
     369             :                 INetURLObject::LAST_SEGMENT, true,
     370           0 :                 INetURLObject::DECODE_WITH_CHARSET));
     371           0 :         o.removeSegment();
     372           0 :         o.removeFinalSlash();
     373             :         std::vector<OUString> cs(
     374           0 :             getContents(o.GetMainURL(INetURLObject::NO_DECODE)));
     375           0 :         for (std::vector<OUString>::iterator i(cs.begin()); i != cs.end();
     376             :              ++i)
     377             :         {
     378           0 :             if (INetURLObject(*i).getName(
     379             :                     INetURLObject::LAST_SEGMENT, true,
     380             :                     INetURLObject::DECODE_WITH_CHARSET).
     381           0 :                 equalsIgnoreAsciiCase(name))
     382             :             {
     383           0 :                 return true;
     384             :             }
     385             :         }
     386           0 :         return false;
     387         409 :     }
     388             : }
     389             : 
     390           0 : bool utl::UCBContentHelper::IsSubPath(
     391             :     OUString const & parent, OUString const & child)
     392             : {
     393             :     // The comparison is done in the following way:
     394             :     // - First, compare case sensitively
     395             :     // - If names are different, try a fallback comparing case insensitively
     396             :     // - If the last comparison succeeded, get case preserving normalized names
     397             :     //   for the files and compare them
     398             :     // (The second step is required because retrieving the normalized names
     399             :     // might be very expensive in some cases.)
     400           0 :     INetURLObject candidate(child);
     401           0 :     INetURLObject folder(parent);
     402           0 :     if (candidate.GetProtocol() != folder.GetProtocol()) {
     403           0 :         return false;
     404             :     }
     405           0 :     INetURLObject candidateLower(child.toAsciiLowerCase());
     406           0 :     INetURLObject folderLower(parent.toAsciiLowerCase());
     407             :     try {
     408           0 :         INetURLObject tmp;
     409           0 :         do {
     410           0 :             if (candidate == folder
     411           0 :                 || (candidate.GetProtocol() == INetProtocol::File
     412           0 :                     && candidateLower == folderLower
     413           0 :                     && (getCasePreservingUrl(candidate)
     414           0 :                         == getCasePreservingUrl(folder))))
     415             :             {
     416           0 :                 return true;
     417             :             }
     418           0 :             tmp = candidate;
     419           0 :         } while (candidate.removeSegment() && candidateLower.removeSegment()
     420           0 :                  && candidate != tmp);
     421             :             // INetURLObject::removeSegment sometimes returns true without
     422             :             // modifying the URL, e.g., in case of "file:///"
     423           0 :     } catch (css::uno::RuntimeException const &) {
     424           0 :         throw;
     425           0 :     } catch (css::ucb::CommandAbortedException const &) {
     426             :         assert(false && "this cannot happen");
     427           0 :         throw;
     428           0 :     } catch (css::uno::Exception const &) {
     429           0 :         css::uno::Any e(cppu::getCaughtException());
     430             :         SAL_INFO(
     431             :             "unotools.ucbhelper",
     432             :             "UCBContentHelper::IsSubPath(" << parent << ", " << child << ") "
     433             :                 << e.getValueType().getTypeName() << " \""
     434           0 :                 << e.get<css::uno::Exception>().Message << '"');
     435             :     }
     436           0 :     return false;
     437             : }
     438             : 
     439         991 : bool utl::UCBContentHelper::EqualURLs(
     440             :     OUString const & url1, OUString const & url2)
     441             : {
     442         991 :     if (url1.isEmpty() || url2.isEmpty()) {
     443         130 :         return false;
     444             :     }
     445             :     css::uno::Reference< css::ucb::XUniversalContentBroker > ucb(
     446             :         css::ucb::UniversalContentBroker::create(
     447         861 :             comphelper::getProcessComponentContext()));
     448             :     return
     449         861 :         ucb->compareContentIds(
     450        1722 :             ucb->createContentIdentifier(canonic(url1)),
     451        3444 :             ucb->createContentIdentifier(canonic(url2)))
     452        1722 :         == 0;
     453             : }
     454             : 
     455         879 : bool utl::UCBContentHelper::ensureFolder(
     456             :     css::uno::Reference< css::uno::XComponentContext > xCtx,
     457             :     css::uno::Reference< css::ucb::XCommandEnvironment > xEnv,
     458             :     const OUString& rFolder, ucbhelper::Content & result) throw()
     459             : {
     460             :     try
     461             :     {
     462         879 :         INetURLObject aURL( rFolder );
     463         879 :         OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
     464         879 :         aURL.removeSegment();
     465         879 :         ::ucbhelper::Content aParent;
     466             : 
     467        1758 :         if ( ::ucbhelper::Content::create( aURL.GetMainURL( INetURLObject::NO_DECODE ),
     468        1758 :                                   xEnv, xCtx, aParent ) )
     469             :         {
     470         879 :             return ::utl::UCBContentHelper::MakeFolder(aParent, aTitle, result);
     471           0 :         }
     472             :     }
     473           0 :     catch (...)
     474             :     {
     475             :     }
     476             : 
     477           0 :     return false;
     478             : }
     479             : 
     480             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11