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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.java.sip.communicator.plugin.otr.authdialog;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import net.java.otr4j.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.plugin.otr.*;
import net.java.sip.communicator.service.protocol.*;
/**
* A special {@link Panel} that manages the OTR configuration.
*
* @author George Politis
*/
@SuppressWarnings("serial")
public class OtrConfigurationPanel
extends TransparentPanel
{
/**
* Creates the <tt>OtrConfigurationPanel</tt>
*/
public OtrConfigurationPanel()
{
this.initComponents();
}
/**
* A special {@link Panel} for Private Keys display.
*
* @author George Politis
*/
private static class PrivateKeysPanel
extends TransparentPanel
{
private AccountsComboBox cbAccounts;
private JTextField lblFingerprint;
private JButton btnGenerate;
public PrivateKeysPanel()
{
super(new BorderLayout());
this.initComponents();
this.openAccount(cbAccounts.getSelectedAccountID());
}
/**
* A special {@link JComboBox} for {@link AccountID} enumeration.
*
* @author George Politis
*/
private static class AccountsComboBox
extends JComboBox
{
/**
* A class hosted in an {@link AccountsComboBox} that holds a single
* {@link AccountID}.
*
* @author George Politis
*/
private static class AccountsComboBoxItem
{
public final AccountID accountID;
public AccountsComboBoxItem(AccountID accountID)
{
this.accountID = accountID;
}
@Override
public String toString()
{
return accountID.getDisplayName();
}
}
public AccountsComboBox()
{
List<AccountID> accountIDs = OtrActivator.getAllAccountIDs();
if (accountIDs == null)
return;
for (AccountID accountID : accountIDs)
this.addItem(new AccountsComboBoxItem(accountID));
}
/**
* Gets the selected {@link AccountID} for this
* {@link AccountsComboBox}.
*
* @return the selected account id
*/
public AccountID getSelectedAccountID()
{
Object selectedItem = this.getSelectedItem();
if (selectedItem instanceof AccountsComboBoxItem)
return ((AccountsComboBoxItem) selectedItem).accountID;
else
return null;
}
}
/**
* Sets up the {@link PrivateKeysPanel} components so that they reflect
* the {@link AccountID} param.
*
* @param account the {@link AccountID} to setup the components for.
*/
private void openAccount(AccountID account)
{
if (account == null)
{
lblFingerprint.setEnabled(false);
btnGenerate.setEnabled(false);
lblFingerprint.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.NO_KEY_PRESENT"));
btnGenerate.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.GENERATE"));
}
else
{
lblFingerprint.setEnabled(true);
btnGenerate.setEnabled(true);
String fingerprint =
OtrActivator.scOtrKeyManager
.getLocalFingerprint(account);
if (fingerprint == null || fingerprint.length() < 1)
{
lblFingerprint.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.NO_KEY_PRESENT"));
btnGenerate.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.GENERATE"));
}
else
{
lblFingerprint.setText(fingerprint);
btnGenerate.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.REGENERATE"));
}
}
}
/**
* Initializes the {@link PrivateKeysPanel} components.
*/
private void initComponents()
{
this.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createEtchedBorder(EtchedBorder.LOWERED),
OtrActivator.resourceService
.getI18NString("plugin.otr.configform.MY_PRIVATE_KEYS")));
JPanel labelsPanel = new TransparentPanel(new GridLayout(0, 1));
labelsPanel.add(new JLabel(OtrActivator.resourceService
.getI18NString("service.gui.ACCOUNT") + ": "));
labelsPanel.add(new JLabel(
OtrActivator.resourceService
.getI18NString("plugin.otr.configform.FINGERPRINT") + ": "));
JPanel valuesPanel = new TransparentPanel(new GridLayout(0, 1));
cbAccounts = new AccountsComboBox();
cbAccounts.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openAccount(((AccountsComboBox) e.getSource())
.getSelectedAccountID());
}
});
valuesPanel.add(cbAccounts);
lblFingerprint = new JTextField();
lblFingerprint.setEditable(false);
lblFingerprint.setOpaque(false);
lblFingerprint.setBorder(BorderFactory.createEmptyBorder());
valuesPanel.add(lblFingerprint);
JPanel buttonPanel
= new TransparentPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
btnGenerate = new JButton();
buttonPanel.add(btnGenerate);
btnGenerate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
AccountID account = cbAccounts.getSelectedAccountID();
if (account == null)
return;
OtrActivator.scOtrKeyManager
.generateKeyPair(account);
openAccount(account);
}
});
add(labelsPanel, BorderLayout.WEST);
add(valuesPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.EAST);
}
}
/**
* A special {@link Panel} for OTR policy display.
*
* @author George Politis
*/
private static class DefaultOtrPolicyPanel
extends TransparentPanel
{
// TODO We should listen for configuration value changes.
public DefaultOtrPolicyPanel()
{
this.initComponents();
this.loadPolicy();
}
/**
* Sets up the {@link DefaultOtrPolicyPanel} components so that they
* reflect the global OTR policy.
*
*/
public void loadPolicy()
{
OtrPolicy otrPolicy
= OtrActivator.scOtrEngine.getGlobalPolicy();
boolean otrEnabled = otrPolicy.getEnableManual();
cbEnable.setSelected(otrEnabled);
cbAutoInitiate.setEnabled(otrEnabled);
cbRequireOtr.setEnabled(otrEnabled);
boolean isAutoInit = otrPolicy.getEnableAlways();
cbAutoInitiate.setSelected(isAutoInit);
String otrMandatoryPropValue
= OtrActivator.configService.getString(
OtrActivator.OTR_MANDATORY_PROP);
String defaultOtrPropValue
= OtrActivator.resourceService.getSettingsString(
OtrActivator.OTR_MANDATORY_PROP);
boolean isMandatory = otrPolicy.getRequireEncryption();
if (otrMandatoryPropValue != null)
isMandatory = Boolean.parseBoolean(otrMandatoryPropValue);
else if (!isMandatory && defaultOtrPropValue != null)
isMandatory = Boolean.parseBoolean(defaultOtrPropValue);
cbRequireOtr.setSelected(isMandatory);
}
private SIPCommCheckBox cbEnable;
private SIPCommCheckBox cbAutoInitiate;
private SIPCommCheckBox cbRequireOtr;
/**
* Initializes the {@link DefaultOtrPolicyPanel} components.
*/
private void initComponents()
{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
cbEnable =
new SIPCommCheckBox(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.CB_ENABLE"));
cbEnable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
OtrPolicy otrPolicy
= OtrActivator.scOtrEngine
.getGlobalPolicy();
otrPolicy.setEnableManual(((JCheckBox) e.getSource())
.isSelected());
OtrActivator.scOtrEngine.setGlobalPolicy(otrPolicy);
DefaultOtrPolicyPanel.this.loadPolicy();
}
});
this.add(cbEnable);
cbAutoInitiate =
new SIPCommCheckBox(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.CB_AUTO"));
cbAutoInitiate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
OtrPolicy otrPolicy =
OtrActivator.scOtrEngine
.getGlobalPolicy();
boolean isAutoInit
= ((JCheckBox) e.getSource()).isSelected();
otrPolicy.setSendWhitespaceTag(isAutoInit);
OtrActivator.scOtrEngine.setGlobalPolicy(otrPolicy);
DefaultOtrPolicyPanel.this.loadPolicy();
}
});
this.add(cbAutoInitiate);
cbRequireOtr =
new SIPCommCheckBox(OtrActivator.resourceService
.getI18NString("plugin.otr.configform.CB_REQUIRE"));
cbRequireOtr.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
OtrPolicy otrPolicy =
OtrActivator.scOtrEngine.getGlobalPolicy();
boolean isRequired
= ((JCheckBox) e.getSource()).isSelected();
otrPolicy.setRequireEncryption(isRequired);
OtrActivator.configService.setProperty(
OtrActivator.OTR_MANDATORY_PROP,
Boolean.toString(isRequired));
OtrActivator.scOtrEngine.setGlobalPolicy(otrPolicy);
DefaultOtrPolicyPanel.this.loadPolicy();
}
});
this.add(cbRequireOtr);
}
}
/**
* Initializes all 3 panels of the {@link OtrConfigurationPanel}
*/
private void initComponents()
{
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.insets = new Insets(0, 0, 10, 0);
c.anchor = GridBagConstraints.PAGE_START;
JPanel pnlPrivateKeys = new PrivateKeysPanel();
c.gridy = 0;
this.add(pnlPrivateKeys, c);
JPanel pnlFingerprints = new KnownFingerprintsPanel();
pnlFingerprints.setMinimumSize(new Dimension(Short.MAX_VALUE, 160));
c.weighty = 1.0;
c.gridy = 1;
this.add(pnlFingerprints, c);
JPanel pnlPolicy = new DefaultOtrPolicyPanel();
c.gridy = 2;
this.add(pnlPolicy, c);
}
}
|