GDirectoryModel.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <time.h>
  13. #include <unistd.h>
  14. static HashMap<String, RefPtr<GraphicsBitmap>> s_thumbnail_cache;
  15. static RefPtr<GraphicsBitmap> render_thumbnail(const StringView& path)
  16. {
  17. auto png_bitmap = GraphicsBitmap::load_from_file(path);
  18. if (!png_bitmap)
  19. return nullptr;
  20. auto thumbnail = GraphicsBitmap::create(png_bitmap->format(), { 32, 32 });
  21. Painter painter(*thumbnail);
  22. painter.draw_scaled_bitmap(thumbnail->rect(), *png_bitmap, png_bitmap->rect());
  23. return thumbnail;
  24. }
  25. GDirectoryModel::GDirectoryModel()
  26. {
  27. m_directory_icon = GIcon::default_icon("filetype-folder");
  28. m_file_icon = GIcon::default_icon("filetype-unknown");
  29. m_symlink_icon = GIcon::default_icon("filetype-symlink");
  30. m_socket_icon = GIcon::default_icon("filetype-socket");
  31. m_executable_icon = GIcon::default_icon("filetype-executable");
  32. m_filetype_image_icon = GIcon::default_icon("filetype-image");
  33. m_filetype_sound_icon = GIcon::default_icon("filetype-sound");
  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. LibThread::BackgroundAction<RefPtr<GraphicsBitmap>>::create(
  115. [path] {
  116. return render_thumbnail(path);
  117. },
  118. [this, path](auto thumbnail) {
  119. s_thumbnail_cache.set(path, move(thumbnail));
  120. m_thumbnail_progress++;
  121. if (on_thumbnail_progress)
  122. on_thumbnail_progress(m_thumbnail_progress, m_thumbnail_progress_total);
  123. if (m_thumbnail_progress == m_thumbnail_progress_total) {
  124. m_thumbnail_progress = 0;
  125. m_thumbnail_progress_total = 0;
  126. }
  127. did_update();
  128. });
  129. return false;
  130. }
  131. GIcon GDirectoryModel::icon_for(const Entry& entry) const
  132. {
  133. if (S_ISDIR(entry.mode))
  134. return m_directory_icon;
  135. if (S_ISLNK(entry.mode))
  136. return m_symlink_icon;
  137. if (S_ISSOCK(entry.mode))
  138. return m_socket_icon;
  139. if (entry.mode & S_IXUSR)
  140. return m_executable_icon;
  141. if (entry.name.to_lowercase().ends_with(".wav"))
  142. return m_filetype_sound_icon;
  143. if (entry.name.to_lowercase().ends_with(".png")) {
  144. if (!entry.thumbnail) {
  145. if (!const_cast<GDirectoryModel*>(this)->fetch_thumbnail_for(entry))
  146. return m_filetype_image_icon;
  147. }
  148. return GIcon(m_filetype_image_icon.bitmap_for_size(16), *entry.thumbnail);
  149. }
  150. return m_file_icon;
  151. }
  152. static String timestamp_string(time_t timestamp)
  153. {
  154. auto* tm = localtime(&timestamp);
  155. return String::format("%4u-%02u-%02u %02u:%02u:%02u",
  156. tm->tm_year + 1900,
  157. tm->tm_mon + 1,
  158. tm->tm_mday,
  159. tm->tm_hour,
  160. tm->tm_min,
  161. tm->tm_sec);
  162. }
  163. static String permission_string(mode_t mode)
  164. {
  165. StringBuilder builder;
  166. if (S_ISDIR(mode))
  167. builder.append("d");
  168. else if (S_ISLNK(mode))
  169. builder.append("l");
  170. else if (S_ISBLK(mode))
  171. builder.append("b");
  172. else if (S_ISCHR(mode))
  173. builder.append("c");
  174. else if (S_ISFIFO(mode))
  175. builder.append("f");
  176. else if (S_ISSOCK(mode))
  177. builder.append("s");
  178. else if (S_ISREG(mode))
  179. builder.append("-");
  180. else
  181. builder.append("?");
  182. builder.appendf("%c%c%c%c%c%c%c%c",
  183. mode & S_IRUSR ? 'r' : '-',
  184. mode & S_IWUSR ? 'w' : '-',
  185. mode & S_ISUID ? 's' : (mode & S_IXUSR ? 'x' : '-'),
  186. mode & S_IRGRP ? 'r' : '-',
  187. mode & S_IWGRP ? 'w' : '-',
  188. mode & S_ISGID ? 's' : (mode & S_IXGRP ? 'x' : '-'),
  189. mode & S_IROTH ? 'r' : '-',
  190. mode & S_IWOTH ? 'w' : '-');
  191. if (mode & S_ISVTX)
  192. builder.append("t");
  193. else
  194. builder.appendf("%c", mode & S_IXOTH ? 'x' : '-');
  195. return builder.to_string();
  196. }
  197. String GDirectoryModel::name_for_uid(uid_t uid) const
  198. {
  199. auto it = m_user_names.find(uid);
  200. if (it == m_user_names.end())
  201. return String::number(uid);
  202. return (*it).value;
  203. }
  204. String GDirectoryModel::name_for_gid(uid_t gid) const
  205. {
  206. auto it = m_user_names.find(gid);
  207. if (it == m_user_names.end())
  208. return String::number(gid);
  209. return (*it).value;
  210. }
  211. GVariant GDirectoryModel::data(const GModelIndex& index, Role role) const
  212. {
  213. ASSERT(is_valid(index));
  214. auto& entry = this->entry(index.row());
  215. if (role == Role::Custom) {
  216. ASSERT(index.column() == Column::Name);
  217. return entry.full_path(*this);
  218. }
  219. if (role == Role::Sort) {
  220. switch (index.column()) {
  221. case Column::Icon:
  222. return entry.is_directory() ? 0 : 1;
  223. case Column::Name:
  224. return entry.name;
  225. case Column::Size:
  226. return (int)entry.size;
  227. case Column::Owner:
  228. return name_for_uid(entry.uid);
  229. case Column::Group:
  230. return name_for_gid(entry.gid);
  231. case Column::Permissions:
  232. return permission_string(entry.mode);
  233. case Column::ModificationTime:
  234. return entry.mtime;
  235. case Column::Inode:
  236. return (int)entry.inode;
  237. }
  238. ASSERT_NOT_REACHED();
  239. }
  240. if (role == Role::Display) {
  241. switch (index.column()) {
  242. case Column::Icon:
  243. return icon_for(entry);
  244. case Column::Name:
  245. return entry.name;
  246. case Column::Size:
  247. return (int)entry.size;
  248. case Column::Owner:
  249. return name_for_uid(entry.uid);
  250. case Column::Group:
  251. return name_for_gid(entry.gid);
  252. case Column::Permissions:
  253. return permission_string(entry.mode);
  254. case Column::ModificationTime:
  255. return timestamp_string(entry.mtime);
  256. case Column::Inode:
  257. return (int)entry.inode;
  258. }
  259. }
  260. if (role == Role::Icon) {
  261. return icon_for(entry);
  262. }
  263. return {};
  264. }
  265. void GDirectoryModel::update()
  266. {
  267. CDirIterator di(m_path, CDirIterator::SkipDots);
  268. if (di.has_error()) {
  269. fprintf(stderr, "CDirIterator: %s\n", di.error_string());
  270. exit(1);
  271. }
  272. m_directories.clear();
  273. m_files.clear();
  274. m_bytes_in_files = 0;
  275. while (di.has_next()) {
  276. String name = di.next_path();
  277. Entry entry;
  278. entry.name = name;
  279. struct stat st;
  280. int rc = lstat(String::format("%s/%s", m_path.characters(), name.characters()).characters(), &st);
  281. if (rc < 0) {
  282. perror("lstat");
  283. continue;
  284. }
  285. entry.size = st.st_size;
  286. entry.mode = st.st_mode;
  287. entry.uid = st.st_uid;
  288. entry.gid = st.st_gid;
  289. entry.inode = st.st_ino;
  290. entry.mtime = st.st_mtime;
  291. auto& entries = S_ISDIR(st.st_mode) ? m_directories : m_files;
  292. entries.append(move(entry));
  293. m_bytes_in_files += st.st_size;
  294. }
  295. did_update();
  296. }
  297. void GDirectoryModel::open(const StringView& a_path)
  298. {
  299. auto path = canonicalized_path(a_path);
  300. if (m_path == path)
  301. return;
  302. DIR* dirp = opendir(path.characters());
  303. if (!dirp)
  304. return;
  305. closedir(dirp);
  306. if (m_notifier)
  307. close(m_notifier->fd());
  308. m_path = path;
  309. int watch_fd = watch_file(path.characters(), path.length());
  310. if (watch_fd < 0) {
  311. perror("watch_file");
  312. ASSERT_NOT_REACHED();
  313. }
  314. m_notifier = CNotifier::construct(watch_fd, CNotifier::Event::Read);
  315. m_notifier->on_ready_to_read = [this] {
  316. update();
  317. char buffer[32];
  318. int rc = read(m_notifier->fd(), buffer, sizeof(buffer));
  319. ASSERT(rc >= 0);
  320. };
  321. if (on_path_change)
  322. on_path_change();
  323. update();
  324. }