-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Featues - Added http file downloader, integrated into TVRageProvider - Implemented 'check for updates' and preference option Code changes - FINALLY made preferences static (yay!) - Renaming of many UIStarter variables to conform to nameType - Moved string handling into StringUtils - Updated tests
- Loading branch information
1 parent
5c0dd4a
commit f594066
Showing
17 changed files
with
461 additions
and
293 deletions.
There are no files selected for viewing
Binary file not shown.
100 changes: 100 additions & 0 deletions
100
src/main/com/google/code/tvrenamer/controller/HttpConnectionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.google.code.tvrenamer.controller; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.zip.GZIPInputStream; | ||
import java.util.zip.Inflater; | ||
import java.util.zip.InflaterInputStream; | ||
|
||
import com.google.code.tvrenamer.controller.util.StringUtils; | ||
|
||
public class HttpConnectionHandler { | ||
private static Logger logger = Logger.getLogger(HttpConnectionHandler.class.getName()); | ||
private static final int CONNECT_TIMEOUT_MS = 2000; | ||
private static final int READ_TIMEOUT_MS = 5000; | ||
|
||
/** | ||
* Download the URL and return as a String | ||
* @param urlString the URL as a String | ||
* @return String of the contents | ||
*/ | ||
public String downloadUrl(String urlString) { | ||
try { | ||
return downloadUrl(new URL(urlString)); | ||
} catch (MalformedURLException e) { | ||
logger.log(Level.SEVERE, urlString + " is not a valid URL ", e); | ||
return ""; | ||
} | ||
} | ||
|
||
/** | ||
* Download the URL and return as a String. | ||
* Gzip handling from http://goo.gl/J88WG | ||
* | ||
* @param url | ||
* the URL to download | ||
* @return String of the URL contents | ||
* @throws IOException | ||
* when there is an error connecting or reading the URL | ||
*/ | ||
public String downloadUrl(URL url) { | ||
InputStream inputStream = null; | ||
StringBuilder contents = new StringBuilder(); | ||
|
||
try { | ||
if (url != null) { | ||
logger.info("Downloading URL " + url.toString()); | ||
|
||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
|
||
HttpURLConnection.setFollowRedirects(true); | ||
// allow both GZip and Deflate (ZLib) encodings | ||
conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); | ||
conn.setConnectTimeout(CONNECT_TIMEOUT_MS); | ||
conn.setReadTimeout(READ_TIMEOUT_MS); | ||
|
||
// create the appropriate stream wrapper based on the encoding type | ||
String encoding = conn.getContentEncoding(); | ||
if (encoding != null && encoding.equalsIgnoreCase("gzip")) { | ||
inputStream = new GZIPInputStream(conn.getInputStream()); | ||
} else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { | ||
inputStream = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); | ||
} else { | ||
inputStream = conn.getInputStream(); | ||
} | ||
|
||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
|
||
logger.finer("Before reading url stream"); | ||
|
||
String s; | ||
while ((s = reader.readLine()) != null) { | ||
contents.append(s); | ||
} | ||
|
||
if (logger.isLoggable(Level.FINEST)) { | ||
logger.finest("Url stream:\n" + StringUtils.encodeSpecialCharacters(contents.toString())); | ||
} | ||
} | ||
} catch (Exception e) { | ||
logger.log(Level.SEVERE, "Exception when attempting to download and parse URL " + url, e); | ||
} finally { | ||
try { | ||
if(inputStream != null) { | ||
inputStream.close(); | ||
} | ||
} catch (IOException e) { | ||
logger.log(Level.SEVERE, "Exception when attempting to close input stream", e); | ||
} | ||
} | ||
|
||
return StringUtils.encodeSpecialCharacters(contents.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/com/google/code/tvrenamer/controller/UpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.google.code.tvrenamer.controller; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import com.google.code.tvrenamer.model.util.Constants; | ||
|
||
public class UpdateChecker { | ||
private static Logger logger = Logger.getLogger(UpdateChecker.class.getName()); | ||
|
||
private static final String VERSION_URL = "http://r.ac.nz/tvrenamer.version"; | ||
|
||
/** | ||
* Checks if a newer version is available. | ||
* @return the new version number as a string if available, empty string if no new version or null if an error has occurred | ||
*/ | ||
public static boolean isUpdateAvailable() { | ||
String latestVersion = new HttpConnectionHandler().downloadUrl(VERSION_URL); | ||
|
||
boolean newVersionAvailable = latestVersion.compareToIgnoreCase(Constants.VERSION_NUMBER) > 0; | ||
|
||
if (newVersionAvailable) { | ||
logger.info("There is a new version available, running " + Constants.VERSION_NUMBER + ", new version is " + latestVersion); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tvrenamer/view/UpdateCompleteHandler.java → ...mer/controller/UpdateCompleteHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...mer/model/UserPreferencesChangeEvent.java → ...ontroller/UserPreferencesChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 5 additions & 8 deletions
13
...enamer/model/UserPreferencesListener.java → ...roller/UserPreferencesChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
package com.google.code.tvrenamer.model; | ||
package com.google.code.tvrenamer.controller; | ||
|
||
import java.util.Observable; | ||
import java.util.Observer; | ||
import java.util.logging.Logger; | ||
|
||
import com.google.code.tvrenamer.model.UserPreferences; | ||
import com.google.code.tvrenamer.view.UIStarter; | ||
|
||
public class UserPreferencesListener implements Observer { | ||
private static Logger logger = Logger.getLogger(UserPreferencesListener.class.getName()); | ||
public class UserPreferencesChangeListener implements Observer { | ||
private static Logger logger = Logger.getLogger(UserPreferencesChangeListener.class.getName()); | ||
|
||
/* (non-Javadoc) | ||
* @see java.util.Observer#update(java.util.Observable, java.lang.Object) | ||
*/ | ||
public void update(Observable observable, Object value) { | ||
logger.info("Preference change for: " + observable + ", " + value); | ||
logger.info("Preference change event: " + value); | ||
|
||
if(observable instanceof UserPreferences && value instanceof UserPreferencesChangeEvent) { | ||
UserPreferences preferences = (UserPreferences) observable; | ||
UserPreferencesChangeEvent upce = (UserPreferencesChangeEvent) value; | ||
|
||
if(upce.getPreference().equals("moveEnabled")) { | ||
UIStarter.setRenameButtonText(); | ||
UIStarter.setColumnDestText(); | ||
} else if(upce.getPreference().equals("proxy")) { | ||
preferences.getProxy().apply(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.