FileIconProvider.cpp 9.8 KB

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