@@ -45,6 +45,12 @@ public sealed class SettingsViewModel : SectionViewModel
private bool _isTesting ;
private CancellationTokenSource ? _selfTest ;
private Archive ? _archive ;
/// <summary>
/// The open of the archive at the folder chosen last (<see cref="RetargetArchiveAsync"/>), with the archive it found
/// there: edits of the Archive rows made meanwhile wait for it (<see cref="ArchiveAtFolderAsync"/>).
/// </summary>
private Task < Archive ? > _retarget = Task . FromResult < Archive ? > ( null ) ;
private bool _isDetectingFormats ;
private IReadOnlyList < BackupOption > _backups = [ ] ;
private BackupOption ? _selectedBackup ;
@@ -82,7 +88,7 @@ public sealed class SettingsViewModel : SectionViewModel
_s . LastArchivePath = v . Length = = 0 ? null : Path . GetFullPath ( v ) ;
Save ( ) ;
UpdateArchiveFree ( ) ;
_ = RetargetArchiveAsync ( ) ;
_retarget = RetargetArchiveAsync ( ) ;
} , ( ) = > Loc . L ( "Tooltips.Settings.ArchiveRoot" ) ) ;
FolderTemplate = new EditableField ( _s . FolderTemplate , v = > NamingPreview . Validate ( v , isFolder : true ) , v = >
{
@@ -770,7 +776,8 @@ public sealed class SettingsViewModel : SectionViewModel
/// <summary>
/// Loads what needs the services (certificates, cache usage, the open archive's settings), follows the server session,
/// applies "Background priority" while Titano is in front and says how a restore staged before this start went.
/// applies "Background priority" while Titano is in front and says how a restore staged before this start went, and
/// where the data of a portable copy in a OneDrive folder went.
/// </summary>
public void Attach ( )
{
@@ -798,6 +805,10 @@ public sealed class SettingsViewModel : SectionViewModel
Shell . Notify ( Loc . L ( "Settings.Advanced.RestoreFailed" ) , Loc . L ( "Errors.Backup.Deferred" ) , isError : true ) ;
break ;
}
// BRIEF §6.2: a portable copy in a folder OneDrive synchronises keeps its data in this user's folder instead.
if ( Shell . Services . Environment . SyncedFolder is not null )
Shell . Notify ( Loc . L ( "Settings.Advanced.DataOutsideOneDrive" ) , Formatting . LeftToRight ( DataFolder ) , Loc . L ( "Common.Open" ) ,
Loc . L ( "Tooltips.Settings.OpenDataFolder" ) , ( ) = > ShellActions . Reveal ( DataFolder ) ) ;
Startup . Refresh ( ) ;
}
@@ -868,33 +879,39 @@ public sealed class SettingsViewModel : SectionViewModel
/// <summary>
/// Takes the archive and server values other screens set (opening an archive, connecting), unless the field holds an
/// edit not committed yet.
/// edit not committed yet, and the sign-in entry as Task Manager may have left it .
/// </summary>
private void ReloadShared ( )
{
if ( ! ArchiveRoot . IsDirty ) ArchiveRoot . Load ( _s . LastArchivePath ? ? string . Empty ) ;
if ( ! DefaultServer . IsDirty ) DefaultServer . Load ( ServerAddress . Display ( _s . ServerUrl ) ) ;
OnPropertyChanged ( nameof ( StartWithWindows ) ) ;
UpdateArchiveFree ( ) ;
_ = LoadArchiveAsync ( ) ;
if ( _page = = SettingsPage . Advanced ) LoadBackups ( ) ;
}
/// <summary>
/// Shows the open archive's own templates, proxy size and thumbnail size (its archive.json; the index's while it is
/// offline) unless a field holds an edit; they also become the defaults of new archives. The open archive is the one
/// at the archive folder of this PC, never another one of the index: with none registered there, the rows edit only
/// this PC's defaults.
/// Shows the open archive's own templates, proxy size and thumbnail size unless a field holds an edit; they also become
/// the defaults of new archives. They come from its archive.json, which also brings the index up to a change another
/// process sharing the archive made, so imports follow it too. While the file cannot be read (offline), the templates
/// and proxy size are the index's and the thumbnail size stays this PC's: only the file knows the archive's. The open
/// archive is the one at the archive folder of this PC, never another one of the index: with no folder, or none
/// registered there, the rows edit only this PC's defaults. Returns that archive (null for none).
/// </summary>
private async Task LoadArchiveAsync ( )
private async Task < Archive ? > LoadArchiveAsync ( )
{
if ( Shell . Services . Database is not { } db | | Shell . Services . Archives is not { } archives ) return ;
if ( Shell . Services . Database is not { } db | | Shell . Services . Archives is not { } archives ) return null ;
Archive ? archive = null ;
try
{
var registered = await db . Archives . ListAsync ( ) ;
var archive = _s . LastArchivePath is { Length : > 0 } root ? CurrentArchive . At ( registered , root ) : CurrentArchive . Pick ( registered , null ) ;
archive = _s . LastArchivePath is { Length : > 0 } root ? CurrentArchive . At ( registered , root ) : null ;
_archive = archive ;
if ( archive is null ) return ;
var settings = await Task . Run ( ( ) = > archives . GetSettingsAsync ( archive . Id ) ) ;
if ( archive is null ) return null ;
var settings = await Task . Run ( ( ) = > archives . RefreshSettingsAsync ( archive . Id ) )
? ? new ArchiveSettings ( archive . Name , new LayoutTemplates { Folder = archive . FolderTemplate , File = archive . FileTemplate } ,
new ProxyPolicy { LongEdge = archive . ProxyLongEdge } , new ThumbnailPolicy { LongEdge = _s . ThumbnailLongEdge } ) ;
if ( ! FolderTemplate . IsDirty ) FolderTemplate . Load ( settings . Templates . Folder ) ;
if ( ! FileTemplate . IsDirty ) FileTemplate . Load ( settings . Templates . File ) ;
var changed = _s . FolderTemplate ! = settings . Templates . Folder | | _s . FileTemplate ! = settings . Templates . File
@@ -915,20 +932,41 @@ public sealed class SettingsViewModel : SectionViewModel
{
Log . Warning ( "settings" , "The open archive's settings could not be read" , ex ) ;
}
return archive ;
}
/// <summary>
/// The archive an Archive row edits, as the folder was when the row was edited (<paramref name="retarget"/>, the
/// latest folder change then): the open one, or, while the archive at that folder is still opening, the one found
/// there once it has (null for none), so the edit is neither lost nor sent to a folder chosen later.
/// </summary>
private async Task < Archive ? > ArchiveAtFolderAsync ( Task < Archive ? > retarget )
{
if ( retarget . IsCompleted ) return _archive ;
// Its failures were told already; only the archive it found matters here.
await ( ( Task ) retarget ) . ConfigureAwait ( ConfigureAwaitOptions . ContinueOnCapturedContext | ConfigureAwaitOptions . SuppressThrowing ) ;
return retarget . IsCompletedSuccessfully ? retarget . Result : null ;
}
/// <summary>
/// Writes one Archive row into the open archive's archive.json and index through the archive engine (BRIEF §7). When
/// that fails (offline, read-only, busy) the user hears why and the rows show the archive's values again.
/// that fails (offline, read-only, busy) the user hears why and the rows show the archive's values again. An edit made
/// while the archive at a folder just chosen is opening waits for it, then goes to that archive (or stays this PC's
/// default when there is none), instead of being lost.
/// </summary>
private async Task UpdateArchiveAsync ( Func < ArchiveSettings , ArchiveSettings > edit )
{
if ( _archive is not { } archive | | Shell . Services . Archives is not { } archives ) return ;
var retarget = _retarget ;
var waited = ! retarget . IsCompleted ;
if ( await ArchiveAtFolderAsync ( retarget ) is not { } archive | | Shell . Services . Archives is not { } archives ) return ;
try
{
var updated = await Task . Run ( ( ) = > archives . UpdateSettingsAsync ( archive . Id , edit ) ) ;
// The archive folder may have changed meanwhile: the rows then belong to another archive.
if ( _archive ? . Id = = updated . Id ) _archive = updated ;
// The open showed the archive's values over the edit: unless another folder was chosen since, the rows (and
// this PC's defaults) take what it holds now.
if ( waited & & _retarget = = retarget ) await LoadArchiveAsync ( ) ;
}
catch ( Exception ex ) when ( ex is IOException or UnauthorizedAccessException or ArgumentException or InvalidDataException or NewerFormatException
or DatabaseException )
@@ -950,9 +988,9 @@ public sealed class SettingsViewModel : SectionViewModel
/// <summary>
/// The archive folder changed: an archive already there is registered as at start (relinked when it moved) and given
/// its rescan, and the rows show its settings; any other folder leaves them with this PC's defaults for new archives.
/// Later edits never reach the archive shown before.
/// Later edits never reach the archive shown before. Returns the archive found there (null for none).
/// </summary>
private async Task RetargetArchiveAsync ( )
private async Task < Archive ? > RetargetArchiveAsync ( )
{
_archive = null ;
if ( _s . LastArchivePath is { } root & & Shell . Services . Archives is { } archives )
@@ -968,7 +1006,7 @@ public sealed class SettingsViewModel : SectionViewModel
Shell . Notify ( Loc . L ( "Errors.Archive.Open" ) , ArchiveActions . Cause ( ex ) , isError : true ) ;
}
}
await LoadArchiveAsync ( ) ;
return await LoadArchiveAsync ( ) ;
}
/// <summary>
@@ -1306,19 +1344,8 @@ public sealed class SettingsViewModel : SectionViewModel
UiLanguage . Apply ( null ) ;
break ;
case SettingsPage . Archive :
// The rows are the open archive's own: resetting them changes it for everyone sharing it, so only when confirmed.
var open = _archive ;
if ( open is not null & & ! ConfirmArchiveReset ( open ) ) return ;
_s . FolderTemplate = defaults . FolderTemplate ;
_s . FileTemplate = defaults . FileTemplate ;
_s . ProxyLongEdge = defaults . ProxyLongEdge ;
_s . ThumbnailLongEdge = defaults . ThumbnailLongEdge ;
_s . ScanIntervalMinutes = defaults . ScanIntervalMinutes ;
FolderTemplate . Load ( _s . FolderTemplate ) ;
FileTemplate . Load ( _s . FileTemplate ) ;
UpdatePreview ( ) ;
if ( open is not null ) _ = PushArchiveSettingsAsync ( ) ;
break ;
_ = ResetArchiveAsync ( defaults ) ;
return ;
case SettingsPage . Import :
_s . ImportSessionGapMinutes = defaults . ImportSessionGapMinutes ;
_s . ImportMinimumFrames = defaults . ImportMinimumFrames ;
@@ -1367,6 +1394,28 @@ public sealed class SettingsViewModel : SectionViewModel
Shell . Server . Refresh ( ) ;
}
/// <summary>
/// Archive › Reset section. The rows are the open archive's own: resetting them changes it for everyone sharing it, so
/// only when confirmed. The archive at a folder just chosen is opened first, so the question names it and the reset
/// reaches it instead of being undone when it opens.
/// </summary>
private async Task ResetArchiveAsync ( AppSettings defaults )
{
var open = await ArchiveAtFolderAsync ( _retarget ) ;
if ( open is not null & & ! ConfirmArchiveReset ( open ) ) return ;
_s . FolderTemplate = defaults . FolderTemplate ;
_s . FileTemplate = defaults . FileTemplate ;
_s . ProxyLongEdge = defaults . ProxyLongEdge ;
_s . ThumbnailLongEdge = defaults . ThumbnailLongEdge ;
_s . ScanIntervalMinutes = defaults . ScanIntervalMinutes ;
FolderTemplate . Load ( _s . FolderTemplate ) ;
FileTemplate . Load ( _s . FileTemplate ) ;
UpdatePreview ( ) ;
Save ( ) ;
OnAllPropertiesChanged ( ) ;
if ( open is not null ) await PushArchiveSettingsAsync ( ) ;
}
private void LoadImportFields ( )
{
SessionGap . Load ( _s . ImportSessionGapMinutes . ToString ( CultureInfo . CurrentCulture ) ) ;
@@ -1484,7 +1533,7 @@ public sealed class SettingsViewModel : SectionViewModel
ApplyAll ( imported ) ;
// Settings of a PC never rewrite the shared archive.json: the imported templates and sizes are the defaults of new
// archives, and the rows show the own settings of the archive at the (possibly imported) archive folder.
_ = RetargetArchiveAsync ( ) ;
_retarget = RetargetArchiveAsync ( ) ;
Shell . Notify ( Loc . L ( "Settings.Import.Done" ) ) ;
}