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.

272 lines
8.5KB

  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. Pamac.ManagerWindow manager_window;
  20. // custom sort functions
  21. int aur_compare_name (Json.Object pkg_a, Json.Object pkg_b) {
  22. return strcmp (pkg_a.get_string_member ("Name"), pkg_b.get_string_member ("Name"));
  23. }
  24. int aur_compare_state (Json.Object pkg_a, Json.Object pkg_b) {
  25. unowned Alpm.Package? alpm_pkg_a = manager_window.transaction.alpm_utils.get_installed_pkg (pkg_a.get_string_member ("Name"));
  26. unowned Alpm.Package? alpm_pkg_b = manager_window.transaction.alpm_utils.get_installed_pkg (pkg_b.get_string_member ("Name"));
  27. if (pkg_a != null) {
  28. if (pkg_b != null) {
  29. return (int) (alpm_pkg_a.origin > alpm_pkg_b.origin) - (int) (alpm_pkg_a.origin < alpm_pkg_b.origin);
  30. } else {
  31. return 1;
  32. }
  33. } else {
  34. if (pkg_b != null) {
  35. return -1;
  36. } else {
  37. return 0;
  38. }
  39. }
  40. }
  41. int aur_compare_version (Json.Object pkg_a, Json.Object pkg_b) {
  42. return Alpm.pkg_vercmp (pkg_a.get_string_member ("Version"), pkg_b.get_string_member ("Version"));
  43. }
  44. int aur_compare_votes (Json.Object pkg_a, Json.Object pkg_b) {
  45. return (int) (pkg_a.get_int_member ("NumVotes") > pkg_b.get_int_member ("NumVotes")) - (int) (pkg_a.get_int_member ("NumVotes") < pkg_b.get_int_member ("NumVotes"));
  46. }
  47. namespace Pamac {
  48. class AURModel : Object, Gtk.TreeModel {
  49. private Json.Array pkgs_infos;
  50. private GLib.List<Json.Object?> pkgs;
  51. public AURModel (Json.Array? pkgs_infos, ManagerWindow _manager_window) {
  52. manager_window = _manager_window;
  53. this.pkgs_infos = pkgs_infos;
  54. pkgs = new GLib.List<Json.Object?> ();
  55. if (pkgs_infos != null) {
  56. pkgs_infos.foreach_element ((array, index, node) => {
  57. pkgs.append (node.get_object ());
  58. });
  59. }
  60. }
  61. // TreeModel interface
  62. Type get_column_type (int index) {
  63. switch (index) {
  64. case 0: // name
  65. case 2: // version
  66. return typeof (string);
  67. case 3: // votes
  68. return typeof (int64);
  69. case 1: // icon
  70. return typeof (Gdk.Pixbuf);
  71. default:
  72. return Type.INVALID;
  73. }
  74. }
  75. Gtk.TreeModelFlags get_flags () {
  76. return Gtk.TreeModelFlags.LIST_ONLY | Gtk.TreeModelFlags.ITERS_PERSIST;
  77. }
  78. void get_value (Gtk.TreeIter iter, int column, out Value val) {
  79. unowned Json.Object? pkg_info = pkgs.nth_data (iter.stamp);
  80. switch (column) {
  81. case 0:
  82. val = Value (typeof (string));
  83. if (pkg_info == null) {
  84. val.set_string (dgettext (null, "No package found"));
  85. } else {
  86. val.set_string (pkg_info.get_string_member ("Name"));
  87. }
  88. break;
  89. case 1:
  90. val = Value (typeof (Object));
  91. if (pkg_info != null) {
  92. unowned Alpm.Package? pkg = manager_window.transaction.alpm_utils.get_installed_pkg (pkg_info.get_string_member ("Name"));
  93. if (pkg != null) {
  94. if (manager_window.transaction.alpm_utils.get_holdpkgs ().find_custom (pkg.name, strcmp) != null) {
  95. val.set_object (manager_window.locked_icon);
  96. } else if (manager_window.transaction.to_add.contains (pkg.name)) {
  97. val.set_object (manager_window.to_reinstall_icon);
  98. } else if (manager_window.transaction.to_remove.contains (pkg.name)) {
  99. val.set_object (manager_window.to_remove_icon);
  100. } else {
  101. val.set_object (manager_window.installed_icon);
  102. }
  103. } else if (manager_window.transaction.to_build.contains (pkg_info.get_string_member ("Name"))) {
  104. val.set_object (manager_window.to_install_icon);
  105. } else {
  106. val.set_object (manager_window.uninstalled_icon);
  107. }
  108. }
  109. break;
  110. case 2:
  111. val = Value (typeof (string));
  112. if (pkg_info != null) {
  113. unowned Alpm.Package? pkg = manager_window.transaction.alpm_utils.get_installed_pkg (pkg_info.get_string_member ("Name"));
  114. if (pkg != null) {
  115. val.set_string (pkg.version);
  116. } else {
  117. val.set_string (pkg_info.get_string_member ("Version"));
  118. }
  119. }
  120. break;
  121. case 3:
  122. if (pkg_info != null) {
  123. val = Value (typeof (int64));
  124. val.set_int64 (pkg_info.get_int_member ("NumVotes"));
  125. } else {
  126. // if pkg_info is null, set val to an empty string to not display "0"
  127. val = Value (typeof (string));
  128. }
  129. break;
  130. default:
  131. val = Value (Type.INVALID);
  132. break;
  133. }
  134. }
  135. bool get_iter (out Gtk.TreeIter iter, Gtk.TreePath path) {;
  136. if (path.get_depth () != 1) {
  137. return invalid_iter (out iter);
  138. }
  139. iter = Gtk.TreeIter ();
  140. int pos = path.get_indices ()[0];
  141. iter.stamp = pos;
  142. return true;
  143. }
  144. int get_n_columns () {
  145. // name, icon, version, votes
  146. return 4;
  147. }
  148. Gtk.TreePath? get_path (Gtk.TreeIter iter) {
  149. return new Gtk.TreePath.from_indices (iter.stamp);
  150. }
  151. int iter_n_children (Gtk.TreeIter? iter) {
  152. return 0;
  153. }
  154. bool iter_next (ref Gtk.TreeIter iter) {
  155. int pos = (iter.stamp) + 1;
  156. if (pos >= pkgs.length ()) {
  157. return false;
  158. }
  159. iter.stamp = pos;
  160. return true;
  161. }
  162. bool iter_previous (ref Gtk.TreeIter iter) {
  163. int pos = iter.stamp;
  164. if (pos >= 0) {
  165. return false;
  166. }
  167. iter.stamp = (--pos);
  168. return true;
  169. }
  170. bool iter_nth_child (out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n) {
  171. return invalid_iter (out iter);
  172. }
  173. bool iter_children (out Gtk.TreeIter iter, Gtk.TreeIter? parent) {
  174. return invalid_iter (out iter);
  175. }
  176. bool iter_has_child (Gtk.TreeIter iter) {
  177. return false;
  178. }
  179. bool iter_parent (out Gtk.TreeIter iter, Gtk.TreeIter child) {
  180. return invalid_iter (out iter);
  181. }
  182. bool invalid_iter (out Gtk.TreeIter iter) {
  183. iter = Gtk.TreeIter ();
  184. iter.stamp = -1;
  185. return false;
  186. }
  187. // custom get pkg function
  188. public unowned Json.Object? get_pkg_at_path (Gtk.TreePath path) {
  189. return pkgs.nth_data (path.get_indices ()[0]);
  190. }
  191. public void sort_by_name (Gtk.SortType order) {
  192. pkgs.sort ((GLib.CompareFunc) aur_compare_name);
  193. if (order == Gtk.SortType.DESCENDING) {
  194. pkgs.reverse ();
  195. }
  196. manager_window.aur_name_column.sort_order = order;
  197. manager_window.aur_state_column.sort_indicator = false;
  198. manager_window.aur_name_column.sort_indicator = true;
  199. manager_window.aur_version_column.sort_indicator = false;
  200. manager_window.aur_votes_column.sort_indicator = false;
  201. manager_window.sortinfo.column_number = 0;
  202. manager_window.sortinfo.sort_type = order;
  203. }
  204. public void sort_by_state (Gtk.SortType order) {
  205. pkgs.sort ((GLib.CompareFunc) aur_compare_state);
  206. if (order == Gtk.SortType.DESCENDING) {
  207. pkgs.reverse ();
  208. }
  209. manager_window.aur_state_column.sort_order = order;
  210. manager_window.aur_state_column.sort_indicator = true;
  211. manager_window.aur_name_column.sort_indicator = false;
  212. manager_window.aur_version_column.sort_indicator = false;
  213. manager_window.aur_votes_column.sort_indicator = false;
  214. manager_window.sortinfo.column_number = 1;
  215. manager_window.sortinfo.sort_type = order;
  216. }
  217. public void sort_by_version (Gtk.SortType order) {
  218. pkgs.sort ((GLib.CompareFunc) aur_compare_version);
  219. if (order == Gtk.SortType.DESCENDING) {
  220. pkgs.reverse ();
  221. }
  222. manager_window.aur_version_column.sort_order = order;
  223. manager_window.aur_state_column.sort_indicator = false;
  224. manager_window.aur_name_column.sort_indicator = false;
  225. manager_window.aur_version_column.sort_indicator = true;
  226. manager_window.aur_votes_column.sort_indicator = false;
  227. manager_window.sortinfo.column_number = 2;
  228. manager_window.sortinfo.sort_type = order;
  229. }
  230. public void sort_by_votes (Gtk.SortType order) {
  231. pkgs.sort ((GLib.CompareFunc) aur_compare_votes);
  232. if (order == Gtk.SortType.DESCENDING) {
  233. pkgs.reverse ();
  234. }
  235. manager_window.aur_votes_column.sort_order = order;
  236. manager_window.aur_state_column.sort_indicator = false;
  237. manager_window.aur_name_column.sort_indicator = false;
  238. manager_window.aur_version_column.sort_indicator = false;
  239. manager_window.aur_votes_column.sort_indicator = true;
  240. manager_window.sortinfo.column_number = 3;
  241. manager_window.sortinfo.sort_type = order;
  242. }
  243. }
  244. }