GDirectoryModel.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <LibDraw/GraphicsBitmap.h>
  7. #include <LibGUI/GPainter.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 CLockable<HashMap<String, RefPtr<GraphicsBitmap>>>& thumbnail_cache()
  15. {
  16. static CLockable<HashMap<String, RefPtr<GraphicsBitmap>>>* s_map;
  17. if (!s_map)
  18. s_map = new CLockable<HashMap<String, RefPtr<GraphicsBitmap>>>();
  19. return *s_map;
  20. }
  21. int thumbnail_thread(void* model_ptr)
  22. {
  23. auto& model = *(GDirectoryModel*)model_ptr;
  24. for (;;) {
  25. sleep(1);
  26. Vector<String> to_generate;
  27. {
  28. LOCKER(thumbnail_cache().lock());
  29. to_generate.ensure_capacity(thumbnail_cache().resource().size());
  30. for (auto& it : thumbnail_cache().resource()) {
  31. if (it.value)
  32. continue;
  33. to_generate.append(it.key);
  34. }
  35. }
  36. if (to_generate.is_empty())
  37. continue;
  38. for (int i = 0; i < to_generate.size(); ++i) {
  39. auto& path = to_generate[i];
  40. auto png_bitmap = GraphicsBitmap::load_from_file(path);
  41. if (!png_bitmap)
  42. continue;
  43. auto thumbnail = GraphicsBitmap::create(png_bitmap->format(), { 32, 32 });
  44. Painter painter(*thumbnail);
  45. painter.draw_scaled_bitmap(thumbnail->rect(), *png_bitmap, png_bitmap->rect());
  46. {
  47. LOCKER(thumbnail_cache().lock());
  48. thumbnail_cache().resource().set(path, move(thumbnail));
  49. }
  50. if (model.on_thumbnail_progress)
  51. model.on_thumbnail_progress(i + 1, to_generate.size());
  52. model.did_update();
  53. }
  54. }
  55. }
  56. GDirectoryModel::GDirectoryModel()
  57. {
  58. create_thread(thumbnail_thread, this);
  59. m_directory_icon = GIcon::default_icon("filetype-folder");
  60. m_file_icon = GIcon::default_icon("filetype-unknown");
  61. m_symlink_icon = GIcon::default_icon("filetype-symlink");
  62. m_socket_icon = GIcon::default_icon("filetype-socket");
  63. m_executable_icon = GIcon::default_icon("filetype-executable");
  64. m_filetype_image_icon = GIcon::default_icon("filetype-image");
  65. setpwent();
  66. while (auto* passwd = getpwent())
  67. m_user_names.set(passwd->pw_uid, passwd->pw_name);
  68. endpwent();
  69. setgrent();
  70. while (auto* group = getgrent())
  71. m_group_names.set(group->gr_gid, group->gr_name);
  72. endgrent();
  73. }
  74. GDirectoryModel::~GDirectoryModel()
  75. {
  76. }
  77. int GDirectoryModel::row_count(const GModelIndex&) const
  78. {
  79. return m_directories.size() + m_files.size();
  80. }
  81. int GDirectoryModel::column_count(const GModelIndex&) const
  82. {
  83. return Column::__Count;
  84. }
  85. String GDirectoryModel::column_name(int column) const
  86. {
  87. switch (column) {
  88. case Column::Icon:
  89. return "";
  90. case Column::Name:
  91. return "Name";
  92. case Column::Size:
  93. return "Size";
  94. case Column::Owner:
  95. return "Owner";
  96. case Column::Group:
  97. return "Group";
  98. case Column::Permissions:
  99. return "Mode";
  100. case Column::ModificationTime:
  101. return "Modified";
  102. case Column::Inode:
  103. return "Inode";
  104. }
  105. ASSERT_NOT_REACHED();
  106. }
  107. GModel::ColumnMetadata GDirectoryModel::column_metadata(int column) const
  108. {
  109. switch (column) {
  110. case Column::Icon:
  111. return { 16, TextAlignment::Center, nullptr, GModel::ColumnMetadata::Sortable::False };
  112. case Column::Name:
  113. return { 120, TextAlignment::CenterLeft };
  114. case Column::Size:
  115. return { 80, TextAlignment::CenterRight };
  116. case Column::Owner:
  117. return { 50, TextAlignment::CenterLeft };
  118. case Column::Group:
  119. return { 50, TextAlignment::CenterLeft };
  120. case Column::ModificationTime:
  121. return { 110, TextAlignment::CenterLeft };
  122. case Column::Permissions:
  123. return { 65, TextAlignment::CenterLeft };
  124. case Column::Inode:
  125. return { 60, TextAlignment::CenterRight };
  126. }
  127. ASSERT_NOT_REACHED();
  128. }
  129. GIcon GDirectoryModel::icon_for(const Entry& entry) const
  130. {
  131. if (S_ISDIR(entry.mode))
  132. return m_directory_icon;
  133. if (S_ISLNK(entry.mode))
  134. return m_symlink_icon;
  135. if (S_ISSOCK(entry.mode))
  136. return m_socket_icon;
  137. if (entry.mode & S_IXUSR)
  138. return m_executable_icon;
  139. if (entry.name.to_lowercase().ends_with(".png")) {
  140. if (!entry.thumbnail) {
  141. auto path = entry.full_path(*this);
  142. LOCKER(thumbnail_cache().lock());
  143. auto it = thumbnail_cache().resource().find(path);
  144. if (it != thumbnail_cache().resource().end()) {
  145. entry.thumbnail = (*it).value;
  146. } else {
  147. thumbnail_cache().resource().set(path, nullptr);
  148. }
  149. if (!entry.thumbnail)
  150. return m_filetype_image_icon;
  151. }
  152. return GIcon(m_filetype_image_icon.bitmap_for_size(16), *entry.thumbnail);
  153. }
  154. return m_file_icon;
  155. }
  156. static String timestamp_string(time_t timestamp)
  157. {
  158. auto* tm = localtime(&timestamp);
  159. return String::format("%4u-%02u-%02u %02u:%02u:%02u",
  160. tm->tm_year + 1900,
  161. tm->tm_mon + 1,
  162. tm->tm_mday,
  163. tm->tm_hour,
  164. tm->tm_min,
  165. tm->tm_sec);
  166. }
  167. static String permission_string(mode_t mode)
  168. {
  169. StringBuilder builder;
  170. if (S_ISDIR(mode))
  171. builder.append("d");
  172. else if (S_ISLNK(mode))
  173. builder.append("l");
  174. else if (S_ISBLK(mode))
  175. builder.append("b");
  176. else if (S_ISCHR(mode))
  177. builder.append("c");
  178. else if (S_ISFIFO(mode))
  179. builder.append("f");
  180. else if (S_ISSOCK(mode))
  181. builder.append("s");
  182. else if (S_ISREG(mode))
  183. builder.append("-");
  184. else
  185. builder.append("?");
  186. builder.appendf("%c%c%c%c%c%c%c%c",
  187. mode & S_IRUSR ? 'r' : '-',
  188. mode & S_IWUSR ? 'w' : '-',
  189. mode & S_ISUID ? 's' : (mode & S_IXUSR ? 'x' : '-'),
  190. mode & S_IRGRP ? 'r' : '-',
  191. mode & S_IWGRP ? 'w' : '-',
  192. mode & S_ISGID ? 's' : (mode & S_IXGRP ? 'x' : '-'),
  193. mode & S_IROTH ? 'r' : '-',
  194. mode & S_IWOTH ? 'w' : '-');
  195. if (mode & S_ISVTX)
  196. builder.append("t");
  197. else
  198. builder.appendf("%c", mode & S_IXOTH ? 'x' : '-');
  199. return builder.to_string();
  200. }
  201. String GDirectoryModel::name_for_uid(uid_t uid) const
  202. {
  203. auto it = m_user_names.find(uid);
  204. if (it == m_user_names.end())
  205. return String::number(uid);
  206. return (*it).value;
  207. }
  208. String GDirectoryModel::name_for_gid(uid_t gid) const
  209. {
  210. auto it = m_user_names.find(gid);
  211. if (it == m_user_names.end())
  212. return String::number(gid);
  213. return (*it).value;
  214. }
  215. GVariant GDirectoryModel::data(const GModelIndex& index, Role role) const
  216. {
  217. ASSERT(is_valid(index));
  218. auto& entry = this->entry(index.row());
  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. if (S_ISREG(entry.mode))
  294. m_bytes_in_files += st.st_size;
  295. }
  296. did_update();
  297. }
  298. void GDirectoryModel::open(const StringView& a_path)
  299. {
  300. auto path = canonicalized_path(a_path);
  301. if (m_path == path)
  302. return;
  303. DIR* dirp = opendir(path.characters());
  304. if (!dirp)
  305. return;
  306. closedir(dirp);
  307. if (m_notifier)
  308. close(m_notifier->fd());
  309. m_path = path;
  310. int watch_fd = watch_file(path.characters(), path.length());
  311. if (watch_fd < 0) {
  312. perror("watch_file");
  313. ASSERT_NOT_REACHED();
  314. }
  315. m_notifier = make<CNotifier>(watch_fd, CNotifier::Event::Read);
  316. m_notifier->on_ready_to_read = [this] {
  317. update();
  318. char buffer[32];
  319. int rc = read(m_notifier->fd(), buffer, sizeof(buffer));
  320. ASSERT(rc >= 0);
  321. };
  322. update();
  323. set_selected_index(index(0, 0));
  324. }