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.
 
 
 
 

91 lines
3.0 KiB

  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 AUR {
  20. // AUR urls
  21. const string aur_url = "http://aur.archlinux.org";
  22. const string rpc_url = aur_url + "/rpc/?v=5";
  23. const string rpc_search = "&type=search&arg=";
  24. const string rpc_multiinfo = "&type=info";
  25. const string rpc_multiinfo_arg = "&arg[]=";
  26. Json.Array rpc_query (string uri) {
  27. var results = new Json.Array ();
  28. var session = new Soup.Session ();
  29. // set a 15 seconds timeout because it is also the dbus daemon timeout
  30. session.timeout = 15;
  31. var message = new Soup.Message ("GET", uri);
  32. var parser = new Json.Parser ();
  33. session.send_message (message);
  34. try {
  35. parser.load_from_data ((string) message.response_body.flatten ().data, -1);
  36. } catch (Error e) {
  37. critical (e.message);
  38. }
  39. unowned Json.Node? root = parser.get_root ();
  40. if (root != null) {
  41. if (root.get_object ().get_string_member ("type") == "error") {
  42. critical ("Failed to query %s from AUR", uri);
  43. } else {
  44. results = root.get_object ().get_array_member ("results");
  45. }
  46. }
  47. return results;
  48. }
  49. public async Json.Array search (string[] needles) {
  50. if (needles.length == 0) {
  51. return new Json.Array ();
  52. } else if (needles.length == 1) {
  53. return rpc_query (rpc_url + rpc_search + Uri.escape_string (needles[0]));
  54. } else {
  55. var inter = new Json.Array ();
  56. var prev_inter = new Json.Array ();
  57. foreach (unowned string needle in needles) {
  58. inter = new Json.Array ();
  59. var found = rpc_query (rpc_url + rpc_search + Uri.escape_string (needle));
  60. prev_inter.foreach_element ((prev_inter_array, prev_inter_index, prev_inter_node) => {
  61. found.foreach_element ((found_array, found_index, found_node) => {
  62. if (strcmp (prev_inter_node.get_object ().get_string_member ("Name"),
  63. found_node.get_object ().get_string_member ("Name")) == 0) {
  64. inter.add_element (prev_inter_node);
  65. }
  66. });
  67. });
  68. prev_inter = (owned) inter;
  69. }
  70. return inter;
  71. }
  72. }
  73. public async Json.Array multiinfo (string[] pkgnames) {
  74. if (pkgnames.length == 0) {
  75. return new Json.Array ();
  76. }
  77. var builder = new StringBuilder ();
  78. builder.append (rpc_url);
  79. builder.append (rpc_multiinfo);
  80. foreach (unowned string pkgname in pkgnames) {
  81. builder.append (rpc_multiinfo_arg);
  82. builder.append (Uri.escape_string (pkgname));
  83. }
  84. return rpc_query (builder.str);
  85. }
  86. }