look for java, javac and javadoc with the .exe suffix on windows
[LibreOffice.git] / linguistic / source / misc2.cxx
blob86a81e5a1f0694d9bd72b9239aac9fa029d1d832
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 <sal/config.h>
22 #include <string_view>
24 #include <tools/urlobj.hxx>
25 #include <ucbhelper/content.hxx>
26 #include <tools/debug.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/uno/Reference.h>
30 #include <com/sun/star/util/thePathSettings.hpp>
31 #include <o3tl/typed_flags_set.hxx>
33 #include <linguistic/misc.hxx>
35 using namespace com::sun::star;
37 namespace {
39 /// Flags to be used with the multi-path related functions
40 /// @see GetDictionaryPaths
41 enum class DictionaryPathFlags
43 NONE = 0x00,
44 INTERNAL = 0x01,
45 USER = 0x02,
50 namespace o3tl
52 template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x03> {};
54 #define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
56 namespace linguistic
60 bool FileExists( const OUString &rMainURL )
62 bool bExists = false;
63 if (!rMainURL.isEmpty())
65 try
67 ::ucbhelper::Content aContent( rMainURL,
68 uno::Reference< css::ucb::XCommandEnvironment >(),
69 comphelper::getProcessComponentContext());
70 bExists = aContent.isDocument();
72 catch (uno::Exception &)
76 return bExists;
79 static std::vector< OUString > GetMultiPaths_Impl(
80 std::u16string_view rPathPrefix,
81 DictionaryPathFlags nPathFlags )
83 std::vector< OUString > aRes;
84 uno::Sequence< OUString > aInternalPaths;
85 uno::Sequence< OUString > aUserPaths;
86 OUString aWritablePath;
88 bool bSuccess = true;
89 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
90 try
92 OUString aInternal( OUString::Concat(rPathPrefix) + "_internal" );
93 OUString aUser( OUString::Concat(rPathPrefix) + "_user" );
94 OUString aWriteable( OUString::Concat(rPathPrefix) + "_writable" );
96 uno::Reference< util::XPathSettings > xPathSettings =
97 util::thePathSettings::get( xContext );
98 xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
99 xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
100 xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
102 catch (uno::Exception &)
104 bSuccess = false;
106 if (bSuccess)
108 // build resulting sequence by adding the paths in the following order:
109 // 1. writable path
110 // 2. all user paths
111 // 3. all internal paths
112 sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
113 if (!aWritablePath.isEmpty())
114 ++nMaxEntries;
115 aRes.reserve( nMaxEntries );
116 if (!aWritablePath.isEmpty())
117 aRes.push_back(aWritablePath);
119 auto lPathIsNotEmpty = [](const OUString& rPath) { return !rPath.isEmpty(); };
121 if (nPathFlags & DictionaryPathFlags::USER)
122 std::copy_if(std::cbegin(aUserPaths), std::cend(aUserPaths), std::back_inserter(aRes), lPathIsNotEmpty);
124 if (nPathFlags & DictionaryPathFlags::INTERNAL)
125 std::copy_if(std::cbegin(aInternalPaths), std::cend(aInternalPaths), std::back_inserter(aRes), lPathIsNotEmpty);
128 return aRes;
131 OUString GetDictionaryWriteablePath()
133 std::vector< OUString > aPaths(
134 GetMultiPaths_Impl( u"Dictionary", DictionaryPathFlags::NONE ) );
135 DBG_ASSERT( aPaths.size() == 1, "Dictionary_writable path corrupted?" );
136 OUString aRes;
137 if (!aPaths.empty())
138 aRes = aPaths[0];
139 return aRes;
142 std::vector< OUString > GetDictionaryPaths()
144 return GetMultiPaths_Impl( u"Dictionary", PATH_FLAG_ALL );
147 OUString GetWritableDictionaryURL( std::u16string_view rDicName )
149 // new user writable dictionaries should be created in the 'writable' path
150 OUString aDirName( GetDictionaryWriteablePath() );
152 // build URL to use for a new (persistent) dictionary
153 INetURLObject aURLObj;
154 aURLObj.SetSmartProtocol( INetProtocol::File );
155 aURLObj.SetSmartURL( aDirName );
156 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
157 aURLObj.Append( rDicName, INetURLObject::EncodeMechanism::All );
158 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
160 // DecodeMechanism::NONE preserves the escape sequences that might be included in aDirName
161 // depending on the characters used in the path string. (Needed when comparing
162 // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
163 return aURLObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
166 } // namespace linguistic
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */