GDirectoryModel.cpp 10 KB

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