DirectoryTableModel.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "DirectoryTableModel.h"
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <grp.h>
  6. #include <pwd.h>
  7. #include <AK/FileSystemPath.h>
  8. #include <AK/StringBuilder.h>
  9. DirectoryTableModel::DirectoryTableModel()
  10. {
  11. m_directory_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/folder16.rgb", { 16, 16 });
  12. m_file_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/file16.rgb", { 16, 16 });
  13. m_symlink_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/link16.rgb", { 16, 16 });
  14. m_socket_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/socket16.rgb", { 16, 16 });
  15. m_executable_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/executable16.rgb", { 16, 16 });
  16. setpwent();
  17. while (auto* passwd = getpwent())
  18. m_user_names.set(passwd->pw_uid, passwd->pw_name);
  19. endpwent();
  20. setgrent();
  21. while (auto* group = getgrent())
  22. m_group_names.set(group->gr_gid, group->gr_name);
  23. endgrent();
  24. }
  25. DirectoryTableModel::~DirectoryTableModel()
  26. {
  27. }
  28. int DirectoryTableModel::row_count() const
  29. {
  30. return m_directories.size() + m_files.size();
  31. }
  32. int DirectoryTableModel::column_count() const
  33. {
  34. return Column::__Count;
  35. }
  36. String DirectoryTableModel::column_name(int column) const
  37. {
  38. switch (column) {
  39. case Column::Icon: return "";
  40. case Column::Name: return "Name";
  41. case Column::Size: return "Size";
  42. case Column::Owner: return "Owner";
  43. case Column::Group: return "Group";
  44. case Column::Permissions: return "Mode";
  45. case Column::Inode: return "Inode";
  46. }
  47. ASSERT_NOT_REACHED();
  48. }
  49. GTableModel::ColumnMetadata DirectoryTableModel::column_metadata(int column) const
  50. {
  51. switch (column) {
  52. case Column::Icon: return { 16, TextAlignment::Center };
  53. case Column::Name: return { 120, TextAlignment::CenterLeft };
  54. case Column::Size: return { 80, TextAlignment::CenterRight };
  55. case Column::Owner: return { 50, TextAlignment::CenterLeft };
  56. case Column::Group: return { 50, TextAlignment::CenterLeft };
  57. case Column::Permissions: return { 80, TextAlignment::CenterLeft };
  58. case Column::Inode: return { 80, TextAlignment::CenterRight };
  59. }
  60. ASSERT_NOT_REACHED();
  61. }
  62. const GraphicsBitmap& DirectoryTableModel::icon_for(const Entry& entry) const
  63. {
  64. if (S_ISDIR(entry.mode))
  65. return *m_directory_icon;
  66. if (S_ISLNK(entry.mode))
  67. return *m_symlink_icon;
  68. if (S_ISSOCK(entry.mode))
  69. return *m_socket_icon;
  70. if (entry.mode & S_IXUSR)
  71. return *m_executable_icon;
  72. return *m_file_icon;
  73. }
  74. static String permission_string(mode_t mode)
  75. {
  76. StringBuilder builder;
  77. if (S_ISDIR(mode))
  78. builder.append("d");
  79. else if (S_ISLNK(mode))
  80. builder.append("l");
  81. else if (S_ISBLK(mode))
  82. builder.append("b");
  83. else if (S_ISCHR(mode))
  84. builder.append("c");
  85. else if (S_ISFIFO(mode))
  86. builder.append("f");
  87. else if (S_ISSOCK(mode))
  88. builder.append("s");
  89. else if (S_ISREG(mode))
  90. builder.append("-");
  91. else
  92. builder.append("?");
  93. builder.appendf("%c%c%c%c%c%c%c%c",
  94. mode & S_IRUSR ? 'r' : '-',
  95. mode & S_IWUSR ? 'w' : '-',
  96. mode & S_ISUID ? 's' : (mode & S_IXUSR ? 'x' : '-'),
  97. mode & S_IRGRP ? 'r' : '-',
  98. mode & S_IWGRP ? 'w' : '-',
  99. mode & S_ISGID ? 's' : (mode & S_IXGRP ? 'x' : '-'),
  100. mode & S_IROTH ? 'r' : '-',
  101. mode & S_IWOTH ? 'w' : '-'
  102. );
  103. if (mode & S_ISVTX)
  104. builder.append("t");
  105. else
  106. builder.appendf("%c", mode & S_IXOTH ? 'x' : '-');
  107. return builder.to_string();
  108. }
  109. String DirectoryTableModel::name_for_uid(uid_t uid) const
  110. {
  111. auto it = m_user_names.find(uid);
  112. if (it == m_user_names.end())
  113. return String::format("%u", uid);
  114. return (*it).value;
  115. }
  116. String DirectoryTableModel::name_for_gid(uid_t gid) const
  117. {
  118. auto it = m_user_names.find(gid);
  119. if (it == m_user_names.end())
  120. return String::format("%u", gid);
  121. return (*it).value;
  122. }
  123. GVariant DirectoryTableModel::data(const GModelIndex& index) const
  124. {
  125. auto& entry = this->entry(index.row());
  126. switch (index.column()) {
  127. case Column::Icon: return icon_for(entry);
  128. case Column::Name: return entry.name;
  129. case Column::Size: return (int)entry.size;
  130. case Column::Owner: return name_for_uid(entry.uid);
  131. case Column::Group: return name_for_gid(entry.gid);
  132. case Column::Permissions: return permission_string(entry.mode);
  133. case Column::Inode: return (int)entry.inode;
  134. }
  135. ASSERT_NOT_REACHED();
  136. }
  137. void DirectoryTableModel::update()
  138. {
  139. DIR* dirp = opendir(m_path.characters());
  140. if (!dirp) {
  141. perror("opendir");
  142. exit(1);
  143. }
  144. m_directories.clear();
  145. m_files.clear();
  146. m_bytes_in_files = 0;
  147. while (auto* de = readdir(dirp)) {
  148. Entry entry;
  149. entry.name = de->d_name;
  150. struct stat st;
  151. int rc = lstat(String::format("%s/%s", m_path.characters(), de->d_name).characters(), &st);
  152. if (rc < 0) {
  153. perror("lstat");
  154. continue;
  155. }
  156. entry.size = st.st_size;
  157. entry.mode = st.st_mode;
  158. entry.uid = st.st_uid;
  159. entry.gid = st.st_gid;
  160. entry.inode = st.st_ino;
  161. auto& entries = S_ISDIR(st.st_mode) ? m_directories : m_files;
  162. entries.append(move(entry));
  163. if (S_ISREG(entry.mode))
  164. m_bytes_in_files += st.st_size;
  165. }
  166. closedir(dirp);
  167. did_update();
  168. }
  169. void DirectoryTableModel::open(const String& a_path)
  170. {
  171. FileSystemPath canonical_path(a_path);
  172. auto path = canonical_path.string();
  173. if (m_path == path)
  174. return;
  175. DIR* dirp = opendir(path.characters());
  176. if (!dirp)
  177. return;
  178. closedir(dirp);
  179. m_path = path;
  180. update();
  181. set_selected_index({ 0, 0 });
  182. }
  183. void DirectoryTableModel::activate(const GModelIndex& index)
  184. {
  185. auto& entry = this->entry(index.row());
  186. FileSystemPath path(String::format("%s/%s", m_path.characters(), entry.name.characters()));
  187. if (entry.is_directory()) {
  188. open(path.string());
  189. return;
  190. }
  191. if (entry.is_executable()) {
  192. if (fork() == 0) {
  193. int rc = execl(path.string().characters(), path.string().characters(), nullptr);
  194. if (rc < 0)
  195. perror("exec");
  196. ASSERT_NOT_REACHED();
  197. }
  198. return;
  199. }
  200. if (fork() == 0) {
  201. int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.string().characters(), nullptr);
  202. if (rc < 0)
  203. perror("exec");
  204. ASSERT_NOT_REACHED();
  205. }
  206. return;
  207. }