pamac-classic/src/pamac-tray/tray.vala

339 lines
9.0 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2017-10-10 16:29:22 -03:00
* Copyright (C) 2017 Chris Cromer <cromer@cromnix.org>
2017-01-03 05:43:19 -03:00
* Copyright (C) 2014-2017 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/>.
*/
const string update_icon_name = "pamac-tray-update";
const string noupdate_icon_name = "pamac-tray-no-update";
const string noupdate_info = _("Your system is up-to-date");
namespace Pamac {
2017-10-10 16:29:22 -03:00
[DBus (name = "org.pamac.user")]
2017-10-26 13:45:40 -03:00
interface UserDaemon : Object {
2019-07-06 20:28:51 -04:00
public abstract void refresh_handle () throws DBusError, IOError;
public abstract string get_lockfile () throws DBusError, IOError;
2017-10-26 13:45:40 -03:00
#if DISABLE_AUR
2019-07-06 20:28:51 -04:00
public abstract void start_get_updates () throws DBusError, IOError;
2017-10-26 13:45:40 -03:00
#else
2019-07-06 20:28:51 -04:00
public abstract void start_get_updates (bool check_aur_updates) throws DBusError, IOError;
2017-10-26 13:45:40 -03:00
#endif
[DBus (no_reply = true)]
2019-07-06 20:28:51 -04:00
public abstract void quit () throws DBusError, IOError;
2017-10-26 13:45:40 -03:00
public signal void get_updates_finished (Updates updates);
}
2014-10-22 13:44:02 -03:00
public abstract class TrayIcon: Gtk.Application {
2015-03-10 13:14:36 -03:00
Notify.Notification notification;
2017-07-29 10:35:04 -04:00
UserDaemon daemon;
bool extern_lock;
2014-10-22 13:44:02 -03:00
uint refresh_timeout_id;
public Gtk.Menu menu;
GLib.File lockfile;
2017-11-10 22:36:50 -03:00
bool updates_available;
2014-10-22 13:44:02 -03:00
2019-09-17 19:18:57 -03:00
protected TrayIcon () {
2017-10-10 16:29:22 -03:00
application_id = "org.pamac.tray";
2014-10-22 13:44:02 -03:00
flags = ApplicationFlags.FLAGS_NONE;
}
public abstract void init_status_icon ();
2014-10-22 13:44:02 -03:00
void start_daemon () {
try {
2017-10-10 16:29:22 -03:00
daemon = Bus.get_proxy_sync (BusType.SESSION, "org.pamac.user", "/org/pamac/user");
2016-02-02 05:28:07 -03:00
daemon.get_updates_finished.connect (on_get_updates_finished);
2014-10-22 13:44:02 -03:00
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
}
}
void stop_daemon () {
2016-02-02 05:28:07 -03:00
if (!check_pamac_running ()) {
2015-06-19 12:48:34 -03:00
try {
daemon.quit ();
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
2019-07-06 20:28:51 -04:00
} catch (DBusError e) {
stderr.printf ("DBusError: %s\n", e.message);
2015-06-19 12:48:34 -03:00
}
}
}
2014-10-22 13:44:02 -03:00
// Create menu for right button
void create_menu () {
menu = new Gtk.Menu ();
2017-07-29 10:35:04 -04:00
var item = new Gtk.MenuItem.with_label (_("Package Manager"));
2014-10-22 13:44:02 -03:00
item.activate.connect (execute_manager);
menu.append (item);
2014-10-30 10:44:09 -03:00
item = new Gtk.MenuItem.with_mnemonic (_("_Quit"));
2014-10-22 13:44:02 -03:00
item.activate.connect (this.release);
menu.append (item);
menu.show_all ();
2014-10-22 13:44:02 -03:00
}
public void left_clicked () {
if (get_icon () == "pamac-tray-update") {
2014-10-22 13:44:02 -03:00
execute_updater ();
} else {
execute_manager ();
2015-03-04 11:55:36 -03:00
}
2014-10-22 13:44:02 -03:00
}
void execute_updater () {
try {
2015-03-10 13:14:36 -03:00
Process.spawn_command_line_async ("pamac-updater");
} catch (SpawnError e) {
stderr.printf ("SpawnError: %s\n", e.message);
2014-10-22 13:44:02 -03:00
}
}
void execute_manager () {
try {
2015-03-10 13:14:36 -03:00
Process.spawn_command_line_async ("pamac-manager");
} catch (SpawnError e) {
stderr.printf ("SpawnError: %s\n", e.message);
2014-10-22 13:44:02 -03:00
}
}
public abstract void set_tooltip (string info);
public abstract void set_icon (string icon);
public abstract string get_icon ();
2017-11-10 22:36:50 -03:00
public abstract bool get_icon_visible ();
public abstract void set_icon_visible (bool visible);
2014-10-22 13:44:02 -03:00
2017-07-29 10:35:04 -04:00
bool check_updates () {
2017-11-10 22:36:50 -03:00
var pamac_config = new Pamac.Config ();
2017-07-29 10:35:04 -04:00
if (pamac_config.refresh_period != 0) {
try {
2017-10-10 16:29:22 -03:00
#if DISABLE_AUR
daemon.start_get_updates ();
#else
2017-07-29 10:35:04 -04:00
daemon.start_get_updates (pamac_config.enable_aur && pamac_config.check_aur_updates);
2017-10-10 16:29:22 -03:00
#endif
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
2019-07-06 20:28:51 -04:00
} catch (DBusError e) {
stderr.printf ("DBusError: %s\n", e.message);
}
2014-10-30 10:44:09 -03:00
}
2014-10-22 13:44:02 -03:00
return true;
}
2016-02-02 05:28:07 -03:00
void on_get_updates_finished (Updates updates) {
2017-10-10 16:29:22 -03:00
#if DISABLE_AUR
uint updates_nb = updates.repos_updates.length;
#else
2016-02-02 05:28:07 -03:00
uint updates_nb = updates.repos_updates.length + updates.aur_updates.length;
2017-10-10 16:29:22 -03:00
#endif
2016-02-02 05:28:07 -03:00
if (updates_nb == 0) {
2017-11-10 22:36:50 -03:00
updates_available = false;
set_icon (noupdate_icon_name);
set_tooltip (noupdate_info);
2017-11-10 22:36:50 -03:00
var pamac_config = new Pamac.Config ();
set_icon_visible (!pamac_config.no_update_hide_icon);
2017-10-03 11:07:12 -03:00
close_notification ();
2016-02-02 05:28:07 -03:00
} else {
2017-11-10 22:36:50 -03:00
updates_available = true;
2016-02-02 05:28:07 -03:00
string info = ngettext ("%u available update", "%u available updates", updates_nb).printf (updates_nb);
set_icon (update_icon_name);
set_tooltip (info);
set_icon_visible (true);
2016-02-02 05:28:07 -03:00
if (check_pamac_running ()) {
update_notification (info);
} else {
show_notification (info);
}
}
stop_daemon ();
}
2014-10-22 13:44:02 -03:00
void show_notification (string info) {
try {
2017-10-03 11:07:12 -03:00
close_notification ();
2017-07-29 10:35:04 -04:00
notification = new Notify.Notification (_("Package Manager"), info, "system-software-update");
notification.add_action ("default", _("Details"), execute_updater);
2014-10-22 13:44:02 -03:00
notification.show ();
} catch (Error e) {
stderr.printf ("Notify Error: %s", e.message);
}
}
2016-02-02 05:28:07 -03:00
void update_notification (string info) {
try {
if (notification != null) {
2017-10-03 11:07:12 -03:00
if (notification.get_closed_reason () == -1 && notification.body != info) {
2017-07-29 10:35:04 -04:00
notification.update (_("Package Manager"), info, "system-software-update");
2016-02-02 05:28:07 -03:00
notification.show ();
}
} else {
show_notification (info);
}
} catch (Error e) {
stderr.printf ("Notify Error: %s", e.message);
}
}
void close_notification () {
try {
2017-10-03 11:07:12 -03:00
if (notification != null && notification.get_closed_reason () == -1) {
notification.close ();
notification = null;
2016-02-02 05:28:07 -03:00
}
} catch (Error e) {
stderr.printf ("Notify Error: %s", e.message);
}
}
2014-10-22 13:44:02 -03:00
bool check_pamac_running () {
Application app;
bool run = false;
2017-10-10 16:29:22 -03:00
app = new Application ("org.pamac.manager", 0);
2014-10-22 13:44:02 -03:00
try {
app.register ();
} catch (GLib.Error e) {
stderr.printf ("%s\n", e.message);
}
run = app.get_is_remote ();
if (run) {
2014-10-22 13:44:02 -03:00
return run;
}
2017-10-10 16:29:22 -03:00
app = new Application ("org.pamac.installer", 0);
try {
app.register ();
} catch (GLib.Error e) {
stderr.printf ("%s\n", e.message);
}
run = app.get_is_remote ();
return run;
2014-10-22 13:44:02 -03:00
}
bool check_extern_lock () {
if (extern_lock) {
2016-02-02 05:28:07 -03:00
if (!lockfile.query_exists ()) {
extern_lock = false;
2017-07-29 10:35:04 -04:00
try {
daemon.refresh_handle ();
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
2019-07-06 20:28:51 -04:00
} catch (DBusError e) {
stderr.printf ("DBusError: %s\n", e.message);
2017-07-29 10:35:04 -04:00
}
check_updates ();
2014-10-22 13:44:02 -03:00
}
} else {
2016-02-02 05:28:07 -03:00
if (lockfile.query_exists ()) {
2017-08-22 06:11:31 -03:00
if (!check_pamac_running ()) {
extern_lock = true;
}
2014-10-22 13:44:02 -03:00
}
}
return true;
}
2017-11-10 22:36:50 -03:00
bool check_icon_hide_setting () {
var pamac_config = new Pamac.Config ();
if (!updates_available) {
2017-11-11 15:56:30 -03:00
if (get_icon_visible () && pamac_config.no_update_hide_icon) {
set_icon_visible (false);
} else if (!get_icon_visible () && !pamac_config.no_update_hide_icon) {
set_icon_visible (true);
}
2017-11-10 22:36:50 -03:00
}
return true;
}
void launch_icon_check_timeout () {
2017-11-11 15:56:30 -03:00
Timeout.add_seconds ((uint) 1, check_icon_hide_setting);
2017-11-10 22:36:50 -03:00
}
2016-02-02 05:28:07 -03:00
void launch_refresh_timeout (uint64 refresh_period_in_hours) {
2014-10-22 13:44:02 -03:00
if (refresh_timeout_id != 0) {
Source.remove (refresh_timeout_id);
2015-08-20 10:11:18 -03:00
refresh_timeout_id = 0;
}
if (refresh_period_in_hours != 0) {
2017-07-29 10:35:04 -04:00
refresh_timeout_id = Timeout.add_seconds ((uint) refresh_period_in_hours*3600, check_updates);
2014-10-22 13:44:02 -03:00
}
}
public override void startup () {
// i18n
2019-07-06 20:28:51 -04:00
Intl.bindtextdomain(Constants.GETTEXT_PACKAGE, Path.build_filename(Constants.DATADIR,"locale"));
2014-10-22 13:44:02 -03:00
Intl.setlocale (LocaleCategory.ALL, "");
2019-07-06 20:28:51 -04:00
Intl.textdomain(Constants.GETTEXT_PACKAGE);
Intl.bind_textdomain_codeset(Constants.GETTEXT_PACKAGE, "utf-8" );
2014-10-22 13:44:02 -03:00
2017-11-10 22:36:50 -03:00
var pamac_config = new Pamac.Config ();
2015-08-20 10:11:18 -03:00
// if refresh period is 0, just return so tray will exit
if (pamac_config.refresh_period == 0) {
return;
}
2014-10-22 13:44:02 -03:00
base.startup ();
extern_lock = false;
2014-10-22 13:44:02 -03:00
refresh_timeout_id = 0;
create_menu ();
init_status_icon ();
2017-05-14 09:59:00 -04:00
set_icon (noupdate_icon_name);
set_tooltip (noupdate_info);
set_icon_visible (!pamac_config.no_update_hide_icon);
2014-10-22 13:44:02 -03:00
2017-07-29 10:35:04 -04:00
Notify.init (_("Package Manager"));
2014-10-22 13:44:02 -03:00
2015-08-20 10:11:18 -03:00
start_daemon ();
2016-04-14 13:19:20 -03:00
try {
2019-07-19 08:42:19 -04:00
string path = daemon.get_lockfile ();
if (path != null) {
lockfile = GLib.File.new_for_path (path);
}
2016-04-14 13:19:20 -03:00
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
2019-07-19 08:32:30 -04:00
try_standard_lock ();
2019-07-06 20:28:51 -04:00
} catch (DBusError e) {
stderr.printf ("DBusError: %s\n", e.message);
2019-07-19 08:32:30 -04:00
try_standard_lock ();
2016-04-14 13:19:20 -03:00
}
Timeout.add (200, check_extern_lock);
2017-08-04 08:56:08 -04:00
// wait 30 seconds before check updates
Timeout.add_seconds (30, () => {
check_updates ();
return false;
});
2017-11-10 22:36:50 -03:00
launch_icon_check_timeout ();
2016-02-02 05:28:07 -03:00
launch_refresh_timeout (pamac_config.refresh_period);
2014-10-22 13:44:02 -03:00
this.hold ();
}
2019-07-19 08:42:19 -04:00
private void try_standard_lock () {
lockfile = GLib.File.new_for_path ("var/lib/pacman/db.lck");
2019-07-19 08:32:30 -04:00
}
2014-10-22 13:44:02 -03:00
public override void activate () {
// nothing to do
}
}
}