PDA

View Full Version : Yum Notification Daemon for XFCE!



Cuvou
21st May 2013, 11:05 PM
I don't think I posted about this on here before, but I wrote a custom update notification daemon to use on XFCE about 3 versions ago of Fedora.

It's a simple Perl Gtk2 script that looks for available updates, and puts an icon in your Notification Area along with a notification pop-up when updates become available (just like the old Gnome-PackageKit). Clicking the icon will start yumex (you can replace this with your preferred graphical package updater. When I first wrote it, gpk-update-viewer was still used in the XFCE spin, but they moved to yumex as of now).

Downloadable link: http://sh.kirsle.net/kupdatesd

It requires some Perl Gtk2 modules:


yum install perl-Gtk2 perl-Gtk2-TrayIcon perl-Gtk2-Notify

Put it in your session startup applications and it will go to work. :)

Source code:


#!/usr/bin/perl

# kupdatesd - A simple yum update checker.
#
# This script will watch for available yum updates and show a GTK+ TrayIcon
# and notification pop-up when updates become available.
#
# This is intended for desktop environments like XFCE that don't have a native
# PackageKit update watcher.
#
# Set this script to run on session startup and it will check for updates every
# 5 minutes (by default; this is configurable in the source code).
#
# --Kirsle
# http://sh.kirsle.net

use 5.14.0;
use strict;
use warnings;
use Gtk2 -init;
use Gtk2::TrayIcon;
use Gtk2::Notify -init, "kupdatesd";

$SIG{HUP} = $SIG{TERM} = sub {
exec($0);
};

################################################## ##############################
# Configuration Section #
################################################## ##############################

my %c = (
# The title to be shown on the pop-up and the icon tooltip.
title => "Updates Available",

# The message to be shown in the pop-up.
message => "There are updates ready to install.",

# The icon to use for the pop-up and tray icon.
icon => '/usr/share/icons/gnome/32x32/status/software-update-available.png',

# How often to check for updates (in seconds).
interval => 900,

# The path to your yum binary.
yum => '/usr/bin/yum',

# The path to your graphical updater.
# gpk-update-viewer is provided by gnome-packagekit
gui => '/usr/bin/yumex',
);

################################################## ##############################
# End Configuration Section #
################################################## ##############################

# Gtk objects
my ($icon, $image, $eventbox, $tooltip, $notify);
my $visible = 0; # Icon is currently being displayed?

# Enter the main loop.
my $check = time() + $c{interval};
while (1) {
select(undef,undef,undef,0.1);

# Keep Gtk2 active.
if (defined $icon) {
Gtk2->main_iteration while Gtk2->events_pending;
}

if (time() > $check) {
# Look for updates.
system("$c{yum} check-update > /dev/null 2>&1");
if (!$visible && $? >> 8 == 100) {
say "There are updates available!";
show_icon();
}
elsif ($visible && $? == 0) {
# Updates have gone away behind our back!
$icon->hide;
$visible = 0;
}

# Queue another check.
$check = time() + $c{interval};
}
}

sub show_icon {
# Already initialized this once before?
if (defined $icon) {
# Just show the icon and notification again.
$icon->show_all;
$notify->show;
$visible = 1;
return;
}

# Tray icon. Image goes in EventBox, EventBox goes inside TrayIcon.
$icon = Gtk2::TrayIcon->new("kupdatesd");
$image = Gtk2::Image->new_from_file($c{icon});
$eventbox = Gtk2::EventBox->new;
$eventbox->add($image);
$icon->add($eventbox);
$icon->show_all;

# Attach the tooltip.
$tooltip = Gtk2::Tooltips->new;
$tooltip->set_tip($icon, $c{title});

$eventbox->signal_connect("button_press_event", sub {
$icon->hide;
$visible = 0;
system($c{gui});
});

$notify = Gtk2::Notify->new(
$c{title},
$c{message},
$c{icon},
);
$notify->show;
$visible = 1;
}

nirik
24th May 2013, 03:02 AM
Nice. :)

We recently dropped perl from the Xfce spin to save space... would you have any interest in porting to python? :) Otherwise, you might also mention it on the fedora xfce list for interested folks?

http://lists.fedoraproject.org/mailman/listinfo/xfce

Cuvou
24th May 2013, 10:05 PM
I've ported it to Python!

http://sh.kirsle.net/pyupdatesd

I couldn't find a method of sending GTK notification pop-ups in Python using the standard pygtk2... so this port of the script runs the notify-send command to do that instead (I did see pynotify, but it doesn't appear to be in the yum repos whereas pygtk2 was preinstalled on my XFCE desktops).

This is very lightly tested, so testers are welcome. :) I'll mention it on the XFCE mailing list too.


#!/usr/bin/env python

"""
pyupdatesd: A simple yum update checker.

This script will watch for available yum updates and show a Gtk2 tray icon and
notification pop-up when updates become available.

This is intended for desktop environments like XFCE that don't have a native
PackageKit update watcher.

Set this script to run on session startup, and it will check for updates every
15 minutes (by default; this is configurable in the source code).

This software is open domain and may be freely modified and distributed.

Requires: pygtk2

--Kirsle
http://sh.kirsle.net
"""

################################################## ##############################
# Configuration Section Begins Here #
################################################## ##############################

c = dict(
# The title to be shown on the pop-up and the icon tooltip.
title = "Updates Available",

# The message to be shown in the pop-up.
message = "There are updates available to install.",

# Icon to use in the system tray and pop-up.
icon = "/usr/share/icons/gnome/32x32/status/software-update-available.png",

# Frequency to check for available updates.
interval = 900, # 15 minutes

# Command to run to check for available updates, and the expected status
# code that indicates updates are available.
check = "/usr/bin/yum check-update",

# Path to notify-send (set to None if you don't want notifications)
notify = "/usr/bin/notify-send",

# Command to run for your graphical updater (i.e. yumex, gpk-update-viewer)
gui = "/usr/bin/yumex",
)

################################################## ##############################
# Configuration Section Ends Here #
################################################## ##############################

import gtk
import gobject

import commands
import subprocess
from time import time

def do_updates():
"""Show your graphical update manager."""
subprocess.call(c['gui'], shell=True)

def onClick(widget):
"""Event handler for the tray icon being clicked."""
widget.set_visible(False)
gobject.timeout_add(1, do_updates)

def show_notify():
subprocess.call([c['notify'],
'-a', __name__,
'-i', c['icon'],
c['message'],
])

tray = gtk.StatusIcon()
tray.set_from_file(c['icon'])
tray.set_title(c['title'])
tray.set_tooltip(c['title'])
tray.set_visible(False)
tray.connect('activate', onClick)

next_check = int(time()) + c['interval']
def main_loop():
# Time to check?
global next_check
if int(time()) >= next_check:
status, output = commands.getstatusoutput(c['check'])
status = status >> 8

# Updates?
if status == 100:
tray.set_visible(True)
show_notify()
elif tray.get_visible() == True and status == 0:
# Updates have disappeared behind our back!
tray.set_visible(False)

next_check = int(time()) + c['interval']

gobject.timeout_add(1000, main_loop)

gobject.timeout_add(1000, main_loop)

gtk.main()

# vim:expandtab