/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program 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.
*/
package org.jampa.net.podcast;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.podcasts.Podcast;
import org.jampa.model.podcasts.PodcastItem;
import org.jampa.preferences.PreferenceConstants;
import org.jampa.utils.SystemUtils;
public class PodcastUpdaterJob extends Job {
private List<Podcast> _list;
private int _newItemsCount;
public PodcastUpdaterJob(List<Podcast> list) {
super(Messages.getString("PodcastUpdaterJob.JobTitle"));
_list = list;
_newItemsCount = 0;
}
private boolean doesPodcastItemExist(String fileName) {
boolean result = false;
Iterator<Podcast> iter = _list.iterator();
while ((iter.hasNext()) &&
(!result)) {
Iterator<PodcastItem> iterItem = iter.next().getPodcastList().iterator();
while ((iterItem.hasNext()) &&
(!result)) {
if (iterItem.next().getTemporaryFileName().equals(fileName)) {
result = true;
}
}
}
return result;
}
private void cleanPodcastCache() {
File toDelete;
File dir = new File(SystemUtils.podcastDowloadDirectory);
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
if (!doesPodcastItemExist(SystemUtils.podcastDowloadDirectory + files[i])) {
toDelete = new File(SystemUtils.podcastDowloadDirectory + files[i]);
toDelete.delete();
}
}
}
@Override
protected IStatus run(IProgressMonitor monitor) {
Controller.getInstance().getEventController().firePodcastsUpdateChange(EventConstants.EVT_PODCAST_UPDATE_BEGIN, null, null);
int nbTask = _list.size();
boolean cleanAfterUpdate = Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.PODCAST_CLEAN_DOWNLOAD_CACHE_ON_UPDATE);
if (cleanAfterUpdate) {
nbTask++;
}
monitor.beginTask(Messages.getString("PodcastUpdaterJob.MainTask"), nbTask);
Podcast podcast;
PodcastUpdater downloader;
Iterator<Podcast> iter = _list.iterator();
while (iter.hasNext()) {
podcast = iter.next();
Log.getInstance(PodcastUpdaterJob.class).debug("Refreshing podcast: " + podcast.getName());
monitor.subTask(podcast.getName());
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
downloader = new PodcastUpdater(podcast.getUrl());
if (downloader.download()) {
podcast.setData(downloader.getData());
Log.getInstance(PodcastUpdaterJob.class).debug("Parsing data for podcast: " + podcast.getName());
_newItemsCount += podcast.parseData();
Controller.getInstance().getEventController().fireAudioItemChange(EventConstants.EVT_ITEM_CHANGE_IN_PLAYLIST, null, null);
}
monitor.worked(1);
}
if (cleanAfterUpdate) {
Log.getInstance(PodcastUpdaterJob.class).debug("Cleaning download cache.");
monitor.subTask(Messages.getString("PodcastUpdaterJob.CleanTask"));
cleanPodcastCache();
monitor.worked(1);
}
Log.getInstance(PodcastUpdaterJob.class).debug("Refreshing podcasts completed.");
monitor.done();
Controller.getInstance().getEventController().firePodcastsUpdateChange(EventConstants.EVT_PODCAST_UPDATE_END, null, new Integer(_newItemsCount));
return Status.OK_STATUS;
}
}