Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / source / app / IconThemeInfo.cxx
blobf3d054b066ce6c70aca334d355677a7e8a756c6c
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 OUString vcl::IconThemeInfo::HIGH_CONTRAST_ID = "hicontrast";
21 namespace {
23 static const OUString HIGH_CONTRAST_DISPLAY_NAME = "High Contrast";
24 static const OUString TANGO_TESTING_ID = "tango_testing";
25 static const OUString TANGO_TESTING_DISPLAY_NAME = "Tango Testing";
27 OUString
28 filename_from_url(const OUString& url)
30 sal_Int32 slashPosition = url.lastIndexOf( '/' );
31 if (slashPosition < 0) {
32 return OUString("");
34 OUString filename = url.copy( slashPosition+1 );
35 return filename;
38 } // end anonymous namespace
40 namespace vcl {
42 static const char ICON_THEME_PACKAGE_PREFIX[] = "images_";
44 static const char EXTENSION_FOR_ICON_PACKAGES[] = ".zip";
46 IconThemeInfo::IconThemeInfo()
50 IconThemeInfo::IconThemeInfo(const OUString& urlToFile)
51 : mUrlToFile(urlToFile)
53 OUString filename = filename_from_url(urlToFile);
54 if (filename.isEmpty()) {
55 throw std::runtime_error("invalid URL passed to IconThemeInfo()");
58 mThemeId = FileNameToThemeId(filename);
59 mDisplayName = ThemeIdToDisplayName(mThemeId);
63 /*static*/ Size
64 IconThemeInfo::SizeByThemeName(const OUString& themeName)
66 if (themeName == "tango") {
67 return Size( 24, 24 );
69 else if (themeName == "crystal") {
70 return Size( 22, 22 );
72 else if (themeName == "galaxy") {
73 return Size( 22, 22 );
75 else {
76 return Size( 26, 26 );
80 /*static*/ bool
81 IconThemeInfo::UrlCanBeParsed(const OUString& url)
83 OUString fname = filename_from_url(url);
84 if (fname.isEmpty()) {
85 return false;
88 if (!fname.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX)) {
89 return false;
92 if (!fname.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES)) {
93 return false;
96 return true;
99 /*static*/ OUString
100 IconThemeInfo::FileNameToThemeId(const OUString& filename)
102 OUString r;
103 sal_Int32 positionOfLastDot = filename.lastIndexOf(EXTENSION_FOR_ICON_PACKAGES);
104 if (positionOfLastDot < 0) { // -1 means index not found
105 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
107 sal_Int32 positionOfFirstUnderscore = filename.indexOf(ICON_THEME_PACKAGE_PREFIX);
108 if (positionOfFirstUnderscore < 0) { // -1 means index not found. Use the whole name instead
109 throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
111 positionOfFirstUnderscore += RTL_CONSTASCII_LENGTH(ICON_THEME_PACKAGE_PREFIX);
112 r = filename.copy(positionOfFirstUnderscore, positionOfLastDot - positionOfFirstUnderscore);
113 return r;
116 /*static*/ OUString
117 IconThemeInfo::ThemeIdToDisplayName(const OUString& themeId)
119 if (themeId.isEmpty()) {
120 throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
123 // sepcial cases
124 if (themeId.equalsIgnoreAsciiCase(HIGH_CONTRAST_ID)) {
125 return HIGH_CONTRAST_DISPLAY_NAME;
127 else if (themeId.equalsIgnoreAsciiCase(TANGO_TESTING_ID)) {
128 return TANGO_TESTING_DISPLAY_NAME;
131 // make the first letter uppercase
132 OUString r;
133 sal_Unicode firstLetter = themeId[0];
134 if (rtl::isAsciiLowerCase(firstLetter)) {
135 r = OUString(rtl::toAsciiUpperCase(firstLetter));
136 r += themeId.copy(1);
138 else {
139 r = themeId;
142 return r;
145 namespace
147 class SameTheme :
148 public std::unary_function<const vcl::IconThemeInfo &, bool>
150 private:
151 const OUString& m_rThemeId;
152 public:
153 SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
154 bool operator()(const vcl::IconThemeInfo &rInfo)
156 return m_rThemeId == rInfo.GetThemeId();
161 /*static*/ const vcl::IconThemeInfo&
162 IconThemeInfo::FindIconThemeById(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
164 std::vector<vcl::IconThemeInfo>::const_iterator it = std::find_if(themes.begin(), themes.end(),
165 SameTheme(themeId));
166 if (it == themes.end())
168 throw std::runtime_error("Could not find theme id in theme vector.");
170 return *it;
173 /*static*/ bool
174 IconThemeInfo::IconThemeIsInVector(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
176 return std::any_of(themes.begin(), themes.end(), SameTheme(themeId));
179 } // end namespace vcl
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */