Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / app / IconThemeSelector.cxx
blob225414fcc8b797374d6ac38db552e4b9f60568d1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <comphelper/lok.hxx>
12 #include <vcl/IconThemeSelector.hxx>
14 #include <vcl/IconThemeInfo.hxx>
15 #include <config_mpl.h>
17 #include <algorithm>
19 namespace vcl {
21 /*static*/ const OUStringLiteral IconThemeSelector::FALLBACK_ICON_THEME_ID("tango");
23 namespace {
25 class SameTheme
27 private:
28 const OUString& m_rThemeId;
29 public:
30 explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
31 bool operator()(const vcl::IconThemeInfo &rInfo)
33 return m_rThemeId == rInfo.GetThemeId();
37 bool icon_theme_is_in_installed_themes(const OUString& theme,
38 const std::vector<IconThemeInfo>& installedThemes)
40 return std::any_of(installedThemes.begin(), installedThemes.end(),
41 SameTheme(theme));
44 } // end anonymous namespace
46 IconThemeSelector::IconThemeSelector()
47 : mUseHighContrastTheme(false)
48 , mPreferDarkIconTheme(false)
52 /*static*/ OUString
53 IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString& desktopEnvironment)
55 if (comphelper::LibreOfficeKit::isActive())
56 return "colibre";
58 #ifdef _WIN32
59 (void)desktopEnvironment;
60 return "colibre";
61 #else
62 OUString r;
63 if ( desktopEnvironment.equalsIgnoreAsciiCase("plasma5") ||
64 desktopEnvironment.equalsIgnoreAsciiCase("lxqt") ) {
65 r = "breeze";
67 else if ( desktopEnvironment.equalsIgnoreAsciiCase("macosx") ) {
68 #if MPL_HAVE_SUBSET
69 r = "tango";
70 #else
71 r = "breeze";
72 #endif
74 else if ( desktopEnvironment.equalsIgnoreAsciiCase("gnome") ||
75 desktopEnvironment.equalsIgnoreAsciiCase("mate") ||
76 desktopEnvironment.equalsIgnoreAsciiCase("unity") ) {
77 r = "elementary";
78 } else
80 r = FALLBACK_ICON_THEME_ID;
82 return r;
83 #endif // _WIN32
86 OUString
87 IconThemeSelector::SelectIconThemeForDesktopEnvironment(
88 const std::vector<IconThemeInfo>& installedThemes,
89 const OUString& desktopEnvironment) const
91 if (!mPreferredIconTheme.isEmpty()) {
92 if (icon_theme_is_in_installed_themes(mPreferredIconTheme, installedThemes)) {
93 return mPreferredIconTheme;
95 //if a dark variant is preferred, and we didn't have an exact match, then try our one and only dark theme
96 if (mPreferDarkIconTheme && icon_theme_is_in_installed_themes("breeze_dark", installedThemes)) {
97 return "breeze_dark";
101 OUString themeForDesktop = GetIconThemeForDesktopEnvironment(desktopEnvironment);
102 if (icon_theme_is_in_installed_themes(themeForDesktop, installedThemes)) {
103 return themeForDesktop;
106 return ReturnFallback(installedThemes);
109 OUString
110 IconThemeSelector::SelectIconTheme(
111 const std::vector<IconThemeInfo>& installedThemes,
112 const OUString& theme) const
114 if (mUseHighContrastTheme) {
115 if (icon_theme_is_in_installed_themes(IconThemeInfo::HIGH_CONTRAST_ID, installedThemes)) {
116 return IconThemeInfo::HIGH_CONTRAST_ID;
120 if (icon_theme_is_in_installed_themes(theme, installedThemes)) {
121 return theme;
124 return ReturnFallback(installedThemes);
127 void
128 IconThemeSelector::SetUseHighContrastTheme(bool v)
130 mUseHighContrastTheme = v;
133 void
134 IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
136 // lower case theme name, and (tdf#120175) replace - with _
137 // see icon-themes/README
138 mPreferredIconTheme = theme.toAsciiLowerCase().replace('-','_');
139 mPreferDarkIconTheme = bDarkIconTheme;
142 bool
143 IconThemeSelector::operator==(const vcl::IconThemeSelector& other) const
145 if (this == &other) {
146 return true;
148 if (mPreferredIconTheme != other.mPreferredIconTheme) {
149 return false;
151 if (mPreferDarkIconTheme != other.mPreferDarkIconTheme) {
152 return false;
154 if (mUseHighContrastTheme != other.mUseHighContrastTheme) {
155 return false;
157 return true;
160 bool
161 IconThemeSelector::operator!=(const vcl::IconThemeSelector& other) const
163 return !(*this == other);
166 /*static*/ OUString
167 IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
169 if (!installedThemes.empty()) {
170 return installedThemes.front().GetThemeId();
172 else {
173 return FALLBACK_ICON_THEME_ID;
177 } /* namespace vcl */
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */