Skip to content

Commit

Permalink
Proper single-time download notifications (bug 1161)
Browse files Browse the repository at this point in the history
Only show a notification for finished or failed downloads
once, both on the Desktop version and on Maemo 5 by adding
a special function to DownloadTask to keep track whether or
not the notification has already been shown previously.
  • Loading branch information
thp committed Dec 18, 2010
1 parent 8f167f8 commit 10cad7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
35 changes: 34 additions & 1 deletion src/gpodder/download.py
Expand Up @@ -516,6 +516,14 @@ class DownloadTask(object):
it from the UI, so that it can carry out any pending clean-up
actions (e.g. removing the temporary file when the task has not
finished successfully; i.e. task.status != DownloadTask.DONE).
The UI can call the method "notify_as_finished()" to determine if
this episode still has still to be shown as "finished" download
in a notification window. This will return True only the first time
it is called when the status is DONE. After returning True once,
it will always return False afterwards.
The same thing works for failed downloads ("notify_as_failed()").
"""
# Possible states this download task can be in
STATUS_MESSAGE = (_('Added'), _('Queued'), _('Downloading'),
Expand Down Expand Up @@ -582,6 +590,9 @@ def __init__(self, episode, config):
self.progress = 0.0
self.error_message = None

# Have we already shown this task in a notification?
self._notification_shown = False

# Variables for speed limit and speed calculation
self.__start_time = 0
self.__start_blocks = 0
Expand All @@ -604,6 +615,26 @@ def __init__(self, episode, config):
# files for resuming when the file is queued
open(self.tempname, 'w').close()

def notify_as_finished(self):
if self.status == DownloadTask.DONE:
if self._notification_shown:
return False
else:
self._notification_shown = True
return True

return False

def notify_as_failed(self):
if self.status == DownloadTask.FAILED:
if self._notification_shown:
return False
else:
self._notification_shown = True
return True

return False

def add_progress_callback(self, callback):
self._progress_updated = callback

Expand Down Expand Up @@ -683,6 +714,7 @@ def run(self):

# We are downloading this file right now
self.status = DownloadTask.DOWNLOADING
self._notification_shown = False

try:
# Resolve URL and start downloading the episode
Expand Down Expand Up @@ -761,7 +793,8 @@ def run(self):
self.error_message = _('HTTP Error %(code)s: %(message)s') % d
except Exception, e:
self.status = DownloadTask.FAILED
self.error_message = _('Error: %s') % (e.message,)
log('Download error: %s', str(e), traceback=True, sender=self)
self.error_message = _('Error: %s') % (str(e),)

if self.status == DownloadTask.DOWNLOADING:
# Everything went well - we're done
Expand Down
14 changes: 6 additions & 8 deletions src/gpodder/gui.py
Expand Up @@ -1321,7 +1321,6 @@ def update_downloads_list(self, can_call_cleanup=True):
if model is None:
model = ()

failed_downloads = []
for row in model:
self.download_status_model.request_update(row.iter)

Expand All @@ -1345,7 +1344,6 @@ def update_downloads_list(self, can_call_cleanup=True):
downloading += 1
total_speed += speed
elif status == download.DownloadTask.FAILED:
failed_downloads.append(task)
failed += 1
elif status == download.DownloadTask.DONE:
finished += 1
Expand Down Expand Up @@ -1424,10 +1422,11 @@ def update_downloads_list(self, can_call_cleanup=True):
if self.config.cmd_all_downloads_complete:
util.run_external_command(self.config.cmd_all_downloads_complete)

if gpodder.ui.fremantle and failed:
if gpodder.ui.fremantle:
message = '\n'.join(['%s: %s' % (str(task), \
task.error_message) for task in failed_downloads])
self.show_message(message, _('Downloads failed'), important=True)
task.error_message) for task in self.download_tasks_seen if task.notify_as_failed()])
if message:
self.show_message(message, _('Downloads failed'), important=True)

# Remove finished episodes
if self.config.auto_cleanup_downloads and can_call_cleanup:
Expand Down Expand Up @@ -1645,9 +1644,8 @@ def downloads_list_get_selection(self, model=None, paths=None):
return selected_tasks, can_queue, can_cancel, can_pause, can_remove, can_force

def downloads_finished(self, download_tasks_seen):
# FIXME: Filter all tasks that have already been reported
finished_downloads = [str(task) for task in download_tasks_seen if task.status == task.DONE]
failed_downloads = [str(task)+' ('+task.error_message+')' for task in download_tasks_seen if task.status == task.FAILED]
finished_downloads = [str(task) for task in download_tasks_seen if task.notify_as_finished()]
failed_downloads = [str(task)+' ('+task.error_message+')' for task in download_tasks_seen if task.notify_as_failed()]

if finished_downloads and failed_downloads:
message = self.format_episode_list(finished_downloads, 5)
Expand Down

0 comments on commit 10cad7a

Please sign in to comment.