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