1 /*
2     SPDX-FileCopyrightText: 2012-2020 Harald Sitter <sitter@kde.org>
3     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
4 */
5 
6 #include "Entry.h"
7 
Entry(const KLocalizedString & label_,const QString & value_)8 Entry::Entry(const KLocalizedString &label_, const QString &value_)
9     : m_label(label_)
10     , m_value(value_)
11 {
12     Q_ASSERT(m_label.isEmpty() || localizedLabel(Language::English).endsWith(':'));
13 }
14 
15 Entry::~Entry() = default;
16 
17 // When false this entry is garbage (e.g. incomplete data) and shouldn't be rendered.
isValid() const18 bool Entry::isValid() const
19 {
20     return !localizedLabel().isEmpty() && !localizedValue().isEmpty();
21 }
22 
23 // Returns textual representation of entry.
diagnosticLine(Language language) const24 QString Entry::diagnosticLine(Language language) const
25 {
26     // FIXME: This isn't really working for right-to-left
27     // The answer probably is in unicode control characters, but
28     // didn't work when tried.
29     // Essentially what needs to happen is that the label should be RTL
30     // that is to say the colon should be on the left, BUT englishy words
31     // within that should be LTR, everything besides the label should be LTR
32     // because we do not localize the values I don't think?
33     return localizedLabel(language) + ' ' + localizedValue(language) + '\n';
34 }
35 
localize(const KLocalizedString & string,Language language) const36 QString Entry::localize(const KLocalizedString &string, Language language) const
37 {
38     switch (language) {
39     case Language::System:
40         return string.toString();
41     case Language::English:
42         // https://bugs.kde.org/show_bug.cgi?id=416247
43         return string.toString(QStringList{QStringLiteral("en_US")});
44     }
45     Q_UNREACHABLE();
46     return QStringLiteral("Unknown Language %1 (bug in KInfocenter!):").arg(QString::number(static_cast<int>(language)));
47 }
48 
localizedLabel(Language language) const49 QString Entry::localizedLabel(Language language) const
50 {
51     return localize(m_label, language);
52 }
53 
localizedValue(Language language) const54 QString Entry::localizedValue(Language language) const
55 {
56     Q_UNUSED(language);
57     return m_value;
58 }
59 
localeForLanguage(Language language) const60 QLocale Entry::localeForLanguage(Language language) const
61 {
62     switch (language) {
63     case Language::System:
64         return QLocale::system();
65     case Language::English:
66         return QLocale(QLocale::English, QLocale::UnitedStates);
67     }
68     Q_UNREACHABLE();
69     return QLocale::system();
70 }
71