Search in sources :

Example 1 with TorrentManagerImpl

use of com.biglybt.pifimpl.local.torrent.TorrentManagerImpl in project BiglyBT by BiglySoftware.

the class OpenTorrentOptionsWindow method addTorrent.

public static OpenTorrentOptionsWindow addTorrent(final TorrentOpenOptions torrentOptions) {
    TOTorrent torrent = torrentOptions.getTorrent();
    TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
    try {
        final HashWrapper hw = torrent.getHashWrapper();
        synchronized (active_windows) {
            final OpenTorrentOptionsWindow existing = active_windows.get(hw);
            if (existing != null) {
                Utils.execSWTThread(new AERunnable() {

                    @Override
                    public void runSupport() {
                        existing.swt_activate();
                    }
                });
                torrentOptions.cancel();
                return (existing);
            }
            boolean separate_dialogs = COConfigurationManager.getBooleanParameter(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS_SEP);
            if (active_window_checker == null) {
                active_window_checker = SimpleTimer.addPeriodicEvent("awc", 250, new TimerEventPerformer() {

                    @Override
                    public void perform(TimerEvent event) {
                        Utils.execSWTThread(new AERunnable() {

                            @Override
                            public void runSupport() {
                                synchronized (active_windows) {
                                    if (active_windows.size() == 0) {
                                        if (active_window_checker != null) {
                                            active_window_checker.cancel();
                                            active_window_checker = null;
                                        }
                                    } else {
                                        for (OpenTorrentOptionsWindow w : active_windows.values()) {
                                            List<OpenTorrentInstance> instances = w.getInstances();
                                            int num_reject = 0;
                                            int num_accept = 0;
                                            for (OpenTorrentInstance inst : instances) {
                                                TorrentOpenOptions opts = inst.getOptions();
                                                int act = opts.getCompleteAction();
                                                if (act == TorrentOpenOptions.CA_REJECT) {
                                                    w.removeInstance(inst, true);
                                                    num_reject++;
                                                } else if (act == TorrentOpenOptions.CA_ACCEPT) {
                                                    num_accept++;
                                                }
                                                if (opts.getAndClearDirt()) {
                                                    inst.refresh();
                                                }
                                            }
                                            if (num_reject >= instances.size()) {
                                                w.cancelPressed();
                                            } else if (num_accept + num_reject >= instances.size()) {
                                                w.okPressed();
                                            }
                                        }
                                    }
                                }
                            }
                        });
                    }
                });
            }
            if (!separate_dialogs) {
                if (active_windows.size() > 0) {
                    final OpenTorrentOptionsWindow reuse_window = active_windows.values().iterator().next();
                    active_windows.put(hw, reuse_window);
                    t_man.optionsAdded(torrentOptions);
                    Utils.execSWTThread(new AERunnable() {

                        @Override
                        public void runSupport() {
                            reuse_window.swt_addTorrent(hw, torrentOptions);
                        }
                    });
                    return (reuse_window);
                }
            }
            final OpenTorrentOptionsWindow new_window = new OpenTorrentOptionsWindow();
            active_windows.put(hw, new_window);
            t_man.optionsAdded(torrentOptions);
            Utils.execSWTThread(new AERunnable() {

                @Override
                public void runSupport() {
                    new_window.swt_addTorrent(hw, torrentOptions);
                }
            });
            return (new_window);
        }
    } catch (Throwable e) {
        Debug.out(e);
        return (null);
    }
}
Also used : TorrentManagerImpl(com.biglybt.pifimpl.local.torrent.TorrentManagerImpl) TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions) List(java.util.List)

Example 2 with TorrentManagerImpl

use of com.biglybt.pifimpl.local.torrent.TorrentManagerImpl in project BiglyBT by BiglySoftware.

the class OpenTorrentOptionsWindow method swt_addTorrent.

