001    /*
002     *  Copyright (C) 2003 Adam Olsen
003     *  This program is free software; you can redistribute it and/or modify
004     *  it under the terms of the GNU General Public License as published by
005     *  the Free Software Foundation; either version 1, or (at your option)
006     *  any later version.
007     *  This program is distributed in the hope that it will be useful,
008     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     *  GNU General Public License for more details.
011     *  You should have received a copy of the GNU General Public License
012     *  along with this program; if not, write to the Free Software
013     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother;
016    
017    import java.awt.BorderLayout;
018    import java.awt.GridBagConstraints;
019    import java.awt.event.ActionEvent;
020    import java.awt.event.ActionListener;
021    import java.awt.event.MouseAdapter;
022    import java.awt.event.MouseEvent;
023    import java.awt.event.WindowAdapter;
024    import java.awt.event.WindowEvent;
025    import java.io.File;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.util.*;
029    
030    import javax.swing.*;
031    import javax.swing.event.ChangeEvent;
032    import javax.swing.event.ChangeListener;
033    
034    import com.valhalla.gui.MJTextField;
035    import com.valhalla.gui.Standard;
036    import com.valhalla.jbother.jabber.BuddyStatus;
037    import com.valhalla.misc.GnuPG;
038    import com.valhalla.misc.SimpleXOR;
039    import com.valhalla.settings.Settings;
040    import com.valhalla.settings.SettingsProperties;
041    
042    /**
043     * Allows a user to edit a profile
044     *
045     * @author synic
046     * @author Andrey Zakirov
047     * @created April 10, 2004
048     */
049    public class ProfileEditorDialog extends JDialog {
050        private ResourceBundle resources = ResourceBundle.getBundle(
051                "JBotherBundle", Locale.getDefault());
052    
053        private JPanel main;
054    
055        private JTabbedPane pane = new JTabbedPane();
056    
057        private MJTextField nameField = new MJTextField();
058    
059        private MJTextField usernameField = new MJTextField(20);
060    
061        private MJTextField serverField = new MJTextField(25);
062    
063        private MJTextField resourceField = new MJTextField(20);
064    
065        private MJTextField portField = new MJTextField(5);
066    
067        private JPasswordField passwordField = new JPasswordField(20);
068    
069        private JCheckBox savePassword = new JCheckBox();
070    
071        private JCheckBox sslBox = new JCheckBox();
072    
073        private SettingsProperties settings = new SettingsProperties();
074    
075        private JCheckBox autoLoginBox = new JCheckBox();
076    
077        private JCheckBox defaultBox = new JCheckBox();
078    
079        private JCheckBox reconnectBox = new JCheckBox();
080    
081        private JButton createButton = new JButton(resources
082                .getString("createAccountButton"));
083    
084        private JButton saveButton = new JButton(resources.getString("saveButton"));
085    
086        private JButton cancelButton = new JButton(resources
087                .getString("cancelButton"));
088    
089        private File profDir = new File(JBother.settingsDir, "profiles");
090    
091        private JPanel innerPanel = new JPanel();
092    
093        private String origProf = null;
094    
095        private ProfileManager dialog = null;
096    
097        private boolean exitOnClose = false;
098    
099        private boolean isCurrentProfile = false;
100    
101        private JButton gnupgusenoneButton = new JButton(resources
102                .getString("gnupgUseNone"));
103    
104        private JButton gnupgselectButton = new JButton(resources
105                .getString("gnupgSelectKey"));
106    
107        private JLabel keyinfoLabel = new JLabel(resources
108                .getString("gnupgNoKeySelected"));
109    
110        private String gnupgSecretKeyID = null;
111    
112        private JCheckBox savepassphraseCheck = new JCheckBox();
113    
114        private JPasswordField gnupgpasswordField = new JPasswordField(15);
115    
116        private JCheckBox gnupgSignPresenceCheck = new JCheckBox();
117    
118        private String[] gnupgSecurityVariants = { "Sign and Encrypt",
119                "Encrypt Only", "Sign Only" };
120    
121        private JComboBox gnupgSecurityVariantBox = new JComboBox(
122                gnupgSecurityVariants);
123    
124        private String tempgnupgSecretKeyID = "";
125        private JFrame parent;
126    
127        private JCheckBox useProxyBox = new JCheckBox(resources
128                .getString("useProxy"));
129    
130        private MJTextField proxyHostBox = new MJTextField(15);
131    
132        private MJTextField proxyPortBox = new MJTextField(15);
133    
134        /**
135         * Contructs the ProfileEditorDialog
136         *
137         * @param dialog
138         *            the ProfileManager dialog that's calling this editor, or
139         *            <tt>null</tt> if nothing is calling it
140         * @param profile
141         *            the profile to edit, or <tt>null</tt> if it's a new profile
142         */
143        public ProfileEditorDialog(JFrame parent,ProfileManager dialog, String profile) {
144            super(parent, "", true);
145            this.parent = parent;
146            this.dialog = dialog;
147            setModal(false);
148            setTitle(resources.getString("profileEditor"));
149            origProf = profile;
150    
151            main = (JPanel) getContentPane();
152            main.setLayout(new BorderLayout());
153            main.setBorder(BorderFactory.createTitledBorder(resources
154                    .getString("profileEditor")));
155            main.add(pane, BorderLayout.CENTER);
156    
157            pane.addChangeListener(new ChangeListener() {
158                public void stateChanged(ChangeEvent e) {
159                    if (pane.getSelectedIndex() == 2) {
160                        if (!JBotherLoader.isGPGEnabled()) {
161                            if (Settings.getInstance().getProperty(
162                                    "gnupgSecretKeyID") != null) {
163                                int result = JOptionPane
164                                        .showConfirmDialog(
165                                                ProfileEditorDialog.this,
166                                                "Warning: There is a GnuPG secrety key ID in your profile,\nbut it appears as though GnuPG is not installed on this system.\nWould you like to remove the ID from your profile?",
167                                                "GnuPG", JOptionPane.YES_NO_OPTION);
168    
169                                if (result == JOptionPane.YES_OPTION) {
170                                    Settings.getInstance().remove(
171                                            "gnupgSecretKeyID");
172                                    Settings.getInstance()
173                                            .remove("gnupgPassPhrase");
174                                    Settings.getInstance().remove(
175                                            "gnupgSavePassphrase");
176                                }
177                            }
178    
179                            Standard
180                                    .warningMessage(ProfileEditorDialog.this, "GnuPG Error",
181                                            "GnuPG is not executable or sends unknown response.  GnuPG is disabled");
182    
183                            pane.setSelectedIndex(0);
184                        }
185                    }
186                }
187            });
188    
189            pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
190    
191            JPanel topPanel = new JPanel();
192            topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
193    
194            JPanel namePanel = new JPanel();
195            namePanel.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
196            namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS));
197            namePanel.add(new JLabel(resources.getString("profileName") + ": "));
198            namePanel.add(nameField);
199    
200            topPanel.add(namePanel);
201    
202            JPanel defaultPanel = new JPanel();
203            defaultPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
204            defaultPanel.setLayout(new BoxLayout(defaultPanel, BoxLayout.X_AXIS));
205            defaultPanel
206                    .add(new JLabel(resources.getString("setAsDefault") + ": "));
207            defaultPanel.add(defaultBox);
208            defaultPanel.add(Box.createHorizontalGlue());
209    
210            topPanel.add(defaultPanel);
211    
212            main.add(topPanel, BorderLayout.NORTH);
213    
214            createAccountPanel();
215            createOptionsPanel();
216            createGnuPGPanel();
217            createProxyPanel();
218    
219            JPanel buttonPanel = new JPanel();
220            buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
221            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
222            buttonPanel.add(Box.createHorizontalGlue());
223            buttonPanel.add(cancelButton);
224            buttonPanel.add(saveButton);
225    
226            main.add(buttonPanel, BorderLayout.SOUTH);
227    
228            addListeners();
229            loadProfile(profile);
230    
231            pack();
232            setLocationRelativeTo(null);
233            addWindowListener(new WindowAdapter() {
234                public void windowClosing(WindowEvent e) {
235                    cancelHandler();
236                }
237            });
238        }
239    
240        protected JFrame getDialogParent() { return parent; }
241    
242        /**
243         * Sets whether or not this dialog should exit the application when it's
244         * cancel button has been pressed
245         *
246         * @param e
247         *            true to close the app
248         */
249        protected void setExitOnClose(boolean e) {
250            this.exitOnClose = true;
251        }
252    
253        /**
254         * Sets the isCurrentProfile attribute of the ProfileEditorDialog object
255         *
256         * @param i
257         *            The new isCurrentProfile value
258         */
259        public void setIsCurrentProfile(boolean i) {
260            this.isCurrentProfile = i;
261        }
262    
263        /**
264         * @return the defaultBox
265         */
266        protected JCheckBox getDefaultBox() {
267            return defaultBox;
268        }
269    
270        /**
271         * Adds the event listeners to the buttons
272         */
273        private void addListeners() {
274            PEDialogListener listener = new PEDialogListener();
275            createButton.addActionListener(listener);
276            saveButton.addActionListener(listener);
277            cancelButton.addActionListener(listener);
278            gnupgusenoneButton.addActionListener(listener);
279            gnupgselectButton.addActionListener(listener);
280            useProxyBox.addActionListener(listener);
281    
282        }
283    
284        /**
285         * Handles events
286         *
287         * @author synic
288         * @created November 30, 2004
289         */
290        class PEDialogListener implements ActionListener {
291            /**
292             * Description of the Method
293             *
294             * @param e
295             *            Description of the Parameter
296             */
297            public void actionPerformed(ActionEvent e) {
298                if (e.getSource() == createButton) {
299                    createAccountHandler();
300                } else if (e.getSource() == saveButton) {
301                    saveHandler();
302                } else if (e.getSource() == cancelButton) {
303                    cancelHandler();
304                } else if (e.getSource() == gnupgusenoneButton) {
305                    gnupgusenoneHandler();
306                } else if (e.getSource() == gnupgselectButton) {
307                    gnupgselectHandler();
308                } else if (e.getSource() == useProxyBox) {
309                    useProxyBoxHandler();
310                }
311            }
312        }
313    
314        /**
315         * Cancels the dialog, and quits if exitOnClose is set to true
316         */
317        private void cancelHandler() {
318            dispose();
319            if (exitOnClose) {
320                System.exit(0);
321            }
322        }
323    
324        /**
325         * Description of the Method
326         */
327        private void gnupgusenoneHandler() {
328            keyinfoLabel.setText(resources.getString("gnupgNoKeySelected"));
329            settings.remove("gnupgPassPhrase");
330            this.gnupgSecretKeyID = null;
331            savepassphraseCheck.setSelected(false);
332            savepassphraseCheck.setEnabled(false);
333            gnupgpasswordField.setText("");
334            gnupgpasswordField.setEnabled(false);
335            gnupgusenoneButton.setEnabled(false);
336            gnupgSignPresenceCheck.setSelected(false);
337            gnupgSignPresenceCheck.setEnabled(false);
338            gnupgSecurityVariantBox.setEnabled(false);
339            BuddyList.getInstance().setGnuPGPassword(null);
340    
341            if (BuddyList.getInstance().checkConnection()) {
342                settings.setProperty("gnupgPassPhrase", null);
343            }
344    
345        }
346    
347        /**
348         * Description of the Method
349         */
350        private void gnupgselectHandler() {
351            KeySelectDialog dialog = new KeySelectDialog((JDialog) this,
352                    "sec");
353            dialog.showDialog();
354            if ((dialog.getName() != null) && (dialog.getID() != null)) {
355                keyinfoLabel.setText(dialog.getName());
356                this.gnupgSecretKeyID = dialog.getID();
357                savepassphraseCheck.setEnabled(true);
358                gnupgpasswordField.setText("");
359                gnupgpasswordField.setEnabled(savepassphraseCheck.isSelected());
360                gnupgusenoneButton.setEnabled(true);
361                gnupgSignPresenceCheck.setSelected(true);
362                gnupgSignPresenceCheck.setEnabled(true);
363                gnupgSecurityVariantBox.setEnabled(true);
364            }
365        }
366    
367        /**
368         * Saves the currently opened profile
369         */
370        private void saveHandler() {
371            try {
372                Standard.setBundle(resources);
373                Standard.assure(nameField.getText(), "Profile Name");
374                Standard.assure(usernameField.getText(), "Username");
375    
376                if (savePassword.isSelected()) {
377                    Standard.assure(new String(passwordField.getPassword()),
378                            "Password");
379                }
380    
381                Standard.assure(resourceField.getText(), "Resource");
382                Standard.assure(serverField.getText(), "Server");
383            } catch (Exception e) {
384                com.valhalla.Logger.logException(e);
385                return;
386            }
387    
388            settings.setProperty("username", usernameField.getText());
389    
390            if (savePassword.isSelected()) {
391                settings.setProperty("password", SimpleXOR.encrypt(new String(
392                        passwordField.getPassword()), "JBother rules!"));
393            } else
394                settings.remove("password");
395    
396            settings.setProperty("resource", resourceField.getText());
397            settings.setProperty("defaultServer", serverField.getText());
398            settings.setProperty("port", portField.getText());
399            settings.setBoolean("useSSL", sslBox.isSelected());
400            settings.setBoolean("autoLogin", autoLoginBox.isSelected());
401            settings.setBoolean("reconnectOnDisconnect", reconnectBox.isSelected());
402            if (gnupgSecretKeyID != null) {
403                String gnupgTempPass = null;
404                GnuPG gnupg = new GnuPG();
405                boolean check = true;
406                if (BuddyList.getInstance().checkConnection()
407                        && !savepassphraseCheck.isSelected()
408                        && gnupgSecretKeyID.matches(tempgnupgSecretKeyID) == false) {
409                    PasswordDialog d = new PasswordDialog(this,resources
410                            .getString("gnupgKeyPassword"));
411                    gnupgTempPass = d.getText();
412                } else if (savepassphraseCheck.isSelected()) {
413                    gnupgTempPass = new String(gnupgpasswordField.getPassword());
414                } else {
415                    check = false;
416                }
417    
418                if (!check
419                        || ((gnupgTempPass != null) && (gnupg.sign("1",
420                                gnupgSecretKeyID, gnupgTempPass)))) {
421                    BuddyList.getInstance().setGnuPGPassword(gnupgTempPass);
422                } else {
423                    BuddyList.getInstance().setGnuPGPassword(null);
424                    Standard
425                            .warningMessage(null, "GnuPG Error",
426                                    "Wrong GnuPG passphrase! Please, try entering it again again.");
427                    return;
428                }
429    
430                Hashtable buddyStatuses = BuddyList.getInstance()
431                        .getBuddyStatuses();
432                if (buddyStatuses != null) {
433                    Iterator iterator = buddyStatuses.keySet().iterator();
434                    while (iterator.hasNext()) {
435                        String user = (String) iterator.next();
436                        BuddyStatus buddy = (BuddyStatus) buddyStatuses.get(user);
437                        if (buddy.getConversation() != null
438                                && buddy.getConversation() instanceof ChatPanel) {
439                            ((ChatPanel) buddy.getConversation()).enableEncrypt();
440                        }
441                    }
442                }
443                if (gnupgSignPresenceCheck.isSelected()) {
444                    settings.setBoolean("gnupgSignPresence", true);
445                } else {
446                    settings.remove("gnupgSignPresence");
447                }
448                int variant = gnupgSecurityVariantBox.getSelectedIndex();
449                settings.setProperty("gnupgSecurityVariant", String
450                        .valueOf(variant));
451                settings.setProperty("gnupgSecretKeyID", gnupgSecretKeyID);
452                if (savepassphraseCheck.isSelected()) {
453                    String pass = new String(gnupgpasswordField.getPassword());
454                    if (!pass.equals("")) {
455                        settings.setBoolean("gnupgSavePassphrase", true);
456                        settings.setProperty("gnupgPassPhrase", SimpleXOR.encrypt(
457                                pass, "86753099672539"));
458                    } else {
459                        Standard.warningMessage(this, "GnuPG", resources
460                                .getString("gnupgEnterPassphrase"));
461                        return;
462                    }
463                } else {
464                    settings.setBoolean("gnupgSavePassphrase", false);
465                    settings.remove("gnupgPassPhrase");
466                }
467            } else {
468                settings.remove("gnupgSecretKeyID");
469            }
470    
471            settings.setBoolean("useProxy", useProxyBox.isSelected());
472            settings.setProperty("proxyHost", proxyHostBox.getText().trim());
473            settings.setProperty("proxyPort", proxyPortBox.getText().trim());
474            // this sould make sure that new settings are activated
475            // when user presses 'Save' button
476            Properties sysProperties = System.getProperties();
477            sysProperties.put("proxySet",settings.getBoolean("useProxy") ? "true" : "false");
478            sysProperties.put("proxyHost",settings.getProperty("proxyHost"));
479            sysProperties.put("proxyPort",settings.getProperty("proxyPort"));
480    
481            String profile = nameField.getText();
482            File profileDir = new File(profDir, profile);
483    
484            if (origProf == null) {
485                // check to see if the profile already exists
486                if (profileDir.exists()) {
487                    Standard.warningMessage(this, resources
488                            .getString("profileEditor"), resources
489                            .getString("profileExists"));
490                    return;
491                }
492    
493                profileDir.mkdirs();
494            } else if (!profile.equals(origProf)) {
495                File origProfDir = new File(profDir, origProf);
496                origProfDir.renameTo(profileDir);
497            }
498    
499            try {
500                settings.saveSettings(profDir.getPath() + File.separatorChar
501                        + profile + File.separatorChar + "settings.properties",
502                        "JBother Settings File");
503            } catch (IOException ex) {
504                Standard.warningMessage(this, resources.getString("profileEditor"),
505                        resources.getString("errorSavingSettings"));
506                return;
507            }
508    
509            if (defaultBox.isSelected()) {
510                ProfileManager.setDefaultProfile(profile);
511            }
512    
513            if (dialog != null) {
514                dialog.loadProfileList();
515            }
516    
517            if (isCurrentProfile) {
518                ProfileManager.setCurrentProfile(profile);
519                Settings.loadSettings(profDir.getPath() + File.separatorChar
520                        + profile, "settings.properties");
521                ConnectorThread.getInstance().resetCredentials();
522            }
523    
524            if (exitOnClose) {
525                ProfileManager.loadProfile(nameField.getText());
526            }
527    
528            dispose();
529        }
530    
531        /**
532         * Calls the NewAccoutDialog to create a new account
533         */
534        private void createAccountHandler() {
535            if (serverField.getText().equals("")) {
536                Standard.warningMessage(this, "createAccount", resources
537                        .getString("enterNewAccountServer"));
538                return;
539            }
540    
541            int port = -1;
542            boolean ssl = sslBox.isSelected();
543    
544            try {
545                port = Integer.parseInt(portField.getText());
546            } catch (NumberFormatException e) {
547            }
548    
549            if (port == -1) {
550                if (ssl) {
551                    port = 5223;
552                } else {
553                    port = 5222;
554                }
555            }
556    
557    
558            String server = JOptionPane.showInputDialog(resources.getString("enterNewAccountServer"));
559            if( server == null || server.equals("")) return;
560    
561            NewAccountDialog dialog = new NewAccountDialog(this, server,
562                usernameField.getText(), new String(passwordField
563                    .getPassword()), port, ssl);
564            dialog.getRegistrationInfo();
565        }
566    
567        /**
568         * makes sure that
569         */
570        private void useProxyBoxHandler() {
571            if(useProxyBox.isSelected())
572            {
573                proxyHostBox.setEditable(true);
574                proxyPortBox.setEditable(true);
575            } else
576            {
577                proxyHostBox.setEditable(false);
578                proxyPortBox.setEditable(false);
579            }
580        }
581    
582        /**
583         * Sets the username
584         *
585         * @param username
586         *            The new username value
587         */
588        public void setUsername(String username) {
589            usernameField.setText(username);
590        }
591    
592        /**
593         * Sets the password
594         *
595         * @param password
596         *            The new password value
597         */
598        public void setPassword(String password) {
599            passwordField.setText(password);
600        }
601    
602        public void setServer(String server)
603        {
604            serverField.setText(server);
605        }
606    
607        /**
608         * Creates the Account Panel
609         */
610        private void createAccountPanel() {
611            savePassword.addActionListener(new ActionListener() {
612                public void actionPerformed(ActionEvent e) {
613                    String text = new String(passwordField.getPassword());
614                    passwordField.setEnabled(savePassword.isSelected());
615                    if (!savePassword.isSelected()) {
616                        passwordField.setText("");
617                    }
618                    else passwordField.setText(text);
619                }
620            });
621    
622            AbstractOptionPanel panel = new AbstractOptionPanel();
623            panel.addComponent(resources.getString("username"), usernameField);
624            passwordField.setFont(usernameField.getFont());
625            panel.addComponent(resources.getString("savePassword"), savePassword);
626            panel.addComponent(resources.getString("password"), passwordField);
627            panel.addComponent(resources.getString("resource"), resourceField);
628            panel.addComponent(resources.getString("server"), serverField);
629            panel.addComponent(createButton, 1, GridBagConstraints.EAST);
630            panel.end();
631    
632            pane.add(resources.getString("account"), panel);
633        }
634    
635        /**
636         * Creates the options panel
637         */
638        private void createOptionsPanel() {
639            AbstractOptionPanel panel = new AbstractOptionPanel();
640            panel.addComponent(resources.getString("useSsl"), sslBox);
641    
642            JPanel portPanel = new JPanel(new BorderLayout());
643            portPanel.add(portField, BorderLayout.WEST);
644            portPanel.add(new JLabel(" "
645                    + resources.getString("leaveBlankForDefault")),
646                    BorderLayout.CENTER);
647            panel.addComponent(resources.getString("logInAutomatically"),
648                    autoLoginBox);
649            panel.addComponent(resources.getString("reconnectOnDisconnect"),
650                    reconnectBox);
651            panel.addComponent(resources.getString("connectPort"), portPanel);
652            panel.end();
653    
654            pane.add(resources.getString("options"), panel);
655        }
656    
657        /**
658         * Creates proxy panel
659         */
660        private void createProxyPanel() {
661            AbstractOptionPanel panel = new AbstractOptionPanel();
662            panel.addComponent( resources.getString("useNetworkProxy"),
663                useProxyBox );
664            panel.addComponent( resources.getString("proxyHost"),
665                proxyHostBox );
666            panel.addComponent( resources.getString("proxyPort"),
667                proxyPortBox );
668            panel.end();
669    
670            pane.add(resources.getString("proxy"), panel);
671        }
672    
673        /**
674         * Creates the GnuPG Panel
675         */
676    
677        private void createGnuPGPanel() {
678            JPanel main = new JPanel(new BorderLayout());
679    
680            AbstractOptionPanel panel = new AbstractOptionPanel();
681            panel.addComponent(resources.getString("gnupgSecretKey"), keyinfoLabel);
682            panel.addComponent(resources.getString("gnupgSavePassphrase"),
683                    savepassphraseCheck);
684            panel.addComponent(resources.getString("gnupgPassphrase"),
685                    gnupgpasswordField);
686            panel.addComponent("Sign Presence", gnupgSignPresenceCheck);
687            panel.addComponent("Security Variant", gnupgSecurityVariantBox);
688    
689            savepassphraseCheck.addMouseListener(new MouseAdapter() {
690                public void mouseClicked(MouseEvent e) {
691                    gnupgpasswordField.setEnabled(savepassphraseCheck.isSelected());
692    
693                    if (savepassphraseCheck.isSelected()) {
694                        if (savepassphraseCheck.isEnabled()) {
695                            int result = JOptionPane.showConfirmDialog(null,
696                                    resources.getString("gnupgInsecure"), "GnuPG",
697                                    JOptionPane.YES_NO_OPTION);
698    
699                            if (result != JOptionPane.YES_OPTION) {
700                                savepassphraseCheck.setSelected(false);
701                            }
702                        } else {
703                            gnupgpasswordField.setText("");
704                        }
705                        gnupgpasswordField.setEnabled(savepassphraseCheck
706                                .isSelected());
707                    } else {
708                        gnupgpasswordField.setText("");
709                    }
710                }
711            });
712    
713            JPanel buttons = new JPanel();
714            buttons.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
715            buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
716            buttons.add(Box.createHorizontalGlue());
717            buttons.add(gnupgselectButton);
718            buttons.add(gnupgusenoneButton);
719            panel.end();
720    
721            main.add(panel, BorderLayout.CENTER);
722            main.add(buttons, BorderLayout.SOUTH);
723    
724            pane.add("GnuPG", main);
725    
726        }
727    
728        /**
729         * Loads a certain profile
730         *
731         * @param profile
732         *            the profile to load, or null to create a new one
733         */
734        private void loadProfile(String profile) {
735            if (profile != null) {
736                nameField.setText(profile);
737    
738                try {
739                    settings.loadSettings(profDir.getPath() + File.separatorChar
740                            + profile + File.separatorChar + "settings.properties");
741                } catch (Exception ex) {
742                    return;
743                }
744            } else {
745                // copy the default file in to place
746                InputStream stream = getClass().getClassLoader()
747                        .getResourceAsStream("defaultsettings.properties");
748                try {
749                    settings.load(stream);
750                } catch (Exception ex) {
751                    return;
752                }
753            }
754    
755            usernameField.setText(settings.getProperty("username"));
756    
757            String pass = settings.getProperty("password");
758    
759            savePassword.setSelected(pass != null);
760            passwordField.setEnabled(savePassword.isSelected());
761            if (savePassword.isSelected()) {
762                passwordField.setText(SimpleXOR.decrypt(settings
763                        .getProperty("password"), "JBother rules!"));
764            }
765    
766            resourceField.setText(settings.getProperty("resource"));
767            serverField.setText(settings.getProperty("defaultServer"));
768            portField.setText(settings.getProperty("port"));
769    
770            sslBox.setSelected(settings.getBoolean("useSSL"));
771    
772            autoLoginBox.setSelected(settings.getBoolean("autoLogin"));
773            reconnectBox.setSelected(settings.getBoolean("reconnectOnDisconnect"));
774    
775            // proxy
776            useProxyBox.setSelected(settings.getBoolean("useProxy"));
777            proxyHostBox.setEditable(useProxyBox.isSelected());
778            proxyPortBox.setEditable(useProxyBox.isSelected());
779            proxyHostBox.setText(settings.getProperty("proxyHost"));
780            proxyPortBox.setText(settings.getProperty("proxyPort"));
781    
782    
783            String[] entries;
784            String name;
785            String id;
786            GnuPG gnupg = new GnuPG();
787            id = settings.getProperty("gnupgSecretKeyID");
788            if (id != null) {
789                if (gnupg.listSecretKeys(id)) {
790                    entries = gnupg.getResult().split("\n");
791                    for (int i = 0; i < entries.length; i++) {
792                        name = entries[i]
793                                .replaceAll(
794                                        "^sec:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:([^:]*):[^:]*:[^:]*:[^:]*$",
795                                        "$1");
796                        if (!name.equals(entries[i])) {
797                            keyinfoLabel.setText(name);
798                            gnupgSecretKeyID = id;
799                            if (id != null) {
800                                tempgnupgSecretKeyID = id;
801                            }
802                        }
803                    }
804                }
805    
806                if (settings.getProperty("gnupgPassPhrase") != null) {
807                    gnupgpasswordField.setText(SimpleXOR.decrypt(settings
808                            .getProperty("gnupgPassPhrase"), "86753099672539"));
809                    savepassphraseCheck.setSelected(true);
810                }
811    
812                gnupgSignPresenceCheck.setSelected(settings
813                        .getBoolean("gnupgSignPresence"));
814                gnupgSignPresenceCheck.setEnabled(true);
815    
816                if (settings.getProperty("gnupgSecurityVariant") != null) {
817                    int variant;
818                    String vs = settings.getProperty("gnupgSecurityVariant");
819                    if (vs != null) {
820                        if (vs.matches("0") == false && vs.matches("1") == false
821                                && vs.matches("2") == false) {
822                            variant = 0;
823                        } else {
824                            variant = Integer.parseInt(vs);
825                        }
826                    } else {
827                        variant = 0;
828                    }
829                    gnupgSecurityVariantBox.setSelectedIndex(variant);
830                    gnupgSecurityVariantBox.setEnabled(true);
831                }
832            } else {
833                gnupgSecurityVariantBox.setEnabled(false);
834                gnupgpasswordField.setText("");
835                gnupgpasswordField.setEnabled(false);
836                savepassphraseCheck.setSelected(false);
837                savepassphraseCheck.setEnabled(false);
838                gnupgSignPresenceCheck.setEnabled(false);
839            }
840    
841            String defaultProf = ProfileManager.getDefaultProfile();
842            com.valhalla.Logger.debug(defaultProf);
843    
844            if (defaultProf != null && defaultProf.equals(profile)) {
845                defaultBox.setSelected(true);
846            }
847        }
848    }
849