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.

132 lines
4.1KB

  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. namespace Pamac {
  21. public class MirrorsConfig {
  22. string conf_path;
  23. public string choosen_generation_method { get; private set; }
  24. public string choosen_country { get; private set; }
  25. public MirrorsConfig (string path) {
  26. conf_path = path;
  27. reload ();
  28. }
  29. public void reload () {
  30. // set default options
  31. choosen_generation_method = "rank";
  32. choosen_country = "ALL";
  33. parse_file (conf_path);
  34. }
  35. void parse_file (string path) {
  36. var file = GLib.File.new_for_path (path);
  37. if (file.query_exists ()) {
  38. try {
  39. // Open file for reading and wrap returned FileInputStream into a
  40. // DataInputStream, so we can read line by line
  41. var dis = new DataInputStream (file.read ());
  42. string? line;
  43. // Read lines until end of file (null) is reached
  44. while ((line = dis.read_line ()) != null) {
  45. if (line.length == 0) {
  46. continue;
  47. }
  48. // ignore whole line and end of line comments
  49. string[] splitted = line.split ("#", 2);
  50. line = splitted[0].strip ();
  51. if (line.length == 0) {
  52. continue;
  53. }
  54. splitted = line.split ("=", 2);
  55. unowned string key = splitted[0]._strip ();
  56. unowned string? val = null;
  57. if (splitted.length == 2) {
  58. val = splitted[1]._strip ();
  59. }
  60. if (key == "Method") {
  61. choosen_generation_method = val;
  62. } else if (key == "OnlyCountry") {
  63. choosen_country = val;
  64. }
  65. }
  66. } catch (Error e) {
  67. stderr.printf("%s\n", e.message);
  68. }
  69. } else {
  70. stderr.printf ("File '%s' doesn't exist.\n", path);
  71. }
  72. }
  73. public void write (HashTable<string,Variant> new_conf) {
  74. var file = GLib.File.new_for_path (conf_path);
  75. if (file.query_exists ()) {
  76. try {
  77. // Open file for reading and wrap returned FileInputStream into a
  78. // DataInputStream, so we can read line by line
  79. var dis = new DataInputStream (file.read ());
  80. string? line;
  81. var data = new GLib.List<string> ();
  82. // Read lines until end of file (null) is reached
  83. while ((line = dis.read_line ()) != null) {
  84. if (line.length == 0) {
  85. data.append ("\n");
  86. continue;
  87. }
  88. unowned Variant variant;
  89. if (line.contains ("Method")) {
  90. if (new_conf.lookup_extended ("Method", null, out variant)) {
  91. data.append ("Method = %s\n".printf (variant.get_string ()));
  92. } else {
  93. data.append (line + "\n");
  94. }
  95. } else if (line.contains ("OnlyCountry")) {
  96. if (new_conf.lookup_extended ("OnlyCountry", null, out variant)) {
  97. if (variant.get_string () == "ALL") {
  98. data.append ("#%s\n".printf (line));
  99. } else {
  100. data.append ("OnlyCountry = %s\n".printf (variant.get_string ()));
  101. }
  102. } else {
  103. data.append (line + "\n");
  104. }
  105. } else {
  106. data.append (line + "\n");
  107. }
  108. }
  109. // delete the file before rewrite it
  110. file.delete ();
  111. // creating a DataOutputStream to the file
  112. var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
  113. foreach (unowned string new_line in data) {
  114. // writing a short string to the stream
  115. dos.put_string (new_line);
  116. }
  117. } catch (GLib.Error e) {
  118. stderr.printf("%s\n", e.message);
  119. }
  120. } else {
  121. stderr.printf ("File '%s' doesn't exist.\n", file.get_path ());
  122. }
  123. }
  124. }
  125. }