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