FileIconProvider.cpp 9.4 KB

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