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/IconThemeSelector.hxx>
11 :
12 : #include <vcl/IconThemeScanner.hxx>
13 : #include <vcl/IconThemeInfo.hxx>
14 :
15 : #include <algorithm>
16 :
17 : namespace vcl {
18 :
19 : /*static*/ const OUString
20 267 : IconThemeSelector::FALLBACK_ICON_THEME_ID("tango");
21 :
22 : namespace {
23 :
24 : class SameTheme :
25 : public std::unary_function<const vcl::IconThemeInfo &, bool>
26 : {
27 : private:
28 : const OUString& m_rThemeId;
29 : public:
30 175994 : explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
31 352066 : bool operator()(const vcl::IconThemeInfo &rInfo)
32 : {
33 352066 : return m_rThemeId == rInfo.GetThemeId();
34 : }
35 : };
36 :
37 175994 : bool icon_theme_is_in_installed_themes(const OUString& theme,
38 : const std::vector<IconThemeInfo>& installedThemes)
39 : {
40 : return std::any_of(installedThemes.begin(), installedThemes.end(),
41 175994 : SameTheme(theme));
42 : }
43 :
44 : } // end anonymous namespace
45 :
46 434 : IconThemeSelector::IconThemeSelector()
47 434 : : mUseHighContrastTheme(false)
48 : {
49 434 : }
50 :
51 : /*static*/ OUString
52 470 : IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString& desktopEnvironment)
53 : {
54 470 : OUString r;
55 940 : if ( desktopEnvironment.equalsIgnoreAsciiCase("tde") ||
56 470 : desktopEnvironment.equalsIgnoreAsciiCase("kde") ) {
57 0 : r = "crystal";
58 : }
59 470 : else if ( desktopEnvironment.equalsIgnoreAsciiCase("kde4") ) {
60 1 : r = "oxygen";
61 : }
62 469 : else if ( desktopEnvironment.equalsIgnoreAsciiCase("kde5") ) {
63 0 : r = "breeze";
64 : }
65 469 : else if ( desktopEnvironment.equalsIgnoreAsciiCase("MacOSX") ) {
66 0 : r = "breeze";
67 : }
68 : else {
69 469 : r = FALLBACK_ICON_THEME_ID;
70 : }
71 470 : return r;
72 : }
73 :
74 : OUString
75 471 : IconThemeSelector::SelectIconThemeForDesktopEnvironment(
76 : const std::vector<IconThemeInfo>& installedThemes,
77 : const OUString& desktopEnvironment) const
78 : {
79 471 : if (!mPreferredIconTheme.isEmpty()) {
80 17 : if (icon_theme_is_in_installed_themes(mPreferredIconTheme, installedThemes)) {
81 1 : return mPreferredIconTheme;
82 : }
83 : }
84 :
85 470 : OUString themeForDesktop = GetIconThemeForDesktopEnvironment(desktopEnvironment);
86 470 : if (icon_theme_is_in_installed_themes(themeForDesktop, installedThemes)) {
87 470 : return themeForDesktop;
88 : }
89 :
90 0 : return ReturnFallback(installedThemes);
91 : }
92 :
93 : OUString
94 175507 : IconThemeSelector::SelectIconTheme(
95 : const std::vector<IconThemeInfo>& installedThemes,
96 : const OUString& theme) const
97 : {
98 175507 : if (mUseHighContrastTheme) {
99 1 : if (icon_theme_is_in_installed_themes(IconThemeInfo::HIGH_CONTRAST_ID, installedThemes)) {
100 1 : return IconThemeInfo::HIGH_CONTRAST_ID;
101 : }
102 : }
103 :
104 175506 : if (icon_theme_is_in_installed_themes(theme, installedThemes)) {
105 175504 : return theme;
106 : }
107 :
108 2 : return ReturnFallback(installedThemes);
109 : }
110 :
111 : void
112 5 : IconThemeSelector::SetUseHighContrastTheme(bool v)
113 : {
114 5 : mUseHighContrastTheme = v;
115 5 : }
116 :
117 : void
118 14 : IconThemeSelector::SetPreferredIconTheme(const OUString& theme)
119 : {
120 14 : mPreferredIconTheme = theme;
121 14 : }
122 :
123 : bool
124 50118 : IconThemeSelector::operator==(const vcl::IconThemeSelector& other) const
125 : {
126 50118 : if (this == &other) {
127 0 : return true;
128 : }
129 50118 : if (mPreferredIconTheme != other.mPreferredIconTheme) {
130 1 : return false;
131 : }
132 50117 : if (mUseHighContrastTheme != other.mUseHighContrastTheme) {
133 1 : return false;
134 : }
135 50116 : return true;
136 : }
137 :
138 : bool
139 50116 : IconThemeSelector::operator!=(const vcl::IconThemeSelector& other) const
140 : {
141 50116 : return !((*this) == other);
142 : }
143 :
144 : /*static*/ OUString
145 2 : IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
146 : {
147 2 : if (!installedThemes.empty()) {
148 1 : return installedThemes.front().GetThemeId();
149 : }
150 : else {
151 1 : return FALLBACK_ICON_THEME_ID;
152 : }
153 : }
154 :
155 801 : } /* namespace vcl */
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|