/* (non-Javadoc)
   * @see com.aelitis.azureus.ui.swt.UIFunctionsSWT#openTorrentOpenOptions(org.eclipse.swt.widgets.Shell, java.lang.String, java.lang.String[], boolean, boolean, boolean)
   */
  public void openTorrentOpenOptions(
      Shell shell,
      String sPathOfFilesToOpen,
      String[] sFilesToOpen,
      boolean defaultToStopped,
      boolean forceOpen) {

    TorrentOpenOptions torrentOptions = new TorrentOpenOptions();
    if (defaultToStopped) {
      torrentOptions.iStartID = TorrentOpenOptions.STARTMODE_STOPPED;
    }
    if (sFilesToOpen == null) {
      new OpenTorrentWindow(shell);
    } else {
      // with no listener, Downloader will open options window if user configured
      TorrentOpener.openTorrentsFromStrings(
          torrentOptions, shell, sPathOfFilesToOpen, sFilesToOpen, null, null, forceOpen);
    }
  }
  public boolean addTorrentWithOptions(boolean force, final TorrentOpenOptions torrentOptions) {

    if (AzureusCoreFactory.isCoreRunning()) {
      GlobalManager gm = AzureusCoreFactory.getSingleton().getGlobalManager();
      // Check if torrent already exists in gm, and add if not
      DownloadManager existingDownload = gm.getDownloadManager(torrentOptions.getTorrent());

      if (existingDownload != null) {

        final String fExistingName = existingDownload.getDisplayName();
        final DownloadManager fExistingDownload = existingDownload;

        fExistingDownload.fireGlobalManagerEvent(GlobalManagerEvent.ET_REQUEST_ATTENTION);

        Utils.execSWTThread(
            new AERunnable() {
              public void runSupport() {
                boolean can_merge =
                    TorrentUtils.canMergeAnnounceURLs(
                        torrentOptions.getTorrent(), fExistingDownload.getTorrent());

                Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();

                if ((Display.getDefault().getActiveShell() == null
                        || !mainShell.isVisible()
                        || mainShell.getMinimized())
                    && (!can_merge)) {

                  new MessageSlideShell(
                      Display.getCurrent(),
                      SWT.ICON_INFORMATION,
                      MSG_ALREADY_EXISTS,
                      null,
                      new String[] {
                        ":" + torrentOptions.sOriginatingLocation,
                        fExistingName,
                        MessageText.getString(MSG_ALREADY_EXISTS_NAME),
                      },
                      new Object[] {fExistingDownload},
                      -1);
                } else {

                  if (can_merge) {

                    String text =
                        MessageText.getString(
                            MSG_ALREADY_EXISTS + ".text",
                            new String[] {
                              ":" + torrentOptions.sOriginatingLocation,
                              fExistingName,
                              MessageText.getString(MSG_ALREADY_EXISTS_NAME),
                            });

                    text +=
                        "\n\n" + MessageText.getString("openTorrentWindow.mb.alreadyExists.merge");

                    MessageBoxShell mb =
                        new MessageBoxShell(
                            SWT.YES | SWT.NO,
                            MessageText.getString(MSG_ALREADY_EXISTS + ".title"),
                            text);

                    mb.open(
                        new UserPrompterResultListener() {
                          public void prompterClosed(int result) {
                            if (result == SWT.YES) {

                              TorrentUtils.mergeAnnounceURLs(
                                  torrentOptions.getTorrent(), fExistingDownload.getTorrent());
                            }
                          }
                        });
                  } else {
                    MessageBoxShell mb =
                        new MessageBoxShell(
                            SWT.OK,
                            MSG_ALREADY_EXISTS,
                            new String[] {
                              ":" + torrentOptions.sOriginatingLocation,
                              fExistingName,
                              MessageText.getString(MSG_ALREADY_EXISTS_NAME),
                            });
                    mb.open(null);
                  }
                }
              }
            });

        if (torrentOptions.bDeleteFileOnCancel) {
          File torrentFile = new File(torrentOptions.sFileName);
          torrentFile.delete();
        }
        return true;
      }
    }

    if (!force) {
      String showAgainMode =
          COConfigurationManager.getStringParameter(
              ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS);
      if (showAgainMode != null
          && ((showAgainMode.equals(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS_NEVER))
              || (showAgainMode.equals(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS_MANY)
                  && torrentOptions.getFiles() != null
                  && torrentOptions.getFiles().length == 1))) {

        // we're about to silently add the download - ensure that it is going to be saved somewhere
        // vaguely sensible
        // as the current save location is simply taken from the 'default download' config which can
        // be blank (for example)

        boolean looks_good = false;

        String save_loc = torrentOptions.getParentDir().trim();

        if (save_loc.length() == 0) {

          // blank :(

        } else if (save_loc.startsWith(".")) {

          // relative to who knows where
        } else {

          File f = new File(save_loc);

          if (!f.exists()) {

            f.mkdirs();
          }

          if (f.isDirectory() && FileUtil.canWriteToDirectory(f)) {

            if (!f.equals(AETemporaryFileHandler.getTempDirectory())) {

              looks_good = true;
            }
          }
        }

        if (looks_good) {

          return TorrentOpener.addTorrent(torrentOptions);

        } else {

          torrentOptions.setParentDir("");

          MessageBoxShell mb =
              new MessageBoxShell(
                  SWT.OK | SWT.ICON_ERROR,
                  "OpenTorrentWindow.mb.invaliddefsave",
                  new String[] {save_loc});

          mb.open(
              new UserPrompterResultListener() {
                public void prompterClosed(int result) {
                  OpenTorrentOptionsWindow.addTorrent(torrentOptions);
                }
              });

          return (true);
        }
      }
    }

    OpenTorrentOptionsWindow.addTorrent(torrentOptions);

    return true;
  }