pamac-classic/src/pamac_config.vala

288 lines
9.3 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2015-03-04 11:55:36 -03:00
* Copyright (C) 2014-2015 Guillaume Benoit <guillaume@manjaro.org>
2014-10-22 13:44:02 -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/>.
*/
2016-02-02 05:28:07 -03:00
const string VERSION = "3.0.0";
2014-10-22 13:44:02 -03:00
namespace Pamac {
2016-02-02 05:28:07 -03:00
[Compact]
public class Config {
public string conf_path;
2014-10-22 13:44:02 -03:00
public bool recurse;
2016-02-02 05:28:07 -03:00
public uint64 refresh_period;
2015-08-20 10:11:18 -03:00
public bool no_update_hide_icon;
2015-08-24 11:13:18 -03:00
public bool enable_aur;
public bool search_aur;
2015-08-20 10:11:18 -03:00
public bool check_aur_updates;
public bool no_confirm_build;
2016-02-02 05:28:07 -03:00
public HashTable<string,string> environment_variables;
2014-10-22 13:44:02 -03:00
public Config (string path) {
2014-12-03 12:02:14 -03:00
conf_path = path;
2016-02-02 05:28:07 -03:00
//get environment variables
environment_variables = new HashTable<string,string> (str_hash, str_equal);
var utsname = Posix.utsname();
environment_variables.insert ("HTTP_USER_AGENT", "pamac/%s (%s %s)".printf (VERSION, utsname.sysname, utsname.machine));
unowned string? variable = Environment.get_variable ("http_proxy");
if (variable != null) {
environment_variables.insert ("http_proxy", variable);
}
variable = Environment.get_variable ("https_proxy");
if (variable != null) {
environment_variables.insert ("https_proxy", variable);
}
variable = Environment.get_variable ("ftp_proxy");
if (variable != null) {
environment_variables.insert ("ftp_proxy", variable);
}
variable = Environment.get_variable ("socks_proxy");
if (variable != null) {
environment_variables.insert ("socks_proxy", variable);
}
variable = Environment.get_variable ("no_proxy");
if (variable != null) {
environment_variables.insert ("no_proxy", variable);
}
// set default option
2015-08-20 10:11:18 -03:00
refresh_period = 6;
reload ();
}
public void reload () {
// set default options
2014-12-03 12:02:14 -03:00
recurse = false;
2015-08-20 10:11:18 -03:00
no_update_hide_icon = false;
2015-08-24 11:13:18 -03:00
enable_aur = false;
search_aur = false;
2015-08-20 10:11:18 -03:00
check_aur_updates = false;
no_confirm_build = 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);
2015-08-20 10:11:18 -03:00
if (file.query_exists ()) {
2014-10-22 13:44:02 -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-10-22 13:44:02 -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;
}
2016-02-02 05:28:07 -03:00
splitted = line.split ("=", 2);
unowned string key = splitted[0]._strip ();
2015-08-20 10:11:18 -03:00
if (key == "RemoveUnrequiredDeps") {
2014-12-03 12:02:14 -03:00
recurse = true;
2015-08-20 10:11:18 -03:00
} else if (key == "RefreshPeriod") {
2016-02-02 05:28:07 -03:00
if (splitted.length == 2) {
unowned string val = splitted[1]._strip ();
refresh_period = uint64.parse (val);
}
2015-08-20 10:11:18 -03:00
} else if (key == "NoUpdateHideIcon") {
no_update_hide_icon = true;
} else if (key == "EnableAUR") {
enable_aur = true;
2015-08-24 11:13:18 -03:00
} else if (key == "SearchInAURByDefault") {
search_aur = true;
2015-08-20 10:11:18 -03:00
} else if (key == "CheckAURUpdates") {
check_aur_updates = true;
} else if (key == "NoConfirmBuild") {
no_confirm_build = true;
2015-03-04 11:55:36 -03:00
}
2014-10-22 13:44:02 -03:00
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
2015-08-20 10:11:18 -03:00
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", path);
2014-10-22 13:44:02 -03:00
}
}
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);
2016-02-02 05:28:07 -03:00
var data = new GLib.List<string> ();
2015-08-20 10:11:18 -03:00
if (file.query_exists ()) {
2014-10-22 13:44:02 -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-10-22 13:44:02 -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) {
2016-02-02 05:28:07 -03:00
data.append ("\n");
2015-03-04 11:55:36 -03:00
continue;
}
2016-02-02 05:28:07 -03:00
unowned Variant variant;
2015-08-20 10:11:18 -03:00
if (line.contains ("RemoveUnrequiredDeps")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("RemoveUnrequiredDeps", null, out variant)) {
if (variant.get_boolean ()) {
data.append ("RemoveUnrequiredDeps\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#RemoveUnrequiredDeps\n");
2015-08-20 10:11:18 -03:00
}
new_conf.remove ("RemoveUnrequiredDeps");
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-08-20 10:11:18 -03:00
}
} else if (line.contains ("RefreshPeriod")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("RefreshPeriod", null, out variant)) {
data.append ("RefreshPeriod = %llu\n".printf (variant.get_uint64 ()));
2015-08-20 10:11:18 -03:00
new_conf.remove ("RefreshPeriod");
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-08-20 10:11:18 -03:00
}
} else if (line.contains ("NoUpdateHideIcon")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("NoUpdateHideIcon", null, out variant)) {
if (variant.get_boolean ()) {
data.append ("NoUpdateHideIcon\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#NoUpdateHideIcon\n");
2015-08-20 10:11:18 -03:00
}
new_conf.remove ("NoUpdateHideIcon");
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
}
2014-10-22 13:44:02 -03:00
} else if (line.contains ("EnableAUR")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("EnableAUR", null, out variant)) {
if (variant.get_boolean ()) {
data.append ("EnableAUR\n");
2015-03-04 11:55:36 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#EnableAUR\n");
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
new_conf.remove ("EnableAUR");
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
}
2015-08-24 11:13:18 -03:00
} else if (line.contains ("SearchInAURByDefault")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("SearchInAURByDefault", null, out variant)) {
if (variant.get_boolean ()) {
data.append ("SearchInAURByDefault\n");
2015-08-24 11:13:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#SearchInAURByDefault\n");
2015-08-24 11:13:18 -03:00
}
new_conf.remove ("SearchInAURByDefault");
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-08-24 11:13:18 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (line.contains ("CheckAURUpdates")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("CheckAURUpdates", null, out variant)) {
if (variant.get_boolean ()) {
data.append ("CheckAURUpdates\n");
2015-03-04 11:55:36 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#CheckAURUpdates\n");
2015-08-20 10:11:18 -03:00
}
new_conf.remove ("CheckAURUpdates");
} else {
2016-02-02 05:28:07 -03:00
data.append (line + "\n");
2015-08-20 10:11:18 -03:00
}
} else if (line.contains ("NoConfirmBuild")) {
2016-02-02 05:28:07 -03:00
if (new_conf.lookup_extended ("NoConfirmBuild", null, out variant)) {
if (variant.get_boolean ()) {
data.append ("NoConfirmBuild\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#NoConfirmBuild\n");
2015-03-04 11:55:36 -03:00
}
2015-08-20 10:11:18 -03:00
new_conf.remove ("NoConfirmBuild");
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
}
2014-10-22 13:44:02 -03:00
}
// delete the file before rewrite it
file.delete ();
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
}
2015-08-20 10:11:18 -03:00
} else {
GLib.stderr.printf ("File '%s' doesn't exist.\n", conf_path);
}
// create lines for unexisted options
if (new_conf.size () != 0) {
2016-02-02 05:28:07 -03:00
data.append ("\n");
var iter = HashTableIter<string,Variant> (new_conf);
unowned string key;
unowned Variant val;
while (iter.next (out key, out val)) {
2015-08-20 10:11:18 -03:00
if (key == "RemoveUnrequiredDeps") {
2016-02-02 05:28:07 -03:00
if (val.get_boolean ()) {
data.append ("RemoveUnrequiredDeps\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#RemoveUnrequiredDeps\n");
2015-08-20 10:11:18 -03:00
}
} else if (key == "RefreshPeriod") {
2016-02-02 05:28:07 -03:00
data.append ("RefreshPeriod = %llu\n".printf (val.get_uint64 ()));
2015-08-20 10:11:18 -03:00
} else if (key =="NoUpdateHideIcon") {
2016-02-02 05:28:07 -03:00
if (val.get_boolean ()) {
data.append ("NoUpdateHideIcon\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#NoUpdateHideIcon\n");
2015-08-20 10:11:18 -03:00
}
} else if (key == "EnableAUR") {
2016-02-02 05:28:07 -03:00
if (val.get_boolean ()) {
data.append ("EnableAUR\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#EnableAUR\n");
2015-08-20 10:11:18 -03:00
}
2015-08-24 11:13:18 -03:00
} else if (key == "SearchInAURByDefault") {
2016-02-02 05:28:07 -03:00
if (val.get_boolean ()) {
data.append ("SearchInAURByDefault\n");
2015-08-24 11:13:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#SearchInAURByDefault\n");
2015-08-24 11:13:18 -03:00
}
2015-08-20 10:11:18 -03:00
} else if (key == "CheckAURUpdates") {
2016-02-02 05:28:07 -03:00
if (val.get_boolean ()) {
data.append ("CheckAURUpdates\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#CheckAURUpdates\n");
2015-08-20 10:11:18 -03:00
}
} else if (key == "NoConfirmBuild") {
2016-02-02 05:28:07 -03:00
if (val.get_boolean ()) {
data.append ("NoConfirmBuild\n");
2015-08-20 10:11:18 -03:00
} else {
2016-02-02 05:28:07 -03:00
data.append ("#NoConfirmBuild\n");
2015-08-20 10:11:18 -03:00
}
}
2016-02-02 05:28:07 -03:00
}
2015-08-20 10:11:18 -03:00
}
// write the file
try {
// 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) {
2015-08-20 10:11:18 -03:00
// writing a short string to the stream
dos.put_string (new_line);
}
} catch (GLib.Error e) {
GLib.stderr.printf("%s\n", e.message);
2014-10-22 13:44:02 -03:00
}
}
}
}