From c136308d98128ab24e8512ed8e4c2b8b21d971f0 Mon Sep 17 00:00:00 2001
From: guinux <nuxgui@gmail.com>
Date: Wed, 19 Jun 2019 23:20:57 +0200
Subject: [PATCH] always refresh before installing

---
 src/cli.vala         | 10 ++++-
 src/transaction.vala | 89 ++++++++++++++++++++++++++++++--------------
 2 files changed, 71 insertions(+), 28 deletions(-)

diff --git a/src/cli.vala b/src/cli.vala
index fa73059b..683aa5ff 100644
--- a/src/cli.vala
+++ b/src/cli.vala
@@ -2347,7 +2347,15 @@ namespace Pamac {
 		}
 
 		void start_transaction () {
-			transaction.start (to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files);
+			if (to_install.length > 0 && Posix.geteuid () != 0) {
+				// let's time to pkttyagent to get registred
+				Timeout.add (200, () => {
+					transaction.start (to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files);
+					return false;
+				});
+			} else {
+				transaction.start (to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files);
+			}
 			loop.run ();
 		}
 
diff --git a/src/transaction.vala b/src/transaction.vala
index bec2c677..b64b1f8e 100644
--- a/src/transaction.vala
+++ b/src/transaction.vala
@@ -255,6 +255,62 @@ namespace Pamac {
 			transaction_interface.start_set_pkgreason (pkgname, reason);
 		}
 
+		void on_refresh_for_install_finished (bool success) {
+			stop_downloading ();
+			current_filename = "";
+			transaction_interface.refresh_finished.disconnect (on_refresh_for_install_finished);
+			if (!success) {
+				on_trans_prepare_finished (false);
+			} else {
+				// choose optdeps
+				var to_add_to_install = new GenericSet<string?> (str_hash, str_equal);
+				foreach (unowned string name in this.to_install) {
+					// do not check if reinstall
+					if (!database.is_installed_pkg (name)) {
+						List<string> uninstalled_optdeps = database.get_uninstalled_optdeps (name);
+						var real_uninstalled_optdeps = new GenericArray<string> ();
+						foreach (unowned string optdep in uninstalled_optdeps) {
+							string optdep_name = optdep.split (": ", 2)[0];
+							if (!(optdep_name in this.to_install) && !(optdep_name in to_add_to_install)) {
+								real_uninstalled_optdeps.add (optdep);
+							}
+						}
+						if (real_uninstalled_optdeps.length > 0) {
+							foreach (unowned string optdep in choose_optdeps (name, real_uninstalled_optdeps.data)) {
+								string optdep_name = optdep.split (": ", 2)[0];
+								to_add_to_install.add (optdep_name);
+							}
+						}
+					}
+				}
+				foreach (unowned string name in to_add_to_install) {
+					this.to_install += name;
+					this.to_mark_as_dep += name;
+				}
+				emit_action (dgettext (null, "Preparing") + "...");
+				trans_prepare_real ();
+			}
+		}
+
+		void launch_refresh_for_install (bool authorized) {
+			get_authorization_finished.disconnect (launch_refresh_for_sysupgrade);
+			if (authorized) {
+				emit_action (dgettext (null, "Synchronizing package databases") + "...");
+				connecting_signals ();
+				transaction_interface.refresh_finished.connect (on_refresh_for_install_finished);
+				transaction_interface.start_refresh (force_refresh);
+				start_downloading ();
+			} else {
+				on_refresh_for_install_finished (false);
+			}
+		}
+
+		void start_refresh_for_install () {
+			// check autorization to send start_downloading signal after that
+			get_authorization_finished.connect (launch_refresh_for_install);
+			start_get_authorization ();
+		}
+
 		void on_refresh_for_sysupgrade_finished (bool success) {
 			stop_downloading ();
 			current_filename = "";
@@ -738,34 +794,13 @@ namespace Pamac {
 			this.temporary_ignorepkgs = temporary_ignorepkgs;
 			this.overwrite_files = overwrite_files;
 			this.to_mark_as_dep = {};
-			// choose optdeps
-			var to_add_to_install = new GenericSet<string?> (str_hash, str_equal);
-			foreach (unowned string name in this.to_install) {
-				// do not check if reinstall
-				if (!database.is_installed_pkg (name)) {
-					List<string> uninstalled_optdeps = database.get_uninstalled_optdeps (name);
-					var real_uninstalled_optdeps = new GenericArray<string> ();
-					foreach (unowned string optdep in uninstalled_optdeps) {
-						string optdep_name = optdep.split (": ", 2)[0];
-						if (!(optdep_name in this.to_install) && !(optdep_name in to_add_to_install)) {
-							real_uninstalled_optdeps.add (optdep);
-						}
-					}
-					if (real_uninstalled_optdeps.length > 0) {
-						foreach (unowned string optdep in choose_optdeps (name, real_uninstalled_optdeps.data)) {
-							string optdep_name = optdep.split (": ", 2)[0];
-							to_add_to_install.add (optdep_name);
-						}
-					}
-				}
-			}
-			foreach (unowned string name in to_add_to_install) {
-				this.to_install += name;
-				this.to_mark_as_dep += name;
+			if (this.to_install.length > 0) {
+				start_refresh_for_install ();
+			} else {
+				emit_action (dgettext (null, "Preparing") + "...");
+				connecting_signals ();
+				trans_prepare_real ();
 			}
-			emit_action (dgettext (null, "Preparing") + "...");
-			connecting_signals ();
-			trans_prepare_real ();
 		}
 
 		void start_commit () {
-- 
GitLab