FileIconProvider.cpp 9.6 KB

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