From 0ddd38e3f253d17f15f7675888c08ddc166d677e Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Thu, 15 Jun 2023 17:28:38 -0400 Subject: [PATCH] Add support for per-feed user agent --- Application/Entities.cs | 45 ++++++++++++------ Application/Feeds/Feed.cs | 50 +++++++++++++------- Application/Options/FeedWindow.xaml | 7 +++ Application/Options/GeneralOptionsPanel.xaml | 9 ++-- Application/Options/UserAgentItem.cs | 32 +++++++++++-- Application/Properties/Resources.Designer.cs | 20 +++++++- Application/Properties/Resources.resx | 6 +++ 7 files changed, 126 insertions(+), 43 deletions(-) diff --git a/Application/Entities.cs b/Application/Entities.cs index 4e61206..fc33045 100644 --- a/Application/Entities.cs +++ b/Application/Entities.cs @@ -1,22 +1,31 @@ -using FeedCenter.Data; +using System; +using System.Linq; +using FeedCenter.Data; using FeedCenter.Options; using Realms; -using System; -using System.Linq; namespace FeedCenter; public class FeedCenterEntities { - public Realm RealmInstance { get; } - - public RealmObservableCollection Categories { get; } - public RealmObservableCollection Feeds { get; private set; } - public RealmObservableCollection Settings { get; private set; } - public FeedCenterEntities() { - var realmConfiguration = new RealmConfiguration($"{Database.DatabaseFile}"); + var realmConfiguration = new RealmConfiguration($"{Database.DatabaseFile}") + { + SchemaVersion = 1, + MigrationCallback = (migration, oldSchemaVersion) => + { + var newVersionFeeds = migration.NewRealm.All(); + + foreach (var newVersionFeed in newVersionFeeds) + { + if (oldSchemaVersion == 0) + { + newVersionFeed.UserAgent = null; + } + } + } + }; RealmInstance = Realm.GetInstance(realmConfiguration); @@ -30,6 +39,17 @@ public class FeedCenterEntities } } + public RealmObservableCollection Categories { get; } + + public Category DefaultCategory + { + get { return Categories.First(c => c.IsDefault); } + } + + public RealmObservableCollection Feeds { get; private set; } + private Realm RealmInstance { get; } + public RealmObservableCollection Settings { get; private set; } + public void Refresh() { RealmInstance.Refresh(); @@ -44,9 +64,4 @@ public class FeedCenterEntities { return RealmInstance.BeginWrite(); } - - public Category DefaultCategory - { - get { return Categories.First(c => c.IsDefault); } - } } \ No newline at end of file diff --git a/Application/Feeds/Feed.cs b/Application/Feeds/Feed.cs index 2a3614d..74062fa 100644 --- a/Application/Feeds/Feed.cs +++ b/Application/Feeds/Feed.cs @@ -1,12 +1,4 @@ -using ChrisKaczor.ApplicationUpdate; -using FeedCenter.Data; -using FeedCenter.FeedParsers; -using FeedCenter.Properties; -using FeedCenter.Xml; -using JetBrains.Annotations; -using Realms; -using Serilog; -using System; +using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; @@ -19,6 +11,14 @@ using System.Net.Sockets; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; +using ChrisKaczor.ApplicationUpdate; +using FeedCenter.Data; +using FeedCenter.FeedParsers; +using FeedCenter.Properties; +using FeedCenter.Xml; +using JetBrains.Annotations; +using Realms; +using Serilog; namespace FeedCenter; @@ -98,9 +98,6 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo } } - [MapTo("Password")] - public string RawPassword { get; set; } - public string Password { get => RawPassword; @@ -122,9 +119,15 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo [MapTo("Name")] private string RawName { get; set; } = string.Empty; + [MapTo("Password")] + public string RawPassword { get; set; } + [MapTo("Source")] private string RawSource { get; set; } = string.Empty; + [MapTo("Username")] + public string RawUsername { get; set; } + public string Source { get => RawSource; @@ -139,8 +142,7 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo public string Title { get; set; } - [MapTo("Username")] - public string RawUsername { get; set; } + public string UserAgent { get; set; } public string Username { @@ -242,6 +244,17 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo return new Tuple(feedType, retrieveResult.Item2); } + private string GetUserAgent() + { + if (!string.IsNullOrWhiteSpace(UserAgent)) + return UserAgent; + + if (!string.IsNullOrWhiteSpace(Settings.Default.DefaultUserAgent)) + return Settings.Default.DefaultUserAgent; + + return "FeedCenter/" + UpdateCheck.LocalVersion; + } + private Tuple RetrieveFeed() { try @@ -258,14 +271,15 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo _httpClient = new HttpClient(clientHandler); - // Set a user agent string - var userAgent = string.IsNullOrWhiteSpace(Settings.Default.DefaultUserAgent) ? "FeedCenter/" + UpdateCheck.LocalVersion : Settings.Default.DefaultUserAgent; - _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent); - // Set a timeout _httpClient.Timeout = TimeSpan.FromSeconds(10); } + // Set a user agent string + var userAgent = GetUserAgent(); + _httpClient.DefaultRequestHeaders.UserAgent.Clear(); + _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent); + // If we need to authenticate then set the credentials _httpClient.DefaultRequestHeaders.Authorization = Authenticate ? new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))) : null; diff --git a/Application/Options/FeedWindow.xaml b/Application/Options/FeedWindow.xaml index 641bd22..ed92259 100644 --- a/Application/Options/FeedWindow.xaml +++ b/Application/Options/FeedWindow.xaml @@ -94,6 +94,13 @@ + diff --git a/Application/Options/GeneralOptionsPanel.xaml b/Application/Options/GeneralOptionsPanel.xaml index 1f304d9..93689b4 100644 --- a/Application/Options/GeneralOptionsPanel.xaml +++ b/Application/Options/GeneralOptionsPanel.xaml @@ -12,11 +12,9 @@ d:DesignWidth="300"> - - diff --git a/Application/Options/UserAgentItem.cs b/Application/Options/UserAgentItem.cs index 110b6b0..0f589c3 100644 --- a/Application/Options/UserAgentItem.cs +++ b/Application/Options/UserAgentItem.cs @@ -1,17 +1,18 @@ using System.Collections.Generic; +using System.Linq; +using FeedCenter.Properties; namespace FeedCenter.Options; public class UserAgentItem { public string Caption { get; set; } - public string UserAgent { get; set; } - public static List UserAgents => new() + public static List DefaultUserAgents => new() { new UserAgentItem { - Caption = Properties.Resources.DefaultUserAgentCaption, + Caption = Properties.Resources.ApplicationUserAgentCaption, UserAgent = string.Empty }, new UserAgentItem @@ -30,4 +31,29 @@ public class UserAgentItem UserAgent = "curl/7.47.0" } }; + + public string UserAgent { get; set; } + + public static List UserAgents + { + get + { + var defaultUserAgents = DefaultUserAgents; + + var applicationDefaultUserAgent = defaultUserAgents.First(dua => dua.UserAgent == Settings.Default.DefaultUserAgent); + + var userAgents = new List + { + new() + { + Caption = string.Format(Resources.DefaultUserAgentCaption, applicationDefaultUserAgent.Caption), + UserAgent = null + } + }; + + userAgents.AddRange(defaultUserAgents); + + return userAgents; + } + } } \ No newline at end of file diff --git a/Application/Properties/Resources.Designer.cs b/Application/Properties/Resources.Designer.cs index 19d7bb0..b694911 100644 --- a/Application/Properties/Resources.Designer.cs +++ b/Application/Properties/Resources.Designer.cs @@ -133,6 +133,15 @@ namespace FeedCenter.Properties { } } + /// + /// Looks up a localized string similar to Feed Center. + /// + public static string ApplicationUserAgentCaption { + get { + return ResourceManager.GetString("ApplicationUserAgentCaption", resourceCulture); + } + } + /// /// Looks up a localized string similar to Password. /// @@ -517,7 +526,7 @@ namespace FeedCenter.Properties { } /// - /// Looks up a localized string similar to Feed Center. + /// Looks up a localized string similar to Default ({0}). /// public static string DefaultUserAgentCaption { get { @@ -1461,6 +1470,15 @@ namespace FeedCenter.Properties { } } + /// + /// Looks up a localized string similar to User agent. + /// + public static string userAgentLabel { + get { + return ResourceManager.GetString("userAgentLabel", resourceCulture); + } + } + /// /// Looks up a localized string similar to Version {0}. /// diff --git a/Application/Properties/Resources.resx b/Application/Properties/Resources.resx index 9f530a9..87b5335 100644 --- a/Application/Properties/Resources.resx +++ b/Application/Properties/Resources.resx @@ -524,6 +524,9 @@ Category: {0} + Default ({0}) + + Feed Center @@ -549,4 +552,7 @@ All feeds currently in category "{0}" will be moved to the default category. Are you sure you want to delete the selected feeds? + + User agent + \ No newline at end of file