54 lines
3.4 KiB
C#
54 lines
3.4 KiB
C#
using PlotDirector.Companion.Protocol;
|
|
namespace PlotDirector.Companion.Client;
|
|
|
|
public sealed class DocumentBinding
|
|
{
|
|
public const string AssociationProperty="PlotDirector.Native.Association";
|
|
public static string AssociationValue(Guid identity,int book,string site)=>identity.ToString("D")+"|"+book.ToString(System.Globalization.CultureInfo.InvariantCulture)+"|"+site;
|
|
public static bool ReadAssociation(string value,out Guid identity,out int book,out string site)
|
|
{
|
|
identity=Guid.Empty;book=0;site="";
|
|
if(value==null||value.Length>512)return false;
|
|
var fields=value.Split('|');
|
|
if(fields.Length!=3||!Guid.TryParse(fields[0],out identity)||identity==Guid.Empty||!int.TryParse(fields[1],out book)||book<=0)return false;
|
|
site=fields[2];
|
|
return CanRestore(book,site,site,new[]{new CompanionBook{BookId=book}});
|
|
}
|
|
public static bool CanRestore(int book,string savedSite,string currentSite,IEnumerable<CompanionBook> accessible)
|
|
{
|
|
return book>0 && Uri.TryCreate(savedSite,UriKind.Absolute,out var saved)
|
|
&& Uri.TryCreate(currentSite,UriKind.Absolute,out var current)
|
|
&& saved.Scheme==Uri.UriSchemeHttps && current.Scheme==Uri.UriSchemeHttps
|
|
&& saved.AbsolutePath=="/" && current.AbsolutePath=="/"
|
|
&& string.IsNullOrEmpty(saved.UserInfo) && string.IsNullOrEmpty(saved.Query) && string.IsNullOrEmpty(saved.Fragment)
|
|
&& saved==current && accessible.Any(b=>b.BookId==book);
|
|
}
|
|
public int BookId {get;private set;}
|
|
public Guid Id {get;private set;}
|
|
public Guid AuthenticationContext {get;private set;}
|
|
public SavedWorkspace Workspace {get;private set;}=new();
|
|
public Guid PendingPreview {get;private set;}
|
|
public void BeginPreview(Guid id){PendingPreview=id;Preview=new(){RequestId=id,State="loading",Message="Validating selected structure…"};}
|
|
public void CancelPreview(){PendingPreview=Guid.Empty;Preview=new(){State="cancelled",Message="Preview cancelled; nothing was imported."};}
|
|
public ImportPreview? Preview {get;private set;}
|
|
public void Select(int book,Guid authentication)
|
|
{BookId=book;AuthenticationContext=authentication;Id=Guid.NewGuid();Workspace=new(){BookId=book,State=book==0?"unselected":"loading"};Preview=null;PendingPreview=Guid.Empty;}
|
|
public void Reset()=>Select(0,Guid.Empty);
|
|
public bool Accept(Message response)
|
|
{
|
|
if(response.SnapshotId!=Id||response.AuthenticationContext!=AuthenticationContext||response.BookId!=BookId)return false;
|
|
if(response.Workspace!=null)Workspace=response.Workspace;
|
|
if(response.ImportPreview!=null&&response.ImportPreview.RequestId==PendingPreview&&PendingPreview!=Guid.Empty)Preview=response.ImportPreview;return true;
|
|
}
|
|
}
|
|
public sealed class StructureSelection
|
|
{
|
|
private readonly Dictionary<string,StructureSuggestion> rows=new();
|
|
public static string Key(StructureSuggestion item)=>item.Kind+":"+item.Start+(item.Kind=="Chapter"?"":":"+item.Origin);
|
|
public bool Contains(StructureSuggestion item)=>rows.ContainsKey(Key(item));
|
|
public bool Set(StructureSuggestion item,bool selected)
|
|
{var key=Key(item);if(!selected){rows.Remove(key);return true;}if(rows.Count>=80&&!rows.ContainsKey(key))return false;rows[key]=item;return true;}
|
|
public StructureSuggestion[] Items=>rows.Values.OrderBy(s=>s.Start).ThenBy(s=>s.Kind).ToArray();
|
|
public void Clear()=>rows.Clear();
|
|
}
|