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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/*
* 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;
import java.awt.event.*;
import java.security.*;
import javax.swing.*;
import net.java.otr4j.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.plugin.otr.OtrContactManager.OtrContact;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;
/**
* A special {@link JMenu} that holds the menu items for controlling the
* Off-the-Record functionality for a specific contact.
*
* @author George Politis
* @author Lyubomir Marinov
* @author Marin Dzhigarov
*/
class OtrContactMenu
implements ActionListener,
ScOtrEngineListener,
ScOtrKeyManagerListener
{
private final Logger logger = Logger.getLogger(OtrContactMenu.class);
private static final String ACTION_COMMAND_AUTHENTICATE_BUDDY =
"AUTHENTICATE_BUDDY";
private static final String ACTION_COMMAND_CB_AUTO = "CB_AUTO";
private static final String ACTION_COMMAND_CB_AUTO_ALL = "CB_AUTO_ALL";
private static final String ACTION_COMMAND_CB_ENABLE = "CB_ENABLE";
private static final String ACTION_COMMAND_CB_REQUIRE = "CB_REQUIRE";
private static final String ACTION_COMMAND_CB_RESET = "CB_RESET";
private static final String ACTION_COMMAND_END_OTR = "END_OTR";
private static final String ACTION_COMMAND_REFRESH_OTR = "REFRESH_OTR";
private static final String ACTION_COMMAND_START_OTR = "START_OTR";
private final OtrContact contact;
/**
* The indicator which determines whether this <tt>JMenu</tt> is displayed
* in the Mac OS X screen menu bar and thus should work around the known
* problem of PopupMenuListener not being invoked.
*/
private final boolean inMacOSXScreenMenuBar;
/**
* We keep this variable so we can determine if the policy has changed
* or not in {@link OtrContactMenu#setOtrPolicy(OtrPolicy)}.
*/
private OtrPolicy otrPolicy;
private ScSessionStatus sessionStatus;
private final JMenu parentMenu;
private final SIPCommMenu separateMenu;
/**
* The OtrContactMenu constructor.
*
* @param otrContact the OtrContact this menu refers to.
* @param inMacOSXScreenMenuBar <tt>true</tt> if the new menu is to be
* displayed in the Mac OS X screen menu bar; <tt>false</tt>, otherwise
* @param menu the parent menu
*/
public OtrContactMenu( OtrContact otrContact,
boolean inMacOSXScreenMenuBar,
JMenu menu,
boolean isSeparateMenu)
{
this.contact = otrContact;
this.inMacOSXScreenMenuBar = inMacOSXScreenMenuBar;
this.parentMenu = menu;
String resourceName =
otrContact.resource != null
? "/" + otrContact.resource.getResourceName()
: "";
separateMenu
= isSeparateMenu
? new SIPCommMenu(otrContact.contact.getDisplayName()
+ resourceName)
: null;
/*
* XXX This OtrContactMenu instance cannot be added as a listener to
* scOtrEngine and scOtrKeyManager without being removed later on
* because the latter live forever. Unfortunately, the dispose() method
* of this instance is never executed. OtrWeakListener will keep this
* instance as a listener of scOtrEngine and scOtrKeyManager for as long
* as this instance is necessary. And this instance will be strongly
* referenced by the JMenuItems which depict it. So when the JMenuItems
* are gone, this instance will become obsolete and OtrWeakListener will
* remove it as a listener of scOtrEngine and scOtrKeyManager.
*/
new OtrWeakListener<OtrContactMenu>(
this,
OtrActivator.scOtrEngine, OtrActivator.scOtrKeyManager);
setSessionStatus(
OtrActivator.scOtrEngine.getSessionStatus(this.contact));
setOtrPolicy(
OtrActivator.scOtrEngine.getContactPolicy(otrContact.contact));
buildMenu();
}
/*
* Implements ActionListener#actionPerformed(ActionEvent).
*/
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (ACTION_COMMAND_END_OTR.equals(actionCommand))
{
OtrPolicy policy =
OtrActivator.scOtrEngine.getContactPolicy(contact.contact);
policy.setSendWhitespaceTag(false);
OtrActivator.scOtrEngine.setContactPolicy(contact.contact, policy);
// End session.
OtrActivator.scOtrEngine.endSession(contact);
}
else if (ACTION_COMMAND_START_OTR.equals(actionCommand))
{
OtrPolicy policy =
OtrActivator.scOtrEngine.getContactPolicy(contact.contact);
OtrPolicy globalPolicy =
OtrActivator.scOtrEngine.getGlobalPolicy();
policy.setSendWhitespaceTag(globalPolicy.getSendWhitespaceTag());
OtrActivator.scOtrEngine.setContactPolicy(contact.contact, policy);
// Start session.
OtrActivator.scOtrEngine.startSession(contact);
}
else if (ACTION_COMMAND_REFRESH_OTR.equals(actionCommand))
// Refresh session.
OtrActivator.scOtrEngine.refreshSession(contact);
else if (ACTION_COMMAND_AUTHENTICATE_BUDDY.equals(actionCommand))
// Launch auth buddy dialog.
SwingOtrActionHandler.openAuthDialog(contact);
else if (ACTION_COMMAND_CB_ENABLE.equals(actionCommand))
{
OtrPolicy policy =
OtrActivator.scOtrEngine.getContactPolicy(contact.contact);
boolean state = ((JCheckBoxMenuItem) e.getSource()).isSelected();
policy.setEnableManual(state);
OtrActivator.scOtrEngine.setContactPolicy(contact.contact, policy);
}
else if (ACTION_COMMAND_CB_AUTO.equals(actionCommand))
{
OtrPolicy policy =
OtrActivator.scOtrEngine.getContactPolicy(contact.contact);
boolean state = ((JCheckBoxMenuItem) e.getSource()).isSelected();
policy.setSendWhitespaceTag(state);
OtrActivator.scOtrEngine.setContactPolicy(contact.contact, policy);
}
else if (ACTION_COMMAND_CB_AUTO_ALL.equals(actionCommand))
{
OtrPolicy globalPolicy =
OtrActivator.scOtrEngine.getGlobalPolicy();
boolean state = ((JCheckBoxMenuItem) e.getSource()).isSelected();
globalPolicy.setSendWhitespaceTag(state);
OtrActivator.scOtrEngine.setGlobalPolicy(globalPolicy);
}
else if (ACTION_COMMAND_CB_REQUIRE.equals(actionCommand))
{
OtrPolicy policy =
OtrActivator.scOtrEngine.getContactPolicy(contact.contact);
boolean state = ((JCheckBoxMenuItem) e.getSource()).isSelected();
policy.setRequireEncryption(state);
OtrActivator.configService.setProperty(
OtrActivator.OTR_MANDATORY_PROP,
Boolean.toString(state));
OtrActivator.scOtrEngine.setContactPolicy(contact.contact, policy);
}
else if (ACTION_COMMAND_CB_RESET.equals(actionCommand))
OtrActivator.scOtrEngine.setContactPolicy(contact.contact, null);
}
/*
* Implements ScOtrEngineListener#contactPolicyChanged(Contact).
*/
public void contactPolicyChanged(Contact contact)
{
// Update the corresponding to the contact menu.
if (OtrContactMenu.this.contact != null &&
contact.equals(OtrContactMenu.this.contact.contact))
setOtrPolicy(OtrActivator.scOtrEngine.getContactPolicy(contact));
}
/*
* Implements ScOtrKeyManagerListener#contactVerificationStatusChanged(
* Contact).
*/
public void contactVerificationStatusChanged(OtrContact otrContact)
{
if (otrContact.equals(OtrContactMenu.this.contact))
setSessionStatus(
OtrActivator.scOtrEngine.getSessionStatus(otrContact));
}
/**
* Disposes of this instance by making it available for garage collection
* e.g. removes the listeners it has installed on global instances such as
* <tt>OtrActivator#scOtrEngine</tt> and
* <tt>OtrActivator#scOtrKeyManager</tt>.
*/
void dispose()
{
OtrActivator.scOtrEngine.removeListener(this);
OtrActivator.scOtrKeyManager.removeListener(this);
}
/*
* Implements ScOtrEngineListener#globalPolicyChanged().
*/
public void globalPolicyChanged()
{
setOtrPolicy(OtrActivator.scOtrEngine.getContactPolicy(contact.contact));
}
/**
* Rebuilds own menuitems according to {@link OtrContactMenu#sessionStatus}
* and the {@link OtrPolicy} for {@link OtrContactMenu#contact}.
*/
private void buildMenu()
{
if(separateMenu != null)
separateMenu.removeAll();
OtrPolicy policy =
OtrActivator.scOtrEngine.getContactPolicy(contact.contact);
JMenuItem endOtr = new JMenuItem();
endOtr.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.END_OTR"));
endOtr.setActionCommand(ACTION_COMMAND_END_OTR);
endOtr.addActionListener(this);
JMenuItem startOtr = new JMenuItem();
startOtr.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.START_OTR"));
startOtr.setEnabled(policy.getEnableManual());
startOtr.setActionCommand(ACTION_COMMAND_START_OTR);
startOtr.addActionListener(this);
JMenuItem refreshOtr = new JMenuItem();
refreshOtr.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.REFRESH_OTR"));
refreshOtr.setEnabled(policy.getEnableManual());
refreshOtr.setActionCommand(ACTION_COMMAND_REFRESH_OTR);
refreshOtr.addActionListener(this);
switch (this.sessionStatus)
{
case LOADING:
if (separateMenu != null)
{
separateMenu.add(endOtr);
separateMenu.add(refreshOtr);
}
else
{
parentMenu.add(endOtr);
parentMenu.add(refreshOtr);
}
break;
case ENCRYPTED:
JMenuItem authBuddy = new JMenuItem();
authBuddy.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.AUTHENTICATE_BUDDY"));
authBuddy.setActionCommand(ACTION_COMMAND_AUTHENTICATE_BUDDY);
authBuddy.addActionListener(this);
if (separateMenu != null)
{
separateMenu.add(endOtr);
separateMenu.add(refreshOtr);
separateMenu.add(authBuddy);
}
else
{
parentMenu.add(endOtr);
parentMenu.add(refreshOtr);
parentMenu.add(authBuddy);
}
break;
case FINISHED:
if (separateMenu != null)
{
separateMenu.add(endOtr);
separateMenu.add(refreshOtr);
}
else
{
parentMenu.add(endOtr);
parentMenu.add(refreshOtr);
}
break;
case TIMED_OUT:
case PLAINTEXT:
if (separateMenu != null)
separateMenu.add(startOtr);
else
parentMenu.add(startOtr);
break;
}
JCheckBoxMenuItem cbEnable = new JCheckBoxMenuItem();
cbEnable.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.CB_ENABLE"));
cbEnable.setSelected(policy.getEnableManual());
cbEnable.setActionCommand(ACTION_COMMAND_CB_ENABLE);
cbEnable.addActionListener(this);
JCheckBoxMenuItem cbAlways = new JCheckBoxMenuItem();
cbAlways.setText(String.format(
OtrActivator.resourceService
.getI18NString(
"plugin.otr.menu.CB_AUTO",
new String[]
{contact.contact.getDisplayName()})));
cbAlways.setEnabled(policy.getEnableManual());
cbAlways.setSelected(policy.getEnableAlways());
cbAlways.setActionCommand(ACTION_COMMAND_CB_AUTO);
cbAlways.addActionListener(this);
JCheckBoxMenuItem cbAlwaysAll = new JCheckBoxMenuItem();
cbAlwaysAll.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.CB_AUTO_ALL"));
cbAlwaysAll.setEnabled(policy.getEnableManual());
boolean isAutoInit =
OtrActivator.scOtrEngine.getGlobalPolicy().getEnableAlways();
cbAlwaysAll.setSelected(isAutoInit);
cbAlwaysAll.setActionCommand(ACTION_COMMAND_CB_AUTO_ALL);
cbAlwaysAll.addActionListener(this);
JCheckBoxMenuItem cbRequire = new JCheckBoxMenuItem();
cbRequire.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.CB_REQUIRE"));
cbRequire.setEnabled(policy.getEnableManual());
String otrMandatoryPropValue
= OtrActivator.configService.getString(
OtrActivator.OTR_MANDATORY_PROP);
String defaultOtrPropValue
= OtrActivator.resourceService.getSettingsString(
OtrActivator.OTR_MANDATORY_PROP);
boolean isMandatory = policy.getRequireEncryption();
if (otrMandatoryPropValue != null)
isMandatory = Boolean.parseBoolean(otrMandatoryPropValue);
else if (!isMandatory && defaultOtrPropValue != null)
isMandatory = Boolean.parseBoolean(defaultOtrPropValue);
cbRequire.setSelected(isMandatory);
cbRequire.setActionCommand(ACTION_COMMAND_CB_REQUIRE);
cbRequire.addActionListener(this);
JMenuItem cbReset = new JMenuItem();
cbReset.setText(OtrActivator.resourceService
.getI18NString("plugin.otr.menu.CB_RESET"));
cbReset.setActionCommand(ACTION_COMMAND_CB_RESET);
cbReset.addActionListener(this);
if (separateMenu != null)
{
separateMenu.addSeparator();
separateMenu.add(cbEnable);
separateMenu.add(cbAlways);
separateMenu.add(cbAlwaysAll);
separateMenu.add(cbRequire);
separateMenu.addSeparator();
separateMenu.add(cbReset);
parentMenu.add(separateMenu);
}
else
{
parentMenu.addSeparator();
parentMenu.add(cbEnable);
parentMenu.add(cbAlways);
parentMenu.add(cbAlwaysAll);
parentMenu.add(cbRequire);
parentMenu.addSeparator();
parentMenu.add(cbReset);
}
}
/*
* Implements ScOtrEngineListener#sessionStatusChanged(Contact).
*/
public void sessionStatusChanged(OtrContact otrContact)
{
if (otrContact.equals(OtrContactMenu.this.contact))
setSessionStatus(
OtrActivator.scOtrEngine.getSessionStatus(otrContact));
}
/**
* Sets the {@link OtrContactMenu#sessionStatus} value, updates the menu
* icon and, if necessary, rebuilds the menuitems to match the passed in
* sessionStatus.
*
* @param sessionStatus the {@link ScSessionStatus}.
*/
private void setSessionStatus(ScSessionStatus sessionStatus)
{
if (sessionStatus != this.sessionStatus)
{
logger.debug(
"Setting session status of contact " + contact.contact +
" to " + sessionStatus + ". Was " + this.sessionStatus);
this.sessionStatus = sessionStatus;
if (separateMenu != null)
{
updateIcon();
if (separateMenu.isPopupMenuVisible() || inMacOSXScreenMenuBar)
buildMenu();
}
}
}
/**
* Sets the {@link OtrContactMenu#otrPolicy} and, if necessary, rebuilds the
* menuitems to match the passed in otrPolicy.
*
* @param otrPolicy
*/
private void setOtrPolicy(OtrPolicy otrPolicy)
{
if (!otrPolicy.equals(this.otrPolicy))
{
this.otrPolicy = otrPolicy;
if ((separateMenu != null)
&& (separateMenu.isPopupMenuVisible()
|| inMacOSXScreenMenuBar))
{
buildMenu();
}
}
}
/**
* Updates the menu icon based on {@link OtrContactMenu#sessionStatus}
* value.
*/
private void updateIcon()
{
if (separateMenu == null)
return;
String imageID;
switch (sessionStatus)
{
case ENCRYPTED:
PublicKey pubKey =
OtrActivator.scOtrEngine.getRemotePublicKey(contact);
String fingerprint =
OtrActivator.scOtrKeyManager.
getFingerprintFromPublicKey(pubKey);
imageID
= OtrActivator.scOtrKeyManager.isVerified(
contact.contact, fingerprint)
? "plugin.otr.ENCRYPTED_ICON_16x16"
: "plugin.otr.ENCRYPTED_UNVERIFIED_ICON_16x16";
break;
case FINISHED:
imageID = "plugin.otr.FINISHED_ICON_16x16";
break;
case PLAINTEXT:
imageID = "plugin.otr.PLAINTEXT_ICON_16x16";
break;
default:
return;
}
separateMenu.setIcon(OtrActivator.resourceService.getImage(imageID));
}
@Override
public void multipleInstancesDetected(OtrContact contact) {}
@Override
public void outgoingSessionChanged(OtrContact otrContact)
{
if (otrContact.equals(OtrContactMenu.this.contact))
setSessionStatus(
OtrActivator.scOtrEngine.getSessionStatus(otrContact));
}
}
|