From 5e3029ddcdad0309cfcb8663a67e277eba4a496d Mon Sep 17 00:00:00 2001
From: guinux <nuxgui@gmail.com>
Date: Thu, 20 Jun 2019 11:08:20 +0200
Subject: [PATCH] add launch button

---
 resources/manager_window.ui | 68 ++++++++++++++++++++++++++++---------
 src/database.vala           | 10 ++++++
 src/manager_window.vala     | 25 ++++++++++++--
 src/package.vala            |  1 +
 src/transaction-gtk.vala    | 10 +++---
 5 files changed, 90 insertions(+), 24 deletions(-)

diff --git a/resources/manager_window.ui b/resources/manager_window.ui
index 5530b56a..301c1777 100644
--- a/resources/manager_window.ui
+++ b/resources/manager_window.ui
@@ -820,7 +820,10 @@
                       <object class="GtkBox" id="properties_box1">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
-                        <property name="border_width">18</property>
+                        <property name="margin_left">18</property>
+                        <property name="margin_right">18</property>
+                        <property name="margin_top">18</property>
+                        <property name="margin_bottom">12</property>
                         <property name="spacing">18</property>
                         <child>
                           <object class="GtkBox" id="properties_box2">
@@ -893,6 +896,22 @@
                             <property name="position">0</property>
                           </packing>
                         </child>
+                        <child>
+                          <object class="GtkButton" id="launch_button">
+                            <property name="label" translatable="yes">Launch</property>
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="receives_default">True</property>
+                            <property name="halign">end</property>
+                            <property name="valign">start</property>
+                            <signal name="clicked" handler="on_launch_button_clicked" swapped="no"/>
+                          </object>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">True</property>
+                            <property name="position">1</property>
+                          </packing>
+                        </child>
                         <child>
                           <object class="GtkButtonBox" id="details_buttonbox">
                             <property name="visible">True</property>
@@ -960,21 +979,6 @@
                                 <property name="position">3</property>
                               </packing>
                             </child>
-                            <child>
-                              <object class="GtkButton" id="reset_files_button">
-                                <property name="label" translatable="yes">Reset build files</property>
-                                <property name="visible">True</property>
-                                <property name="can_focus">True</property>
-                                <property name="focus_on_click">False</property>
-                                <property name="receives_default">False</property>
-                                <signal name="clicked" handler="on_reset_files_button_clicked" swapped="no"/>
-                              </object>
-                              <packing>
-                                <property name="expand">False</property>
-                                <property name="fill">True</property>
-                                <property name="position">4</property>
-                              </packing>
-                            </child>
                           </object>
                           <packing>
                             <property name="expand">False</property>
@@ -1208,6 +1212,38 @@
                             <property name="position">2</property>
                           </packing>
                         </child>
+                        <child>
+                          <object class="GtkBox" id="build_files_box">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="orientation">vertical</property>
+                            <property name="spacing">12</property>
+                            <child>
+                              <object class="GtkButton" id="reset_files_button">
+                                <property name="label" translatable="yes">Reset build files</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="focus_on_click">False</property>
+                                <property name="receives_default">False</property>
+                                <property name="halign">end</property>
+                                <property name="margin_right">18</property>
+                                <signal name="clicked" handler="on_reset_files_button_clicked" swapped="no"/>
+                              </object>
+                              <packing>
+                                <property name="expand">False</property>
+                                <property name="fill">True</property>
+                                <property name="position">0</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                          </object>
+                          <packing>
+                            <property name="name">build_files</property>
+                            <property name="position">3</property>
+                          </packing>
+                        </child>
                       </object>
                       <packing>
                         <property name="expand">False</property>
diff --git a/src/database.vala b/src/database.vala
index 21ce7f22..c50a6979 100644
--- a/src/database.vala
+++ b/src/database.vala
@@ -334,6 +334,14 @@ namespace Pamac {
 			return get_localized_string (app.get_names ());
 		}
 
+		string get_app_launchable (As.App app) {
+			As.Launchable? launchable = app.get_launchable_by_kind (As.LaunchableKind.DESKTOP_ID);
+			if (launchable != null) {
+				return launchable.get_value ();
+			}
+			return "";
+		}
+
 		string get_app_summary (As.App app) {
 			return get_localized_string (app.get_comments ());
 		}
