IconTheme.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "IconTheme.h"
  2. #include <algorithm>
  3. #include <cctype>
  4. #include <cstdlib>
  5. #include <fstream>
  6. #include <set>
  7. #include <sstream>
  8. using namespace WallpaperEngine::Desktop;
  9. namespace {
  10. std::filesystem::path homeDir () {
  11. const char* home = std::getenv ("HOME");
  12. return home != nullptr ? home : "";
  13. }
  14. std::vector<std::filesystem::path> dataDirs () {
  15. std::vector<std::filesystem::path> result;
  16. const char* dataHome = std::getenv ("XDG_DATA_HOME");
  17. const char* dirs = std::getenv ("XDG_DATA_DIRS");
  18. result.emplace_back (
  19. dataHome != nullptr && *dataHome ? std::filesystem::path (dataHome) : homeDir () / ".local/share"
  20. );
  21. std::stringstream stream (dirs != nullptr && *dirs ? dirs : "/usr/local/share:/usr/share");
  22. std::string dir;
  23. while (std::getline (stream, dir, ':')) {
  24. if (!dir.empty ()) {
  25. result.emplace_back (dir);
  26. }
  27. }
  28. return result;
  29. }
  30. std::optional<std::string>
  31. iniValue (const std::filesystem::path& file, const std::string& section, const std::string& key) {
  32. std::ifstream stream (file);
  33. std::string line;
  34. bool inSection = false;
  35. while (std::getline (stream, line)) {
  36. if (line.starts_with ('[')) {
  37. inSection = line == "[" + section + "]";
  38. continue;
  39. }
  40. if (inSection && line.starts_with (key + "=")) {
  41. return line.substr (key.size () + 1);
  42. }
  43. }
  44. return std::nullopt;
  45. }
  46. std::string userTheme () {
  47. const char* configHome = std::getenv ("XDG_CONFIG_HOME");
  48. const auto config
  49. = configHome != nullptr && *configHome ? std::filesystem::path (configHome) : homeDir () / ".config";
  50. if (const auto theme = iniValue (config / "kdeglobals", "Icons", "Theme"); theme.has_value () && !theme->empty ()) {
  51. return *theme;
  52. }
  53. return "breeze";
  54. }
  55. /** Scalable beats every fixed size, then bigger is better, SVG beats PNG at the same size */
  56. int score (const std::filesystem::path& path) {
  57. const std::string dir = path.parent_path ().string ();
  58. const bool svg = path.extension () == ".svg";
  59. int size = 0;
  60. if (dir.find ("scalable") != std::string::npos) {
  61. size = 10000;
  62. } else {
  63. for (const auto& part : path.parent_path ()) {
  64. const std::string name = part.string ();
  65. if (!name.empty () && std::isdigit (static_cast<unsigned char> (name.front ()))) {
  66. size = std::max (size, std::atoi (name.c_str ()));
  67. }
  68. }
  69. }
  70. // symbolic icons are monochrome glyphs meant for toolbars, not what a launcher should show
  71. const bool symbolic = path.stem ().string ().ends_with ("-symbolic");
  72. return size * 2 + (svg ? 1 : 0) - (symbolic ? 100000 : 0);
  73. }
  74. }
  75. IconTheme& IconTheme::get () {
  76. static IconTheme instance;
  77. return instance;
  78. }
  79. IconTheme::IconTheme () {
  80. const auto home = homeDir ();
  81. for (const auto& dir : dataDirs ()) {
  82. this->m_baseDirs.push_back (dir / "icons");
  83. }
  84. this->m_baseDirs.push_back (home / ".icons");
  85. // the user's theme first, then whatever it inherits from, hicolor always last
  86. std::vector<std::string> pending { userTheme () };
  87. std::set<std::string> seen;
  88. while (!pending.empty ()) {
  89. const std::string theme = pending.front ();
  90. pending.erase (pending.begin ());
  91. if (theme == "hicolor" || !seen.insert (theme).second) {
  92. continue;
  93. }
  94. this->m_themes.push_back (theme);
  95. for (const auto& parent : this->inherits (theme)) {
  96. pending.push_back (parent);
  97. }
  98. }
  99. this->m_themes.emplace_back ("hicolor");
  100. }
  101. std::vector<std::string> IconTheme::inherits (const std::string& theme) const {
  102. for (const auto& base : this->m_baseDirs) {
  103. const auto parents = iniValue (base / theme / "index.theme", "Icon Theme", "Inherits");
  104. if (!parents.has_value ()) {
  105. continue;
  106. }
  107. std::vector<std::string> result;
  108. std::stringstream stream (*parents);
  109. std::string parent;
  110. while (std::getline (stream, parent, ',')) {
  111. if (!parent.empty ()) {
  112. result.push_back (parent);
  113. }
  114. }
  115. return result;
  116. }
  117. return {};
  118. }
  119. const std::map<std::string, IconTheme::Candidate>& IconTheme::index (const std::string& theme) {
  120. if (const auto it = this->m_indexes.find (theme); it != this->m_indexes.end ()) {
  121. return it->second;
  122. }
  123. auto& result = this->m_indexes[theme];
  124. for (const auto& base : this->m_baseDirs) {
  125. std::error_code error;
  126. const auto root = base / theme;
  127. if (!std::filesystem::is_directory (root, error)) {
  128. continue;
  129. }
  130. for (auto it = std::filesystem::recursive_directory_iterator (
  131. root, std::filesystem::directory_options::skip_permission_denied, error
  132. );
  133. it != std::filesystem::recursive_directory_iterator (); it.increment (error)) {
  134. if (error) {
  135. break;
  136. }
  137. const auto& path = it->path ();
  138. const auto extension = path.extension ();
  139. if (extension != ".png" && extension != ".svg") {
  140. continue;
  141. }
  142. const int candidateScore = score (path);
  143. const auto [entry, inserted]
  144. = result.try_emplace (path.stem ().string (), Candidate { path, candidateScore });
  145. if (!inserted && candidateScore > entry->second.score) {
  146. entry->second = Candidate { path, candidateScore };
  147. }
  148. }
  149. }
  150. return result;
  151. }
  152. std::optional<std::filesystem::path> IconTheme::find (const std::string& icon) {
  153. if (icon.empty ()) {
  154. return std::nullopt;
  155. }
  156. std::error_code error;
  157. if (icon.starts_with ('/')) {
  158. return std::filesystem::is_regular_file (icon, error) ? std::optional<std::filesystem::path> (icon)
  159. : std::nullopt;
  160. }
  161. for (const auto& theme : this->m_themes) {
  162. const auto& icons = this->index (theme);
  163. if (const auto it = icons.find (icon); it != icons.end ()) {
  164. return it->second.path;
  165. }
  166. }
  167. for (const auto& extension : { ".svg", ".png" }) {
  168. const auto path = std::filesystem::path ("/usr/share/pixmaps") / (icon + extension);
  169. if (std::filesystem::is_regular_file (path, error)) {
  170. return path;
  171. }
  172. }
  173. return std::nullopt;
  174. }