/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* 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 1 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 .
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using log4net;
namespace GreenshotPlugin.Core {
public class RssFile {
private readonly string _file;
public string File {
get {return _file;}
}
private readonly DateTime _pubdate;
public DateTime Pubdate {
get {return _pubdate;}
}
private readonly string _link;
public string Link {
get {return _link;}
}
private Version _version;
public Version Version {
get {return _version;}
set {
_version = value;
}
}
private string _language;
public string Language {
get {return _language;}
set {_language = value;}
}
public bool isExe {
get {
if (_file != null) {
return _file.ToLower().EndsWith(".exe");
}
return false;
}
}
public bool isUnstable {
get {
if (_file != null) {
return _file.ToLower().Contains("unstable");
}
return false;
}
}
public bool isReleaseCandidate {
get {
if (_file != null) {
return Regex.IsMatch(_file.ToLower(), "rc[0-9]+");
}
return false;
}
}
public RssFile(string file, string pubdate, string link) {
_file = file;
if (!DateTime.TryParse(pubdate, out _pubdate))
{
DateTime.TryParseExact(pubdate.Replace(" UT", ""), "ddd, dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out _pubdate);
}
_link = link;
}
}
///
/// Description of RssHelper.
///
public class RssHelper {
private static ILog LOG = LogManager.GetLogger(typeof(RssHelper));
private const string RSSFEED = "http://getgreenshot.org/project-feed/";
///
/// This is using the HTTP HEAD Method to check if the RSS Feed is modified after the supplied date
///
/// DateTime
/// true if the feed is newer
public static bool IsRSSModifiedAfter(DateTime updateTime) {
DateTime lastModified = NetworkHelper.GetLastModified(new Uri(RSSFEED));
if (lastModified == DateTime.MinValue)
{
// Time could not be read, just take now and add one hour to it.
// This assist BUG-1850
lastModified = DateTime.Now.AddHours(1);
}
return updateTime.CompareTo(lastModified) < 0;
}
///
/// Read the Greenshot RSS feed, so we can use this information to check for updates
///
/// List with RssFile(s)
public static IList readRSS() {
XmlDocument rssDoc = new XmlDocument();
try {
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(RSSFEED);
XmlTextReader rssReader = new XmlTextReader(webRequest.GetResponse().GetResponseStream());
// Load the XML content into a XmlDocument
rssDoc.Load(rssReader);
} catch (Exception wE) {
LOG.WarnFormat("Problem reading RSS from {0}", RSSFEED);
LOG.Warn(wE.Message);
return null;
}
// Loop for the tag
XmlNode nodeRss = null;
for (int i = 0; i < rssDoc.ChildNodes.Count; i++) {
// If it is the rss tag
if (rssDoc.ChildNodes[i].Name == "rss") {
// tag found
nodeRss = rssDoc.ChildNodes[i];
}
}
if (nodeRss == null) {
LOG.Debug("No RSS Feed!");
return null;
}
// Loop for the tag
XmlNode nodeChannel = null;
for (int i = 0; i < nodeRss.ChildNodes.Count; i++) {
// If it is the channel tag
if (nodeRss.ChildNodes[i].Name == "channel") {
// tag found
nodeChannel = nodeRss.ChildNodes[i];
}
}
if (nodeChannel == null) {
LOG.Debug("No channel in RSS feed!");
return null;
}
IList rssFiles = new List();
// Loop for the , , and all the other tags
for (int i = 0; i < nodeChannel.ChildNodes.Count; i++) {
// If it is the item tag, then it has children tags which we will add as items to the ListView
if (nodeChannel.ChildNodes[i].Name == "item") {
XmlNode nodeItem = nodeChannel.ChildNodes[i];
string link = nodeItem["link"].InnerText;
string pubdate = nodeItem["pubDate"].InnerText;
try {
Match match= Regex.Match(Uri.UnescapeDataString(link), @"^.*\/(Greenshot.+)\/download$");
if (match.Success) {
string file = match.Groups[1].Value;
RssFile rssFile = new RssFile(file, pubdate, link);
if (file.EndsWith(".exe") ||file.EndsWith(".zip")) {
string version = Regex.Replace(file, @".*[a-zA-Z_]\-", "");
version = version.Replace(@"\-[a-zA-Z]+.*","");
version = Regex.Replace(version, @"\.exe$", "");
version = Regex.Replace(version, @"\.zip$", "");
version = Regex.Replace(version, @"RC[0-9]+", "");
if (version.Trim().Length > 0) {
version = version.Replace('-','.');
version = version.Replace(',','.');
version = Regex.Replace(version, @"^[a-zA-Z_]*\.", "");
version = Regex.Replace(version, @"\.[a-zA-Z_]*$", "");
try {
rssFile.Version = new Version(version);
} catch (Exception) {
LOG.DebugFormat("Found invalid version {0} in file {1}", version, file);
}
}
rssFiles.Add(rssFile);
}
}
} catch (Exception ex) {
LOG.WarnFormat("Couldn't read RSS entry for: {0}", nodeChannel["title"].InnerText);
LOG.Warn("Reason: ", ex);
}
}
}
return rssFiles;
}
}
}