+++ /dev/null
-package com.tedpearson.util.update;
-
-import java.net.*;
-import java.io.*;
-import java.util.prefs.*;
-import java.awt.Component;
-import javax.swing.JOptionPane;
-import javax.swing.*;
-
-/*
- TODO:
- show update window with progress bar when updating (optionally)
-*/
-
-public class Updater {
- private int version = -1;
- private URL downloadURL;
- private static Updater updater = new Updater();
-
- public static Updater getUpdater() {
- return updater;
- }
-
- public boolean checkForUpdate(
- Component parentComponent,
- String updateUrl,
- int currentVersion) {
- return checkForUpdate(parentComponent, updateUrl, "this program", currentVersion, true);
- }
-
-
- /**
- * Just check to see if there is an update.
- */
- public boolean checkForUpdate(
- Component parentComponent,
- String updateUrl,
- String program,
- int currentVersion,
- boolean promptUser) {
- String node = "com/tedpearson/update";
- Preferences prefs = Preferences.userRoot().node(node);
- String update = prefs.get(updateUrl,"");
- if(update.equals("Never")) {
- return false;
- }
- if(!update.equals("Yes")) {
- if(!promptUser) {
- return false;
- }
- // first, confirm user wants to allow updates
- int option = JOptionPane.showOptionDialog(parentComponent,
- "Do you want to let " + program + " check for updates when it opens?",
- "Check for Updates?",
- 0, JOptionPane.QUESTION_MESSAGE,
- null, new String[] {"Yes","This Time","Not Now","Never"},"Yes");
- switch(option) {
- case 2:
- return false;
- case 3:
- prefs.put(updateUrl,"Never");
- return false;
- case 0:
- prefs.put(updateUrl,"Yes");
- case 1:
- }
- }
- // query the server to check the version number
- accessVersion(parentComponent, updateUrl);
- // System.out.println(version+","+currentVersion);
- if(version > currentVersion) {
- return true;
- } else {
- return false;
- }
- }
-
- public void checkAndUpdate(
- Component parentComponent,
- String updateUrl,
- String program,
- int currentVersion,
- boolean promptUser,
- String jar) {
- if(checkForUpdate(parentComponent, updateUrl, program, currentVersion, promptUser)) {
- updateJar(parentComponent, updateUrl, currentVersion, promptUser, jar);
- }
- }
-
- public void checkAndUpdate(
- Component parentComponent,
- String updateUrl,
- int currentVersion) {
- if(checkForUpdate(parentComponent, updateUrl, currentVersion)) {
- updateJar(parentComponent, updateUrl, currentVersion);
- }
- }
-
- public void updateJar(
- Component parentComponent,
- String updateUrl,
- int currentVersion) {
- updateJar(parentComponent, updateUrl, currentVersion, true, null);
- }
-
- public void updateJar(
- Component parentComponent,
- String updateUrl,
- int currentVersion,
- boolean promptUser,
- String jar) {
- try {
- accessVersion(parentComponent, updateUrl);
- // download new version
- File temp = File.createTempFile("com.tedpearson.updater.download",null);
- temp.deleteOnExit();
- URLConnection conn = downloadURL.openConnection();
- ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(
- parentComponent,
- "Downloading program update",
- conn.getInputStream()
- );
- ProgressMonitor pm = pmis.getProgressMonitor();
- pm.setMillisToDecideToPopup(0);
- pm.setMillisToPopup(0);
- if(!promptUser) {
- pm.setMillisToDecideToPopup(Integer.MAX_VALUE);
- pm.setMillisToPopup(Integer.MAX_VALUE);
- }
- pm.setMaximum(conn.getHeaderFieldInt("Content-Length",0));
- copyFile(pmis, new FileOutputStream(temp));
- // replace old with new.
- if(jar == null) {
- jar = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
- }
- jar = jar.replaceAll("%20"," ");
- copyFile(new FileInputStream(temp), new FileOutputStream(new File(jar)));
- // remove temp file
- temp.delete();
- // launch new version and quit this one.
- ProcessBuilder pb = new ProcessBuilder("java","-jar",jar);
- pb.start();
- System.exit(0);
- // no new version
- } catch(Exception e) {
- // errors.
- e.printStackTrace();
- // show error here.
- if(promptUser) {
- JOptionPane.showMessageDialog(parentComponent, "There was an error while trying to update.",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
-
- private void accessVersion(Component parentComponent, String updateUrl) {
- if(version != -1) return;
- try {
- URL url = new URL(updateUrl);
- BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
- version = Integer.parseInt(br.readLine());
- downloadURL = new URL(br.readLine());
- } catch(Exception e) {
- // errors.
- e.printStackTrace();
- // show error here.
- JOptionPane.showMessageDialog(parentComponent, "There was an error while trying to update.",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
-
- /**
- * Utility method to copy a file. I can't believe java doesn't have anything
- * built-in to do something this simple with less code. Other than channels, that is.
- *
- * @param in stream to read file from
- * @param out stream to write file to
- */
- public static void copyFile(InputStream in, OutputStream out) throws IOException {
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
-
- }
- in.close();
- out.close();
- }
-}
\ No newline at end of file
+++ /dev/null
-package com.tedpearson.ypp.market;
-
-import javax.swing.*;
-import java.awt.*;
-import java.awt.event.*;
-import javax.swing.border.*;
-import java.io.*;
-import com.tedpearson.util.update.*;
-import java.util.Properties;
-import java.net.URLDecoder;
-
-/*
- allow adding islands/oceans
- implement uploads to YAARG
-*/
-
-/**
-* An Installer for PCTB Java Client.
-*/
-public class Installer extends JFrame {
- private JLabel label;
- private JProgressBar progress;
- private JButton install, uninstall;
- private static String os = System.getProperty("os.name");
- private static String home = System.getProperty("java.home");
- private static String user_home = System.getProperty("user.home");
- private boolean installed = false;
- private boolean uninstalled = false;
- private static PrintWriter pw;
-
- public static void debug(String str) {
- try {
- pw.println(str);
- pw.flush();
- }catch(Exception e) {
-
- }
- }
-
- public static void main(String[] args) {
- /*
- try{
- pw = new PrintWriter("C:/Users/Public/ERRORS");
- }catch(Exception e) {
-
- }
- */
- new Installer(args);
- }
-
- /**
- * Checks if we have permission to write to the Java Home folder
- * that runs YPP. Pops up an error message and exits if we don't have access,
- * or on Mac OS X, re-runs the installer using applescript to authenticate.
- */
- private void checkPermission(String[] args) {
- File a11y = null;
- a11y = new File(getJavaHome(),"lib");
- if(os.equals("Mac OS X")) {
- if(!a11y.canWrite()) {
- JOptionPane.showMessageDialog(null,"Please authenticate as an Administrator, when prompted, to continue installation.");
- try {
- String installer = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation()
- .getPath(), "URT-8");
- Runtime.getRuntime().exec(new String[]{"osascript","-e","do shell script \"java -jar " +
- installer + "\" with administrator privileges"});
- } catch(Exception e) {
- e.printStackTrace();
- }
- System.exit(0);
- }
- } else {
- // clean up after myself
- if(os.contains("Vista")) {
- File tempDir = new File(System.getProperty("java.io.tmpdir"));
- for(File f : tempDir.listFiles()) {
- if(f.getName().startsWith("PCTB-lib")) {
- for(File g : f.listFiles()) {
- g.delete();
- }
- f.delete();
- }
- }
- }
- // first check for YPP java
- boolean canWrite = true;
- File test = new File(a11y, "test_pctb");
- try {
- test.createNewFile();
- test.delete();
- } catch(IOException e) {
- canWrite = false;
- }
- if(!canWrite || !a11y.canWrite()) {
- if(os.contains("Vista")) {
- if(args.length == 1 && args[0].equals("--elevate")) {
- JOptionPane.showMessageDialog(null,"Please run this installer while logged in as an Administrator.");
- System.exit(0);
- }
- try {
- String installer = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation()
- .getPath(), "UTF-8").replaceFirst("/","");
- ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "elevate javaw -jar \"" + installer +
- "\" --elevate");
- // create temp lib directory
- File temp = File.createTempFile("PCTB-lib",null);
- temp.delete();
- temp.mkdir();
- File elevate = new File(temp, "elevate.cmd");
- OutputStream out = new FileOutputStream(elevate);
- InputStream in = getClass().getResourceAsStream("/lib/elevate.cmd");
- Updater.copyFile(in, out);
-
- elevate = new File(temp, "elevate.vbs");
- out = new FileOutputStream(elevate);
- in = getClass().getResourceAsStream("/lib/elevate.vbs");
- Updater.copyFile(in, out);
-
- pb.directory(temp);
- Process p = pb.start();
- } catch(Exception e) {
- e.printStackTrace();
- }
- System.exit(0);
- } else {
- JOptionPane.showMessageDialog(null,"Please run this installer while logged in as an Administrator.");
- System.exit(0);
- }
- }
- }
- }
-
- /**
- * Create the installer GUI
- */
- public Installer(String[] args) {
- super("PCTB Installer");
-
- // gui: simple window with status area and progress bar
- checkPermission(args);
- label = new JLabel("Ready to install!");
- add(label,BorderLayout.NORTH);
- progress = new JProgressBar();
- progress.setBorder(new EmptyBorder(10,0,10,0));
- add(progress,BorderLayout.CENTER);
- String buttonText = "Install";
- install = new JButton(buttonText);
- JPanel buttons = new JPanel();
- add(buttons,BorderLayout.SOUTH);
- install.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(installed) {
- System.exit(0);
- }
- try {
- install.setEnabled(false);
- uninstall.setEnabled(false);
- install();
- } catch(IOException err) {
- err.printStackTrace();
- JOptionPane.showMessageDialog(Installer.this, "Error during installation.");
- setButtonStatus(false,false);
- label.setText("Install failed!");
- }
- }
- });
- uninstall = new JButton("Uninstall");
- buttons.add(uninstall);
- buttons.add(install);
- uninstall.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(uninstalled) {
- System.exit(0);
- }
- try {
- install.setEnabled(false);
- uninstall.setEnabled(false);
- uninstall();
- } catch(IOException err) {
- err.printStackTrace();
- JOptionPane.showMessageDialog(Installer.this, "Error during installation.");
- setButtonStatus(false,false);
- label.setText("Uninstall failed!");
- }
- }
- });
- getRootPane().setBorder(new EmptyBorder(10,10,10,10));
- getRootPane().setDefaultButton(install);
- pack();
- setSize(300,getHeight());
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
-
- /**
- * Uninstalls the client. Throws an IOException ir errors occur.
- * (removes the PCTB class from assistive tech, deletes the jar)
- */
- private void uninstall() throws IOException {
- label.setText("Uninstalling...");
- progress.setIndeterminate(true);
- // remove from assistive tech
- File java_home = getJavaHome();
- File a11y = new File(java_home,"lib/accessibility.properties");
- if(a11y.exists()) {
- Properties p = new Properties();
- p.load(new FileInputStream(a11y));
- String tech = p.getProperty("assistive_technologies");
- tech = tech.replace("com.tedpearson.ypp.market.MarketUploader", "");
- p.setProperty("assistive_technologies", tech);
- p.store(new FileOutputStream(a11y),"Last Modified by PCTB-Installer");
- }
- // remove jar
- File jar = new File(java_home, "lib/ext/PCTB-Uploader.jar");
- jar.delete();
- setButtonStatus(false,true);
- }
-
- /**
- * Utility method to find the Java Home folder that runs YPP. On Windows, it could be
- * inside the YPP folder.
- */
- private File getJavaHome() {
- File java_home = new File(home);
- if(os.contains("Windows")) {
- File defaultLocation = null;
- // check for javavm inside YPP folder, otherwise default location
- if(os.contains("Vista")) {
- String user = System.getProperty("user.name");
- defaultLocation = new File("C:\\Users\\" + user + "\\AppData\\Roaming\\Three Rings Design\\Puzzle Pirates\\");
- } else if(os.contains("XP")) {
- defaultLocation = new File("C:\\Program Files\\Three Rings Design\\Puzzle Pirates\\");
- }
- if(defaultLocation != null && defaultLocation.exists()) {
- File java_vm = new File(defaultLocation, "java_vm");
- if(java_vm.exists()) {
- // this is where we want to install instead
- java_home = new File(defaultLocation, "java_vm");
- }
- }
- }
- return java_home;
- }
-
- /**
- * Installs the client.
- */
- private void install() throws IOException {
- label.setText("Installing...");
- progress.setIndeterminate(true);
- File java_home = getJavaHome();
- File controlPanel = new File(user_home);
- JFileChooser chooser = new JFileChooser(controlPanel);
- chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- chooser.setDialogTitle("Choose location for Control Panel");
- chooser.setApproveButtonText("Install Control Panel here");
- int state = chooser.showOpenDialog(this);
- if(state == JFileChooser.APPROVE_OPTION) {
- controlPanel = chooser.getSelectedFile();
- } else {
- setButtonStatus(false,false);
- label.setText("Install failed!");
- return;
- }
- InputStream in = getClass().getResourceAsStream("/PCTB-ControlPanel.jar");
- File newFile = new File(controlPanel, "PCTB-ControlPanel.jar");
- newFile.createNewFile();
- OutputStream out = new FileOutputStream(newFile);
- Updater.copyFile(in, out);
- String search = "assistive_technologies";
- String value = "com.tedpearson.ypp.market.MarketUploader";
- File a11y = new File(java_home,"lib/accessibility.properties");
- boolean skipA11y = false;
- Properties p = new Properties();
- if(a11y.exists()) {
- // if already contains our modification, ignore
- // else add our modification
- p.load(new FileInputStream(a11y));
- String tech = p.getProperty(search);
- if(tech == null || tech.trim().equals("")) {
- // add it!
- p.setProperty(search, value);
- p.store(new FileOutputStream(a11y),"Last Modified by PCTB-Installer");
- } else if(!tech.contains(value)) {
- p.setProperty(search, tech+","+value);
- p.store(new FileOutputStream(a11y),"Last Modified by PCTB-Installer");
- }
- } else {
- // create file with our modification
- a11y.createNewFile();
- p.setProperty(search, value);
- p.store(new FileOutputStream(a11y),"Last Modified by PCTB-Installer");
- }
-
- // install program
- // copy jar from resource to new file in ext
- //String installer = getClass().getProtectionDomain().getCodeSource().getLocation()
- // .getPath().replaceAll("%20"," ");
- //new FileInputStream(installer);
- in = getClass().getResourceAsStream("/PCTB-Uploader.jar");
- newFile = new File(java_home, "lib/ext/PCTB-Uploader.jar");
- newFile.createNewFile();
- out = new FileOutputStream(newFile);
- Updater.copyFile(in, out);
- JOptionPane.showMessageDialog(this, "Install successful!\n\nWhen you open a new YPP client, you'll see\n" +
- "the upload client window alongside YPP.\n\n" +
- "To stop the window from appearing, use\n" +
- "the Control Panel to disable it, or uninstall.", "Success!", JOptionPane.INFORMATION_MESSAGE);
- setButtonStatus(true,false);
- }
-
- /**
- * Cleanup after an install, uninstall, or error. Buttons, statuses, and such
- */
- private void setButtonStatus(boolean installQ, boolean uninstallQ) {
- progress.setIndeterminate(false);
- progress.setValue(100);
- install.setEnabled(true);
- uninstall.setEnabled(true);
- String q = "Quit";
- if(installQ) {
- install.setText(q);
- installed = true;
- label.setText("Install complete!");
- } else {
- install.setText("Install");
- installed = false;
- }
-
- if(uninstallQ) {
- uninstall.setText(q);
- uninstalled = true;
- label.setText("Uninstall successful.");
- } else {
- uninstall.setText("Uninstall");
- uninstalled = false;
- }
- }
-}