Search in sources :

Example 1 with BuddyPluginTracker

use of com.biglybt.plugin.net.buddy.tracker.BuddyPluginTracker in project BiglyBT by BiglySoftware.

the class BuddyPlugin method initialize.

@Override
public void initialize(final PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    ta_category = plugin_interface.getTorrentManager().getAttribute(TorrentAttribute.TA_CATEGORY);
    az2_handler = new BuddyPluginAZ2(this);
    sec_man = plugin_interface.getUtilities().getSecurityManager();
    logger = plugin_interface.getLogger().getChannel("Friends");
    logger.setDiagnostic();
    final LocaleUtilities lu = plugin_interface.getUtilities().getLocaleUtilities();
    lu.addListener(new LocaleListener() {

        @Override
        public void localeChanged(Locale l) {
            updateLocale(lu);
        }
    });
    updateLocale(lu);
    BasicPluginConfigModel config = plugin_interface.getUIManager().createBasicPluginConfigModel("Views.plugins." + VIEW_ID + ".title");
    // enabled
    classic_enabled_param = config.addBooleanParameter2("azbuddy.enabled", "azbuddy.enabled", false);
    // nickname
    nick_name_param = config.addStringParameter2("azbuddy.nickname", "azbuddy.nickname", "");
    nick_name_param.setGenerateIntermediateEvents(false);
    nick_name_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            updateNickName(nick_name_param.getValue());
        }
    });
    // online status
    String[] os_values = STATUS_VALUES;
    String[] os_labels = STATUS_STRINGS;
    online_status_param = config.addStringListParameter2("azbuddy.online_status", "azbuddy.online_status", os_values, os_labels, os_values[0]);
    online_status_param.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            int status = Integer.parseInt(online_status_param.getValue());
            updateOnlineStatus(status);
        }
    });
    // If we add this then use proper message texts in the STATUS_STRINGS
    online_status_param.setVisible(SUPPORT_ONLINE_STATUS);
    // protocol speed
    final IntParameter protocol_speed = config.addIntParameter2("azbuddy.protocolspeed", "azbuddy.protocolspeed", 32);
    protocol_speed.setMinimumRequiredUserMode(Parameter.MODE_ADVANCED);
    ConnectionManager cman = plugin_interface.getConnectionManager();
    int inbound_limit = protocol_speed.getValue() * 1024;
    inbound_limiter = cman.createRateLimiter("buddy_up", inbound_limit);
    outbound_limiter = cman.createRateLimiter("buddy_down", 0);
    protocol_speed.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            inbound_limiter.setRateLimitBytesPerSecond(protocol_speed.getValue() * 1024);
        }
    });
    // chat notifications
    enable_chat_notifications = config.addBooleanParameter2("azbuddy.enable_chat_notif", "azbuddy.enable_chat_notif", true);
    // default published tags or cats
    cat_pub = config.addStringParameter2("azbuddy.enable_cat_pub", "azbuddy.enable_cat_pub", "");
    cat_pub.setGenerateIntermediateEvents(false);
    setPublicTagsOrCategories(cat_pub.getValue(), false);
    final BooleanParameter tracker_enable = config.addBooleanParameter2("azbuddy.tracker.enabled", "azbuddy.tracker.enabled", true);
    final BooleanParameter tracker_so_enable = config.addBooleanParameter2("azbuddy.tracker.seeding.only.enabled", "azbuddy.tracker.seeding.only.enabled", false);
    final BooleanParameter buddies_lan_local = config.addBooleanParameter2("azbuddy.tracker.con.lan.local", "azbuddy.tracker.con.lan.local", true);
    buddies_lan_local.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            lan_local_peers = buddies_lan_local.getValue();
        }
    });
    lan_local_peers = buddies_lan_local.getValue();
    cat_pub.addListener(new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            setPublicTagsOrCategories(cat_pub.getValue(), false);
        }
    });
    config.createGroup("label.friends", new Parameter[] { classic_enabled_param, nick_name_param, online_status_param, protocol_speed, enable_chat_notifications, cat_pub, tracker_enable, tracker_so_enable, buddies_lan_local });
    // decentralised stuff
    beta_enabled_param = config.addBooleanParameter2("azbuddy.dchat.decentralized.enabled", "azbuddy.dchat.decentralized.enabled", true);
    config.createGroup("azbuddy.dchat.decentralized", new Parameter[] { beta_enabled_param });
    // config end
    beta_plugin = new BuddyPluginBeta(plugin_interface, this, beta_enabled_param);
    final TableContextMenuItem menu_item_itorrents = plugin_interface.getUIManager().getTableManager().addContextMenuItem(TableManager.TABLE_MYTORRENTS_INCOMPLETE, "azbuddy.contextmenu");
    final TableContextMenuItem menu_item_ctorrents = plugin_interface.getUIManager().getTableManager().addContextMenuItem(TableManager.TABLE_MYTORRENTS_COMPLETE, "azbuddy.contextmenu");
    menu_item_itorrents.setStyle(TableContextMenuItem.STYLE_MENU);
    menu_item_itorrents.setHeaderCategory(MenuItem.HEADER_SOCIAL);
    menu_item_ctorrents.setStyle(TableContextMenuItem.STYLE_MENU);
    menu_item_ctorrents.setHeaderCategory(MenuItem.HEADER_SOCIAL);
    MenuItemFillListener menu_fill_listener = new MenuItemFillListener() {

        @Override
        public void menuWillBeShown(MenuItem menu, Object _target) {
            menu.removeAllChildItems();
            if (!(isClassicEnabled() && isAvailable())) {
                menu.setEnabled(false);
                return;
            }
            final List<Torrent> torrents = new ArrayList<>();
            if (_target instanceof TableRow) {
                addDownload(torrents, (TableRow) _target);
            } else {
                TableRow[] rows = (TableRow[]) _target;
                for (TableRow row : rows) {
                    addDownload(torrents, row);
                }
            }
            if (torrents.size() == 0) {
                menu.setEnabled(false);
            } else {
                List<BuddyPluginBuddy> buddies = getBuddies();
                boolean incomplete = ((TableContextMenuItem) menu).getTableID() == TableManager.TABLE_MYTORRENTS_INCOMPLETE;
                TableContextMenuItem parent = incomplete ? menu_item_itorrents : menu_item_ctorrents;
                for (int i = 0; i < buddies.size(); i++) {
                    final BuddyPluginBuddy buddy = (BuddyPluginBuddy) buddies.get(i);
                    boolean online = buddy.isOnline(true);
                    TableContextMenuItem item = plugin_interface.getUIManager().getTableManager().addContextMenuItem(parent, "!" + buddy.getName() + (online ? "" : (" - " + MessageText.getString("label.disconnected"))) + "!");
                    item.addMultiListener(new MenuItemListener() {

                        @Override
                        public void selected(MenuItem menu, Object target) {
                            for (Torrent torrent : torrents) {
                                az2_handler.sendAZ2Torrent(torrent, buddy);
                            }
                        }
                    });
                    item.setEnabled(online);
                }
                menu.setEnabled(true);
            }
        }

        protected void addDownload(List<Torrent> torrents, TableRow row) {
            Object obj = row.getDataSource();
            Download download;
            if (obj instanceof Download) {
                download = (Download) obj;
            } else {
                DiskManagerFileInfo file = (DiskManagerFileInfo) obj;
                try {
                    download = file.getDownload();
                } catch (DownloadException e) {
                    Debug.printStackTrace(e);
                    return;
                }
            }
            Torrent torrent = download.getTorrent();
            if (torrent != null && !TorrentUtils.isReallyPrivate(PluginCoreUtils.unwrap(torrent))) {
                torrents.add(torrent);
            }
        }
    };
    menu_item_itorrents.addFillListener(menu_fill_listener);
    menu_item_ctorrents.addFillListener(menu_fill_listener);
    buddy_tracker = new BuddyPluginTracker(this, tracker_enable, tracker_so_enable);
    plugin_interface.getUIManager().addUIListener(new UIManagerListener() {

        @Override
        public void UIAttached(final UIInstance instance) {
            if (instance.getUIType().equals(UIInstance.UIT_SWT)) {
                try {
                    synchronized (swt_ui_waiters) {
                        swt_ui = (BuddyPluginViewInterface) Class.forName("com.biglybt.plugin.net.buddy.swt.BuddyPluginView").getConstructor(new Class[] { BuddyPlugin.class, UIInstance.class, String.class }).newInstance(new Object[] { BuddyPlugin.this, instance, VIEW_ID });
                        for (Runnable r : swt_ui_waiters) {
                            try {
                                r.run();
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                        swt_ui_waiters.clear();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
            setupDisablePrompt(instance);
        }

        @Override
        public void UIDetached(UIInstance instance) {
            if (instance.getUIType().equals(UIInstance.UIT_SWT) && swt_ui != null) {
                swt_ui.destroy();
                swt_ui = null;
            }
        }
    });
    ParameterListener enabled_listener = new ParameterListener() {

        @Override
        public void parameterChanged(Parameter param) {
            boolean classic_enabled = classic_enabled_param.getValue();
            nick_name_param.setEnabled(classic_enabled);
            online_status_param.setEnabled(classic_enabled);
            protocol_speed.setEnabled(classic_enabled);
            enable_chat_notifications.setEnabled(classic_enabled);
            cat_pub.setEnabled(classic_enabled);
            tracker_enable.setEnabled(classic_enabled);
            tracker_so_enable.setEnabled(tracker_enable.getValue());
            if (param != null) {
                setClassicEnabledInternal(classic_enabled);
                fireEnabledStateChanged();
            }
        }
    };
    enabled_listener.parameterChanged(null);
    classic_enabled_param.addListener(enabled_listener);
    beta_enabled_param.addListener(enabled_listener);
    tracker_enable.addListener(enabled_listener);
    loadConfig();
    registerMessageHandler();
    plugin_interface.addListener(new PluginListener() {

        @Override
        public void initializationComplete() {
            final DelayedTask dt = plugin_interface.getUtilities().createDelayedTask(new Runnable() {

                @Override
                public void run() {
                    new AEThread2("BuddyPlugin:init", true) {

                        @Override
                        public void run() {
                            startup();
                            beta_plugin.startup();
                            initialization_complete.set(true);
                        }
                    }.start();
                }
            });
            dt.queue();
        }

        @Override
        public void closedownInitiated() {
            saveConfig(true);
            closedown();
            beta_plugin.closedown();
        }

        @Override
        public void closedownComplete() {
        }
    });
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TableContextMenuItem(com.biglybt.pif.ui.tables.TableContextMenuItem) PluginListener(com.biglybt.pif.PluginListener) ConnectionManager(com.biglybt.pif.network.ConnectionManager) DownloadException(com.biglybt.pif.download.DownloadException) MenuItemListener(com.biglybt.pif.ui.menus.MenuItemListener) BuddyPluginTracker(com.biglybt.plugin.net.buddy.tracker.BuddyPluginTracker) Download(com.biglybt.pif.download.Download) UIInstance(com.biglybt.pif.ui.UIInstance) DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) TableContextMenuItem(com.biglybt.pif.ui.tables.TableContextMenuItem) MenuItem(com.biglybt.pif.ui.menus.MenuItem) MenuItemFillListener(com.biglybt.pif.ui.menus.MenuItemFillListener) TableRow(com.biglybt.pif.ui.tables.TableRow) ParameterListener(com.biglybt.pif.ui.config.ParameterListener) UIManagerListener(com.biglybt.pif.ui.UIManagerListener) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Aggregations

PluginListener (com.biglybt.pif.PluginListener)1 DiskManagerFileInfo (com.biglybt.pif.disk.DiskManagerFileInfo)1 Download (com.biglybt.pif.download.Download)1 DownloadException (com.biglybt.pif.download.DownloadException)1 ConnectionManager (com.biglybt.pif.network.ConnectionManager)1 Torrent (com.biglybt.pif.torrent.Torrent)1 UIInstance (com.biglybt.pif.ui.UIInstance)1 UIManagerListener (com.biglybt.pif.ui.UIManagerListener)1 ParameterListener (com.biglybt.pif.ui.config.ParameterListener)1 MenuItem (com.biglybt.pif.ui.menus.MenuItem)1 MenuItemFillListener (com.biglybt.pif.ui.menus.MenuItemFillListener)1 MenuItemListener (com.biglybt.pif.ui.menus.MenuItemListener)1 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)1 TableContextMenuItem (com.biglybt.pif.ui.tables.TableContextMenuItem)1 TableRow (com.biglybt.pif.ui.tables.TableRow)1 BuddyPluginTracker (com.biglybt.plugin.net.buddy.tracker.BuddyPluginTracker)1