pamac-classic/pamac-tray

114 lines
3.1 KiB
Plaintext
Raw Normal View History

2013-02-10 11:59:53 -03:00
#! /usr/bin/python
# -*-coding:utf-8-*-
2013-02-02 10:02:18 -03:00
2013-02-10 11:59:53 -03:00
from gi.repository import Gtk
from subprocess import Popen
from pamac import transaction
2013-02-11 11:45:24 -03:00
import dbus
2013-02-16 13:27:46 -03:00
import threading
2013-02-11 11:45:24 -03:00
bus = dbus.SystemBus()
2013-02-10 11:59:53 -03:00
class Tray:
2013-02-16 13:27:46 -03:00
def __init__(self):
2013-02-10 11:59:53 -03:00
self.statusIcon = Gtk.StatusIcon()
self.statusIcon.set_visible(True)
self.menu = Gtk.Menu()
2013-02-16 13:27:46 -03:00
self.menuItem = Gtk.ImageMenuItem('Install/Check for updates')
2013-02-10 11:59:53 -03:00
self.menuItem.connect('activate', self.execute_update, self.statusIcon)
self.menu.append(self.menuItem)
2013-02-16 13:27:46 -03:00
self.menuItem = Gtk.ImageMenuItem('Run pamac-manager')
2013-02-10 11:59:53 -03:00
self.menuItem.connect('activate', self.execute_manager, self.statusIcon)
self.menu.append(self.menuItem)
self.menuItem = Gtk.ImageMenuItem('Quit')
self.menuItem.connect('activate', self.quit_tray, self.statusIcon)
self.menu.append(self.menuItem)
self.statusIcon.connect('popup-menu', self.popup_menu_cb, self.menu)
def execute_update(self, widget, event, data = None):
Popen(['/usr/bin/pamac-updater'])
def execute_manager(self, widget, event, data = None):
Popen(['/usr/bin/pamac-manager'])
def quit_tray(self, widget, data = None):
2013-02-16 13:27:46 -03:00
t.shutdown()
2013-02-10 11:59:53 -03:00
Gtk.main_quit()
def popup_menu_cb(self, widget, button, time, data = None):
if button == 3:
if data:
data.show_all()
data.popup(None, None, Gtk.StatusIcon.position_menu, self.statusIcon, 3, time)
2013-02-16 13:27:46 -03:00
def update_icon(self, icon, info):
self.statusIcon.set_from_file(icon)
self.statusIcon.set_tooltip_markup(info)
2013-02-10 11:59:53 -03:00
2013-02-16 13:27:46 -03:00
class PeriodicTask(threading.Thread):
"""Thread that executes a task every N seconds"""
def __init__(self):
threading.Thread.__init__(self)
self._finished = threading.Event()
self._interval = 60
def setInterval(self, interval):
"""Set the number of seconds we sleep between executing our task"""
self._interval = interval
def shutdown(self):
"""Stop this thread"""
self._finished.set()
def run(self):
while 1:
if self._finished.isSet():
return
self.task()
# sleep for interval or until shutdown
self._finished.wait(self._interval)
def task(self):
Popen(['/usr/bin/pamac-check-updates'])
def set_icon(*arg):
print('set-icon')
2013-02-10 11:59:53 -03:00
do_syncfirst, updates = transaction.get_updates()
if updates:
icon = '/usr/share/pamac/icons/24x24/status/update-normal.png'
info = str(len(updates))+' update(s) available'
else:
icon = '/usr/share/pamac/icons/24x24/status/update-enhancement.png'
info = ' No update available'
2013-02-16 13:27:46 -03:00
tray.update_icon(icon, info)
def handle_error(error):
global tray
print('error',error)
icon = '/usr/share/pamac/icons/24x24/status/update-enhancement.png'
info = ' No update available'
tray = Tray()
tray.update_icon(icon, info)
transaction.StopDaemon()
def handle_reply(reply):
global tray
tray = Tray()
set_icon()
2013-02-10 11:59:53 -03:00
transaction.StopDaemon()
def do_refresh():
"""Sync databases like pacman -Sy"""
transaction.get_handle()
transaction.Refresh(reply_handler = handle_reply, error_handler = handle_error, timeout = 2000*1000)
2013-02-16 13:27:46 -03:00
bus.add_signal_receiver(set_icon, dbus_interface = "org.manjaro.pamac", signal_name = "EmitAvailableUpdates")
2013-02-11 11:45:24 -03:00
2013-02-16 13:27:46 -03:00
tray = None
do_refresh()
t = PeriodicTask()
t.start()
#Gtk.main()