2012-07-02 05:53:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# gPodder - A media aggregator and podcast client
|
2012-07-09 21:08:40 +02:00
|
|
|
# Copyright (c) 2005-2012 Thomas Perl and the gPodder Team
|
2012-07-02 05:53:33 +02:00
|
|
|
#
|
|
|
|
# gPodder is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gPodder is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
# gpodder.gtkui.desktop.sync - Glue code between GTK+ UI and sync module
|
|
|
|
# Thomas Perl <thp@gpodder.org>; 2009-09-05 (based on code from gui.py)
|
2012-07-09 21:08:40 +02:00
|
|
|
# Ported to gPodder 3 by Joseph Wickremasinghe in June 2012
|
2012-07-02 05:53:33 +02:00
|
|
|
|
|
|
|
import gtk
|
|
|
|
import gpodder
|
|
|
|
|
|
|
|
_ = gpodder.gettext
|
|
|
|
|
|
|
|
from gpodder import util
|
|
|
|
from gpodder import sync
|
2012-07-09 21:08:40 +02:00
|
|
|
|
2012-07-02 05:53:33 +02:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class gPodderSyncUI(object):
|
2012-07-09 21:08:40 +02:00
|
|
|
def __init__(self, config, notification, parent_window,
|
|
|
|
show_confirmation,
|
|
|
|
update_episode_list_icons,
|
|
|
|
update_podcast_list_model,
|
|
|
|
preferences_widget,
|
|
|
|
episode_selector_class,
|
|
|
|
download_status_model,
|
|
|
|
download_queue_manager,
|
|
|
|
enable_download_list_update,
|
2012-07-02 05:53:33 +02:00
|
|
|
commit_changes_to_database):
|
2012-07-09 21:08:40 +02:00
|
|
|
self.device = None
|
|
|
|
|
2012-07-02 05:53:33 +02:00
|
|
|
self._config = config
|
|
|
|
self.notification = notification
|
|
|
|
self.parent_window = parent_window
|
|
|
|
self.show_confirmation = show_confirmation
|
2012-07-09 21:08:40 +02:00
|
|
|
|
2012-07-02 05:53:33 +02:00
|
|
|
self.update_episode_list_icons = update_episode_list_icons
|
|
|
|
self.update_podcast_list_model = update_podcast_list_model
|
|
|
|
self.preferences_widget = preferences_widget
|
|
|
|
self.episode_selector_class = episode_selector_class
|
2012-07-09 21:08:40 +02:00
|
|
|
self.download_status_model = download_status_model
|
|
|
|
self.download_queue_manager = download_queue_manager
|
|
|
|
self.enable_download_list_update = enable_download_list_update
|
2012-07-02 05:53:33 +02:00
|
|
|
self.commit_changes_to_database = commit_changes_to_database
|
2012-07-09 21:08:40 +02:00
|
|
|
|
2012-07-02 05:53:33 +02:00
|
|
|
|
|
|
|
def _filter_sync_episodes(self, channels, only_downloaded=False):
|
|
|
|
"""Return a list of episodes for device synchronization
|
|
|
|
|
|
|
|
If only_downloaded is True, this will skip episodes that
|
|
|
|
have not been downloaded yet and podcasts that are marked
|
|
|
|
as "Do not synchronize to my device".
|
|
|
|
"""
|
|
|
|
episodes = []
|
|
|
|
for channel in channels:
|
2012-08-30 04:35:43 +02:00
|
|
|
if only_downloaded or not channel.sync_to_mp3_player:
|
2012-07-02 05:53:33 +02:00
|
|
|
logger.info('Skipping channel: %s', channel.title)
|
|
|
|
continue
|
|
|
|
|
|
|
|
for episode in channel.get_all_episodes():
|
2012-07-09 21:08:40 +02:00
|
|
|
if (episode.was_downloaded(and_exists=True) or
|
2012-07-02 05:53:33 +02:00
|
|
|
not only_downloaded):
|
|
|
|
episodes.append(episode)
|
|
|
|
return episodes
|
|
|
|
|
|
|
|
def _show_message_unconfigured(self):
|
|
|
|
title = _('No device configured')
|
|
|
|
message = _('Please set up your device in the preferences dialog.')
|
|
|
|
self.notification(message, title, widget=self.preferences_widget)
|
|
|
|
|
|
|
|
def _show_message_cannot_open(self):
|
|
|
|
title = _('Cannot open device')
|
|
|
|
message = _('Please check the settings in the preferences dialog.')
|
|
|
|
self.notification(message, title, widget=self.preferences_widget)
|
|
|
|
|
|
|
|
def on_synchronize_episodes(self, channels, episodes=None, force_played=True):
|
|
|
|
device = sync.open_device(self)
|
|
|
|
|
|
|
|
if device is None:
|
|
|
|
return self._show_message_unconfigured()
|
|
|
|
|
|
|
|
if not device.open():
|
|
|
|
return self._show_message_cannot_open()
|
|
|
|
else:
|
2012-07-09 21:08:40 +02:00
|
|
|
# Only set if device is configured and opened successfully
|
|
|
|
self.device = device
|
2012-07-02 05:53:33 +02:00
|
|
|
|
|
|
|
if episodes is None:
|
|
|
|
force_played = False
|
|
|
|
episodes = self._filter_sync_episodes(channels)
|
2012-07-09 21:08:40 +02:00
|
|
|
|
2012-07-02 05:53:33 +02:00
|
|
|
def check_free_space():
|
|
|
|
# "Will we add this episode to the device?"
|
|
|
|
def will_add(episode):
|
|
|
|
# If already on-device, it won't take up any space
|
|
|
|
if device.episode_on_device(episode):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Might not be synced if it's played already
|
2012-07-09 21:08:40 +02:00
|
|
|
if (not force_played and
|
2012-07-02 05:53:33 +02:00
|
|
|
self._config.device_sync.skip_played_episodes):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# In all other cases, we expect the episode to be
|
|
|
|
# synchronized to the device, so "answer" positive
|
|
|
|
return True
|
|
|
|
|
|
|
|
# "What is the file size of this episode?"
|
|
|
|
def file_size(episode):
|
|
|
|
filename = episode.local_filename(create=False)
|
|
|
|
if filename is None:
|
|
|
|
return 0
|
|
|
|
return util.calculate_size(str(filename))
|
|
|
|
|
|
|
|
# Calculate total size of sync and free space on device
|
|
|
|
total_size = sum(file_size(e) for e in episodes if will_add(e))
|
|
|
|
free_space = max(device.get_free_space(), 0)
|
|
|
|
|
|
|
|
if total_size > free_space:
|
|
|
|
title = _('Not enough space left on device')
|
2012-07-07 23:41:56 +02:00
|
|
|
message = (_('Additional free space required: %(required_space)s\nDo you want to continue?') %
|
|
|
|
{'required_space': util.format_filesize(total_size - free_space)})
|
2012-07-02 05:53:33 +02:00
|
|
|
if not self.show_confirmation(message, title):
|
|
|
|
device.cancel()
|
|
|
|
device.close()
|
|
|
|
return
|
|
|
|
|
2012-07-09 21:08:40 +02:00
|
|
|
# Finally start the synchronization process
|
2012-07-10 13:52:34 +02:00
|
|
|
@util.run_in_background
|
2012-07-02 05:53:33 +02:00
|
|
|
def sync_thread_func():
|
2012-07-09 21:08:40 +02:00
|
|
|
self.enable_download_list_update()
|
2012-07-02 05:53:33 +02:00
|
|
|
device.add_sync_tasks(episodes, force_played=force_played)
|
|
|
|
|
|
|
|
# This function is used to remove files from the device
|
|
|
|
def cleanup_episodes():
|
|
|
|
# 'skip_played_episodes' must be used or else all the
|
|
|
|
# played tracks will be copied then immediately deleted
|
2012-07-09 21:08:40 +02:00
|
|
|
if (self._config.device_sync.delete_played_episodes and
|
2012-07-02 05:53:33 +02:00
|
|
|
self._config.device_sync.skip_played_episodes):
|
2012-07-09 21:08:40 +02:00
|
|
|
all_episodes = self._filter_sync_episodes(channels,
|
2012-07-02 05:53:33 +02:00
|
|
|
only_downloaded=False)
|
|
|
|
episodes_on_device = device.get_all_tracks()
|
|
|
|
for local_episode in all_episodes:
|
|
|
|
episode = device.episode_on_device(local_episode)
|
|
|
|
if episode is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if local_episode.state == gpodder.STATE_DELETED:
|
|
|
|
logger.info('Removing episode from device: %s',
|
|
|
|
episode.title)
|
|
|
|
device.remove_track(episode)
|
|
|
|
|
|
|
|
# When this is done, start the callback in the UI code
|
|
|
|
util.idle_add(check_free_space)
|
|
|
|
|
|
|
|
# This will run the following chain of actions:
|
|
|
|
# 1. Remove old episodes (in worker thread)
|
|
|
|
# 2. Check for free space (in UI thread)
|
|
|
|
# 3. Sync the device (in UI thread)
|
2012-07-10 13:52:34 +02:00
|
|
|
util.run_in_background(cleanup_episodes)
|
2012-07-09 21:08:40 +02:00
|
|
|
|