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

Last change on this file since 7474 was 7434, checked in by Don-vip, 10 years ago

fix #8885 (see #4614) - add offline mode with new command line argument --offline which can take one of several of these values (comma separated):

  • josm_website: to disable all accesses to JOSM website (when not cached, disables Getting Started page, help, plugin list, styles, imagery, presets, rules)
  • osm_api: to disable all accesses to OSM API (disables download, upload, changeset queries, history, user message notification)
  • all: alias to disable all values. Currently equivalent to "josm_website,osm_api"

Plus improved javadoc, fixed EDT violations, and fixed a bug with HTTP redirection sent without "Location" header

File size: 6.6 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;
32
33/**
34 * Notifies user periodically of new received (unread) messages
35 * @since 6349
36 */
37public final class MessageNotifier {
38
39 private MessageNotifier() {
40 // Hide default constructor for utils classes
41 }
42
43 /** Property defining if this task is enabled or not */
44 public static final BooleanProperty PROP_NOTIFIER_ENABLED = new BooleanProperty("message.notifier.enabled", true);
45 /** Property defining the update interval in minutes */
46 public static final IntegerProperty PROP_INTERVAL = new IntegerProperty("message.notifier.interval", 5);
47
48 private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor();
49
50 private static final Runnable WORKER = new Worker();
51
52 private static ScheduledFuture<?> task = null;
53
54 private static class Worker implements Runnable {
55
56 private int lastUnreadCount = 0;
57
58 @Override
59 public void run() {
60 try {
61 final UserInfo userInfo = new OsmServerUserInfoReader().fetchUserInfo(NullProgressMonitor.INSTANCE, tr("get number of unread messages"));
62 final int unread = userInfo.getUnreadMessages();
63 if (unread > 0 && unread != lastUnreadCount) {
64 GuiHelper.runInEDT(new Runnable() {
65 @Override
66 public void run() {
67 JPanel panel = new JPanel(new GridBagLayout());
68 panel.add(new JLabel(trn("You have {0} unread message.", "You have {0} unread messages.", unread, unread)), GBC.eol());
69 panel.add(new UrlLabel(Main.getOSMWebsite() + "/user/"+userInfo.getDisplayName()+"/inbox", tr("Click here to see your inbox.")), GBC.eol());
70 panel.setOpaque(false);
71 new Notification().setContent(panel)
72 .setIcon(JOptionPane.INFORMATION_MESSAGE)
73 .setDuration(Notification.TIME_LONG)
74 .show();
75 }
76 });
77 lastUnreadCount = unread;
78 }
79 } catch (OsmTransferException e) {
80 Main.warn(e);
81 }
82 }
83 }
84
85 /**
86 * Starts the message notifier task if not already started and if user is fully identified
87 */
88 public static void start() {
89 int interval = PROP_INTERVAL.get();
90 if (Main.isOffline(OnlineResource.OSM_API)) {
91 Main.info(tr("{0} not available (offline mode)", tr("Message notifier")));
92 } else if (!isRunning() && interval > 0 && isUserEnoughIdentified()) {
93 task = EXECUTOR.scheduleAtFixedRate(WORKER, 0, interval * 60, TimeUnit.SECONDS);
94 Main.info("Message notifier active (checks every "+interval+" minute"+(interval>1?"s":"")+")");
95 }
96 }
97
98 /**
99 * Stops the message notifier task if started
100 */
101 public static void stop() {
102 if (isRunning()) {
103 task.cancel(false);
104 Main.info("Message notifier inactive");
105 task = null;
106 }
107 }
108
109 /**
110 * Determines if the message notifier is currently running
111 * @return {@code true} if the notifier is running, {@code false} otherwise
112 */
113 public static boolean isRunning() {
114 return task != null;
115 }
116
117 /**
118 * Determines if user set enough information in JOSM preferences to make the request to OSM API without
119 * prompting him for a password.
120 * @return {@code true} if user chose an OAuth token or supplied both its username and password, {@code false otherwise}
121 */
122 public static boolean isUserEnoughIdentified() {
123 JosmUserIdentityManager identManager = JosmUserIdentityManager.getInstance();
124 if (identManager.isFullyIdentified()) {
125 return true;
126 } else {
127 CredentialsManager credManager = CredentialsManager.getInstance();
128 try {
129 if (JosmPreferencesCredentialAgent.class.equals(credManager.getCredentialsAgentClass())) {
130 if (OsmApi.isUsingOAuth()) {
131 return credManager.lookupOAuthAccessToken() != null;
132 } else {
133 String username = Main.pref.get("osm-server.username", null);
134 String password = Main.pref.get("osm-server.password", null);
135 return username != null && !username.isEmpty() && password != null && !password.isEmpty();
136 }
137 } else {
138 CredentialsAgentResponse credentials = credManager.getCredentials(
139 RequestorType.SERVER, OsmApi.getOsmApi().getHost(), false);
140 if (credentials != null) {
141 String username = credentials.getUsername();
142 char[] password = credentials.getPassword();
143 return username != null && !username.isEmpty() && password != null && password.length > 0;
144 }
145 }
146 } catch (CredentialsAgentException e) {
147 Main.warn("Unable to get credentials: "+e.getMessage());
148 }
149 }
150 return false;
151 }
152}
Note: See TracBrowser for help on using the repository browser.