From 77703cc395af68adb6827482d94c7288fb15aad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Sun, 5 May 2013 16:07:23 +0200 Subject: [PATCH] better update system --- .../src/com/jpexs/decompiler/flash/Main.java | 138 ++++++++++-------- trunk/src/com/jpexs/decompiler/flash/SWF.java | 2 +- .../com/jpexs/decompiler/flash/Version.java | 37 +++++ .../flash/gui/NewVersionDialog.java | 137 +++++++++++++++++ 4 files changed, 252 insertions(+), 62 deletions(-) create mode 100644 trunk/src/com/jpexs/decompiler/flash/Version.java create mode 100644 trunk/src/com/jpexs/decompiler/flash/gui/NewVersionDialog.java diff --git a/trunk/src/com/jpexs/decompiler/flash/Main.java b/trunk/src/com/jpexs/decompiler/flash/Main.java index 26dae1de2..5aefc1cc6 100644 --- a/trunk/src/com/jpexs/decompiler/flash/Main.java +++ b/trunk/src/com/jpexs/decompiler/flash/Main.java @@ -21,6 +21,7 @@ import com.jpexs.decompiler.flash.gui.AboutDialog; import com.jpexs.decompiler.flash.gui.LoadingDialog; import com.jpexs.decompiler.flash.gui.MainFrame; import com.jpexs.decompiler.flash.gui.ModeFrame; +import com.jpexs.decompiler.flash.gui.NewVersionDialog; import com.jpexs.decompiler.flash.gui.View; import com.jpexs.decompiler.flash.gui.player.FlashPlayerPanel; import com.jpexs.decompiler.flash.gui.proxy.ProxyFrame; @@ -31,6 +32,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.*; import java.net.Socket; +import java.util.ArrayList; import java.util.Calendar; import java.util.Properties; import java.util.logging.ConsoleHandler; @@ -45,11 +47,6 @@ import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingWorker; import javax.swing.filechooser.FileFilter; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; /** * Main executable class @@ -68,7 +65,7 @@ public class Main { public static final String shortApplicationName = "FFDec"; public static String shortApplicationVerName; public static final String projectPage = "http://www.free-decompiler.com/flash"; - public static String updatePageStub = "http://www.free-decompiler.com/flash/?update="; + public static String updatePageStub = "http://www.free-decompiler.com/flash/update.html?currentVersion="; public static String updatePage; public static final String vendor = "JPEXS"; public static LoadingDialog loadingDialog; @@ -78,6 +75,8 @@ public class Main { private static MenuItem stopMenuItem; private static boolean commandLineMode = false; public static MainFrame mainFrame; + private static final int UPDATE_SYSTEM_MAJOR = 1; + private static final int UPDATE_SYSTEM_MINOR = 0; private static void loadProperties() { Properties prop = new Properties(); @@ -768,74 +767,91 @@ public class Main { public static boolean checkForUpdates() { try { - Socket sock = new Socket("code.google.com", 80); + Socket sock = new Socket("www.free-decompiler.com", 80); OutputStream os = sock.getOutputStream(); - os.write("GET /feeds/p/asdec/downloads/basic HTTP/1.1\r\nHost: code.google.com\r\nConnection: close\r\n\r\n".getBytes()); + os.write(("GET /flash/update.html?action=check¤tVersion=" + version + " HTTP/1.1\r\nHost: www.free-decompiler.com\r\nUser-Agent: " + shortApplicationVerName + "\r\nConnection: close\r\n\r\n").getBytes()); BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); String s; - String response = ""; boolean start = false; + java.util.List versions = new ArrayList(); + String header = ""; + Pattern headerPat = Pattern.compile("\\[([a-zA-Z0-9]+)\\]"); + int updateMajor = 0; + int updateMinor = 0; + Version ver = null; while ((s = br.readLine()) != null) { if (start) { - response += s + "\r\n"; + Matcher m = headerPat.matcher(s); + if (m.matches()) { + header = m.group(1); + if (header.equals("version")) { + ver = new Version(); + versions.add(ver); + } + if (header.equals("noversion")) { + break; + } + } else { + if (s.contains("=")) { + String key = s.substring(0, s.indexOf("=")); + String val = s.substring(s.indexOf("=") + 1); + if ("updateSystem".equals(header)) { + if (key.equals("majorVersion")) { + updateMajor = Integer.parseInt(val); + if (updateMajor > UPDATE_SYSTEM_MAJOR) { + break; + } + } + if (key.equals("minorVersion")) { + updateMinor = Integer.parseInt(val); + } + } + if ("version".equals(header) && (ver != null)) { + if (key.equals("versionId")) { + ver.versionId = Integer.parseInt(val); + } + if (key.equals("versionName")) { + ver.versionName = val; + } + if (key.equals("longVersionName")) { + ver.longVersionName = val; + } + if (key.equals("releaseDate")) { + ver.releaseDate = val; + } + if (key.equals("appName")) { + ver.appName = val; + } + if (key.equals("appFullName")) { + ver.appFullName = val; + } + if (key.equals("updateLink")) { + ver.updateLink = val; + } + if (key.equals("change[]")) { + String changeType = val.substring(0, val.indexOf("|")); + String change = val.substring(val.indexOf("|") + 1); + if (!ver.changes.containsKey(changeType)) { + ver.changes.put(changeType, new ArrayList()); + } + java.util.List chlist = ver.changes.get(changeType); + chlist.add(change); + } + } + } + } } if (s.equals("")) { start = true; } } - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document doc = builder.parse(new ByteArrayInputStream(response.getBytes())); - NodeList contents = doc.getElementsByTagName("content"); - for (int i = 0; i < contents.getLength(); i++) { - Node nod = contents.item(i); - String cont = nod.getTextContent().trim(); - String parts[] = cont.split("\n"); - boolean isUpdate = false; - for (String part : parts) { - if (part.trim().equals("Update")) { - isUpdate = true; - break; - } - } - if ((parts.length > 4) && (isUpdate)) { - String downloadName = parts[1]; - String link = parts[parts.length - 2]; - if (isUpdate) { - String downVersion = "NEW"; - if (downloadName.startsWith(shortApplicationName + " version ")) { - downVersion = downloadName.substring((shortApplicationName + " version ").length()); - if (downVersion.contains(" ")) { - downVersion = downVersion.substring(0, downVersion.indexOf(" ")); - } - } - if (link.startsWith(". + */ +package com.jpexs.decompiler.flash; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + * @author JPEXS + */ +public class Version { + + public int versionId; + public String versionName; + public String longVersionName; + public String releaseDate; + public String appName; + public String appFullName; + public String updateLink; + public Map> changes = new HashMap>(); +} diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/NewVersionDialog.java b/trunk/src/com/jpexs/decompiler/flash/gui/NewVersionDialog.java new file mode 100644 index 000000000..053b87d9c --- /dev/null +++ b/trunk/src/com/jpexs/decompiler/flash/gui/NewVersionDialog.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2013 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.decompiler.flash.gui; + +import com.jpexs.decompiler.flash.Main; +import static com.jpexs.decompiler.flash.Main.projectPage; +import static com.jpexs.decompiler.flash.Main.shortApplicationName; +import com.jpexs.decompiler.flash.Version; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.SwingConstants; +import javax.swing.UIManager; + +/** + * + * @author JPEXS + */ +public class NewVersionDialog extends JDialog implements ActionListener { + + Version latestVersion; + + public NewVersionDialog(List versions) { + setSize(new Dimension(500, 300)); + Container cnt = getContentPane(); + cnt.setLayout(new BoxLayout(cnt, BoxLayout.PAGE_AXIS)); + JTextArea changesText = new JTextArea(); + changesText.setEditable(false); + changesText.setFont(UIManager.getFont("TextField.font")); + String changesStr = ""; + for (Version v : versions) { + changesStr += "version " + v.versionName + "\r\n"; + changesStr += "-----------------------\r\n"; + changesStr += "Release date:" + v.releaseDate + "\r\n"; + for (String type : v.changes.keySet()) { + changesStr += type + ":" + "\r\n"; + for (String ch : v.changes.get(type)) { + changesStr += " - " + ch + "\r\n"; + } + } + changesStr += "\r\n"; + } + latestVersion = null; + if (!versions.isEmpty()) { + latestVersion = versions.get(0); + } + changesText.setText(changesStr); + JLabel newAvailableLabel = new JLabel("
New version is available: " + latestVersion.appName + " version " + latestVersion.versionName + "
", SwingConstants.CENTER); + newAvailableLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); + cnt.add(newAvailableLabel); + + JLabel changeslogLabel = new JLabel("Changeslog:"); + changeslogLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); + cnt.add(changeslogLabel); + + JScrollPane span = new JScrollPane(changesText); + span.setAlignmentX(JLabel.CENTER_ALIGNMENT); + cnt.add(span); + JPanel buttonsPanel = new JPanel(new FlowLayout()); + JButton buttonOk = new JButton("OK"); + buttonOk.setActionCommand("OK"); + buttonOk.addActionListener(this); + + JButton buttonCancel = new JButton("Cancel"); + buttonCancel.setActionCommand("CANCEL"); + buttonCancel.addActionListener(this); + + buttonsPanel.add(buttonOk); + buttonsPanel.add(buttonCancel); + buttonsPanel.setAlignmentX(JLabel.CENTER_ALIGNMENT); + + JLabel downloadNowLabel = new JLabel("
Download now?
", SwingConstants.CENTER); + downloadNowLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); + cnt.add(downloadNowLabel); + cnt.add(buttonsPanel); + + setResizable(false); + setTitle("New version available"); + this.getRootPane().setDefaultButton(buttonOk); + View.centerScreen(this); + setModalityType(ModalityType.APPLICATION_MODAL); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getActionCommand().equals("OK")) { + java.awt.Desktop desktop = null; + if (java.awt.Desktop.isDesktopSupported()) { + desktop = java.awt.Desktop.getDesktop(); + if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) { + try { + if (latestVersion.updateLink != null) { + java.net.URI uri = new java.net.URI(latestVersion.updateLink); + desktop.browse(uri); + } else { + java.net.URI uri = new java.net.URI(Main.updatePage); + desktop.browse(uri); + } + Main.exit(); + } catch (Exception ex) { + } + } else { + desktop = null; + } + } + if (desktop == null) { + JOptionPane.showMessageDialog(null, "New version of " + shortApplicationName + " is available: " + latestVersion.appName + ".\r\nPlease go to " + projectPage + " to download it.", "New version", JOptionPane.INFORMATION_MESSAGE); + } + } + setVisible(false); + } +}