Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/linguistic/source/misc2.cxx
Line
Count
Source
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>
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>
32
33
#include <linguistic/misc.hxx>
34
35
using namespace com::sun::star;
36
37
namespace {
38
39
/// Flags to be used with the multi-path related functions
40
/// @see GetDictionaryPaths
41
enum class DictionaryPathFlags
42
{
43
    NONE      = 0x00,
44
    INTERNAL  = 0x01,
45
    USER      = 0x02,
46
};
47
48
}
49
50
namespace o3tl
51
{
52
    template<> struct typed_flags<DictionaryPathFlags> : is_typed_flags<DictionaryPathFlags, 0x03> {};
53
}
54
0
#define PATH_FLAG_ALL       (DictionaryPathFlags::INTERNAL | DictionaryPathFlags::USER)
55
56
namespace linguistic
57
{
58
59
60
bool FileExists( const OUString &rMainURL )
61
0
{
62
0
    bool bExists = false;
63
0
    if (!rMainURL.isEmpty())
64
0
    {
65
0
        try
66
0
        {
67
0
            ::ucbhelper::Content aContent( rMainURL,
68
0
                    uno::Reference< css::ucb::XCommandEnvironment >(),
69
0
                    comphelper::getProcessComponentContext());
70
0
            bExists = aContent.isDocument();
71
0
        }
72
0
        catch (uno::Exception &)
73
0
        {
74
0
        }
75
0
    }
76
0
    return bExists;
77
0
}
78
79
static std::vector< OUString > GetMultiPaths_Impl(
80
    std::u16string_view rPathPrefix,
81
    DictionaryPathFlags nPathFlags )
82
0
{
83
0
    std::vector< OUString >     aRes;
84
0
    uno::Sequence< OUString >   aInternalPaths;
85
0
    uno::Sequence< OUString >   aUserPaths;
86
0
    OUString                    aWritablePath;
87
88
0
    bool bSuccess = true;
89
0
    const uno::Reference< uno::XComponentContext >&  xContext( comphelper::getProcessComponentContext() );
90
0
    try
91
0
    {
92
0
        OUString aInternal( OUString::Concat(rPathPrefix) + "_internal" );
93
0
        OUString aUser( OUString::Concat(rPathPrefix) + "_user" );
94
0
        OUString aWriteable( OUString::Concat(rPathPrefix) + "_writable" );
95
96
0
        uno::Reference< util::XPathSettings > xPathSettings =
97
0
            util::thePathSettings::get( xContext );
98
0
        xPathSettings->getPropertyValue( aInternal )  >>= aInternalPaths;
99
0
        xPathSettings->getPropertyValue( aUser )      >>= aUserPaths;
100
0
        xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
101
0
    }
102
0
    catch (uno::Exception &)
103
0
    {
104
0
        bSuccess = false;
105
0
    }
106
0
    if (bSuccess)
107
0
    {
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
0
        sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
113
0
        if (!aWritablePath.isEmpty())
114
0
            ++nMaxEntries;
115
0
        aRes.reserve( nMaxEntries );
116
0
        if (!aWritablePath.isEmpty())
117
0
            aRes.push_back(aWritablePath);
118
119
0
        auto lPathIsNotEmpty = [](const OUString& rPath) { return !rPath.isEmpty(); };
120
121
0
        if (nPathFlags & DictionaryPathFlags::USER)
122
0
            std::copy_if(std::cbegin(aUserPaths), std::cend(aUserPaths), std::back_inserter(aRes), lPathIsNotEmpty);
123
124
0
        if (nPathFlags & DictionaryPathFlags::INTERNAL)
125
0
            std::copy_if(std::cbegin(aInternalPaths), std::cend(aInternalPaths), std::back_inserter(aRes), lPathIsNotEmpty);
126
0
    }
127
128
0
    return aRes;
129
0
}
130
131
OUString GetDictionaryWriteablePath()
132
0
{
133
0
    std::vector< OUString > aPaths(
134
0
        GetMultiPaths_Impl( u"Dictionary", DictionaryPathFlags::NONE ) );
135
0
    DBG_ASSERT( aPaths.size() == 1, "Dictionary_writable path corrupted?" );
136
0
    OUString aRes;
137
0
    if (!aPaths.empty())
138
0
        aRes = aPaths[0];
139
0
    return aRes;
140
0
}
141
142
std::vector< OUString > GetDictionaryPaths()
143
0
{
144
0
    return GetMultiPaths_Impl( u"Dictionary", PATH_FLAG_ALL );
145
0
}
146
147
OUString  GetWritableDictionaryURL( std::u16string_view rDicName )
148
0
{
149
    // new user writable dictionaries should be created in the 'writable' path
150
0
    OUString aDirName( GetDictionaryWriteablePath() );
151
152
    // build URL to use for a new (persistent) dictionary
153
0
    INetURLObject aURLObj;
154
0
    aURLObj.SetSmartProtocol( INetProtocol::File );
155
0
    aURLObj.SetSmartURL( aDirName );
156
0
    DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
157
0
    aURLObj.Append( rDicName, INetURLObject::EncodeMechanism::All );
158
0
    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.)
163
0
    return aURLObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
164
0
}
165
166
}   // namespace linguistic
167
168
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */