logindialog.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 1,189 行 · 第 1/4 页

SVN-BASE
1,189
字号
            if (savePasswordBox.isSelected()) {
                String encodedPassword = null;
                try {
                    encodedPassword = Encryptor.encrypt(getPassword());
                    localPref.setPassword(encodedPassword);
                }
                catch (Exception e) {
                    Log.error("Error encrypting password.", e);
                }
            }
            else {
                localPref.setPassword("");
            }

            localPref.setSavePassword(savePasswordBox.isSelected());
            localPref.setAutoLogin(autoLoginBox.isSelected());

            if (localPref.isSSOEnabled()) {
                localPref.setAutoLogin(true);
            }

            localPref.setServer(serverField.getText());


            SettingsManager.saveSettings();

            return !hasErrors;
        }
    }

    /**
     * If the user quits, just shut down the
     * application.
     */
    private void quitLogin() {
        System.exit(1);
    }

    /**
     * Initializes Spark and initializes all plugins.
     */
    private void startSpark() {
        // Invoke the MainWindow.
        final MainWindow mainWindow = MainWindow.getInstance();

        /*
        if (tray != null) {
            // Remove trayIcon
            tray.removeTrayIcon(trayIcon);
        }
        */
        // Creates the Spark  Workspace and add to MainWindow
        Workspace workspace = Workspace.getInstance();

        LayoutSettings settings = LayoutSettingsManager.getLayoutSettings();
        int x = settings.getMainWindowX();
        int y = settings.getMainWindowY();
        int width = settings.getMainWindowWidth();
        int height = settings.getMainWindowHeight();

        LocalPreferences pref = SettingsManager.getLocalPreferences();
        if (pref.isDockingEnabled()) {
            JSplitPane splitPane = mainWindow.getSplitPane();
            workspace.getCardPanel().setMinimumSize(null);
            splitPane.setLeftComponent(workspace.getCardPanel());
            SparkManager.getChatManager().getChatContainer().setMinimumSize(null);
            splitPane.setRightComponent(SparkManager.getChatManager().getChatContainer());
            int dividerLoc = settings.getSplitPaneDividerLocation();
            if (dividerLoc != -1) {
                mainWindow.getSplitPane().setDividerLocation(dividerLoc);
            }
            else {
                mainWindow.getSplitPane().setDividerLocation(240);
            }

            mainWindow.getContentPane().add(splitPane, BorderLayout.CENTER);
        }
        else {
            mainWindow.getContentPane().add(workspace.getCardPanel(), BorderLayout.CENTER);
        }

        if (x == 0 && y == 0) {
            // Use Default size
            mainWindow.setSize(310, 520);

            // Center Window on Screen
            GraphicUtils.centerWindowOnScreen(mainWindow);
        }
        else {
            mainWindow.setBounds(x, y, width, height);
        }

        if (loginDialog.isVisible()) {
            mainWindow.setVisible(true);
        }

        loginDialog.setVisible(false);

        // Build the layout in the workspace
        workspace.buildLayout();
    }

    /**
     * Updates System properties with Proxy configuration.
     *
     * @throws Exception thrown if an exception occurs.
     */
    private void updateProxyConfig() throws Exception {
        if (ModelUtil.hasLength(Default.getString(Default.PROXY_PORT)) && ModelUtil.hasLength(Default.getString(Default.PROXY_HOST))) {
            String port = Default.getString(Default.PROXY_PORT);
            String host = Default.getString(Default.PROXY_HOST);
            System.setProperty("socksProxyHost", host);
            System.setProperty("socksProxyPort", port);
            return;
        }

        boolean proxyEnabled = localPref.isProxyEnabled();
        if (proxyEnabled) {
            String host = localPref.getHost();
            String port = localPref.getPort();
            String username = localPref.getProxyUsername();
            String password = localPref.getProxyPassword();
            String protocol = localPref.getProtocol();

            if (protocol.equals("SOCKS")) {
                System.setProperty("socksProxyHost", host);
                System.setProperty("socksProxyPort", port);

                if (ModelUtil.hasLength(username) && ModelUtil.hasLength(password)) {
                    System.setProperty("java.net.socks.username", username);
                    System.setProperty("java.net.socks.password", password);
                }
            }
            else {
                System.setProperty("http.proxyHost", host);
                System.setProperty("http.proxyPort", port);
                System.setProperty("https.proxyHost", host);
                System.setProperty("https.proxyPort", port);


                if (ModelUtil.hasLength(username) && ModelUtil.hasLength(password)) {
                    System.setProperty("http.proxyUser", username);
                    System.setProperty("http.proxyPassword", password);
                }

            }
        }
    }

    /**
     * Defines the background to use with the Login panel.
     */
    public class LoginBackgroundPanel extends JPanel {
        final ImageIcon icons = Default.getImageIcon(Default.LOGIN_DIALOG_BACKGROUND_IMAGE);

        /**
         * Empty constructor.
         */
        public LoginBackgroundPanel() {

        }

        /**
         * Uses an image to paint on background.
         *
         * @param g the graphics.
         */
        public void paintComponent(Graphics g) {
            Image backgroundImage = icons.getImage();
            double scaleX = getWidth() / (double)backgroundImage.getWidth(null);
            double scaleY = getHeight() / (double)backgroundImage.getHeight(null);
            AffineTransform xform = AffineTransform.getScaleInstance(scaleX, scaleY);
            ((Graphics2D)g).drawImage(backgroundImage, xform, this);
        }
    }

    /**
     * The image panel to display the Spark Logo.
     */
    public class ImagePanel extends JPanel {

        private final ImageIcon icons = Default.getImageIcon(Default.MAIN_IMAGE);

        /**
         * Uses the Spark logo to paint as the background.
         *
         * @param g the graphics to use.
         */
        public void paintComponent(Graphics g) {
            final Image backgroundImage = icons.getImage();
            final double scaleX = getWidth() / (double)backgroundImage.getWidth(null);
            final double scaleY = getHeight() / (double)backgroundImage.getHeight(null);
            AffineTransform xform = AffineTransform.getScaleInstance(scaleX, scaleY);
            ((Graphics2D)g).drawImage(backgroundImage, xform, this);
        }

        public Dimension getPreferredSize() {
            final Dimension size = super.getPreferredSize();
            size.width = icons.getIconWidth();
            size.height = icons.getIconHeight();
            return size;
        }
    }

    /**
     * Checks for historic Spark settings and upgrades the user.
     *
     * @throws Exception thrown if an error occurs.
     */
    private void checkForOldSettings() throws Exception {
        // Check for old settings.xml
        File settingsXML = new File(Spark.getSparkUserHome(), "/settings.xml");
        if (settingsXML.exists()) {
            SAXReader saxReader = new SAXReader();
            Document pluginXML = null;
            try {
                pluginXML = saxReader.read(settingsXML);
            }
            catch (DocumentException e) {
                Log.error(e);
                return;
            }

            List plugins = pluginXML.selectNodes("/settings");
            Iterator iter = plugins.iterator();
            while (iter.hasNext()) {
                Element plugin = (Element)iter.next();

                String password = plugin.selectSingleNode("password").getText();
                localPref.setPassword(password);

                String username = plugin.selectSingleNode("username").getText();
                localPref.setUsername(username);

                String server = plugin.selectSingleNode("server").getText();
                localPref.setServer(server);

                String autoLogin = plugin.selectSingleNode("autoLogin").getText();
                localPref.setAutoLogin(Boolean.parseBoolean(autoLogin));

                String savePassword = plugin.selectSingleNode("savePassword").getText();
                localPref.setSavePassword(Boolean.parseBoolean(savePassword));
            }

            // Delete settings File
            settingsXML.delete();
        }
    }

    /**
     * Use DNS to lookup a KDC
     * @param realm The realm to look up
     * @return the KDC hostname
     */
    private String getDnsKdc(String realm) {
        //Assumption: the KDC will be found with the SRV record
        // _kerberos._udp.$realm
        try {
            Hashtable env= new Hashtable();
            env.put("java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory");
            DirContext context = new InitialDirContext(env);
            Attributes dnsLookup = context.getAttributes("_kerberos._udp."+realm, new String[]{"SRV"});
    
            ArrayList<Integer> priorities = new ArrayList<Integer>();
            HashMap<Integer,List> records = new HashMap<Integer,List>();
            for (Enumeration e = dnsLookup.getAll() ; e.hasMoreElements() ; ) {
                Attribute record = (Attribute)e.nextElement();
                for (Enumeration e2 = record.getAll() ; e2.hasMoreElements() ; ) {
                    String sRecord = (String)e2.nextElement();
                    String [] sRecParts = sRecord.split(" ");
                    Integer pri = new Integer(sRecParts[0]);
                    if(priorities.contains(pri)) {
                        List recs = records.get(pri);
                        if(recs == null) recs = new ArrayList<String>();
                        recs.add(sRecord);
                    } else {
                        priorities.add(pri);
                        List recs = new ArrayList<String>();
                        recs.add(sRecord);
                        records.put(pri,recs);
                    }
                }
            }
            Collections.sort(priorities);
            List l = records.get(priorities.get(0));
            String toprec = (String)l.get(0);
            String [] sRecParts = toprec.split(" ");
            return sRecParts[3];
        } catch (NamingException e){
            return "";
        }
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?