/*
* Created on Feb 24, 2005
* Created by omschaub
*
*/
package omschaub.azcvsupdater.utilities;
import java.io.File;
import omschaub.azcvsupdater.main.StatusBoxUtils;
import omschaub.azcvsupdater.main.Tab1_Subtab_1;
import omschaub.azcvsupdater.main.Tab4;
import omschaub.azcvsupdater.main.View;
import omschaub.azcvsupdater.utilities.download.MainCVSGet;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloader;
import org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloaderFactory;
import org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloaderListener;
public class DownloaderMain {
static ResourceDownloaderFactory rdf;
static ResourceDownloaderListener rdl;
static ResourceDownloader rd_t;
public static void sameFilePresent() {
StatusBoxUtils.mainStatusAdd(" Now Downloading "
+ Tab1_Subtab_1.version,0);
File f = new File(DirectoryUtils.getBackupDirectory());
File[] files = f.listFiles();
for(int i = 0 ; i < files.length ; i++) {
String fileNameCheck = files[i].getName();
if(fileNameCheck.equals(Tab1_Subtab_1.version)){
Shell shell = new Shell();
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.NO | SWT.YES);
messageBox.setText("Duplicate File Name");
messageBox.setMessage("A file named " + Tab1_Subtab_1.version + " already exists, Redownload?");
int response = messageBox.open();
switch (response){
case SWT.YES:
break;
case SWT.NO:
//loadDirectory(type);
StatusBoxUtils.mainStatusAdd(" Download Cancelled",0);
shell.dispose();
return;
}
shell.dispose();
}
}
try {
ButtonStatus.set(false,false,false,false,false);
if(View.getDisplay() == null && View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run () {
if(Tab4.refreshBackup != null && !Tab4.refreshBackup.isDisposed())
Tab4.refreshBackup.setEnabled(false);
}
});
//DownloaderMain.resource_getter(URLReader.get_cvsurl(),pm);
call_MainCVSGet();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void autoDownloader(final PluginInterface pm){
Thread autoDownloaderThread = new Thread() {
public void run() {
try {
Thread.sleep(5000);
if(URLReader.get_whenLastModified() == 0){
Thread.sleep(5000);
}
if (URLReader.get_whenLastModified() == 0){
return;
}
//oldestFile = 1;
File f = new File(DirectoryUtils.getBackupDirectory());
if(!f.exists() || !f.isDirectory())
return;
File[] files = f.listFiles();
for(int i = 0 ; i < files.length ; i++)
{
//String fileName = files[i].getName();
if(files[i].getName().equalsIgnoreCase(Tab1_Subtab_1.version))
{
//output message that download is not needed
StatusBoxUtils.mainStatusAdd(" Auto Download not needed",1);
// auto seed latest code
//System.out.println("Auto_seed: " + View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("auto_seed",false) + " Sharing Latest: " + TorrentUtils.sharingLatest());
if(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("auto_seed",false) && !TorrentUtils.sharingLatest()){
TorrentUtils.seedLatest();
// TorrentUtils.setForceSeed(Tab1.version);
}
return;
}
}
//resource_getter(URLReader.get_cvsurl(),pm);
StatusBoxUtils.mainStatusAdd(" Auto Downloading " + Tab1_Subtab_1.version + " via torrent",1);
call_MainCVSGet();
} catch(Exception e) {
//Stop process and trace the exception
//e.printStackTrace();
System.out.println("AZCVSUpdater Error: Error in autodupdate code");
}
}
};
if (pm.getPluginconfig().getPluginBooleanParameter("AutoDownload"))
{
autoDownloaderThread.setDaemon(true);
autoDownloaderThread.start();
}else if(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("auto_seed",false) && !TorrentUtils.sharingLatest()){
TorrentUtils.seedLatest();
//TorrentUtils.setForceSeed(Tab1.version);
}
}
/**
* setup for main resource get
*/
public static void call_MainCVSGet(){
if (Tab1_Subtab_1.version.equals("Checking...."))
return;
MainCVSGet mainCVSGet = new MainCVSGet();
mainCVSGet.setURL(URLReader.get_cvsurl());
mainCVSGet.setDir(DirectoryUtils.getBackupDirectory() + System.getProperty("file.separator"));
mainCVSGet.initialize("torrent");
mainCVSGet.start();
}
}//EOF