60
src/IconRegistry.cpp
Normal file
60
src/IconRegistry.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include "IconRegistry.hpp"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <ranges>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
IconTheme::IconTheme(std::filesystem::path const &themes_directory_path)
|
||||||
|
{
|
||||||
|
for (auto const &dir :
|
||||||
|
std::filesystem::directory_iterator(themes_directory_path)) {
|
||||||
|
if (!dir.is_directory())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto const index_path = dir.path() / "index.theme";
|
||||||
|
if (std::filesystem::is_regular_file(index_path))
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IconRegistry::IconRegistry()
|
||||||
|
{
|
||||||
|
std::vector<std::filesystem::path> theme_directory_paths;
|
||||||
|
|
||||||
|
{
|
||||||
|
auto const *env { getenv("HOME") };
|
||||||
|
if (env && *env) {
|
||||||
|
theme_directory_paths.push_back(
|
||||||
|
std::filesystem::path(env) / ".icons");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto const *env { getenv("XDG_DATA_DIRS") };
|
||||||
|
if (env && *env) {
|
||||||
|
theme_directory_paths.push_back(
|
||||||
|
std::filesystem::path(env) / "icons");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::filesystem::path const path { "/usr/share/pixmaps" };
|
||||||
|
if (std::filesystem::exists(path))
|
||||||
|
theme_directory_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto const &path : std::move(theme_directory_paths)
|
||||||
|
| std::views::filter([](std::filesystem::path &path) {
|
||||||
|
return std::filesystem::is_directory(path);
|
||||||
|
})) {
|
||||||
|
try {
|
||||||
|
m_themes.push_back({ path });
|
||||||
|
} catch (...) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_themes.empty()) {
|
||||||
|
throw std::runtime_error("Could not find any icon themes.");
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/IconRegistry.hpp
Normal file
23
src/IconRegistry.hpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
struct Icon { };
|
||||||
|
|
||||||
|
struct IconTheme {
|
||||||
|
IconTheme(std::filesystem::path const &themes_directory_path);
|
||||||
|
~IconTheme() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, Icon> m_cached_icons;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IconRegistry {
|
||||||
|
IconRegistry();
|
||||||
|
~IconRegistry() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<IconTheme> m_themes;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user