1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <vcl/IconThemeInfo.hxx>
11 #include <rtl/character.hxx>
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");
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");
30 filename_from_url(const OUString
& url
)
32 sal_Int32 slashPosition
= url
.lastIndexOf( '/' );
33 if (slashPosition
< 0) {
36 OUString filename
= url
.copy( slashPosition
+1 );
40 } // end anonymous namespace
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
);
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 );
78 return Size( 26, 26 );
83 IconThemeInfo::UrlCanBeParsed(const OUString
& url
)
85 OUString fname
= filename_from_url(url
);
86 if (fname
.isEmpty()) {
90 if (!fname
.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX
)) {
94 if (!fname
.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES
)) {
102 IconThemeInfo::FileNameToThemeId(const OUString
& filename
)
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
);
119 IconThemeInfo::ThemeIdToDisplayName(const OUString
& themeId
)
121 if (themeId
.isEmpty()) {
122 throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
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
138 sal_Unicode firstLetter
= themeId
[0];
139 if (rtl::isAsciiLowerCase(firstLetter
)) {
140 r
= OUString(sal_Unicode(rtl::toAsciiUpperCase(firstLetter
)));
141 r
+= themeId
.copy(1);
153 public std::unary_function
<const vcl::IconThemeInfo
&, bool>
156 const OUString
& m_rThemeId
;
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(),
171 if (it
== themes
.end())
173 throw std::runtime_error("Could not find theme id in theme vector.");
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: */