Skip to content

Commit

Permalink
can now use a local directory for the gallery feed
Browse files Browse the repository at this point in the history
Fixes #670
  • Loading branch information
Yuki-Codes committed Dec 12, 2021
1 parent 20af93a commit 12902fb
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Anamnesis/Languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,15 @@
"Settings_Dir_Characters": "Characters",
"Settings_Dir_Poses": "Poses",
"Settings_Dir_Scenes": "Scenes",
"Settings_Gallery": "Gallery",
"Settings_Gallery_None": "None",
"Settings_Gallery_Curated": "Curated",
"Settings_Gallery_Local": "Local",
"Settings_Theme": "Theme",
"Settings_Theme_Override": "Override system theme",
"Settings_Theme_Light": "Light Mode",
"Settings_Theme_Color": "Color",
"Settings_Hyperlegible": "Use Atkinson Hyperlegible font",
"Settings_ShowGallery": "Show Gallery",
"Settings_OverlayWindow": "Mini window",

"About_Version_Header": "Version",
Expand Down
1 change: 1 addition & 0 deletions Anamnesis/Services/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Settings : INotifyPropertyChanged
public bool FlipPoseGuiSides { get; set; } = false;
public bool UseHyperlegibleFont { get; set; } = false;
public bool ShowGallery { get; set; } = true;
public string? GalleryDirectory { get; set; }
public bool EnableTranslucency { get; set; } = true;
public bool ExtendIntoWindowChrome { get; set; } = true;

Expand Down
55 changes: 43 additions & 12 deletions Anamnesis/Views/Gallery.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Anamnesis.Views
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -75,7 +76,30 @@ private async Task Run()

while (Application.Current != null && SettingsService.Current.ShowGallery)
{
List<Entry> entries = EmbeddedFileUtility.Load<List<Entry>>("Data/Images.json");
List<Entry> entries;

if (string.IsNullOrEmpty(SettingsService.Current.GalleryDirectory))
{
entries = EmbeddedFileUtility.Load<List<Entry>>("Data/Images.json");
}
else
{
string[] files = Directory.GetFiles(FileService.ParseToFilePath(SettingsService.Current.GalleryDirectory), "*.*", SearchOption.AllDirectories);

entries = new List<Entry>();
foreach (string filePath in files)
{
string ext = Path.GetExtension(filePath);
if (ext != ".jpg" && ext != ".png" && ext != ".jpeg" && ext != ".bmp")
continue;

Entry entry = new Entry();
entry.Url = filePath;
entry.Author = Path.GetFileNameWithoutExtension(filePath);
entries.Add(entry);
}
}

Log.Information("Loading gallery image list");

while (entries.Count > 0)
Expand Down Expand Up @@ -112,24 +136,31 @@ private async Task Show(Entry entry, Random rnd)

bool valid = true;

try
if (string.IsNullOrEmpty(SettingsService.Current.GalleryDirectory))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(entry.Url);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(entry.Url);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode != HttpStatusCode.OK)
{
Log.Information($"Failed to get image from url: {entry.Url}: {response.StatusCode}");
valid = false;
}

if (response.StatusCode != HttpStatusCode.OK)
response.Close();
}
catch (Exception ex)
{
Log.Information($"Failed to get image from url: {entry.Url}: {response.StatusCode}");
Log.Information($"Failed to get image from url: {entry.Url}: {ex.Message}");
valid = false;
}

response.Close();
}
catch (Exception ex)
else
{
Log.Information($"Failed to get image from url: {entry.Url}: {ex.Message}");
valid = false;
valid = true;
}

if (!valid)
Expand Down
26 changes: 22 additions & 4 deletions Anamnesis/Views/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,28 @@
<XivToolsWPF:TextBlock Grid.Column="0" Grid.Row="7" Key="Settings_Hyperlegible" Style="{StaticResource Label}"/>
<CheckBox IsChecked="{Binding SettingsService.Settings.UseHyperlegibleFont}" Grid.Column="1" Grid.Row="7" Margin="6"/>

<XivToolsWPF:TextBlock Grid.Column="0" Grid.Row="8" Key="Settings_ShowGallery" Style="{StaticResource Label}"/>
<CheckBox IsChecked="{Binding SettingsService.Settings.ShowGallery}" Grid.Column="1" Grid.Row="8" Margin="6"/>


<Grid Grid.Row="8" Grid.ColumnSpan="2" Margin="0, 0, 4, 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>

<XivToolsWPF:TextBlock Grid.Column="0" Grid.Row="0" Key="Settings_Gallery" Style="{StaticResource Label}"/>
<ComboBox Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" Margin="1" x:Name="GalleryCombobox" SelectionChanged="OnGalleryChanged">
<ComboBoxItem> <XivToolsWPF:TextBlock Key="Settings_Gallery_None"/></ComboBoxItem>
<ComboBoxItem> <XivToolsWPF:TextBlock Key="Settings_Gallery_Curated"/></ComboBoxItem>
<ComboBoxItem> <XivToolsWPF:TextBlock Key="Settings_Gallery_Local"/></ComboBoxItem>
</ComboBox>

<TextBox Grid.Row="1" Grid.Column="1" Margin="3, 0, 0, 0" Style="{StaticResource MaterialDesignTextBox}" Text="{Binding SettingsService.Settings.GalleryDirectory}" IsEnabled="False"/>
<Button Grid.Row="1" Grid.Column="2" Margin="6, 3, 0, 0" Style="{StaticResource TransparentButton}" Content="..." Click="OnBrowseGallery"/>
</Grid>

<GroupBox Grid.Row="9" Grid.ColumnSpan="2">
<GroupBox.Header>
Expand Down
40 changes: 40 additions & 0 deletions Anamnesis/Views/SettingsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Anamnesis.GUI.Views
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using Anamnesis.Files;
Expand Down Expand Up @@ -45,6 +46,19 @@ public SettingsView()
}

this.Languages = languages;

if (!SettingsService.Current.ShowGallery)
{
this.GalleryCombobox.SelectedIndex = 0;
}
else if (string.IsNullOrEmpty(SettingsService.Current.GalleryDirectory))
{
this.GalleryCombobox.SelectedIndex = 1;
}
else
{
this.GalleryCombobox.SelectedIndex = 2;
}
}

public SettingsService SettingsService => SettingsService.Instance;
Expand Down Expand Up @@ -109,6 +123,32 @@ private void OnBrowseScene(object sender, RoutedEventArgs e)
SettingsService.Current.DefaultSceneDirectory = FileService.ParseFromFilePath(dlg.SelectedPath);
}

private void OnBrowseGallery(object sender, RoutedEventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();

if (SettingsService.Current.GalleryDirectory != null)
dlg.SelectedPath = FileService.ParseToFilePath(SettingsService.Current.GalleryDirectory);

DialogResult result = dlg.ShowDialog();

if (result != DialogResult.OK)
return;

SettingsService.Current.GalleryDirectory = FileService.ParseFromFilePath(dlg.SelectedPath);
}

private void OnGalleryChanged(object sender, SelectionChangedEventArgs e)
{
// 0 - none
// 1 - Curated
// 2 - Local
if (this.GalleryCombobox.SelectedIndex != 2)
SettingsService.Current.GalleryDirectory = null;

SettingsService.Current.ShowGallery = this.GalleryCombobox.SelectedIndex != 0;
}

public class LanguageOption
{
public LanguageOption(string key, string display)
Expand Down

0 comments on commit 12902fb

Please sign in to comment.