Example #1
0
        /// <summary>
        /// Sets the contents of the <see cref="FileSave"/> class instance.
        /// </summary>
        /// <param name="fileSave">The file save of which contents to set.</param>
        /// <param name="contents">The contents as a string.</param>
        /// <param name="commit">A value indicating whether to commit the changes to the
        /// database or to the file system cache depending on the setting.</param>
        /// <param name="saveToFileSystem">A value indicating whether to override existing copy of the file in the file system.</param>
        /// <param name="contentChanged">A value indicating whether the file contents have been changed.</param>
        /// <returns>An instance to a <see cref="FileSave"/> modified class.</returns>
        public static FileSave SetContents(this FileSave fileSave, string contents, bool commit,
                                           bool saveToFileSystem, bool contentChanged)
        {
            fileSave.SetFileContents(fileSave.GetEncoding().GetBytes(contents), commit, saveToFileSystem, contentChanged);

            return(fileSave);
        }
Example #2
0
        /// <summary>
        /// Adds or updates a <see cref="RecentFile"/> entity into the database.
        /// </summary>
        /// <param name="fileSave">The <see cref="FileSave"/> entity to use for a recent file data.</param>
        /// <returns><c>true</c> if the operation was successful, <c>false</c> otherwise.</returns>
        public static bool AddOrUpdateRecentFile(FileSave fileSave)
        {
            try
            {
                var dbContext  = ScriptNotepadDbContext.DbContext;
                var recentFile = dbContext.RecentFiles.FirstOrDefault(f =>
                                                                      f.FileNameFull == fileSave.FileNameFull && f.Session.SessionName == fileSave.Session.SessionName);

                if (recentFile != null)
                {
                    recentFile.ClosedDateTime = DateTime.Now;
                    recentFile.SetEncoding(fileSave.GetEncoding());
                }
                else
                {
                    dbContext.RecentFiles.Add(new RecentFile
                    {
                        FileNameFull     = fileSave.FileNameFull,
                        Session          = fileSave.Session,
                        EncodingAsString = EncodingData.EncodingToString(fileSave.GetEncoding()),
                        ClosedDateTime   = DateTime.Now,
                        FileName         = fileSave.FileName,
                        FilePath         = fileSave.FilePath,
                    });
                }

                dbContext.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                // log the exception..
                ErrorHandlingBase.ExceptionLogAction?.Invoke(ex);
                return(false);
            }
        }
Example #3
0
        /// <summary>
        /// Adds the or update file.
        /// </summary>
        /// <param name="fileSave">A <see cref="FileSave"/> class instance to be added or updated into the database.</param>
        /// <param name="document">An instance to a ScintillaTabbedDocument class.</param>
        /// <param name="commit">A value indicating whether to commit the changes to the
        /// database or to the file system cache depending on the setting.</param>
        /// <param name="saveToFileSystem">A value indicating whether to override existing copy of the file in the file system.</param>
        /// <param name="contentChanged">A value indicating whether the file contents have been changed.</param>
        /// <returns>An instance to a <see cref="FileSave"/> modified class.</returns>
        public static FileSave AddOrUpdateFile(this FileSave fileSave, ScintillaTabbedDocument document, bool commit,
                                               bool saveToFileSystem, bool contentChanged)
        {
            fileSave.SetFileContents(fileSave.GetEncoding().GetBytes(document.Scintilla.Text), commit, saveToFileSystem, contentChanged);
            fileSave.CurrentCaretPosition = document.Scintilla.CurrentPosition;
            fileSave.FilePath             = Path.GetDirectoryName(fileSave.FileNameFull);
            ScriptNotepadDbContext.DbContext.SaveChanges();

            if (!ScriptNotepadDbContext.DbContext.FileSaves.Any(f => f.Id == fileSave.Id))
            {
                return(ScriptNotepadDbContext.DbContext.FileSaves.Add(fileSave).Entity);
            }

            return(ScriptNotepadDbContext.DbContext.FileSaves.FirstOrDefault(f => f.Id == fileSave.Id));
        }
