Bump version to 6.4.7.2.M8
[LibreOffice.git] / linguistic / source / misc2.cxx
blob664812f4b2799c4e37cff0c311dd5e63c57f2be9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/urlobj.hxx>
21 #include <ucbhelper/content.hxx>
22 #include <tools/debug.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/uno/Reference.h>
26 #include <com/sun/star/util/thePathSettings.hpp>
27 #include <o3tl/typed_flags_set.hxx>
29 #include <linguistic/misc.hxx>
31 using namespace com::sun::star;
33 /// Flags to be used with the multi-path related functions
34 /// @see GetDictionaryPaths
35 enum class DictionaryPathFlags
37 NONE = 0x00,
38 INTERNAL = 0x01,
39 USER = 0x02,
41 namespace o3tl
43 template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x03> {};
45 #define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
47 namespace linguistic
51 bool FileExists( const OUString &rMainURL )
53 bool bExists = false;
54 if (!rMainURL.isEmpty())
56 try
58 ::ucbhelper::Content aContent( rMainURL,
59 uno::Reference< css::ucb::XCommandEnvironment >(),
60 comphelper::getProcessComponentContext());
61 bExists = aContent.isDocument();
63 catch (uno::Exception &)
67 return bExists;
70 static std::vector< OUString > GetMultiPaths_Impl(
71 const OUString &rPathPrefix,
72 DictionaryPathFlags nPathFlags )
74 std::vector< OUString > aRes;
75 uno::Sequence< OUString > aInternalPaths;
76 uno::Sequence< OUString > aUserPaths;
77 OUString aWritablePath;
79 bool bSuccess = true;
80 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
81 try
83 OUString aInternal( rPathPrefix + "_internal" );
84 OUString aUser( rPathPrefix + "_user" );
85 OUString aWriteable( rPathPrefix + "_writable" );
87 uno::Reference< util::XPathSettings > xPathSettings =
88 util::thePathSettings::get( xContext );
89 xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
90 xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
91 xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
93 catch (uno::Exception &)
95 bSuccess = false;
97 if (bSuccess)
99 // build resulting sequence by adding the paths in the following order:
100 // 1. writable path
101 // 2. all user paths
102 // 3. all internal paths
103 sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
104 if (!aWritablePath.isEmpty())
105 ++nMaxEntries;
106 aRes.reserve( nMaxEntries );
107 if (!aWritablePath.isEmpty())
108 aRes.push_back(aWritablePath);
110 auto lPathIsNotEmpty = [](const OUString& rPath) { return !rPath.isEmpty(); };
112 if (nPathFlags & DictionaryPathFlags::USER)
113 std::copy_if(std::cbegin(aUserPaths), std::cend(aUserPaths), std::back_inserter(aRes), lPathIsNotEmpty);
115 if (nPathFlags & DictionaryPathFlags::INTERNAL)
116 std::copy_if(std::cbegin(aInternalPaths), std::cend(aInternalPaths), std::back_inserter(aRes), lPathIsNotEmpty);
119 return aRes;
122 OUString GetDictionaryWriteablePath()
124 std::vector< OUString > aPaths( GetMultiPaths_Impl( "Dictionary", DictionaryPathFlags::NONE ) );
125 DBG_ASSERT( aPaths.size() == 1, "Dictionary_writable path corrupted?" );
126 OUString aRes;
127 if (!aPaths.empty())
128 aRes = aPaths[0];
129 return aRes;
132 std::vector< OUString > GetDictionaryPaths()
134 return GetMultiPaths_Impl( "Dictionary", PATH_FLAG_ALL );
137 OUString GetWritableDictionaryURL( const OUString &rDicName )
139 // new user writable dictionaries should be created in the 'writable' path
140 OUString aDirName( GetDictionaryWriteablePath() );
142 // build URL to use for a new (persistent) dictionary
143 INetURLObject aURLObj;
144 aURLObj.SetSmartProtocol( INetProtocol::File );
145 aURLObj.SetSmartURL( aDirName );
146 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
147 aURLObj.Append( rDicName, INetURLObject::EncodeMechanism::All );
148 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
150 // DecodeMechanism::NONE preserves the escape sequences that might be included in aDirName
151 // depending on the characters used in the path string. (Needed when comparing
152 // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
153 return aURLObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
156 } // namespace linguistic
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */