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

Feature: Show error when a shortcut can't be created #11013

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a1cb1c7
Feature: "Can't create shortcut" modal
ferrariofilippo Jan 14, 2023
a479eaf
ShortcutCompleteName default value
ferrariofilippo Jan 14, 2023
e34555f
Resource value
ferrariofilippo Jan 15, 2023
7d0fc3a
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
ferrariofilippo Jan 15, 2023
0968fad
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
yaira2 Jan 15, 2023
ed5d4c0
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
ferrariofilippo Jan 19, 2023
98a833a
Modal description & Bug fix
ferrariofilippo Jan 19, 2023
ac3a76a
Spacing
ferrariofilippo Jan 19, 2023
4df9c26
Added back binding
ferrariofilippo Jan 19, 2023
c5df826
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
ferrariofilippo Jan 19, 2023
c4f23d6
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
ferrariofilippo Jan 22, 2023
ee188d1
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
QuaintMako Jan 24, 2023
7af7b3b
Strings
ferrariofilippo Jan 24, 2023
aa6cd6c
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
ferrariofilippo Jan 24, 2023
bfe5618
Merge branch 'main' into Feature_Cannot_Create_Shortcut_Dialog
yaira2 Feb 1, 2023
d2a3d61
Update ShellFilesystemOperations.cs
yaira2 Feb 1, 2023
ca87b3f
Update ShellFilesystemOperations.cs
yaira2 Feb 1, 2023
6ab9604
Update UIFilesystemHelpers.cs
yaira2 Feb 1, 2023
c1f17dc
Update src/Files.App/Strings/en-US/Resources.resw
yaira2 Feb 1, 2023
f7dbb69
Update CreateShortcutDialogViewModel.cs
yaira2 Feb 1, 2023
4885899
Update CreateShortcutDialogViewModel.cs
yaira2 Feb 1, 2023
6faffcb
Update Resources.resw
yaira2 Feb 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ public async Task<IStorageHistory> CreateShortcutItemsAsync(IList<IStorageItemWi
var items = source.Zip(destination, (src, dest, index) => new { src, dest, index }).Where(x => !string.IsNullOrEmpty(x.src.Path) && !string.IsNullOrEmpty(x.dest));
foreach (var item in items)
{
if (await FileOperationsHelpers.CreateOrUpdateLinkAsync(item.dest, item.src.Path))
var result = await FileOperationsHelpers.CreateOrUpdateLinkAsync(item.dest, item.src.Path);
if (!result)
result = await UIFilesystemHelpers.HandleShortcutCannotBeCreated(Path.GetFileName(item.dest), item.src.Path);
if (result)
{
createdSources.Add(item.src);
createdDestination.Add(StorageHelpers.FromPathAndType(item.dest, FilesystemItemType.File));
Expand Down
20 changes: 20 additions & 0 deletions src/Files.App/Helpers/UIFilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Files.App.ViewModels;
using Files.App.ViewModels.Dialogs;
using Files.Backend.Enums;
using Files.Backend.Extensions;
using Files.Backend.Services;
using Files.Shared;
using Files.Shared.Enums;
Expand Down Expand Up @@ -395,6 +396,25 @@ public static async Task CreateShortcutFromDialogAsync(IShellPage associatedInst
var viewModel = new CreateShortcutDialogViewModel(associatedInstance.FilesystemViewModel.WorkingDirectory);
var dialogService = Ioc.Default.GetRequiredService<IDialogService>();
await dialogService.ShowDialogAsync(viewModel);

if (viewModel.ShortcutCreatedSuccessfully)
return;

await HandleShortcutCannotBeCreated(viewModel.ShortcutCompleteName, viewModel.DestinationItemPath);
}

public static async Task<bool> HandleShortcutCannotBeCreated(string shortcutName, string destinationPath)
{
var result = await DialogDisplayHelper.ShowDialogAsync(
"CannotCreateShortcutDialogTitle".ToLocalized(),
"CannotCreateShortcutDialogMessage".ToLocalized(),
"Create".ToLocalized(),
"Cancel".ToLocalized());
if (!result)
return false;

var shortcutPath = Path.Combine(CommonPaths.DesktopPath, shortcutName);
return await FileOperationsHelpers.CreateOrUpdateLinkAsync(shortcutPath, destinationPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ public virtual async void CreateShortcut(RoutedEventArgs e)
{
foreach (ListedItem selectedItem in SlimContentPage.SelectedItems)
{
var filePath = Path.Combine(associatedInstance.FilesystemViewModel.WorkingDirectory,
string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk");
var fileName = string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk";
var filePath = Path.Combine(associatedInstance.FilesystemViewModel.WorkingDirectory, fileName);

await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath);
if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))
await UIFilesystemHelpers.HandleShortcutCannotBeCreated(fileName, selectedItem.ItemPath);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2877,4 +2877,10 @@
<data name="ReleaseNotes" xml:space="preserve">
<value>Release Notes</value>
</data>
<data name="CannotCreateShortcutDialogTitle" xml:space="preserve">
<value>Files is not authorized to create a shortcut here.</value>
ferrariofilippo marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="CannotCreateShortcutDialogMessage" xml:space="preserve">
<value>Do you want to place the shortcut on the desktop?</value>
</data>
</root>
27 changes: 18 additions & 9 deletions src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ public class CreateShortcutDialogViewModel : ObservableObject
public readonly string WorkingDirectory;

// Tells whether destination path exists
public bool DestinationPathExists { get; set; } = false;
public bool DestinationPathExists { get; set; }

// Tells wheteher the shortcut has been created
public bool ShortcutCreatedSuccessfully { get; private set; }

// Shortcut name with extension
public string ShortcutCompleteName { get; private set; } = string.Empty;

// Destination of the shortcut chosen by the user (can be a path or a URL)
private string _destinationItemPath;
Expand Down Expand Up @@ -73,7 +79,12 @@ private async Task CreateShortcut()
if (DestinationPathExists)
{
destinationName = Path.GetFileName(DestinationItemPath);
destinationName ??= Path.GetDirectoryName(DestinationItemPath);
if (destinationName is null)
{
if (!DestinationItemPath.EndsWith('\\'))
DestinationItemPath += "\\";
destinationName = Path.GetDirectoryName(DestinationItemPath);
}
}
else
{
Expand All @@ -82,19 +93,17 @@ private async Task CreateShortcut()
}

var shortcutName = string.Format("ShortcutCreateNewSuffix".ToLocalized(), destinationName);
var filePath = Path.Combine(
WorkingDirectory,
shortcutName + extension);
ShortcutCompleteName = shortcutName + extension;
var filePath = Path.Combine(WorkingDirectory, ShortcutCompleteName);

int fileNumber = 1;
while (Path.Exists(filePath))
{
filePath = Path.Combine(
WorkingDirectory,
shortcutName + $" ({++fileNumber})" + extension);
ShortcutCompleteName = shortcutName + $" ({++fileNumber})" + extension;
filePath = Path.Combine(WorkingDirectory, ShortcutCompleteName);
}

await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, DestinationItemPath);
ShortcutCreatedSuccessfully = await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, DestinationItemPath);
}
}
}