protected void swt_addTorrent(HashWrapper hash, TorrentOpenOptions torrentOptions) {
    final TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
    try {
        if (dlg == null) {
            dlg = new SkinnedDialog("skin3_dlg_opentorrent_options", "shell", SWT.RESIZE | SWT.MAX | SWT.DIALOG_TRIM);
            final SWTSkin skin_outter = dlg.getSkin();
            SWTSkinObject so;
            if (COConfigurationManager.hasParameter(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS, true)) {
                so = skin_outter.getSkinObject("showagain-area");
                if (so != null) {
                    so.setVisible(false);
                }
            }
            SWTSkinObject soButtonArea = skin_outter.getSkinObject("button-area");
            if (soButtonArea instanceof SWTSkinObjectContainer) {
                buttonsArea = new StandardButtonsArea() {

                    @Override
                    protected void clicked(int intValue) {
                        if (intValue == SWT.OK) {
                            okPressed();
                        } else {
                            cancelPressed();
                        }
                    }
                };
                buttonsArea.setButtonIDs(new String[] { MessageText.getString("Button.ok"), MessageText.getString("Button.cancel") });
                buttonsArea.setButtonVals(new Integer[] { SWT.OK, SWT.CANCEL });
                buttonsArea.swt_createButtons(((SWTSkinObjectContainer) soButtonArea).getComposite());
            }
            sash_object = (SWTSkinObjectSash) skin_outter.getSkinObject("multi-sash");
            SWTSkinObjectContainer select_area = (SWTSkinObjectContainer) skin_outter.getSkinObject("torrents-table");
            setupTVTorrents(select_area.getComposite());
            SWTSkinObjectContainer torrents_info = (SWTSkinObjectContainer) skin_outter.getSkinObject("torrents-info");
            Composite info_area = torrents_info.getComposite();
            info_area.setLayout(new GridLayout());
            torrents_info_label = new Label(info_area, SWT.NULL);
            Utils.setLayoutData(torrents_info_label, new GridData(GridData.FILL_HORIZONTAL));
            sash_object.setVisible(false);
            sash_object.setAboveVisible(false);
            so = skin_outter.getSkinObject("expand-area");
            expand_stack_area = ((SWTSkinObjectContainer) so).getComposite();
            expand_stack = new StackLayout();
            expand_stack_area.setLayout(expand_stack);
            Composite expand_area = new Composite(expand_stack_area, SWT.NULL);
            expand_area.setLayout(new FormLayout());
            expand_stack.topControl = expand_area;
            OpenTorrentInstance instance = new OpenTorrentInstance(hash, expand_area, torrentOptions, optionListener);
            addInstance(instance);
            selected_instances.add(instance);
            UIUpdaterSWT.getInstance().addUpdater(this);
            setupShowAgainOptions(skin_outter);
            /*
					 * The bring-to-front logic for torrent addition is controlled by other parts of the code so we don't
					 * want the dlg to override this behaviour (main example here is torrents passed from, say, a browser,
					 * and the user has disabled the 'show vuze on external torrent add' feature)
					 */
            dlg.open("otow", false);
            synchronized (active_windows) {
                int num_active_windows = active_windows.size();
                Shell shell = dlg.getShell();
                if (num_active_windows > 1) {
                    int max_x = 0;
                    int max_y = 0;
                    for (OpenTorrentOptionsWindow window : active_windows.values()) {
                        if (window == this || !window.isInitialised()) {
                            continue;
                        }
                        Rectangle rect = window.getBounds();
                        max_x = Math.max(max_x, rect.x);
                        max_y = Math.max(max_y, rect.y);
                    }
                    Rectangle rect = shell.getBounds();
                    rect.x = max_x + 16;
                    rect.y = max_y + 16;
                    shell.setBounds(rect);
                }
                // String before = "disp="+shell.getDisplay().getBounds()+",shell=" + shell.getBounds();
                Utils.verifyShellRect(shell, true);
            // Debug.outNoStack( "Opening torrent options dialog: " + before + " -> " + shell.getBounds());
            }
            dlg.addCloseListener(new SkinnedDialog.SkinnedDialogClosedListener() {

                @Override
                public void skinDialogClosed(SkinnedDialog dialog) {
                    try {
                        dispose();
                    } finally {
                        synchronized (active_windows) {
                            Iterator<OpenTorrentOptionsWindow> it = active_windows.values().iterator();
                            while (it.hasNext()) {
                                OpenTorrentOptionsWindow window = it.next();
                                if (window == OpenTorrentOptionsWindow.this) {
                                    it.remove();
                                }
                            }
                            TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
                            for (OpenTorrentInstance inst : open_instances) {
                                inst.cancelPressed();
                                t_man.optionsRemoved(inst.getOptions());
                            }
                        }
                    }
                }
            });
            window_initialised = true;
        } else {
            Composite expand_area = new Composite(expand_stack_area, SWT.NULL);
            expand_area.setLayout(new FormLayout());
            OpenTorrentInstance instance = new OpenTorrentInstance(hash, expand_area, torrentOptions, optionListener);
            addInstance(instance);
            if (!sash_object.isVisible()) {
                sash_object.setVisible(true);
                sash_object.setAboveVisible(true);
                Utils.execSWTThreadLater(0, new Runnable() {

                    @Override
                    public void run() {
                        tvTorrents.processDataSourceQueueSync();
                        List<TableRowCore> rows = new ArrayList<>();
                        for (OpenTorrentInstance instance : selected_instances) {
                            TableRowCore row = tvTorrents.getRow(instance);
                            if (row != null) {
                                rows.add(row);
                            }
                        }
                        if (rows.size() > 0) {
                            tvTorrents.setSelectedRows(rows.toArray(new TableRowCore[rows.size()]));
                        }
                    }
                });
            }
        }
    } catch (Throwable e) {
        Debug.out(e);
        synchronized (active_windows) {
            active_windows.remove(hash);
            torrentOptions.cancel();
            t_man.optionsRemoved(torrentOptions);
        }
    }
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) TableRowCore(com.biglybt.ui.common.table.TableRowCore) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) StackLayout(org.eclipse.swt.custom.StackLayout) List(java.util.List) TorrentManagerImpl(com.biglybt.pifimpl.local.torrent.TorrentManagerImpl) Point(org.eclipse.swt.graphics.Point) SkinnedDialog(com.biglybt.ui.swt.views.skin.SkinnedDialog) StandardButtonsArea(com.biglybt.ui.swt.views.skin.StandardButtonsArea)