Example #4
0
        /// <summary>
        /// Adds the or update file.
        /// </summary>
        /// <param name="fileSave">A <see cref="FileSave"/> class instance to be added or updated into the database.</param>
        /// <param name="document">An instance to a ScintillaTabbedDocument class.</param>
        /// <param name="isHistory">if set to <c>true</c> the file is to be considered as a closed/history file.</param>
        /// <param name="sessionName">Name of the session the file belongs to.</param>
        /// <param name="encoding">The encoding of the file.</param>
        /// <param name="commit">A value indicating whether to commit the changes to the
        /// database or to the file system cache depending on the setting.</param>
        /// <param name="saveToFileSystem">A value indicating whether to override existing copy of the file in the file system.</param>
        /// <param name="contentChanged">A value indicating whether the file contents have been changed.</param>
        /// <returns>An instance to a <see cref="FileSave"/> modified class.</returns>
        public static FileSave AddOrUpdateFile(this FileSave fileSave, ScintillaTabbedDocument document, bool isHistory,
                                               string sessionName, Encoding encoding, bool commit,
                                               bool saveToFileSystem, bool contentChanged)
        {
            fileSave.SetFileContents(fileSave.GetEncoding().GetBytes(document.Scintilla.Text), commit, saveToFileSystem, contentChanged);
            fileSave.CurrentCaretPosition = document.Scintilla.CurrentPosition;
            fileSave.FilePath             = Path.GetDirectoryName(fileSave.FileNameFull);
            fileSave.IsHistory            = isHistory;
            fileSave.Session =
                ScriptNotepadDbContext.DbContext.FileSessions.FirstOrDefault(f => f.SessionName == sessionName);

            fileSave.SetEncoding(encoding);

            ScriptNotepadDbContext.DbContext.SaveChanges();
            return(ScriptNotepadDbContext.DbContext.FileSaves.FirstOrDefault(f => f.Id == fileSave.Id));
        }
Example #5
0
        /// <summary>
        /// Reloads the contents of the document from the disk.
        /// </summary>
        /// <param name="fileSave">An instance to a <see cref="FileSave"/> class.</param>
        /// <param name="document">A ScintillaTabbedDocument to which contents should also be updated.</param>
        /// <returns>True if the operation was successful; otherwise false.</returns>
        public static bool ReloadFromDisk(this FileSave fileSave, ScintillaTabbedDocument document)
        {
            try
            {
                // can't reload what doesn't exist..
                if (File.Exists(fileSave.FileNameFull))
                {
                    // read the file contents from the file..
                    using (FileStream fileStream = new FileStream(fileSave.FileNameFull, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        // create a byte buffer the contain all the bytes if the file with an assumption
                        // no one wishes to open massive binary files..
                        byte[] fileContents = new byte[fileStream.Length];

                        // read the file contents to the buffer..
                        fileStream.Read(fileContents, 0, (int)fileStream.Length);

                        // set the file system's modified flag..
                        fileSave.FileSystemModified = new FileInfo(fileSave.FileNameFull).LastWriteTime;

                        fileSave.SetDatabaseModified(fileSave.FileSystemModified); // set the other DateTime flags to indicate the same..
                        fileSave.FileSystemSaved = fileSave.FileSystemModified;    // set the other DateTime flags to indicate the same..
                        fileSave.ResetPreviousDbModified();


                        // create a new memory stream to hold the file contents..
                        MemoryStream memoryStream = new MemoryStream(fileContents);

                        document.Scintilla.Text = StreamStringHelpers.MemoryStreamToText(memoryStream, fileSave.GetEncoding());

                        // a reload doesn't need to be undone..
                        document.Scintilla.EmptyUndoBuffer();

                        fileSave.SetFileContentsAsMemoryStream(memoryStream);

                        // set the saved position of the document's caret..
                        if (fileSave.CurrentCaretPosition > 0 && fileSave.CurrentCaretPosition < document.Scintilla.TextLength)
                        {
                            document.Scintilla.CurrentPosition = fileSave.CurrentCaretPosition;
                            document.Scintilla.SelectionStart  = fileSave.CurrentCaretPosition;
                            document.Scintilla.SelectionEnd    = fileSave.CurrentCaretPosition;
                            document.Scintilla.ScrollCaret();
                        }
                    }
                    return(true); // success..
                }
                else
                {
                    return(false); // the file didn't exists, so fail..
                }
            }
            catch (Exception ex)
            {
                // log the exception..
                ErrorHandlingBase.ExceptionLogAction?.Invoke(ex);

                return(false); // an exception occurred, so fail..
            }
        }