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 <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>
28
29 #include <linguistic/misc.hxx>
30
31 using namespace com::sun::star;
32
33 /// Flags to be used with the multi-path related functions
34 /// @see GetDictionaryPaths
35 enum class DictionaryPathFlags
36 {
37 NONE = 0x00,
38 INTERNAL = 0x01,
39 USER = 0x02,
40 };
41 namespace o3tl
42 {
43 template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x03> {};
44 }
45 #define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
46
47 namespace linguistic
48 {
49
50
FileExists(const OUString & rMainURL)51 bool FileExists( const OUString &rMainURL )
52 {
53 bool bExists = false;
54 if (!rMainURL.isEmpty())
55 {
56 try
57 {
58 ::ucbhelper::Content aContent( rMainURL,
59 uno::Reference< css::ucb::XCommandEnvironment >(),
60 comphelper::getProcessComponentContext());
61 bExists = aContent.isDocument();
62 }
63 catch (uno::Exception &)
64 {
65 }
66 }
67 return bExists;
68 }
69
GetMultiPaths_Impl(const OUString & rPathPrefix,DictionaryPathFlags nPathFlags)70 static std::vector< OUString > GetMultiPaths_Impl(
71 const OUString &rPathPrefix,
72 DictionaryPathFlags nPathFlags )
73 {
74 std::vector< OUString > aRes;
75 uno::Sequence< OUString > aInternalPaths;
76 uno::Sequence< OUString > aUserPaths;
77 OUString aWritablePath;
78
79 bool bSuccess = true;
80 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
81 try
82 {
83 OUString aInternal( rPathPrefix + "_internal" );
84 OUString aUser( rPathPrefix + "_user" );
85 OUString aWriteable( rPathPrefix + "_writable" );
86
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;
92 }
93 catch (uno::Exception &)
94 {
95 bSuccess = false;
96 }
97 if (bSuccess)
98 {
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);
109
110 auto lPathIsNotEmpty = [](const OUString& rPath) { return !rPath.isEmpty(); };
111
112 if (nPathFlags & DictionaryPathFlags::USER)
113 std::copy_if(std::cbegin(aUserPaths), std::cend(aUserPaths), std::back_inserter(aRes), lPathIsNotEmpty);
114
115 if (nPathFlags & DictionaryPathFlags::INTERNAL)
116 std::copy_if(std::cbegin(aInternalPaths), std::cend(aInternalPaths), std::back_inserter(aRes), lPathIsNotEmpty);
117 }
118
119 return aRes;
120 }
121
GetDictionaryWriteablePath()122 OUString GetDictionaryWriteablePath()
123 {
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;
130 }
131
GetDictionaryPaths()132 std::vector< OUString > GetDictionaryPaths()
133 {
134 return GetMultiPaths_Impl( "Dictionary", PATH_FLAG_ALL );
135 }
136
GetWritableDictionaryURL(const OUString & rDicName)137 OUString GetWritableDictionaryURL( const OUString &rDicName )
138 {
139 // new user writable dictionaries should be created in the 'writable' path
140 OUString aDirName( GetDictionaryWriteablePath() );
141
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");
149
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 );
154 }
155
156 } // namespace linguistic
157
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
159