FileIconProvider.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/MappedFile.h>
  28. #include <AK/String.h>
  29. #include <LibCore/ConfigFile.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <LibCore/StandardPaths.h>
  33. #include <LibELF/Image.h>
  34. #include <LibGUI/FileIconProvider.h>
  35. #include <LibGUI/Icon.h>
  36. #include <LibGUI/Painter.h>
  37. #include <LibGfx/Bitmap.h>
  38. #include <LibGfx/PNGLoader.h>
  39. #include <sys/stat.h>
  40. namespace GUI {
  41. static Icon s_hard_disk_icon;
  42. static Icon s_directory_icon;
  43. static Icon s_directory_open_icon;
  44. static Icon s_inaccessible_directory_icon;
  45. static Icon s_home_directory_icon;
  46. static Icon s_home_directory_open_icon;
  47. static Icon s_file_icon;
  48. static Icon s_symlink_icon;
  49. static Icon s_socket_icon;
  50. static Icon s_executable_icon;
  51. static Icon s_filetype_image_icon;
  52. static RefPtr<Gfx::Bitmap> s_symlink_emblem;
  53. static RefPtr<Gfx::Bitmap> s_symlink_emblem_small;
  54. static HashMap<String, Icon> s_filetype_icons;
  55. static HashMap<String, Vector<String>> s_filetype_patterns;
  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");
  62. s_symlink_emblem = Gfx::Bitmap::load_from_file("/res/icons/symlink-emblem.png");
  63. s_symlink_emblem_small = Gfx::Bitmap::load_from_file("/res/icons/symlink-emblem-small.png");
  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_file_icon = Icon::default_icon("filetype-unknown");
  71. s_symlink_icon = Icon::default_icon("filetype-symlink");
  72. s_socket_icon = Icon::default_icon("filetype-socket");
  73. s_executable_icon = Icon::default_icon("filetype-executable");
  74. s_filetype_image_icon = Icon::default_icon("filetype-image");
  75. for (auto& filetype : config->keys("Icons")) {
  76. s_filetype_icons.set(filetype, Icon::default_icon(String::formatted("filetype-{}", filetype)));
  77. s_filetype_patterns.set(filetype, config->read_entry("Icons", filetype).split(','));
  78. }
  79. s_initialized = true;
  80. }
  81. Icon FileIconProvider::directory_icon()
  82. {
  83. initialize_if_needed();
  84. return s_directory_icon;
  85. }
  86. Icon FileIconProvider::directory_open_icon()
  87. {
  88. initialize_if_needed();
  89. return s_directory_open_icon;
  90. }
  91. Icon FileIconProvider::home_directory_icon()
  92. {
  93. initialize_if_needed();
  94. return s_home_directory_icon;
  95. }
  96. Icon FileIconProvider::home_directory_open_icon()
  97. {
  98. initialize_if_needed();
  99. return s_home_directory_open_icon;
  100. }
  101. Icon FileIconProvider::filetype_image_icon()
  102. {
  103. initialize_if_needed();
  104. return s_filetype_image_icon;
  105. }
  106. Icon FileIconProvider::icon_for_path(const String& path)
  107. {
  108. struct stat stat;
  109. if (::stat(path.characters(), &stat) < 0)
  110. return {};
  111. return icon_for_path(path, stat.st_mode);
  112. }
  113. static Icon icon_for_executable(const String& path)
  114. {
  115. static HashMap<String, Icon> app_icon_cache;
  116. if (auto it = app_icon_cache.find(path); it != app_icon_cache.end())
  117. return it->value;
  118. // If the icon for an app isn't in the cache we attempt to load the file as an ELF image and extract
  119. // the serenity_app_icon_* sections which should contain the icons as raw PNG data. In the future it would
  120. // be better if the binary signalled the image format being used or we deduced it, e.g. using magic bytes.
  121. auto mapped_file = make<MappedFile>(path);
  122. if (!mapped_file->is_valid()) {
  123. app_icon_cache.set(path, s_executable_icon);
  124. return s_executable_icon;
  125. }
  126. if (mapped_file->size() < SELFMAG) {
  127. app_icon_cache.set(path, s_executable_icon);
  128. return s_executable_icon;
  129. }
  130. if (memcmp(mapped_file->data(), ELFMAG, SELFMAG) != 0) {
  131. app_icon_cache.set(path, s_executable_icon);
  132. return s_executable_icon;
  133. }
  134. auto image = ELF::Image((const u8*)mapped_file->data(), mapped_file->size());
  135. if (!image.is_valid()) {
  136. app_icon_cache.set(path, s_executable_icon);
  137. return s_executable_icon;
  138. }
  139. // If any of the required sections are missing then use the defaults
  140. Icon icon;
  141. struct IconSection {
  142. const char* section_name;
  143. int image_size;
  144. };
  145. static const IconSection icon_sections[] = { { .section_name = "serenity_icon_s", .image_size = 16 }, { .section_name = "serenity_icon_m", .image_size = 32 } };
  146. bool had_error = false;
  147. for (const auto& icon_section : icon_sections) {
  148. auto section = image.lookup_section(icon_section.section_name);
  149. RefPtr<Gfx::Bitmap> bitmap;
  150. if (section.is_undefined()) {
  151. bitmap = s_executable_icon.bitmap_for_size(icon_section.image_size)->clone();
  152. } else {
  153. bitmap = Gfx::load_png_from_memory(reinterpret_cast<const u8*>(section.raw_data()), section.size());
  154. }
  155. if (!bitmap) {
  156. dbgln("Failed to find embedded icon and failed to clone default icon for application {} at icon size {}", path, icon_section.image_size);
  157. had_error = true;
  158. continue;
  159. }
  160. icon.set_bitmap_for_size(icon_section.image_size, std::move(bitmap));
  161. }
  162. if (had_error) {
  163. app_icon_cache.set(path, s_executable_icon);
  164. return s_executable_icon;
  165. }
  166. app_icon_cache.set(path, icon);
  167. return icon;
  168. }
  169. Icon FileIconProvider::icon_for_path(const String& path, mode_t mode)
  170. {
  171. initialize_if_needed();
  172. if (path == "/")
  173. return s_hard_disk_icon;
  174. if (S_ISDIR(mode)) {
  175. if (path == Core::StandardPaths::home_directory())
  176. return s_home_directory_icon;
  177. if (access(path.characters(), R_OK | X_OK) < 0)
  178. return s_inaccessible_directory_icon;
  179. return s_directory_icon;
  180. }
  181. if (S_ISLNK(mode)) {
  182. auto raw_symlink_target = Core::File::read_link(path);
  183. if (raw_symlink_target.is_null())
  184. return s_symlink_icon;
  185. String target_path;
  186. if (raw_symlink_target.starts_with('/')) {
  187. target_path = raw_symlink_target;
  188. } else {
  189. target_path = Core::File::real_path_for(String::formatted("{}/{}", LexicalPath(path).dirname(), raw_symlink_target));
  190. }
  191. auto target_icon = icon_for_path(target_path);
  192. Icon generated_icon;
  193. for (auto size : target_icon.sizes()) {
  194. auto& emblem = size < 32 ? *s_symlink_emblem_small : *s_symlink_emblem;
  195. auto original_bitmap = target_icon.bitmap_for_size(size);
  196. ASSERT(original_bitmap);
  197. auto generated_bitmap = original_bitmap->clone();
  198. if (!generated_bitmap) {
  199. dbgln("Failed to clone {}x{} icon for symlink variant", size, size);
  200. return s_symlink_icon;
  201. }
  202. GUI::Painter painter(*generated_bitmap);
  203. painter.blit({ size - emblem.width(), size - emblem.height() }, emblem, emblem.rect());
  204. generated_icon.set_bitmap_for_size(size, move(generated_bitmap));
  205. }
  206. return generated_icon;
  207. }
  208. if (S_ISSOCK(mode))
  209. return s_socket_icon;
  210. if (mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  211. return icon_for_executable(path);
  212. if (Gfx::Bitmap::is_path_a_supported_image_format(path.view()))
  213. return s_filetype_image_icon;
  214. for (auto& filetype : s_filetype_icons.keys()) {
  215. auto patterns = s_filetype_patterns.get(filetype).value();
  216. for (auto& pattern : patterns) {
  217. if (path.matches(pattern, CaseSensitivity::CaseInsensitive))
  218. return s_filetype_icons.get(filetype).value();
  219. }
  220. }
  221. return s_file_icon;
  222. }
  223. }