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