Example 3 with TorrentManagerImpl

use of com.biglybt.pifimpl.local.torrent.TorrentManagerImpl in project BiglyBT by BiglySoftware.

the class OpenTorrentOptionsWindow method removeInstance.

private void removeInstance(OpenTorrentInstance instance, boolean is_removal) {
    TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
    synchronized (active_windows) {
        active_windows.remove(instance.getHash());
        t_man.optionsRemoved(instance.getOptions());
    }
    int index = open_instances.indexOf(instance);
    open_instances.remove(instance);
    updateDialogTitle();
    tvTorrents.removeDataSource(instance);
    instance.getComposite().dispose();
    updateInstanceInfo();
    if (selected_instances.contains(instance) && selected_instances.size() > 1) {
        List<OpenTorrentInstance> temp = new ArrayList<>(selected_instances);
        temp.remove(instance);
        selectInstances(temp);
    } else {
        int num_instances = open_instances.size();
        if (num_instances > index) {
            selectInstance(open_instances.get(index));
        } else if (num_instances > 0) {
            selectInstance(open_instances.get(num_instances - 1));
        } else {
            selectInstance(null);
        }
    }
    swt_updateTVTorrentButtons();
    refreshTVTorrentIndexes();
    if (is_removal) {
        instance.cancelPressed();
    }
    instance.dispose();
}
Also used : TorrentManagerImpl(com.biglybt.pifimpl.local.torrent.TorrentManagerImpl) Point(org.eclipse.swt.graphics.Point)

Example 4 with TorrentManagerImpl

use of com.biglybt.pifimpl.local.torrent.TorrentManagerImpl in project BiglyBT by BiglySoftware.

the class OpenTorrentOptionsWindow method okPressed.

private void okPressed() {
    TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
    boolean all_ok = true;
    AsyncDispatcher dispatcher = new AsyncDispatcher();
    for (final OpenTorrentInstance instance : new ArrayList<>(open_instances)) {
        String dataDir = instance.cmbDataDir.getText();
        if (!instance.okPressed(dataDir)) {
            all_ok = false;
        } else {
            // serialise additions in correct order
            t_man.optionsAccepted(instance.getOptions());
            dispatcher.dispatch(new AERunnable() {

                @Override
                public void runSupport() {
                    TorrentOpener.addTorrent(instance.getOptions());
                }
            });
            removeInstance(instance, false);
        }
    }
    if (all_ok) {
        if (dlg != null) {
            dlg.close();
        }
    }
}
Also used : TorrentManagerImpl(com.biglybt.pifimpl.local.torrent.TorrentManagerImpl)

