Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / source / app / IconThemeInfo.cxx
blob5c70d1998b9779d9fc218692bfe924dd985c3f1e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <vcl/IconThemeInfo.hxx>
11 #include <rtl/character.hxx>
13 #include <stdexcept>
14 #include <algorithm>
16 // constants for theme ids and display names. Only the theme id for hicontrast is used
17 // outside of this class and hence made public.
19 const OUStringLiteral vcl::IconThemeInfo::HIGH_CONTRAST_ID("hicontrast");
21 namespace {
23 static const OUStringLiteral HIGH_CONTRAST_DISPLAY_NAME("High Contrast");
24 static const OUStringLiteral TANGO_TESTING_ID("tango_testing");
25 static const OUStringLiteral TANGO_TESTING_DISPLAY_NAME("Tango Testing");
26 static const OUStringLiteral BREEZE_DARK_ID("breeze_dark");
27 static const OUStringLiteral BREEZE_DARK_DISPLAY_NAME("Breeze Dark");
28 static const OUStringLiteral SIFR_DARK_ID("sifr_dark");
29 static const OUStringLiteral SIFR_DARK_DISPLAY_NAME("Sifr Dark");
31 static const OUStringLiteral HELPIMG_FAKE_THEME("helpimg");
33 OUString
34 filename_from_url(const OUString& url)
36 sal_Int32 slashPosition = url.lastIndexOf( '/' );
37 if (slashPosition < 0) {
38 return OUString();
40 OUString filename = url.copy( slashPosition+1 );
41 return filename;
44 } // end anonymous namespace
46 namespace vcl {
48 static const char ICON_THEME_PACKAGE_PREFIX[] = "images_";
50 static const char EXTENSION_FOR_ICON_PACKAGES[] = ".zip";
52 IconThemeInfo::IconThemeInfo()
56 IconThemeInfo::IconThemeInfo(const OUString& urlToFile)
57 : mUrlToFile(urlToFile)
59 OUString filename = filename_from_url(urlToFile);
60 if (filename.isEmpty()) {
61 throw std::runtime_error("invalid URL passed to IconThemeInfo()");
64 mThemeId = FileNameToThemeId(filename);
65 mDisplayName = ThemeIdToDisplayName(mThemeId);
69 /*static*/ Size
70 IconThemeInfo::SizeByThemeName(const OUString& themeName)
72 if (themeName == "tango") {
73 return Size( 24, 24 );
75 else if (themeName == "crystal") {
76 return Size( 22, 22 );
78 else if (themeName == "galaxy") {
79 return Size( 22, 22 );
81 else {
82 return Size( 26, 26 );
86 /*static*/ bool
87 IconThemeInfo::UrlCanBeParsed(const OUString& url)
89 OUString fname = filename_from_url(url);
90 if (fname.isEmpty()) {
91 return false;
94 if (!fname.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX)) {
95 return false;
98 if (!fname.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES)) {
99 return false;
102 if (fname.indexOf(HELPIMG_FAKE_THEME) != -1 ) {
103 return false;
106 return true;
109 /*static*/ OUString
110 IconThemeInfo::FileNameToThemeId(const OUString& filename)
112 OUString r;
113 sal_Int32 positionOfLastDot = filename.lastIndexOf(EXTENSION_FOR_ICON_PACKAGES);
114 if (positionOfLastDot < 0) { // -1 means index not found
115 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
117 sal_Int32 positionOfFirstUnderscore = filename.indexOf(ICON_THEME_PACKAGE_PREFIX);
118 if (positionOfFirstUnderscore < 0) { // -1 means index not found. Use the whole name instead
119 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
121 positionOfFirstUnderscore += RTL_CONSTASCII_LENGTH(ICON_THEME_PACKAGE_PREFIX);
122 r = filename.copy(positionOfFirstUnderscore, positionOfLastDot - positionOfFirstUnderscore);
123 return r;
126 /*static*/ OUString
127 IconThemeInfo::ThemeIdToDisplayName(const OUString& themeId)
129 if (themeId.isEmpty()) {
130 throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
133 // special cases
134 if (themeId.equalsIgnoreAsciiCase(HIGH_CONTRAST_ID)) {
135 return HIGH_CONTRAST_DISPLAY_NAME;
137 else if (themeId.equalsIgnoreAsciiCase(TANGO_TESTING_ID)) {
138 return TANGO_TESTING_DISPLAY_NAME;
140 else if (themeId.equalsIgnoreAsciiCase(BREEZE_DARK_ID)) {
141 return BREEZE_DARK_DISPLAY_NAME;
143 else if (themeId.equalsIgnoreAsciiCase(SIFR_DARK_ID)) {
144 return SIFR_DARK_DISPLAY_NAME;
147 // make the first letter uppercase
148 OUString r;
149 sal_Unicode firstLetter = themeId[0];
150 if (rtl::isAsciiLowerCase(firstLetter)) {
151 r = OUString(sal_Unicode(rtl::toAsciiUpperCase(firstLetter)));
152 r += themeId.copy(1);
154 else {
155 r = themeId;
158 return r;
161 namespace
163 class SameTheme :
164 public std::unary_function<const vcl::IconThemeInfo &, bool>
166 private:
167 const OUString& m_rThemeId;
168 public:
169 explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
170 bool operator()(const vcl::IconThemeInfo &rInfo)
172 return m_rThemeId == rInfo.GetThemeId();
177 /*static*/ const vcl::IconThemeInfo&
178 IconThemeInfo::FindIconThemeById(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
180 std::vector<vcl::IconThemeInfo>::const_iterator it = std::find_if(themes.begin(), themes.end(),
181 SameTheme(themeId));
182 if (it == themes.end())
184 throw std::runtime_error("Could not find theme id in theme vector.");
186 return *it;
189 /*static*/ bool
190 IconThemeInfo::IconThemeIsInVector(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
192 return std::any_of(themes.begin(), themes.end(), SameTheme(themeId));
195 } // end namespace vcl
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */