| 1 |
/* |
| 2 |
* Copyright © 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License version 3, as published |
| 6 |
* by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but |
| 9 |
* WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
* PURPOSE. See the GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Larry Price <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "application-icon-finder.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include <regex> |
| 23 |
|
| 24 |
#include <lomiri/util/GlibMemory.h> |
| 25 |
|
| 26 |
using namespace lomiri::util; |
| 27 |
|
| 28 |
namespace lomiri |
| 29 |
{ |
| 30 |
namespace app_launch |
| 31 |
{ |
| 32 |
namespace |
| 33 |
{ |
| 34 |
constexpr auto ICONS_DIR = "/icons"; |
| 35 |
constexpr auto ICON_THEMES = {"suru", "Humanity", "Adwaita", "gnome", "hicolor"}; |
| 36 |
constexpr auto THEME_INDEX_FILE = "index.theme"; |
| 37 |
constexpr auto SIZE_PROPERTY = "Size"; |
| 38 |
constexpr auto MAXSIZE_PROPERTY = "MaxSize"; |
| 39 |
constexpr auto THRESHOLD_PROPERTY = "Threshold"; |
| 40 |
constexpr auto FIXED_CONTEXT = "Fixed"; |
| 41 |
constexpr auto SCALABLE_CONTEXT = "Scalable"; |
| 42 |
constexpr auto THRESHOLD_CONTEXT = "Threshold"; |
| 43 |
constexpr auto CONTEXT_PROPERTY = "Context"; |
| 44 |
constexpr auto TYPE_PROPERTY = "Type"; |
| 45 |
constexpr auto DIRECTORIES_PROPERTY = "Directories"; |
| 46 |
constexpr auto ICON_THEME_KEY = "Icon Theme"; |
| 47 |
constexpr auto PIXMAPS_PATH = "/pixmaps/"; |
| 48 |
constexpr auto ICON_TYPES = {".png", ".svg", ".xpm"}; |
| 49 |
constexpr auto METAGUI_PATH = "/meta/gui"; |
| 50 |
|
| 51 |
static const std::regex ICON_SIZE_DIRNAME = std::regex("^(\\d+)x\\1$"); |
| 52 |
static const std::regex SCALABLE_WITH_REGEX = std::regex("^scalable-up-to-(\\d+)$"); |
| 53 |
} // anonymous namespace |
| 54 |
|
| 55 |
IconFinder::IconFinder(std::string basePath) |
| 56 |
: _searchPaths(getSearchPaths(basePath)) |
| 57 |
, _basePath(basePath) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
/** Finds an icon in the search paths that we have for this path */ |
| 62 |
Application::Info::IconPath IconFinder::find(const std::string& iconName) |
| 63 |
{ |
| 64 |
if (iconName[0] == '/') // explicit icon path received |
| 65 |
{ |
| 66 |
return Application::Info::IconPath::from_raw(iconName); |
| 67 |
} |
| 68 |
|
| 69 |
/* Look in each directory slowly decreasing the size until we find |
| 70 |
an icon */ |
| 71 |
auto size = 0; |
| 72 |
std::string iconPath; |
| 73 |
for (const auto& path : _searchPaths) |
| 74 |
{ |
| 75 |
if (path.size > size) |
| 76 |
{ |
| 77 |
auto foundIcon = findExistingIcon(path.path, iconName); |
| 78 |
if (!foundIcon.empty()) |
| 79 |
{ |
| 80 |
size = path.size; |
| 81 |
iconPath = foundIcon; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return Application::Info::IconPath::from_raw(iconPath); |
| 87 |
} |
| 88 |
|
| 89 |
/** Check to see if this is an icon name or an icon filename */ |
| 90 |
bool IconFinder::hasImageExtension(const char* filename) |
| 91 |
{ |
| 92 |
for (const auto& extension : ICON_TYPES) |
| 93 |
{ |
| 94 |
if (g_str_has_suffix(filename, extension)) |
| 95 |
{ |
| 96 |
return true; |
| 97 |
} |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
/** Check in a given path if there is an existing file in it that satifies our name */ |
| 103 |
std::string IconFinder::findExistingIcon(const std::string& path, const std::string& iconName) |
| 104 |
{ |
| 105 |
std::string iconPath; |
| 106 |
|
| 107 |
/* If it already has an extension, only check that one */ |
| 108 |
if (hasImageExtension(iconName.c_str())) |
| 109 |
{ |
| 110 |
auto fullpath = unique_gchar(g_build_filename(path.c_str(), iconName.c_str(), nullptr)); |
| 111 |
if (g_file_test(fullpath.get(), G_FILE_TEST_EXISTS)) |
| 112 |
{ |
| 113 |
iconPath = fullpath.get(); |
| 114 |
} |
| 115 |
return iconPath; |
| 116 |
} |
| 117 |
|
| 118 |
/* Otherwise check all the valid extensions to see if they exist */ |
| 119 |
for (const auto& extension : ICON_TYPES) |
| 120 |
{ |
| 121 |
auto fullpath = unique_gchar(g_build_filename(path.c_str(), (iconName + extension).c_str(), nullptr)); |
| 122 |
if (g_file_test(fullpath.get(), G_FILE_TEST_EXISTS)) |
| 123 |
{ |
| 124 |
iconPath = fullpath.get(); |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
return iconPath; |
| 129 |
} |
| 130 |
|
| 131 |
/** Create a directory item if the directory exists */ |
| 132 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::validDirectories(const std::string& themePath, |
| 133 |
gchar* directory, |
| 134 |
int size) |
| 135 |
{ |
| 136 |
std::list<IconFinder::ThemeSubdirectory> dirs; |
| 137 |
auto globalHicolorTheme = unique_gchar(g_build_filename(themePath.c_str(), directory, nullptr)); |
| 138 |
if (g_file_test(globalHicolorTheme.get(), G_FILE_TEST_EXISTS)) |
| 139 |
{ |
| 140 |
dirs.emplace_back(ThemeSubdirectory{std::string(globalHicolorTheme.get()), size}); |
| 141 |
} |
| 142 |
|
| 143 |
return dirs; |
| 144 |
} |
| 145 |
|
| 146 |
/** Take the data in a directory stanza and turn it into an actual directory */ |
| 147 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::addSubdirectoryByType(std::shared_ptr<GKeyFile> themefile, |
| 148 |
gchar* directory, |
| 149 |
const std::string& themePath) |
| 150 |
{ |
| 151 |
GError* error = nullptr; |
| 152 |
auto gType = unique_gchar(g_key_file_get_string(themefile.get(), directory, TYPE_PROPERTY, &error)); |
| 153 |
if (error != nullptr) |
| 154 |
{ |
| 155 |
g_error_free(error); |
| 156 |
return std::list<ThemeSubdirectory>{}; |
| 157 |
} |
| 158 |
std::string type(gType.get()); |
| 159 |
|
| 160 |
if (type == FIXED_CONTEXT) |
| 161 |
{ |
| 162 |
auto size = g_key_file_get_integer(themefile.get(), directory, SIZE_PROPERTY, &error); |
| 163 |
if (error != nullptr) |
| 164 |
{ |
| 165 |
g_error_free(error); |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
return validDirectories(themePath, directory, size); |
| 170 |
} |
| 171 |
} |
| 172 |
else if (type == SCALABLE_CONTEXT) |
| 173 |
{ |
| 174 |
auto size = g_key_file_get_integer(themefile.get(), directory, MAXSIZE_PROPERTY, &error); |
| 175 |
if (error != nullptr) |
| 176 |
{ |
| 177 |
g_error_free(error); |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
return validDirectories(themePath, directory, size); |
| 182 |
} |
| 183 |
} |
| 184 |
else if (type == THRESHOLD_CONTEXT) |
| 185 |
{ |
| 186 |
auto size = g_key_file_get_integer(themefile.get(), directory, SIZE_PROPERTY, &error); |
| 187 |
if (error != nullptr) |
| 188 |
{ |
| 189 |
g_error_free(error); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
auto threshold = g_key_file_get_integer(themefile.get(), directory, THRESHOLD_PROPERTY, &error); |
| 194 |
if (error != nullptr) |
| 195 |
{ |
| 196 |
threshold = 2; // threshold defaults to 2 |
| 197 |
g_error_free(error); |
| 198 |
} |
| 199 |
return validDirectories(themePath, directory, size + threshold); |
| 200 |
} |
| 201 |
} |
| 202 |
return std::list<ThemeSubdirectory>{}; |
| 203 |
} |
| 204 |
|
| 205 |
/** Parse a theme file's various stanzas for each directory */ |
| 206 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::searchIconPaths(std::shared_ptr<GKeyFile> themefile, |
| 207 |
gchar** directories, |
| 208 |
const std::string& themePath) |
| 209 |
{ |
| 210 |
std::list<ThemeSubdirectory> subdirs; |
| 211 |
for (auto i = 0; directories[i] != nullptr; ++i) |
| 212 |
{ |
| 213 |
GError* error = nullptr; |
| 214 |
auto context = unique_gchar(g_key_file_get_string(themefile.get(), directories[i], CONTEXT_PROPERTY, &error)); |
| 215 |
if (error != nullptr) |
| 216 |
{ |
| 217 |
g_error_free(error); |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
auto newDirs = addSubdirectoryByType(themefile, directories[i], themePath); |
| 222 |
subdirs.splice(subdirs.end(), newDirs); |
| 223 |
} |
| 224 |
return subdirs; |
| 225 |
} |
| 226 |
|
| 227 |
/** Try to get theme subdirectories using .theme file in the given theme path |
| 228 |
if it exists */ |
| 229 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::themeFileSearchPaths(const std::string& themePath) |
| 230 |
{ |
| 231 |
auto themeFilePath = unique_gchar(g_build_filename(themePath.c_str(), THEME_INDEX_FILE, nullptr)); |
| 232 |
GError* error = nullptr; |
| 233 |
auto themefile = share_glib(g_key_file_new()); |
| 234 |
g_key_file_load_from_file(themefile.get(), themeFilePath.get(), G_KEY_FILE_NONE, &error); |
| 235 |
if (error != nullptr) |
| 236 |
{ |
| 237 |
g_debug("Unable to find theme file: %s", themeFilePath.get()); |
| 238 |
g_error_free(error); |
| 239 |
return std::list<ThemeSubdirectory>(); |
| 240 |
} |
| 241 |
|
| 242 |
g_key_file_set_list_separator(themefile.get(), ','); |
| 243 |
auto directories = unique_gcharv( |
| 244 |
g_key_file_get_string_list(themefile.get(), ICON_THEME_KEY, DIRECTORIES_PROPERTY, nullptr, &error)); |
| 245 |
if (error != nullptr) |
| 246 |
{ |
| 247 |
g_debug("Theme file '%s' didn't have any directories", themeFilePath.get()); |
| 248 |
g_error_free(error); |
| 249 |
return std::list<ThemeSubdirectory>(); |
| 250 |
} |
| 251 |
|
| 252 |
auto iconPaths = searchIconPaths(themefile, directories.get(), themePath); |
| 253 |
return iconPaths; |
| 254 |
} |
| 255 |
|
| 256 |
/** Look into a theme directory and see if we can use the subdirectories |
| 257 |
as icon folders. This is a fallback, and is sadly inefficient. */ |
| 258 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::themeDirSearchPaths(const std::string& themeDir) |
| 259 |
{ |
| 260 |
std::list<IconFinder::ThemeSubdirectory> searchPaths; |
| 261 |
GError* error = nullptr; |
| 262 |
auto gdir = unique_glib(g_dir_open(themeDir.c_str(), 0, &error)); |
| 263 |
|
| 264 |
if (error != nullptr) |
| 265 |
{ |
| 266 |
g_debug("Unable to open directory '%s' because: %s", themeDir.c_str(), error->message); |
| 267 |
g_error_free(error); |
| 268 |
return searchPaths; |
| 269 |
} |
| 270 |
|
| 271 |
const gchar* dirname = nullptr; |
| 272 |
while ((dirname = g_dir_read_name(gdir.get())) != nullptr) |
| 273 |
{ |
| 274 |
/* Iterate over size-based directories */ |
| 275 |
auto sizePath = unique_gchar(g_build_filename(themeDir.c_str(), dirname, nullptr)); |
| 276 |
if (g_file_test(sizePath.get(), G_FILE_TEST_IS_DIR)) |
| 277 |
{ |
| 278 |
std::smatch match; |
| 279 |
std::string dirstr(dirname); |
| 280 |
int size; |
| 281 |
if (std::regex_match(dirstr, match, ICON_SIZE_DIRNAME)) |
| 282 |
{ |
| 283 |
size = std::atoi(match[1].str().c_str()); |
| 284 |
} |
| 285 |
else if (g_strcmp0(dirname, "scalable") == 0) |
| 286 |
{ |
| 287 |
/* We don't really know what to do with scalable icons, let's |
| 288 |
call them 256 images */ |
| 289 |
size = 256; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
/* Some directories are "scalable with", meaning that they |
| 294 |
are meant to be used up to a certain size */ |
| 295 |
std::smatch scalablewith; |
| 296 |
if (std::regex_match(dirstr, scalablewith, SCALABLE_WITH_REGEX)) |
| 297 |
{ |
| 298 |
size = std::atoi(scalablewith[1].str().c_str()); |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
/* Otherwise we don't know what to do with this dir, |
| 303 |
ignore for now */ |
| 304 |
continue; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
auto subdir = unique_glib(g_dir_open(sizePath.get(), 0, &error)); |
| 309 |
const gchar* subdirname = nullptr; |
| 310 |
while ((subdirname = g_dir_read_name(subdir.get())) != nullptr) |
| 311 |
{ |
| 312 |
auto fullPath = unique_gchar(g_build_filename(sizePath.get(), subdirname, nullptr)); |
| 313 |
if (g_file_test(fullPath.get(), G_FILE_TEST_IS_DIR)) |
| 314 |
{ |
| 315 |
searchPaths.emplace_back(IconFinder::ThemeSubdirectory{fullPath.get(), size}); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return searchPaths; |
| 322 |
} |
| 323 |
|
| 324 |
/** Gets all search paths from a given theme directory via theme file or |
| 325 |
manually scanning the directory. */ |
| 326 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::iconsFromThemePath(const gchar* themeDir) |
| 327 |
{ |
| 328 |
std::list<IconFinder::ThemeSubdirectory> iconPaths; |
| 329 |
if (g_file_test(themeDir, G_FILE_TEST_IS_DIR)) |
| 330 |
{ |
| 331 |
/* If the directory exists, it could have icons of unknown size */ |
| 332 |
iconPaths.emplace_back(IconFinder::ThemeSubdirectory{themeDir, 1}); |
| 333 |
|
| 334 |
/* Now see if we can get directories from a theme file */ |
| 335 |
auto themeDirs = themeFileSearchPaths(themeDir); |
| 336 |
if (themeDirs.size() > 0) |
| 337 |
{ |
| 338 |
iconPaths.splice(iconPaths.end(), themeDirs); |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
/* If we didn't get from a theme file, we need to manually scan the directories */ |
| 343 |
auto dirPaths = themeDirSearchPaths(themeDir); |
| 344 |
iconPaths.splice(iconPaths.end(), dirPaths); |
| 345 |
} |
| 346 |
} |
| 347 |
return iconPaths; |
| 348 |
} |
| 349 |
|
| 350 |
/** Gets search paths based on common icon directories including themes and pixmaps. */ |
| 351 |
std::list<IconFinder::ThemeSubdirectory> IconFinder::getSearchPaths(const std::string& basePath) |
| 352 |
{ |
| 353 |
std::list<IconFinder::ThemeSubdirectory> iconPaths; |
| 354 |
|
| 355 |
for (const auto& theme : ICON_THEMES) |
| 356 |
{ |
| 357 |
auto dir = unique_gchar(g_build_filename(basePath.c_str(), ICONS_DIR, theme, nullptr)); |
| 358 |
auto icons = iconsFromThemePath(dir.get()); |
| 359 |
iconPaths.splice(iconPaths.end(), icons); |
| 360 |
} |
| 361 |
|
| 362 |
/* Add root icons directory as potential path */ |
| 363 |
auto iconsPath = unique_gchar(g_build_filename(basePath.c_str(), ICONS_DIR, nullptr)); |
| 364 |
if (g_file_test(iconsPath.get(), G_FILE_TEST_IS_DIR)) |
| 365 |
{ |
| 366 |
iconPaths.emplace_back(IconFinder::ThemeSubdirectory{iconsPath.get(), 1}); |
| 367 |
} |
| 368 |
|
| 369 |
/* Add the pixmaps path as a fallback if it exists */ |
| 370 |
auto pixmapsPath = unique_gchar(g_build_filename(basePath.c_str(), PIXMAPS_PATH, nullptr)); |
| 371 |
if (g_file_test(pixmapsPath.get(), G_FILE_TEST_IS_DIR)) |
| 372 |
{ |
| 373 |
iconPaths.emplace_back(IconFinder::ThemeSubdirectory{pixmapsPath.get(), 1}); |
| 374 |
} |
| 375 |
|
| 376 |
/* Add the snap meta/gui path as a fallback if it exists */ |
| 377 |
auto snapMetaGuiPath = unique_gchar(g_build_filename(basePath.c_str(), METAGUI_PATH, nullptr)); |
| 378 |
if (g_file_test(snapMetaGuiPath.get(), G_FILE_TEST_IS_DIR)) |
| 379 |
{ |
| 380 |
iconPaths.emplace_back(IconFinder::ThemeSubdirectory{snapMetaGuiPath.get(), 1}); |
| 381 |
} |
| 382 |
|
| 383 |
/* Add the base directory itself as a fallback, for "foo.png" icon names */ |
| 384 |
iconPaths.emplace_back(IconFinder::ThemeSubdirectory{basePath, 1}); |
| 385 |
|
| 386 |
// find icons sorted by size, highest to lowest |
| 387 |
iconPaths.sort([](const ThemeSubdirectory& lhs, const ThemeSubdirectory& rhs) { return lhs.size > rhs.size; }); |
| 388 |
return iconPaths; |
| 389 |
} |
| 390 |
|
| 391 |
} // namespace app_launch |
| 392 |
} // namespace lomiri |