Search in sources :

Example 6 with BasicPluginViewModel

use of com.biglybt.pif.ui.model.BasicPluginViewModel in project BiglyBT by BiglySoftware.

the class PluginUpdatePlugin method initialize.

@Override
public void initialize(PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    plugin_interface.getPluginProperties().setProperty("plugin.version", "1.0");
    plugin_interface.getPluginProperties().setProperty("plugin.name", "Plugin Updater");
    log = plugin_interface.getLogger().getChannel("Plugin Update");
    log.setDiagnostic();
    log.setForce(true);
    UIManager ui_manager = plugin_interface.getUIManager();
    final BasicPluginViewModel model = ui_manager.createBasicPluginViewModel(PLUGIN_RESOURCE_ID);
    final PluginConfig plugin_config = plugin_interface.getPluginconfig();
    boolean enabled = plugin_config.getPluginBooleanParameter("enable.update", true);
    model.setConfigSectionID(PLUGIN_CONFIGSECTION_ID);
    model.getStatus().setText(enabled ? "Running" : "Optional checks disabled");
    model.getActivity().setVisible(false);
    model.getProgress().setVisible(false);
    log.addListener(new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String message) {
            model.getLogArea().appendText(message + "\n");
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            model.getLogArea().appendText(error.toString() + "\n");
        }
    });
    BasicPluginConfigModel config = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, PLUGIN_CONFIGSECTION_ID);
    config.addBooleanParameter2("enable.update", "Plugin.pluginupdate.enablecheck", true);
    plugin_interface.addEventListener(new PluginEventListener() {

        @Override
        public void handleEvent(PluginEvent ev) {
            if (ev.getType() == PluginEvent.PEV_ALL_PLUGINS_INITIALISED) {
                plugin_interface.removeEventListener(this);
                initComplete(plugin_config);
            }
        }
    });
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) UIManager(com.biglybt.pif.ui.UIManager) BasicPluginViewModel(com.biglybt.pif.ui.model.BasicPluginViewModel) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Example 7 with BasicPluginViewModel

use of com.biglybt.pif.ui.model.BasicPluginViewModel in project BiglyBT by BiglySoftware.

the class ExternalSeedPlugin method initialize.

@Override
public void initialize(PluginInterface _plugin_interface) {
    plugin_interface = _plugin_interface;
    dm_stats = plugin_interface.getDownloadManager().getStats();
    log = plugin_interface.getLogger().getTimeStampedChannel("External Seeds");
    final BasicPluginViewModel view_model = plugin_interface.getUIManager().createBasicPluginViewModel("Plugin.extseed.name");
    view_model.getActivity().setVisible(false);
    view_model.getProgress().setVisible(false);
    log.addListener(new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String content) {
            view_model.getLogArea().appendText(content + "\n");
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            if (str.length() > 0) {
                view_model.getLogArea().appendText(str + "\n");
            }
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            error.printStackTrace(pw);
            pw.flush();
            view_model.getLogArea().appendText(sw.toString() + "\n");
        }
    });
    status_field = view_model.getStatus();
    setStatus("Initialising");
    download_mon = plugin_interface.getUtilities().getMonitor();
    Utilities utilities = plugin_interface.getUtilities();
    final DelayedTask dt = plugin_interface.getUtilities().createDelayedTask(new Runnable() {

        @Override
        public void run() {
            AEThread2 t = new AEThread2("ExternalSeedInitialise", true) {

                @Override
                public void run() {
                    setStatus("Running");
                    plugin_interface.getDownloadManager().addListener(ExternalSeedPlugin.this);
                }
            };
            t.setPriority(Thread.MIN_PRIORITY);
            t.start();
        }
    });
    dt.queue();
    UTTimer timer = utilities.createTimer("ExternalPeerScheduler", true);
    timer.addPeriodicEvent(5000, new UTTimerEventPerformer() {

        @Override
        public void perform(UTTimerEvent event) {
            try {
                Iterator it = download_map.values().iterator();
                while (it.hasNext()) {
                    List peers = randomiseList((List) it.next());
                    for (int i = 0; i < peers.size(); i++) {
                        if (((ExternalSeedPeer) peers.get(i)).checkConnection()) {
                            break;
                        }
                    }
                }
            } catch (Throwable e) {
            // we do this without holding the monitor as doing so causes potential
            // deadlock between download_mon and the connection's connection_mon
            // so ignore possible errors here that may be caused by concurrent
            // modification to the download_map ans associated lists. We are only
            // reading the data so errors will only be transient
            }
        }
    });
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) BasicPluginViewModel(com.biglybt.pif.ui.model.BasicPluginViewModel) AEThread2(com.biglybt.core.util.AEThread2) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 8 with BasicPluginViewModel

