forked from cromer/pamac-classic
customized builds
This commit is contained in:
131
src/pamac-system-daemon/mirrors_config.vala
Normal file
131
src/pamac-system-daemon/mirrors_config.vala
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* pamac-vala
|
||||
*
|
||||
* Copyright (C) 2017 Chris Cromer <cromer@cromnix.org>
|
||||
* 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 {
|
||||
class MirrorsConfig {
|
||||
string conf_path;
|
||||
|
||||
public string choosen_generation_method { get; private set; }
|
||||
public string choosen_country { get; private set; }
|
||||
|
||||
public MirrorsConfig (string path) {
|
||||
conf_path = path;
|
||||
reload ();
|
||||
}
|
||||
|
||||
public void reload () {
|
||||
// set default options
|
||||
choosen_generation_method = "rank";
|
||||
choosen_country = "ALL";
|
||||
parse_file (conf_path);
|
||||
}
|
||||
|
||||
void parse_file (string path) {
|
||||
var file = GLib.File.new_for_path (path);
|
||||
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 ());
|
||||
string? line;
|
||||
// Read lines until end of file (null) is reached
|
||||
while ((line = dis.read_line ()) != null) {
|
||||
if (line.length == 0) {
|
||||
continue;
|
||||
}
|
||||
// ignore whole line and end of line comments
|
||||
string[] splitted = line.split ("#", 2);
|
||||
line = splitted[0].strip ();
|
||||
if (line.length == 0) {
|
||||
continue;
|
||||
}
|
||||
splitted = line.split ("=", 2);
|
||||
unowned string key = splitted[0]._strip ();
|
||||
unowned string? val = null;
|
||||
if (splitted.length == 2) {
|
||||
val = splitted[1]._strip ();
|
||||
}
|
||||
if (key == "Method") {
|
||||
choosen_generation_method = val;
|
||||
} else if (key == "OnlyCountry") {
|
||||
choosen_country = val;
|
||||
}
|
||||
}
|
||||
} catch (Error e) {
|
||||
GLib.stderr.printf("%s\n", e.message);
|
||||
}
|
||||
} 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);
|
||||
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 ());
|
||||
string? line;
|
||||
var data = new GLib.List<string> ();
|
||||
// Read lines until end of file (null) is reached
|
||||
while ((line = dis.read_line ()) != null) {
|
||||
if (line.length == 0) {
|
||||
data.append ("\n");
|
||||
continue;
|
||||
}
|
||||
unowned Variant variant;
|
||||
if (line.contains ("Method")) {
|
||||
if (new_conf.lookup_extended ("Method", null, out variant)) {
|
||||
data.append ("Method = %s\n".printf (variant.get_string ()));
|
||||
} else {
|
||||
data.append (line + "\n");
|
||||
}
|
||||
} else if (line.contains ("OnlyCountry")) {
|
||||
if (new_conf.lookup_extended ("OnlyCountry", null, out variant)) {
|
||||
if (variant.get_string () == "ALL") {
|
||||
data.append ("#%s\n".printf (line));
|
||||
} else {
|
||||
data.append ("OnlyCountry = %s\n".printf (variant.get_string ()));
|
||||
}
|
||||
} else {
|
||||
data.append (line + "\n");
|
||||
}
|
||||
} else {
|
||||
data.append (line + "\n");
|
||||
}
|
||||
}
|
||||
// delete the file before rewrite it
|
||||
file.delete ();
|
||||
// creating a DataOutputStream to the file
|
||||
var dos = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
GLib.stderr.printf ("File '%s' doesn't exist.\n", file.get_path ());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user