build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / app / IconThemeInfo.cxx
blob3c886be551425824ced57dfe695d6e5b7269d697
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");
29 OUString
30 filename_from_url(const OUString& url)
32 sal_Int32 slashPosition = url.lastIndexOf( '/' );
33 if (slashPosition < 0) {
34 return OUString("");
36 OUString filename = url.copy( slashPosition+1 );
37 return filename;
40 } // end anonymous namespace
42 namespace vcl {
44 static const char ICON_THEME_PACKAGE_PREFIX[] = "images_";
46 static const char EXTENSION_FOR_ICON_PACKAGES[] = ".zip";
48 IconThemeInfo::IconThemeInfo()
52 IconThemeInfo::IconThemeInfo(const OUString& urlToFile)
53 : mUrlToFile(urlToFile)
55 OUString filename = filename_from_url(urlToFile);
56 if (filename.isEmpty()) {
57 throw std::runtime_error("invalid URL passed to IconThemeInfo()");
60 mThemeId = FileNameToThemeId(filename);
61 mDisplayName = ThemeIdToDisplayName(mThemeId);
65 /*static*/ Size
66 IconThemeInfo::SizeByThemeName(const OUString& themeName)
68 if (themeName == "tango") {
69 return Size( 24, 24 );
71 else if (themeName == "crystal") {
72 return Size( 22, 22 );
74 else if (themeName == "galaxy") {
75 return Size( 22, 22 );
77 else {
78 return Size( 26, 26 );
82 /*static*/ bool
83 IconThemeInfo::UrlCanBeParsed(const OUString& url)
85 OUString fname = filename_from_url(url);
86 if (fname.isEmpty()) {
87 return false;
90 if (!fname.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX)) {
91 return false;
94 if (!fname.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES)) {
95 return false;
98 return true;
101 /*static*/ OUString
102 IconThemeInfo::FileNameToThemeId(const OUString& filename)
104 OUString r;
105 sal_Int32 positionOfLastDot = filename.lastIndexOf(EXTENSION_FOR_ICON_PACKAGES);
106 if (positionOfLastDot < 0) { // -1 means index not found
107 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
109 sal_Int32 positionOfFirstUnderscore = filename.indexOf(ICON_THEME_PACKAGE_PREFIX);
110 if (positionOfFirstUnderscore < 0) { // -1 means index not found. Use the whole name instead
111 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
113 positionOfFirstUnderscore += RTL_CONSTASCII_LENGTH(ICON_THEME_PACKAGE_PREFIX);
114 r = filename.copy(positionOfFirstUnderscore, positionOfLastDot - positionOfFirstUnderscore);
115 return r;
118 /*static*/ OUString
119 IconThemeInfo::ThemeIdToDisplayName(const OUString& themeId)
121 if (themeId.isEmpty()) {
122 throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
125 // special cases
126 if (themeId.equalsIgnoreAsciiCase(HIGH_CONTRAST_ID)) {
127 return HIGH_CONTRAST_DISPLAY_NAME;
129 else if (themeId.equalsIgnoreAsciiCase(TANGO_TESTING_ID)) {
130 return TANGO_TESTING_DISPLAY_NAME;
132 else if (themeId.equalsIgnoreAsciiCase(BREEZE_DARK_ID)) {
133 return BREEZE_DARK_DISPLAY_NAME;
136 // make the first letter uppercase
137 OUString r;
138 sal_Unicode firstLetter = themeId[0];
139 if (rtl::isAsciiLowerCase(firstLetter)) {
140 r = OUString(sal_Unicode(rtl::toAsciiUpperCase(firstLetter)));
141 r += themeId.copy(1);
143 else {
144 r = themeId;
147 return r;
150 namespace
152 class SameTheme :
153 public std::unary_function<const vcl::IconThemeInfo &, bool>
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)
161 return m_rThemeId == rInfo.GetThemeId();
166 /*static*/ const vcl::IconThemeInfo&
167 IconThemeInfo::FindIconThemeById(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
169 std::vector<vcl::IconThemeInfo>::const_iterator it = std::find_if(themes.begin(), themes.end(),
170 SameTheme(themeId));
171 if (it == themes.end())
173 throw std::runtime_error("Could not find theme id in theme vector.");
175 return *it;
178 /*static*/ bool
179 IconThemeInfo::IconThemeIsInVector(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
181 return std::any_of(themes.begin(), themes.end(), SameTheme(themeId));
184 } // end namespace vcl
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */