-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftware-update-icon.py
executable file
·141 lines (116 loc) · 4.87 KB
/
software-update-icon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# update-icon.py - Simple update notification system
#
# Copyright 2013 Ikey Doherty <[email protected]>
#
# 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., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import gi.repository
from gi.repository import Gtk, GObject, Notify
from gi.repository import PackageKitGlib as PackageKit
import gettext
import os
import sys
gettext.install("update-icon", "/usr/share/locale")
class UpdateIcon:
def __init__(self):
# Bail if we're in a live session
with open("/proc/cmdline", "r") as cmdline:
lines = " ".join(cmdline.readlines())
if "live" in lines:
sys.exit(0)
# in future we must support appindicators so that we're not X dependent
self.icon = Gtk.StatusIcon()
self.icon.set_from_icon_name("system-software-install")
self.icon.set_title(_("Software Update"))
self.icon.connect("popup-menu", self.popup)
self.icon.connect("activate", self.open_client)
# our menu
self.menu = Gtk.Menu()
reload = Gtk.MenuItem(_("Reload"))
reload.connect("activate", self.refresh)
self.menu.append(reload)
quit = Gtk.MenuItem(_("Quit"))
quit.connect("activate", Gtk.main_quit)
self.menu.append(quit)
self.menu.show_all()
# We'll reload our icon every 3 minutes
GObject.timeout_add (3 * (60 * 1000), self.refresh)
self.reported = False
try:
Notify.init('update-icon')
except:
# Should probably log this
print "Notification daemon could not be reached"
self.client = PackageKit.Client()
self.refresh()
def popup(self, source, button, time):
self.menu.popup(None, None, Gtk.StatusIcon.position_menu, self.icon, button, time)
def open_client(self, widg, user=None):
self.icon.set_visible(False)
GObject.idle_add(self._open_client)
def _open_client(self):
os.system("gpk-update-viewer")
# Essentially blocked until we return from the process
self._reload()
def refresh(self, wid=None):
''' Refresh possible updates '''
GObject.idle_add(self._reload)
return True
def _reload(self):
result = self.client.get_updates(PackageKit.FilterEnum.NONE, None, self.progress, None)
security = 0
packages = result.get_package_array()
updates = len(packages)
for package in packages:
name = package.get_name()
version = package.get_version()
if package.get_info() == PackageKit.InfoEnum.SECURITY:
security += 1
if security >= 1:
self.icon.set_from_icon_name("software-update-urgent")
self.icon.set_visible(True)
# Show notification
update_str = _("There is a new security update available") if security == 1 else \
_("There are %d security updates available") % security
self.icon.set_tooltip_text(update_str)
if not self.reported:
notif = Notify.Notification.new(_('Security update'), update_str, 'software-update-urgent')
notif.show()
self.reported = True
else:
if updates >= 1:
self.icon.set_from_icon_name("software-update-available")
self.icon.set_visible(True)
# Show notification
update_str = _("There is a new software update available") if updates == 1 else \
_("There are %d software updates available") % updates
self.icon.set_tooltip_text(update_str)
if not self.reported:
notif = Notify.Notification.new(_('Software update'), update_str, 'software-update-available')
notif.show()
self.reported = True
else:
self.icon.set_tooltip_text(_("All software is up to date"))
self.icon.set_visible(False)
def progress(self, progress, type, user_data=None):
pass
if __name__ == "__main__":
icon = UpdateIcon()
Gtk.main()