GDirectoryModel.cpp 9.7 KB

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