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 /*static*/ const OUStringLiteral IconThemeSelector::FALLBACK_ICON_THEME_ID("colibre");
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  r = "sukapura";
69  }
70  else if ( desktopEnvironment.equalsIgnoreAsciiCase("gnome") ||
71  desktopEnvironment.equalsIgnoreAsciiCase("mate") ||
72  desktopEnvironment.equalsIgnoreAsciiCase("unity") ) {
73  r = "elementary";
74  } else
75  {
77  }
78  return r;
79 #endif // _WIN32
80 }
81 
82 OUString
84  const std::vector<IconThemeInfo>& installedThemes,
85  const OUString& desktopEnvironment) const
86 {
87  if (!mPreferredIconTheme.isEmpty()) {
88  if (icon_theme_is_in_installed_themes(mPreferredIconTheme, installedThemes)) {
89  return mPreferredIconTheme;
90  }
91  //if a dark variant is preferred, and we didn't have an exact match, then try our one and only dark theme
92  if (mPreferDarkIconTheme && icon_theme_is_in_installed_themes("breeze_dark", installedThemes)) {
93  return "breeze_dark";
94  }
95  }
96 
97  OUString themeForDesktop = GetIconThemeForDesktopEnvironment(desktopEnvironment);
98  if (icon_theme_is_in_installed_themes(themeForDesktop, installedThemes)) {
99  return themeForDesktop;
100  }
101 
102  return ReturnFallback(installedThemes);
103 }
104 
105 OUString
107  const std::vector<IconThemeInfo>& installedThemes,
108  const OUString& theme) const
109 {
110  if (mUseHighContrastTheme) {
111  if (icon_theme_is_in_installed_themes(IconThemeInfo::HIGH_CONTRAST_ID, installedThemes)) {
113  }
114  }
115 
116  if (icon_theme_is_in_installed_themes(theme, installedThemes)) {
117  return theme;
118  }
119 
120  return ReturnFallback(installedThemes);
121 }
122 
123 void
125 {
127 }
128 
129 void
130 IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
131 {
132  // lower case theme name, and (tdf#120175) replace - with _
133  // see icon-themes/README
134  mPreferredIconTheme = theme.toAsciiLowerCase().replace('-','_');
135  mPreferDarkIconTheme = bDarkIconTheme;
136 }
137 
138 bool
140 {
141  if (this == &other) {
142  return true;
143  }
145  return false;
146  }
148  return false;
149  }
151  return false;
152  }
153  return true;
154 }
155 
156 bool
158 {
159  return !(*this == other);
160 }
161 
162 /*static*/ OUString
163 IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
164 {
165  if (!installedThemes.empty()) {
166  return installedThemes.front().GetThemeId();
167  }
168  else {
169  return FALLBACK_ICON_THEME_ID;
170  }
171 }
172 
173 } /* namespace vcl */
174 
175 /* 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