From f3c6e46bafccf36e280c15a0ea45c94b488fc2aa Mon Sep 17 00:00:00 2001
From: guinux <nuxgui@gmail.com>
Date: Mon, 24 Aug 2015 16:13:18 +0200
Subject: [PATCH] add search in AUR by default option

---
 data/config/pamac.conf          |  3 +++
 po/pamac.pot                    |  4 ++++
 resources/preferences_dialog.ui | 24 ++++++++++++++++++++----
 src/daemon.vala                 | 14 +++++++-------
 src/manager_window.vala         | 17 +++++++++++------
 src/pamac_config.vala           | 28 +++++++++++++++++++++++++---
 src/preferences_dialog.vala     | 23 ++++++++++++++++++-----
 src/transaction.vala            | 12 +++++++-----
 src/tray.vala                   | 12 +++++++-----
 9 files changed, 102 insertions(+), 35 deletions(-)

diff --git a/data/config/pamac.conf b/data/config/pamac.conf
index 959574c7..1790da18 100644
--- a/data/config/pamac.conf
+++ b/data/config/pamac.conf
@@ -13,6 +13,9 @@ RefreshPeriod = 6
 ## Allow Pamac to search and install packages from AUR:
 #EnableAUR
 
+## When AUR support is enabled search in AUR by default:
+#SearchInAURByDefault
+
 ## When AUR support is enabled check for updates from AUR:
 #CheckAURUpdates
 
diff --git a/po/pamac.pot b/po/pamac.pot
index 2b885e96..d14fc1a0 100644
--- a/po/pamac.pot
+++ b/po/pamac.pot
@@ -681,6 +681,10 @@ msgstr ""
 msgid "Allow Pamac to search and install packages from AUR"
 msgstr ""
 
+#: ../resources/preferences_dialog.ui
+msgid "Search in AUR by default"
+msgstr ""
+
 #: ../resources/preferences_dialog.ui
 msgid "Check for updates from AUR"
 msgstr ""
diff --git a/resources/preferences_dialog.ui b/resources/preferences_dialog.ui
index 863f12f6..cf77c790 100644
--- a/resources/preferences_dialog.ui
+++ b/resources/preferences_dialog.ui
@@ -383,12 +383,12 @@
               <object class="GtkBox" id="mirrors_config_box">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="orientation">vertical</property>
-                <property name="spacing">6</property>
                 <property name="margin_start">6</property>
                 <property name="margin_end">6</property>
                 <property name="margin_top">6</property>
                 <property name="margin_bottom">6</property>
+                <property name="orientation">vertical</property>
+                <property name="spacing">6</property>
                 <child>
                   <object class="GtkBox" id="box5">
                     <property name="visible">True</property>
@@ -602,6 +602,22 @@ All AUR users should be familiar with the build process.</property>
                     <property name="position">1</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkCheckButton" id="search_aur_checkbutton">
+                    <property name="label" translatable="yes">Search in AUR by default</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">False</property>
+                    <property name="margin_start">24</property>
+                    <property name="hexpand">True</property>
+                    <property name="draw_indicator">True</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">2</property>
+                  </packing>
+                </child>
                 <child>
                   <object class="GtkCheckButton" id="check_aur_updates_checkbutton">
                     <property name="label" translatable="yes">Check for updates from AUR</property>
@@ -615,7 +631,7 @@ All AUR users should be familiar with the build process.</property>
                   <packing>
                     <property name="expand">False</property>
                     <property name="fill">True</property>
-                    <property name="position">2</property>
+                    <property name="position">3</property>
                   </packing>
                 </child>
                 <child>
@@ -631,7 +647,7 @@ All AUR users should be familiar with the build process.</property>
                   <packing>
                     <property name="expand">False</property>
                     <property name="fill">True</property>
-                    <property name="position">3</property>
+                    <property name="position">4</property>
                   </packing>
                 </child>
               </object>
