pamac-classic/src/pamac_config.vala

130 lines
4.1 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
* Copyright (C) 2014 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 {
public class Config: Object {
2014-12-03 12:02:14 -03:00
string conf_path;
public int refresh_period;
2014-10-22 13:44:02 -03:00
public bool enable_aur;
public bool recurse;
public Config (string path) {
2014-12-03 12:02:14 -03:00
conf_path = path;
// set default option
2014-12-03 12:02:14 -03:00
refresh_period = 4;
reload ();
}
public void reload () {
// set default options
2014-12-03 12:02:14 -03:00
enable_aur = false;
recurse = false;
parse_file (conf_path);
2014-10-22 13:44:02 -03:00
}
public void parse_file (string path) {
2014-10-22 13:44:02 -03:00
var file = GLib.File.new_for_path (path);
if (file.query_exists () == false)
GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
2014-10-22 13:44:02 -03:00
else {
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)) != null) {
line = line.strip ();
if (line.length == 0) continue;
if (line[0] == '#') continue;
string[] splitted = line.split ("=");
string _key = splitted[0].strip ();
string _value = null;
if (splitted[1] != null)
_value = splitted[1].strip ();
if (_key == "RefreshPeriod")
refresh_period = int.parse (_value);
2014-10-22 13:44:02 -03:00
else if (_key == "EnableAUR")
2014-12-03 12:02:14 -03:00
enable_aur = true;
2014-10-22 13:44:02 -03:00
else if (_key == "RemoveUnrequiredDeps")
2014-12-03 12:02:14 -03:00
recurse = true;
2014-10-22 13:44:02 -03:00
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
}
}
public void write (HashTable<string,Variant> new_conf) {
2014-12-03 12:02:14 -03:00
var file = GLib.File.new_for_path (conf_path);
2014-10-22 13:44:02 -03:00
if (file.query_exists () == false)
GLib.stderr.printf ("File '%s' doesn't exist.\n", conf_path);
2014-10-22 13:44:02 -03:00
else {
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;
string[] data = {};
// Read lines until end of file (null) is reached
while ((line = dis.read_line (null)) != null) {
if (line.length == 0) continue;
2014-10-22 13:44:02 -03:00
if (line.contains ("RefreshPeriod")) {
if (new_conf.contains ("RefreshPeriod")) {
int _value = new_conf.get ("RefreshPeriod").get_int32 ();
data += "RefreshPeriod = %u\n".printf (_value);
2014-10-22 13:44:02 -03:00
} else
data += line + "\n";
} else if (line.contains ("EnableAUR")) {
if (new_conf.contains ("EnableAUR")) {
bool _value = new_conf.get ("EnableAUR").get_boolean ();
2014-10-22 13:44:02 -03:00
if (_value == true)
data += "EnableAUR\n";
else
data += "#EnableAUR\n";
} else
data += line + "\n";
} else if (line.contains ("RemoveUnrequiredDeps")) {
if (new_conf.contains ("RemoveUnrequiredDeps")) {
bool _value = new_conf.get ("RemoveUnrequiredDeps").get_boolean ();
2014-10-22 13:44:02 -03:00
if (_value == true)
data += "RemoveUnrequiredDeps\n";
else
data += "#RemoveUnrequiredDeps\n";
} else
data += line + "\n";
} else
data += 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 (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);
}
}
}
}
}