Example 5 with TorrentManagerImpl

use of com.biglybt.pifimpl.local.torrent.TorrentManagerImpl in project BiglyBT by BiglySoftware.

the class UIFunctionsImpl method addTorrentWithOptions.

@Override
public boolean addTorrentWithOptions(final TorrentOpenOptions torrentOptions, Map<String, Object> addOptions) {
    Boolean is_silent = (Boolean) addOptions.get(UIFunctions.OTO_SILENT);
    if (is_silent == null) {
        is_silent = UIFunctions.OTO_SILENT_DEFAULT;
    }
    if (CoreFactory.isCoreRunning()) {
        Core core = CoreFactory.getSingleton();
        GlobalManager gm = core.getGlobalManager();
        // Check if torrent already exists in gm, and add if not
        TOTorrent torrent = torrentOptions.getTorrent();
        DownloadManager existingDownload = gm.getDownloadManager(torrent);
        if (existingDownload != null) {
            if (!is_silent) {
                final String fExistingName = existingDownload.getDisplayName();
                final DownloadManager fExistingDownload = existingDownload;
                fExistingDownload.fireGlobalManagerEvent(GlobalManagerEvent.ET_REQUEST_ATTENTION);
                Utils.execSWTThread(new AERunnable() {

                    @Override
                    public void runSupport() {
                        boolean can_merge = TorrentUtils.canMergeAnnounceURLs(torrentOptions.getTorrent(), fExistingDownload.getTorrent());
                        long existed_for = SystemTime.getCurrentTime() - fExistingDownload.getCreationTime();
                        Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
                        if ((Display.getDefault().getActiveShell() == null || !mainShell.isVisible() || mainShell.getMinimized()) && (!can_merge)) {
                            if (existed_for > 15 * 1000) {
                                new MessageSlideShell(Display.getCurrent(), SWT.ICON_INFORMATION, MSG_ALREADY_EXISTS, null, new String[] { // : prefix is deliberate to disable click on ref in message as might be an unwanted action
                                ":" + 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() {

                                    @Override
                                    public void prompterClosed(int result) {
                                        if (result == SWT.YES) {
                                            TorrentUtils.mergeAnnounceURLs(torrentOptions.getTorrent(), fExistingDownload.getTorrent());
                                        }
                                    }
                                });
                            } else {
                                if (existed_for > 15 * 1000) {
                                    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.getDeleteFileOnCancel()) {
                File torrentFile = new File(torrentOptions.sFileName);
                torrentFile.delete();
            }
            return (true);
        } else {
            try {
                final DownloadStub archived = core.getPluginManager().getDefaultPluginInterface().getDownloadManager().lookupDownloadStub(torrent.getHash());
                if (archived != null) {
                    if (is_silent) {
                        // restore it for them
                        archived.destubbify();
                        if (torrentOptions.getDeleteFileOnCancel()) {
                            File torrentFile = new File(torrentOptions.sFileName);
                            torrentFile.delete();
                        }
                        return (true);
                    } else {
                        Utils.execSWTThread(new AERunnable() {

                            @Override
                            public void runSupport() {
                                Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
                                String existingName = archived.getName();
                                if (Display.getDefault().getActiveShell() == null || !mainShell.isVisible() || mainShell.getMinimized()) {
                                    new MessageSlideShell(Display.getCurrent(), SWT.ICON_INFORMATION, "OpenTorrentWindow.mb.inArchive", null, new String[] { existingName }, new Object[0], -1);
                                } else {
                                    MessageBoxShell mb = new MessageBoxShell(SWT.OK, "OpenTorrentWindow.mb.inArchive", new String[] { existingName });
                                    mb.open(null);
                                }
                            }
                        });
                        return (true);
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
            if (!is_silent) {
                try {
                    DownloadHistoryManager dlm = (DownloadHistoryManager) core.getGlobalManager().getDownloadHistoryManager();
                    final long[] existing = dlm.getDates(torrentOptions.getTorrent().getHash());
                    if (existing != null) {
                        long redownloaded = existing[3];
                        if (SystemTime.getCurrentTime() - redownloaded > 60 * 10 * 1000) {
                            Utils.execSWTThread(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    Shell mainShell = UIFunctionsManagerSWT.getUIFunctionsSWT().getMainShell();
                                    if (mainShell != null && !mainShell.isDisposed()) {
                                        new MessageSlideShell(mainShell.getDisplay(), SWT.ICON_INFORMATION, "OpenTorrentWindow.mb.inHistory", null, new String[] { torrentOptions.getTorrentName(), new SimpleDateFormat().format(new Date(existing[0])) }, new Object[] {}, -1);
                                    }
                                }
                            });
                        }
                    }
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        }
    }
    Boolean force = (Boolean) addOptions.get(UIFunctions.OTO_FORCE_OPEN);
    if (force == null) {
        force = UIFunctions.OTO_FORCE_OPEN_DEFAULT;
    }
    if (!force) {
        TOTorrent torrent = torrentOptions.getTorrent();
        boolean is_featured = torrent != null && PlatformTorrentUtils.isFeaturedContent(torrent);
        String showAgainMode = COConfigurationManager.getStringParameter(ConfigurationDefaults.CFG_TORRENTADD_OPENOPTIONS);
        if (is_featured || is_silent || (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) {
                TorrentManagerImpl t_man = TorrentManagerImpl.getSingleton();
                t_man.optionsAdded(torrentOptions);
                t_man.optionsAccepted(torrentOptions);
                boolean ok = TorrentOpener.addTorrent(torrentOptions);
                t_man.optionsRemoved(torrentOptions);
                return (ok);
            }
            torrentOptions.setParentDir("");
            if (is_silent) {
                return (false);
            } else {
                MessageBoxShell mb = new MessageBoxShell(SWT.OK | SWT.ICON_ERROR, "OpenTorrentWindow.mb.invaliddefsave", new String[] { save_loc });
                mb.open(new UserPrompterResultListener() {

                    @Override
                    public void prompterClosed(int result) {
                        OpenTorrentOptionsWindow.addTorrent(torrentOptions);
                    }
                });
            }
            return (true);
        }
    }
    if (is_silent) {
        return (false);
    } else {
        OpenTorrentOptionsWindow.addTorrent(torrentOptions);
        return (true);
    }
}
Also used : DownloadHistoryManager(com.biglybt.core.history.DownloadHistoryManager) MessageSlideShell(com.biglybt.ui.swt.shells.MessageSlideShell) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) TorrentManagerImpl(com.biglybt.pifimpl.local.torrent.TorrentManagerImpl) DownloadManager(com.biglybt.core.download.DownloadManager) Date(java.util.Date) Point(org.eclipse.swt.graphics.Point) DownloadStub(com.biglybt.pif.download.DownloadStub) MessageSlideShell(com.biglybt.ui.swt.shells.MessageSlideShell) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) GlobalManager(com.biglybt.core.global.GlobalManager) TOTorrent(com.biglybt.core.torrent.TOTorrent) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) UISWTViewCore(com.biglybt.ui.swt.pifimpl.UISWTViewCore) Core(com.biglybt.core.Core)

Aggregations

TorrentManagerImpl (com.biglybt.pifimpl.local.torrent.TorrentManagerImpl)5 Point (org.eclipse.swt.graphics.Point)3 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)2 List (java.util.List)2 Core (com.biglybt.core.Core)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 DownloadHistoryManager (com.biglybt.core.history.DownloadHistoryManager)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TorrentOpenOptions (com.biglybt.core.torrent.impl.TorrentOpenOptions)1 DownloadStub (com.biglybt.pif.download.DownloadStub)1 TableRowCore (com.biglybt.ui.common.table.TableRowCore)1 UISWTViewCore (com.biglybt.ui.swt.pifimpl.UISWTViewCore)1 MessageSlideShell (com.biglybt.ui.swt.shells.MessageSlideShell)1 SkinnedDialog (com.biglybt.ui.swt.views.skin.SkinnedDialog)1 StandardButtonsArea (com.biglybt.ui.swt.views.skin.StandardButtonsArea)1 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 StackLayout (org.eclipse.swt.custom.StackLayout)1