FileIconProvider.cpp 11 KB

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