comparison frontends/src/jp/base.py @ 1627:5a641e7b858a

jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
author Goffi <goffi@goffi.org>
date Thu, 19 Nov 2015 18:13:26 +0100
parents ec48b35309dc
children 44a14f83e64b
comparison
equal deleted inserted replaced
1626:63cef4dbf2a4 1627:5a641e7b858a
103 self.need_loop = False # to set by commands when loop is needed 103 self.need_loop = False # to set by commands when loop is needed
104 104
105 # progress attributes 105 # progress attributes
106 self._progress_id = None # TODO: manage several progress ids 106 self._progress_id = None # TODO: manage several progress ids
107 self.quit_on_progress_end = True 107 self.quit_on_progress_end = True
108 self.progress_started = lambda dummy: self.disp(_(u"Operation started"), 2)
109 self.progress_success = lambda dummy: self.disp(_(u"Operation successfully finished"), 2)
110 self.progress_failure = lambda dummy: self.disp(_(u"Error while doing operation"), error=True)
111 108
112 @property 109 @property
113 def version(self): 110 def version(self):
114 return self.bridge.getVersion() 111 return self.bridge.getVersion()
115 112
377 main_resource = self.bridge.getMainResource(param_jid, self.profile) 374 main_resource = self.bridge.getMainResource(param_jid, self.profile)
378 if main_resource: 375 if main_resource:
379 return "%s/%s" % (_jid.bare, main_resource) 376 return "%s/%s" % (_jid.bare, main_resource)
380 return param_jid 377 return param_jid
381 378
382 def _onProgressStarted(self, uid, profile): 379 def progressUpdate(self):
383 if profile != self.profile:
384 return
385 self.progress_started(None)
386 if self.watch_progress and self.progress_id and uid == self.progress_id:
387 GLib.timeout_add(PROGRESS_DELAY, self._progress_cb)
388
389 def _onProgressFinished(self, uid, profile):
390 if profile != self.profile:
391 return
392 if uid == self.progress_id:
393 try:
394 self.pbar.finish()
395 except AttributeError:
396 pass
397 self.progress_success(None)
398 if self.quit_on_progress_end:
399 self.quit()
400
401 def _onProgressError(self, uid, profile):
402 if profile != self.profile:
403 return
404 if uid == self.progress_id:
405 self.disp('') # progress is not finished, so we skip a line
406 if self.quit_on_progress_end:
407 self.progress_failure(None)
408 self.quitFromSignal(1)
409
410 def _progress_cb(self):
411 """This method is continualy called to update the progress bar""" 380 """This method is continualy called to update the progress bar"""
412 data = self.bridge.progressGet(self.progress_id, self.profile) 381 data = self.bridge.progressGet(self.progress_id, self.profile)
413 if data: 382 if data:
414 try: 383 try:
415 size = data['size'] 384 size = data['size']
503 472
504 @progress_id.setter 473 @progress_id.setter
505 def progress_id(self, value): 474 def progress_id(self, value):
506 self.host.progress_id = value 475 self.host.progress_id = value
507 476
477 def progressStartedHandler(self, uid, metadata, profile):
478 if profile != self.profile:
479 return
480 self.onProgressStarted(metadata)
481 if self.host.watch_progress and self.progress_id and uid == self.progress_id:
482 GLib.timeout_add(PROGRESS_DELAY, self.host.progressUpdate)
483
484 def progressFinishedHandler(self, uid, metadata, profile):
485 if profile != self.profile:
486 return
487 if uid == self.progress_id:
488 try:
489 self.host.pbar.finish()
490 except AttributeError:
491 pass
492 self.onProgressFinished(metadata)
493 if self.host.quit_on_progress_end:
494 self.host.quit()
495
496 def progressErrorHandler(self, uid, message, profile):
497 if profile != self.profile:
498 return
499 if uid == self.progress_id:
500 self.disp('') # progress is not finished, so we skip a line
501 if self.host.quit_on_progress_end:
502 self.onProgressError(message)
503 self.host.quitFromSignal(1)
504
505 def onProgressStarted(self, metadata):
506 self.disp(_(u"Operation started"), 2)
507
508 def onProgressFinished(self, metadata):
509 self.disp(_(u"Operation successfully finished"), 2)
510
511 def onProgressError(self, error_msg):
512 self.disp(_(u"Error while doing operation: {}").format(error_msg), error=True)
513
508 def disp(self, msg, verbosity=0, error=False): 514 def disp(self, msg, verbosity=0, error=False):
509 return self.host.disp(msg, verbosity, error) 515 return self.host.disp(msg, verbosity, error)
510 516
511 def add_parser_options(self): 517 def add_parser_options(self):
512 try: 518 try:
536 pass 542 pass
537 else: 543 else:
538 if show_progress: 544 if show_progress:
539 self.host.watch_progress = True 545 self.host.watch_progress = True
540 # we need to register the following signal even if we don't display the progress bas 546 # we need to register the following signal even if we don't display the progress bas
541 self.host.bridge.register("progressStarted", self.host._onProgressStarted) 547 self.host.bridge.register("progressStarted", self.progressStartedHandler)
542 self.host.bridge.register("progressFinished", self.host._onProgressFinished) 548 self.host.bridge.register("progressFinished", self.progressFinishedHandler)
543 self.host.bridge.register("progressError", self.host._onProgressError) 549 self.host.bridge.register("progressError", self.progressErrorHandler)
544 550
545 def connected(self): 551 def connected(self):
546 if not self.need_loop: 552 if not self.need_loop:
547 self.host.stop_loop() 553 self.host.stop_loop()
548 554