Skip to content

Commit

Permalink
Fix FileSession generating empty values.
Browse files Browse the repository at this point in the history
  • Loading branch information
VPKSoft committed Jun 9, 2021
1 parent bc016cf commit 422f679
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ScriptNotepad/Database/Entity/Entities/FileSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public class FileSave: IEntity
/// Gets or sets the session the <see cref="FileSave"/> belongs to.
/// </summary>
[ForeignKey(nameof(SessionId))]
public virtual FileSession Session { get; set; } = new();
public virtual FileSession? Session { get; set; }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class MiscellaneousTextEntry: IEntity
/// Gets or sets the <see cref="Session"/> this miscellaneous text data belongs to.
/// </summary>
[ForeignKey(nameof(SessionId))]
public virtual FileSession Session { get; set; } = new();
public virtual FileSession? Session { get; set; }

/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
Expand Down
2 changes: 1 addition & 1 deletion ScriptNotepad/Database/Entity/Entities/RecentFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class RecentFile: IEntity
/// Gets or sets the session the recent file belongs to.
/// </summary>
[ForeignKey(nameof(SessionId))]
public virtual FileSession Session { get; set; } = new();
public virtual FileSession? Session { get; set; }

/// <summary>
/// Gets or sets a string representing the encoding of the file save.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class SearchAndReplaceHistory : IEntity
/// Gets or sets the session the search or replace history entry belongs to.
/// </summary>
[ForeignKey(nameof(SessionId))]
public virtual FileSession Session { get; set; } = new();
public virtual FileSession? Session { get; set; }

