FileIconProvider.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Array.h>
  8. #include <AK/LexicalPath.h>
  9. #include <AK/String.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibCore/File.h>
  12. #include <LibCore/MappedFile.h>
  13. #include <LibCore/StandardPaths.h>
  14. #include <LibELF/Image.h>
  15. #include <LibGUI/FileIconProvider.h>
  16. #include <LibGUI/Icon.h>
  17. #include <LibGUI/Painter.h>
  18. #include <LibGfx/Bitmap.h>
  19. #include <LibGfx/PNGLoader.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. namespace GUI {
  24. static Icon s_hard_disk_icon;
  25. static Icon s_directory_icon;
  26. static Icon s_directory_open_icon;
  27. static Icon s_inaccessible_directory_icon;
  28. static Icon s_desktop_directory_icon;
  29. static Icon s_home_directory_icon;
  30. static Icon s_home_directory_open_icon;
  31. static Icon s_git_directory_icon;
  32. static Icon s_git_directory_open_icon;
  33. static Icon s_file_icon;
  34. static Icon s_symlink_icon;
  35. static Icon s_socket_icon;
  36. static Icon s_executable_icon;
  37. static Icon s_filetype_image_icon;
  38. static RefPtr<Gfx::Bitmap> s_symlink_emblem;
  39. static RefPtr<Gfx::Bitmap> s_symlink_emblem_small;
  40. static HashMap<String, Icon> s_filetype_icons;
  41. static HashMap<String, Vector<String>> s_filetype_patterns;
  42. static void initialize_executable_icon_if_needed()
  43. {
  44. static bool initialized = false;
  45. if (initialized)
  46. return;
  47. initialized = true;
  48. s_executable_icon = Icon::default_icon("filetype-executable"sv);
  49. }
  50. static void initialize_filetype_image_icon_if_needed()
  51. {
  52. static bool initialized = false;
  53. if (initialized)
  54. return;
  55. initialized = true;
  56. s_filetype_image_icon = Icon::default_icon("filetype-image"sv);
  57. }
  58. static void initialize_if_needed()
  59. {
  60. static bool s_initialized = false;
  61. if (s_initialized)
  62. return;
  63. auto config = Core::ConfigFile::open("/etc/FileIconProvider.ini").release_value_but_fixme_should_propagate_errors();
  64. s_symlink_emblem = Gfx::Bitmap::try_load_from_file("/res/icons/symlink-emblem.png"sv).release_value_but_fixme_should_propagate_errors();
  65. s_symlink_emblem_small = Gfx::Bitmap::try_load_from_file("/res/icons/symlink-emblem-small.png"sv).release_value_but_fixme_should_propagate_errors();
  66. s_hard_disk_icon = Icon::default_icon("hard-disk"sv);
  67. s_directory_icon = Icon::default_icon("filetype-folder"sv);
  68. s_directory_open_icon = Icon::default_icon("filetype-folder-open"sv);
  69. s_inaccessible_directory_icon = Icon::default_icon("filetype-folder-inaccessible"sv);
  70. s_home_directory_icon = Icon::default_icon("home-directory"sv);
  71. s_home_directory_open_icon = Icon::default_icon("home-directory-open"sv);
  72. s_git_directory_icon = Icon::default_icon("git-directory"sv);
  73. s_git_directory_open_icon = Icon::default_icon("git-directory-open"sv);
  74. s_desktop_directory_icon = Icon::default_icon("desktop"sv);
  75. s_file_icon = Icon::default_icon("filetype-unknown"sv);
  76. s_symlink_icon = Icon::default_icon("filetype-symlink"sv);
  77. s_socket_icon = Icon::default_icon("filetype-socket"sv);
  78. initialize_filetype_image_icon_if_needed();
  79. initialize_executable_icon_if_needed();
  80. for (auto& filetype : config->keys("Icons")) {
  81. s_filetype_icons.set(filetype, Icon::default_icon(String::formatted("filetype-{}", filetype)));
  82. s_filetype_patterns.set(filetype, config->read_entry("Icons", filetype).split(','));
  83. }
  84. s_initialized = true;
  85. }
  86. Icon FileIconProvider::directory_icon()
  87. {
  88. initialize_if_needed();
  89. return s_directory_icon;
  90. }
  91. Icon FileIconProvider::directory_open_icon()
  92. {
  93. initialize_if_needed();
  94. return s_directory_open_icon;
  95. }
  96. Icon FileIconProvider::home_directory_icon()
  97. {
  98. initialize_if_needed();
  99. return s_home_directory_icon;
  100. }
  101. Icon FileIconProvider::desktop_directory_icon()
  102. {
  103. initialize_if_needed();
  104. return s_desktop_directory_icon;
  105. }
  106. Icon FileIconProvider::home_directory_open_icon()
  107. {
  108. initialize_if_needed();
  109. return s_home_directory_open_icon;
  110. }
  111. Icon FileIconProvider::git_directory_icon()
  112. {
  113. initialize_if_needed();
  114. return s_git_directory_icon;
  115. }
  116. Icon FileIconProvider::git_directory_open_icon()
  117. {
  118. initialize_if_needed();
  119. return s_git_directory_open_icon;
  120. }
  121. Icon FileIconProvider::filetype_image_icon()
  122. {
  123. initialize_filetype_image_icon_if_needed();
  124. return s_filetype_image_icon;
  125. }
  126. Icon FileIconProvider::icon_for_path(String const& path)
  127. {
  128. struct stat stat;
  129. if (::stat(path.characters(), &stat) < 0)
  130. return s_file_icon;
  131. return icon_for_path(path, stat.st_mode);
  132. }
  133. Icon FileIconProvider::icon_for_executable(String const& path)
  134. {
  135. static HashMap<String, Icon> app_icon_cache;
  136. if (auto it = app_icon_cache.find(path); it != app_icon_cache.end())
  137. return it->value;
  138. initialize_executable_icon_if_needed();
  139. // If the icon for an app isn't in the cache we attempt to load the file as an ELF image and extract
  140. // the serenity_app_icon_* sections which should contain the icons as raw PNG data. In the future it would
  141. // be better if the binary signalled the image format being used or we deduced it, e.g. using magic bytes.
  142. auto file_or_error = Core::MappedFile::map(path);
  143. if (file_or_error.is_error()) {
  144. app_icon_cache.set(path, s_executable_icon);
  145. return s_executable_icon;
  146. }
  147. auto& mapped_file = file_or_error.value();
  148. if (mapped_file->size() < SELFMAG) {
  149. app_icon_cache.set(path, s_executable_icon);
  150. return s_executable_icon;
  151. }
  152. if (memcmp(mapped_file->data(), ELFMAG, SELFMAG) != 0) {
  153. app_icon_cache.set(path, s_executable_icon);
  154. return s_executable_icon;
  155. }
  156. auto image = ELF::Image((u8 const*)mapped_file->data(), mapped_file->size());
  157. if (!image.is_valid()) {
  158. app_icon_cache.set(path, s_executable_icon);
  159. return s_executable_icon;
  160. }
  161. // If any of the required sections are missing then use the defaults
  162. Icon icon;
  163. struct IconSection {
  164. StringView section_name;
  165. int image_size;
  166. };
  167. static constexpr Array<IconSection, 2> icon_sections = {
  168. IconSection { .section_name = "serenity_icon_s"sv, .image_size = 16 },
  169. IconSection { .section_name = "serenity_icon_m"sv, .image_size = 32 }
  170. };
  171. bool had_error = false;
  172. for (auto const& icon_section : icon_sections) {
  173. auto section = image.lookup_section(icon_section.section_name);
  174. RefPtr<Gfx::Bitmap> bitmap;
  175. if (!section.has_value()) {
  176. bitmap = s_executable_icon.bitmap_for_size(icon_section.image_size);
  177. } else {
  178. // FIXME: Use the ImageDecoder service.
  179. auto frame_or_error = Gfx::PNGImageDecoderPlugin(reinterpret_cast<u8 const*>(section->raw_data()), section->size()).frame(0);
  180. if (!frame_or_error.is_error()) {
  181. bitmap = frame_or_error.value().image;
  182. }
  183. }
  184. if (!bitmap) {
  185. dbgln("Failed to find embedded icon and failed to clone default icon for application {} at icon size {}", path, icon_section.image_size);
  186. had_error = true;
  187. continue;
  188. }
  189. icon.set_bitmap_for_size(icon_section.image_size, std::move(bitmap));
  190. }
  191. if (had_error) {
  192. app_icon_cache.set(path, s_executable_icon);
  193. return s_executable_icon;
  194. }
  195. app_icon_cache.set(path, icon);
  196. return icon;
  197. }
  198. Icon FileIconProvider::icon_for_path(String const& path, mode_t mode)
  199. {
  200. initialize_if_needed();
  201. if (path == "/")
  202. return s_hard_disk_icon;
  203. if (S_ISDIR(mode)) {
  204. if (path == Core::StandardPaths::home_directory())
  205. return s_home_directory_icon;
  206. if (path == Core::StandardPaths::desktop_directory())
  207. return s_desktop_directory_icon;
  208. if (access(path.characters(), R_OK | X_OK) < 0)
  209. return s_inaccessible_directory_icon;
  210. if (path.ends_with(".git"sv))
  211. return s_git_directory_icon;
  212. return s_directory_icon;
  213. }
  214. if (S_ISLNK(mode)) {
  215. auto raw_symlink_target_or_error = Core::File::read_link(path);
  216. if (raw_symlink_target_or_error.is_error())
  217. return s_symlink_icon;
  218. auto raw_symlink_target = raw_symlink_target_or_error.release_value();
  219. if (raw_symlink_target.is_null())
  220. return s_symlink_icon;
  221. String target_path;
  222. if (raw_symlink_target.starts_with('/')) {
  223. target_path = raw_symlink_target;
  224. } else {
  225. target_path = Core::File::real_path_for(String::formatted("{}/{}", LexicalPath::dirname(path), raw_symlink_target));
  226. }
  227. auto target_icon = icon_for_path(target_path);
  228. Icon generated_icon;
  229. for (auto size : target_icon.sizes()) {
  230. auto& emblem = size < 32 ? *s_symlink_emblem_small : *s_symlink_emblem;
  231. auto original_bitmap = target_icon.bitmap_for_size(size);
  232. VERIFY(original_bitmap);
  233. auto generated_bitmap_or_error = original_bitmap->clone();
  234. if (generated_bitmap_or_error.is_error()) {
  235. dbgln("Failed to clone {}x{} icon for symlink variant", size, size);
  236. return s_symlink_icon;
  237. }
  238. auto generated_bitmap = generated_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  239. GUI::Painter painter(*generated_bitmap);
  240. painter.blit({ size - emblem.width(), size - emblem.height() }, emblem, emblem.rect());
  241. generated_icon.set_bitmap_for_size(size, move(generated_bitmap));
  242. }
  243. return generated_icon;
  244. }
  245. if (S_ISSOCK(mode))
  246. return s_socket_icon;
  247. if (mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  248. return icon_for_executable(path);
  249. if (Gfx::Bitmap::is_path_a_supported_image_format(path.view()))
  250. return s_filetype_image_icon;
  251. for (auto& filetype : s_filetype_icons.keys()) {
  252. auto pattern_it = s_filetype_patterns.find(filetype);
  253. if (pattern_it == s_filetype_patterns.end())
  254. continue;
  255. for (auto& pattern : pattern_it->value) {
  256. if (path.matches(pattern, CaseSensitivity::CaseInsensitive))
  257. return s_filetype_icons.get(filetype).value();
  258. }
  259. }
  260. return s_file_icon;
  261. }
  262. }