Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ask user to create file when non-existing path provided via commandline. #383

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Notepads/Controls/Dialog/FileCreateDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Notepads.Controls.Dialog
{
using System;

public class FileCreateDialog : NotepadsDialog
{
public FileCreateDialog(string filePath, Action createAction)
{
Title = ResourceLoader.GetString("GetFileOpenCreateNewFileReminderDialog_Title");
Content = string.Format(ResourceLoader.GetString("GetFileOpenCreateNewFileReminderDialog_Content"), filePath);
PrimaryButtonText = ResourceLoader.GetString("RevertAllChangesConfirmationDialog_PrimaryButtonText");
CloseButtonText = ResourceLoader.GetString("RevertAllChangesConfirmationDialog_CloseButtonText");

PrimaryButtonClick += (dialog, args) => { createAction(); };
}
}
}
1 change: 1 addition & 0 deletions src/Notepads/Notepads.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<Compile Include="Commands\KeyboardCommand.cs" />
<Compile Include="Commands\KeyboardCommandHandler.cs" />
<Compile Include="Controls\Dialog\AppCloseSaveReminderDialog.cs" />
<Compile Include="Controls\Dialog\FileCreateDialog.cs" />
<Compile Include="Controls\Dialog\FileRenameDialog.cs" />
<Compile Include="Controls\Dialog\FileOpenErrorDialog.cs" />
<Compile Include="Controls\Dialog\FileSaveErrorDialog.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Notepads/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -1181,5 +1181,6 @@
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="confirmAppClose"/>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
</Package>
5 changes: 5 additions & 0 deletions src/Notepads/Services/ActivationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Notepads.Utilities;
using Notepads.Views.MainPage;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml.Controls;

public static class ActivationService
Expand Down Expand Up @@ -98,6 +99,10 @@ private static async Task CommandActivated(Frame rootFrame, CommandLineActivated
{
await mainPage.OpenFile(file);
}
else
{
await mainPage.CreateAndOpenFile();
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Notepads/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,16 @@
<value>File extension "{0}" is not supported at this moment</value>
<comment>FileRenameError: Extension is not currently supported. {0} stands for the file extension string.</comment>
</data>
<data name="GetFileOpenCreateNewFileReminderDialog_Content" xml:space="preserve">
<value>Create file "{0}"?</value>
<comment>GetFileOpenCreateNewFileReminderDialog: "Content" display text. {0} stands for file name/path. You can change the order but DO NOT REMOVE it from the string.</comment>
</data>
<data name="GetFileOpenCreateNewFileReminderDialog_Title" xml:space="preserve">
<value>Can't find the path specified</value>
<comment>GetFileOpenCreateNewFileReminderDialog: "Title" display text.</comment>
</data>
<data name="GetFileOpenCreateNewFileReminderDialog_NotificationMsg_FileCreateError" xml:space="preserve">
<value>Failed to create "{0}"</value>
<comment>GetFileOpenCreateNewFileReminderDialog: Notification message when file creation fails. {0} stands for file name/path. You can change the order but DO NOT REMOVE it from the string.</comment>
</data>
</root>
27 changes: 26 additions & 1 deletion src/Notepads/Utilities/FileSystemUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static class FileSystemUtility

private const string WslRootPath = "\\\\wsl$\\";

private static string _lastErrorFileOpenPath = string.Empty;

// https://stackoverflow.com/questions/62771/how-do-i-check-if-a-given-string-is-a-legal-valid-file-name-under-windows
private static readonly Regex ValidWindowsFileNames = new Regex(@"^(?!(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)(?:\..+)?$)[^\x00-\x1F\xA5\\?*:\"";|\/<>]+(?<![\s.])$", RegexOptions.IgnoreCase);

Expand Down Expand Up @@ -268,8 +270,10 @@ public static async Task<StorageFile> GetFile(string filePath)
{
return await StorageFile.GetFileFromPathAsync(filePath);
}
catch
catch (Exception ex)
{
if (ex is FileNotFoundException) _lastErrorFileOpenPath = filePath;

return null;
}
}
Expand Down Expand Up @@ -596,6 +600,27 @@ public static async Task<StorageFile> CreateFile(StorageFolder folder, string fi
return await folder.CreateFileAsync(fileName, option);
}

public static async Task<StorageFile> CreateFile(string filePath)
{
try
{
if (!string.IsNullOrEmpty(filePath))
{
var pos = filePath.IndexOf(Path.DirectorySeparatorChar, 2);
return await CreateFile(await StorageFolder.GetFolderFromPathAsync(filePath.Substring(0, pos)), filePath.Substring(pos + 1));
}
}
catch (Exception) { }

_lastErrorFileOpenPath = string.Empty;
return null;
}

public static string GetLastErrorFileOpenPath()
{
return _lastErrorFileOpenPath;
}

public static async Task<bool> FileExists(StorageFile file)
{
try
Expand Down
24 changes: 24 additions & 0 deletions src/Notepads/Views/MainPage/NotepadsMainPage.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,29 @@ private static bool HaveNonemptyTextEditor(ITextEditor[] textEditors)
}
return false;
}

public async Task CreateAndOpenFile()
{
if (!(FileSystemUtility.GetLastErrorFileOpenPath() is string lastErrorFileOpenPath) || string.IsNullOrEmpty(lastErrorFileOpenPath)) return;

bool createFile = false;
var createNewFileReminderDialog = new FileCreateDialog(lastErrorFileOpenPath, () =>
{
createFile = true;
});

await DialogManager.OpenDialogAsync(createNewFileReminderDialog, awaitPreviousDialog: true);

var file = await FileSystemUtility.CreateFile(createFile ? lastErrorFileOpenPath : string.Empty);

if (file != null)
{
await OpenFile(file);
}
else if (createFile)
{
NotificationCenter.Instance.PostNotification(string.Format(_resourceLoader.GetString("GetFileOpenCreateNewFileReminderDialog_NotificationMsg_FileCreateError"), lastErrorFileOpenPath), 1500);
}
}
}
}
15 changes: 13 additions & 2 deletions src/Notepads/Views/MainPage/NotepadsMainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ private async void Sets_Loaded(object sender, RoutedEventArgs e)
if (file != null && await OpenFile(file))
{
loadedCount++;
_appLaunchCmdDir = null;
_appLaunchCmdArgs = null;
}
_appLaunchCmdDir = null;
_appLaunchCmdArgs = null;
}
else if (_appLaunchUri != null)
{
Expand Down Expand Up @@ -274,6 +274,17 @@ private async void Sets_Loaded(object sender, RoutedEventArgs e)
Window.Current.CoreWindow.Activated -= CoreWindow_Activated;
Window.Current.CoreWindow.Activated += CoreWindow_Activated;
}
Window.Current.CoreWindow.Activated -= CoreWindow_Activated;
Window.Current.CoreWindow.Activated += CoreWindow_Activated;
Application.Current.EnteredBackground -= App_EnteredBackground;
Application.Current.EnteredBackground += App_EnteredBackground;

if (_appLaunchCmdDir != null)
{
await CreateAndOpenFile();
_appLaunchCmdDir = null;
_appLaunchCmdArgs = null;
}
}

private async void App_EnteredBackground(object sender, Windows.ApplicationModel.EnteredBackgroundEventArgs e)
Expand Down