[blam] Channel: move the update logic into the class
- From: Carlos Martín Nieto <cmartin src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [blam] Channel: move the update logic into the class
- Date: Sat, 15 Jun 2013 14:32:16 +0000 (UTC)
commit c4a47e975f2427e6b1ed08029fec7bb1d000b77c
Author: Carlos Martín Nieto <cmn dwim me>
Date: Sat Jun 15 16:29:50 2013 +0200
Channel: move the update logic into the class
Reduce the amount of try-catch blocks we need and simplify the
logic. This also brings back ignoring SSL errors which got lost at
some point in the past.
blam.csproj | 2 +-
src/Channel.cs | 48 ++++++++++++++++++++++++++++++++----
src/ChannelGroup.cs | 2 +-
src/FeedUpdater.cs | 66 ---------------------------------------------------
src/Filter.cs | 26 ++++++--------------
src/Makefile.am | 2 +-
6 files changed, 53 insertions(+), 93 deletions(-)
---
diff --git a/blam.csproj b/blam.csproj
index 1e0f3ab..aa296e3 100644
--- a/blam.csproj
+++ b/blam.csproj
@@ -77,6 +77,7 @@
<Private>False</Private>
<Package>gconf-sharp-2.0</Package>
</Reference>
+ <Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Include="src\Application.cs" />
@@ -87,7 +88,6 @@
<Compile Include="src\Conf.cs" />
<Compile Include="src\Delegates.cs" />
<Compile Include="src\Dialogs.cs" />
- <Compile Include="src\FeedUpdater.cs" />
<Compile Include="src\Filter.cs" />
<Compile Include="src\HigUtils.cs" />
<Compile Include="src\HtmlUtils.cs" />
diff --git a/src/Channel.cs b/src/Channel.cs
index fd477ca..b2f3b1f 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -7,9 +7,14 @@
using System.Collections;
using System;
using System.Net;
+using System.Net.Http;
+using System.Net.Security;
+using System.Xml;
using System.Xml.Serialization;
+using System.IO;
using System.Threading.Tasks;
using System.ServiceModel.Syndication;
+using System.Security.Cryptography.X509Certificates;
namespace Imendio.Blam {
@@ -26,7 +31,6 @@ namespace Imendio.Blam {
void Setup();
event ChannelEventHandler Updated;
void RemoveItems();
- bool Refresh();
Task<bool> RefreshAsync();
}
@@ -296,15 +300,47 @@ namespace Imendio.Blam {
return false;
}
- delegate bool RefreshDelegate();
- public bool Refresh()
+ bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
- return FeedUpdater.Update(this);
+ return Conf.Get(Preference.IGNORE_SSL_ERR, false);
}
- public Task<bool> RefreshAsync()
+ /// <summary>
+ /// Loads the string as an SyndicationFee. It will try to clean it up once. If the clean up
attempt fails,
+ /// it will let SyndicationFeed.Load() throw its exception.
+ /// </summary>
+ /// <returns>The feed.</returns>
+ /// <param name="str">The XML string to load</param>
+ SyndicationFeed LoadFeed(string str)
{
- return Task<bool>.Factory.StartNew(() => Refresh());
+ SyndicationFeed feed = null;
+ try {
+ var xml = XmlReader.Create(new StringReader(str));
+ feed = SyndicationFeed.Load(xml);
+ } catch (Exception) {
+ str = Filter.FilterString(str);
+ var xml = XmlReader.Create(new StringReader(str));
+ feed = SyndicationFeed.Load(xml);
+ }
+
+ return feed;
+ }
+
+ public async Task<bool> RefreshAsync()
+ {
+ using (var http = new HttpClient())
+ {
+ ServicePointManager.ServerCertificateValidationCallback = ValidateCertificate;
+ try {
+ var xmlstring = await http.GetStringAsync(Url);
+ var feed = LoadFeed(xmlstring);
+
+ return Update(feed);
+ } catch (Exception e) {
+ Console.WriteLine("Failed to get feed {0}: {1}", Name, e.Message);
+ return false;
+ }
+ }
}
}
}
diff --git a/src/ChannelGroup.cs b/src/ChannelGroup.cs
index 5f8efb2..cafb66e 100644
--- a/src/ChannelGroup.cs
+++ b/src/ChannelGroup.cs
@@ -163,7 +163,7 @@ namespace Imendio.Blam
public delegate bool RefreshDelegate();
public bool Refresh()
{
- Parallel.ForEach(Channels.Cast<Channel>(), c => c.Refresh());
+ Parallel.ForEach(Channels.Cast<Channel>(), c => c.RefreshAsync());
return true; // Whatever
}
diff --git a/src/Filter.cs b/src/Filter.cs
index 5389def..93f3654 100644
--- a/src/Filter.cs
+++ b/src/Filter.cs
@@ -6,22 +6,12 @@ using System.IO;
namespace Imendio.Blam
{
- public class Filter
- {
- static public XmlTextReader FromUrl(string url)
- {
- string orig;
- try {
- WebClient client = new WebClient();
- orig = client.DownloadString(url);
- } catch (Exception ex) {
- Console.Error.WriteLine("Can't download {0}: {1}", url, ex.Message);
- return null;
- }
-
- Regex regex = new Regex(@"\x0C|\x0E");
- string result = regex.Replace(orig, String.Empty);
- return new XmlTextReader(new StringReader(result));
- }
- }
+ public class Filter
+ {
+ public static string FilterString(string str)
+ {
+ Regex regex = new Regex(@"\x0C|\x0E");
+ return regex.Replace(str, String.Empty);
+ }
+ }
}
diff --git a/src/Makefile.am b/src/Makefile.am
index caa8d73..dd80f0b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,6 +11,7 @@ RESOURCES = \
ASSEMBLIES = \
-r:System.Web \
-r:Mono.Posix \
+ -r:System.Net.Http \
-r:System.ServiceModel
BLAM_CSFILES = Application.cs \
@@ -23,7 +24,6 @@ BLAM_CSFILES = Application.cs \
Conf.cs \
Delegates.cs \
Dialogs.cs \
- FeedUpdater.cs \
GLibSynchronizationContext.cs \
HigUtils.cs \
HtmlUtils.cs \
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]