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