diff --git a/src/daemon.vala b/src/daemon.vala
index 0368d2e7..9fd405b7 100644
--- a/src/daemon.vala
+++ b/src/daemon.vala
@@ -66,8 +66,8 @@ namespace Pamac {
 		public signal void trans_prepare_finished (ErrorInfos error);
 		public signal void trans_commit_finished (ErrorInfos error);
 		public signal void get_authorization_finished (bool authorized);
-		public signal void write_pamac_config_finished (int refresh_period, bool aur_enabled, bool recurse,
-														bool no_update_hide_icon, bool check_aur_updates,
+		public signal void write_pamac_config_finished (bool recurse, int refresh_period, bool no_update_hide_icon,
+														bool enable_aur, bool search_aur, bool check_aur_updates,
 														bool no_confirm_build);
 		public signal void write_alpm_config_finished (bool checkspace);
 		public signal void write_mirrors_config_finished (string choosen_country, string choosen_generation_method);
@@ -183,9 +183,9 @@ namespace Pamac {
 					pamac_config.write (new_pamac_conf);
 					pamac_config.reload ();
 				}
-				write_pamac_config_finished (pamac_config.refresh_period, pamac_config.enable_aur,
-											pamac_config.recurse, pamac_config.no_update_hide_icon,
-											pamac_config.check_aur_updates, pamac_config.no_confirm_build);
+				write_pamac_config_finished (pamac_config.recurse, pamac_config.refresh_period, pamac_config.no_update_hide_icon,
+											pamac_config.enable_aur, pamac_config.search_aur, pamac_config.check_aur_updates,
+											pamac_config.no_confirm_build);
 			});
 		}
 
@@ -415,7 +415,7 @@ namespace Pamac {
 			return Pamac.Package (get_syncpkg (pkgname), null);
 		}
 
