Skip to content
Snippets Groups Projects
manager.vala 3.45 KiB
Newer Older
guinux's avatar
guinux committed
/*
 *  pamac-vala
 *
guinux's avatar
guinux committed
 *  Copyright (C) 2014-2017 Guillaume Benoit <guillaume@manjaro.org>
guinux's avatar
guinux committed
 *
 *  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 {

guinux's avatar
guinux committed
	class Manager : Gtk.Application {
guinux's avatar
guinux committed
		ManagerWindow manager_window;
guinux's avatar
guinux committed
		bool pamac_run;
guinux's avatar
guinux committed
		bool started;
guinux's avatar
guinux committed

		public Manager () {
			application_id = "org.manjaro.pamac.manager";
guinux's avatar
guinux committed
			flags = ApplicationFlags.HANDLES_COMMAND_LINE;
guinux's avatar
guinux committed
		}

		public override void startup () {
			// i18n
			Intl.textdomain ("pamac");
			Intl.setlocale (LocaleCategory.ALL, "");

			base.startup ();

guinux's avatar
guinux committed
			pamac_run = check_pamac_running ();
			if (pamac_run) {
				var msg = new Gtk.MessageDialog (null,
												Gtk.DialogFlags.MODAL,
												Gtk.MessageType.ERROR,
												Gtk.ButtonsType.OK,
												dgettext (null, "Pamac is already running"));
				msg.run ();
				msg.destroy ();
guinux's avatar
guinux committed
			} else {
guinux's avatar
guinux committed
				manager_window = new ManagerWindow (this);
guinux's avatar
guinux committed
				// quit accel
guinux's avatar
guinux committed
				var action =  new SimpleAction ("quit", null);
				action.activate.connect  (() => {this.quit ();});
				this.add_action (action);
				string[] accels = {"<Ctrl>Q", "<Ctrl>W"};
guinux's avatar
guinux committed
				this.set_accels_for_action ("app.quit", accels);
guinux's avatar
guinux committed
				// back accel
				action =  new SimpleAction ("back", null);
				action.activate.connect  (() => {manager_window.on_button_back_clicked ();});
				this.add_action (action);
				accels = {"<Alt>Left"};
				this.set_accels_for_action ("app.back", accels);
				// search accel
				action =  new SimpleAction ("search", null);
				action.activate.connect  (() => {manager_window.search_button.activate ();});
				this.add_action (action);
				accels = {"<Ctrl>F"};
				this.set_accels_for_action ("app.search", accels);
guinux's avatar
guinux committed
			}
guinux's avatar
guinux committed
		}

guinux's avatar
guinux committed
		public override int command_line (ApplicationCommandLine cmd) {
guinux's avatar
guinux committed
			// fix #367
			if (manager_window == null) {
				return 1;
			}
guinux's avatar
guinux committed
			if (cmd.get_arguments ()[0] == "pamac-updater") {
				if (!started) {
					manager_window.update_lists ();
					started = true;
				}
guinux's avatar
guinux committed
				manager_window.display_package_queue.clear ();
				manager_window.main_stack.visible_child_name = "browse";
				manager_window.filters_stack.visible_child_name = "updates";
			} else if (!started) {
				manager_window.update_lists ();
				manager_window.refresh_packages_list ();
guinux's avatar
guinux committed
				started = true;
			}
guinux's avatar
guinux committed
			if (!pamac_run) {
guinux's avatar
guinux committed
				manager_window.present ();
guinux's avatar
guinux committed
				while (Gtk.events_pending ()) {
guinux's avatar
guinux committed
					Gtk.main_iteration ();
guinux's avatar
guinux committed
				}
guinux's avatar
guinux committed
			}
guinux's avatar
guinux committed
			return 0;
guinux's avatar
guinux committed
		}

		public override void shutdown () {
			base.shutdown ();
guinux's avatar
guinux committed
			if (!pamac_run) {
guinux's avatar
guinux committed
				manager_window.transaction.stop_daemon ();
guinux's avatar
guinux committed
			}
guinux's avatar
guinux committed
		}

		bool check_pamac_running () {
			Application app;
			bool run = false;
guinux's avatar
guinux committed
			app = new Application ("org.manjaro.pamac.installer", 0);
			try {
				app.register ();
			} catch (GLib.Error e) {
				stderr.printf ("%s\n", e.message);
			}
			run = app.get_is_remote ();
			return run;
guinux's avatar
guinux committed
		}
	}

guinux's avatar
guinux committed
	static int main (string[] args) {
guinux's avatar
guinux committed
		var manager = new Manager ();
		return manager.run (args);
	}
}