/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
Expand Down
32 changes: 27 additions & 5 deletions ScriptNotepad/Database/Entity/Migrations/02_FoldSave.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region License
/*
MIT License
Copyright(c) 2021 Petteri Kautonen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion

using System;
using FluentMigrator;

namespace ScriptNotepad.Database.Entity.Migrations
Expand Down
57 changes: 57 additions & 0 deletions ScriptNotepad/Database/Entity/Migrations/03_FixEmptySession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region License
/*
MIT License
Copyright(c) 2021 Petteri Kautonen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion

using System;
using FluentMigrator;

namespace ScriptNotepad.Database.Entity.Migrations
{
/// <summary>
/// Fixes the empty session bug from the database.
/// Implements the <see cref="FluentMigrator.Migration" />
/// </summary>
/// <seealso cref="FluentMigrator.Migration" />
[Migration(2021_0609_21_15_38)]
public class FixEmptySession : Migration
{
/// <summary>
/// Collect the UP migration expressions
/// </summary>
public override void Up()
{
Execute.Sql("DELETE FROM FileSessions WHERE SessionName IS NULL");
}

/// <summary>
/// Collects the DOWN migration expressions
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public override void Down()
{
// nothing to do here..
}
}
}
1 change: 1 addition & 0 deletions ScriptNotepad/Editor/EntityHelpers/FileSaveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public static void PopPreviousDbModified(this FileSave fileSave)
{
if (fileSave.TemporaryFileSaveName == null && fileSave.UseFileSystemOnContents == true)
{
fileSave.Session ??= new FileSession();
fileSave.Session.SetRandomPath();
fileSave.TemporaryFileSaveName = Path.Combine(fileSave.Session.TemporaryFilePath ?? string.Empty,
Path.GetRandomFileName());
Expand Down
2 changes: 2 additions & 0 deletions ScriptNotepad/Editor/EntityHelpers/RecentFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static Encoding GetEncoding(this RecentFile recentFile)
/// Gets the encoding of the recent file.
/// </summary>
/// <param name="recentFile">The <see cref="RecentFile"/> instance.</param>
/// <param name="value">The encoding to set for the recent file.</param>
public static void SetEncoding(this RecentFile recentFile, Encoding value)
{
recentFile.EncodingAsString = EncodingData.EncodingToString(value);
Expand All @@ -62,6 +63,7 @@ public static void SetEncoding(this RecentFile recentFile, Encoding value)
/// Gets a value indicating whether a snapshot of the file in question exists in the database.
/// </summary>
/// <param name="recentFile">The <see cref="RecentFile"/> instance.</param>
/// <param name="fileSaves">The <see cref="FileSave"/> entities to compare the specified <see cref="RecentFile"/> to.</param>
public static bool ExistsInDatabase(this RecentFile recentFile, DbSet<FileSave> fileSaves)
{
return fileSaves.Count(f => f.FileNameFull == recentFile.FileNameFull && f.Session.SessionName == recentFile.Session.SessionName && f.IsHistory) > 0;
Expand Down
1 change: 1 addition & 0 deletions ScriptNotepad/Localization/StatusStripTexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static bool Initialized
/// <param name="labelSessionName">The status label for a session name text.</param>
/// <param name="labelModeInsertOverride">The status label indicating whether a document editor is in insert or in override mode text.</param>
/// <param name="labelZoom">A status label indicating the zoom value.</param>
/// <param name="labelTab">The status label indicating the selected tab page in the view.</param>
public static void InitLabels(
ToolStripStatusLabel labelLineColumn,
ToolStripStatusLabel labelLineColumnSelection,
Expand Down
6 changes: 3 additions & 3 deletions ScriptNotepad/ScriptNotepad.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<Product>ScriptNotepad</Product>
<Description>A tabbed notepad software with scripting support (C#).</Description>
<Copyright>Copyright © VPKSoft 2020</Copyright>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<AssemblyVersion>1.1.1.0</AssemblyVersion>
<FileVersion>1.1.1.0</FileVersion>
<DocumentationFile>bin\$(Configuration)\ScriptNotepad.xml</DocumentationFile>
<LangVersion>latest</LangVersion>
<OutputPath>bin\$(Configuration)\</OutputPath>
Expand All @@ -31,7 +31,7 @@
<RepositoryType>git</RepositoryType>
<PackageIcon>ScriptNotepad_icon.png</PackageIcon>
<PackageTags>notepad script editor</PackageTags>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<PackageReleaseNotes>See: https://github.com/VPKSoft/ScriptNotepad</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions ScriptNotepad/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,12 @@ public Color GetMarkColor(int index)
// UTF7..
if (enc.CodePage == 65000)
{
#pragma warning disable 618
#pragma warning disable SYSLIB0001 // Type or member is obsolete
// the UTF7 encoding is required to access legacy files..
enc = new UTF7Encoding(bool.Parse(encoding.Split(';')[1]));
#pragma warning restore SYSLIB0001 // Type or member is obsolete
#pragma warning restore 618
}

// UTF8..
Expand Down
8 changes: 8 additions & 0 deletions ScriptNotepad/Settings/SettingsOld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ internal void MoveSettings(Settings settingsNew)
settingsNew.BraceHighlightForegroundColor = BraceHighlightForegroundColor;
settingsNew.BraceHighlightBackgroundColor = BraceHighlightBackgroundColor;
settingsNew.BraceBadHighlightForegroundColor = BraceBadHighlightForegroundColor;
#pragma warning disable 618
// this is for backwards compatibility..
settingsNew.DefaultEncoding = DefaultEncoding;
#pragma warning restore 618
settingsNew.LocalizeThread = LocalizeThread;
settingsNew.AutoDetectEncoding = AutoDetectEncoding;
settingsNew.DetectNoBom = DetectNoBom;
Expand Down Expand Up @@ -1060,7 +1063,12 @@ public StringComparison TextCurrentComparison
// UTF7..
if (enc.CodePage == 65000)
{
#pragma warning disable 618
#pragma warning disable SYSLIB0001 // Type or member is obsolete
// the UTF7 encoding is required to access legacy files..
enc = new UTF7Encoding(bool.Parse(encoding.Split(';')[1]));
#pragma warning restore SYSLIB0001 // Type or member is obsolete
#pragma warning restore 618
}

// UTF8..
Expand Down
5 changes: 5 additions & 0 deletions ScriptNotepad/UtilityClasses/Encodings/DetectEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ public static Encoding FromStream(MemoryStream stream)
ByteMatch(bytes, Utf7Bom2) ||
ByteMatch(bytes, Utf7Bom1))
{
#pragma warning disable 618
#pragma warning disable SYSLIB0001 // Type or member is obsolete
// the UTF7 encoding is required to access legacy files..
return new UTF7Encoding(false);
#pragma warning restore SYSLIB0001 // Type or member is obsolete
#pragma warning restore 618
}

if (ByteMatch(bytes, Utf8Bom))
Expand Down

0 comments on commit 422f679

Please sign in to comment.