DirectoryModel.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "DirectoryModel.h"
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <grp.h>
  6. #include <pwd.h>
  7. #include <AK/FileSystemPath.h>
  8. #include <AK/StringBuilder.h>
  9. #include <SharedGraphics/GraphicsBitmap.h>
  10. #include <LibGUI/GPainter.h>
  11. #include <LibCore/CLock.h>
  12. static CLockable<HashMap<String, RetainPtr<GraphicsBitmap>>>& thumbnail_cache()
  13. {
  14. static CLockable<HashMap<String, RetainPtr<GraphicsBitmap>>>* s_map;
  15. if (!s_map)
  16. s_map = new CLockable<HashMap<String, RetainPtr<GraphicsBitmap>>>();
  17. return *s_map;
  18. }
  19. int thumbnail_thread(void* model_ptr)
  20. {
  21. auto& model = *(DirectoryModel*)model_ptr;
  22. for (;;) {
  23. sleep(1);
  24. Vector<String> to_generate;
  25. {
  26. LOCKER(thumbnail_cache().lock());
  27. to_generate.ensure_capacity(thumbnail_cache().resource().size());
  28. for (auto& it : thumbnail_cache().resource()) {
  29. if (it.value)
  30. continue;
  31. to_generate.append(it.key);
  32. }
  33. }
  34. if (to_generate.is_empty())
  35. continue;
  36. for (int i = 0; i < to_generate.size(); ++i) {
  37. auto& path = to_generate[i];
  38. auto png_bitmap = GraphicsBitmap::load_from_file(path);
  39. if (!png_bitmap)
  40. continue;
  41. auto thumbnail = GraphicsBitmap::create(png_bitmap->format(), { 32, 32 });
  42. Painter painter(*thumbnail);
  43. painter.draw_scaled_bitmap(thumbnail->rect(), *png_bitmap, png_bitmap->rect());
  44. {
  45. LOCKER(thumbnail_cache().lock());
  46. thumbnail_cache().resource().set(path, move(thumbnail));
  47. }
  48. if (model.on_thumbnail_progress)
  49. model.on_thumbnail_progress(i + 1, to_generate.size());
  50. model.did_update();
  51. }
  52. }
  53. }
  54. DirectoryModel::DirectoryModel()
  55. {
  56. create_thread(thumbnail_thread, this);
  57. m_directory_icon = GIcon::default_icon("filetype-folder");
  58. m_file_icon = GIcon::default_icon("filetype-unknown");
  59. m_symlink_icon = GIcon::default_icon("filetype-symlink");
  60. m_socket_icon = GIcon::default_icon("filetype-socket");
  61. m_executable_icon = GIcon::default_icon("filetype-executable");
  62. m_filetype_image_icon = GIcon::default_icon("filetype-image");
  63. setpwent();
  64. while (auto* passwd = getpwent())
  65. m_user_names.set(passwd->pw_uid, passwd->pw_name);
  66. endpwent();
  67. setgrent();
  68. while (auto* group = getgrent())
  69. m_group_names.set(group->gr_gid, group->gr_name);
  70. endgrent();
  71. }
  72. DirectoryModel::~DirectoryModel()
  73. {
  74. }
  75. int DirectoryModel::row_count(const GModelIndex&) const
  76. {
  77. return m_directories.size() + m_files.size();
  78. }
  79. int DirectoryModel::column_count(const GModelIndex&) const
  80. {
  81. return Column::__Count;
  82. }
  83. String DirectoryModel::column_name(int column) const
  84. {
  85. switch (column) {
  86. case Column::Icon: return "";
  87. case Column::Name: return "Name";
  88. case Column::Size: return "Size";
  89. case Column::Owner: return "Owner";
  90. case Column::Group: return "Group";
  91. case Column::Permissions: return "Mode";
  92. case Column::Inode: return "Inode";
  93. }
  94. ASSERT_NOT_REACHED();
  95. }
  96. GModel::ColumnMetadata DirectoryModel::column_metadata(int column) const
  97. {
  98. switch (column) {
  99. case Column::Icon: return { 16, TextAlignment::Center };
  100. case Column::Name: return { 120, TextAlignment::CenterLeft };
  101. case Column::Size: return { 80, TextAlignment::CenterRight };
  102. case Column::Owner: return { 50, TextAlignment::CenterLeft };
  103. case Column::Group: return { 50, TextAlignment::CenterLeft };
  104. case Column::Permissions: return { 80, TextAlignment::CenterLeft };
  105. case Column::Inode: return { 80, TextAlignment::CenterRight };
  106. }
  107. ASSERT_NOT_REACHED();
  108. }
  109. GIcon DirectoryModel::icon_for(const Entry& entry) const
  110. {
  111. if (S_ISDIR(entry.mode))
  112. return m_directory_icon;
  113. if (S_ISLNK(entry.mode))
  114. return m_symlink_icon;
  115. if (S_ISSOCK(entry.mode))
  116. return m_socket_icon;
  117. if (entry.mode & S_IXUSR)
  118. return m_executable_icon;
  119. if (entry.name.to_lowercase().ends_with(".png")) {
  120. if (!entry.thumbnail) {
  121. auto path = entry.full_path(*this);
  122. LOCKER(thumbnail_cache().lock());
  123. auto it = thumbnail_cache().resource().find(path);
  124. if (it != thumbnail_cache().resource().end()) {
  125. entry.thumbnail = (*it).value.copy_ref();
  126. } else {
  127. thumbnail_cache().resource().set(path, nullptr);
  128. }
  129. }
  130. if (!entry.thumbnail)
  131. return m_filetype_image_icon;
  132. return GIcon(m_filetype_image_icon.bitmap_for_size(16), *entry.thumbnail);
  133. }
  134. return m_file_icon;
  135. }
  136. static String permission_string(mode_t mode)
  137. {
  138. StringBuilder builder;
  139. if (S_ISDIR(mode))
  140. builder.append("d");
  141. else if (S_ISLNK(mode))
  142. builder.append("l");
  143. else if (S_ISBLK(mode))
  144. builder.append("b");
  145. else if (S_ISCHR(mode))
  146. builder.append("c");
  147. else if (S_ISFIFO(mode))
  148. builder.append("f");
  149. else if (S_ISSOCK(mode))
  150. builder.append("s");
  151. else if (S_ISREG(mode))
  152. builder.append("-");
  153. else
  154. builder.append("?");
  155. builder.appendf("%c%c%c%c%c%c%c%c",
  156. mode & S_IRUSR ? 'r' : '-',
  157. mode & S_IWUSR ? 'w' : '-',
  158. mode & S_ISUID ? 's' : (mode & S_IXUSR ? 'x' : '-'),
  159. mode & S_IRGRP ? 'r' : '-',
  160. mode & S_IWGRP ? 'w' : '-',
  161. mode & S_ISGID ? 's' : (mode & S_IXGRP ? 'x' : '-'),
  162. mode & S_IROTH ? 'r' : '-',
  163. mode & S_IWOTH ? 'w' : '-'
  164. );
  165. if (mode & S_ISVTX)
  166. builder.append("t");
  167. else
  168. builder.appendf("%c", mode & S_IXOTH ? 'x' : '-');
  169. return builder.to_string();
  170. }
  171. String DirectoryModel::name_for_uid(uid_t uid) const
  172. {
  173. auto it = m_user_names.find(uid);
  174. if (it == m_user_names.end())
  175. return String::format("%u", uid);
  176. return (*it).value;
  177. }
  178. String DirectoryModel::name_for_gid(uid_t gid) const
  179. {
  180. auto it = m_user_names.find(gid);
  181. if (it == m_user_names.end())
  182. return String::format("%u", gid);
  183. return (*it).value;
  184. }
  185. GVariant DirectoryModel::data(const GModelIndex& index, Role role) const
  186. {
  187. ASSERT(is_valid(index));
  188. auto& entry = this->entry(index.row());
  189. if (role == Role::Sort) {
  190. switch (index.column()) {
  191. case Column::Icon: return entry.is_directory() ? 0 : 1;
  192. case Column::Name: return entry.name;
  193. case Column::Size: return (int)entry.size;
  194. case Column::Owner: return name_for_uid(entry.uid);
  195. case Column::Group: return name_for_gid(entry.gid);
  196. case Column::Permissions: return permission_string(entry.mode);
  197. case Column::Inode: return (int)entry.inode;
  198. }
  199. ASSERT_NOT_REACHED();
  200. }
  201. if (role == Role::Display) {
  202. switch (index.column()) {
  203. case Column::Icon: return icon_for(entry);
  204. case Column::Name: return entry.name;
  205. case Column::Size: return (int)entry.size;
  206. case Column::Owner: return name_for_uid(entry.uid);
  207. case Column::Group: return name_for_gid(entry.gid);
  208. case Column::Permissions: return permission_string(entry.mode);
  209. case Column::Inode: return (int)entry.inode;
  210. }
  211. }
  212. if (role == Role::Icon) {
  213. return icon_for(entry);
  214. }
  215. return { };
  216. }
  217. void DirectoryModel::update()
  218. {
  219. DIR* dirp = opendir(m_path.characters());
  220. if (!dirp) {
  221. perror("opendir");
  222. exit(1);
  223. }
  224. m_directories.clear();
  225. m_files.clear();
  226. m_bytes_in_files = 0;
  227. while (auto* de = readdir(dirp)) {
  228. Entry entry;
  229. entry.name = de->d_name;
  230. if (entry.name == "." || entry.name == "..")
  231. continue;
  232. struct stat st;
  233. int rc = lstat(String::format("%s/%s", m_path.characters(), de->d_name).characters(), &st);
  234. if (rc < 0) {
  235. perror("lstat");
  236. continue;
  237. }
  238. entry.size = st.st_size;
  239. entry.mode = st.st_mode;
  240. entry.uid = st.st_uid;
  241. entry.gid = st.st_gid;
  242. entry.inode = st.st_ino;
  243. auto& entries = S_ISDIR(st.st_mode) ? m_directories : m_files;
  244. entries.append(move(entry));
  245. if (S_ISREG(entry.mode))
  246. m_bytes_in_files += st.st_size;
  247. }
  248. closedir(dirp);
  249. did_update();
  250. }
  251. void DirectoryModel::open(const String& a_path)
  252. {
  253. FileSystemPath canonical_path(a_path);
  254. auto path = canonical_path.string();
  255. if (m_path == path)
  256. return;
  257. DIR* dirp = opendir(path.characters());
  258. if (!dirp)
  259. return;
  260. closedir(dirp);
  261. m_path = path;
  262. update();
  263. set_selected_index(index(0, 0));
  264. }
  265. void DirectoryModel::activate(const GModelIndex& index)
  266. {
  267. if (!index.is_valid())
  268. return;
  269. auto& entry = this->entry(index.row());
  270. FileSystemPath path(String::format("%s/%s", m_path.characters(), entry.name.characters()));
  271. if (entry.is_directory()) {
  272. open(path.string());
  273. return;
  274. }
  275. if (entry.is_executable()) {
  276. if (fork() == 0) {
  277. int rc = execl(path.string().characters(), path.string().characters(), nullptr);
  278. if (rc < 0)
  279. perror("exec");
  280. ASSERT_NOT_REACHED();
  281. }
  282. return;
  283. }
  284. if (path.string().to_lowercase().ends_with(".png")) {
  285. if (fork() == 0) {
  286. int rc = execl("/bin/qs", "/bin/qs", path.string().characters(), nullptr);
  287. if (rc < 0)
  288. perror("exec");
  289. ASSERT_NOT_REACHED();
  290. }
  291. return;
  292. }
  293. if (fork() == 0) {
  294. int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.string().characters(), nullptr);
  295. if (rc < 0)
  296. perror("exec");
  297. ASSERT_NOT_REACHED();
  298. }
  299. return;
  300. }