FileDocCategorySizeDatePackage
TorrentOpener.javaAPI DocAzureus 3.0.3.410388Wed Jun 13 14:31:36 BST 2007org.gudy.azureus2.ui.swt.mainwindow

TorrentOpener

public class TorrentOpener extends Object
author
Olivier Chalouhi
author
TuxPaper (openTorrentWindow)

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringgetFilterPathData()

    String before = COConfigurationManager.getStringParameter("previous.filter.dir.data");
    if( before != null && before.length() > 0 ) {
      return before;
    }
    String def;
		try {
			def = COConfigurationManager.getDirectoryParameter("Default save path");
	    return def;
		} catch (IOException e) {
			return "";
		}
  
public static java.lang.StringgetFilterPathTorrent()

    String before = COConfigurationManager.getStringParameter("previous.filter.dir.torrent");
    if( before != null && before.length() > 0 ) {
      return before;
    }
    return COConfigurationManager.getStringParameter("General_sDefaultTorrent_Directory");
  
public static voidopenDroppedTorrents(AzureusCore azureus_core, org.eclipse.swt.dnd.DropTargetEvent event, boolean bAllowShareAdd)

		if (event.data == null)
			return;

		boolean bOverrideToStopped = event.detail == DND.DROP_COPY;

		if (event.data instanceof String[] || event.data instanceof String) {
			final String[] sourceNames = (event.data instanceof String[])
					? (String[]) event.data : new String[] { (String) event.data };
			if (sourceNames == null)
				event.detail = DND.DROP_NONE;
			if (event.detail == DND.DROP_NONE)
				return;

			for (int i = 0; (i < sourceNames.length); i++) {
				final File source = new File(sourceNames[i]);
				String sURL = UrlUtils.parseTextForURL(sourceNames[i], true);

				if (sURL != null && !source.exists()) {
					openTorrentWindow(null, new String[] { sURL }, bOverrideToStopped);
				} else if (source.isFile()) {
					String filename = source.getAbsolutePath();
					try {
						if (!TorrentUtils.isTorrentFile(filename) && bAllowShareAdd) {
							Logger.log(new LogEvent(LogIDs.GUI,
											"openDroppedTorrents: file not a torrent file, sharing"));
							ShareUtils.shareFile(azureus_core, filename);
						} else {
							openTorrentWindow(null, new String[] { filename },
									bOverrideToStopped);
						}
					} catch (Exception e) {
						Logger.log(new LogAlert(LogAlert.REPEATABLE,
								"Torrent open fails for '" + filename + "'", e));
					}
				} else if (source.isDirectory()) {
					
					String dir_name = source.getAbsolutePath();

					if (!bAllowShareAdd) {
						openTorrentWindow(dir_name, null, bOverrideToStopped);
					} else {
						String drop_action = COConfigurationManager.getStringParameter(
								"config.style.dropdiraction" );
	
						if (drop_action.equals("1")) {
							ShareUtils.shareDir(azureus_core, dir_name);
						} else if (drop_action.equals("2")) {
							ShareUtils.shareDirContents(azureus_core, dir_name, false);
						} else if (drop_action.equals("3")) {
							ShareUtils.shareDirContents(azureus_core, dir_name, true);
						} else {
							openTorrentWindow(dir_name, null, bOverrideToStopped);
						}
					}
				}
			}
		} else if (event.data instanceof URLTransfer.URLType) {
			openTorrentWindow(null,
					new String[] { ((URLTransfer.URLType) event.data).linkURL },
					bOverrideToStopped);
		}
	
public static voidopenTorrent(java.lang.String torrentFile)
Open a torrent. Possibly display a window if the user config says so

param
torrentFile Torrent to open (file, url, etc)

		openTorrentWindow(null, new String[] { torrentFile }, false);
	
public static voidopenTorrentSimple()

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				final Shell shell = Utils.findAnyShell();
				if (shell == null)
					return;

				FileDialog fDialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
				fDialog.setFilterPath(getFilterPathTorrent());
				fDialog.setFilterExtensions(new String[] {
						"*.torrent",
						"*.tor",
						Constants.FILE_WILDCARD });
				fDialog.setFilterNames(new String[] {
						"*.torrent",
						"*.tor",
						Constants.FILE_WILDCARD });
				fDialog.setText(MessageText.getString("MainWindow.dialog.choose.file"));
				String path = setFilterPathTorrent(fDialog.open());
				if (path == null)
					return;

				openTorrentWindow(path, fDialog.getFileNames(), false);
			}
		});
	
public static voidopenTorrentTrackingOnly()

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				final Shell shell = Utils.findAnyShell();
		  	if (shell == null)
		  		return;

				FileDialog fDialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
				fDialog.setFilterPath(getFilterPathTorrent());
				fDialog
						.setFilterExtensions(new String[] { "*.torrent", "*.tor", Constants.FILE_WILDCARD });
				fDialog.setFilterNames(new String[] { "*.torrent", "*.tor", Constants.FILE_WILDCARD });
				fDialog.setText(MessageText.getString("MainWindow.dialog.choose.file"));
				String path = setFilterPathTorrent(fDialog.open());
				if (path == null)
					return;

				TorrentOpener.openTorrentsForTracking(path, fDialog.getFileNames());
			}
		});
  
private static voidopenTorrentWindow(java.lang.String path, java.lang.String[] torrents, boolean bOverrideStartModeToStopped)

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				Shell shell = Utils.findAnyShell();
				AzureusCore core = AzureusCoreFactory.getSingleton();
				GlobalManager gm = null;
				try {
					gm = core.getGlobalManager();
				} catch (AzureusCoreException e) {
				}

				if (gm == null) {
					core.addLifecycleListener(new AzureusCoreLifecycleAdapter() {
						public void componentCreated(AzureusCore core, AzureusCoreComponent component) {
							if (component instanceof UIFunctionsSWT) {
								openTorrentWindow(path, torrents, bOverrideStartModeToStopped);
							}
						}
					});
					return;
				}

				if (shell == null) {
					Debug.out("openTorrentWindow().. no shell");
					return;
				}

				OpenTorrentWindow.invoke(shell, gm, path, torrents,
						bOverrideStartModeToStopped, false, false);
			}
		});
	
public static voidopenTorrentWindow()
Open the torrent window

  	openTorrentWindow(null, null, false);
  
public static voidopenTorrents(java.lang.String[] torrentFiles)

		openTorrentWindow(null, torrentFiles, false);
	
protected static voidopenTorrentsForTracking(java.lang.String path, java.lang.String[] fileNames)

  	Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				final Display display = SWTThread.getInstance().getDisplay();
		  	final AzureusCore azureus_core = AzureusCoreFactory.getSingleton();
		  	if (display == null || display.isDisposed() || azureus_core == null)
		  		return;
		  	
				new AEThread("TorrentOpener") {
					public void runSupport() {

						for (int i = 0; i < fileNames.length; i++) {

							try {
								TOTorrent t = TorrentUtils.readFromFile(new File(path,
										fileNames[i]), true);

								azureus_core.getTrackerHost().hostTorrent(t, true, true);

							} catch (Throwable e) {
								Logger.log(new LogAlert(LogAlert.UNREPEATABLE,
										"Torrent open fails for '" + path + File.separator
												+ fileNames[i] + "'", e));
							}
						}
					}
				}.start();
			}
		});
  
public static java.lang.StringsetFilterPathData(java.lang.String path)

    if( path != null && path.length() > 0 ) {
      File test = new File( path );
      if( !test.isDirectory() ) test = test.getParentFile();
      String now = "";
      if( test != null ) now = test.getAbsolutePath();
      String before = COConfigurationManager.getStringParameter("previous.filter.dir.data");
      if( before == null || before.length() == 0 || !before.equals( now ) ) {
        COConfigurationManager.setParameter( "previous.filter.dir.data", now );
        COConfigurationManager.save();
      }
    }
    return path;
  
public static java.lang.StringsetFilterPathTorrent(java.lang.String path)

    if( path != null && path.length() > 0 ) {
      File test = new File( path );
      if( !test.isDirectory() ) test = test.getParentFile();
      String now = "";
      if( test != null ) now = test.getAbsolutePath();
      String before = COConfigurationManager.getStringParameter("previous.filter.dir.torrent");
      if( before == null || before.length() == 0 || !before.equals( now ) ) {
        COConfigurationManager.setParameter( "previous.filter.dir.torrent", now );
        COConfigurationManager.save();
      }
      return now;
    }
    return path;