1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
35 /// Flags to be used with the multi-path related functions
36 /// @see GetDictionaryPaths
37 enum class DictionaryPathFlags
48 template<> struct typed_flags
<DictionaryPathFlags
> : is_typed_flags
<DictionaryPathFlags
, 0x03> {};
50 #define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
56 bool FileExists( const OUString
&rMainURL
)
59 if (!rMainURL
.isEmpty())
63 ::ucbhelper::Content
aContent( rMainURL
,
64 uno::Reference
< css::ucb::XCommandEnvironment
>(),
65 comphelper::getProcessComponentContext());
66 bExists
= aContent
.isDocument();
68 catch (uno::Exception
&)
75 static std::vector
< OUString
> GetMultiPaths_Impl(
76 const OUString
&rPathPrefix
,
77 DictionaryPathFlags nPathFlags
)
79 std::vector
< OUString
> aRes
;
80 uno::Sequence
< OUString
> aInternalPaths
;
81 uno::Sequence
< OUString
> aUserPaths
;
82 OUString aWritablePath
;
85 uno::Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext() );
88 OUString
aInternal( rPathPrefix
+ "_internal" );
89 OUString
aUser( rPathPrefix
+ "_user" );
90 OUString
aWriteable( rPathPrefix
+ "_writable" );
92 uno::Reference
< util::XPathSettings
> xPathSettings
=
93 util::thePathSettings::get( xContext
);
94 xPathSettings
->getPropertyValue( aInternal
) >>= aInternalPaths
;
95 xPathSettings
->getPropertyValue( aUser
) >>= aUserPaths
;
96 xPathSettings
->getPropertyValue( aWriteable
) >>= aWritablePath
;
98 catch (uno::Exception
&)
104 // build resulting sequence by adding the paths in the following order:
107 // 3. all internal paths
108 sal_Int32 nMaxEntries
= aInternalPaths
.getLength() + aUserPaths
.getLength();
109 if (!aWritablePath
.isEmpty())
111 aRes
.reserve( nMaxEntries
);
112 if (!aWritablePath
.isEmpty())
113 aRes
.push_back(aWritablePath
);
115 auto lPathIsNotEmpty
= [](const OUString
& rPath
) { return !rPath
.isEmpty(); };
117 if (nPathFlags
& DictionaryPathFlags::USER
)
118 std::copy_if(std::cbegin(aUserPaths
), std::cend(aUserPaths
), std::back_inserter(aRes
), lPathIsNotEmpty
);
120 if (nPathFlags
& DictionaryPathFlags::INTERNAL
)
121 std::copy_if(std::cbegin(aInternalPaths
), std::cend(aInternalPaths
), std::back_inserter(aRes
), lPathIsNotEmpty
);
127 OUString
GetDictionaryWriteablePath()
129 std::vector
< OUString
> aPaths( GetMultiPaths_Impl( "Dictionary", DictionaryPathFlags::NONE
) );
130 DBG_ASSERT( aPaths
.size() == 1, "Dictionary_writable path corrupted?" );
137 std::vector
< OUString
> GetDictionaryPaths()
139 return GetMultiPaths_Impl( "Dictionary", PATH_FLAG_ALL
);
142 OUString
GetWritableDictionaryURL( const OUString
&rDicName
)
144 // new user writable dictionaries should be created in the 'writable' path
145 OUString
aDirName( GetDictionaryWriteablePath() );
147 // build URL to use for a new (persistent) dictionary
148 INetURLObject aURLObj
;
149 aURLObj
.SetSmartProtocol( INetProtocol::File
);
150 aURLObj
.SetSmartURL( aDirName
);
151 DBG_ASSERT(!aURLObj
.HasError(), "lng : invalid URL");
152 aURLObj
.Append( rDicName
, INetURLObject::EncodeMechanism::All
);
153 DBG_ASSERT(!aURLObj
.HasError(), "lng : invalid URL");
155 // DecodeMechanism::NONE preserves the escape sequences that might be included in aDirName
156 // depending on the characters used in the path string. (Needed when comparing
157 // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
158 return aURLObj
.GetMainURL( INetURLObject::DecodeMechanism::NONE
);
161 } // namespace linguistic
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */