source: josm/trunk/src/org/openstreetmap/josm/io/MessageNotifier.java@ 9308

Last change on this file since 9308 was 8846, checked in by Don-vip, 9 years ago

sonar - fb-contrib - minor performance improvements:

  • Method passes constant String of length 1 to character overridden method
  • Method needlessly boxes a boolean constant
  • Method uses iterator().next() on a List to get the first item
  • Method converts String to boxed primitive using excessive boxing
  • Method converts String to primitive using excessive boxing
  • Method creates array using constants
  • Class defines List based fields but uses them like Sets
  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.GridBagLayout;
8import java.net.Authenticator.RequestorType;
9import java.util.concurrent.Executors;
10import java.util.concurrent.ScheduledExecutorService;
11import java.util.concurrent.ScheduledFuture;
12import java.util.concurrent.TimeUnit;
13
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.osm.UserInfo;
20import org.openstreetmap.josm.data.preferences.BooleanProperty;
21import org.openstreetmap.josm.data.preferences.IntegerProperty;
22import org.openstreetmap.josm.gui.JosmUserIdentityManager;
23import org.openstreetmap.josm.gui.Notification;
24import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26import org.openstreetmap.josm.gui.widgets.UrlLabel;
27import org.openstreetmap.josm.io.auth.CredentialsAgentException;
28import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
29import org.openstreetmap.josm.io.auth.CredentialsManager;
30import org.openstreetmap.josm.io.auth.JosmPreferencesCredentialAgent;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.Utils;
33
34/**
35 * Notifies user periodically of new received (unread) messages
36 * @since 6349
37 */
38public final class MessageNotifier {
39
40 private MessageNotifier() {
41 // Hide default constructor for utils classes
42 }
43
44 /** Property defining if this task is enabled or not */
45 public static final BooleanProperty PROP_NOTIFIER_ENABLED = new BooleanProperty("message.notifier.enabled", true);
46 /** Property defining the update interval in minutes */
47 public static final IntegerProperty PROP_INTERVAL = new IntegerProperty("message.notifier.interval", 5);
48
49 private static final ScheduledExecutorService EXECUTOR =
50 Executors.newSingleThreadScheduledExecutor(Utils.newThreadFactory("message-notifier-%d", Thread.NORM_PRIORITY));
51
52 private static final Runnable WORKER = new Worker();
53
54 private static volatile ScheduledFuture<?> task;
55
56 private static class Worker implements Runnable {
57
58 private int lastUnreadCount;
59
60 @Override
61 public void run() {
62 try {
63 final UserInfo userInfo = new OsmServerUserInfoReader().fetchUserInfo(NullProgressMonitor.INSTANCE,
64 tr("get number of unread messages"));
65 final int unread = userInfo.getUnreadMessages();
66 if (unread > 0 && unread != lastUnreadCount) {
67 GuiHelper.runInEDT(new Runnable() {
68 @Override
69 public void run() {
70 JPanel panel = new JPanel(new GridBagLayout());
71 panel.add(new JLabel(trn("You have {0} unread message.", "You have {0} unread messages.", unread, unread)),
72 GBC.eol());
73 panel.add(new UrlLabel(Main.getBaseUserUrl() + '/' + userInfo.getDisplayName() + "/inbox",
74 tr("Click here to see your inbox.")), GBC.eol());
75 panel.setOpaque(false);
76 new Notification().setContent(panel)
77 .setIcon(JOptionPane.INFORMATION_MESSAGE)
78 .setDuration(Notification.TIME_LONG)
79 .show();
80 }
81 });
82 lastUnreadCount = unread;
83 }
84 } catch (OsmTransferException e) {
85 Main.warn(e);
86 }
87 }
88 }
89
90 /**
91 * Starts the message notifier task if not already started and if user is fully identified
92 */
93 public static void start() {
94 int interval = PROP_INTERVAL.get();
95 if (Main.isOffline(OnlineResource.OSM_API)) {
96 Main.info(tr("{0} not available (offline mode)", tr("Message notifier")));
97 } else if (!isRunning() && interval > 0 && isUserEnoughIdentified()) {
98 task = EXECUTOR.scheduleAtFixedRate(WORKER, 0, interval * 60, TimeUnit.SECONDS);
99 Main.info("Message notifier active (checks every "+interval+" minute"+(interval > 1 ? "s" : "")+')');
100 }
101 }
102
103 /**
104 * Stops the message notifier task if started
105 */
106 public static void stop() {
107 if (isRunning()) {
108 task.cancel(false);
109 Main.info("Message notifier inactive");
110 task = null;
111 }
112 }
113
114 /**
115 * Determines if the message notifier is currently running
116 * @return {@code true} if the notifier is running, {@code false} otherwise
117 */
118 public static boolean isRunning() {
119 return task != null;
120 }
121
122 /**
123 * Determines if user set enough information in JOSM preferences to make the request to OSM API without
124 * prompting him for a password.
125 * @return {@code true} if user chose an OAuth token or supplied both its username and password, {@code false otherwise}
126 */
127 public static boolean isUserEnoughIdentified() {
128 JosmUserIdentityManager identManager = JosmUserIdentityManager.getInstance();
129 if (identManager.isFullyIdentified()) {
130 return true;
131 } else {
132 CredentialsManager credManager = CredentialsManager.getInstance();
133 try {
134 if (JosmPreferencesCredentialAgent.class.equals(credManager.getCredentialsAgentClass())) {
135 if (OsmApi.isUsingOAuth()) {
136 return credManager.lookupOAuthAccessToken() != null;
137 } else {
138 String username = Main.pref.get("osm-server.username", null);
139 String password = Main.pref.get("osm-server.password", null);
140 return username != null && !username.isEmpty() && password != null && !password.isEmpty();
141 }
142 } else {
143 CredentialsAgentResponse credentials = credManager.getCredentials(
144 RequestorType.SERVER, OsmApi.getOsmApi().getHost(), false);
145 if (credentials != null) {
146 String username = credentials.getUsername();
147 char[] password = credentials.getPassword();
148 return username != null && !username.isEmpty() && password != null && password.length > 0;
149 }
150 }
151 } catch (CredentialsAgentException e) {
152 Main.warn("Unable to get credentials: "+e.getMessage());
153 }
154 }
155 return false;
156 }
157}
Note: See TracBrowser for help on using the repository browser.