Exemplo n.º 1
0
 /// <summary>
 /// New file which wraps another file.
 /// </summary>
 /// <param name="file">The base file.</param>
 public WrapFile(FarFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     _File = file;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Saves the panel state.
        /// </summary>
        /// <remarks>
        /// It is called when the panel is about to be offline (pushed or replaced by a child panel).
        /// The panel UI state is saved by the core (view and sort modes, etc.).
        /// The base method posts the current file to be restored as current.
        /// It is important to have the proper <see cref="FarNet.Explorer.FileComparer"/>.
        /// </remarks>
        protected virtual void SaveState()
        {
            FarFile file = CurrentFile;

            if (file != null)
            {
                PostFile(file);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Opens the file in the viewer.
        /// </summary>
        /// <param name="file">The file to view.</param>
        /// <remarks>
        /// The default method calls <see cref="FarNet.Explorer.GetContent"/> to get a temporary file to view.
        /// The explorer should have it implemented.
        /// </remarks>
        public virtual void UIViewFile(FarFile file)
        {
            if (file == null)
            {
                return;
            }

            // target
            var temp = Far.Api.TempName();

            // export
            var xExportArgs = WorksExportExplorerFile(Explorer, this, ExplorerModes.View, file, temp);

            if (xExportArgs == null)
            {
                return;
            }

            // case: actual file exists
            var asExportFileEventArgs = xExportArgs as GetContentEventArgs;

            if (asExportFileEventArgs != null && !string.IsNullOrEmpty(asExportFileEventArgs.UseFileName))
            {
                var viewerActual = Far.Api.CreateViewer();
                viewerActual.FileName = asExportFileEventArgs.UseFileName;
                viewerActual.Title    = file.Name;
                viewerActual.Open();
                return;
            }

            // viewer
            var viewerTemp = Far.Api.CreateViewer();

            viewerTemp.DeleteSource   = DeleteSource.File;
            viewerTemp.DisableHistory = true;
            viewerTemp.FileName       = temp;
            viewerTemp.Title          = file.Name;
            viewerTemp.Switching      = Switching.Enabled;
            if (asExportFileEventArgs.CodePage != 0)
            {
                viewerTemp.CodePage = asExportFileEventArgs.CodePage;
            }

            // open
            viewerTemp.Open();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates file data snapshot from a <see cref="FarFile"/> object.
        /// </summary>
        /// <param name="file">Any panel file which data are taken.</param>
        public SetFile(FarFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            Attributes     = file.Attributes;
            CreationTime   = file.CreationTime;
            Data           = file.Data;
            Description    = file.Description;
            LastAccessTime = file.LastAccessTime;
            LastWriteTime  = file.LastWriteTime;
            Length         = file.Length;
            Name           = file.Name;
            Owner          = file.Owner;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Opens the file.
        /// </summary>
        /// <remarks>
        /// It is called for the current file when [Enter] is pressed.
        /// The base method just calls <see cref="FarNet.Explorer.OpenFile"/> if the explorer supports it.
        /// </remarks>
        /// <param name="file">The file to be opened.</param>
        public virtual void UIOpenFile(FarFile file)
        {
            if (file == null)
            {
                return;
            }

            if (!Explorer.CanOpenFile)
            {
                return;
            }

            var args     = new OpenFileEventArgs(file);
            var explorer = UIOpenFile(args);

            if (explorer != null)
            {
                explorer.OpenPanelChild(this);
            }
        }
Exemplo n.º 6
0
        ///
        public static GetContentEventArgs WorksExportExplorerFile(Explorer explorer, Panel panel, ExplorerModes mode, FarFile file, string fileName)
        {
            if (explorer == null)
            {
                throw new ArgumentNullException("explorer");
            }
            if (panel == null)
            {
                throw new ArgumentNullException("panel");
            }

            if (!explorer.CanGetContent)
            {
                return(null);
            }

            // export file
            Log.Source.TraceInformation("ExportFile");
            var args = new GetContentEventArgs(mode, file, fileName);

            panel.UIGetContent(args);
            if (args.Result != JobResult.Done)
            {
                return(null);
            }

            // no text or an actual file exists?
            if (args.UseText == null || !string.IsNullOrEmpty(args.UseFileName))
            {
                return(args);
            }

            // export text
            string text = args.UseText as string;

            if (text == null)
            {
                IEnumerable collection = args.UseText as IEnumerable;
                if (collection == null)
                {
                    text = args.UseText.ToString();
                }
                else
                {
                    // write collection
                    using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.Unicode))
                        foreach (var it in collection)
                        {
                            writer.WriteLine(it);
                        }
                    return(args);
                }
            }

            // write text
            File.WriteAllText(fileName, text, Encoding.Unicode);
            args.CodePage = 1200;
            return(args);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Opens the file in the editor.
        /// </summary>
        /// <param name="file">The file to edit.</param>
        /// <remarks>
        /// The default method calls <see cref="FarNet.Explorer.GetContent"/>  to get a temporary file to edit
        /// and <see cref="FarNet.Explorer.SetFile"/> to save changes when the editor closes.
        /// The explorer should have at least export implemented.
        /// </remarks>
        public virtual void UIEditFile(FarFile file)
        {
            if (file == null)
            {
                return;
            }

            // target
            var temp = Far.Api.TempName();

            // export
            var xExportArgs = WorksExportExplorerFile(Explorer, this, ExplorerModes.Edit, file, temp);

            if (xExportArgs == null)
            {
                return;
            }

            // case: actual file exists
            var asExportFileEventArgs = xExportArgs as GetContentEventArgs;

            if (asExportFileEventArgs != null && !string.IsNullOrEmpty(asExportFileEventArgs.UseFileName))
            {
                var editorActual = Far.Api.CreateEditor();
                editorActual.FileName = asExportFileEventArgs.UseFileName;
                editorActual.Title    = file.Name;
                if (!asExportFileEventArgs.CanSet)
                {
                    editorActual.IsLocked = true;
                }
                editorActual.Open();
                return;
            }

            // rename
            if (!string.IsNullOrEmpty(xExportArgs.UseFileExtension))
            {
                string temp2 = temp + xExportArgs.UseFileExtension;
                File.Move(temp, temp2);
                temp = temp2;
            }

            // editor
            var editorTemp = Far.Api.CreateEditor();

            editorTemp.DeleteSource   = DeleteSource.File;
            editorTemp.DisableHistory = true;
            editorTemp.FileName       = temp;
            editorTemp.Title          = file.Name;
            if (asExportFileEventArgs.CodePage != 0)
            {
                editorTemp.CodePage = asExportFileEventArgs.CodePage;
            }

            // future
            if (xExportArgs.CanSet)
            {
                if (Explorer.CanSetText)
                {
                    editorTemp.Saving += delegate
                    {
                        var xImportTextArgs = new SetTextEventArgs(ExplorerModes.Edit, file, editorTemp.GetText());
                        Log.Source.TraceInformation("ImportText");
                        UISetText(xImportTextArgs);
                    };
                }
                else
                {
                    editorTemp.Closed += delegate                     //???? to use Saved (Far 3), update docs.
                    {
                        if (editorTemp.TimeOfSave == DateTime.MinValue)
                        {
                            return;
                        }

                        var xImportFileArgs = new SetFileEventArgs(ExplorerModes.Edit, file, temp);
                        Log.Source.TraceInformation("ImportFile");
                        UISetFile(xImportFileArgs);
                    };
                }
            }
            else
            {
                // to lock
                editorTemp.IsLocked = true;
            }

            // go
            editorTemp.Open();
        }
Exemplo n.º 8
0
 /// <summary>
 /// Posts the file to be found and set current on redrawing.
 /// </summary>
 /// <param name="file">The file to be found on redrawing.</param>
 /// <remarks>
 /// The posted file is ignored if <see cref="PostData"/> or <see cref="PostName"/> were called.
 /// The <see cref="FarNet.Explorer.FileComparer"/> is used in order to find the file.
 /// </remarks>
 public void PostFile(FarFile file)
 {
     _Panel.PostFile(file);
 }
Exemplo n.º 9
0
 /// <param name="mode">See <see cref="ExplorerEventArgs.Mode"/></param>
 /// <param name="file">See <see cref="ExplorerFileEventArgs.File"/></param>
 /// <param name="text">See <see cref="Text"/></param>
 public SetTextEventArgs(ExplorerModes mode, FarFile file, string text) : base(mode, file)
 {
     Text = text;
 }
Exemplo n.º 10
0
 /// <param name="mode">See <see cref="ExplorerEventArgs.Mode"/></param>
 /// <param name="file">See <see cref="ExplorerFileEventArgs.File"/></param>
 /// <param name="fileName">See <see cref="FileName"/></param>
 public SetFileEventArgs(ExplorerModes mode, FarFile file, string fileName) : base(mode, file)
 {
     FileName = fileName;
 }
Exemplo n.º 11
0
 /// <param name="mode">See <see cref="ExplorerEventArgs.Mode"/></param>
 /// <param name="file">See <see cref="ExplorerFileEventArgs.File"/></param>
 public RenameFileEventArgs(ExplorerModes mode, FarFile file) : base(mode, file)
 {
 }
Exemplo n.º 12
0
 /// <param name="file">See <see cref="ExplorerFileEventArgs.File"/></param>
 public OpenFileEventArgs(FarFile file) : base(ExplorerModes.None, file)
 {
 }
Exemplo n.º 13
0
 /// <param name="mode">See <see cref="ExplorerEventArgs.Mode"/></param>
 /// <param name="file">See <see cref="File"/></param>
 protected ExplorerFileEventArgs(ExplorerModes mode, FarFile file) : base(mode)
 {
     File = file;
 }
Exemplo n.º 14
0
 /// <param name="mode">See <see cref="ExplorerEventArgs.Mode"/></param>
 /// <param name="file">See <see cref="File"/></param>
 public ExploreDirectoryEventArgs(ExplorerModes mode, FarFile file) : base(mode)
 {
     File = file;
 }
Exemplo n.º 15
0
        /// <summary>
        /// Opens the file in the editor.
        /// </summary>
        /// <param name="file">The file to edit.</param>
        /// <remarks>
        /// The default method calls <see cref="FarNet.Explorer.GetContent"/>  to get a temporary file to edit
        /// and <see cref="FarNet.Explorer.SetFile"/> to save changes when the editor closes.
        /// The explorer should have at least export implemented.
        /// </remarks>
        public virtual void UIEditFile(FarFile file)
        {
            if (file == null)
            {
                return;
            }

            // target file path
            // _201223_vc Avoid Far.Api.TempName(). I think it reuses names if files do not exist. But file history may exist unexpectedly.
            var temp = Kit.TempFileName(null);

            // export
            var xExportArgs = WorksExportExplorerFile(Explorer, this, ExplorerModes.Edit, file, temp);

            if (xExportArgs == null)
            {
                return;
            }

            // case: actual file exists
            if (!string.IsNullOrEmpty(xExportArgs.UseFileName))
            {
                var editorActual = Far.Api.CreateEditor();
                editorActual.FileName = xExportArgs.UseFileName;
                editorActual.Title    = file.Name;
                if (!xExportArgs.CanSet)
                {
                    editorActual.IsLocked = true;
                }
                if (xExportArgs.EditorOpened != null)
                {
                    editorActual.Opened += xExportArgs.EditorOpened;
                }
                editorActual.Open();
                return;
            }

            // rename
            if (!string.IsNullOrEmpty(xExportArgs.UseFileExtension))
            {
                var temp2 = Path.ChangeExtension(temp, xExportArgs.UseFileExtension);
                File.Move(temp, temp2);
                temp = temp2;
            }

            // editor
            var editorTemp = Far.Api.CreateEditor();

            editorTemp.DisableHistory = true;
            editorTemp.FileName       = temp;
            editorTemp.Title          = file.Name;
            if (xExportArgs.CodePage != 0)
            {
                editorTemp.CodePage = xExportArgs.CodePage;
            }
            if (xExportArgs.EditorOpened != null)
            {
                editorTemp.Opened += xExportArgs.EditorOpened;
            }

            // future
            //! Not sure why but with used DisableHistory the file reopens with the last caret restored.
            //! If this is some Far feature and it changes then save and set the last caret manually.
            var timeError = DateTime.MinValue;

            if (xExportArgs.CanSet)
            {
                if (Explorer.CanSetText)
                {
                    editorTemp.Saving += delegate
                    {
                        var xImportTextArgs = new SetTextEventArgs(ExplorerModes.Edit, file, editorTemp.GetText());
                        Log.Source.TraceInformation("ImportText");
                        try
                        {
                            timeError = DateTime.MinValue;
                            UISetText(xImportTextArgs);
                        }
                        catch (Exception exn)
                        {
                            //! show first, then save error time
                            Far.Api.ShowError("Cannot set text", exn);
                            timeError = DateTime.UtcNow;
                        }
                    };
                    editorTemp.Closed += delegate
                    {
                        //! if error was on saving without closing then on closing "much later" without new saving ignore error
                        if (timeError != DateTime.MinValue && (DateTime.UtcNow - timeError).TotalSeconds > 1)
                        {
                            timeError = DateTime.MinValue;
                        }
                    };
                }
                else
                {
                    editorTemp.Closed += delegate
                    {
                        if (editorTemp.TimeOfSave == DateTime.MinValue)
                        {
                            return;
                        }

                        var xImportFileArgs = new SetFileEventArgs(ExplorerModes.Edit, file, temp);
                        Log.Source.TraceInformation("ImportFile");
                        try
                        {
                            UISetFile(xImportFileArgs);
                        }
                        catch (Exception exn)
                        {
                            Far.Api.ShowError("Cannot set file", exn);
                            timeError = DateTime.UtcNow;
                        }
                    };
                }
            }
            else
            {
                // lock, do nothing else
                editorTemp.IsLocked = true;
            }

            // loop while errors on saving
            async void Loop()
            {
                try
                {
                    for (; ;)
                    {
                        timeError = DateTime.MinValue;

                        await Tasks.Editor(editorTemp);

                        if (timeError == DateTime.MinValue)
                        {
                            break;
                        }
                    }
                }
                finally
                {
                    File.Delete(temp);
                }
            }

            Loop();
        }
Exemplo n.º 16
0
 /// <summary>
 /// New file which wraps another file.
 /// </summary>
 /// <param name="file">The base file.</param>
 public WrapFile(FarFile file)
 {
     _File = file ?? throw new ArgumentNullException("file");
 }