Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cmdline home #43

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 121 additions & 94 deletions src/de/relaunch64/popelganda/CheckForUpdates.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,114 +32,141 @@
*/
package de.relaunch64.popelganda;

import de.relaunch64.popelganda.util.ConstantsR64;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;

import de.relaunch64.popelganda.util.ConstantsR64;

/**
*
* @author Daniel Lüdecke
*/
public class CheckForUpdates extends org.jdesktop.application.Task<Object, Void> {
public class CheckForUpdates
extends org.jdesktop.application.Task<Object, Void>
{

// indicates whether the zettelkasten has updates or not.
// indicates whether the zettelkasten has updates or not.

boolean updateavailable = false;
String updateBuildNr = null;
boolean updateavailable = false;
String updateBuildNr = null;

CheckForUpdates(org.jdesktop.application.Application app) {
// Runs on the EDT. Copy GUI state that
// doInBackground() depends on from parameters
// to ImportFileTask fields, here.
super(app);
}
CheckForUpdates(org.jdesktop.application.Application app)
{
// Runs on the EDT. Copy GUI state that
// doInBackground() depends on from parameters
// to ImportFileTask fields, here.
super(app);
}

/**
* Opens and reads the file {@code updatetext} to get information about the
* latest releases.
*
* @param updatetext an URL to the (text-)file containg the update
* information. See ConstantsR64.UPDATE_INFO_URI.
*
* @return the content of the (text-)file
*/
protected String accessUpdateFile(URL updatetext) {
// input stream that will read the update text
InputStream is = null;
// stringbuilder that will contain the content of the update-file
StringBuilder updateinfo = new StringBuilder("");
try {
// open update-file on server
is = updatetext.openStream();
// buffer for stream
int buff = 0;
// read update-file and copy content to string builder
while (buff != -1) {
buff = is.read();
if (buff != -1) {
updateinfo.append((char) buff);
}
}
} catch (IOException e) {
// tell about fail
ConstantsR64.r64logger.log(Level.INFO, "No access to Relaunch64-Website. Automatic update-check failed.");
updateavailable = false;
return null;
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
}
}
return updateinfo.toString()
.replace("\n", "")
.replace("\r", "")
.trim();
}
/**
* Opens and reads the file {@code updatetext} to get information about the
* latest releases.
*
* @param updatetext
* an URL to the (text-)file containg the update information. See
* ConstantsR64.UPDATE_INFO_URI.
*
* @return the content of the (text-)file
*/
protected String accessUpdateFile(URL updatetext)
{
// input stream that will read the update text
InputStream is = null;
// stringbuilder that will contain the content of the update-file
StringBuilder updateinfo = new StringBuilder("");
try
{
// open update-file on server
is = updatetext.openStream();
// buffer for stream
int buff = 0;
// read update-file and copy content to string builder
while (buff != -1)
{
buff = is.read();
if (buff != -1)
{
updateinfo.append((char) buff);
}
}
}
catch (IOException e)
{
// tell about fail
ConstantsR64.r64logger.log(Level.INFO,
"No access to Relaunch64-Website. Automatic update-check failed.");
updateavailable = false;
return null;
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch (IOException e)
{
}
}
return updateinfo.toString().replace("\n", "").replace("\r", "").trim();
}

/**
* This thread accesses the update information file and checks whether new
* releases are available. f yes, an information will be shown in the log
* window.
*
* @return always {@code null}.
* @throws IOException
*/
@Override
protected Object doInBackground() throws IOException {
// Your Task's code here. This method runs
// on a background thread, so don't reference
// the Swing GUI from here.
updateBuildNr = accessUpdateFile(new URL(ConstantsR64.UPDATE_INFO_URI));
// check for valid access
if (null == updateBuildNr || updateBuildNr.isEmpty()) {
return null;
}
// check whether there's a newer version online
updateavailable = (ConstantsR64.BUILD_NUMBER.compareTo(updateBuildNr) < 0);
return null; // return your result
}
/**
* This thread accesses the update information file and checks whether new
* releases are available. f yes, an information will be shown in the log
* window.
*
* @return always {@code null}.
* @throws IOException
*/
@Override
protected Object doInBackground() throws IOException
{
// Your Task's code here. This method runs
// on a background thread, so don't reference
// the Swing GUI from here.
updateBuildNr = accessUpdateFile(new URL(ConstantsR64.UPDATE_INFO_URI));
// check for valid access
if (null == updateBuildNr || updateBuildNr.isEmpty())
{
return null;
}
// check whether there's a newer version online
updateavailable = (ConstantsR64.BUILD_NUMBER.compareTo(updateBuildNr) < 0);
return null; // return your result
}

@Override
protected void succeeded(Object result) {
// Runs on the EDT. Update the GUI based on
// the result computed by doInBackground().
}
@Override
protected void succeeded(Object result)
{
// Runs on the EDT. Update the GUI based on
// the result computed by doInBackground().
}

@Override
protected void finished() {
if (updateavailable) {
//log info
ConstantsR64.r64logger.log(Level.INFO, ("A new version of Relaunch64 is available!" + System.lineSeparator() + "Download from " + ConstantsR64.UPDATE_URI));
} else {
// log latest available build
if (updateBuildNr != null) {
ConstantsR64.r64logger.log(Level.INFO, ("Latest stable Relaunch64-build: " + updateBuildNr));
}
}
}
@Override
protected void finished()
{
if (updateavailable)
{
// log info
ConstantsR64.r64logger.log(Level.INFO,
("A new version of Relaunch64 is available!"
+ System.lineSeparator() + "Download from "
+ ConstantsR64.UPDATE_URI));
}
else
{
// log latest available build
if (updateBuildNr != null)
{
ConstantsR64.r64logger.log(Level.INFO,
("Latest stable Relaunch64-build: " + updateBuildNr));
}
}
}
}
Loading