diff --git a/OpenBullet/ViewModels/ConfigManagerViewModel.cs b/OpenBullet/ViewModels/ConfigManagerViewModel.cs index b02a7ef..34414d2 100644 --- a/OpenBullet/ViewModels/ConfigManagerViewModel.cs +++ b/OpenBullet/ViewModels/ConfigManagerViewModel.cs @@ -1,10 +1,13 @@ -using RuriLib; +using OpenBullet.Models; +using RuriLib; using RuriLib.ViewModels; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.IO; +using System.IO.Compression; using System.Linq; +using System.Net; namespace OpenBullet.ViewModels { @@ -49,8 +52,11 @@ namespace OpenBullet.ViewModels public void RefreshList() { - // Scan the directory for new configs - ConfigsList = new ObservableCollection(GetConfigsFromDisk(true)); + // Scan the directory and the sources for configs + ConfigsList = new ObservableCollection( + GetConfigsFromSources() + .Concat(GetConfigsFromDisk(true)) + ); OnPropertyChanged("Total"); } @@ -71,7 +77,7 @@ namespace OpenBullet.ViewModels { foreach(var file in Directory.EnumerateFiles(categoryFolder).Where(file => file.EndsWith(".loli"))) { - try { models.Add(new ConfigViewModel(file, System.IO.Path.GetFileName(categoryFolder), IOManager.LoadConfig(file))); } + try { models.Add(new ConfigViewModel(file, Path.GetFileName(categoryFolder), IOManager.LoadConfig(file))); } catch { Globals.LogError(Components.ConfigManager, "Could not load file: " + file); } } } @@ -79,5 +85,53 @@ namespace OpenBullet.ViewModels if (sort) { models.Sort((m1, m2) => m1.Config.Settings.LastModified.CompareTo(m2.Config.Settings.LastModified)); } return models; } + + public List GetConfigsFromSources() + { + var list = new List(); + + foreach(var source in Globals.obSettings.Sources.Sources) + { + try + { + WebClient wc = new WebClient(); + switch (source.Auth) + { + case Source.AuthMode.ApiKey: + wc.Headers.Add(HttpRequestHeader.Authorization, source.ApiKey); + break; + + case Source.AuthMode.UserPass: + var header = BlockFunction.Base64Encode($"{source.Username}:{source.Password}"); + wc.Headers.Add(HttpRequestHeader.Authorization, $"Basic {header}"); + break; + + default: + break; + } + + var file = wc.DownloadData(source.ApiUrl); + + using (var zip = new ZipArchive(new MemoryStream(file), ZipArchiveMode.Read)) + { + foreach (var entry in zip.Entries) + { + using (var stream = entry.Open()) + { + using (TextReader tr = new StreamReader(stream)) + { + var text = tr.ReadToEnd(); + var cfg = IOManager.DeserializeConfig(text); + list.Add(new ConfigViewModel("", "Remote", cfg, true)); + } + } + } + } + } + catch { } + } + + return list; + } } } \ No newline at end of file diff --git a/RuriLib/ViewModels/ConfigViewModel.cs b/RuriLib/ViewModels/ConfigViewModel.cs index 680325b..a517a06 100644 --- a/RuriLib/ViewModels/ConfigViewModel.cs +++ b/RuriLib/ViewModels/ConfigViewModel.cs @@ -18,6 +18,9 @@ namespace RuriLib.ViewModels /// The path of the config file on disk. public string Path { get { return path; } set { path = value; OnPropertyChanged(); } } + /// Whether the config was pulled from a remote source. + public bool Remote { get; set; } = false; + /// The name of the config. public string Name { get { return Config.Settings.Name; } } @@ -27,11 +30,13 @@ namespace RuriLib.ViewModels /// The path of the config file on disk /// The category of the config /// The actual Config object - public ConfigViewModel(string path, string category, Config config) + /// Whether the Config was pulled from a remote source + public ConfigViewModel(string path, string category, Config config, bool remote = false) { Path = path; Category = category; Config = config; + Remote = remote; } } }