LibreOffice Module vcl (master) 1
IconThemeSelector.cxx
Go to the documentation of this file.
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 <comphelper/lok.hxx>
11
12#include <IconThemeSelector.hxx>
13
14#include <vcl/IconThemeInfo.hxx>
15#include <config_mpl.h>
16
17#include <algorithm>
18
19namespace vcl {
20
21namespace {
22
23 class SameTheme
24 {
25 private:
26 const OUString& m_rThemeId;
27 public:
28 explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
29 bool operator()(const vcl::IconThemeInfo &rInfo)
30 {
31 return m_rThemeId == rInfo.GetThemeId();
32 }
33 };
34
35bool icon_theme_is_in_installed_themes(const OUString& theme,
36 const std::vector<IconThemeInfo>& installedThemes)
37{
38 return std::any_of(installedThemes.begin(), installedThemes.end(),
39 SameTheme(theme));
40}
41
42} // end anonymous namespace
43
45 : mUseHighContrastTheme(false)
46 , mPreferDarkIconTheme(false)
47{
48}
49
50/*static*/ OUString
52{
54 return "colibre";
55
56#ifdef _WIN32
57 (void)desktopEnvironment;
58 return "colibre";
59#else
60 OUString r;
61 if ( desktopEnvironment.equalsIgnoreAsciiCase("plasma5") ||
62 desktopEnvironment.equalsIgnoreAsciiCase("lxqt") ) {
63 r = "breeze";
64 }
65 else if ( desktopEnvironment.equalsIgnoreAsciiCase("macosx") ) {
66 r = "sukapura";
67 }
68 else if ( desktopEnvironment.equalsIgnoreAsciiCase("gnome") ||
69 desktopEnvironment.equalsIgnoreAsciiCase("mate") ||
70 desktopEnvironment.equalsIgnoreAsciiCase("unity") ) {
71 r = "elementary";
72 } else
73 {
75 }
76 return r;
77#endif // _WIN32
78}
79
80OUString
82 const std::vector<IconThemeInfo>& installedThemes,
83 const OUString& desktopEnvironment) const
84{
85 if (!mPreferredIconTheme.isEmpty()) {
86 if (icon_theme_is_in_installed_themes(mPreferredIconTheme, installedThemes)) {
88 }
89 //if a dark variant is preferred, and we didn't have an exact match, then try our one and only dark theme
90 if (mPreferDarkIconTheme && icon_theme_is_in_installed_themes("breeze_dark", installedThemes)) {
91 return "breeze_dark";
92 }
93 }
94
95 OUString themeForDesktop = GetIconThemeForDesktopEnvironment(desktopEnvironment);
96 if (icon_theme_is_in_installed_themes(themeForDesktop, installedThemes)) {
97 return themeForDesktop;
98 }
99
100 return ReturnFallback(installedThemes);
101}
102
103OUString
105 const std::vector<IconThemeInfo>& installedThemes,
106 const OUString& theme) const
107{
109 if (icon_theme_is_in_installed_themes(IconThemeInfo::HIGH_CONTRAST_ID, installedThemes)) {
111 }
112 }
113
114 if (icon_theme_is_in_installed_themes(theme, installedThemes)) {
115 return theme;
116 }
117
118 return ReturnFallback(installedThemes);
119}
120
121void
123{
125}
126
127bool
128IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
129{
130 // lower case theme name, and (tdf#120175) replace - with _
131 // see icon-themes/README
132 OUString sIconTheme = theme.toAsciiLowerCase().replace('-','_');
133
134 const bool bChanged = mPreferredIconTheme != sIconTheme || mPreferDarkIconTheme != bDarkIconTheme;
135 if (bChanged)
136 {
137 mPreferredIconTheme = sIconTheme;
138 mPreferDarkIconTheme = bDarkIconTheme;
139 }
140 return bChanged;
141}
142
143bool
145{
146 if (this == &other) {
147 return true;
148 }
150 return false;
151 }
153 return false;
154 }
156 return false;
157 }
158 return true;
159}
160
161bool
163{
164 return !(*this == other);
165}
166
167/*static*/ OUString
168IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
169{
170 if (!installedThemes.empty()) {
171 return installedThemes.front().GetThemeId();
172 }
173 else {
175 }
176}
177
178} /* namespace vcl */
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & m_rThemeId
This class provides information about an icon theme.
const OUString & GetThemeId() const
static constexpr OUStringLiteral HIGH_CONTRAST_ID
The name of the icon theme to use for high contrast mode.
This class helps to choose an icon theme from a list of installed themes.
bool SetPreferredIconTheme(const OUString &, bool bDarkIconTheme)
Returns true if the PreferredIconTheme was changed.
bool operator==(const vcl::IconThemeSelector &) const
bool operator!=(const vcl::IconThemeSelector &) const
static constexpr OUStringLiteral FALLBACK_ICON_THEME_ID
The name of the icon theme which is used as fallback.
static OUString ReturnFallback(const std::vector< IconThemeInfo > &installedThemes)
Return the first element of the themes, or the fallback if the vector is empty.
static OUString GetIconThemeForDesktopEnvironment(const OUString &desktopEnvironment)
OUString SelectIconTheme(const std::vector< IconThemeInfo > &installedThemes, const OUString &theme) const
Select an icon theme from the list of installed themes.
OUString SelectIconThemeForDesktopEnvironment(const std::vector< IconThemeInfo > &installedThemes, const OUString &desktopEnvironment) const
Select the standard icon theme for a desktop environment from a list of installed themes.
float v