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 
10 /*
11  * The world's quickest and lamest .desktop / .ini file parser.
12  * Ideally the .thm file would move to a .desktop file in
13  * future.
14  */
15 
16 #include <sal/config.h>
17 #include <sal/log.hxx>
18 
19 #include <unotools/ucbstreamhelper.hxx>
20 #include <svx/gallerybinaryengineentry.hxx>
21 #include <i18nlangtag/languagetag.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/settings.hxx>
24 #include <memory>
25 
ReadStrFromIni(std::u16string_view aKeyName)26 OUString GalleryBinaryEngineEntry::ReadStrFromIni(std::u16string_view aKeyName )
27 {
28     std::unique_ptr<SvStream> pStrm(::utl::UcbStreamHelper::CreateStream(
29         GetStrURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ),
30                                 StreamMode::READ ));
31 
32     const LanguageTag &rLangTag = Application::GetSettings().GetUILanguageTag();
33 
34     ::std::vector< OUString > aFallbacks = rLangTag.getFallbackStrings( true);
35 
36     OUString aResult;
37     sal_Int32 nRank = 42;
38 
39     if( pStrm )
40     {
41         OString aLine;
42         while( pStrm->ReadLine( aLine ) )
43         {
44             OUString aKey;
45             OUString aLocale;
46             OUString aValue;
47             sal_Int32 n;
48 
49             // comments
50             if( aLine.startsWith( "#" ) )
51                 continue;
52 
53             // a[en_US] = Bob
54             if( ( n = aLine.indexOf( '=' ) ) >= 1)
55             {
56                 aKey = OStringToOUString(
57                     aLine.copy( 0, n ).trim(), RTL_TEXTENCODING_ASCII_US );
58                 aValue = OStringToOUString(
59                     aLine.copy( n + 1 ).trim(), RTL_TEXTENCODING_UTF8 );
60 
61                 if( ( n = aKey.indexOf( '[' ) ) >= 1 )
62                 {
63                     aLocale = aKey.copy( n + 1 ).trim();
64                     aKey = aKey.copy( 0, n ).trim();
65                     if( (n = aLocale.indexOf( ']' ) ) >= 1 )
66                         aLocale = aLocale.copy( 0, n ).trim();
67                 }
68             }
69             SAL_INFO("svx", "ini file has '" << aKey << "' [ '" << aLocale << "' ] = '" << aValue << "'");
70 
71             // grisly language matching, is this not available somewhere else?
72             if( aKey == aKeyName )
73             {
74                 /* FIXME-BCP47: what is this supposed to do? */
75                 n = 0;
76                 OUString aLang = aLocale.replace('_','-');
77                 for( const auto& rFallback : aFallbacks )
78                 {
79                     SAL_INFO( "svx", "compare '" << aLang << "' with '" << rFallback << "' rank " << nRank << " vs. " << n );
80                     if( rFallback == aLang && n < nRank ) {
81                         nRank = n; // try to get the most accurate match
82                         aResult = aValue;
83                     }
84                     ++n;
85                 }
86             }
87         }
88     }
89 
90     SAL_INFO( "svx", "readStrFromIni returns '" << aResult << "'");
91     return aResult;
92 }
93 
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
95