PushService: Initial release
[enigma2-plugins.git] / pushservice / src / Plugins / IPKGUpdateNotification.py
1 # Config
2 from Components.config import ConfigYesNo, ConfigText, ConfigNumber, NoSave
3
4 # Plugin internal
5 from Plugins.Extensions.PushService.__init__ import _
6 from Plugins.Extensions.PushService.PluginBase import PluginBase
7
8 # Plugin specific
9 import os
10 from time import time
11 from Plugins.SystemPlugins.SoftwareManager.SoftwareTools import iSoftwareTools
12
13
14 SUBJECT = _("IPKG Update Notification")
15 BODY    = _("There are updates available:\n%s")
16
17
18 class IPKGUpdateNotification(PluginBase):
19         
20         ForceSingleInstance = True
21         
22         def __init__(self):
23                 # Is called on instance creation
24                 PluginBase.__init__(self)
25                 
26                 # Default configuration
27                 self.setOption( 'selfcheck', NoSave(ConfigYesNo( default = False )), _("Start update check if not done yet") )
28
29         def run(self):
30                 # Return Header, Body, Attachment
31                 # If empty or none is returned, nothing will be sent
32                 # Adapted from Software Manager
33                 if iSoftwareTools.lastDownloadDate is not None and iSoftwareTools.lastDownloadDate > ( time() - (24*60*60) ):
34                         # Last refresh was within one day
35                         updates = self.buildList()
36                         if updates:
37                                 return SUBJECT, BODY % (updates)
38                 else:
39                         print "IPKGUpdateNotification run else"
40                         if self.getValue('selfcheck'):
41                                 # Refresh package list
42                                 iSoftwareTools.startSoftwareTools(self.getUpdateInfosCB)
43                                 #TODO async
44
45         def getUpdateInfosCB(self, retval = None):
46                 if retval is not None:
47                         if retval is True:
48                                 if iSoftwareTools.available_updates is not 0:
49                                         # _("There are at least ") + str(iSoftwareTools.available_updates) + _(" updates available.")
50                                         print "Updates available."
51                                         return self.buildList()
52                                 else:
53                                         # _("There are no updates available.")
54                                         print "There are no updates available."
55                                         pass
56                         elif retval is False:
57                                 if iSoftwareTools.lastDownloadDate is None:
58                                         if iSoftwareTools.NetworkConnectionAvailable:
59                                                 # _("Updatefeed not available.")
60                                                 print "Updatefeed not available."
61                                                 pass
62                                         else:
63                                                 # _("No network connection available.")
64                                                 print "No network connection available."
65                                                 pass
66                                 else:
67                                         print "IPKGUpdateNotification getUpdates"
68                                         # Call update
69                                         iSoftwareTools.lastDownloadDate = time()
70                                         iSoftwareTools.list_updating = True
71                                         iSoftwareTools.getUpdates(self.getUpdateInfosCB)
72                                         #TODO async
73
74         def buildList(self):
75                 updates = ""
76                 for package in iSoftwareTools.available_updatelist:
77                         packagename = package[0]
78                         instversion = ""
79                         if packagename in iSoftwareTools.installed_packetlist:
80                                 instversion = iSoftwareTools.installed_packetlist[packagename]
81                         updversion = ""
82                         for p, v, d in iSoftwareTools.available_packetlist:
83                                 if p == packagename:
84                                         updversion = v
85                                         break
86                         updates += packagename + " :\t" + instversion + " :\t" + updversion + "\n"
87                 return updates
88