pamac-classic/src/tray.vala

384 lines
11 KiB
Vala
Raw Normal View History

2014-10-22 13:44:02 -03:00
/*
* pamac-vala
*
2016-02-02 05:28:07 -03:00
* Copyright (C) 2014-2016 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/>.
*/
// i18n
const string GETTEXT_PACKAGE = "pamac";
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 {
[DBus (name = "org.manjaro.pamac")]
interface Daemon : Object {
2016-04-14 13:19:20 -03:00
public abstract string get_lockfile () throws IOError;
2016-02-02 05:28:07 -03:00
public abstract void start_refresh (bool force) throws IOError;
public abstract void start_get_updates (bool check_aur_updates) throws IOError;
2015-06-19 12:48:34 -03:00
[DBus (no_reply = true)]
public abstract void quit () throws IOError;
2016-02-02 05:28:07 -03:00
public signal void get_updates_finished (Updates updates);
public signal void write_pamac_config_finished (bool recurse, uint64 refresh_period, bool no_update_hide_icon,
2015-08-24 11:13:18 -03:00
bool enable_aur, bool search_aur, bool check_aur_updates,
bool no_confirm_build);
2015-08-20 10:11:18 -03:00
public signal void write_alpm_config_finished (bool checkspace);
2014-10-22 13:44:02 -03:00
}
class TrayIcon: Gtk.Application {
2015-03-10 13:14:36 -03:00
Notify.Notification notification;
2016-02-02 05:28:07 -03:00
//~ Notification notification;
2014-10-22 13:44:02 -03:00
Daemon daemon;
bool extern_lock;
2014-10-22 13:44:02 -03:00
uint refresh_timeout_id;
Gtk.StatusIcon status_icon;
AppIndicator.Indicator indicator_status_icon;
2014-10-22 13:44:02 -03:00
Gtk.Menu menu;
GLib.File lockfile;
bool use_indicator;
2014-10-22 13:44:02 -03:00
public TrayIcon () {
application_id = "org.manjaro.pamac.tray";
flags = ApplicationFlags.FLAGS_NONE;
use_indicator = Environment.get_variable ("XDG_CURRENT_DESKTOP") == "KDE";
2014-10-22 13:44:02 -03:00
}
void start_daemon () {
try {
2016-02-02 05:28:07 -03:00
daemon = Bus.get_proxy_sync (BusType.SYSTEM, "org.manjaro.pamac", "/org/manjaro/pamac");
2015-08-20 10:11:18 -03:00
// Connecting to signals
2016-02-02 05:28:07 -03:00
daemon.get_updates_finished.connect (on_get_updates_finished);
2015-08-20 10:11:18 -03:00
daemon.write_pamac_config_finished.connect (on_write_pamac_config_finished);
daemon.write_alpm_config_finished.connect (on_write_alpm_config_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);
}
}
}
2014-10-22 13:44:02 -03:00
// Create menu for right button
void create_menu () {
menu = new Gtk.Menu ();
var item = new Gtk.MenuItem.with_label (_("Update Manager"));
2014-10-22 13:44:02 -03:00
item.activate.connect (execute_updater);
menu.append (item);
item = new Gtk.MenuItem.with_label (_("Package Manager"));
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 ();
if (use_indicator) {
indicator_status_icon.set_menu (menu);
}
2014-10-22 13:44:02 -03:00
}
// Show popup menu on right button
void menu_popup (uint button, uint time) {
menu.popup (null, null, null, button, time);
}
void left_clicked () {
2015-03-04 11:55:36 -03:00
if (status_icon.icon_name == "pamac-tray-update") {
2014-10-22 13:44:02 -03:00
execute_updater ();
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 void update_icon (string icon, string info) {
if (use_indicator) {
indicator_status_icon.set_icon_full (icon, icon);
} else {
status_icon.set_from_icon_name (icon);
status_icon.set_tooltip_markup (info);
}
2014-10-22 13:44:02 -03:00
}
bool start_refresh () {
2015-08-20 10:11:18 -03:00
// if pamac is not running start refresh else just check updates
if (check_pamac_running ()) {
check_updates ();
} else {
try {
2016-02-02 05:28:07 -03:00
daemon.start_refresh (false);
} catch (IOError e) {
stderr.printf ("IOError: %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_write_pamac_config_finished (bool recurse, uint64 refresh_period) {
launch_refresh_timeout (refresh_period);
2015-08-20 10:11:18 -03:00
if (refresh_period == 0) {
if (use_indicator) {
indicator_status_icon.set_status (AppIndicator.IndicatorStatus.PASSIVE);
} else {
status_icon.visible = false;
}
2015-08-20 10:11:18 -03:00
} else {
check_updates ();
}
}
void on_write_alpm_config_finished (bool checkspace) {
check_updates ();
}
2016-02-02 05:28:07 -03:00
void on_get_updates_finished (Updates updates) {
uint updates_nb = updates.repos_updates.length + updates.aur_updates.length;
if (updates_nb == 0) {
update_icon (noupdate_icon_name, noupdate_info);
2016-02-02 05:28:07 -03:00
var pamac_config = new Pamac.Config ("/etc/pamac.conf");
if (pamac_config.no_update_hide_icon) {
if (use_indicator) {
indicator_status_icon.set_status (AppIndicator.IndicatorStatus.PASSIVE);
} else {
status_icon.visible = false;
}
2016-02-02 05:28:07 -03:00
} else {
if (use_indicator) {
indicator_status_icon.set_status (AppIndicator.IndicatorStatus.ACTIVE);
} else {
status_icon.visible = true;
}
2016-02-02 05:28:07 -03:00
}
close_notification();
} else {
string info = ngettext ("%u available update", "%u available updates", updates_nb).printf (updates_nb);
this.update_icon (update_icon_name, info);
if (use_indicator) {
indicator_status_icon.set_status (AppIndicator.IndicatorStatus.ACTIVE);
} else {
status_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 check_updates () {
2015-03-07 07:22:33 -03:00
var pamac_config = new Pamac.Config ("/etc/pamac.conf");
2015-08-20 10:11:18 -03:00
if (pamac_config.refresh_period == 0) {
return;
}
2016-02-02 05:28:07 -03:00
try {
daemon.start_get_updates (pamac_config.enable_aur && pamac_config.check_aur_updates);
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
}
2014-10-22 13:44:02 -03:00
}
void show_notification (string info) {
2015-03-10 13:14:36 -03:00
//~ notification = new Notification (_("Update Manager"));
2014-10-30 10:44:09 -03:00
//~ notification.set_body (info);
2014-10-22 13:44:02 -03:00
//~ Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default ();
//~ Gdk.Pixbuf icon = icon_theme.load_icon ("system-software-update", 32, 0);
//~ notification.set_icon (icon);
//~ var action = new SimpleAction ("update", null);
//~ action.activate.connect (execute_updater);
//~ this.add_action (action);
//~ notification.add_button (_("Show available updates"), "app.update");
2016-02-02 05:28:07 -03:00
//~ notification.set_default_action ("app.update");
2014-10-22 13:44:02 -03:00
//~ this.send_notification (_("Update Manager"), notification);
try {
2016-02-02 05:28:07 -03:00
close_notification();
2015-03-10 13:14:36 -03:00
notification = new Notify.Notification (_("Update Manager"), info, "system-software-update");
2016-02-02 05:28:07 -03:00
notification.add_action ("default", _("Show available updates"), 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) {
if (notification.get_closed_reason() == -1 && notification.body != info) {
notification.update (_("Update Manager"), info, "system-software-update");
notification.show ();
}
} else {
show_notification (info);
}
} catch (Error e) {
stderr.printf ("Notify Error: %s", e.message);
}
}
void close_notification () {
try {
if (notification != null) {
notification.close();
notification = null;
}
} 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;
app = new Application ("org.manjaro.pamac.manager", 0);
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;
}
app = new Application ("org.manjaro.pamac.updater", 0);
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;
}
app = new Application ("org.manjaro.pamac.install", 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_pacman_running () {
if (extern_lock) {
2016-02-02 05:28:07 -03:00
if (!lockfile.query_exists ()) {
extern_lock = false;
// let the time to the daemon to update packages
Timeout.add (1000, () => {
check_updates ();
return false;
});
2014-10-22 13:44:02 -03:00
}
} else {
2016-02-02 05:28:07 -03:00
if (lockfile.query_exists ()) {
2015-08-20 10:11:18 -03:00
extern_lock = true;
2014-10-22 13:44:02 -03:00
}
}
return true;
}
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) {
2016-02-02 05:28:07 -03:00
refresh_timeout_id = Timeout.add_seconds ((uint) refresh_period_in_hours*3600, start_refresh);
2014-10-22 13:44:02 -03:00
}
}
public override void startup () {
// i18n
Intl.textdomain ("pamac");
Intl.setlocale (LocaleCategory.ALL, "");
2015-08-20 10:11:18 -03:00
var pamac_config = new Pamac.Config ("/etc/pamac.conf");
// 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;
if (use_indicator) {
indicator_status_icon = new AppIndicator.Indicator ("Update Manager", noupdate_icon_name, AppIndicator.IndicatorCategory.APPLICATION_STATUS);
indicator_status_icon.set_title (_("Update Manager"));
if (pamac_config.no_update_hide_icon) {
indicator_status_icon.set_status (AppIndicator.IndicatorStatus.PASSIVE);
} else {
indicator_status_icon.set_status (AppIndicator.IndicatorStatus.ACTIVE);
}
} else {
status_icon = new Gtk.StatusIcon ();
status_icon.visible = !(pamac_config.no_update_hide_icon);
status_icon.activate.connect (left_clicked);
status_icon.popup_menu.connect (menu_popup);
}
update_icon (noupdate_icon_name, noupdate_info);
2014-10-22 13:44:02 -03:00
create_menu ();
2014-10-30 10:44:09 -03:00
Notify.init (_("Update 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 {
lockfile = GLib.File.new_for_path (daemon.get_lockfile ());
} catch (IOError e) {
stderr.printf ("IOError: %s\n", e.message);
//try standard lock file
lockfile = GLib.File.new_for_path ("var/lib/pacman/db.lck");
}
2016-02-02 05:28:07 -03:00
Timeout.add (200, check_pacman_running);
start_refresh ();
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 ();
}
public override void activate () {
// nothing to do
}
static int main (string[] args) {
2014-10-22 13:44:02 -03:00
var tray_icon = new TrayIcon();
return tray_icon.run (args);
}
}
}