Search in sources :

Example 6 with GlobalManagerAdapter

use of com.biglybt.core.global.GlobalManagerAdapter in project BiglyBT by BiglySoftware.

the class TorrentUIUtilsV3 method _loadTorrent.

private static void _loadTorrent(final Core core, final DownloadUrlInfo dlInfo, // open player
final boolean playNow, // as for open player but don't actually open it
final boolean playPrepare, final boolean bringToFront) {
    if (dlInfo instanceof DownloadUrlInfoSWT) {
        DownloadUrlInfoSWT dlInfoSWT = (DownloadUrlInfoSWT) dlInfo;
        dlInfoSWT.invoke(playNow ? "play" : "download");
        return;
    }
    String url = dlInfo.getDownloadURL();
    try {
        Matcher m = hashPattern.matcher(url);
        if (m.find()) {
            String hash = m.group(1);
            GlobalManager gm = core.getGlobalManager();
            final DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));
            if (dm != null) {
                if (playNow || playPrepare) {
                    new AEThread2("playExisting", true) {

                        @Override
                        public void run() {
                            if (playNow) {
                                Debug.outNoStack("loadTorrent already exists.. playing", false);
                                TorrentListViewsUtils.playOrStream(dm, -1);
                            } else {
                                Debug.outNoStack("loadTorrent already exists.. preparing", false);
                                PlayUtils.prepareForPlay(dm);
                            }
                        }
                    }.start();
                } else {
                    new MessageBoxShell(SWT.OK, MSG_ALREADY_EXISTS, new String[] { " ", dm.getDisplayName(), MessageText.getString(MSG_ALREADY_EXISTS_NAME) }).open(null);
                }
                return;
            }
        }
        UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
        if (uiFunctions != null) {
            // if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) { not used 11/30/2015
            if (bringToFront) {
                uiFunctions.bringToFront();
            }
            // }
            Shell shell = uiFunctions.getMainShell();
            if (shell != null) {
                new FileDownloadWindow(shell, url, dlInfo.getReferer(), dlInfo.getRequestProperties(), null, new TorrentDownloaderCallBackInterface() {

                    @Override
                    public void TorrentDownloaderEvent(int state, TorrentDownloader inf) {
                        if (state == TorrentDownloader.STATE_FINISHED) {
                            File file = inf.getFile();
                            file.deleteOnExit();
                            // Do a quick check to see if it's a torrent
                            if (!TorrentUtil.isFileTorrent(dlInfo.getDownloadURL(), file, file.getName(), true)) {
                                return;
                            }
                            TOTorrent torrent;
                            try {
                                torrent = TorrentUtils.readFromFile(file, false);
                            } catch (TOTorrentException e) {
                                Debug.out(e);
                                return;
                            }
                            // Security: Only allow torrents from whitelisted trackers
                            if (playNow && !PlatformTorrentUtils.isPlatformTracker(torrent)) {
                                Debug.out("stopped loading torrent because it's not in whitelist");
                                return;
                            }
                            HashWrapper hw;
                            try {
                                hw = torrent.getHashWrapper();
                            } catch (TOTorrentException e1) {
                                Debug.out(e1);
                                return;
                            }
                            GlobalManager gm = core.getGlobalManager();
                            if (playNow || playPrepare) {
                                DownloadManager existingDM = gm.getDownloadManager(hw);
                                if (existingDM != null) {
                                    if (playNow) {
                                        TorrentListViewsUtils.playOrStream(existingDM, -1);
                                    } else {
                                        PlayUtils.prepareForPlay(existingDM);
                                    }
                                    return;
                                }
                            }
                            final HashWrapper fhw = hw;
                            GlobalManagerListener l = new GlobalManagerAdapter() {

                                @Override
                                public void downloadManagerAdded(DownloadManager dm) {
                                    try {
                                        core.getGlobalManager().removeListener(this);
                                        handleDMAdded(dm, playNow, playPrepare, fhw);
                                    } catch (Exception e) {
                                        Debug.out(e);
                                    }
                                }
                            };
                            gm.addListener(l, false);
                            TorrentOpener.openTorrent(file.getAbsolutePath());
                        }
                    }
                });
            }
        }
    } catch (Exception e) {
        Debug.out(e);
    }
}
Also used : TorrentDownloaderCallBackInterface(com.biglybt.core.torrentdownloader.TorrentDownloaderCallBackInterface) Matcher(java.util.regex.Matcher) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) DownloadManager(com.biglybt.core.download.DownloadManager) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) DownloadUrlInfoSWT(com.biglybt.ui.swt.browser.listener.DownloadUrlInfoSWT) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) Shell(org.eclipse.swt.widgets.Shell) GlobalManager(com.biglybt.core.global.GlobalManager) GlobalManagerAdapter(com.biglybt.core.global.GlobalManagerAdapter) TOTorrent(com.biglybt.core.torrent.TOTorrent) GlobalManagerListener(com.biglybt.core.global.GlobalManagerListener) TorrentDownloader(com.biglybt.core.torrentdownloader.TorrentDownloader) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Example 7 with GlobalManagerAdapter

use of com.biglybt.core.global.GlobalManagerAdapter in project BiglyBT by BiglySoftware.

the class SB_Transfers method setupViewTitleWithCore.

protected void setupViewTitleWithCore(Core _core) {
    synchronized (SB_Transfers.class) {
        if (!first) {
            return;
        }
        first = false;
        core = _core;
        coreCreateTime = core.getCreateTime();
    }
    categoryListener = new CategoryListener() {

        @Override
        public void downloadManagerRemoved(Category cat, DownloadManager removed) {
            RefreshCategorySideBar(cat);
        }

        @Override
        public void downloadManagerAdded(Category cat, DownloadManager manager) {
            RefreshCategorySideBar(cat);
        }
    };
    paramCatInSidebarListener = new ParameterListener() {

        @Override
        public void parameterChanged(String parameterName) {
            if (Utils.isAZ2UI()) {
                return;
            }
            Category[] categories = CategoryManager.getCategories();
            if (categories.length == 0) {
                return;
            }
            boolean catInSidebar = COConfigurationManager.getBooleanParameter("Library.CatInSideBar");
            if (catInSidebar) {
                if (categoryManagerListener != null) {
                    return;
                }
                categoryManagerListener = new CategoryManagerListener() {

                    @Override
                    public void categoryRemoved(Category category) {
                        removeCategory(category);
                    }

                    @Override
                    public void categoryChanged(Category category) {
                        RefreshCategorySideBar(category);
                    }

                    @Override
                    public void categoryAdded(Category category) {
                        Category[] categories = CategoryManager.getCategories();
                        if (categories.length == 3) {
                            for (Category cat : categories) {
                                setupCategory(cat);
                            }
                        } else {
                            setupCategory(category);
                        }
                    }
                };
                CategoryManager.addCategoryManagerListener(categoryManagerListener);
                if (categories.length > 2) {
                    for (Category category : categories) {
                        category.addCategoryListener(categoryListener);
                        setupCategory(category);
                    }
                }
            } else {
                if (categoryManagerListener != null) {
                    CategoryManager.removeCategoryManagerListener(categoryManagerListener);
                    categoryManagerListener = null;
                }
                for (Category category : categories) {
                    category.removeCategoryListener(categoryListener);
                    removeCategory(category);
                }
            }
        }
    };
    COConfigurationManager.addAndFireParameterListener("Library.CatInSideBar", paramCatInSidebarListener);
    tagListener = new TagListener() {

        @Override
        public void taggableAdded(Tag tag, Taggable tagged) {
            refreshTagSideBar(tag);
        }

        @Override
        public void taggableSync(Tag tag) {
            refreshTagSideBar(tag);
        }

        @Override
        public void taggableRemoved(Tag tag, Taggable tagged) {
            refreshTagSideBar(tag);
        }
    };
    paramTagsInSidebarListener = new ParameterListener() {

        @Override
        public void parameterChanged(String parameterName) {
            boolean tagInSidebar = COConfigurationManager.getBooleanParameter("Library.TagInSideBar");
            if (tagInSidebar) {
                addTagManagerListeners();
            } else {
                removeTagManagerListeners(true);
            }
        }
    };
    COConfigurationManager.addAndFireParameterListener("Library.TagInSideBar", paramTagsInSidebarListener);
    final GlobalManager gm = core.getGlobalManager();
    dmListener = new DownloadManagerAdapter() {

        @Override
        public void stateChanged(DownloadManager dm, int state) {
            stateChanged(dm, state, statsNoLowNoise);
            stateChanged(dm, state, statsWithLowNoise);
        }

        public void stateChanged(DownloadManager dm, int state, stats stats) {
            if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
                return;
            }
            synchronized (statsLock) {
                updateDMCounts(dm);
                boolean complete = dm.getAssumedComplete();
                Boolean wasErrorStateB = (Boolean) dm.getUserData("wasErrorState");
                boolean wasErrorState = wasErrorStateB != null && wasErrorStateB.booleanValue();
                boolean isErrorState = state == DownloadManager.STATE_ERROR;
                if (isErrorState != wasErrorState) {
                    int rel = isErrorState ? 1 : -1;
                    if (complete) {
                        stats.numErrorComplete += rel;
                    } else {
                        stats.numErrorInComplete += rel;
                    }
                    updateErrorTooltip(gm, stats);
                    dm.setUserData("wasErrorState", Boolean.valueOf(isErrorState));
                }
                refreshAllLibraries();
            }
        }

        @Override
        public void completionChanged(DownloadManager dm, boolean completed) {
            completionChanged(dm, completed, statsNoLowNoise);
            completionChanged(dm, completed, statsWithLowNoise);
        }

        public void completionChanged(DownloadManager dm, boolean completed, stats stats) {
            if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
                return;
            }
            synchronized (statsLock) {
                int dm_state = updateDMCounts(dm);
                if (completed) {
                    stats.numComplete++;
                    stats.numIncomplete--;
                    if (dm_state == DownloadManager.STATE_ERROR) {
                        stats.numErrorComplete++;
                        stats.numErrorInComplete--;
                    }
                    if (dm_state == DownloadManager.STATE_STOPPED) {
                        statsNoLowNoise.numStoppedIncomplete--;
                    }
                } else {
                    stats.numComplete--;
                    stats.numIncomplete++;
                    if (dm_state == DownloadManager.STATE_ERROR) {
                        stats.numErrorComplete--;
                        stats.numErrorInComplete++;
                    }
                    if (dm_state == DownloadManager.STATE_STOPPED) {
                        statsNoLowNoise.numStoppedIncomplete++;
                    }
                }
                recountUnopened();
                updateErrorTooltip(gm, stats);
                refreshAllLibraries();
            }
        }
    };
    gmListener = new GlobalManagerAdapter() {

        @Override
        public void downloadManagerRemoved(DownloadManager dm) {
            downloadManagerRemoved(dm, statsNoLowNoise);
            downloadManagerRemoved(dm, statsWithLowNoise);
        }

        public void downloadManagerRemoved(DownloadManager dm, stats stats) {
            if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
                return;
            }
            synchronized (statsLock) {
                recountUnopened();
                if (dm.getAssumedComplete()) {
                    stats.numComplete--;
                    Boolean wasDownloadingB = (Boolean) dm.getUserData("wasDownloading");
                    if (wasDownloadingB != null && wasDownloadingB.booleanValue()) {
                        stats.numDownloading--;
                    }
                } else {
                    stats.numIncomplete--;
                    Boolean wasSeedingB = (Boolean) dm.getUserData("wasSeeding");
                    if (wasSeedingB != null && wasSeedingB.booleanValue()) {
                        stats.numSeeding--;
                    }
                }
                Boolean wasStoppedB = (Boolean) dm.getUserData("wasStopped");
                boolean wasStopped = wasStoppedB != null && wasStoppedB.booleanValue();
                if (wasStopped) {
                    if (!dm.getAssumedComplete()) {
                        stats.numStoppedIncomplete--;
                    }
                }
                Boolean wasQueuedB = (Boolean) dm.getUserData("wasQueued");
                boolean wasQueued = wasQueuedB != null && wasQueuedB.booleanValue();
                if (wasQueued) {
                    stats.numQueued--;
                }
                refreshAllLibraries();
            }
            dm.removeListener(dmListener);
        }

        @Override
        public void downloadManagerAdded(DownloadManager dm) {
            dm.addListener(dmListener, false);
            synchronized (statsLock) {
                recountUnopened();
                downloadManagerAdded(dm, statsNoLowNoise);
                downloadManagerAdded(dm, statsWithLowNoise);
                refreshAllLibraries();
            }
        }

        public void downloadManagerAdded(DownloadManager dm, stats stats) {
            if (!stats.includeLowNoise && PlatformTorrentUtils.isAdvancedViewOnly(dm)) {
                return;
            }
            boolean assumed_complete = dm.getAssumedComplete();
            synchronized (statsLock) {
                if (dm.isPersistent() && dm.getTorrent() != null && !assumed_complete) {
                    // ignore borked torrents as their create time is inaccurate
                    stats.newestIncompleteDownloadTime = Math.max(stats.newestIncompleteDownloadTime, dm.getCreationTime());
                }
                int dm_state = dm.getState();
                if (assumed_complete) {
                    stats.numComplete++;
                    if (dm_state == DownloadManager.STATE_SEEDING) {
                        stats.numSeeding++;
                    }
                } else {
                    stats.numIncomplete++;
                    if (dm_state == DownloadManager.STATE_DOWNLOADING) {
                        dm.setUserData("wasDownloading", Boolean.TRUE);
                        stats.numDownloading++;
                    } else {
                        dm.setUserData("wasDownloading", Boolean.FALSE);
                    }
                }
            }
        }
    };
    gm.addListener(gmListener, false);
    resetStats(gm, dmListener, statsWithLowNoise, statsNoLowNoise);
    refreshAllLibraries();
    timerEventPeriodic = SimpleTimer.addPeriodicEvent("header:refresh", 60 * 1000, new TimerEventPerformer() {

        @Override
        public void perform(TimerEvent event) {
            stats withNoise = new stats();
            stats noNoise = new stats();
            noNoise.includeLowNoise = false;
            withNoise.includeLowNoise = true;
            synchronized (statsLock) {
                resetStats(gm, null, withNoise, noNoise);
                boolean fixed = false;
                if (!withNoise.sameAs(statsWithLowNoise)) {
                    statsWithLowNoise.copyFrom(withNoise);
                    fixed = true;
                }
                if (!noNoise.sameAs(statsNoLowNoise)) {
                    statsNoLowNoise.copyFrom(noNoise);
                    fixed = true;
                }
                if (fixed) {
                    updateErrorTooltip(gm, statsWithLowNoise);
                    updateErrorTooltip(gm, statsNoLowNoise);
                    refreshAllLibraries();
                }
            }
        }
    });
}
Also used : Category(com.biglybt.core.category.Category) CategoryManagerListener(com.biglybt.core.category.CategoryManagerListener) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerAdapter(com.biglybt.core.download.impl.DownloadManagerAdapter) GlobalManager(com.biglybt.core.global.GlobalManager) GlobalManagerAdapter(com.biglybt.core.global.GlobalManagerAdapter) ParameterListener(com.biglybt.core.config.ParameterListener) CategoryListener(com.biglybt.core.category.CategoryListener)

