1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <vcl/IconThemeSelector.hxx>
12 #include <vcl/IconThemeScanner.hxx>
13 #include <vcl/IconThemeInfo.hxx>
19 /*static*/ const OUStringLiteral
IconThemeSelector::FALLBACK_ICON_THEME_ID("tango");
24 public std::unary_function
<const vcl::IconThemeInfo
&, bool>
27 const OUString
& m_rThemeId
;
29 explicit SameTheme(const OUString
&rThemeId
) : m_rThemeId(rThemeId
) {}
30 bool operator()(const vcl::IconThemeInfo
&rInfo
)
32 return m_rThemeId
== rInfo
.GetThemeId();
36 bool icon_theme_is_in_installed_themes(const OUString
& theme
,
37 const std::vector
<IconThemeInfo
>& installedThemes
)
39 return std::any_of(installedThemes
.begin(), installedThemes
.end(),
43 } // end anonymous namespace
45 IconThemeSelector::IconThemeSelector()
46 : mUseHighContrastTheme(false)
47 , mPreferDarkIconTheme(false)
52 IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString
& desktopEnvironment
)
55 if ( desktopEnvironment
.equalsIgnoreAsciiCase("tde") ||
56 desktopEnvironment
.equalsIgnoreAsciiCase("kde") ) {
59 else if ( desktopEnvironment
.equalsIgnoreAsciiCase("kde4") ) {
62 else if ( desktopEnvironment
.equalsIgnoreAsciiCase("kde5") ) {
65 else if ( desktopEnvironment
.equalsIgnoreAsciiCase("MacOSX") ) {
68 else if ( desktopEnvironment
.equalsIgnoreAsciiCase("unity") ) {
72 r
= FALLBACK_ICON_THEME_ID
;
78 IconThemeSelector::SelectIconThemeForDesktopEnvironment(
79 const std::vector
<IconThemeInfo
>& installedThemes
,
80 const OUString
& desktopEnvironment
) const
82 if (!mPreferredIconTheme
.isEmpty()) {
83 if (icon_theme_is_in_installed_themes(mPreferredIconTheme
, installedThemes
)) {
84 return mPreferredIconTheme
;
86 //if a dark variant is preferred, and we didn't have an exact match, then try our one and only dark theme
87 if (mPreferDarkIconTheme
&& icon_theme_is_in_installed_themes("breeze_dark", installedThemes
)) {
88 return OUString("breeze_dark");
92 OUString themeForDesktop
= GetIconThemeForDesktopEnvironment(desktopEnvironment
);
93 if (icon_theme_is_in_installed_themes(themeForDesktop
, installedThemes
)) {
94 return themeForDesktop
;
97 return ReturnFallback(installedThemes
);
101 IconThemeSelector::SelectIconTheme(
102 const std::vector
<IconThemeInfo
>& installedThemes
,
103 const OUString
& theme
) const
105 if (mUseHighContrastTheme
) {
106 if (icon_theme_is_in_installed_themes(IconThemeInfo::HIGH_CONTRAST_ID
, installedThemes
)) {
107 return IconThemeInfo::HIGH_CONTRAST_ID
;
111 if (icon_theme_is_in_installed_themes(theme
, installedThemes
)) {
115 return ReturnFallback(installedThemes
);
119 IconThemeSelector::SetUseHighContrastTheme(bool v
)
121 mUseHighContrastTheme
= v
;
125 IconThemeSelector::SetPreferredIconTheme(const OUString
& theme
, bool bDarkIconTheme
)
127 mPreferredIconTheme
= theme
;
128 mPreferDarkIconTheme
= bDarkIconTheme
;
132 IconThemeSelector::operator==(const vcl::IconThemeSelector
& other
) const
134 if (this == &other
) {
137 if (mPreferredIconTheme
!= other
.mPreferredIconTheme
) {
140 if (mPreferDarkIconTheme
!= other
.mPreferDarkIconTheme
) {
143 if (mUseHighContrastTheme
!= other
.mUseHighContrastTheme
) {
150 IconThemeSelector::operator!=(const vcl::IconThemeSelector
& other
) const
152 return !((*this) == other
);
156 IconThemeSelector::ReturnFallback(const std::vector
<IconThemeInfo
>& installedThemes
)
158 if (!installedThemes
.empty()) {
159 return installedThemes
.front().GetThemeId();
162 return FALLBACK_ICON_THEME_ID
;
166 } /* namespace vcl */
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */