Search in sources :

Example 1 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class CategoryUIUtils method createMenuItems.

public static void createMenuItems(final Menu menu, final Category category) {
    if (category.getType() == Category.TYPE_USER) {
        final MenuItem itemDelete = new MenuItem(menu, SWT.PUSH);
        Messages.setLanguageText(itemDelete, "MyTorrentsView.menu.category.delete");
        itemDelete.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
                List<DownloadManager> managers = category.getDownloadManagers(gm.getDownloadManagers());
                // move to array,since setCategory removed it from the category,
                // which would mess up our loop
                DownloadManager[] dms = managers.toArray(new DownloadManager[managers.size()]);
                for (DownloadManager dm : dms) {
                    DownloadManagerState state = dm.getDownloadState();
                    if (state != null) {
                        state.setCategory(null);
                    }
                }
                CategoryManager.removeCategory(category);
            }
        });
    }
    if (category.getType() != Category.TYPE_ALL) {
        long kInB = DisplayFormatters.getKinB();
        long maxDownload = COConfigurationManager.getIntParameter("Max Download Speed KBs", 0) * kInB;
        long maxUpload = COConfigurationManager.getIntParameter("Max Upload Speed KBs", 0) * kInB;
        int down_speed = category.getDownloadSpeed();
        int up_speed = category.getUploadSpeed();
        ViewUtils.addSpeedMenu(menu.getShell(), menu, true, true, true, true, false, down_speed == 0, down_speed, down_speed, maxDownload, false, up_speed == 0, up_speed, up_speed, maxUpload, 1, null, new SpeedAdapter() {

            @Override
            public void setDownSpeed(int val) {
                category.setDownloadSpeed(val);
            }

            @Override
            public void setUpSpeed(int val) {
                category.setUploadSpeed(val);
            }
        });
    }
    GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
    List<DownloadManager> managers = category.getDownloadManagers(gm.getDownloadManagers());
    final DownloadManager[] dms = managers.toArray(new DownloadManager[managers.size()]);
    boolean start = false;
    boolean stop = false;
    for (DownloadManager dm : dms) {
        stop = stop || ManagerUtils.isStopable(dm);
        start = start || ManagerUtils.isStartable(dm);
    }
    // Queue
    final MenuItem itemQueue = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(itemQueue, "MyTorrentsView.menu.queue");
    Utils.setMenuItemImage(itemQueue, "start");
    itemQueue.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
            List<?> managers = category.getDownloadManagers(gm.getDownloadManagers());
            Object[] dms = managers.toArray();
            TorrentUtil.queueDataSources(dms, true);
        }
    });
    itemQueue.setEnabled(start);
    // Stop
    final MenuItem itemStop = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(itemStop, "MyTorrentsView.menu.stop");
    Utils.setMenuItemImage(itemStop, "stop");
    itemStop.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
            List<?> managers = category.getDownloadManagers(gm.getDownloadManagers());
            Object[] dms = managers.toArray();
            TorrentUtil.stopDataSources(dms);
        }
    });
    itemStop.setEnabled(stop);
    if (category.canBePublic()) {
        new MenuItem(menu, SWT.SEPARATOR);
        final MenuItem itemPublic = new MenuItem(menu, SWT.CHECK);
        itemPublic.setSelection(category.isPublic());
        Messages.setLanguageText(itemPublic, "cat.share");
        itemPublic.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                category.setPublic(itemPublic.getSelection());
            }
        });
    }
    // share with friends
    PluginInterface bpi = PluginInitializer.getDefaultInterface().getPluginManager().getPluginInterfaceByClass(BuddyPlugin.class);
    int cat_type = category.getType();
    if (bpi != null && cat_type != Category.TYPE_UNCATEGORIZED) {
        final BuddyPlugin buddy_plugin = (BuddyPlugin) bpi.getPlugin();
        if (buddy_plugin.isClassicEnabled()) {
            final Menu share_menu = new Menu(menu.getShell(), SWT.DROP_DOWN);
            final MenuItem share_item = new MenuItem(menu, SWT.CASCADE);
            Messages.setLanguageText(share_item, "azbuddy.ui.menu.cat.share");
            share_item.setMenu(share_menu);
            List<BuddyPluginBuddy> buddies = buddy_plugin.getBuddies();
            if (buddies.size() == 0) {
                final MenuItem item = new MenuItem(share_menu, SWT.CHECK);
                item.setText(MessageText.getString("general.add.friends"));
                item.setEnabled(false);
            } else {
                final String cname;
                if (cat_type == Category.TYPE_ALL) {
                    cname = "All";
                } else {
                    cname = category.getName();
                }
                final boolean is_public = buddy_plugin.isPublicTagOrCategory(cname);
                final MenuItem itemPubCat = new MenuItem(share_menu, SWT.CHECK);
                Messages.setLanguageText(itemPubCat, "general.all.friends");
                itemPubCat.setSelection(is_public);
                itemPubCat.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
                        if (is_public) {
                            buddy_plugin.removePublicTagOrCategory(cname);
                        } else {
                            buddy_plugin.addPublicTagOrCategory(cname);
                        }
                    }
                });
                new MenuItem(share_menu, SWT.SEPARATOR);
                for (final BuddyPluginBuddy buddy : buddies) {
                    if (buddy.getNickName() == null) {
                        continue;
                    }
                    final boolean auth = buddy.isLocalRSSTagOrCategoryAuthorised(cname);
                    final MenuItem itemShare = new MenuItem(share_menu, SWT.CHECK);
                    itemShare.setText(buddy.getName());
                    itemShare.setSelection(auth || is_public);
                    if (is_public) {
                        itemShare.setEnabled(false);
                    }
                    itemShare.addListener(SWT.Selection, new Listener() {

                        @Override
                        public void handleEvent(Event event) {
                            if (auth) {
                                buddy.removeLocalAuthorisedRSSTagOrCategory(cname);
                            } else {
                                buddy.addLocalAuthorisedRSSTagOrCategory(cname);
                            }
                        }
                    });
                }
            }
        }
    }
    if (category.getType() != Category.TYPE_ALL) {
        TrancodeUIUtils.TranscodeTarget[] tts = TrancodeUIUtils.getTranscodeTargets();
        if (tts.length > 0) {
            final Menu t_menu = new Menu(menu.getShell(), SWT.DROP_DOWN);
            final MenuItem t_item = new MenuItem(menu, SWT.CASCADE);
            Messages.setLanguageText(t_item, "cat.autoxcode");
            t_item.setMenu(t_menu);
            String existing = category.getStringAttribute(Category.AT_AUTO_TRANSCODE_TARGET);
            for (TrancodeUIUtils.TranscodeTarget tt : tts) {
                TrancodeUIUtils.TranscodeProfile[] profiles = tt.getProfiles();
                if (profiles.length > 0) {
                    final Menu tt_menu = new Menu(t_menu.getShell(), SWT.DROP_DOWN);
                    final MenuItem tt_item = new MenuItem(t_menu, SWT.CASCADE);
                    tt_item.setText(tt.getName());
                    tt_item.setMenu(tt_menu);
                    for (final TrancodeUIUtils.TranscodeProfile tp : profiles) {
                        final MenuItem p_item = new MenuItem(tt_menu, SWT.CHECK);
                        p_item.setText(tp.getName());
                        boolean selected = existing != null && existing.equals(tp.getUID());
                        if (selected) {
                            Utils.setMenuItemImage(tt_item, "blacktick");
                        }
                        p_item.setSelection(selected);
                        p_item.addListener(SWT.Selection, new Listener() {

                            @Override
                            public void handleEvent(Event event) {
                                category.setStringAttribute(Category.AT_AUTO_TRANSCODE_TARGET, p_item.getSelection() ? tp.getUID() : null);
                            }
                        });
                    }
                }
            }
        }
    }
    // rss feed
    final MenuItem rssOption = new MenuItem(menu, SWT.CHECK);
    rssOption.setSelection(category.getBooleanAttribute(Category.AT_RSS_GEN));
    Messages.setLanguageText(rssOption, "cat.rss.gen");
    rssOption.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            boolean set = rssOption.getSelection();
            category.setBooleanAttribute(Category.AT_RSS_GEN, set);
        }
    });
    if (cat_type != Category.TYPE_UNCATEGORIZED && cat_type != Category.TYPE_ALL) {
        final MenuItem upPriority = new MenuItem(menu, SWT.CHECK);
        upPriority.setSelection(category.getIntAttribute(Category.AT_UPLOAD_PRIORITY) > 0);
        Messages.setLanguageText(upPriority, "cat.upload.priority");
        upPriority.addListener(SWT.Selection, new Listener() {

            @Override
            public void handleEvent(Event event) {
                boolean set = upPriority.getSelection();
                category.setIntAttribute(Category.AT_UPLOAD_PRIORITY, set ? 1 : 0);
            }
        });
    }
    // options
    MenuItem itemOptions = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(itemOptions, "cat.options");
    itemOptions.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
            if (uiFunctions != null) {
                MultipleDocumentInterface mdi = uiFunctions.getMDI();
                if (mdi != null) {
                    mdi.showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_OPTIONS, dms);
                }
            }
        }
    });
    if (dms.length == 0) {
        itemOptions.setEnabled(false);
    }
}
Also used : Listener(org.eclipse.swt.widgets.Listener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) MenuListener(org.eclipse.swt.events.MenuListener) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) BuddyPlugin(com.biglybt.plugin.net.buddy.BuddyPlugin) SpeedAdapter(com.biglybt.ui.swt.views.ViewUtils.SpeedAdapter) GlobalManager(com.biglybt.core.global.GlobalManager) UIFunctions(com.biglybt.ui.UIFunctions) List(java.util.List) Menu(org.eclipse.swt.widgets.Menu) PluginInterface(com.biglybt.pif.PluginInterface) MenuItem(org.eclipse.swt.widgets.MenuItem) MultipleDocumentInterface(com.biglybt.ui.mdi.MultipleDocumentInterface) BuddyPluginBuddy(com.biglybt.plugin.net.buddy.BuddyPluginBuddy) Event(org.eclipse.swt.widgets.Event) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 2 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method restoreSessionTotals.

@Override
public void restoreSessionTotals(long _saved_data_bytes_downloaded, long _saved_data_bytes_uploaded, long _saved_discarded, long _saved_hashfails, long _saved_SecondsDownloading, long _saved_SecondsOnlySeeding) {
    saved_data_bytes_downloaded = _saved_data_bytes_downloaded;
    saved_data_bytes_uploaded = _saved_data_bytes_uploaded;
    saved_discarded = _saved_discarded;
    saved_hashfails = _saved_hashfails;
    saved_SecondsDownloading = _saved_SecondsDownloading;
    saved_SecondsOnlySeeding = _saved_SecondsOnlySeeding;
    session_start_data_bytes_downloaded = saved_data_bytes_downloaded;
    session_start_data_bytes_uploaded = _saved_data_bytes_uploaded;
    DownloadManagerState state = download_manager.getDownloadState();
    saved_SecondsSinceDownload = state.getIntAttribute(DownloadManagerState.AT_TIME_SINCE_DOWNLOAD);
    saved_SecondsSinceUpload = state.getIntAttribute(DownloadManagerState.AT_TIME_SINCE_UPLOAD);
    saved_peak_receive_rate = state.getLongAttribute(DownloadManagerState.AT_PEAK_RECEIVE_RATE);
    saved_peak_send_rate = state.getLongAttribute(DownloadManagerState.AT_PEAK_SEND_RATE);
    if (saved_data_bytes_downloaded > 0 && saved_completed_download_bytes == 0) {
        // Bug where 0 is stored in config for torrent's saved_completed_download_bytes
        // when there's actually completed data.  Force a recalc on next request
        saved_completed_download_bytes = -1;
    }
}
Also used : DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

Example 3 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method saveSessionTotals.

protected void saveSessionTotals() {
    checkShareRatioProgress();
    // re-base the totals from current totals and session totals
    saved_data_bytes_downloaded = getTotalDataBytesReceived();
    saved_data_bytes_uploaded = getTotalDataBytesSent();
    saved_protocol_bytes_downloaded = getTotalProtocolBytesReceived();
    saved_protocol_bytes_uploaded = getTotalProtocolBytesSent();
    saved_discarded = getDiscarded();
    saved_hashfails = getHashFailBytes();
    saved_SecondsDownloading = getSecondsDownloading();
    saved_SecondsOnlySeeding = getSecondsOnlySeeding();
    saved_SecondsSinceDownload = getTimeSinceLastDataReceivedInSeconds();
    saved_SecondsSinceUpload = getTimeSinceLastDataSentInSeconds();
    saved_peak_receive_rate = getPeakDataReceiveRate();
    saved_peak_send_rate = getPeakDataSendRate();
    DownloadManagerState state = download_manager.getDownloadState();
    state.setIntAttribute(DownloadManagerState.AT_TIME_SINCE_DOWNLOAD, saved_SecondsSinceDownload);
    state.setIntAttribute(DownloadManagerState.AT_TIME_SINCE_UPLOAD, saved_SecondsSinceUpload);
    state.setLongAttribute(DownloadManagerState.AT_AVAIL_BAD_TIME, getAvailWentBadTime());
    state.setLongAttribute(DownloadManagerState.AT_PEAK_RECEIVE_RATE, saved_peak_receive_rate);
    state.setLongAttribute(DownloadManagerState.AT_PEAK_SEND_RATE, saved_peak_send_rate);
}
Also used : DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

Example 4 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class GlobalManagerFileMerger method syncFileSets.

void syncFileSets() {
    List<DownloadManager> dms = gm.getDownloadManagers();
    synchronized (dm_map) {
        boolean changed = false;
        Set<HashWrapper> existing_dm_hashes = new HashSet<>(dm_map.keySet());
        if (enabled) {
            for (DownloadManager dm : dms) {
                /* 
					 * not sure why we were ignoring shares, one might share a local file in order to help
					 * out a 'normal' download
					 * 
					if ( !dm.isPersistent()){

						continue;
					}
					*/
                DownloadManagerState state = dm.getDownloadState();
                if (state.getFlag(DownloadManagerState.FLAG_LOW_NOISE) || state.getFlag(DownloadManagerState.FLAG_METADATA_DOWNLOAD)) {
                    continue;
                }
                if (enabled_extended || !dm.isDownloadComplete(false)) {
                    TOTorrent torrent = dm.getTorrent();
                    if (torrent != null) {
                        try {
                            HashWrapper hw = torrent.getHashWrapper();
                            if (dm_map.containsKey(hw)) {
                                existing_dm_hashes.remove(hw);
                            } else {
                                dm_map.put(hw, dm);
                                changed = true;
                            }
                        } catch (Throwable e) {
                        }
                    }
                }
            }
        }
        if (existing_dm_hashes.size() > 0) {
            changed = true;
            for (HashWrapper hw : existing_dm_hashes) {
                dm_map.remove(hw);
            }
        }
        if (changed) {
            List<Set<DiskManagerFileInfo>> interesting = new LinkedList<>();
            Map<Long, Set<DiskManagerFileInfo>> size_map = new HashMap<>();
            for (DownloadManager dm : dm_map.values()) {
                TOTorrent torrent = dm.getTorrent();
                if (torrent == null) {
                    continue;
                }
                DiskManagerFileInfo[] files = dm.getDiskManagerFileInfoSet().getFiles();
                for (DiskManagerFileInfo file : files) {
                    if (file.getNbPieces() < MIN_PIECES) {
                        continue;
                    }
                    long len = file.getLength();
                    Set<DiskManagerFileInfo> set = size_map.get(len);
                    if (set == null) {
                        set = new HashSet<>();
                        size_map.put(len, set);
                    }
                    boolean same_dm = false;
                    for (DiskManagerFileInfo existing : set) {
                        if (existing.getDownloadManager() == dm) {
                            same_dm = true;
                            break;
                        }
                    }
                    if (!same_dm) {
                        set.add(file);
                        if (set.size() == 2) {
                            interesting.add(set);
                        }
                    }
                }
            }
            // remove sets consisting of only completed files
            Iterator<Set<DiskManagerFileInfo>> interesting_it = interesting.iterator();
            while (interesting_it.hasNext()) {
                Set<DiskManagerFileInfo> set = interesting_it.next();
                boolean all_done = true;
                for (DiskManagerFileInfo file : set) {
                    if (file.getDownloaded() != file.getLength()) {
                        all_done = false;
                        break;
                    }
                }
                if (all_done) {
                    interesting_it.remove();
                }
            }
            List<SameSizeFiles> sames_copy = new LinkedList<>(sames);
            for (Set<DiskManagerFileInfo> set : interesting) {
                boolean found = false;
                Iterator<SameSizeFiles> sames_it = sames_copy.iterator();
                while (sames_it.hasNext()) {
                    SameSizeFiles same = sames_it.next();
                    if (same.sameAs(set)) {
                        found = true;
                        sames_it.remove();
                        break;
                    }
                }
                if (!found) {
                    sames.add(new SameSizeFiles(set));
                }
            }
            for (SameSizeFiles dead : sames_copy) {
                dead.destroy();
                sames.remove(dead);
            }
            if (sames.size() > 0) {
                if (timer_event == null) {
                    timer_event = SimpleTimer.addPeriodicEvent("GMFM:sync", TIMER_PERIOD, new TimerEventPerformer() {

                        private int tick_count = 0;

                        @Override
                        public void perform(TimerEvent event) {
                            tick_count++;
                            synchronized (dm_map) {
                                for (SameSizeFiles s : sames) {
                                    s.sync(tick_count);
                                }
                            }
                        }
                    });
                }
            } else {
                if (timer_event != null) {
                    timer_event.cancel();
                    timer_event = null;
                }
            }
        }
    }
}
Also used : DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 5 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class SubscriptionManagerImpl method prepareDownload.

protected void prepareDownload(Download download, Subscription[] subscriptions, SubscriptionResult[] results) {
    try {
        if (subscriptions.length > 0) {
            // deal with first only for cat/tag/nets as will always be just one when called from downloadAdded
            Subscription subs = subscriptions[0];
            if (results != null && results.length > 0) {
                try {
                    SubscriptionResult result = results[0];
                    Map<Integer, Object> props = result.toPropertyMap();
                    Long leechers = (Long) props.get(SearchResult.PR_LEECHER_COUNT);
                    Long seeds = (Long) props.get(SearchResult.PR_SEED_COUNT);
                    if (leechers != null && seeds != null && leechers >= 0 && seeds >= 0) {
                        com.biglybt.core.download.DownloadManager core_dm = PluginCoreUtils.unwrap(download);
                        DownloadManagerState state = core_dm.getDownloadState();
                        long cache = ((seeds & 0x00ffffffL) << 32) | (leechers & 0x00ffffffL);
                        state.setLongAttribute(DownloadManagerState.AT_SCRAPE_CACHE_SOURCE, 1);
                        state.setLongAttribute(DownloadManagerState.AT_SCRAPE_CACHE, cache);
                    }
                } catch (Throwable e) {
                }
            }
            String category = subs.getCategory();
            if (category != null) {
                String existing = download.getAttribute(ta_category);
                if (existing == null) {
                    download.setAttribute(ta_category, category);
                }
            }
            long tag_id = subs.getTagID();
            if (tag_id >= 0) {
                Tag tag = TagManagerFactory.getTagManager().lookupTagByUID(tag_id);
                if (tag != null) {
                    com.biglybt.core.download.DownloadManager core_dm = PluginCoreUtils.unwrap(download);
                    if (!tag.hasTaggable(core_dm)) {
                        tag.addTaggable(core_dm);
                    }
                }
            }
            String[] nets = subs.getHistory().getDownloadNetworks();
            if (nets != null) {
                com.biglybt.core.download.DownloadManager core_dm = PluginCoreUtils.unwrap(download);
                DownloadManagerState state = core_dm.getDownloadState();
                state.setNetworks(nets);
                // ensure that other cide (e.g. the open-torrent stuff) doesn't over-write this
                state.setFlag(DownloadManagerState.FLAG_INITIAL_NETWORKS_SET, true);
            }
        }
    } catch (Throwable e) {
        log("Failed to prepare association", e);
    }
}
Also used : DownloadManagerState(com.biglybt.core.download.DownloadManagerState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) com.biglybt.pif.download(com.biglybt.pif.download) Tag(com.biglybt.core.tag.Tag)

Aggregations

DownloadManagerState (com.biglybt.core.download.DownloadManagerState)38 File (java.io.File)14 DownloadManager (com.biglybt.core.download.DownloadManager)11 TOTorrent (com.biglybt.core.torrent.TOTorrent)9 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)8 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)6 CacheFile (com.biglybt.core.diskmanager.cache.CacheFile)6 PEPeerManager (com.biglybt.core.peer.PEPeerManager)5 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)5 Download (com.biglybt.pif.download.Download)4 UIInputReceiverListener (com.biglybt.pif.ui.UIInputReceiverListener)4 IOException (java.io.IOException)4 Map (java.util.Map)4 CoreRunningListener (com.biglybt.core.CoreRunningListener)3 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)3 Tag (com.biglybt.core.tag.Tag)3 UIFunctions (com.biglybt.ui.UIFunctions)3 List (java.util.List)3 Core (com.biglybt.core.Core)2 ParameterListener (com.biglybt.core.config.ParameterListener)2