pamac-classic/pamac-tray.py

153 lines
5.0 KiB
Python
Raw Normal View History

#! /usr/bin/python3
2013-03-31 14:31:33 -03:00
# -*- coding:utf-8 -*-
2013-02-02 10:02:18 -03:00
2013-11-26 05:24:54 -03:00
# pamac - A Python implementation of alpm
2014-02-01 11:39:58 -03:00
# Copyright (C) 2013-2014 Guillaume Benoit <guillaume@manjaro.org>
2013-11-26 05:24:54 -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 2 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 copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from gi.repository import Gtk, GObject, Notify
from subprocess import call
2013-10-16 10:34:09 -03:00
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from threading import Thread
from time import sleep
2014-01-29 11:44:19 -03:00
from pamac import common, config
2013-02-11 11:45:24 -03:00
2013-03-31 14:31:33 -03:00
# i18n
import gettext
gettext.bindtextdomain('pamac', '/usr/share/locale')
gettext.textdomain('pamac')
_ = gettext.gettext
2013-09-17 03:06:32 -03:00
update_icon = '/usr/share/pamac/icons/24x24/status/pamac-update.png'
2013-03-31 14:31:33 -03:00
update_info = _('{number} available updates')
one_update_info = _('1 available update')
2013-09-17 03:06:32 -03:00
noupdate_icon = '/usr/share/pamac/icons/24x24/status/pamac-tray.png'
2013-03-31 14:31:33 -03:00
noupdate_info = _('Your system is up-to-date')
2013-07-19 10:14:09 -04:00
icon = noupdate_icon
info = noupdate_info
2013-03-18 07:07:58 -03:00
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()
2013-09-26 13:02:07 -03:00
self.statusIcon.set_visible(True)
2013-07-13 05:34:44 -04:00
2013-07-19 10:14:09 -04:00
self.menu = Gtk.Menu()
self.menuItem = Gtk.ImageMenuItem(_('Update Manager'))
2013-09-22 05:02:23 -03:00
self.menuItem.set_image(Gtk.Image.new_from_file('/usr/share/pamac/icons/16x16/apps/pamac-updater.png'))
2013-07-19 10:14:09 -04:00
self.menuItem.connect('activate', self.execute_update, self.statusIcon)
self.menu.append(self.menuItem)
self.menuItem = Gtk.ImageMenuItem(_('Package Manager'))
2013-09-22 05:02:23 -03:00
self.menuItem.set_image(Gtk.Image.new_from_file('/usr/share/pamac/icons/16x16/apps/pamac.png'))
2013-07-19 10:14:09 -04:00
self.menuItem.connect('activate', self.execute_manager, self.statusIcon)
self.menu.append(self.menuItem)
self.menuItem = Gtk.ImageMenuItem(_('Quit'))
2013-09-22 05:02:23 -03:00
self.menuItem.set_image(Gtk.Image.new_from_file('/usr/share/pamac/icons/16x16/apps/exit.png'))
2013-07-19 10:14:09 -04:00
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)
self.statusIcon.connect('activate', self.activate_cb)
2013-10-19 12:26:55 -03:00
self.update_icon(icon, info)
2013-07-19 10:14:09 -04:00
def execute_update(self, widget, event, data = None):
Thread(target = call, args = (['/usr/bin/pamac-updater'],)).start()
2013-07-19 10:14:09 -04:00
def execute_manager(self, widget, event, data = None):
Thread(target = call, args = (['/usr/bin/pamac-manager'],)).start()
2013-07-19 10:14:09 -04:00
def quit_tray(self, widget, data = None):
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-10 11:59:53 -03:00
2013-03-18 07:07:58 -03:00
def activate_cb(self, widget, data = None):
2013-07-13 05:34:44 -04:00
if icon == update_icon:
Thread(target = call, args = (['/usr/bin/pamac-updater'],)).start()
2013-03-18 07:07:58 -03:00
2013-02-16 13:27:46 -03:00
def update_icon(self, icon, info):
2013-10-16 10:34:09 -03:00
self.statusIcon.set_from_file(icon)
self.statusIcon.set_tooltip_markup(info)
2013-02-10 11:59:53 -03:00
2013-07-13 05:34:44 -04:00
def set_visible(self, boolean):
self.statusIcon.set_visible(boolean)
2013-10-16 10:34:09 -03:00
def refresh():
Thread(target = call, args = (['/usr/bin/pamac-refresh'],)).start()
return True
def check_updates():
Thread(target = call, args = (['/usr/bin/pamac-check_updates'],)).start()
locked = False
def check_pacman_running():
global locked
if locked:
if not common.lock_file_exists():
locked = False
check_updates()
else:
if common.lock_file_exists():
if not common.pid_file_exists():
locked = True
sleep(0.5)
return True
2013-02-16 13:27:46 -03:00
def set_icon(update_data):
2013-03-18 07:07:58 -03:00
global icon
global info
syncfirst, updates = update_data
2013-02-10 11:59:53 -03:00
if updates:
2013-03-18 07:07:58 -03:00
icon = update_icon
if len(updates) == 1:
2013-03-31 14:31:33 -03:00
info = one_update_info
else:
info = update_info.format(number = len(updates))
2013-07-13 05:34:44 -04:00
if not common.pid_file_exists():
Notify.Notification.new(_('Update Manager'), info, '/usr/share/pamac/icons/32x32/apps/pamac-updater.png').show()
2013-02-10 11:59:53 -03:00
else:
2013-03-18 07:07:58 -03:00
icon = noupdate_icon
info = noupdate_info
2013-02-22 12:43:52 -03:00
print(info)
2013-02-16 13:27:46 -03:00
tray.update_icon(icon, info)
2013-02-11 11:45:24 -03:00
2014-01-29 11:44:19 -03:00
def launch_refresh_timeout():
# GObject timeout is in milliseconds
global refresh_timeout_id
refresh_timeout_id = GObject.timeout_add(config.refresh_period*3600*1000, refresh)
def relaunch_refresh_timeout(msg):
config.pamac_conf.reload()
GObject.source_remove(refresh_timeout_id)
launch_refresh_timeout()
DBusGMainLoop(set_as_default = True)
2013-10-16 10:34:09 -03:00
bus = dbus.SystemBus()
bus.add_signal_receiver(set_icon, dbus_interface = "org.manjaro.pamac", signal_name = "EmitAvailableUpdates")
2014-01-29 15:30:58 -03:00
bus.add_signal_receiver(relaunch_refresh_timeout, dbus_interface = "org.manjaro.pamac", signal_name = "EmitReloadConfig")
2013-02-22 12:43:52 -03:00
tray = Tray()
Notify.init(_('Update Manager'))
2013-10-16 10:34:09 -03:00
refresh()
2014-01-29 11:44:19 -03:00
launch_refresh_timeout()
GObject.idle_add(check_pacman_running)
2013-02-22 12:43:52 -03:00
Gtk.main()