LibreOffice Module linguistic (master) 1
misc2.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <string_view>
23
24#include <tools/urlobj.hxx>
25#include <ucbhelper/content.hxx>
26#include <tools/debug.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>
32
33#include <linguistic/misc.hxx>
34
35using namespace com::sun::star;
36
37namespace {
38
41enum class DictionaryPathFlags
42{
43 NONE = 0x00,
44 INTERNAL = 0x01,
45 USER = 0x02,
46};
47
48}
49
50namespace o3tl
51{
52 template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x03> {};
53}
54#define PATH_FLAG_ALL (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
55
56namespace linguistic
57{
58
59
60bool FileExists( const OUString &rMainURL )
61{
62 bool bExists = false;
63 if (!rMainURL.isEmpty())
64 {
65 try
66 {
67 ::ucbhelper::Content aContent( rMainURL,
68 uno::Reference< css::ucb::XCommandEnvironment >(),
70 bExists = aContent.isDocument();
71 }
72 catch (uno::Exception &)
73 {
74 }
75 }
76 return bExists;
77}
78
79static std::vector< OUString > GetMultiPaths_Impl(
80 std::u16string_view rPathPrefix,
81 DictionaryPathFlags nPathFlags )
82{
83 std::vector< OUString > aRes;
84 uno::Sequence< OUString > aInternalPaths;
85 uno::Sequence< OUString > aUserPaths;
86 OUString aWritablePath;
87
88 bool bSuccess = true;
89 uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
90 try
91 {
92 OUString aInternal( OUString::Concat(rPathPrefix) + "_internal" );
93 OUString aUser( OUString::Concat(rPathPrefix) + "_user" );
94 OUString aWriteable( OUString::Concat(rPathPrefix) + "_writable" );
95
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;
101 }
102 catch (uno::Exception &)
103 {
104 bSuccess = false;
105 }
106 if (bSuccess)
107 {
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);
118
119 auto lPathIsNotEmpty = [](const OUString& rPath) { return !rPath.isEmpty(); };
120
121 if (nPathFlags & DictionaryPathFlags::USER)
122 std::copy_if(std::cbegin(aUserPaths), std::cend(aUserPaths), std::back_inserter(aRes), lPathIsNotEmpty);
123
124 if (nPathFlags & DictionaryPathFlags::INTERNAL)
125 std::copy_if(std::cbegin(aInternalPaths), std::cend(aInternalPaths), std::back_inserter(aRes), lPathIsNotEmpty);
126 }
127
128 return aRes;
129}
130
132{
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;
140}
141
142std::vector< OUString > GetDictionaryPaths()
143{
144 return GetMultiPaths_Impl( u"Dictionary", PATH_FLAG_ALL );
145}
146
147OUString GetWritableDictionaryURL( std::u16string_view rDicName )
148{
149 // new user writable dictionaries should be created in the 'writable' path
150 OUString aDirName( GetDictionaryWriteablePath() );
151
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");
159
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.)
164}
165
166} // namespace linguistic
167
168/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetSmartProtocol(INetProtocol eTheSmartScheme)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool HasError() const
bool SetSmartURL(std::u16string_view rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
bool Append(std::u16string_view rTheSegment, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
#define DBG_ASSERT(sCon, aError)
float u
#define PATH_FLAG_ALL
Definition: misc2.cxx:54
NONE
Reference< XComponentContext > getProcessComponentContext()
constexpr OUStringLiteral USER
OUString GetWritableDictionaryURL(std::u16string_view rDicName)
Definition: misc2.cxx:147
bool FileExists(const OUString &rMainURL)
Definition: misc2.cxx:60
static std::vector< OUString > GetMultiPaths_Impl(std::u16string_view rPathPrefix, DictionaryPathFlags nPathFlags)
Definition: misc2.cxx:79
OUString GetDictionaryWriteablePath()
Definition: misc2.cxx:131
std::vector< OUString > GetDictionaryPaths()
Definition: misc2.cxx:142