Allow custom handling for background job completion
authorMike Looijmans <milo-software@users.sourceforge.net>
Sat, 8 Dec 2012 11:33:41 +0000 (12:33 +0100)
committerMike Looijmans <milo-software@users.sourceforge.net>
Sat, 8 Dec 2012 11:33:41 +0000 (12:33 +0100)
By default, you get the old behavior of doing nothing on success and
a message box on error.

Both success and fail paths can be assigned different handlers, so
that you can pop up a message when complete or do whatever you see
fit.

lib/python/Components/Task.py
lib/python/Screens/HarddiskSetup.py

index 0360d31..a09acce 100644 (file)
@@ -17,10 +17,9 @@ class Job(object):
                self.__progress = 0
                self.weightScale = 1
                self.afterEvent = None
-
                self.state_changed = CList()
-
                self.status = self.NOT_STARTED
+               self.onSuccess = None
 
        # description is a dict
        def fromDescription(self, description):
@@ -109,8 +108,8 @@ class Job(object):
 
        def cancel(self):
                self.abort()
-               
-       def __str__(self):      
+
+       def __str__(self):
                return "Components.Task.Job name=%s #tasks=%s" % (self.name, len(self.tasks))
 
 class Task(object):
@@ -345,7 +344,14 @@ class JobManager:
                self.visible = False
                self.active_job = None
 
-       def AddJob(self, job):
+       # Set onSuccess to popupTaskView to get a visible notification.
+       # onFail defaults to notifyFailed which tells the user that it went south.
+       def AddJob(self, job, onSuccess=None, onFail=None):
+               job.onSuccess = onSuccess
+               if onFail is None:
+                       job.onFail = self.notifyFailed
+               else:
+                       job.onFail = onFail
                self.active_jobs.append(job)
                self.kick()
 
@@ -355,24 +361,34 @@ class JobManager:
                                self.active_job = self.active_jobs.pop(0)
                                self.active_job.start(self.jobDone)
 
+       def notifyFailed(self, job, task, problems):
+               from Tools import Notifications
+               from Screens.MessageBox import MessageBox
+               if problems[0].RECOVERABLE:
+                       Notifications.AddNotificationWithCallback(self.errorCB, MessageBox, _("Error: %s\nRetry?") % (problems[0].getErrorMessage(task)))
+                       return True
+               else:
+                       Notifications.AddNotification(MessageBox, job.name + "\n" + _("Error") + (': %s') % (problems[0].getErrorMessage(task)), type = MessageBox.TYPE_ERROR )
+                       return False
+
        def jobDone(self, job, task, problems):
                print "job", job, "completed with", problems, "in", task
-               from Tools import Notifications
+               if problems:
+                       if not job.onFail(job, task, problems):
+                               self.errorCB(False)
+               else:
+                       self.active_job = None
+                       if job.onSuccess:
+                               job.onSuccess(job)
+                       self.kick()
+
+       # Set job.onSuccess to this function if you want to pop up the jobview when the job is done/
+       def popupTaskView(self, job):
                if not self.visible:
+                       from Tools import Notifications
                        from Screens.TaskView import JobView
                        self.visible = True
-                       Notifications.AddNotification(JobView, self.active_job)
-               if problems:
-                       from Screens.MessageBox import MessageBox
-                       if problems[0].RECOVERABLE:
-                               Notifications.AddNotificationWithCallback(self.errorCB, MessageBox, _("Error: %s\nRetry?") % (problems[0].getErrorMessage(task)))
-                       else:
-                               Notifications.AddNotification(MessageBox, job.name + "\n" + _("Error") + (': %s') % (problems[0].getErrorMessage(task)), type = MessageBox.TYPE_ERROR )
-                               self.errorCB(False)
-                       return
-                       #self.failed_jobs.append(self.active_job)
-               self.active_job = None
-               self.kick()
+                       Notifications.AddNotification(JobView, job)
 
        def errorCB(self, answer):
                if answer:
index 9697564..d557596 100644 (file)
@@ -33,9 +33,9 @@ class HarddiskSetup(Screen):
        def hddConfirmed(self, confirmed):
                if not confirmed:
                        return
-               import Components.Task
+               from Components.Task import job_manager
                try:
-                       Components.Task.job_manager.AddJob(self.action())
+                       job_manager.AddJob(self.action(), onSuccess=job_manager.popupTaskView)
                except Exception, ex:
                        self.session.open(MessageBox, str(ex), type=MessageBox.TYPE_ERROR, timeout=10)
                self.close()