LibreOffice Module vcl (master) 1
IconThemeInfo.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
10#include <vcl/IconThemeInfo.hxx>
11#include <rtl/character.hxx>
12
13#include <stdexcept>
14#include <algorithm>
15
16// constants for theme ids and display names. (The theme id for high contrast is used
17// outside of this class and hence made public in IconThemeInfo.)
18
19namespace {
20
21constexpr OUStringLiteral HELPIMG_FAKE_THEME(u"helpimg");
22
23OUString
24filename_from_url(std::u16string_view url)
25{
26 size_t slashPosition = url.rfind( '/' );
27 if (slashPosition == std::u16string_view::npos) {
28 return OUString();
29 }
30 OUString filename( url.substr( slashPosition+1 ) );
31 return filename;
32}
33
34} // end anonymous namespace
35
36namespace vcl {
37
39
41
43{
44}
45
46IconThemeInfo::IconThemeInfo(const OUString& urlToFile)
47: mUrlToFile(urlToFile)
48{
49 OUString filename = filename_from_url(urlToFile);
50 if (filename.isEmpty()) {
51 throw std::runtime_error("invalid URL passed to IconThemeInfo()");
52 }
53
54 mThemeId = FileNameToThemeId(filename);
56
57}
58
59/*static*/ Size
60IconThemeInfo::SizeByThemeName(std::u16string_view themeName)
61{
62 if (themeName == u"galaxy") { //kept for compiler because of unused parameter 'themeName'
63 return Size( 26, 26 );
64 }
65 else {
66 return Size( 24, 24 );
67 }
68}
69
70/*static*/ bool
71IconThemeInfo::UrlCanBeParsed(std::u16string_view url)
72{
73 OUString fname = filename_from_url(url);
74 if (fname.isEmpty()) {
75 return false;
76 }
77
78 if (!fname.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX)) {
79 return false;
80 }
81
82 if (!fname.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES)) {
83 return false;
84 }
85
86 if (fname.indexOf(HELPIMG_FAKE_THEME) != -1 ) {
87 return false;
88 }
89
90 return true;
91}
92
93/*static*/ OUString
94IconThemeInfo::FileNameToThemeId(std::u16string_view filename)
95{
96 OUString r;
97 size_t positionOfLastDot = filename.rfind(EXTENSION_FOR_ICON_PACKAGES);
98 if (positionOfLastDot == std::u16string_view::npos) { // means index not found
99 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
100 }
101 size_t positionOfFirstUnderscore = filename.find(ICON_THEME_PACKAGE_PREFIX);
102 if (positionOfFirstUnderscore == std::u16string_view::npos) { // means index not found. Use the whole name instead
103 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
104 }
105 positionOfFirstUnderscore += RTL_CONSTASCII_LENGTH(ICON_THEME_PACKAGE_PREFIX);
106 r = filename.substr(positionOfFirstUnderscore, positionOfLastDot - positionOfFirstUnderscore);
107 return r;
108}
109
110/*static*/ OUString
112{
113 if (themeId.isEmpty()) {
114 throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
115 }
116
117 // Strip _svg and _dark filename "extensions"
118 OUString aDisplayName = themeId;
119
120 bool bIsSvg = aDisplayName.endsWith("_svg", &aDisplayName);
121 bool bIsDark = aDisplayName.endsWith("_dark", &aDisplayName);
122 if (!bIsSvg && bIsDark)
123 bIsSvg = aDisplayName.endsWith("_svg", &aDisplayName);
124
125 // make the first letter uppercase
126 sal_Unicode firstLetter = aDisplayName[0];
127 if (rtl::isAsciiLowerCase(firstLetter))
128 {
129 aDisplayName = OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(firstLetter))) + aDisplayName.subView(1);
130 }
131
132 // replacing underscores with spaces of multi words pack name.
133 aDisplayName = aDisplayName.replace('_', ' ');
134
135 if (bIsSvg && bIsDark)
136 aDisplayName += " (SVG + dark)";
137 else if (bIsSvg)
138 aDisplayName += " (SVG)";
139 else if (bIsDark)
140 aDisplayName += " (dark)";
141
142 return aDisplayName;
143}
144
145namespace
146{
147 class SameTheme
148 {
149 private:
150 const OUString& m_rThemeId;
151 public:
152 explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
153 bool operator()(const vcl::IconThemeInfo &rInfo)
154 {
155 return m_rThemeId == rInfo.GetThemeId();
156 }
157 };
158}
159
160/*static*/ const vcl::IconThemeInfo&
161IconThemeInfo::FindIconThemeById(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
162{
163 std::vector<vcl::IconThemeInfo>::const_iterator it = std::find_if(themes.begin(), themes.end(),
164 SameTheme(themeId));
165 if (it == themes.end())
166 {
167 throw std::runtime_error("Could not find theme id in theme vector.");
168 }
169 return *it;
170}
171
172/*static*/ bool
173IconThemeInfo::IconThemeIsInVector(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
174{
175 return std::any_of(themes.begin(), themes.end(), SameTheme(themeId));
176}
177
178} // end namespace vcl
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & m_rThemeId
This class provides information about an icon theme.
static const vcl::IconThemeInfo & FindIconThemeById(const std::vector< vcl::IconThemeInfo > &themes, const OUString &themeId)
Find an icon theme by its id in a vector.
IconThemeInfo()
private constructor for testing purposes only
static bool UrlCanBeParsed(std::u16string_view url)
Check whether an IconThemeInfo can be constructed from a URL.
static OUString ThemeIdToDisplayName(const OUString &)
Creates the display name for the given id of a file.
const OUString & GetThemeId() const
OUString mDisplayName
The name which is presented to the user.
static OUString FileNameToThemeId(std::u16string_view)
Determine the icon theme name from the filename If the name has an underscore, the name is taken from...
static Size SizeByThemeName(std::u16string_view)
Obtain the icon size by theme name.
static bool IconThemeIsInVector(const std::vector< vcl::IconThemeInfo > &themes, const OUString &themeId)
Check whether a theme with a specified id is in a vector of IconThemeInfo.
OUString mThemeId
The theme id.
float u
const sal_Unicode ICON_THEME_PACKAGE_PREFIX[]
const sal_Unicode EXTENSION_FOR_ICON_PACKAGES[]
sal_uInt16 sal_Unicode