LibreOffice Module vcl (master) 1
IconThemeScanner.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 <sal/config.h>
11#include <sal/log.hxx>
12
13#include <deque>
14
15#include <IconThemeScanner.hxx>
16
17#include <osl/file.hxx>
20#include <vcl/IconThemeInfo.hxx>
21#include <o3tl/string_view.hxx>
22
23namespace vcl {
24
25namespace {
26
27// set the status of a file. Returns false if the status could not be determined.
28bool set_file_status(osl::FileStatus& status, const OUString& file)
29{
30 osl::DirectoryItem dirItem;
31 osl::FileBase::RC retvalGet = osl::DirectoryItem::get(file, dirItem);
32 if (retvalGet != osl::FileBase::E_None) {
33 SAL_WARN("vcl.app", "Could not determine status for file '" << file << "'.");
34 return false;
35 }
36 osl::FileBase::RC retvalStatus = dirItem.getFileStatus(status);
37 if (retvalStatus != osl::FileBase::E_None) {
38 SAL_WARN("vcl.app", "Could not determine status for file '" << file << "'.");
39 return false;
40 }
41 return true;
42}
43
44OUString convert_to_absolute_path(const OUString& path)
45{
47 osl::FileBase::RC rc = resolver.fetchFileStatus(path);
48 if (rc != osl::FileBase::E_None) {
49 SAL_WARN("vcl.app", "Could not resolve path '" << path << "' to search for icon themes.");
50 if (rc == osl::FileBase::E_MULTIHOP)
51 {
52 throw std::runtime_error("Provided a recursive symlink to an icon theme directory that could not be resolved.");
53 }
54 }
55 return resolver.m_aStatus.getFileURL();
56}
57
58}
59
61{}
62
63void IconThemeScanner::ScanDirectoryForIconThemes(std::u16string_view paths)
64{
65 mFoundIconThemes.clear();
66
67 std::deque<OUString> aPaths;
68
69 sal_Int32 nIndex = 0;
70 do
71 {
72 aPaths.push_front(OUString(o3tl::getToken(paths, 0, ';', nIndex)));
73 }
74 while (nIndex >= 0);
75
76 for (const auto& path : aPaths)
77 {
78 osl::FileStatus fileStatus(osl_FileStatus_Mask_Type);
79 bool couldSetFileStatus = set_file_status(fileStatus, path);
80 if (!couldSetFileStatus) {
81 continue;
82 }
83
84 if (!fileStatus.isDirectory()) {
85 SAL_INFO("vcl.app", "Cannot search for icon themes in '"<< path << "'. It is not a directory.");
86 continue;
87 }
88
89 std::vector<OUString> iconThemePaths = ReadIconThemesFromPath(path);
90 if (iconThemePaths.empty()) {
91 SAL_WARN("vcl.app", "Could not find any icon themes in the provided directory ('" <<path<<"'.");
92 continue;
93 }
94 for (auto const& iconThemePath : iconThemePaths)
95 {
96 AddIconThemeByPath(iconThemePath);
97 }
98 }
99}
100
101bool
103{
105 return false;
106 }
107 SAL_INFO("vcl.app", "Found a file that seems to be an icon theme: '" << url << "'" );
108 IconThemeInfo newTheme(url);
109 mFoundIconThemes.push_back(newTheme);
110 SAL_INFO("vcl.app", "Adding the file as '" << newTheme.GetDisplayName() <<
111 "' with id '" << newTheme.GetThemeId() << "'.");
112 return true;
113}
114
115/*static*/ std::vector<OUString>
117{
118 std::vector<OUString> found;
119 SAL_INFO("vcl.app", "Scanning directory '" << dir << " for icon themes.");
120
121 osl::Directory dirToScan(dir);
122 osl::FileBase::RC retvalOpen = dirToScan.open();
123 if (retvalOpen != osl::FileBase::E_None) {
124 return found;
125 }
126
127 osl::DirectoryItem directoryItem;
128 while (dirToScan.getNextItem(directoryItem) == osl::FileBase::E_None) {
129 osl::FileStatus status(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);
130 osl::FileBase::RC retvalStatus = directoryItem.getFileStatus(status);
131 if (retvalStatus != osl::FileBase::E_None) {
132 continue;
133 }
134
135 OUString filename = convert_to_absolute_path(status.getFileURL());
136 if (!FileIsValidIconTheme(filename)) {
137 continue;
138 }
139 found.push_back(filename);
140 }
141 return found;
142}
143
144/*static*/ bool
146{
147 // check whether we can construct an IconThemeInfo from it
148 if (!IconThemeInfo::UrlCanBeParsed(filename)) {
149 SAL_INFO("vcl.app", "File '" << filename << "' does not seem to be an icon theme.");
150 return false;
151 }
152
153 osl::FileStatus fileStatus(osl_FileStatus_Mask_Type);
154 bool couldSetFileStatus = set_file_status(fileStatus, filename);
155 if (!couldSetFileStatus) {
156 return false;
157 }
158
159 if (!fileStatus.isRegular()) {
160 return false;
161 }
162 return true;
163}
164
165bool
166IconThemeScanner::IconThemeIsInstalled(const OUString& themeId) const
167{
169}
170
171/*static*/ std::shared_ptr<IconThemeScanner>
172IconThemeScanner::Create(std::u16string_view path)
173{
174 std::shared_ptr<IconThemeScanner> retval(new IconThemeScanner);
175 retval->ScanDirectoryForIconThemes(path);
176 return retval;
177}
178
179/*static*/ OUString
181{
182 SvtPathOptions aPathOptions;
183 return aPathOptions.GetIconsetPath();
184}
185
186namespace
187{
188 class SameTheme
189 {
190 private:
191 const OUString& m_rThemeId;
192 public:
193 explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
194 bool operator()(const vcl::IconThemeInfo &rInfo)
195 {
196 return m_rThemeId == rInfo.GetThemeId();
197 }
198 };
199}
200
203{
204 std::vector<IconThemeInfo>::iterator info = std::find_if(mFoundIconThemes.begin(), mFoundIconThemes.end(),
205 SameTheme(themeId));
206 if (info == mFoundIconThemes.end()) {
207 SAL_WARN("vcl.app", "Requested information for icon theme with id '" << themeId
208 << "' which does not exist.");
209 throw std::runtime_error("Requested information on not-installed icon theme");
210 }
211 return *info;
212}
213
214} // end namespace vcl
215
216/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & m_rThemeId
const OUString & GetIconsetPath() const
This class provides information about an icon theme.
static bool UrlCanBeParsed(std::u16string_view url)
Check whether an IconThemeInfo can be constructed from a URL.
const OUString & GetThemeId() const
const OUString & GetDisplayName() const
static bool IconThemeIsInVector(const std::vector< vcl::IconThemeInfo > &themes, const OUString &themeId)
Check whether a theme with a specified id is in a vector of IconThemeInfo.
This class scans a folder for icon themes and provides the results.
const IconThemeInfo & GetIconThemeInfo(const OUString &themeId)
Get the IconThemeInfo for a theme.
static OUString GetStandardIconThemePath()
This method will return the standard path where icon themes are located.
bool AddIconThemeByPath(const OUString &path)
Adds the provided icon theme by path.
static std::vector< OUString > ReadIconThemesFromPath(const OUString &dir)
Scans the provided directory for icon themes.
std::vector< IconThemeInfo > mFoundIconThemes
bool IconThemeIsInstalled(const OUString &themeId) const
Checks whether the theme with the provided name has been found in the scanned directory.
void ScanDirectoryForIconThemes(std::u16string_view path)
Scan a directory for icon themes.
static bool FileIsValidIconTheme(const OUString &)
Check whether a single file is valid.
static std::shared_ptr< IconThemeScanner > Create(std::u16string_view path)
Factory method to create the object.
sal_Int32 nIndex
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
resolver
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)