Files
Chronos/src/Chronos.App/Archive/ConvertPlans.cs
T
Alby96andClaude Opus 5.5 efa87f68ce
CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s
feat: owner review 2 phase 4b: mass conversion after import (#13)
Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-03 10:41:50 +01:00

193 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using Chronos.App.Infrastructure;
using Chronos.Core.Conversion;
using Chronos.Core.Localization;
namespace Chronos.App.Archiving;
/// <summary>
/// One sequence of the conversion dry run (owner review 2, R7): its name, what converts (photos, size before and after),
/// and the photos that stay as they are or why the whole sequence is left as it is, each line with why.
/// </summary>
/// <param name="IsNamePath">The sequence has no title: <paramref name="Name"/> is its folder, read left to right.</param>
/// <param name="IsBlocked">Nothing of the sequence converts now (busy, offline, unreadable).</param>
public sealed record ConvertRow(Guid SequenceId, string Name, bool IsNamePath, string Note, IReadOnlyList<string> Kept, bool IsBlocked)
{
public bool HasNote => Note.Length > 0;
public bool HasKept => Kept.Count > 0;
/// <summary>What a screen reader says after the name: what converts, then every line of what stays.</summary>
public string Description => string.Join(Environment.NewLine, [.. HasNote ? [Note] : (string[])[], .. Kept]);
}
/// <summary>The free space line of a conversion dry run, and whether the volume lacks the space.</summary>
public sealed record ConvertSpaceLine(string Text, bool IsShort);
/// <summary>
/// The words of a mass conversion (owner review 2, R7; BRIEF §12.5): the engine's dry run (<see cref="ConvertPlan"/>) as
/// rows, a summary and the free space line, the typed confirmation a Replace asks for, and the one New sequence asks for,
/// which says which photos the new sequences lack.
/// </summary>
public static class ConvertPlans
{
/// <summary>The targets in the order the dialog offers them.</summary>
public static readonly IReadOnlyList<ConvertTarget> Targets = [ConvertTarget.Jpeg, ConvertTarget.Tiff16, ConvertTarget.LinearDng];
/// <summary>The problem of a JPEG quality as typed, localized; null when it is 1–100.</summary>
public static string? ValidateQuality(string? text) =>
int.TryParse(text?.Trim(), NumberStyles.Integer, Loc.Culture, out var quality) && quality is >= 1 and <= 100
? null
: Loc.L("Errors.Number.Range", 1.ToString("N0", Loc.Culture), 100.ToString("N0", Loc.Culture));
/// <summary>A valid quality as typed (see <see cref="ValidateQuality"/>).</summary>
public static int ParseQuality(string text) => int.Parse(text.Trim(), NumberStyles.Integer, Loc.Culture);
/// <summary>
/// The rows of a plan in its order: every sequence that converts, or whose photos stay as they are, with why in the
/// words of the plan's mode (<see cref="Row"/>).
/// </summary>
public static IReadOnlyList<ConvertRow> Rows(ConvertPlan plan)
{
ArgumentNullException.ThrowIfNull(plan);
return [.. plan.Sequences.Where(s => s.Frames.Count > 0 || s.Skipped.Count > 0 || s.Blocked.Count > 0).Select(s => Row(s, plan.Mode))];
}
/// <summary>
/// One sequence of a plan: what converts, then what blocks it, then the photos skipped with why. Of a sequence
/// <see cref="ConvertMode.NewSequence"/> copies, those are not copied into the new sequence, which holds the converted
/// copies alone; otherwise (a Replace, or a sequence that converts nothing now, of which no new sequence is made) they
/// stay as they are.
/// </summary>
public static ConvertRow Row(ConvertSequencePlan step, ConvertMode mode = ConvertMode.Replace)
{
ArgumentNullException.ThrowIfNull(step);
var (name, named) = NameOf(step);
var note = step.Converts
? Loc.Plural("Shell.Status.Photos", step.Frames.Count) + Loc.Separator + Sizes(step.BytesBefore, step.BytesAfter)
: string.Empty;
// What blocks the whole sequence comes first: the photos it would skip anyway do not matter until it can convert.
List<string> kept = [.. step.Blocked.Distinct().Select(b => Loc.L("Archive.Convert.Blocked." + b))];
var why = mode == ConvertMode.NewSequence && step.Converts ? "Archive.Convert.NotCopied." : "Archive.Convert.Skipped.";
kept.AddRange(step.Skipped.Select(s => Loc.L(why + s.Reason) + Loc.Separator + Loc.Plural("Shell.Status.Photos", s.Frames)));
return new ConvertRow(step.SequenceId, name, !named, note, kept, step.Blocked.Count > 0);
}
/// <summary>A sequence's title, else its folder (a path), else the start of its id; and whether it has a title.</summary>
private static (string Name, bool Named) NameOf(ConvertSequencePlan step)
{
var named = !string.IsNullOrWhiteSpace(step.Title);
return (named ? step.Title : step.Folder.Length > 0 ? step.Folder : step.SequenceId.ToString("N")[..8], named);
}
/// <summary>The photos a conversion into new sequences leaves out of them: those the sequences it copies skip.</summary>
public static int LeftOut(ConvertPlan plan)
{
ArgumentNullException.ThrowIfNull(plan);
return plan.Converted.Sum(s => s.SkippedFrames);
}
/// <summary>"12.4 GB → 9.8 GB".</summary>
public static string Sizes(long before, long after) => Loc.L("Archive.Convert.Sizes", Formatting.Bytes(before), Formatting.Bytes(after));
/// <summary>
/// "12 of 36 change · 3,600 photos · 41 GB → 32 GB · 20 skipped": the sequences converted of those planned, then the
/// photos and their sizes when something converts, the photos skipped when there are any. With
/// <see cref="ConvertMode.NewSequence"/>, those of the sequences copied first ("20 not copied": the new sequences lack
/// them), then those of the sequences that convert nothing now ("4 skipped").
/// </summary>
public static string Summary(ConvertPlan plan)
{
ArgumentNullException.ThrowIfNull(plan);
var parts = new List<string> { Loc.Plural("Archive.Rename.Summary", plan.Converted.Count(), plan.Sequences.Count) };
if (plan.Frames > 0)
{
parts.Add(Loc.Plural("Shell.Status.Photos", plan.Frames));
parts.Add(Sizes(plan.BytesBefore, plan.BytesAfter));
}
var leftOut = plan.Mode == ConvertMode.NewSequence ? LeftOut(plan) : 0;
if (leftOut > 0) parts.Add(Loc.Plural("Archive.Convert.NotCopiedPhotos", leftOut));
if (plan.Skipped - leftOut > 0) parts.Add(Loc.Plural("Archive.Convert.SkippedPhotos", plan.Skipped - leftOut));
return string.Join(Loc.Separator, parts);
}
/// <summary>"Needs 12 GB, free 340 GB", or that the volume lacks it, or that its free space cannot be measured.</summary>
public static ConvertSpaceLine Space(ConvertSpace space)
{
ArgumentNullException.ThrowIfNull(space);
var required = Formatting.Bytes(space.Required);
if (space.Free is not { } free) return new(Loc.L("Archive.Convert.SpaceUnknown", required), false);
return space.Enough
? new(Loc.L("Archive.Convert.Space", required, Formatting.Bytes(free)), false)
: new(Loc.L("Archive.Convert.SpaceShort", required, Formatting.Bytes(free)), true);
}
/// <summary>"Linear DNG": the target as the dialog names it.</summary>
public static string TargetText(ConvertTarget target) => Loc.L("Archive.Convert.Target." + target);
/// <summary>
/// The destructive confirmation of a Replace: the sequences whose photos are replaced, that the originals move to the
/// archive's trash, and the number of photos to type; null when the plan converts nothing.
/// </summary>
public static ConfirmRequest? ReplaceRequest(ConvertPlan plan)
{
ArgumentNullException.ThrowIfNull(plan);
var converted = plan.Converted.ToList();
if (converted.Count == 0) return null;
var count = plan.Frames.ToString(CultureInfo.InvariantCulture);
var lines = converted.Take(DeletePlans.ListedLines).Select(s => Loc.L("Archive.Delete.Line", Row(s).Name,
Loc.Plural("Shell.Status.Photos", s.Frames.Count), Formatting.Bytes(s.BytesBefore))).ToList();
if (converted.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", converted.Count - DeletePlans.ListedLines));
return new ConfirmRequest
{
Heading = Loc.Plural("Archive.Convert.ReplaceHeading", converted.Count),
Message = Loc.L("Archive.Convert.ReplaceMessage", Loc.Plural("Shell.Status.Photos", plan.Frames), Formatting.Bytes(plan.BytesBefore),
TargetText(plan.Options.Target)),
Detail = string.Join(Environment.NewLine, lines),
IsDetailMonospaced = false,
DetailToolTip = Loc.L("Tooltips.Archive.ConvertPlan"),
SafeText = Loc.L("Archive.Delete.Keep"),
SafeToolTip = Loc.L("Tooltips.Archive.DeleteKeep"),
ActionText = Loc.L("Archive.Convert.Replace"),
ActionToolTip = Loc.Plural("Tooltips.Archive.ConvertReplace", plan.Frames),
IsDestructive = true,
TypedConfirmation = count,
TypedPrompt = Loc.L("Archive.Delete.Type", count),
};
}
/// <summary>
/// The confirmation of a conversion into new sequences: the photos converted into copies beside their sources, the
/// originals untouched, how many photos the new sequences lack (those the plan skips there, for the reasons its rows
/// give), and each sequence copied with its photos, their size and the photos its new sequence lacks; null when the plan
/// converts nothing.
/// </summary>
public static ConfirmRequest? NewRequest(ConvertPlan plan)
{
ArgumentNullException.ThrowIfNull(plan);
var converted = plan.Converted.ToList();
if (converted.Count == 0) return null;
var lines = converted.Take(DeletePlans.ListedLines).Select(s =>
{
var line = Loc.L("Archive.Delete.Line", NameOf(s).Name, Loc.Plural("Shell.Status.Photos", s.Frames.Count), Sizes(s.BytesBefore, s.BytesAfter));
return s.SkippedFrames > 0 ? line + Loc.Separator + Loc.Plural("Archive.Convert.NotCopiedPhotos", s.SkippedFrames) : line;
}).ToList();
if (converted.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", converted.Count - DeletePlans.ListedLines));
var message = Loc.L("Archive.Convert.NewMessage", Loc.Plural("Shell.Status.Photos", plan.Frames), Formatting.Bytes(plan.BytesBefore),
TargetText(plan.Options.Target));
if (LeftOut(plan) is > 0 and var leftOut) message += " " + Loc.Plural("Archive.Convert.NewLeftOut", leftOut);
return new ConfirmRequest
{
Heading = Loc.Plural("Archive.Convert.NewHeading", converted.Count),
Message = message,
Detail = string.Join(Environment.NewLine, lines),
IsDetailMonospaced = false,
DetailToolTip = Loc.L("Tooltips.Archive.ConvertPlanNew"),
SafeText = Loc.L("Archive.Dialog.Cancel"),
SafeToolTip = Loc.L("Tooltips.Archive.ConvertBack"),
ActionText = Loc.L("Archive.Convert.Confirm"),
ActionToolTip = Loc.L("Tooltips.Archive.ConvertNew"),
};
}
}