pamac-classic/src/mirrors_config.vala

169 lines
5.2 KiB
Vala
Raw Normal View History

/*
* pamac-vala
*
2017-01-03 05:43:19 -03:00
* Copyright (C) 2014-2017 Guillaume Benoit <guillaume@manjaro.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a get of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Pamac {
2016-04-14 13:19:20 -03:00
class MirrorsConfig {
Transaction transaction;
this.transaction = transaction;
AlpmPackage pkg = transaction.find_installed_satisfier ("pacman-mirrors");
2016-04-14 13:19:20 -03:00
string conf_path;
2017-04-15 16:34:35 -03:00
GLib.List<string> _countries ;
2016-04-14 13:19:20 -03:00
public string mirrorlists_dir { get; private set; }
2016-04-14 13:19:20 -03:00
public string choosen_generation_method { get; private set; }
public string choosen_country { get; private set; }
2017-04-15 16:34:35 -03:00
public unowned GLib.List<string> countries {
2016-04-14 13:19:20 -03:00
get {
try {
if (pkg != "") {
string countries_str;
int status;
Process.spawn_command_line_sync ("pacman-mirrors -l",
2017-04-15 16:34:35 -03:00
out countries_str,
null,
out status);
_countries = new GLib.List<string> ();
if (status == 0) {
foreach (unowned string country in countries_str.split ("\n")) {
_countries.append (country);
}
}
} else {
var directory = GLib.File.new_for_path (mirrorlists_dir);
var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
FileInfo file_info;
_countries = new GLib.List<string> ();
while ((file_info = enumerator.next_file ()) != null) {
_countries.append(file_info.get_name ());
2017-04-15 16:34:35 -03:00
}
_countries.sort (strcmp);
2016-04-14 13:19:20 -03:00
}
2017-04-15 16:34:35 -03:00
} catch (SpawnError e) {
stdout.printf ("Error: %s\n", e.message);
2016-04-14 13:19:20 -03:00
}
2017-04-15 16:34:35 -03:00
return _countries;
2016-04-14 13:19:20 -03:00
}
}
public MirrorsConfig (string path) {
conf_path = path;
reload ();
}
public void reload () {
// set default options
choosen_generation_method = "rank";
2016-08-24 10:28:38 -03:00
choosen_country = "ALL";
mirrorlists_dir = "/etc/pacman.d/mirrors";
parse_file (conf_path);
}
2016-04-14 13:19:20 -03:00
void parse_file (string path) {
var file = GLib.File.new_for_path (path);
2016-02-02 05:28:07 -03:00
if (file.query_exists ()) {
try {
// Open file for reading and wrap returned FileInputStream into a
// DataInputStream, so we can read line by line
var dis = new DataInputStream (file.read ());
2015-08-20 10:11:18 -03:00
string? line;
// Read lines until end of file (null) is reached
2015-08-20 10:11:18 -03:00
while ((line = dis.read_line ()) != null) {
2015-03-04 11:55:36 -03:00
if (line.length == 0) {
continue;
}
2015-08-20 10:11:18 -03:00
// ignore whole line and end of line comments
string[] splitted = line.split ("#", 2);
line = splitted[0].strip ();
if (line.length == 0) {
2015-03-04 11:55:36 -03:00
continue;
}
2016-02-02 05:28:07 -03:00
splitted = line.split ("=", 2);
unowned string key = splitted[0]._strip ();
unowned string? val = null;
if (splitted.length == 2) {
val = splitted[1]._strip ();
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
if (key == "Method") {
choosen_generation_method = val;
} else if (key == "OnlyCountry") {
choosen_country = val;
2015-03-04 11:55:36 -03:00
}
}
} catch (Error e) {
GLib.stderr.printf("%s\n", e.message);
}
2016-02-02 05:28:07 -03:00
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
}
}
public void write (HashTable<string,Variant> new_conf) {
var file = GLib.File.new_for_path (conf_path);
2016-02-02 05:28:07 -03:00
if (file.query_exists ()) {
try {
// Open file for reading and wrap returned FileInputStream into a
// DataInputStream, so we can read line by line
var dis = new DataInputStream (file.read ());
2015-08-20 10:11:18 -03:00
string? line;
2016-02-02 05:28:07 -03:00
var data = new GLib.List<string> ();
// Read lines until end of file (null) is reached
2015-08-20 10:11:18 -03:00
while ((line = dis.read_line ()) != null) {
if (line.length == 0) {
2016-02-02 05:28:07 -03:00
data.append ("\n");
2015-08-20 10:11:18 -03:00
continue;
}
2016-02-02 05:28:07 -03:00
unowned Variant variant;
if (line.contains ("Method")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("Method", null, out variant)) {
data.append ("Method = %s\n".printf (variant.get_string ()));
2015-03-04 11:55:36 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-03-04 11:55:36 -03:00
}
} else if (line.contains ("OnlyCountry")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("OnlyCountry", null, out variant)) {
2016-08-24 10:28:38 -03:00
if (variant.get_string () == "ALL") {
2016-02-02 05:28:07 -03:00
data.append ("#%s\n".printf (line));
2015-03-04 11:55:36 -03:00
} else {
data.append ("OnlyCountry = %s\n".printf (variant.get_string ()));
2015-03-04 11:55:36 -03:00
}
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-03-04 11:55:36 -03:00
}
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-03-04 11:55:36 -03:00
}
}
// delete the file before rewrite it
file.delete ();
// creating a DataOutputStream to the file
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
2016-02-02 05:28:07 -03:00
foreach (unowned string new_line in data) {
// writing a short string to the stream
dos.put_string (new_line);
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
2016-02-02 05:28:07 -03:00
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", file.get_path ());
}
}
}
}