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.

155 lines
4.8KB

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