Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Merge remote-tracking branch 'origin/main' into feature/interactive-crop-and-donut-paging

KnifeLemond0d6b705cad6
diff --cc src/Filee.Core/Settings/AppSettings.cs
index 7ec34b1d,7ec34b1d..4b28a9fb
--- a/src/Filee.Core/Settings/AppSettings.cs
+++ b/src/Filee.Core/Settings/AppSettings.cs
@@@ -64,7 -64,7 +64,7 @@@ public sealed class DonutSetting
public sealed class AppSettings
{
/// <summary>Current schema version. Increase when the format changes and add a migration.</summary>
-- public const int CurrentSchemaVersion = 23;
++ public const int CurrentSchemaVersion = 24;
public int SchemaVersion { get; set; } = CurrentSchemaVersion;
diff --cc src/Filee.Core/Settings/UserDataStore.cs
index 032bd586,dc76cec6..92eeb56b
--- a/src/Filee.Core/Settings/UserDataStore.cs
+++ b/src/Filee.Core/Settings/UserDataStore.cs
@@@ -136,10 -136,70 +136,70 @@@ public sealed class UserDataStor
SaveLibrary();
}
+ /// <summary>
+ /// Whether <see cref="ResetProfile"/> can put <paramref name="profile"/> back: only a built-in donut has a default
+ /// state, and not when the reset would leave no profile for mixed selections (a built-in normal donut the user made
+ /// the only mixed one).
+ /// </summary>
+ public bool CanResetProfile(ToolbarProfile profile) =>
+ Profiles.Contains(profile)
+ && BuiltInData.CreateProfiles().FirstOrDefault(p => p.Id == profile.Id) is { } builtIn
+ && (builtIn.IsFallback || !profile.IsFallback || Profiles.Count(p => p.IsFallback) > 1);
+
+ /// <summary>
+ /// Puts one built-in donut back as a new library has it (name, slices and their order, extensions, normal or mixed)
+ /// and leaves the other donuts alone. So no file type opens two donuts, an extension another normal donut takes
+ /// now stays there. Presets are shared by every donut: the ones this donut drops stay in the library, edited ones
+ /// keep their settings, and a built-in preset of its default slices that the user deleted comes back. A donut that
+ /// changes kind moves to the border between the normal profiles and the mixed ones, the built-in mixed donut back
+ /// to the head of the mixed ones (the catch-all).
+ /// </summary>
+ /// <returns>False when the profile can't be reset (<see cref="CanResetProfile"/>).</returns>
+ public bool ResetProfile(ToolbarProfile profile)
+ {
+ if (!CanResetProfile(profile))
+ return false;
+ var builtIn = BuiltInData.CreateProfiles().Single(p => p.Id == profile.Id);
+ RestoreBuiltInPresets(builtIn.PresetIds);
+
+ // Extensions checked on a mixed-selection profile only limit that profile; they don't claim files.
+ var claimed = Profiles.Where(p => p != profile && !p.IsFallback)
+ .SelectMany(p => p.Extensions)
+ .ToHashSet(StringComparer.OrdinalIgnoreCase);
+ profile.Name = "";
+ profile.NameKey = builtIn.NameKey;
+ profile.Extensions = builtIn.IsFallback ? builtIn.Extensions : builtIn.Extensions.Where(e => !claimed.Contains(e)).ToList();
+ profile.PresetIds = builtIn.PresetIds;
+ if (profile.IsFallback != builtIn.IsFallback)
+ {
+ profile.IsFallback = builtIn.IsFallback;
+ Profiles.Remove(profile);
+ var index = Profiles.FindIndex(p => p.IsFallback);
+ Profiles.Insert(index < 0 ? Profiles.Count : index, profile);
+ }
+ SaveLibrary();
+ return true;
+ }
+
+ /// <summary>Adds back the built-in presets among <paramref name="ids"/> that the library no longer has, each after
+ /// the built-in preset before it (as a new library orders them).</summary>
+ private void RestoreBuiltInPresets(IEnumerable<string> ids)
+ {
+ var defaults = BuiltInData.CreatePresets();
+ foreach (var id in ids.Where(id => FindPreset(id) is null))
+ {
+ var at = defaults.FindIndex(p => p.Id == id);
+ if (at < 0)
+ continue;
+ var after = defaults.Take(at).Select(p => Presets.FindIndex(existing => existing.Id == p.Id)).LastOrDefault(i => i >= 0, -1);
+ Presets.Insert(after + 1, defaults[at]);
+ }
+ }
+
public Preset? FindPreset(string id) => Presets.FirstOrDefault(p => p.Id == id);
- /// <summary>The preset <paramref name="id"/> when it converts (a group doesn't), for every place that runs one.</summary>
- public Preset? FindConversion(string id) => Presets.FirstOrDefault(p => p.Id == id && !p.IsGroup);
+ /// <summary>The preset <paramref name="id"/> when it converts (a group or tool doesn't), for every place that runs one.</summary>
+ public Preset? FindConversion(string id) => Presets.FirstOrDefault(p => p.Id == id && !p.IsGroup && !p.IsTool);
/// <summary>Exports presets and profiles to a shareable JSON file.</summary>
public void Export(string path) =>
@@@ -563,30 -623,10 +623,33 @@@ internal static class SettingsMigration
// takes it. Other extensions the user removed from a built-in donut stay removed.
if (from < 22)
changed |= LearnExtensions(profiles, out _, ["fbx"]);
-
- // v23: interactive "Crop" tool, after "Remove background" as in a new library, on no donut: users add it in
- // Settings → Toolbar. Present already but saved since by a Filee before 23: that one read the unknown
+ // v23: MHTML web archives (.mhtml, .mht; read only) open from the text donut next to HTML, unless a donut of
+ // the user already takes them.
+ if (from < 23)
+ changed |= LearnExtensions(profiles, out _, ["mhtml", "mht"]);
++ // v24: interactive "Crop" tool, after "Remove background" as in a new library, on no donut: users add it in
++ // Settings → Toolbar. Present already but saved since by a Filee before 24: that one read the unknown
+ // kind as a Group, dropped the target format and left an empty group. Kind and target format are put back,
+ // the user's own changes (name, file name pattern) stay.
- if (from < 23)
++ if (from < 24)
+ {
+ if (BuiltInData.CreatePresets().FirstOrDefault(p => p.Id == "tool-crop") is { } crop)
+ {
+ if (presets.FirstOrDefault(p => p.Id == "tool-crop") is not { } existing)
+ {
+ var at = presets.FindIndex(p => p.Id == "remove-background");
+ presets.Insert(at < 0 ? presets.Count : at + 1, crop);
+ changed = true;
+ }
+ else if (existing.Kind != PresetKind.Tool)
+ {
+ existing.Kind = PresetKind.Tool;
+ existing.TargetFormat = crop.TargetFormat;
+ existing.SubPresetIds = [];
+ changed = true;
+ }
+ }
+ }
return changed;
}
diff --cc tests/Filee.Core.Tests/UserDataStoreTests.cs
index 80fc8ec6,09a31904..5374aba2
--- a/tests/Filee.Core.Tests/UserDataStoreTests.cs
+++ b/tests/Filee.Core.Tests/UserDataStoreTests.cs
@@@ -819,42 -843,6 +843,42 @@@ public class UserDataStoreTest
Assert.False(SettingsMigrations.ApplyToLibrary(17, presets, profiles)); // once only
}
+ [Fact]
- public void Version_22_libraries_get_crop_tool_preset_after_remove_background_on_no_donut()
++ public void Version_23_libraries_get_crop_tool_preset_after_remove_background_on_no_donut()
+ {
+ var presets = BuiltInData.CreatePresets();
+ presets.RemoveAll(p => p.Id == "tool-crop");
+ var profiles = BuiltInData.CreateProfiles();
+
- Assert.True(SettingsMigrations.ApplyToLibrary(22, presets, profiles));
++ Assert.True(SettingsMigrations.ApplyToLibrary(23, presets, profiles));
+
+ var crop = presets.Single(p => p.Id == "tool-crop");
+ Assert.Equal(PresetKind.Tool, crop.Kind);
+ Assert.Equal("remove-background", presets[presets.IndexOf(crop) - 1].Id);
+ Assert.DoesNotContain(profiles, p => p.PresetIds.Contains("tool-crop"));
- Assert.False(SettingsMigrations.ApplyToLibrary(22, presets, profiles)); // once only
++ Assert.False(SettingsMigrations.ApplyToLibrary(23, presets, profiles)); // once only
+ }
+
+ [Fact]
+ public void Back_from_an_older_Filee_tool_crop_works_again_and_keeps_the_users_changes()
+ {
- // What a Filee before 23 leaves after saving: kind read as Group, TargetFormat dropped, SubPresetIds empty, the rest as it was.
++ // What a Filee before 24 leaves after saving: kind read as Group, TargetFormat dropped, SubPresetIds empty, the rest as it was.
+ var presets = BuiltInData.CreatePresets();
+ var crop = presets.Single(p => p.Id == "tool-crop");
+ crop.Kind = PresetKind.Group;
+ crop.TargetFormat = Preset.GroupTarget;
+ crop.SubPresetIds = [];
+ crop.Name = "Trim";
+
- Assert.True(SettingsMigrations.ApplyToLibrary(22, presets, BuiltInData.CreateProfiles()));
++ Assert.True(SettingsMigrations.ApplyToLibrary(23, presets, BuiltInData.CreateProfiles()));
+
+ Assert.Equal(PresetKind.Tool, crop.Kind);
+ Assert.Equal(BuiltInData.SameAsSource, crop.TargetFormat);
+ Assert.Empty(crop.SubPresetIds);
+ Assert.Equal("Trim", crop.Name);
+ Assert.Single(presets, p => p.Id == "tool-crop");
+ }
+
[Fact]
public void Back_from_an_older_Filee_compress_pdf_compresses_again_and_keeps_the_users_changes()
{