aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/otr/authdialog/SmpAuthenticateBuddyDialog.java
blob: 6f72ce8d02dffd21027b4730049e121445b3d24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.otr.authdialog;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import net.java.otr4j.session.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.plugin.otr.*;
import net.java.sip.communicator.plugin.otr.OtrContactManager.OtrContact;

/**
 * The dialog that pops up when the remote party send us SMP
 * request. It contains detailed information for the user about
 * the authentication process and allows him to authenticate.
 * 
 * @author Marin Dzhigarov
 */
@SuppressWarnings("serial")
public class SmpAuthenticateBuddyDialog
    extends SIPCommDialog
{
    private final OtrContact otrContact;

    private final String question;

    private final InstanceTag receiverTag;

    public SmpAuthenticateBuddyDialog(
        OtrContact contact, InstanceTag receiverTag, String question)
    {
        this.otrContact = contact;
        this.receiverTag = receiverTag;
        this.question = question;
        initComponents();
    }

    private void initComponents()
    {
        this.setTitle(
            OtrActivator.resourceService
                .getI18NString(
                    "plugin.otr.authbuddydialog.TITLE"));

        // The main panel that contains all components.
        JPanel mainPanel = new TransparentPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(
            BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.setPreferredSize(new Dimension(300, 350));

        // Add "authentication from contact" to the main panel.
        JTextArea authenticationFrom = new CustomTextArea();
        Font newFont =
            new Font(
                UIManager.getDefaults().getFont("TextArea.font").
                    getFontName()
                , Font.BOLD
                , 14);
        authenticationFrom.setFont(newFont);

        String resourceName = otrContact.resource != null ?
            "/" + otrContact.resource.getResourceName() : "";
        String authFromText =
            String.format(
                OtrActivator.resourceService
                    .getI18NString(
                        "plugin.otr.authbuddydialog.AUTHENTICATION_FROM",
                        new String[]
                            {otrContact.contact.getDisplayName() +
                            resourceName}));
        authenticationFrom.setText(authFromText);
        mainPanel.add(authenticationFrom);

        // Add "general info" text to the main panel.
        JTextArea generalInfo = new CustomTextArea();
        generalInfo.setText(OtrActivator.resourceService
            .getI18NString(
                "plugin.otr.authbuddydialog.AUTHENTICATION_INFO"));
        mainPanel.add(generalInfo);

        // Add "authentication-by-secret" info text to the main panel.
        JTextArea authBySecretInfo = new CustomTextArea();
        newFont =
            new Font(
                UIManager.getDefaults().getFont("TextArea.font").
                    getFontName()
                , Font.ITALIC
                , 10);
        authBySecretInfo.setText(OtrActivator.resourceService
            .getI18NString(
                "plugin.otr.authbuddydialog.AUTH_BY_SECRET_INFO_RESPOND"));
        authBySecretInfo.setFont(newFont);
        mainPanel.add(authBySecretInfo);

        // Create a panel to add question/answer related components
        JPanel questionAnswerPanel = new JPanel(new GridBagLayout());
        questionAnswerPanel.setBorder(BorderFactory.createEtchedBorder());

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(5, 5, 0, 5);
        c.weightx = 0;

        // Add question label.
        JLabel questionLabel =
            new JLabel(
                OtrActivator.resourceService
                    .getI18NString(
                        "plugin.otr.authbuddydialog.QUESTION_RESPOND"));
        questionAnswerPanel.add(questionLabel, c);

        // Add the question.
        c.insets = new Insets(0, 5, 5, 5);
        c.gridy = 1;
        JTextArea questionArea = 
            new CustomTextArea();
        newFont =
            new Font(
                UIManager.getDefaults().getFont("TextArea.font").
                    getFontName()
                , Font.BOLD
                , UIManager.getDefaults().getFont("TextArea.font")
                    .getSize());
        questionArea.setFont(newFont);
        questionArea.setText(question);
        questionAnswerPanel.add(questionArea, c);

        // Add answer label.
        c.insets = new Insets(5, 5, 5, 5);
        c.gridy = 2;
        JLabel answerLabel =
            new JLabel(OtrActivator.resourceService
                .getI18NString("plugin.otr.authbuddydialog.ANSWER"));
        questionAnswerPanel.add(answerLabel, c);

        // Add the answer text field.
        c.gridy = 3;
        final JTextField answerTextBox = new JTextField();
        questionAnswerPanel.add(answerTextBox, c);

        // Add the question/answer panel to the main panel.
        mainPanel.add(questionAnswerPanel);

        // Buttons panel.
        JPanel buttonPanel = new TransparentPanel(new GridBagLayout());
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        JButton helpButton =
            new JButton(OtrActivator.resourceService
                .getI18NString("plugin.otr.authbuddydialog.HELP"));
        helpButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                OtrActivator.scOtrEngine.launchHelp();
            }
        });

        c.gridwidth = 1;
        c.gridy = 0;
        c.gridx = 0;
        c.weightx = 0;
        c.insets = new Insets(5, 5, 5, 20);
        buttonPanel.add(helpButton, c);

        JButton cancelButton =
            new JButton(OtrActivator.resourceService
                .getI18NString("plugin.otr.authbuddydialog.CANCEL"));
        cancelButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                OtrActivator.scOtrEngine.abortSmp(otrContact);
                SmpAuthenticateBuddyDialog.this.dispose();
            }
        });
        c.insets = new Insets(5, 5, 5, 5);
        c.gridx = 1;
        buttonPanel.add(cancelButton, c);

        c.gridx = 2;
        JButton authenticateButton =
            new JButton(OtrActivator.resourceService
                .getI18NString(
                    "plugin.otr.authbuddydialog.AUTHENTICATE_BUDDY"));
        authenticateButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                OtrActivator.scOtrEngine.respondSmp(
                    otrContact, receiverTag, question, answerTextBox.getText());
                SmpAuthenticateBuddyDialog.this.dispose();
            }
        });

        buttonPanel.add(authenticateButton, c);

        this.getContentPane().add(mainPanel, BorderLayout.NORTH);
        this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        this.pack();
    }
}