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