PushService: Added Schwerkraft Git stuff
[enigma2-plugins.git] / pushservice / src / Plugins / IPKGUpdateNotification.py
1 #######################################################################
2 #
3 #    Push Service for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=167779
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 # Config
20 from Components.config import ConfigYesNo, ConfigText, ConfigNumber, NoSave
21
22 # Plugin internal
23 from Plugins.Extensions.PushService.__init__ import _
24 from Plugins.Extensions.PushService.PluginBase import PluginBase
25
26 # Plugin specific
27 import os
28 from time import time
29 from Tools.BoundFunction import boundFunction
30 from Plugins.SystemPlugins.SoftwareManager.SoftwareTools import iSoftwareTools
31
32
33 # Constants
34 SUBJECT = _("IPKG Update Notification")
35 BODY    = _("There are updates available:\n%s")
36
37
38 class IPKGUpdateNotification(PluginBase):
39         
40         ForceSingleInstance = True
41         
42         def __init__(self):
43                 # Is called on instance creation
44                 PluginBase.__init__(self)
45                 
46                 # Default configuration
47                 self.setOption( 'selfcheck', NoSave(ConfigYesNo( default = False )), _("Start update check if not done yet") )
48
49         def run(self, callback, errback):
50                 # At the end a plugin has to call one of the functions: callback or errback
51                 # Callback should return with at least one of the parameter: Header, Body, Attachment
52                 # If empty or none is returned, nothing will be sent
53                 if iSoftwareTools.lastDownloadDate is not None and iSoftwareTools.lastDownloadDate > ( time() - (24*60*60) ):
54                         # Last refresh was within one day
55                         return self.buildList(callback, errback)
56                 else:
57                         print "IPKGUpdateNotification run else"
58                         if self.getValue('selfcheck'):
59                                 # Refresh package list
60                                 iSoftwareTools.startSoftwareTools( boundFunction(self.getUpdateInfosCB, callback, errback) )
61                                 return
62                 callback()
63
64         def getUpdateInfosCB(self, callback, errback, retval = None):
65                 if retval is not None:
66                         if retval is True:
67                                 if iSoftwareTools.available_updates is not 0:
68                                         # _("There are at least ") + str(iSoftwareTools.available_updates) + _(" updates available.")
69                                         print "Updates available."
70                                         return self.buildList(callback, errback)
71                                 else:
72                                         # _("There are no updates available.")
73                                         print "There are no updates available."
74                                         return callback()
75                         elif retval is False:
76                                 if iSoftwareTools.lastDownloadDate is None:
77                                         if iSoftwareTools.NetworkConnectionAvailable:
78                                                 # _("Updatefeed not available.")
79                                                 print "Updatefeed not available."
80                                                 return errback(_("Updatefeed not available."))
81                                         else:
82                                                 # _("No network connection available.")
83                                                 print "No network connection available."
84                                                 return errback(_("No network connection available."))
85                                 else:
86                                         print "IPKGUpdateNotification getUpdates"
87                                         # Call update
88                                         iSoftwareTools.lastDownloadDate = time()
89                                         iSoftwareTools.list_updating = True
90                                         iSoftwareTools.getUpdates( boundFunction(self.getUpdateInfosCB, callback, errback) )
91                                         return
92                 callback()
93
94         def buildList(self, callback, errback):
95                 updates = ""
96                 for package in iSoftwareTools.available_updatelist:
97                         packagename = package[0]
98                         instversion = ""
99                         if packagename in iSoftwareTools.installed_packetlist:
100                                 instversion = iSoftwareTools.installed_packetlist[packagename]
101                         updversion = ""
102                         for p, v, d in iSoftwareTools.available_packetlist:
103                                 if p == packagename:
104                                         updversion = v
105                                         break
106                         updates += packagename + " :\t" + instversion + " :\t" + updversion + "\n"
107                 if updates:
108                         callback( SUBJECT, BODY % (updates) )
109                 else:
110                         callback()
111