-		public async Pamac.Package[] search_pkgs (string search_string, bool search_from_aur) {
+		public async Pamac.Package[] search_pkgs (string search_string, bool search_aur) {
 			Pamac.Package[] result = {};
 			var needles = new Alpm.List<string> ();
 			string[] splitted = search_string.split (" ");
@@ -426,7 +426,7 @@ namespace Pamac {
 			foreach (var alpm_pkg in alpm_pkgs) {
 				result += Pamac.Package (alpm_pkg, null);
 			}
-			if (search_from_aur) {
+			if (search_aur) {
 				Json.Array aur_pkgs;
 				if (aur_search_results.contains (search_string)) {
 					aur_pkgs = aur_search_results.get (search_string);
diff --git a/src/manager_window.vala b/src/manager_window.vala
index 00a7dcf0..21df373d 100644
--- a/src/manager_window.vala
+++ b/src/manager_window.vala
@@ -173,7 +173,7 @@ namespace Pamac {
 			transaction = new Pamac.Transaction (this as Gtk.ApplicationWindow);
 			transaction.mode = Mode.MANAGER;
 			transaction.finished.connect (on_transaction_finished);
-			transaction.enable_aur.connect (enable_aur);
+			transaction.support_aur.connect (support_aur);
 			transaction.daemon.set_pkgreason_finished.connect (display_package_properties);
 
 			var pamac_config = new Pamac.Config ("/etc/pamac.conf");
@@ -182,9 +182,9 @@ namespace Pamac {
 			}
 			Pamac.Package pkg = transaction.find_local_satisfier ("yaourt");
 			if (pkg.name == "") {
-				enable_aur (false);
+				support_aur (false, false);
 			} else {
-				enable_aur (pamac_config.enable_aur);
+				support_aur (pamac_config.enable_aur, pamac_config.search_aur);
 			}
 
 			set_buttons_sensitive (false);
@@ -194,9 +194,14 @@ namespace Pamac {
 			update_lists ();
 		}
 
-		public void enable_aur (bool enable) {
-			search_aur_button.set_active (false);
-			search_aur_box.set_visible (enable);
+		public void support_aur (bool enable_aur, bool search_aur) {
+			if (enable_aur) {
+				search_aur_button.set_active (search_aur);
+				search_aur_box.set_visible (true);
+			} else {
+				search_aur_button.set_active (false);
+				search_aur_box.set_visible (false);
+			}
 		}
 
 		public void set_buttons_sensitive (bool sensitive) {
diff --git a/src/pamac_config.vala b/src/pamac_config.vala
index 063185db..da48648c 100644
--- a/src/pamac_config.vala
+++ b/src/pamac_config.vala
@@ -20,10 +20,11 @@
 namespace Pamac {
 	public class Config: Object {
 		string conf_path;
-		public int refresh_period;
-		public bool enable_aur;
 		public bool recurse;
+		public int refresh_period;
 		public bool no_update_hide_icon;
+		public bool enable_aur;
+		public bool search_aur;
 		public bool check_aur_updates;
 		public bool no_confirm_build;
 
@@ -36,9 +37,10 @@ namespace Pamac {
 
 		public void reload () {
 			// set default options
-			enable_aur = false;
 			recurse = false;
 			no_update_hide_icon = false;
+			enable_aur = false;
+			search_aur = false;
 			check_aur_updates = false;
 			no_confirm_build = false;
 			parse_file (conf_path);
@@ -77,6 +79,8 @@ namespace Pamac {
 							no_update_hide_icon = true;
 						} else if (key == "EnableAUR") {
 							enable_aur = true;
+						} else if (key == "SearchInAURByDefault") {
+							search_aur = true;
 						} else if (key == "CheckAURUpdates") {
 							check_aur_updates = true;
 						} else if (key == "NoConfirmBuild") {
@@ -150,6 +154,18 @@ namespace Pamac {
 							} else {
 								data += line + "\n";
 							}
+						} else if (line.contains ("SearchInAURByDefault")) {
+							if (new_conf.contains ("SearchInAURByDefault")) {
+								bool val = new_conf.get ("SearchInAURByDefault").get_boolean ();
+								if (val == true) {
+									data += "SearchInAURByDefault\n";
+								} else {
+									data += "#SearchInAURByDefault\n";
+								}
+								new_conf.remove ("SearchInAURByDefault");
+							} else {
+								data += line + "\n";
+							}
 						} else if (line.contains ("CheckAURUpdates")) {
 							if (new_conf.contains ("CheckAURUpdates")) {
 								bool val = new_conf.get ("CheckAURUpdates").get_boolean ();
@@ -210,6 +226,12 @@ namespace Pamac {
 						} else {
 							data += "#EnableAUR\n";
 						}
+					} else if (key == "SearchInAURByDefault") {
+						if (val.get_boolean () == true) {
+							data += "SearchInAURByDefault\n";
+						} else {
+							data += "#SearchInAURByDefault\n";
+						}
 					} else if (key == "CheckAURUpdates") {
 						if (val.get_boolean () == true) {
 							data += "CheckAURUpdates\n";
diff --git a/src/preferences_dialog.vala b/src/preferences_dialog.vala
index 02404548..244a50c4 100644
--- a/src/preferences_dialog.vala
+++ b/src/preferences_dialog.vala
@@ -53,6 +53,8 @@ namespace Pamac {
 		[GtkChild]
 		public Gtk.Switch enable_aur_button;
 		[GtkChild]
+		public Gtk.CheckButton search_aur_checkbutton;
+		[GtkChild]
 		public Gtk.CheckButton check_aur_updates_checkbutton;
 		[GtkChild]
 		public Gtk.CheckButton no_confirm_build_checkbutton;
@@ -129,11 +131,14 @@ namespace Pamac {
 				aur_config_box.visible = false;
 			} else {
 				enable_aur_button.active = pamac_config.enable_aur;
+				search_aur_checkbutton.active = pamac_config.search_aur;
+				search_aur_checkbutton.sensitive = pamac_config.enable_aur;
 				check_aur_updates_checkbutton.active = pamac_config.check_aur_updates;
 				check_aur_updates_checkbutton.sensitive = pamac_config.enable_aur;
 				no_confirm_build_checkbutton.active = pamac_config.no_confirm_build;
 				no_confirm_build_checkbutton.sensitive = pamac_config.enable_aur;
 				enable_aur_button.state_set.connect (on_enable_aur_button_state_set);
+				search_aur_checkbutton.toggled.connect (on_search_aur_checkbutton_toggled);
 				check_aur_updates_checkbutton.toggled.connect (on_check_aur_updates_checkbutton_toggled);
 				no_confirm_build_checkbutton.toggled.connect (on_no_confirm_build_checkbutton_toggled);
 				transaction.daemon.write_alpm_config_finished.connect (on_write_alpm_config_finished);
@@ -181,6 +186,12 @@ namespace Pamac {
 			return true;
 		}
 
+		void on_search_aur_checkbutton_toggled () {
+			var new_pamac_conf = new HashTable<string,Variant> (str_hash, str_equal);
+			new_pamac_conf.insert ("SearchInAURByDefault", new Variant.boolean (search_aur_checkbutton.active));
+			transaction.start_write_pamac_config (new_pamac_conf);
+		}
+
 		void on_check_aur_updates_checkbutton_toggled () {
 			var new_pamac_conf = new HashTable<string,Variant> (str_hash, str_equal);
 			new_pamac_conf.insert ("CheckAURUpdates", new Variant.boolean (check_aur_updates_checkbutton.active));
@@ -193,8 +204,8 @@ namespace Pamac {
 			transaction.start_write_pamac_config (new_pamac_conf);
 		}
 
-		void on_write_pamac_config_finished (int refresh_period, bool aur_enabled, bool recurse,
-											bool no_update_hide_icon, bool check_aur_updates,
+		void on_write_pamac_config_finished (bool recurse, int refresh_period, bool no_update_hide_icon,
+											bool enable_aur, bool search_aur, bool check_aur_updates,
 											bool no_confirm_build) {
 			remove_unrequired_deps_button.state = recurse;
 			if (refresh_period == 0) {
@@ -219,11 +230,13 @@ namespace Pamac {
 				}
 			}
 			no_update_hide_icon_checkbutton.active = no_update_hide_icon;
-			enable_aur_button.state = aur_enabled;
+			enable_aur_button.state = enable_aur;
+			search_aur_checkbutton.active = search_aur;
+			search_aur_checkbutton.sensitive = enable_aur;
 			check_aur_updates_checkbutton.active = check_aur_updates;
-			check_aur_updates_checkbutton.sensitive = aur_enabled;
+			check_aur_updates_checkbutton.sensitive = enable_aur;
 			no_confirm_build_checkbutton.active = no_confirm_build;
-			no_confirm_build_checkbutton.sensitive = aur_enabled;
+			no_confirm_build_checkbutton.sensitive = enable_aur;
 		}
 
 		bool on_check_space_button_state_set (bool new_state) {
diff --git a/src/transaction.vala b/src/transaction.vala
index fb680089..38f69dc3 100644
--- a/src/transaction.vala
+++ b/src/transaction.vala
@@ -74,8 +74,8 @@ namespace Pamac {
 		public signal void trans_prepare_finished (ErrorInfos error);
 		public signal void trans_commit_finished (ErrorInfos error);
 		public signal void get_authorization_finished (bool authorized);
-		public signal void write_pamac_config_finished (int refresh_period, bool aur_enabled, bool recurse,
-														bool no_update_hide_icon, bool check_aur_updates,
+		public signal void write_pamac_config_finished (bool recurse, int refresh_period, bool no_update_hide_icon,
+														bool enable_aur, bool search_aur, bool check_aur_updates,
 														bool no_confirm_build);
 		public signal void write_alpm_config_finished (bool checkspace);
 		public signal void write_mirrors_config_finished (string choosen_country, string choosen_generation_method);
@@ -121,7 +121,7 @@ namespace Pamac {
 		Gtk.ApplicationWindow? window;
 
 		public signal void finished (bool with_error);
-		public signal void enable_aur (bool enable);
+		public signal void support_aur (bool enable_aur, bool search_aur);
 
 		public Transaction (Gtk.ApplicationWindow? window) {
 			flags = Alpm.TransFlag.CASCADE;
@@ -1285,14 +1285,16 @@ namespace Pamac {
 			});
 		}
 
-		void on_write_pamac_config_finished (int refresh_period, bool aur_enabled, bool recurse) {
+		void on_write_pamac_config_finished (bool recurse, int refresh_period, bool no_update_hide_icon,
+											bool enable_aur, bool search_aur, bool check_aur_updates,
+											bool no_confirm_build) {
 			flags = Alpm.TransFlag.CASCADE;
 			if (recurse) {
 				flags |= Alpm.TransFlag.RECURSE;
 			}
 			Pamac.Package pkg = find_local_satisfier ("yaourt");
 			if (pkg.name != "") {
-				enable_aur (aur_enabled);
+				support_aur (enable_aur, search_aur);
 			}
 		}
 
diff --git a/src/tray.vala b/src/tray.vala
index 92ce5cbd..300ee724 100644
--- a/src/tray.vala
+++ b/src/tray.vala
@@ -28,13 +28,13 @@ namespace Pamac {
 	[DBus (name = "org.manjaro.pamac")]
 	public interface Daemon : Object {
 		public abstract void start_refresh (int force) throws IOError;
-		public abstract async Updates get_updates (bool enable_aur) throws IOError;
+		public abstract async Updates get_updates (bool check_aur_updates) throws IOError;
 		[DBus (no_reply = true)]
 		public abstract void quit () throws IOError;
 		public signal void refresh_finished (ErrorInfos error);
-		public signal void write_pamac_config_finished (int refresh_period, bool aur_enabled, bool recurse,
-											bool no_update_hide_icon, bool check_aur_updates,
-											bool no_confirm_build);
+		public signal void write_pamac_config_finished (bool recurse, int refresh_period, bool no_update_hide_icon,
+														bool enable_aur, bool search_aur, bool check_aur_updates,
+														bool no_confirm_build);
 		public signal void write_alpm_config_finished (bool checkspace);
 	}
 
@@ -136,7 +136,9 @@ namespace Pamac {
 			return true;
 		}
 
-		void on_write_pamac_config_finished (int refresh_period) {
+		void on_write_pamac_config_finished (bool recurse, int refresh_period, bool no_update_hide_icon,
+											bool enable_aur, bool search_aur, bool check_aur_updates,
+											bool no_confirm_build) {
 			launch_refresh_timeout ((uint) refresh_period);
 			if (refresh_period == 0) {
 				status_icon.visible = false;
-- 
GitLab