Aggregations

GlobalManagerAdapter (com.biglybt.core.global.GlobalManagerAdapter)7 DownloadManager (com.biglybt.core.download.DownloadManager)5 ParameterListener (com.biglybt.core.config.ParameterListener)3 GlobalManager (com.biglybt.core.global.GlobalManager)3 Category (com.biglybt.core.category.Category)2 CategoryListener (com.biglybt.core.category.CategoryListener)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)2 File (java.io.File)2 CategoryManagerListener (com.biglybt.core.category.CategoryManagerListener)1 DHT (com.biglybt.core.dht.DHT)1 DHTListener (com.biglybt.core.dht.DHTListener)1 DHTSpeedTester (com.biglybt.core.dht.speed.DHTSpeedTester)1 DownloadManagerAdapter (com.biglybt.core.download.impl.DownloadManagerAdapter)1 GlobalManagerListener (com.biglybt.core.global.GlobalManagerListener)1 LogAlert (com.biglybt.core.logging.LogAlert)1 LogEvent (com.biglybt.core.logging.LogEvent)1 NetworkAdmin (com.biglybt.core.networkmanager.admin.NetworkAdmin)1 NetworkAdminPropertyChangeListener (com.biglybt.core.networkmanager.admin.NetworkAdminPropertyChangeListener)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1