use of com.biglybt.pif.ui.model.BasicPluginViewModel in project BiglyBT by BiglySoftware.

the class UISWTInstanceImpl method eventOccurred.

@Override
public boolean eventOccurred(final UIManagerEvent event) {
    boolean done = true;
    final Object data = event.getData();
    switch(event.getType()) {
        case UIManagerEvent.ET_SHOW_TEXT_MESSAGE:
            {
                Utils.execSWTThread(new Runnable() {

                    @Override
                    public void run() {
                        String[] params = (String[]) data;
                        new TextViewerWindow(params[0], params[1], params[2]);
                    }
                });
                break;
            }
        case UIManagerEvent.ET_SHOW_MSG_BOX:
            {
                final int[] result = { UIManagerEvent.MT_NONE };
                Utils.execSWTThread(new Runnable() {

                    @Override
                    public void run() {
                        UIFunctionsManagerSWT.getUIFunctionsSWT().bringToFront();
                        Object[] params = (Object[]) data;
                        long _styles = ((Long) (params[2])).longValue();
                        int styles = 0;
                        int def = 0;
                        if ((_styles & UIManagerEvent.MT_YES) != 0) {
                            styles |= SWT.YES;
                        }
                        if ((_styles & UIManagerEvent.MT_YES_DEFAULT) != 0) {
                            styles |= SWT.YES;
                            def = SWT.YES;
                        }
                        if ((_styles & UIManagerEvent.MT_NO) != 0) {
                            styles |= SWT.NO;
                        }
                        if ((_styles & UIManagerEvent.MT_NO_DEFAULT) != 0) {
                            styles |= SWT.NO;
                            def = SWT.NO;
                        }
                        if ((_styles & UIManagerEvent.MT_OK) != 0) {
                            styles |= SWT.OK;
                        }
                        if ((_styles & UIManagerEvent.MT_OK_DEFAULT) != 0) {
                            styles |= SWT.OK;
                            def = SWT.OK;
                        }
                        if ((_styles & UIManagerEvent.MT_CANCEL) != 0) {
                            styles |= SWT.CANCEL;
                        }
                        MessageBoxShell mb = new MessageBoxShell(styles, MessageText.getString((String) params[0]), MessageText.getString((String) params[1]));
                        if (def != 0) {
                            mb.setDefaultButtonUsingStyle(def);
                        }
                        if (params.length == 4 && params[3] instanceof Map) {
                            Map<String, Object> options = (Map<String, Object>) params[3];
                            String rememberID = (String) options.get(UIManager.MB_PARAM_REMEMBER_ID);
                            Boolean rememberByDefault = (Boolean) options.get(UIManager.MB_PARAM_REMEMBER_BY_DEF);
                            String rememberText = (String) options.get(UIManager.MB_PARAM_REMEMBER_RES);
                            if (rememberID != null && rememberByDefault != null && rememberText != null) {
                                mb.setRemember(rememberID, rememberByDefault, rememberText);
                                Number rememberIfOnlyButton = (Number) options.get(UIManager.MB_PARAM_REMEMBER_IF_ONLY_BUTTON);
                                if (rememberIfOnlyButton != null) {
                                    mb.setRememberOnlyIfButton(rememberIfOnlyButton.intValue());
                                }
                            }
                            Number auto_close_ms = (Number) options.get(UIManager.MB_PARAM_AUTO_CLOSE_MS);
                            if (auto_close_ms != null) {
                                mb.setAutoCloseInMS(auto_close_ms.intValue());
                            }
                        } else if (params.length >= 6) {
                            String rememberID = (String) params[3];
                            Boolean rememberByDefault = (Boolean) params[4];
                            String rememberText = (String) params[5];
                            if (rememberID != null && rememberByDefault != null && rememberText != null) {
                                mb.setRemember(rememberID, rememberByDefault, rememberText);
                            }
                        }
                        mb.open(null);
                        int _r = mb.waitUntilClosed();
                        int r = 0;
                        if ((_r & SWT.YES) != 0) {
                            r |= UIManagerEvent.MT_YES;
                        }
                        if ((_r & SWT.NO) != 0) {
                            r |= UIManagerEvent.MT_NO;
                        }
                        if ((_r & SWT.OK) != 0) {
                            r |= UIManagerEvent.MT_OK;
                        }
                        if ((_r & SWT.CANCEL) != 0) {
                            r |= UIManagerEvent.MT_CANCEL;
                        }
                        result[0] = r;
                    }
                }, false);
                event.setResult(new Long(result[0]));
                break;
            }
        case UIManagerEvent.ET_OPEN_TORRENT_VIA_FILE:
            {
                TorrentOpener.openTorrent(((File) data).toString());
                break;
            }
        case UIManagerEvent.ET_OPEN_TORRENT_VIA_TORRENT:
            {
                Torrent t = (Torrent) data;
                try {
                    File f = AETemporaryFileHandler.createTempFile();
                    t.writeToFile(f);
                    TorrentOpener.openTorrent(f.toString());
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
                break;
            }
        case UIManagerEvent.ET_OPEN_TORRENT_VIA_URL:
            {
                Display display = Utils.getDisplay();
                display.syncExec(new AERunnable() {

                    @Override
                    public void runSupport() {
                        Object[] params = (Object[]) data;
                        URL target = (URL) params[0];
                        URL referrer = (URL) params[1];
                        boolean auto_download = ((Boolean) params[2]).booleanValue();
                        Map<?, ?> request_properties = (Map<?, ?>) params[3];
                        if (auto_download) {
                            final Shell shell = uiFunctions.getMainShell();
                            if (shell != null) {
                                final List<String> alt_uris = new ArrayList<>();
                                if (request_properties != null) {
                                    request_properties = new HashMap(request_properties);
                                    for (int i = 1; i < 16; i++) {
                                        String key = "X-Alternative-URI-" + i;
                                        String uri = (String) request_properties.remove(key);
                                        if (uri != null) {
                                            alt_uris.add(uri);
                                        } else {
                                            break;
                                        }
                                    }
                                }
                                final Map<?, ?> f_request_properties = request_properties;
                                new FileDownloadWindow(shell, target.toString(), referrer == null ? null : referrer.toString(), request_properties, new Runnable() {

                                    int alt_index = 0;

                                    @Override
                                    public void run() {
                                        if (alt_index < alt_uris.size()) {
                                            String alt_target = alt_uris.get(alt_index++);
                                            new FileDownloadWindow(shell, alt_target, null, f_request_properties, this);
                                        }
                                    }
                                });
                            }
                        } else {
                            // TODO: handle referrer?
                            TorrentOpener.openTorrent(target.toString());
                        }
                    }
                });
                break;
            }
        case UIManagerEvent.ET_PLUGIN_VIEW_MODEL_CREATED:
            {
                if (data instanceof BasicPluginViewModel) {
                    BasicPluginViewModel model = (BasicPluginViewModel) data;
                    // property bundles can't handle spaces in keys
                    // 
                    // If this behaviour changes, change the openView(model)
                    // method lower down.
                    String sViewID = model.getName().replaceAll(" ", ".");
                    BasicPluginViewImpl view = new BasicPluginViewImpl(model);
                    addView(UISWTInstance.VIEW_MAIN, sViewID, view);
                }
                break;
            }
        case UIManagerEvent.ET_PLUGIN_VIEW_MODEL_DESTROYED:
            {
                if (data instanceof BasicPluginViewModel) {
                    BasicPluginViewModel model = (BasicPluginViewModel) data;
                    // property bundles can't handle spaces in keys
                    // 
                    // If this behaviour changes, change the openView(model)
                    // method lower down.
                    String sViewID = model.getName().replaceAll(" ", ".");
                    removeViews(UISWTInstance.VIEW_MAIN, sViewID);
                }
                break;
            }
        case UIManagerEvent.ET_PLUGIN_CONFIG_MODEL_CREATED:
            {
                if (data instanceof BasicPluginConfigModel) {
                    BasicPluginConfigModel model = (BasicPluginConfigModel) data;
                    BasicPluginConfigImpl view = new BasicPluginConfigImpl(new WeakReference<>(model));
                    config_view_map.put(model, view);
                    ConfigSectionRepository.getInstance().addConfigSection(view, model.getPluginInterface());
                }
                break;
            }
        case UIManagerEvent.ET_PLUGIN_CONFIG_MODEL_DESTROYED:
            {
                if (data instanceof BasicPluginConfigModel) {
                    BasicPluginConfigModel model = (BasicPluginConfigModel) data;
                    BasicPluginConfigImpl view = config_view_map.get(model);
                    if (view != null) {
                        ConfigSectionRepository.getInstance().removeConfigSection(view);
                    }
                }
                break;
            }
        case UIManagerEvent.ET_COPY_TO_CLIPBOARD:
            {
                ClipboardCopy.copyToClipBoard((String) data);
                break;
            }
        case UIManagerEvent.ET_OPEN_URL:
            {
                Utils.launch(((URL) data).toExternalForm());
                break;
            }
        case UIManagerEvent.ET_SHOW_CONFIG_SECTION:
            {
                event.setResult(Boolean.FALSE);
                if (!(data instanceof String)) {
                    break;
                }
                event.setResult(Boolean.TRUE);
                uiFunctions.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_CONFIG, data);
                break;
            }
        case UIManagerEvent.ET_FILE_OPEN:
            {
                File file_to_use = (File) data;
                Utils.launch(file_to_use.getAbsolutePath());
                break;
            }
        case UIManagerEvent.ET_FILE_SHOW:
            {
                File file_to_use = (File) data;
                final boolean use_open_containing_folder = COConfigurationManager.getBooleanParameter("MyTorrentsView.menu.show_parent_folder_enabled");
                ManagerUtils.open(file_to_use, use_open_containing_folder);
                break;
            }
        case UIManagerEvent.ET_HIDE_ALL:
            {
                boolean hide = (Boolean) data;
                uiFunctions.setHideAll(hide);
                break;
            }
        case UIManagerEvent.ET_HIDE_ALL_TOGGLE:
            {
                uiFunctions.setHideAll(!uiFunctions.getHideAll());
                break;
            }
        default:
            {
                done = false;
                break;
            }
    }
    return (done);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) Torrent(com.biglybt.pif.torrent.Torrent) BasicPluginViewModel(com.biglybt.pif.ui.model.BasicPluginViewModel) URL(java.net.URL) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) Shell(org.eclipse.swt.widgets.Shell) WeakReference(java.lang.ref.WeakReference) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) AERunnable(com.biglybt.core.util.AERunnable) File(java.io.File) Display(org.eclipse.swt.widgets.Display) BasicPluginConfigModel(com.biglybt.pif.ui.model.BasicPluginConfigModel)

Aggregations

BasicPluginViewModel (com.biglybt.pif.ui.model.BasicPluginViewModel)8 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)5 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)5 UIManager (com.biglybt.pif.ui.UIManager)4 DelayedTask (com.biglybt.pif.utils.DelayedTask)2 UTTimerEvent (com.biglybt.pif.utils.UTTimerEvent)2 UTTimerEventPerformer (com.biglybt.pif.utils.UTTimerEventPerformer)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 DHT (com.biglybt.core.dht.DHT)1 DHTLogger (com.biglybt.core.dht.DHTLogger)1 DHTControlActivity (com.biglybt.core.dht.control.DHTControlActivity)1 DHTControlContact (com.biglybt.core.dht.control.DHTControlContact)1 DHTNATPuncher (com.biglybt.core.dht.nat.DHTNATPuncher)1 DHTRouterContact (com.biglybt.core.dht.router.DHTRouterContact)1 DHTTransportContact (com.biglybt.core.dht.transport.DHTTransportContact)1 DHTTransportFullStats (com.biglybt.core.dht.transport.DHTTransportFullStats)1 DHTTransportUDP (com.biglybt.core.dht.transport.udp.DHTTransportUDP)1 DHTTransportUDPImpl (com.biglybt.core.dht.transport.udp.impl.DHTTransportUDPImpl)1