FileIconProvider.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/String.h>
  28. #include <LibCore/ConfigFile.h>
  29. #include <LibCore/DirIterator.h>
  30. #include <LibCore/File.h>
  31. #include <LibCore/StandardPaths.h>
  32. #include <LibGUI/FileIconProvider.h>
  33. #include <LibGUI/Icon.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGfx/Bitmap.h>
  36. #include <sys/stat.h>
  37. namespace GUI {
  38. static Icon s_hard_disk_icon;
  39. static Icon s_directory_icon;
  40. static Icon s_directory_open_icon;
  41. static Icon s_inaccessible_directory_icon;
  42. static Icon s_home_directory_icon;
  43. static Icon s_home_directory_open_icon;
  44. static Icon s_file_icon;
  45. static Icon s_symlink_icon;
  46. static Icon s_socket_icon;
  47. static Icon s_executable_icon;
  48. static Icon s_filetype_image_icon;
  49. static RefPtr<Gfx::Bitmap> s_symlink_emblem;
  50. static RefPtr<Gfx::Bitmap> s_symlink_emblem_small;
  51. static HashMap<String, Icon> s_filetype_icons;
  52. static HashMap<String, Vector<String>> s_filetype_patterns;
  53. static void initialize_if_needed()
  54. {
  55. static bool s_initialized = false;
  56. if (s_initialized)
  57. return;
  58. auto config = Core::ConfigFile::open("/etc/FileIconProvider.ini");
  59. s_symlink_emblem = Gfx::Bitmap::load_from_file("/res/icons/symlink-emblem.png");
  60. s_symlink_emblem_small = Gfx::Bitmap::load_from_file("/res/icons/symlink-emblem-small.png");
  61. s_hard_disk_icon = Icon::default_icon("hard-disk");
  62. s_directory_icon = Icon::default_icon("filetype-folder");
  63. s_directory_open_icon = Icon::default_icon("filetype-folder-open");
  64. s_inaccessible_directory_icon = Icon::default_icon("filetype-folder-inaccessible");
  65. s_home_directory_icon = Icon::default_icon("home-directory");
  66. s_home_directory_open_icon = Icon::default_icon("home-directory-open");
  67. s_file_icon = Icon::default_icon("filetype-unknown");
  68. s_symlink_icon = Icon::default_icon("filetype-symlink");
  69. s_socket_icon = Icon::default_icon("filetype-socket");
  70. s_executable_icon = Icon::default_icon("filetype-executable");
  71. s_filetype_image_icon = Icon::default_icon("filetype-image");
  72. for (auto& filetype : config->keys("Icons")) {
  73. s_filetype_icons.set(filetype, Icon::default_icon(String::formatted("filetype-{}", filetype)));
  74. s_filetype_patterns.set(filetype, config->read_entry("Icons", filetype).split(','));
  75. }
  76. s_initialized = true;
  77. }
  78. Icon FileIconProvider::directory_icon()
  79. {
  80. initialize_if_needed();
  81. return s_directory_icon;
  82. }
  83. Icon FileIconProvider::directory_open_icon()
  84. {
  85. initialize_if_needed();
  86. return s_directory_open_icon;
  87. }
  88. Icon FileIconProvider::home_directory_icon()
  89. {
  90. initialize_if_needed();
  91. return s_home_directory_icon;
  92. }
  93. Icon FileIconProvider::home_directory_open_icon()
  94. {
  95. initialize_if_needed();
  96. return s_home_directory_open_icon;
  97. }
  98. Icon FileIconProvider::filetype_image_icon()
  99. {
  100. initialize_if_needed();
  101. return s_filetype_image_icon;
  102. }
  103. Icon FileIconProvider::icon_for_path(const String& path)
  104. {
  105. struct stat stat;
  106. if (::stat(path.characters(), &stat) < 0)
  107. return {};
  108. return icon_for_path(path, stat.st_mode);
  109. }
  110. static Icon icon_for_executable(const String& path)
  111. {
  112. // FIXME: This is a huge hack and it would be much nicer if executables had icons embedded in them somehow.
  113. static HashMap<String, Icon> app_icon_cache;
  114. if (app_icon_cache.is_empty()) {
  115. Core::DirIterator dt("/res/apps");
  116. while (dt.has_next()) {
  117. auto app_file = Core::ConfigFile::open(dt.next_full_path());
  118. Icon app_icon;
  119. auto icon16_path = app_file->read_entry("Icons", "16x16");
  120. auto icon32_path = app_file->read_entry("Icons", "32x32");
  121. if (auto icon16 = Gfx::Bitmap::load_from_file(icon16_path))
  122. app_icon.set_bitmap_for_size(16, move(icon16));
  123. if (auto icon32 = Gfx::Bitmap::load_from_file(icon32_path))
  124. app_icon.set_bitmap_for_size(32, move(icon32));
  125. app_icon_cache.set(app_file->read_entry("App", "Executable"), move(app_icon));
  126. }
  127. }
  128. if (auto it = app_icon_cache.find(path); it != app_icon_cache.end())
  129. return it->value;
  130. return s_executable_icon;
  131. }
  132. Icon FileIconProvider::icon_for_path(const String& path, mode_t mode)
  133. {
  134. initialize_if_needed();
  135. if (path == "/")
  136. return s_hard_disk_icon;
  137. if (S_ISDIR(mode)) {
  138. if (path == Core::StandardPaths::home_directory())
  139. return s_home_directory_icon;
  140. if (access(path.characters(), R_OK | X_OK) < 0)
  141. return s_inaccessible_directory_icon;
  142. return s_directory_icon;
  143. }
  144. if (S_ISLNK(mode)) {
  145. auto raw_symlink_target = Core::File::read_link(path);
  146. if (raw_symlink_target.is_null())
  147. return s_symlink_icon;
  148. String target_path;
  149. if (raw_symlink_target.starts_with('/')) {
  150. target_path = raw_symlink_target;
  151. } else {
  152. target_path = Core::File::real_path_for(String::formatted("{}/{}", LexicalPath(path).dirname(), raw_symlink_target));
  153. }
  154. auto target_icon = icon_for_path(target_path);
  155. Icon generated_icon;
  156. for (auto size : target_icon.sizes()) {
  157. auto& emblem = size < 32 ? *s_symlink_emblem_small : *s_symlink_emblem;
  158. auto original_bitmap = target_icon.bitmap_for_size(size);
  159. ASSERT(original_bitmap);
  160. auto generated_bitmap = original_bitmap->clone();
  161. if (!generated_bitmap) {
  162. dbgln("Failed to clone {}x{} icon for symlink variant", size, size);
  163. return s_symlink_icon;
  164. }
  165. GUI::Painter painter(*generated_bitmap);
  166. painter.blit({ size - emblem.width(), size - emblem.height() }, emblem, emblem.rect());
  167. generated_icon.set_bitmap_for_size(size, move(generated_bitmap));
  168. }
  169. return generated_icon;
  170. }
  171. if (S_ISSOCK(mode))
  172. return s_socket_icon;
  173. if (mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  174. return icon_for_executable(path);
  175. if (Gfx::Bitmap::is_path_a_supported_image_format(path.view()))
  176. return s_filetype_image_icon;
  177. for (auto& filetype : s_filetype_icons.keys()) {
  178. auto patterns = s_filetype_patterns.get(filetype).value();
  179. for (auto& pattern : patterns) {
  180. if (path.matches(pattern, CaseSensitivity::CaseInsensitive))
  181. return s_filetype_icons.get(filetype).value();
  182. }
  183. }
  184. return s_file_icon;
  185. }
  186. }