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.

aur.vala 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * pamac-vala
  3. *
  4. * Copyright (C) 2017 Chris Cromer <cromer@cromnix.org>
  5. * Copyright (C) 2014-2017 Guillaume Benoit <guillaume@manjaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a get of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. //using Json
  21. //using Soup
  22. //using Alpm
  23. namespace Pamac {
  24. // AUR urls
  25. const string aur_url = "http://aur.archlinux.org";
  26. const string rpc_url = aur_url + "/rpc/?v=5";
  27. const string rpc_search = "&type=search&arg=";
  28. const string rpc_multiinfo = "&type=info";
  29. const string rpc_multiinfo_arg = "&arg[]=";
  30. Json.Array rpc_query (string uri) {
  31. var results = new Json.Array ();
  32. var session = new Soup.Session ();
  33. // set a 15 seconds timeout because it is also the dbus daemon timeout
  34. session.timeout = 15;
  35. var message = new Soup.Message ("GET", uri);
  36. var parser = new Json.Parser ();
  37. session.send_message (message);
  38. try {
  39. parser.load_from_data ((string) message.response_body.flatten ().data, -1);
  40. } catch (Error e) {
  41. critical (e.message);
  42. }
  43. unowned Json.Node? root = parser.get_root ();
  44. if (root != null) {
  45. if (root.get_object ().get_string_member ("type") == "error") {
  46. critical ("Failed to query %s from AUR", uri);
  47. } else {
  48. results = root.get_object ().get_array_member ("results");
  49. }
  50. }
  51. return results;
  52. }
  53. public async Json.Array search (string[] needles) {
  54. if (needles.length == 0) {
  55. return new Json.Array ();
  56. } else {
  57. var result = rpc_query (rpc_url + rpc_search + Uri.escape_string (needles[0]));
  58. int i = 1;
  59. while (i < needles.length) {
  60. var inter = new Json.Array ();
  61. var found = rpc_query (rpc_url + rpc_search + Uri.escape_string (needles[i]));
  62. result.foreach_element ((result_array, result_index, result_node) => {
  63. found.foreach_element ((found_array, found_index, found_node) => {
  64. if (strcmp (result_node.get_object ().get_string_member ("Name"),
  65. found_node.get_object ().get_string_member ("Name")) == 0) {
  66. inter.add_element (result_node);
  67. }
  68. });
  69. });
  70. result = (owned) inter;
  71. i++;
  72. }
  73. return result;
  74. }
  75. }
  76. public async Json.Array multiinfo (string[] pkgnames) {
  77. if (pkgnames.length == 0) {
  78. return new Json.Array ();
  79. }
  80. var builder = new StringBuilder ();
  81. builder.append (rpc_url);
  82. builder.append (rpc_multiinfo);
  83. foreach (unowned string pkgname in pkgnames) {
  84. builder.append (rpc_multiinfo_arg);
  85. builder.append (Uri.escape_string (pkgname));
  86. }
  87. return rpc_query (builder.str);
  88. }
  89. }