@@ -1211,6 +1219,7 @@ namespace Pamac {
 								found = true;
 								if (app.get_pkgname_default () == alpm_pkg.name) {
 									details.app_name = appname;
+									details.launchable = get_app_launchable (app);
 									details.desc = get_app_summary (app);
 									try {
 										details.long_desc = As.markup_convert_simple (get_app_description (app));
@@ -1228,6 +1237,7 @@ namespace Pamac {
 						if (matching_apps.length () == 1) {
 							As.App app = matching_apps.nth_data (0);
 							details.app_name = get_app_name (app);
+							details.launchable = get_app_launchable (app);
 							details.desc = get_app_summary (app);
 							try {
 								details.long_desc = As.markup_convert_simple (get_app_description (app));
diff --git a/src/manager_window.vala b/src/manager_window.vala
index 1db50405..00429f42 100644
--- a/src/manager_window.vala
+++ b/src/manager_window.vala
@@ -302,6 +302,8 @@ namespace Pamac {
 		[GtkChild]
 		Gtk.Stack properties_stack;
 		[GtkChild]
+		Gtk.Box build_files_box;
+		[GtkChild]
 		Gtk.ListBox properties_listbox;
 		[GtkChild]
 		Gtk.Grid deps_grid;
@@ -322,6 +324,8 @@ namespace Pamac {
 		[GtkChild]
 		Gtk.Label licenses_label;
 		[GtkChild]
+		Gtk.Button launch_button;
+		[GtkChild]
 		Gtk.ToggleButton remove_togglebutton;
 		[GtkChild]
 		Gtk.ToggleButton reinstall_togglebutton;
@@ -346,6 +350,7 @@ namespace Pamac {
 
 		public Queue<string> display_package_queue;
 		string current_package_displayed;
+		string current_launchable;
 		string current_files;
 		string current_build_files;
 		GenericSet<string?> previous_to_install;
@@ -489,7 +494,7 @@ namespace Pamac {
 			main_stack.add_named (transaction.details_window, "term");
 			transaction_infobox.pack_start (transaction.progress_box);
 			// integrate build files notebook
-			properties_stack.add_named (transaction.build_files_notebook, "build_files");
+			build_files_box.add (transaction.build_files_notebook);
 
 			display_package_queue = new Queue<string> ();
 			to_install = new GenericSet<string?> (str_hash, str_equal);
@@ -807,7 +812,7 @@ namespace Pamac {
 		}
 
 		Gtk.Widget populate_details_grid (string detail_type, string detail, Gtk.Widget? previous_widget) {
-			var label = new Gtk.Label ("<b>%s</b>".printf (detail_type + ":"));
+			var label = new Gtk.Label ("<b>%s:</b>".printf (detail_type));
 			label.use_markup = true;
 			label.halign = Gtk.Align.START;
 			label.valign = Gtk.Align.START;
@@ -863,7 +868,7 @@ namespace Pamac {
 		}
 
 		Gtk.Widget populate_dep_grid (string dep_type, List<string> dep_list, Gtk.Widget? previous_widget, bool add_install_button = false) {
-			var label = new Gtk.Label ("<b>%s</b>".printf (dep_type + ":"));
+			var label = new Gtk.Label ("<b>%s:</b>".printf (dep_type));
 			label.use_markup = true;
 			label.halign = Gtk.Align.START;
 			label.valign = Gtk.Align.START;
@@ -1000,6 +1005,10 @@ namespace Pamac {
 			}
 			licenses_label.set_text (licenses.str);
 			if (details.installed_version != "") {
+				if (details.launchable != "") {
+					launch_button.visible = true;
+					current_launchable = details.launchable;
+				}
 				install_togglebutton.visible = false;
 				build_togglebutton.visible = false;
 				reset_files_button.visible = false;
@@ -1035,6 +1044,7 @@ namespace Pamac {
 					}
 				}
 			} else {
+				launch_button.visible = false;
 				remove_togglebutton.visible = false;
 				reinstall_togglebutton.visible = false;
 				build_togglebutton.visible = false;
@@ -1322,6 +1332,15 @@ namespace Pamac {
 			}
 		}
 
+		[GtkCallback]
+		void on_launch_button_clicked () {
+			try {
+				Process.spawn_command_line_sync ("gtk-launch %s".printf (current_launchable));
+			} catch (SpawnError e) {
+				stderr.printf ("SpawnError: %s\n", e.message);
+			}
+		}
+
 		[GtkCallback]
 		void on_install_togglebutton_toggled () {
 			if (install_togglebutton.active) {
diff --git a/src/package.vala b/src/package.vala
index bb40f6f0..12ce7ac6 100644
--- a/src/package.vala
+++ b/src/package.vala
@@ -74,6 +74,7 @@ namespace Pamac {
 		internal List<string> backups_priv;
 		public string name { get; internal set; default = "";}
 		public string app_name { get; internal set; default = "";}
+		public string launchable { get; internal set; default = "";}
 		public string version { get; internal set; default = "";}
 		public string installed_version { get; internal set; default = "";}
 		public string desc { get; internal set; default = "";}
diff --git a/src/transaction-gtk.vala b/src/transaction-gtk.vala
index 0f93b5a7..8d2fd380 100644
--- a/src/transaction-gtk.vala
+++ b/src/transaction-gtk.vala
@@ -471,9 +471,9 @@ namespace Pamac {
 				string action = dgettext (null, "Edit %s build files".printf (pkgname));
 				display_action (action);
 				// remove noteboook from manager_window properties stack
-				unowned Gtk.Stack? stack = build_files_notebook.get_parent () as Gtk.Stack;
-				if (stack != null) {
-					stack.remove (build_files_notebook);
+				unowned Gtk.Box? manager_box = build_files_notebook.get_parent () as Gtk.Box;
+				if (manager_box != null) {
+					manager_box.remove (build_files_notebook);
 				}
 				// create dialog
 				var flags = Gtk.DialogFlags.MODAL;
@@ -504,8 +504,8 @@ namespace Pamac {
 				int response = dialog.run ();
 				// re-add noteboook to manager_window properties stack
 				box.remove (build_files_notebook);
-				if (stack != null) {
-					stack.add_named (build_files_notebook, "build_files");
+				if (manager_box != null) {
+					manager_box.add (build_files_notebook);
 				}
 				dialog.destroy ();
 				if (response == Gtk.ResponseType.CLOSE) {
-- 
GitLab