Search in sources :

Example 1 with CryptoManagerKeyListener

use of com.biglybt.core.security.CryptoManagerKeyListener in project BiglyBT by BiglySoftware.

the class BuddyPluginViewInstance method createClassic.

private void createClassic(Composite main) {
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    main.setLayout(layout);
    GridData grid_data = new GridData(GridData.FILL_BOTH);
    main.setLayoutData(grid_data);
    // info
    Composite info_area = new Composite(main, SWT.NULL);
    layout = new GridLayout();
    layout.numColumns = 4;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    info_area.setLayout(layout);
    grid_data = new GridData(GridData.FILL_HORIZONTAL);
    grid_data.horizontalSpan = 3;
    info_area.setLayoutData(grid_data);
    Label label = new Label(info_area, SWT.NULL);
    label.setText(lu.getLocalisedMessageText("azbuddy.classic.info"));
    new LinkLabel(info_area, "ConfigView.label.please.visit.here", Wiki.FRIENDS);
    label = new Label(info_area, SWT.NULL);
    grid_data = new GridData(GridData.FILL_HORIZONTAL);
    label.setLayoutData(grid_data);
    final Button config_button = new Button(info_area, SWT.NULL);
    config_button.setText(lu.getLocalisedMessageText("plugins.basicview.config"));
    config_button.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            plugin.showConfig();
        }
    });
    classic_enabled = plugin.isClassicEnabled();
    if (!classic_enabled) {
        Label control_label = new Label(main, SWT.NULL);
        control_label.setText(lu.getLocalisedMessageText("azbuddy.disabled"));
        return;
    }
    final Composite form = new Composite(main, SWT.NONE);
    FormLayout flayout = new FormLayout();
    flayout.marginHeight = 0;
    flayout.marginWidth = 0;
    form.setLayout(flayout);
    GridData gridData = new GridData(GridData.FILL_BOTH);
    form.setLayoutData(gridData);
    final Composite child1 = new Composite(form, SWT.NULL);
    layout = new GridLayout();
    layout.numColumns = 1;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    child1.setLayout(layout);
    CTabFolder tab_folder = new CTabFolder(child1, SWT.LEFT);
    tab_folder.setBorderVisible(true);
    tab_folder.setTabHeight(20);
    grid_data = new GridData(GridData.FILL_BOTH);
    tab_folder.setLayoutData(grid_data);
    CTabItem public_item = null;
    Composite public_area = null;
    CTabItem anon_item = null;
    Composite anon_area = null;
    for (int i = 0; i < 2; i++) {
        boolean is_pub_tab = i == 0;
        CTabItem tab_item = new CTabItem(tab_folder, SWT.NULL);
        Composite tab_area = new Composite(tab_folder, SWT.NULL);
        if (is_pub_tab) {
            public_item = tab_item;
            public_area = tab_area;
        } else {
            anon_item = tab_item;
            anon_area = tab_area;
        }
        tab_item.setText(lu.getLocalisedMessageText(is_pub_tab ? "label.public" : "label.anon"));
        tab_item.setControl(tab_area);
        layout = new GridLayout();
        layout.numColumns = 1;
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        tab_area.setLayout(layout);
        final Composite controls = new Composite(tab_area, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 5;
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        controls.setLayout(layout);
        grid_data = new GridData(GridData.FILL_HORIZONTAL);
        controls.setLayoutData(grid_data);
        ;
        Label control_label = new Label(controls, SWT.NULL);
        control_label.setText(lu.getLocalisedMessageText("azbuddy.ui.new_buddy") + " ");
        final Text control_text = new Text(controls, SWT.BORDER);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        control_text.setLayoutData(gridData);
        final Button control_button = new Button(controls, SWT.NULL);
        control_button.setText(lu.getLocalisedMessageText("Button.add"));
        control_button.setEnabled(false);
        control_text.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                control_button.setEnabled(plugin.verifyPublicKey(is_pub_tab, control_text.getText().trim()));
            }
        });
        control_button.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                plugin.addBuddy(is_pub_tab, control_text.getText().trim(), BuddyPluginNetwork.SUBSYSTEM_AZ2);
                control_text.setText("");
            }
        });
        final Label control_lab_pk = new Label(controls, SWT.NULL);
        control_lab_pk.setText(lu.getLocalisedMessageText("azbuddy.ui.mykey") + " ");
        final Label control_val_pk = new Label(controls, SWT.NULL);
        gridData = new GridData();
        control_val_pk.setLayoutData(gridData);
        ClipboardCopy.addCopyToClipMenu(control_val_pk, new ClipboardCopy.copyToClipProvider() {

            @Override
            public String getText() {
                return ((String) control_val_pk.getData("key"));
            }
        });
        control_val_pk.setData("key", "");
        final CryptoManager crypt_man = CryptoManagerFactory.getSingleton();
        int ecc_inst = is_pub_tab ? 1 : 2;
        byte[] public_key = crypt_man.getECCHandler(ecc_inst).peekPublicKey();
        if (public_key == null) {
            Messages.setLanguageText(control_val_pk, "ConfigView.section.security.publickey.undef");
        } else {
            String str = Base32.encode(public_key);
            control_val_pk.setData("key", str);
            control_val_pk.setText(truncate(str));
        }
        Messages.setLanguageText(control_val_pk, "ConfigView.copy.to.clipboard.tooltip", true);
        control_val_pk.setCursor(main.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
        control_val_pk.setForeground(main.getDisplay().getSystemColor(SWT.COLOR_LINK_FOREGROUND));
        control_val_pk.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDoubleClick(MouseEvent arg0) {
                copyToClipboard();
            }

            @Override
            public void mouseDown(MouseEvent arg0) {
                copyToClipboard();
            }

            protected void copyToClipboard() {
                new Clipboard(control_val_pk.getDisplay()).setContents(new Object[] { control_val_pk.getData("key") }, new Transfer[] { TextTransfer.getInstance() });
            }
        });
        cryptoManagerKeyListener = new CryptoManagerKeyListener() {

            @Override
            public void keyChanged(CryptoHandler handler) {
                if (control_val_pk.isDisposed()) {
                    crypt_man.removeKeyListener(this);
                } else if (handler.getType() == CryptoManager.HANDLER_ECC) {
                    if (handler.getInstance() == ecc_inst) {
                        control_val_pk.getDisplay().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                byte[] public_key = handler.peekPublicKey();
                                if (public_key == null) {
                                    Messages.setLanguageText(control_val_pk, "ConfigView.section.security.publickey.undef");
                                    control_val_pk.setData("key", "");
                                } else {
                                    String str = Base32.encode(public_key);
                                    control_val_pk.setText(truncate(str));
                                    control_val_pk.setData("key", str);
                                }
                                controls.layout();
                            }
                        });
                    }
                }
            }

            @Override
            public void keyLockStatusChanged(CryptoHandler handler) {
            }
        };
        crypt_man.addKeyListener(cryptoManagerKeyListener);
        main.addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
                if (cryptoManagerKeyListener != null) {
                    CryptoManagerFactory.getSingleton().removeKeyListener(cryptoManagerKeyListener);
                }
            }
        });
    }
    final Sash sash = new Sash(form, SWT.HORIZONTAL);
    final Composite child2 = new Composite(form, SWT.NULL);
    layout = new GridLayout();
    layout.numColumns = 1;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    child2.setLayout(layout);
    FormData formData;
    // child1
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.top = new FormAttachment(0, 0);
    child1.setLayoutData(formData);
    final FormData child1Data = formData;
    final int SASH_WIDTH = 4;
    // sash
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.top = new FormAttachment(child1);
    formData.height = SASH_WIDTH;
    sash.setLayoutData(formData);
    // child2
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.bottom = new FormAttachment(100, 0);
    formData.top = new FormAttachment(sash);
    child2.setLayoutData(formData);
    final PluginConfig pc = plugin.getPluginInterface().getPluginconfig();
    sash.setData("PCT", new Float(pc.getPluginFloatParameter("swt.sash.position", 0.7f)));
    sash.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (e.detail == SWT.DRAG) {
                return;
            }
            child1Data.height = e.y + e.height - SASH_WIDTH;
            form.layout();
            Float l = new Float((double) child1.getBounds().height / form.getBounds().height);
            sash.setData("PCT", l);
            pc.setPluginParameter("swt.sash.position", l.floatValue());
        }
    });
    form.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            Float l = (Float) sash.getData("PCT");
            if (l != null) {
                child1Data.height = (int) (form.getBounds().height * l.doubleValue());
                form.layout();
            }
        }
    });
    // table
    Comparator<BuddyPluginBuddy> comparator = addBuddyTable(public_area, true);
    addBuddyTable(anon_area, false);
    Utils.setEnabled(anon_area, I2PHelpers.isI2PInstalled());
    Label pblab = new Label(child1, SWT.NULL);
    pblab.setText(lu.getLocalisedMessageText("label.partial.friends"));
    Comparator<PartialBuddy> pbcomparator = addPartialBuddyTable(child1);
    // log area
    log = new StyledText(child2, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    grid_data = new GridData(GridData.FILL_BOTH);
    grid_data.horizontalSpan = 1;
    log.setLayoutData(grid_data);
    log.setIndent(4);
    public_buddies.clear();
    anon_buddies.clear();
    List<BuddyPluginBuddy> buddies = plugin.getBuddies();
    for (BuddyPluginBuddy buddy : buddies) {
        if (buddy.isPublicNetwork()) {
            public_buddies.add(buddy);
        } else {
            anon_buddies.add(buddy);
        }
        buddyAdded(buddy);
    }
    Collections.sort(public_buddies, comparator);
    Collections.sort(anon_buddies, comparator);
    partial_buddies = plugin.getPartialBuddies();
    for (int i = 0; i < partial_buddies.size(); i++) {
        partialBuddyAdded(partial_buddies.get(i));
    }
    Collections.sort(partial_buddies, pbcomparator);
    plugin.addListener(this);
    plugin.addPartialBuddyListener(this);
    tab_folder.setSelection(public_item);
    init_complete = true;
    updateTable(true);
    updateTable(false);
    updatePartialBuddyTable();
}
Also used : UserPrompterResultListener(com.biglybt.ui.UserPrompterResultListener) CryptoManagerKeyListener(com.biglybt.core.security.CryptoManagerKeyListener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) LinkLabel(com.biglybt.ui.swt.components.LinkLabel) CryptoManager(com.biglybt.core.security.CryptoManager) CryptoManagerKeyListener(com.biglybt.core.security.CryptoManagerKeyListener) CryptoHandler(com.biglybt.core.security.CryptoHandler) MessageText(com.biglybt.core.internat.MessageText) ClipboardCopy(com.biglybt.ui.swt.mainwindow.ClipboardCopy) Point(org.eclipse.swt.graphics.Point) PluginConfig(com.biglybt.pif.PluginConfig) LinkLabel(com.biglybt.ui.swt.components.LinkLabel) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) UIManagerEvent(com.biglybt.pif.ui.UIManagerEvent) Clipboard(org.eclipse.swt.dnd.Clipboard)

Example 2 with CryptoManagerKeyListener

use of com.biglybt.core.security.CryptoManagerKeyListener in project BiglyBT by BiglySoftware.

the class BuddyPluginNetwork method startup.

protected boolean startup(String initial_nick, int initial_status, boolean initial_enabled) {
    try {
        List<DistributedDatabase> ddbs = plugin_interface.getUtilities().getDistributedDatabases(getDDBNetworks());
        for (DistributedDatabase ddb : ddbs) {
            if (ddb.isAvailable()) {
                DDBDetails details = new DDBDetails(ddb);
                ddb_details.add(details);
                // pick up initial values before enabling
                ddb.addListener(new DistributedDatabaseListener() {

                    @Override
                    public void event(DistributedDatabaseEvent event) {
                        if (event.getType() == DistributedDatabaseEvent.ET_LOCAL_CONTACT_CHANGED) {
                            updateIP();
                        }
                    }
                });
            }
        }
        updateIP();
        updateNickName(initial_nick);
        updateOnlineStatus(initial_status);
        COConfigurationManager.addAndFireParameterListeners(new String[] { "TCP.Listen.Port", "TCP.Listen.Port.Enable", "UDP.Listen.Port", "UDP.Listen.Port.Enable" }, new com.biglybt.core.config.ParameterListener() {

            @Override
            public void parameterChanged(String parameterName) {
                updateListenPorts();
            }
        });
        CryptoManagerFactory.getSingleton().addKeyListener(new CryptoManagerKeyListener() {

            @Override
            public void keyChanged(CryptoHandler handler) {
                for (DDBDetails details : ddb_details) {
                    details.updateKey();
                }
            }

            @Override
            public void keyLockStatusChanged(CryptoHandler handler) {
                boolean unlocked = handler.isUnlocked();
                if (unlocked) {
                    for (DDBDetails details : ddb_details) {
                        details.updatePublish();
                    }
                } else {
                    new AEThread2("BuddyPlugin:disc", true) {

                        @Override
                        public void run() {
                            for (BuddyPluginBuddy buddy : getAllBuddies()) {
                                buddy.disconnect();
                            }
                        }
                    }.start();
                }
            }
        });
        ready_to_publish = true;
        setClassicEnabledInternal(initial_enabled);
        checkBuddiesAndRepublish();
        return (true);
    } catch (Throwable e) {
        log(null, "Initialisation failed", e);
        return (false);
    }
}
Also used : CryptoHandler(com.biglybt.core.security.CryptoHandler) CryptoManagerKeyListener(com.biglybt.core.security.CryptoManagerKeyListener) com.biglybt.core.config(com.biglybt.core.config)

Aggregations

CryptoHandler (com.biglybt.core.security.CryptoHandler)2 CryptoManagerKeyListener (com.biglybt.core.security.CryptoManagerKeyListener)2 com.biglybt.core.config (com.biglybt.core.config)1 MessageText (com.biglybt.core.internat.MessageText)1 CryptoManager (com.biglybt.core.security.CryptoManager)1 PluginConfig (com.biglybt.pif.PluginConfig)1 UIInputReceiverListener (com.biglybt.pif.ui.UIInputReceiverListener)1 UIManagerEvent (com.biglybt.pif.ui.UIManagerEvent)1 UserPrompterResultListener (com.biglybt.ui.UserPrompterResultListener)1 LinkLabel (com.biglybt.ui.swt.components.LinkLabel)1 ClipboardCopy (com.biglybt.ui.swt.mainwindow.ClipboardCopy)1 Clipboard (org.eclipse.swt.dnd.Clipboard)1 TextTransfer (org.eclipse.swt.dnd.TextTransfer)1 Transfer (org.eclipse.swt.dnd.Transfer)1 Point (org.eclipse.swt.graphics.Point)1