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 |
|
---|
33 | /**
|
---|
34 | * Notifies user periodically of new received (unread) messages
|
---|
35 | * @since 6349
|
---|
36 | */
|
---|
37 | public 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.OSM_WEBSITE + "/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 (!isRunning() && interval > 0 && isUserEnoughIdentified()) {
|
---|
91 | task = EXECUTOR.scheduleAtFixedRate(WORKER, 0, interval * 60, TimeUnit.SECONDS);
|
---|
92 | Main.info("Message notifier active (checks every "+interval+" minute"+(interval>1?"s":"")+")");
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Stops the message notifier task if started
|
---|
98 | */
|
---|
99 | public static void stop() {
|
---|
100 | if (isRunning()) {
|
---|
101 | task.cancel(false);
|
---|
102 | Main.info("Message notifier inactive");
|
---|
103 | task = null;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Determines if the message notifier is currently running
|
---|
109 | * @return {@code true} if the notifier is running, {@code false} otherwise
|
---|
110 | */
|
---|
111 | public static boolean isRunning() {
|
---|
112 | return task != null;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Determines if user set enough information in JOSM preferences to make the request to OSM API without
|
---|
117 | * prompting him for a password.
|
---|
118 | * @return {@code true} if user chose an OAuth token or supplied both its username and password, {@code false otherwise}
|
---|
119 | */
|
---|
120 | public static boolean isUserEnoughIdentified() {
|
---|
121 | JosmUserIdentityManager identManager = JosmUserIdentityManager.getInstance();
|
---|
122 | if (identManager.isFullyIdentified()) {
|
---|
123 | return true;
|
---|
124 | } else {
|
---|
125 | CredentialsManager credManager = CredentialsManager.getInstance();
|
---|
126 | try {
|
---|
127 | if (JosmPreferencesCredentialAgent.class.equals(credManager.getCredentialsAgentClass())) {
|
---|
128 | if (OsmApi.isUsingOAuth()) {
|
---|
129 | return credManager.lookupOAuthAccessToken() != null;
|
---|
130 | } else {
|
---|
131 | String username = Main.pref.get("osm-server.username", null);
|
---|
132 | String password = Main.pref.get("osm-server.password", null);
|
---|
133 | return username != null && !username.isEmpty() && password != null && !password.isEmpty();
|
---|
134 | }
|
---|
135 | } else {
|
---|
136 | CredentialsAgentResponse credentials = credManager.getCredentials(
|
---|
137 | RequestorType.SERVER, OsmApi.getOsmApi().getHost(), false);
|
---|
138 | if (credentials != null) {
|
---|
139 | String username = credentials.getUsername();
|
---|
140 | char[] password = credentials.getPassword();
|
---|
141 | return username != null && !username.isEmpty() && password != null && password.length > 0;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | } catch (CredentialsAgentException e) {
|
---|
145 | Main.warn("Unable to get credentials: "+e.getMessage());
|
---|
146 | }
|
---|
147 | }
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 | }
|
---|