Skip to content

Commit

Permalink
Added remote update dialog if new version available
Browse files Browse the repository at this point in the history
  • Loading branch information
delas committed May 5, 2015
1 parent d00955f commit 2b3cda9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 23 deletions.
109 changes: 86 additions & 23 deletions plg/src/plg/gui/remote/RemoteLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
Expand All @@ -29,8 +30,10 @@
import plg.gui.config.ConfigurationSet;
import plg.gui.config.UIConfiguration;
import plg.gui.controller.ApplicationController;
import plg.gui.util.collections.ImagesCollection;
import plg.stream.configuration.StreamConfiguration;
import plg.utils.CPUUtils;
import plg.utils.Logger;
import plg.utils.OSUtils;
import plg.utils.Pair;
import plg.utils.PlgConstants;
Expand Down Expand Up @@ -132,32 +135,92 @@ public void mouseReleased(MouseEvent e) {
* message.
*/
protected void initializeRemoteSession() {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
initializeLogger();
if (loggingEnabled) {
// ask for a new session id
String newSessionUrl = String.format(NEW_SESSION_URL,
PlgConstants.libPLG_VERSION,
OSUtils.determineOS(),
CPUUtils.CPUAvailable());
JSONObject sessionObj = httpRequestToJson(newSessionUrl);
if (sessionObj != null) {
RemoteLogger.this.sessionId = sessionObj.get("session_id").toString();
RemoteLogger.this.token = sessionObj.get("token").toString();
}
new SwingWorker<JSONObject, Void>() {
@Override
protected JSONObject doInBackground() throws Exception {
initializeLogger();
JSONObject sessionObj = null;

if (loggingEnabled) {
// ask for a new session id
String newSessionUrl = String.format(NEW_SESSION_URL,
PlgConstants.libPLG_VERSION,
OSUtils.determineOS(),
CPUUtils.CPUAvailable());
sessionObj = httpRequestToJson(newSessionUrl);
if (sessionObj != null) {
RemoteLogger.this.sessionId = sessionObj.get("session_id").toString();
RemoteLogger.this.token = sessionObj.get("token").toString();
}
return null;
}
return sessionObj;
}

@Override
protected void done() {
// register open application
log(REMOTE_MESSAGES.APPLICATION_STARTED).send();

@Override
protected void done() {
// register open application
log(REMOTE_MESSAGES.APPLICATION_STARTED).send();
};
}.execute();
try {
// check version
if (get() != null) {
parseLastVersion((JSONObject) get().get("last_version"));
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
};
}.execute();
}

/**
* This method parses the version information passed and shows a dialog if
* there is a new version of PLG available
*
* @param versionInfo the JSON object with the updated information
*/
protected void parseLastVersion(JSONObject versionInfo) {
final String version = versionInfo.get("version").toString();
final String date = versionInfo.get("date").toString();
final String info = versionInfo.get("info").toString();
final String url = versionInfo.get("url").toString();

new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
if (!version.equals(PlgConstants.libPLG_VERSION)) {
JLabel message = new JLabel("<html>New version available: <b>" + version + "</b> (released on " + date + ").</html>");
JLabel releaseInfo = new JLabel("<html>Release info:<br>" + info + "</html>");
JLabel urlMessage = new JLabel("<html>Download URL: <a href=\"" + url + "\">" + url + "</a>.</html>");
urlMessage.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (IOException | URISyntaxException ex) { }
}
}
});
urlMessage.setToolTipText("Open " + url);
urlMessage.setCursor(new Cursor(Cursor.HAND_CURSOR));

Object[] params = {message, releaseInfo, urlMessage};

JOptionPane.showMessageDialog(ApplicationController.instance().getMainFrame(),
params,
"New version of PLG available",
JOptionPane.INFORMATION_MESSAGE,
ImagesCollection.UPDATES_ICON);

Logger.instance().debug("A new version of PLG (" + version + ") is available");

} else {
Logger.instance().debug("Your version of PLG (" + PlgConstants.libPLG_VERSION + ") is the latest available");
}

return null;
}
}.execute();
}

/**
Expand Down
1 change: 1 addition & 0 deletions plg/src/plg/gui/util/collections/ImagesCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ImagesCollection {
public static final ImageIcon PLG_ICON_SCALED = ImageUtils.loadImage("/plg/resources/icons/application-icon.png", 32, 32);
public static final ImageIcon ERROR_ICON = ImageUtils.loadImage("/plg/resources/icons/error.png");
public static final ImageIcon HELP_ICON = ImageUtils.loadImage("/plg/resources/icons/famfamfam/book_open.png");
public static final ImageIcon UPDATES_ICON = ImageUtils.loadImage("/plg/resources/icons/updates.png");

// Toolbar icons
public static final ImageIcon ICON_NEW = ImageUtils.loadImage("/plg/resources/icons/famfamfam/page_add.png");
Expand Down
Binary file added plgResources/src/plg/resources/icons/updates.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b3cda9

Please sign in to comment.