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 
13 
14 #include <vcl/IconThemeInfo.hxx>
15 #include <config_mpl.h>
16 
17 #include <algorithm>
18 
19 namespace vcl {
20 
21 /*static*/ const OUStringLiteral IconThemeSelector::FALLBACK_ICON_THEME_ID("tango");
22 
23 namespace {
24 
25  class SameTheme
26  {
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)
32  {
33  return m_rThemeId == rInfo.GetThemeId();
34  }
35  };
36 
37 bool icon_theme_is_in_installed_themes(const OUString& theme,
38  const std::vector<IconThemeInfo>& installedThemes)
39 {
40  return std::any_of(installedThemes.begin(), installedThemes.end(),
41  SameTheme(theme));
42 }
43 
44 } // end anonymous namespace
45 
47  : mUseHighContrastTheme(false)
48  , mPreferDarkIconTheme(false)
49 {
50 }
51 
52 /*static*/ OUString
53 IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString& desktopEnvironment)
54 {
56  return "colibre";
57 
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";
66  }
67  else if ( desktopEnvironment.equalsIgnoreAsciiCase("macosx") ) {
68 #if MPL_HAVE_SUBSET
69  r = "tango";
70 #else
71  r = "breeze";
72 #endif
73  }
74  else if ( desktopEnvironment.equalsIgnoreAsciiCase("gnome") ||
75  desktopEnvironment.equalsIgnoreAsciiCase("mate") ||
76  desktopEnvironment.equalsIgnoreAsciiCase("unity") ) {
77  r = "elementary";
78  } else
79  {
81  }
82  return r;
83 #endif // _WIN32
84 }
85 
86 OUString
88  const std::vector<IconThemeInfo>& installedThemes,
89  const OUString& desktopEnvironment) const
90 {
91  if (!mPreferredIconTheme.isEmpty()) {
92  if (icon_theme_is_in_installed_themes(mPreferredIconTheme, installedThemes)) {
93  return mPreferredIconTheme;
94  }
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";
98  }
99  }
100 
101  OUString themeForDesktop = GetIconThemeForDesktopEnvironment(desktopEnvironment);
102  if (icon_theme_is_in_installed_themes(themeForDesktop, installedThemes)) {
103  return themeForDesktop;
104  }
105 
106  return ReturnFallback(installedThemes);
107 }
108 
109 OUString
111  const std::vector<IconThemeInfo>& installedThemes,
112  const OUString& theme) const
113 {
114  if (mUseHighContrastTheme) {
115  if (icon_theme_is_in_installed_themes(IconThemeInfo::HIGH_CONTRAST_ID, installedThemes)) {
117  }
118  }
119 
120  if (icon_theme_is_in_installed_themes(theme, installedThemes)) {
121  return theme;
122  }
123 
124  return ReturnFallback(installedThemes);
125 }
126 
127 void
129 {
131 }
132 
133 void
134 IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
135 {
136  // lower case theme name, and (tdf#120175) replace - with _
137  // see icon-themes/README
138  mPreferredIconTheme = theme.toAsciiLowerCase().replace('-','_');
139  mPreferDarkIconTheme = bDarkIconTheme;
140 }
141 
142 bool
144 {
145  if (this == &other) {
146  return true;
147  }
149  return false;
150  }
152  return false;
153  }
155  return false;
156  }
157  return true;
158 }
159 
160 bool
162 {
163  return !(*this == other);
164 }
165 
166 /*static*/ OUString
167 IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
168 {
169  if (!installedThemes.empty()) {
170  return installedThemes.front().GetThemeId();
171  }
172  else {
173  return FALLBACK_ICON_THEME_ID;
174  }
175 }
176 
177 } /* namespace vcl */
178 
179 /* 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 const OUStringLiteral HIGH_CONTRAST_ID
The name of the icon theme to use for high contrast mode.
bool operator==(const vcl::IconThemeSelector &) const
static const 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.
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