1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.net.Authenticator.RequestorType;
|
---|
9 | import java.util.concurrent.Executors;
|
---|
10 | import java.util.concurrent.ScheduledExecutorService;
|
---|
11 | import java.util.concurrent.ScheduledFuture;
|
---|
12 | import java.util.concurrent.TimeUnit;
|
---|
13 |
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
20 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
21 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
22 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
23 | import org.openstreetmap.josm.gui.Notification;
|
---|
24 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
27 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
28 | import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
|
---|
29 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
30 | import org.openstreetmap.josm.io.auth.JosmPreferencesCredentialAgent;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Notifies user periodically of new received (unread) messages
|
---|
36 | * @since 6349
|
---|
37 | */
|
---|
38 | public 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 | private long lastTimeInMillis;
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void run() {
|
---|
63 | try {
|
---|
64 | long currentTime = System.currentTimeMillis();
|
---|
65 | // See #14671 - Make sure we don't run the API call many times after system wakeup
|
---|
66 | if (currentTime >= lastTimeInMillis + TimeUnit.MINUTES.toMillis(PROP_INTERVAL.get())) {
|
---|
67 | lastTimeInMillis = currentTime;
|
---|
68 | final UserInfo userInfo = new OsmServerUserInfoReader().fetchUserInfo(NullProgressMonitor.INSTANCE,
|
---|
69 | tr("get number of unread messages"));
|
---|
70 | final int unread = userInfo.getUnreadMessages();
|
---|
71 | if (unread > 0 && unread != lastUnreadCount) {
|
---|
72 | GuiHelper.runInEDT(() -> {
|
---|
73 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
74 | panel.add(new JLabel(trn("You have {0} unread message.", "You have {0} unread messages.", unread, unread)),
|
---|
75 | GBC.eol());
|
---|
76 | panel.add(new UrlLabel(Main.getBaseUserUrl() + '/' + userInfo.getDisplayName() + "/inbox",
|
---|
77 | tr("Click here to see your inbox.")), GBC.eol());
|
---|
78 | panel.setOpaque(false);
|
---|
79 | new Notification().setContent(panel)
|
---|
80 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
81 | .setDuration(Notification.TIME_LONG)
|
---|
82 | .show();
|
---|
83 | });
|
---|
84 | lastUnreadCount = unread;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | } catch (OsmTransferException e) {
|
---|
88 | Main.warn(e);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Starts the message notifier task if not already started and if user is fully identified
|
---|
95 | */
|
---|
96 | public static void start() {
|
---|
97 | int interval = PROP_INTERVAL.get();
|
---|
98 | if (Main.isOffline(OnlineResource.OSM_API)) {
|
---|
99 | Main.info(tr("{0} not available (offline mode)", tr("Message notifier")));
|
---|
100 | } else if (!isRunning() && interval > 0 && isUserEnoughIdentified()) {
|
---|
101 | task = EXECUTOR.scheduleAtFixedRate(WORKER, 0, TimeUnit.MINUTES.toSeconds(interval), TimeUnit.SECONDS);
|
---|
102 | Main.info("Message notifier active (checks every "+interval+" minute"+(interval > 1 ? "s" : "")+')');
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Stops the message notifier task if started
|
---|
108 | */
|
---|
109 | public static void stop() {
|
---|
110 | if (isRunning()) {
|
---|
111 | task.cancel(false);
|
---|
112 | Main.info("Message notifier inactive");
|
---|
113 | task = null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Determines if the message notifier is currently running
|
---|
119 | * @return {@code true} if the notifier is running, {@code false} otherwise
|
---|
120 | */
|
---|
121 | public static boolean isRunning() {
|
---|
122 | return task != null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Determines if user set enough information in JOSM preferences to make the request to OSM API without
|
---|
127 | * prompting him for a password.
|
---|
128 | * @return {@code true} if user chose an OAuth token or supplied both its username and password, {@code false otherwise}
|
---|
129 | */
|
---|
130 | public static boolean isUserEnoughIdentified() {
|
---|
131 | JosmUserIdentityManager identManager = JosmUserIdentityManager.getInstance();
|
---|
132 | if (identManager.isFullyIdentified()) {
|
---|
133 | return true;
|
---|
134 | } else {
|
---|
135 | CredentialsManager credManager = CredentialsManager.getInstance();
|
---|
136 | try {
|
---|
137 | if (JosmPreferencesCredentialAgent.class.equals(credManager.getCredentialsAgentClass())) {
|
---|
138 | if (OsmApi.isUsingOAuth()) {
|
---|
139 | return credManager.lookupOAuthAccessToken() != null;
|
---|
140 | } else {
|
---|
141 | String username = Main.pref.get("osm-server.username", null);
|
---|
142 | String password = Main.pref.get("osm-server.password", null);
|
---|
143 | return username != null && !username.isEmpty() && password != null && !password.isEmpty();
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | CredentialsAgentResponse credentials = credManager.getCredentials(
|
---|
147 | RequestorType.SERVER, OsmApi.getOsmApi().getHost(), false);
|
---|
148 | if (credentials != null) {
|
---|
149 | String username = credentials.getUsername();
|
---|
150 | char[] password = credentials.getPassword();
|
---|
151 | return username != null && !username.isEmpty() && password != null && password.length > 0;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | } catch (CredentialsAgentException e) {
|
---|
155 | Main.warn(e, "Unable to get credentials:");
|
---|
156 | }
|
---|
157 | }
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | }
|
---|