GDirectoryModel.cpp 9.2 KB

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