GDirectoryModel.cpp 10 KB

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