build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / app / IconThemeSelector.cxx
blob95ee517b6f21c40cc75fa38d61fa0c0ec75e682c
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 <vcl/IconThemeSelector.hxx>
12 #include <vcl/IconThemeScanner.hxx>
13 #include <vcl/IconThemeInfo.hxx>
15 #include <algorithm>
17 namespace vcl {
19 /*static*/ const OUStringLiteral IconThemeSelector::FALLBACK_ICON_THEME_ID("tango");
21 namespace {
23 class SameTheme :
24 public std::unary_function<const vcl::IconThemeInfo &, bool>
26 private:
27 const OUString& m_rThemeId;
28 public:
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(),
40 SameTheme(theme));
43 } // end anonymous namespace
45 IconThemeSelector::IconThemeSelector()
46 : mUseHighContrastTheme(false)
47 , mPreferDarkIconTheme(false)
51 /*static*/ OUString
52 IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString& desktopEnvironment)
54 OUString r;
55 if ( desktopEnvironment.equalsIgnoreAsciiCase("tde") ||
56 desktopEnvironment.equalsIgnoreAsciiCase("kde") ) {
57 r = "crystal";
59 else if ( desktopEnvironment.equalsIgnoreAsciiCase("kde4") ) {
60 r = "oxygen";
62 else if ( desktopEnvironment.equalsIgnoreAsciiCase("kde5") ) {
63 r = "breeze";
65 else if ( desktopEnvironment.equalsIgnoreAsciiCase("MacOSX") ) {
66 r = "breeze";
68 else if ( desktopEnvironment.equalsIgnoreAsciiCase("unity") ) {
69 r = "breeze";
71 else {
72 r = FALLBACK_ICON_THEME_ID;
74 return r;
77 OUString
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);
100 OUString
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)) {
112 return theme;
115 return ReturnFallback(installedThemes);
118 void
119 IconThemeSelector::SetUseHighContrastTheme(bool v)
121 mUseHighContrastTheme = v;
124 void
125 IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
127 mPreferredIconTheme = theme;
128 mPreferDarkIconTheme = bDarkIconTheme;
131 bool
132 IconThemeSelector::operator==(const vcl::IconThemeSelector& other) const
134 if (this == &other) {
135 return true;
137 if (mPreferredIconTheme != other.mPreferredIconTheme) {
138 return false;
140 if (mPreferDarkIconTheme != other.mPreferDarkIconTheme) {
141 return false;
143 if (mUseHighContrastTheme != other.mUseHighContrastTheme) {
144 return false;
146 return true;
149 bool
150 IconThemeSelector::operator!=(const vcl::IconThemeSelector& other) const
152 return !((*this) == other);
155 /*static*/ OUString
156 IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
158 if (!installedThemes.empty()) {
159 return installedThemes.front().GetThemeId();
161 else {
162 return FALLBACK_ICON_THEME_ID;
166 } /* namespace vcl */
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */