001: /*
002: * UpdateCheck.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.util.Calendar;
017: import java.util.Date;
018: import workbench.gui.WbSwingUtilities;
019: import workbench.log.LogMgr;
020: import workbench.resource.ResourceMgr;
021: import workbench.resource.Settings;
022:
023: /**
024: * @author support@sql-workbench.net
025: */
026: public class UpdateCheck implements ActionListener {
027: private WbVersionReader versionReader;
028:
029: public UpdateCheck() {
030: }
031:
032: public void startUpdateCheck() {
033: int interval = Settings.getInstance().getUpdateCheckInterval();
034: if (interval < 1)
035: return;
036:
037: Date lastCheck = Settings.getInstance().getLastUpdateCheck();
038:
039: if (needCheck(interval, new java.util.Date(), lastCheck)) {
040: startRead();
041: }
042: }
043:
044: /**
045: * This is so that the method is accessible for Unit-Testing
046: */
047: public boolean needCheck(int interval, Date today, Date lastCheck) {
048: if (interval < 1)
049: return false;
050: Calendar next = Calendar.getInstance();
051: long nextCheck = Long.MIN_VALUE;
052: if (lastCheck != null) {
053: next.setTime(lastCheck);
054: next.set(Calendar.HOUR_OF_DAY, 0);
055: next.clear(Calendar.MINUTE);
056: next.clear(Calendar.SECOND);
057: next.clear(Calendar.MILLISECOND);
058: next.add(Calendar.DAY_OF_MONTH, interval);
059: nextCheck = next.getTimeInMillis();
060: }
061:
062: Calendar now = Calendar.getInstance();
063: now.setTime(today);
064: now.set(Calendar.HOUR_OF_DAY, 0);
065: now.clear(Calendar.MINUTE);
066: now.clear(Calendar.SECOND);
067: now.clear(Calendar.MILLISECOND);
068:
069: long nowMillis = now.getTimeInMillis();
070:
071: return nextCheck <= nowMillis;
072: }
073:
074: public void startRead() {
075: LogMgr.logDebug("UpdateCheck.run()", "Checking versions...");
076: this .versionReader = new WbVersionReader("automatic ", this );
077: versionReader.startCheckThread();
078: }
079:
080: private void versionAvailable() {
081: try {
082: LogMgr.logDebug("UpdateCheck.run()",
083: "Current stable version: "
084: + versionReader.getStableBuildNumber());
085: LogMgr.logDebug("UpdateCheck.run()",
086: "Current development version: "
087: + versionReader.getDevBuildNumber());
088:
089: UpdateVersion update = this .versionReader
090: .getAvailableUpdate();
091: NotifierEvent event = null;
092: if (update == UpdateVersion.stable) {
093: LogMgr.logInfo("UpdateCheck.run()",
094: "New stable version available");
095: event = new NotifierEvent("updates", ResourceMgr
096: .getString("LblVersionNewStableAvailable"),
097: this );
098: } else if (update == UpdateVersion.devBuild) {
099: LogMgr.logInfo("UpdateCheck.run()",
100: "New dev build available");
101: event = new NotifierEvent("updates", ResourceMgr
102: .getString("LblVersionNewDevAvailable"), this );
103: } else {
104: LogMgr.logInfo("UpdateCheck.run()", "No updates found");
105: }
106:
107: if (event != null) {
108: EventNotifier.getInstance().displayNotification(event);
109: }
110:
111: if (this .versionReader.success()) {
112: try {
113: Settings.getInstance().setLastUpdateCheck();
114: } catch (Exception e) {
115: LogMgr.logError("UpdateCheck.run()",
116: "Error when updating last update date", e);
117: }
118: }
119: } catch (Exception e) {
120: LogMgr.logError("UpdateCheck.run()",
121: "Could not check for updates", e);
122: }
123: }
124:
125: public void actionPerformed(ActionEvent e) {
126: if (e.getSource() == this .versionReader) {
127: versionAvailable();
128: return;
129: }
130: try {
131: EventNotifier.getInstance().removeNotification();
132: BrowserLauncher.openURL("http://www.sql-workbench.net");
133: } catch (Exception ex) {
134: WbSwingUtilities.showMessage(null,
135: "Could not open browser ("
136: + ExceptionUtil.getDisplay(ex) + ")");
137: }
138: }
139: }
|