Line data Source code
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 <vcl/IconThemeScanner.hxx>
11 :
12 : #include <config_folders.h>
13 : #include <osl/file.hxx>
14 : #include <rtl/bootstrap.hxx>
15 : #include <salhelper/linkhelper.hxx>
16 :
17 : #include <vcl/svapp.hxx>
18 : #include <vcl/IconThemeInfo.hxx>
19 :
20 : namespace vcl {
21 :
22 : namespace {
23 :
24 : // set the status of a file. Returns false if the status could not be determined.
25 5416 : bool set_file_status(osl::FileStatus& status, const OUString& file)
26 : {
27 5416 : osl::DirectoryItem dirItem;
28 5416 : osl::FileBase::RC retvalGet = osl::DirectoryItem::get(file, dirItem);
29 5416 : if (retvalGet != osl::FileBase::E_None) {
30 : SAL_WARN("vcl.app", "Could not determine status for file '" << file << "'.");
31 0 : return false;
32 : }
33 5416 : osl::FileBase::RC retvalStatus = dirItem.getFileStatus(status);
34 5416 : if (retvalStatus != osl::FileBase::E_None) {
35 : SAL_WARN("vcl.app", "Could not determine status for file '" << file << "'.");
36 0 : return false;
37 : }
38 5416 : return true;
39 : }
40 :
41 6770 : OUString convert_to_absolute_path(const OUString& path)
42 : {
43 6770 : salhelper::LinkResolver resolver(0);
44 6770 : osl::FileBase::RC rc = resolver.fetchFileStatus(path);
45 6770 : if (rc != osl::FileBase::E_None) {
46 : SAL_WARN("vcl.app", "Could not resolve path '" << path << "' to search for icon themes.");
47 0 : if (rc == osl::FileBase::E_MULTIHOP)
48 : {
49 0 : throw std::runtime_error("Provided a recursive symlink to a icon theme directory that could not be resolved.");
50 : }
51 : }
52 6770 : return resolver.m_aStatus.getFileURL();
53 : }
54 :
55 : }
56 :
57 683 : IconThemeScanner::IconThemeScanner()
58 683 : {;}
59 :
60 : bool
61 677 : IconThemeScanner::ScanDirectoryForIconThemes(const OUString& path)
62 : {
63 677 : osl::FileStatus fileStatus(osl_FileStatus_Mask_Type);
64 677 : bool couldSetFileStatus = set_file_status(fileStatus, path);
65 677 : if (!couldSetFileStatus) {
66 0 : return false;
67 : }
68 :
69 677 : if (!fileStatus.isDirectory()) {
70 : SAL_INFO("vcl.app", "Cannot search for icon themes in '"<< path << "'. It is not a directory.");
71 0 : return false;
72 : }
73 :
74 1354 : std::vector<OUString> iconThemePaths = ReadIconThemesFromPath(path);
75 677 : if (iconThemePaths.empty()) {
76 : SAL_WARN("vcl.app", "Could not find any icon themes in the provided directory ('" <<path<<"'.");
77 0 : return false;
78 : }
79 677 : mFoundIconThemes.clear();
80 5416 : for (std::vector<OUString>::iterator aI = iconThemePaths.begin(); aI != iconThemePaths.end(); ++aI)
81 : {
82 4739 : AddIconThemeByPath(*aI);
83 : }
84 1354 : return true;
85 : }
86 :
87 : bool
88 4745 : IconThemeScanner::AddIconThemeByPath(const OUString &url)
89 : {
90 4745 : if (!IconThemeInfo::UrlCanBeParsed(url)) {
91 0 : return false;
92 : }
93 : SAL_INFO("vcl.app", "Found a file that seems to be an icon theme: '" << url << "'" );
94 4745 : IconThemeInfo newTheme(url);
95 4745 : mFoundIconThemes.push_back(newTheme);
96 : SAL_INFO("vcl.app", "Adding the file as '" << newTheme.GetDisplayName() <<
97 : "' with id '" << newTheme.GetThemeId() << "'.");
98 4745 : return true;
99 : }
100 :
101 : /*static*/ std::vector<OUString>
102 677 : IconThemeScanner::ReadIconThemesFromPath(const OUString& dir)
103 : {
104 677 : std::vector<OUString> found;
105 : SAL_INFO("vcl.app", "Scanning directory '" << dir << " for icon themes.");
106 :
107 1354 : osl::Directory dirToScan(dir);
108 677 : osl::FileBase::RC retvalOpen = dirToScan.open();
109 677 : if (retvalOpen != osl::FileBase::E_None) {
110 0 : return found;
111 : }
112 :
113 1354 : osl::DirectoryItem directoryItem;
114 8124 : while (dirToScan.getNextItem(directoryItem) == osl::FileBase::E_None) {
115 6770 : osl::FileStatus status(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);
116 6770 : osl::FileBase::RC retvalStatus = directoryItem.getFileStatus(status);
117 6770 : if (retvalStatus != osl::FileBase::E_None) {
118 0 : continue;
119 : }
120 :
121 11509 : OUString filename = convert_to_absolute_path(status.getFileURL());
122 6770 : if (!FileIsValidIconTheme(filename)) {
123 2031 : continue;
124 : }
125 4739 : found.push_back(filename);
126 4739 : }
127 677 : return found;
128 : }
129 :
130 : /*static*/ bool
131 6770 : IconThemeScanner::FileIsValidIconTheme(const OUString& filename)
132 : {
133 : // check whether we can construct a IconThemeInfo from it
134 6770 : if (!IconThemeInfo::UrlCanBeParsed(filename)) {
135 : SAL_INFO("vcl.app", "File '" << filename << "' does not seem to be an icon theme.");
136 2031 : return false;
137 : }
138 :
139 4739 : osl::FileStatus fileStatus(osl_FileStatus_Mask_Type);
140 4739 : bool couldSetFileStatus = set_file_status(fileStatus, filename);
141 4739 : if (!couldSetFileStatus) {
142 0 : return false;
143 : }
144 :
145 4739 : if (!fileStatus.isRegular()) {
146 0 : return false;
147 : }
148 4739 : return true;
149 : }
150 :
151 : bool
152 2 : IconThemeScanner::IconThemeIsInstalled(const OUString& themeId) const
153 : {
154 2 : return IconThemeInfo::IconThemeIsInVector(mFoundIconThemes, themeId);
155 : }
156 :
157 : /*static*/ boost::shared_ptr<IconThemeScanner>
158 677 : IconThemeScanner::Create(const OUString &path)
159 : {
160 677 : boost::shared_ptr<IconThemeScanner> retval(new IconThemeScanner);
161 677 : retval->ScanDirectoryForIconThemes(path);
162 677 : return retval;
163 : }
164 :
165 : /*static*/ OUString
166 677 : IconThemeScanner::GetStandardIconThemePath()
167 : {
168 677 : OUString url( "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/config/" );
169 677 : rtl::Bootstrap::expandMacros(url);
170 677 : return url;
171 : }
172 :
173 80964 : IconThemeScanner::~IconThemeScanner()
174 80964 : {;}
175 :
176 : namespace
177 : {
178 : class SameTheme :
179 : public std::unary_function<const vcl::IconThemeInfo &, bool>
180 : {
181 : private:
182 : const OUString& m_rThemeId;
183 : public:
184 4 : SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
185 4 : bool operator()(const vcl::IconThemeInfo &rInfo)
186 : {
187 4 : return m_rThemeId == rInfo.GetThemeId();
188 : }
189 : };
190 : }
191 :
192 : const vcl::IconThemeInfo&
193 4 : IconThemeScanner::GetIconThemeInfo(const OUString& themeId)
194 : {
195 : std::vector<IconThemeInfo>::iterator info = std::find_if(mFoundIconThemes.begin(), mFoundIconThemes.end(),
196 4 : SameTheme(themeId));
197 4 : if (info == mFoundIconThemes.end()) {
198 : SAL_WARN("vcl.app", "Requested information for icon theme with id '" << themeId
199 : << "' which does not exist.");
200 2 : throw std::runtime_error("Requested information on not-installed icon theme");
201 : }
202 2 : return *info;
203 : }
204 :
205 1233 : } // end namespace vcl
206 :
207 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|