2014-12-30 11:04:40 -03:00
|
|
|
/*
|
|
|
|
* pamac-vala
|
|
|
|
*
|
2015-03-04 11:55:36 -03:00
|
|
|
* Copyright (C) 2014-2015 Guillaume Benoit <guillaume@manjaro.org>
|
2014-12-30 11:04:40 -03:00
|
|
|
*
|
|
|
|
* 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 Alpm {
|
|
|
|
public class MirrorsConfig: Object {
|
|
|
|
string conf_path;
|
|
|
|
string mirrorlists_dir;
|
|
|
|
public string choosen_generation_method;
|
|
|
|
public string choosen_country;
|
|
|
|
public GLib.List<string> countrys;
|
|
|
|
|
|
|
|
public MirrorsConfig (string path) {
|
|
|
|
conf_path = path;
|
|
|
|
reload ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reload () {
|
|
|
|
// set default options
|
|
|
|
choosen_generation_method = "rank";
|
|
|
|
choosen_country = dgettext (null, "Worldwide");
|
|
|
|
mirrorlists_dir = "/etc/pacman.d/mirrors";
|
|
|
|
parse_file (conf_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void get_countrys () {
|
|
|
|
try {
|
|
|
|
var directory = GLib.File.new_for_path (mirrorlists_dir);
|
|
|
|
var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
|
|
|
|
FileInfo file_info;
|
|
|
|
countrys = new GLib.List<string> ();
|
|
|
|
while ((file_info = enumerator.next_file ()) != null) {
|
|
|
|
countrys.append(file_info.get_name ());
|
|
|
|
}
|
|
|
|
countrys.sort ((a, b) => {
|
|
|
|
return strcmp (a, b);
|
|
|
|
});
|
|
|
|
} catch (Error e) {
|
|
|
|
stderr.printf ("%s\n", e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parse_file (string path) {
|
|
|
|
var file = GLib.File.new_for_path (path);
|
2015-03-04 11:55:36 -03:00
|
|
|
if (file.query_exists () == false) {
|
2014-12-30 11:04:40 -03:00
|
|
|
GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
|
2015-03-04 11:55:36 -03:00
|
|
|
} else {
|
2014-12-30 11:04:40 -03:00
|
|
|
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;
|
2014-12-30 11:04:40 -03:00
|
|
|
// 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;
|
|
|
|
}
|
2015-08-20 10:11:18 -03:00
|
|
|
splitted = line.split ("=");
|
|
|
|
string key = splitted[0].strip ();
|
|
|
|
string? val = null;
|
2015-03-04 11:55:36 -03:00
|
|
|
if (splitted[1] != null) {
|
2015-08-20 10:11:18 -03:00
|
|
|
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;
|
|
|
|
} else if (key == "MirrorlistsDir") {
|
|
|
|
mirrorlists_dir = val.replace ("\"", "");
|
2015-03-04 11:55:36 -03:00
|
|
|
}
|
2014-12-30 11:04:40 -03:00
|
|
|
}
|
|
|
|
} catch (Error e) {
|
|
|
|
GLib.stderr.printf("%s\n", e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write (HashTable<string,Variant> new_conf) {
|
|
|
|
var file = GLib.File.new_for_path (conf_path);
|
2015-03-04 11:55:36 -03:00
|
|
|
if (file.query_exists () == false) {
|
2014-12-30 11:04:40 -03:00
|
|
|
GLib.stderr.printf ("File '%s' doesn't exist.\n", file.get_path ());
|
2015-03-04 11:55:36 -03:00
|
|
|
} else {
|
2014-12-30 11:04:40 -03:00
|
|
|
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;
|
2014-12-30 11:04:40 -03:00
|
|
|
string[] data = {};
|
|
|
|
// 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) {
|
|
|
|
data += "\n";
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-30 11:04:40 -03:00
|
|
|
if (line.contains ("Method")) {
|
|
|
|
if (new_conf.contains ("Method")) {
|
2015-08-20 10:11:18 -03:00
|
|
|
string val = new_conf.get ("Method").get_string ();
|
|
|
|
data += "Method=%s\n".printf (val);
|
2015-03-04 11:55:36 -03:00
|
|
|
} else {
|
2014-12-30 11:04:40 -03:00
|
|
|
data += line + "\n";
|
2015-03-04 11:55:36 -03:00
|
|
|
}
|
2014-12-30 11:04:40 -03:00
|
|
|
} else if (line.contains ("OnlyCountry")) {
|
|
|
|
if (new_conf.contains ("OnlyCountry")) {
|
2015-08-20 10:11:18 -03:00
|
|
|
string val = new_conf.get ("OnlyCountry").get_string ();
|
|
|
|
if (val == dgettext (null, "Worldwide")) {
|
2014-12-30 11:04:40 -03:00
|
|
|
data += "#%s\n".printf (line);
|
2015-03-04 11:55:36 -03:00
|
|
|
} else {
|
2015-08-20 10:11:18 -03:00
|
|
|
data += "OnlyCountry=%s\n".printf (val);
|
2015-03-04 11:55:36 -03:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-30 11:04:40 -03:00
|
|
|
data += line + "\n";
|
2015-03-04 11:55:36 -03:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-30 11:04:40 -03:00
|
|
|
data += line + "\n";
|
2015-03-04 11:55:36 -03:00
|
|
|
}
|
2014-12-30 11:04:40 -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));
|
|
|
|
foreach (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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|