/* ----------------------------------------------------------------- */ /// /// Save /// /// <summary> /// Save the PDF document /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="dest">Saving file information.</param> /// <param name="close">Close action.</param> /// /* ----------------------------------------------------------------- */ public static void Save(this MainFacade src, Information dest, Action close) { var data = src.Bindable; var tmp = data.IO.Combine(dest.DirectoryName, Guid.NewGuid().ToString("D")); try { var reader = data.Source.Value.GetItexReader(data.Query, data.IO); data.Set(reader.Metadata, reader.Encryption); using (var writer = new DocumentWriter()) { writer.Add(reader.Attachments); writer.Add(data.Images.Select(e => e.RawObject), reader); writer.Set(data.Metadata); writer.Set(data.Encryption); writer.Save(tmp); } close(); src.Backup.Invoke(dest); data.IO.Copy(tmp, dest.FullName, true); } finally { data.IO.TryDelete(tmp); } }
/* ----------------------------------------------------------------- */ /// /// Extract /// /// <summary> /// Extracts the selected PDF pages and saves to the specified /// file. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="dest">Path to save.</param> /// /* ----------------------------------------------------------------- */ public static void Extract(this MainFacade src, string dest) => src.Extract(new SaveOption(src.Value.IO) { Target = SaveTarget.Selected, Split = false, Destination = dest, });
/* ----------------------------------------------------------------- */ /// /// Overwrite /// /// <summary> /// Overwrites the PDF document. /// </summary> /// /// <param name="src">Facade object.</param> /// /* ----------------------------------------------------------------- */ public static void Overwrite(this MainFacade src) { if (src.Bindable.History.Undoable) { src.Save(src.Bindable.Source.Value.FullName); } }
/* ----------------------------------------------------------------- */ /// /// Close /// /// <summary> /// Closes the current PDF document. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="save">Save before closing.</param> /// /* ----------------------------------------------------------------- */ public static void Close(this MainFacade src, bool save) { if (save) { src.Save(src.Value.Source.FullName, false); } src.Close(); }
/* ----------------------------------------------------------------- */ /// /// MoveNext /// /// <summary> /// Moves selected items according to the specified condition. /// </summary> /// /* ----------------------------------------------------------------- */ private static void MoveNext(this MainFacade src, DragDropObject obj) { var delta = obj.DropIndex - obj.DragIndex; var n = src.Value.Images.Selection.Indices .Where(i => i > obj.DragIndex && i <= obj.DropIndex).Count(); src.Move(delta - n); }
/* ----------------------------------------------------------------- */ /// /// MovePrevious /// /// <summary> /// Moves selected items accoding to the specified condition. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="obj">Drag&Drop result.</param> /// /* ----------------------------------------------------------------- */ private static void MovePrevious(this MainFacade src, DragDropObject obj) { var delta = obj.DropIndex - obj.DragIndex; var n = src.Bindable.Images.Selection.Indices .Where(i => i < obj.DragIndex && i >= obj.DropIndex).Count(); src.Move(delta + n); }
/* ----------------------------------------------------------------- */ /// /// Zoom /// /// <summary> /// Executes the Zoom command by using the current settings. /// </summary> /// /// <param name="src">Facade object.</param> /// /* ----------------------------------------------------------------- */ public static void Zoom(this MainFacade src) { var items = src.Bindable.Images.Preferences.ItemSizeOptions; var prev = src.Bindable.Images.Preferences.ItemSizeIndex; var next = items.LastIndexOf(x => x <= src.Settings.Value.ItemSize); src.Zoom(next - prev); }
/* ----------------------------------------------------------------- */ /// /// IsInsertable /// /// <summary> /// Gets the value indicating whether the specified file is /// insertable. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="path">File path.</param> /// /// <remarks> /// TODO: 現在は拡張子で判断しているが、ファイル内容の Signature を /// 用いて判断するように修正する。 /// </remarks> /// /* ----------------------------------------------------------------- */ public static bool IsInsertable(this MainFacade src, string path) { var ext = src.Bindable.IO.Get(path).Extension.ToLowerInvariant(); var cmp = new List <string> { ".pdf", ".png", ".jpg", ".jpeg", ".bmp" }; return(cmp.Contains(ext)); }
/* ----------------------------------------------------------------- */ /// /// SetMetadata /// /// <summary> /// Sets the Metadata object. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="value">Metadata object.</param> /// /// <returns> /// History item to execute undo and redo actions. /// </returns> /// /* ----------------------------------------------------------------- */ public static HistoryItem SetMetadata(this MainFacade src, Metadata value) { var prev = src.Bindable.Metadata; return(HistoryItem.CreateInvoke( () => src.Bindable.Metadata = value, () => src.Bindable.Metadata = prev )); }
/* ----------------------------------------------------------------- */ /// /// SetEncryption /// /// <summary> /// Sets the Encryption object. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="value">Encryption object.</param> /// /// <returns> /// History item to execute undo and redo actions. /// </returns> /// /* ----------------------------------------------------------------- */ public static HistoryItem SetEncryption(this MainFacade src, Encryption value) { var prev = src.Bindable.Encryption; return(HistoryItem.CreateInvoke( () => src.Bindable.Encryption = value, () => src.Bindable.Encryption = prev )); }
/* ----------------------------------------------------------------- */ /// /// Open /// /// <summary> /// Opens the first item of the specified collection. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="files">File collection.</param> /// /* ----------------------------------------------------------------- */ public static void Open(this MainFacade src, IEnumerable <string> files) { var path = src.GetFirst(files); if (path.HasValue()) { src.Open(path); } }
/* ----------------------------------------------------------------- */ /// /// Save /// /// <summary> /// Saves the PDF document to the specified file path. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="dest">Path to save.</param> /// <param name="reopen"> /// Value indicating whether to reopen the document. /// </param> /// /* ----------------------------------------------------------------- */ public static void Save(this MainFacade src, string dest, bool reopen) => src.Save( dest, e => { src.Backup.Invoke(e); src.Cache?.Clear(); }, e => { if (reopen) { src.Reload(e.FullName); } src.Value.Set(Properties.Resources.MessageSaved, e.FullName); } );
/* ----------------------------------------------------------------- */ /// /// Restruct /// /// <summary> /// Restructs some properties with the specified new PDF document. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="doc">New PDF document.</param> /// /* ----------------------------------------------------------------- */ public static void Restruct(this MainFacade src, IDocumentReader doc) { var items = doc.Pages.Select((v, i) => new { Value = v, Index = i }); foreach (var e in items) { src.Bindable.Images[e.Index].RawObject = e.Value; } src.Bindable.Source.Value = doc.File; src.Bindable.History.Clear(); }
/* ----------------------------------------------------------------- */ /// /// Reload /// /// <summary> /// Reload the specified file information. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="path">File path to load.</param> /// /* ----------------------------------------------------------------- */ public static void Reload(this MainFacade src, string path) { var doc = src.Cache.GetOrAdd(path, src.Value.Encryption.OwnerPassword); var items = doc.Pages.Select((v, i) => new { Value = v, Index = i }); foreach (var e in items) { src.Value.Images[e.Index].RawObject = e.Value; } src.Value.Source = doc.File; src.Value.History.Clear(); }
/* ----------------------------------------------------------------- */ /// /// Load /// /// <summary> /// Loads properties of the specified file. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="path">File path to load.</param> /// /// <remarks> /// PDFium は Metadata や Encryption の情報取得が不完全なため、 /// これらの情報は、必要になったタイミングで iTextSharp を用いて /// 取得します。 /// </remarks> /// /* ----------------------------------------------------------------- */ public static void Load(this MainFacade src, string path) { src.Value.Set(Properties.Resources.MessageLoading, path); var doc = src.Cache.GetOrAdd(path); src.Value.Source = doc.File; if (!doc.Encryption.Enabled) { src.Value.Encryption = doc.Encryption; } src.Value.Images.Add(doc.Pages); src.Value.Set(string.Empty); }
/* ----------------------------------------------------------------- */ /// /// MainViewModel /// /// <summary> /// Initializes a new instance of the MainViewModel class /// with the specified settings. /// </summary> /// /* ----------------------------------------------------------------- */ public MainViewModel(SettingFolder src, SynchronizationContext context) : base(new Aggregator(), context) { var recent = Environment.SpecialFolder.Recent.GetName(); var mon = new DirectoryMonitor(recent, "*.pdf.lnk", src.IO, GetDispatcher(false)); var password = new Query <string>(e => Send(new PasswordViewModel(e, src.IO, context))); Model = new MainFacade(src, password, context); Ribbon = new RibbonViewModel(Model.Bindable, Aggregator, context); Recent = new RecentViewModel(mon, Aggregator, context); SetCommands(); Track(() => Model.Setup(App.Arguments)); }
/* ----------------------------------------------------------------- */ /// /// Setup /// /// <summary> /// Invokes some actions through the specified arguments. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="args">User arguments.</param> /// /* ----------------------------------------------------------------- */ public static void Setup(this MainFacade src, IEnumerable <string> args) { foreach (var ps in src.Settings.GetSplashProcesses()) { ps.Kill(); } var path = src.GetFirst(args); if (path.HasValue()) { src.Open(path); } src.Backup.Cleanup(); }
/* ----------------------------------------------------------------- */ /// /// OpenLink /// /// <summary> /// Opens a PDF document with the specified link. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="link">Information for the link.</param> /// /* ----------------------------------------------------------------- */ public static void OpenLink(this MainFacade src, Information link) { try { src.Open(Shortcut.Resolve(link?.FullName)?.Target); } catch (Exception err) { var cancel = err is OperationCanceledException || err is TwiceException; if (!cancel) { src.Bindable.IO.TryDelete(link?.FullName); } throw; } }
/* ----------------------------------------------------------------- */ /// /// InsertOrMove /// /// <summary> /// Inserts or moves the specified pages according to the specified /// condition. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="obj">Drag&Drop result.</param> /// /* ----------------------------------------------------------------- */ public static void InsertOrMove(this MainFacade src, DragDropObject obj) { if (!obj.IsCurrentProcess) { var index = Math.Min(obj.DropIndex + 1, src.Value.Count); src.Insert(index, obj.Pages); } else if (obj.DragIndex < obj.DropIndex) { src.MoveNext(obj); } else { src.MovePrevious(obj); } }
/* ----------------------------------------------------------------- */ /// /// MainViewModel /// /// <summary> /// Initializes a new instance of the MainViewModel class /// with the specified settings. /// </summary> /// /* ----------------------------------------------------------------- */ public MainViewModel(SettingsFolder src) : base(new Messenger()) { var recent = Environment.SpecialFolder.Recent.GetName(); var mon = new DirectoryMonitor(recent, "*.pdf.lnk", src.IO); var password = new Query <string>(e => Send(new PasswordViewModel(e, src.IO, Context))); Model = new MainFacade(src, password, Context); Ribbon = new RibbonViewModel(Model.Bindable, MessengerInstance); Recent = new RecentViewModel(mon, MessengerInstance); Data.Source.PropertyChanged += (s, e) => Ribbon.Raise(); Data.Busy.PropertyChanged += (s, e) => Ribbon.Raise(); SetCommands(); Post(() => Model.Setup(App.Arguments)); }
/* ----------------------------------------------------------------- */ /// /// Save /// /// <summary> /// Save the PDF document /// </summary> /// /// <param name="src">Source object.</param> /// <param name="dest">Saving file information.</param> /// <param name="prev">Action to be invoked before saving.</param> /// <param name="next">Action to be invoked after saving.</param> /// /* ----------------------------------------------------------------- */ public static void Save(this MainFacade src, string dest, Action <Entity> prev, Action <Entity> next) { var obj = src.Value; var itext = obj.Source.GetItext(obj.Query, obj.IO, false); obj.Set(itext.Metadata, itext.Encryption); src.Save(itext, new SaveOption(obj.IO) { Target = SaveTarget.All, Split = false, Destination = dest, Metadata = obj.Metadata, Encryption = obj.Encryption, Attachments = itext.Attachments, }, prev, next); }
/* ----------------------------------------------------------------- */ /// /// RibbonViewModel /// /// <summary> /// Initializes a new instance of the RibbonViewModel /// class with the specified arguments. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="aggregator">Message aggregator.</param> /// <param name="context">Synchronization context.</param> /// /* ----------------------------------------------------------------- */ public RibbonViewModel(MainFacade src, Aggregator aggregator, SynchronizationContext context ) : base(src, aggregator, context) { }
/* ----------------------------------------------------------------- */ /// /// Extract /// /// <summary> /// Extracts PDF pages saves to a file with the specified options. /// </summary> /// /// <param name="src">Source object.</param> /// <param name="options">Save options.</param> /// /* ----------------------------------------------------------------- */ public static void Extract(this MainFacade src, SaveOption options) => src.Save( null, options, e => src.Backup.Invoke(e), e => src.Value.Set(Properties.Resources.MessageSaved, e.FullName) );
/* ----------------------------------------------------------------- */ /// /// Select /// /// <summary> /// Sets or resets the IsSelected property of all items according /// to the current condition. /// </summary> /// /// <param name="src">Facade object.</param> /// /* ----------------------------------------------------------------- */ public static void Select(this MainFacade src) => src.Select(src.Bindable.Images.Selection.Count < src.Bindable.Images.Count);
/* ----------------------------------------------------------------- */ /// /// StartProcess /// /// <summary> /// Starts a new process with the specified arguments. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="args">User arguments.</param> /// /* ----------------------------------------------------------------- */ public static void StartProcess(this MainFacade src, string args) => Process.Start(new ProcessStartInfo { FileName = Assembly.GetExecutingAssembly().Location, Arguments = args });
/* ----------------------------------------------------------------- */ /// /// Insert /// /// <summary> /// Inserts the specified file behind the selected index. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="files">Collection of inserting files.</param> /// /* ----------------------------------------------------------------- */ public static void Insert(this MainFacade src, IEnumerable <string> files) => src.Insert(src.Bindable.Images.Selection.Last + 1, files);
/* ----------------------------------------------------------------- */ /// /// GetFirst /// /// <summary> /// Gets the first item of the specified collection /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="files">File collection.</param> /// /* ----------------------------------------------------------------- */ public static string GetFirst(this MainFacade src, IEnumerable <string> files) => files.FirstOrDefault(e => e.IsPdf());
/* ----------------------------------------------------------------- */ /// /// Overwrite /// /// <summary> /// Overwrites the PDF document. /// </summary> /// /// <param name="src">Source object.</param> /// /* ----------------------------------------------------------------- */ public static void Overwrite(this MainFacade src) => src.Value.History.Undoable.Then(() => src.Save(src.Value.Source.FullName));
/* ----------------------------------------------------------------- */ /// /// Save /// /// <summary> /// Saves the PDF document to the specified file path. /// </summary> /// /// <param name="src">Facade object.</param> /// <param name="dest">File path.</param> /// /* ----------------------------------------------------------------- */ public static void Save(this MainFacade src, string dest) => src.Save(dest, true);