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 
19 namespace vcl {
20 
21 namespace {
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 
35 bool 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
51 IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString& desktopEnvironment)
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 
80 OUString
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)) {
87  return mPreferredIconTheme;
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 
103 OUString
105  const std::vector<IconThemeInfo>& installedThemes,
106  const OUString& theme) const
107 {
108  if (mUseHighContrastTheme) {
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 
121 void
123 {
125 }
126 
127 void
128 IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
129 {
130  // lower case theme name, and (tdf#120175) replace - with _
131  // see icon-themes/README
132  mPreferredIconTheme = theme.toAsciiLowerCase().replace('-','_');
133  mPreferDarkIconTheme = bDarkIconTheme;
134 }
135 
136 bool
138 {
139  if (this == &other) {
140  return true;
141  }
143  return false;
144  }
146  return false;
147  }
149  return false;
150  }
151  return true;
152 }
153 
154 bool
156 {
157  return !(*this == other);
158 }
159 
160 /*static*/ OUString
161 IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
162 {
163  if (!installedThemes.empty()) {
164  return installedThemes.front().GetThemeId();
165  }
166  else {
167  return FALLBACK_ICON_THEME_ID;
168  }
169 }
170 
171 } /* namespace vcl */
172 
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class provides information about an icon theme.
bool operator!=(const vcl::IconThemeSelector &) const
static OUString GetIconThemeForDesktopEnvironment(const OUString &desktopEnvironment)
const OUString & GetThemeId() const
This class helps to choose an icon theme from a list of installed themes.
static constexpr OUStringLiteral FALLBACK_ICON_THEME_ID
The name of the icon theme which is used as fallback.
static constexpr OUStringLiteral HIGH_CONTRAST_ID
The name of the icon theme to use for high contrast mode.
bool operator==(const vcl::IconThemeSelector &) const
static OUString ReturnFallback(const std::vector< IconThemeInfo > &installedThemes)
Return the first element of the themes, or the fallback if the vector is empty.
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...
void SetPreferredIconTheme(const OUString &, bool bDarkIconTheme)
OUString SelectIconTheme(const std::vector< IconThemeInfo > &installedThemes, const OUString &theme) const
Select an icon theme from the list of installed themes.
const OUString & m_rThemeId
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo