summaryrefslogtreecommitdiffstats
path: root/svx/source/gallery2/galini.cxx
blob: 8cdfb23ae1a2bcb427d918cc27ae9ec886a05f83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * The world's quickest and lamest .desktop / .ini file parser.
 * Ideally the .thm file would move to a .desktop file in
 * future.
 */

#include <sal/config.h>
#include <sal/log.hxx>

#include <unotools/ucbstreamhelper.hxx>
#include <svx/gallerybinaryengineentry.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <vcl/svapp.hxx>
#include <vcl/settings.hxx>
#include <o3tl/string_view.hxx>
#include <memory>

OUString GalleryBinaryEngineEntry::ReadStrFromIni(std::u16string_view aKeyName ) const
{
    std::unique_ptr<SvStream> pStrm(::utl::UcbStreamHelper::CreateStream(
        GetStrURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ),
                                StreamMode::READ ));

    const LanguageTag &rLangTag = Application::GetSettings().GetUILanguageTag();

    ::std::vector< OUString > aFallbacks = rLangTag.getFallbackStrings( true);

    OUString aResult;
    sal_Int32 nRank = 42;

    if( pStrm )
    {
        OString aLine;
        while( pStrm->ReadLine( aLine ) )
        {
            OUString aKey;
            OUString aLocale;
            OUString aValue;
            sal_Int32 n;

            // comments
            if( aLine.startsWith( "#" ) )
                continue;

            // a[en_US] = Bob
            if( ( n = aLine.indexOf( '=' ) ) >= 1)
            {
                aKey = OStringToOUString(
                    o3tl::trim(aLine.subView( 0, n )), RTL_TEXTENCODING_ASCII_US );
                aValue = OStringToOUString(
                    o3tl::trim(aLine.subView( n + 1 )), RTL_TEXTENCODING_UTF8 );

                if( ( n = aKey.indexOf( '[' ) ) >= 1 )
                {
                    aLocale = o3tl::trim(aKey.subView( n + 1 ));
                    aKey = o3tl::trim(aKey.subView( 0, n ));
                    if( (n = aLocale.indexOf( ']' ) ) >= 1 )
                        aLocale = o3tl::trim(aLocale.subView( 0, n ));
                }
            }
            SAL_INFO("svx", "ini file has '" << aKey << "' [ '" << aLocale << "' ] = '" << aValue << "'");

            // grisly language matching, is this not available somewhere else?
            if( aKey == aKeyName )
            {
                /* FIXME-BCP47: what is this supposed to do? */
                n = 0;
                OUString aLang = aLocale.replace('_','-');
                for( const auto& rFallback : aFallbacks )
                {
                    SAL_INFO( "svx", "compare '" << aLang << "' with '" << rFallback << "' rank " << nRank << " vs. " << n );
                    if( rFallback == aLang && n < nRank ) {
                        nRank = n; // try to get the most accurate match
                        aResult = aValue;
                    }
                    ++n;
                }
            }
        }
    }

    SAL_INFO( "svx", "readStrFromIni returns '" << aResult << "'");
    return aResult;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */