Graphical package manager for pacman based on pamac 5.x.x
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.1KB

  1. /*
  2. * pamac-vala
  3. *
  4. * Copyright (C) 2014-2016 Guillaume Benoit <guillaume@manjaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a get of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. namespace Pamac {
  20. class PackagesModel : Object, Gtk.TreeModel {
  21. private Alpm.List<unowned Alpm.Package?>? pkgs;
  22. private ManagerWindow manager_window;
  23. public PackagesModel (owned Alpm.List<unowned Alpm.Package?>? pkgs, ManagerWindow manager_window) {
  24. this.manager_window = manager_window;
  25. this.pkgs = (owned) pkgs;
  26. }
  27. // TreeModel interface
  28. public Type get_column_type (int index) {
  29. switch (index) {
  30. case 0: // name
  31. case 2: // version
  32. case 3: // repo
  33. case 4: // installed size
  34. return typeof (string);
  35. case 1: // icon
  36. return typeof (Gdk.Pixbuf);
  37. default:
  38. return Type.INVALID;
  39. }
  40. }
  41. Gtk.TreeModelFlags get_flags () {
  42. return Gtk.TreeModelFlags.LIST_ONLY | Gtk.TreeModelFlags.ITERS_PERSIST;
  43. }
  44. void get_value (Gtk.TreeIter iter, int column, out Value val) {
  45. unowned Alpm.Package? pkg = pkgs.nth (iter.stamp).data;
  46. switch (column) {
  47. case 0:
  48. val = Value (typeof (string));
  49. if (pkg == null) {
  50. val.set_string (dgettext (null, "No package found"));
  51. } else {
  52. val.set_string (pkg.name);
  53. }
  54. break;
  55. case 1:
  56. val = Value (typeof (Object));
  57. if (pkg != null) {
  58. if (pkg.origin == Alpm.Package.From.LOCALDB) {
  59. if (manager_window.transaction.alpm_utils.get_holdpkgs ().find_custom (pkg.name, strcmp) != null) {
  60. val.set_object (manager_window.locked_icon);
  61. } else if (manager_window.transaction.to_add.contains (pkg.name)) {
  62. val.set_object (manager_window.to_reinstall_icon);
  63. } else if (manager_window.transaction.to_remove.contains (pkg.name)) {
  64. val.set_object (manager_window.to_remove_icon);
  65. } else {
  66. val.set_object (manager_window.installed_icon);
  67. }
  68. } else if (manager_window.transaction.to_add.contains (pkg.name)) {
  69. val.set_object (manager_window.to_install_icon);
  70. } else {
  71. val.set_object (manager_window.uninstalled_icon);
  72. }
  73. }
  74. break;
  75. case 2:
  76. val = Value (typeof (string));
  77. if (pkg != null) {
  78. val.set_string (pkg.version);
  79. }
  80. break;
  81. case 3:
  82. val = Value (typeof (string));
  83. if (pkg != null) {
  84. val.set_string (pkg.db.name);
  85. }
  86. break;
  87. case 4:
  88. val = Value (typeof (string));
  89. if (pkg != null) {
  90. val.set_string (format_size (pkg.isize));
  91. }
  92. break;
  93. default:
  94. val = Value (Type.INVALID);
  95. break;
  96. }
  97. }
  98. bool get_iter (out Gtk.TreeIter iter, Gtk.TreePath path) {
  99. if (path.get_depth () == 1) {
  100. int pos = path.get_indices ()[0];
  101. // return a valid iter for pos == 0 to display "No package found"
  102. if (pos < pkgs.length || pos == 0) {
  103. iter = Gtk.TreeIter ();
  104. iter.stamp = pos;
  105. return true;
  106. }
  107. }
  108. return invalid_iter (out iter);
  109. }
  110. int get_n_columns () {
  111. // name, icon, version, repo, isize
  112. return 5;
  113. }
  114. Gtk.TreePath? get_path (Gtk.TreeIter iter) {
  115. int pos = iter.stamp;
  116. // return a valid path for pos == 0 to display "No package found"
  117. if (pos < pkgs.length || pos == 0) {
  118. return new Gtk.TreePath.from_indices (pos);
  119. }
  120. return null;
  121. }
  122. int iter_n_children (Gtk.TreeIter? iter) {
  123. return 0;
  124. }
  125. bool iter_next (ref Gtk.TreeIter iter) {
  126. int pos = (iter.stamp) + 1;
  127. if (pos >= pkgs.length) {
  128. return false;
  129. }
  130. iter.stamp = pos;
  131. return true;
  132. }
  133. bool iter_previous (ref Gtk.TreeIter iter) {
  134. int pos = iter.stamp;
  135. if (pos >= 0) {
  136. return false;
  137. }
  138. iter.stamp = (--pos);
  139. return true;
  140. }
  141. bool iter_nth_child (out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n) {
  142. return invalid_iter (out iter);
  143. }
  144. bool iter_children (out Gtk.TreeIter iter, Gtk.TreeIter? parent) {
  145. return invalid_iter (out iter);
  146. }
  147. bool iter_has_child (Gtk.TreeIter iter) {
  148. return false;
  149. }
  150. bool iter_parent (out Gtk.TreeIter iter, Gtk.TreeIter child) {
  151. return invalid_iter (out iter);
  152. }
  153. bool invalid_iter (out Gtk.TreeIter iter) {
  154. iter = Gtk.TreeIter ();
  155. iter.stamp = -1;
  156. return false;
  157. }
  158. // custom get pkg function
  159. public unowned Alpm.Package? get_pkg_at_path (Gtk.TreePath path) {
  160. int pos = path.get_indices ()[0];
  161. if (pos < pkgs.length) {
  162. return pkgs.nth (pos).data;
  163. }
  164. return null;
  165. }
  166. // custom sort functions
  167. public void sort_by_name (Gtk.SortType order) {
  168. pkgs.sort ((Alpm.List.CompareFunc) alpm_pkg_compare_name);
  169. if (order == Gtk.SortType.DESCENDING) {
  170. pkgs.reverse ();
  171. }
  172. manager_window.packages_name_column.sort_order = order;
  173. manager_window.packages_state_column.sort_indicator = false;
  174. manager_window.packages_name_column.sort_indicator = true;
  175. manager_window.packages_version_column.sort_indicator = false;
  176. manager_window.packages_repo_column.sort_indicator = false;
  177. manager_window.packages_size_column.sort_indicator = false;
  178. manager_window.sortinfo.column_number = 0;
  179. manager_window.sortinfo.sort_type = order;
  180. }
  181. public void sort_by_state (Gtk.SortType order) {
  182. pkgs.sort ((Alpm.List.CompareFunc) alpm_pkg_compare_origin);
  183. if (order == Gtk.SortType.DESCENDING) {
  184. pkgs.reverse ();
  185. }
  186. manager_window.packages_state_column.sort_order = order;
  187. manager_window.packages_state_column.sort_indicator = true;
  188. manager_window.packages_name_column.sort_indicator = false;
  189. manager_window.packages_version_column.sort_indicator = false;
  190. manager_window.packages_repo_column.sort_indicator = false;
  191. manager_window.packages_size_column.sort_indicator = false;
  192. manager_window.sortinfo.column_number = 1;
  193. manager_window.sortinfo.sort_type = order;
  194. }
  195. public void sort_by_version (Gtk.SortType order) {
  196. pkgs.sort ((Alpm.List.CompareFunc) alpm_pkg_compare_version);
  197. if (order == Gtk.SortType.DESCENDING) {
  198. pkgs.reverse ();
  199. }
  200. manager_window.packages_version_column.sort_order = order;
  201. manager_window.packages_state_column.sort_indicator = false;
  202. manager_window.packages_name_column.sort_indicator = false;
  203. manager_window.packages_version_column.sort_indicator = true;
  204. manager_window.packages_repo_column.sort_indicator = false;
  205. manager_window.packages_size_column.sort_indicator = false;
  206. manager_window.sortinfo.column_number = 2;
  207. manager_window.sortinfo.sort_type = order;
  208. }
  209. public void sort_by_repo (Gtk.SortType order) {
  210. pkgs.sort ((Alpm.List.CompareFunc) alpm_pkg_compare_db_name);
  211. if (order == Gtk.SortType.DESCENDING) {
  212. pkgs.reverse ();
  213. }
  214. manager_window.packages_repo_column.sort_order = order;
  215. manager_window.packages_state_column.sort_indicator = false;
  216. manager_window.packages_name_column.sort_indicator = false;
  217. manager_window.packages_version_column.sort_indicator = false;
  218. manager_window.packages_repo_column.sort_indicator = true;
  219. manager_window.packages_size_column.sort_indicator = false;
  220. manager_window.sortinfo.column_number = 3;
  221. manager_window.sortinfo.sort_type = order;
  222. }
  223. public void sort_by_size (Gtk.SortType order) {
  224. pkgs.sort ((Alpm.List.CompareFunc) alpm_pkg_compare_installed_size);
  225. if (order == Gtk.SortType.DESCENDING) {
  226. pkgs.reverse ();
  227. }
  228. manager_window.packages_size_column.sort_order = order;
  229. manager_window.packages_state_column.sort_indicator = false;
  230. manager_window.packages_name_column.sort_indicator = false;
  231. manager_window.packages_version_column.sort_indicator = false;
  232. manager_window.packages_repo_column.sort_indicator = false;
  233. manager_window.packages_size_column.sort_indicator = true;
  234. manager_window.sortinfo.column_number = 4;
  235. manager_window.sortinfo.sort_type = order;
  236. }
  237. }
  238. }