LCOV - code coverage report
Current view: top level - linguistic/source - misc2.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 55 57 96.5 %
Date: 2015-06-13 12:38:46 Functions: 5 5 100.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 <tools/urlobj.hxx>
      21             : #include <ucbhelper/content.hxx>
      22             : #include <tools/debug.hxx>
      23             : #include <unotools/pathoptions.hxx>
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <unotools/localfilehelper.hxx>
      26             : #include <unotools/localedatawrapper.hxx>
      27             : #include <unotools/ucbhelper.hxx>
      28             : #include <com/sun/star/beans/XPropertySet.hpp>
      29             : #include <com/sun/star/beans/XFastPropertySet.hpp>
      30             : #include <com/sun/star/beans/PropertyValues.hpp>
      31             : #include <com/sun/star/uno/Sequence.hxx>
      32             : #include <com/sun/star/uno/Reference.h>
      33             : #include <com/sun/star/util/thePathSettings.hpp>
      34             : #include <o3tl/typed_flags_set.hxx>
      35             : 
      36             : #include "linguistic/misc.hxx"
      37             : 
      38             : using namespace com::sun::star;
      39             : 
      40             : /// Flags to be used with the multi-path related functions
      41             : /// @see GetDictionaryPaths
      42             : enum class DictionaryPathFlags
      43             : {
      44             :     INTERNAL  = 0x01,
      45             :     USER      = 0x02,
      46             :     WRITABLE  = 0x04
      47             : };
      48             : namespace o3tl
      49             : {
      50             :     template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x07> {};
      51             : }
      52             : #define PATH_FLAG_ALL       (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER | DictionaryPathFlags::WRITABLE)
      53             : 
      54             : namespace linguistic
      55             : {
      56             : 
      57             : 
      58         124 : bool FileExists( const OUString &rMainURL )
      59             : {
      60         124 :     bool bExists = false;
      61         124 :     if (!rMainURL.isEmpty())
      62             :     {
      63             :         try
      64             :         {
      65             :             ::ucbhelper::Content aContent( rMainURL,
      66             :                     uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
      67         124 :                     comphelper::getProcessComponentContext());
      68         125 :             bExists = aContent.isDocument();
      69             :         }
      70           1 :         catch (uno::Exception &)
      71             :         {
      72             :         }
      73             :     }
      74         124 :     return bExists;
      75             : }
      76             : 
      77         114 : static uno::Sequence< OUString > GetMultiPaths_Impl(
      78             :     const OUString &rPathPrefix,
      79             :     DictionaryPathFlags nPathFlags )
      80             : {
      81         114 :     uno::Sequence< OUString >   aRes;
      82         228 :     uno::Sequence< OUString >   aInternalPaths;
      83         228 :     uno::Sequence< OUString >   aUserPaths;
      84         228 :     OUString                    aWritablePath;
      85             : 
      86         114 :     bool bSuccess = true;
      87         228 :     uno::Reference< uno::XComponentContext >  xContext( comphelper::getProcessComponentContext() );
      88             :     try
      89             :     {
      90         114 :         OUString aInternal( rPathPrefix + "_internal" );
      91         228 :         OUString aUser( rPathPrefix + "_user" );
      92         228 :         OUString aWriteable( rPathPrefix + "_writable" );
      93             : 
      94             :         uno::Reference< util::XPathSettings > xPathSettings =
      95         228 :             util::thePathSettings::get( xContext );
      96         114 :         xPathSettings->getPropertyValue( aInternal )  >>= aInternalPaths;
      97         114 :         xPathSettings->getPropertyValue( aUser )      >>= aUserPaths;
      98         228 :         xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
      99             :     }
     100           0 :     catch (uno::Exception &)
     101             :     {
     102           0 :         bSuccess = false;
     103             :     }
     104         114 :     if (bSuccess)
     105             :     {
     106             :         // build resulting sequence by adding the paths in the following order:
     107             :         // 1. writable path
     108             :         // 2. all user paths
     109             :         // 3. all internal paths
     110         114 :         sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
     111         114 :         if (!aWritablePath.isEmpty())
     112         114 :             ++nMaxEntries;
     113         114 :         aRes.realloc( nMaxEntries );
     114         114 :         OUString *pRes = aRes.getArray();
     115         114 :         sal_Int32 nCount = 0;   // number of actually added entries
     116         114 :         if ((nPathFlags & DictionaryPathFlags::WRITABLE) && !aWritablePath.isEmpty())
     117         114 :             pRes[ nCount++ ] = aWritablePath;
     118         342 :         for (int i = 0;  i < 2;  ++i)
     119             :         {
     120         228 :             const uno::Sequence< OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
     121         228 :             const OUString *pPathSeq = rPathSeq.getConstArray();
     122         342 :             for (sal_Int32 k = 0;  k < rPathSeq.getLength();  ++k)
     123             :             {
     124         114 :                 const bool bAddUser     = &rPathSeq == &aUserPaths     && (nPathFlags & DictionaryPathFlags::USER);
     125         114 :                 const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & DictionaryPathFlags::INTERNAL);
     126         114 :                 if ((bAddUser || bAddInternal) && !pPathSeq[k].isEmpty())
     127          30 :                     pRes[ nCount++ ] = pPathSeq[k];
     128             :             }
     129             :         }
     130         114 :         aRes.realloc( nCount );
     131             :     }
     132             : 
     133         228 :     return aRes;
     134             : }
     135             : 
     136          84 : OUString GetDictionaryWriteablePath()
     137             : {
     138          84 :     uno::Sequence< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", DictionaryPathFlags::WRITABLE ) );
     139             :     DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
     140          84 :     OUString aRes;
     141          84 :     if (aPaths.getLength() > 0)
     142          84 :         aRes = aPaths[0];
     143          84 :     return aRes;
     144             : }
     145             : 
     146          30 : uno::Sequence< OUString > GetDictionaryPaths()
     147             : {
     148          30 :     return GetMultiPaths_Impl( "Dictionary", PATH_FLAG_ALL );
     149             : }
     150             : 
     151           1 : OUString  GetWritableDictionaryURL( const OUString &rDicName )
     152             : {
     153             :     // new user writable dictionaries should be created in the 'writable' path
     154           1 :     OUString aDirName( GetDictionaryWriteablePath() );
     155             : 
     156             :     // build URL to use for a new (persistent) dictionary
     157           2 :     INetURLObject aURLObj;
     158           1 :     aURLObj.SetSmartProtocol( INetProtocol::File );
     159           1 :     aURLObj.SetSmartURL( aDirName );
     160             :     DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
     161           1 :     aURLObj.Append( rDicName, INetURLObject::ENCODE_ALL );
     162             :     DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
     163             : 
     164             :     // NO_DECODE preserves the escape sequences that might be included in aDirName
     165             :     // depending on the characters used in the path string. (Needed when comparing
     166             :     // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
     167           2 :     return aURLObj.GetMainURL( INetURLObject::NO_DECODE );
     168             : }
     169             : 
     170             : }   // namespace linguistic
     171             : 
     172             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11