Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <vcl/IconThemeInfo.hxx>
11 : #include <rtl/character.hxx>
12 :
13 : #include <stdexcept>
14 : #include <algorithm>
15 :
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.
18 :
19 267 : const OUString vcl::IconThemeInfo::HIGH_CONTRAST_ID = "hicontrast";
20 :
21 : namespace {
22 :
23 267 : static const OUString HIGH_CONTRAST_DISPLAY_NAME = "High Contrast";
24 267 : static const OUString TANGO_TESTING_ID = "tango_testing";
25 267 : static const OUString TANGO_TESTING_DISPLAY_NAME = "Tango Testing";
26 :
27 : OUString
28 10137 : filename_from_url(const OUString& url)
29 : {
30 10137 : sal_Int32 slashPosition = url.lastIndexOf( '/' );
31 10137 : if (slashPosition < 0) {
32 0 : return OUString("");
33 : }
34 10137 : OUString filename = url.copy( slashPosition+1 );
35 10137 : return filename;
36 : }
37 :
38 : } // end anonymous namespace
39 :
40 : namespace vcl {
41 :
42 : static const char ICON_THEME_PACKAGE_PREFIX[] = "images_";
43 :
44 : static const char EXTENSION_FOR_ICON_PACKAGES[] = ".zip";
45 :
46 7 : IconThemeInfo::IconThemeInfo()
47 : {
48 7 : }
49 :
50 2957 : IconThemeInfo::IconThemeInfo(const OUString& urlToFile)
51 2957 : : mUrlToFile(urlToFile)
52 : {
53 2957 : OUString filename = filename_from_url(urlToFile);
54 2957 : if (filename.isEmpty()) {
55 0 : throw std::runtime_error("invalid URL passed to IconThemeInfo()");
56 : }
57 :
58 2957 : mThemeId = FileNameToThemeId(filename);
59 2957 : mDisplayName = ThemeIdToDisplayName(mThemeId);
60 :
61 2957 : }
62 :
63 : /*static*/ Size
64 24 : IconThemeInfo::SizeByThemeName(const OUString& themeName)
65 : {
66 24 : if (themeName == "tango") {
67 24 : return Size( 24, 24 );
68 : }
69 0 : else if (themeName == "crystal") {
70 0 : return Size( 22, 22 );
71 : }
72 0 : else if (themeName == "galaxy") {
73 0 : return Size( 22, 22 );
74 : }
75 : else {
76 0 : return Size( 26, 26 );
77 : }
78 : }
79 :
80 : /*static*/ bool
81 7180 : IconThemeInfo::UrlCanBeParsed(const OUString& url)
82 : {
83 7180 : OUString fname = filename_from_url(url);
84 7180 : if (fname.isEmpty()) {
85 0 : return false;
86 : }
87 :
88 7180 : if (!fname.startsWithIgnoreAsciiCase(ICON_THEME_PACKAGE_PREFIX)) {
89 1268 : return false;
90 : }
91 :
92 5912 : if (!fname.endsWithIgnoreAsciiCase(EXTENSION_FOR_ICON_PACKAGES)) {
93 0 : return false;
94 : }
95 :
96 5912 : return true;
97 : }
98 :
99 : /*static*/ OUString
100 2962 : IconThemeInfo::FileNameToThemeId(const OUString& filename)
101 : {
102 2962 : OUString r;
103 2962 : sal_Int32 positionOfLastDot = filename.lastIndexOf(EXTENSION_FOR_ICON_PACKAGES);
104 2962 : if (positionOfLastDot < 0) { // -1 means index not found
105 1 : throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
106 : }
107 2961 : sal_Int32 positionOfFirstUnderscore = filename.indexOf(ICON_THEME_PACKAGE_PREFIX);
108 2961 : if (positionOfFirstUnderscore < 0) { // -1 means index not found. Use the whole name instead
109 1 : throw std::runtime_error("IconThemeInfo::FileNameToThemeId() called with invalid filename.");
110 : }
111 2960 : positionOfFirstUnderscore += RTL_CONSTASCII_LENGTH(ICON_THEME_PACKAGE_PREFIX);
112 2960 : r = filename.copy(positionOfFirstUnderscore, positionOfLastDot - positionOfFirstUnderscore);
113 2960 : return r;
114 : }
115 :
116 : /*static*/ OUString
117 2960 : IconThemeInfo::ThemeIdToDisplayName(const OUString& themeId)
118 : {
119 2960 : if (themeId.isEmpty()) {
120 0 : throw std::runtime_error("IconThemeInfo::ThemeIdToDisplayName() called with invalid id.");
121 : }
122 :
123 : // sepcial cases
124 2960 : if (themeId.equalsIgnoreAsciiCase(HIGH_CONTRAST_ID)) {
125 423 : return HIGH_CONTRAST_DISPLAY_NAME;
126 : }
127 2537 : else if (themeId.equalsIgnoreAsciiCase(TANGO_TESTING_ID)) {
128 423 : return TANGO_TESTING_DISPLAY_NAME;
129 : }
130 :
131 : // make the first letter uppercase
132 2114 : OUString r;
133 2114 : sal_Unicode firstLetter = themeId[0];
134 2114 : if (rtl::isAsciiLowerCase(firstLetter)) {
135 2114 : r = OUString(rtl::toAsciiUpperCase(firstLetter));
136 2114 : r += themeId.copy(1);
137 : }
138 : else {
139 0 : r = themeId;
140 : }
141 :
142 2114 : return r;
143 : }
144 :
145 : namespace
146 : {
147 : class SameTheme :
148 : public std::unary_function<const vcl::IconThemeInfo &, bool>
149 : {
150 : private:
151 : const OUString& m_rThemeId;
152 : public:
153 1 : explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
154 1 : bool operator()(const vcl::IconThemeInfo &rInfo)
155 : {
156 1 : return m_rThemeId == rInfo.GetThemeId();
157 : }
158 : };
159 : }
160 :
161 : /*static*/ const vcl::IconThemeInfo&
162 0 : IconThemeInfo::FindIconThemeById(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
163 : {
164 : std::vector<vcl::IconThemeInfo>::const_iterator it = std::find_if(themes.begin(), themes.end(),
165 0 : SameTheme(themeId));
166 0 : if (it == themes.end())
167 : {
168 0 : throw std::runtime_error("Could not find theme id in theme vector.");
169 : }
170 0 : return *it;
171 : }
172 :
173 : /*static*/ bool
174 1 : IconThemeInfo::IconThemeIsInVector(const std::vector<vcl::IconThemeInfo>& themes, const OUString& themeId)
175 : {
176 1 : return std::any_of(themes.begin(), themes.end(), SameTheme(themeId));
177 : }
178 :
179 801 : } // end namespace vcl
180 :
181 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|