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 OUString
vcl::IconThemeInfo::HIGH_CONTRAST_ID
= "hicontrast";
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";
28 filename_from_url(const OUString
& url
)
30 sal_Int32 slashPosition
= url
.lastIndexOf( '/' );
31 if (slashPosition
< 0) {
34 OUString filename
= url
.copy( slashPosition
+1 );
38 } // end anonymous namespace
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
);
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 );
76 return Size( 26, 26 );
81 IconThemeInfo::UrlCanBeParsed(const OUString
& url
)
83 OUString fname
= filename_from_url(url
);
84 if (fname
.isEmpty()) {
88 if (!fname
.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX
)) {
92 if (!fname
.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES
)) {
100 IconThemeInfo::FileNameToThemeId(const OUString
& filename
)
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
);
117 IconThemeInfo::ThemeIdToDisplayName(const OUString
& themeId
)
119 if (themeId
.isEmpty()) {
120 throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
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
133 sal_Unicode firstLetter
= themeId
[0];
134 if (rtl::isAsciiLowerCase(firstLetter
)) {
135 r
= OUString(rtl::toAsciiUpperCase(firstLetter
));
136 r
+= themeId
.copy(1);
148 public std::unary_function
<const vcl::IconThemeInfo
&, bool>
151 const OUString
& m_rThemeId
;
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(),
166 if (it
== themes
.end())
168 throw std::runtime_error("Could not find theme id in theme vector.");
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: */