Skip to content

Refactor Cloud #9427

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

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
96 changes: 51 additions & 45 deletions src/Files.FullTrust/Helpers/CloudDrivesDetector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Files.Shared;
using Files.Shared.Cloud;
using Files.Shared.Extensions;
using Microsoft.Win32;
using System;
Expand All @@ -13,9 +13,9 @@ namespace Files.FullTrust.Helpers
[SupportedOSPlatform("Windows10.0.10240")]
public class CloudDrivesDetector
{
public static async Task<List<CloudProvider>> DetectCloudDrives()
public static async Task<IEnumerable<ICloudProvider>> DetectCloudDrives()
{
var tasks = new Task<List<CloudProvider>>[]
var tasks = new Task<IEnumerable<ICloudProvider>>[]
{
SafetyExtensions.IgnoreExceptions(DetectOneDrive, Program.Logger),
SafetyExtensions.IgnoreExceptions(DetectSharepoint, Program.Logger),
Expand All @@ -25,39 +25,46 @@ public static async Task<List<CloudProvider>> DetectCloudDrives()

await Task.WhenAll(tasks);

return tasks.Where(o => o.Result != null).SelectMany(o => o.Result).OrderBy(o => o.ID.ToString()).ThenBy(o => o.Name).Distinct().ToList();
return tasks
.Where(o => o.Result is not null)
.SelectMany(o => o.Result)
.OrderBy(o => o.ID.ToString())
.ThenBy(o => o.Name)
.Distinct();
}

private static Task<List<CloudProvider>> DetectYandexDisk()
private static Task<IEnumerable<ICloudProvider>> DetectYandexDisk()
{
var results = new List<CloudProvider>();
var results = new List<ICloudProvider>();
using var yandexKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Yandex\Yandex.Disk.2");

var syncedFolder = (string)yandexKey?.GetValue("RootFolder");
if (syncedFolder != null)
if (syncedFolder is not null)
{
results.Add(new CloudProvider()
results.Add(new CloudProvider(CloudProviders.Yandex)
{
ID = CloudProviders.Yandex,
Name = $"Yandex Disk",
SyncFolder = syncedFolder
SyncFolder = syncedFolder,
});
}
return Task.FromResult(results);

return Task.FromResult<IEnumerable<ICloudProvider>>(results);
}

private static Task<List<CloudProvider>> DetectGenericCloudDrive()
private static Task<IEnumerable<ICloudProvider>> DetectGenericCloudDrive()
{
var results = new List<CloudProvider>();
var results = new List<ICloudProvider>();
using var clsidKey = Registry.ClassesRoot.OpenSubKey(@"CLSID");
using var namespaceKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace");

foreach (var subKeyName in namespaceKey.GetSubKeyNames())
{
using var clsidSubKey = SafetyExtensions.IgnoreExceptions(() => clsidKey.OpenSubKey(subKeyName));
if (clsidSubKey != null && (int?)clsidSubKey.GetValue("System.IsPinnedToNameSpaceTree") == 1)
if (clsidSubKey is not null && (int?)clsidSubKey.GetValue("System.IsPinnedToNameSpaceTree") is 1)
{
using var namespaceSubKey = namespaceKey.OpenSubKey(subKeyName);
var driveType = (string)namespaceSubKey?.GetValue("");
if (driveType == null)
var driveType = (string)namespaceSubKey?.GetValue(string.Empty);
if (driveType is null)
{
continue;
}
Expand All @@ -71,7 +78,7 @@ private static Task<List<CloudProvider>> DetectGenericCloudDrive()

using var bagKey = clsidSubKey.OpenSubKey(@"Instance\InitPropertyBag");
var syncedFolder = (string)bagKey?.GetValue("TargetFolderPath");
if (syncedFolder == null)
if (syncedFolder is null)
{
continue;
}
Expand All @@ -83,71 +90,70 @@ private static Task<List<CloudProvider>> DetectGenericCloudDrive()
"Amazon Drive" => CloudProviders.AmazonDrive,
"Nextcloud" => CloudProviders.Nextcloud,
"Jottacloud" => CloudProviders.Jottacloud,
_ => null
_ => null,
};
if (driveID == null)
if (driveID is null)
{
continue;
}

results.Add(new CloudProvider()
string nextCloudValue = (string)namespaceSubKey?.GetValue(string.Empty);
results.Add(new CloudProvider(driveID.Value)
{
ID = driveID.Value,
Name = driveID switch
{
CloudProviders.Mega => $"MEGA ({Path.GetFileName(syncedFolder.TrimEnd('\\'))})",
CloudProviders.AmazonDrive => $"Amazon Drive",
CloudProviders.Nextcloud => $"{ (!string.IsNullOrEmpty((string)namespaceSubKey?.GetValue("")) ? (string)namespaceSubKey?.GetValue(""):"Nextcloud")}",
CloudProviders.Nextcloud => !string.IsNullOrEmpty(nextCloudValue) ? nextCloudValue : "Nextcloud",
CloudProviders.Jottacloud => $"Jottacloud",
_ => null
},
SyncFolder = syncedFolder
SyncFolder = syncedFolder,
});
}
}
return Task.FromResult(results);

return Task.FromResult<IEnumerable<ICloudProvider>>(results);
}

private static Task<List<CloudProvider>> DetectOneDrive()
private static Task<IEnumerable<ICloudProvider>> DetectOneDrive()
{
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");

if (oneDriveAccountsKey == null)
if (oneDriveAccountsKey is null)
{
return Task.FromResult<List<CloudProvider>>(null);
return Task.FromResult<IEnumerable<ICloudProvider>>(null);
}

var oneDriveAccounts = new List<CloudProvider>();
var oneDriveAccounts = new List<ICloudProvider>();
foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
{
var accountKeyName = @$"{oneDriveAccountsKey.Name}\{account}";
var displayName = (string)Registry.GetValue(accountKeyName, "DisplayName", null);
var userFolder = (string)Registry.GetValue(accountKeyName, "UserFolder", null);
var accountName = string.IsNullOrWhiteSpace(displayName) ? "OneDrive" : $"OneDrive - {displayName}";

if (!string.IsNullOrWhiteSpace(userFolder) && !oneDriveAccounts.Any(x => x.Name == accountName))
{
oneDriveAccounts.Add(new CloudProvider()
oneDriveAccounts.Add(new CloudProvider(CloudProviders.OneDrive)
{
ID = CloudProviders.OneDrive,
Name = accountName,
SyncFolder = userFolder
SyncFolder = userFolder,
});
}
}
return Task.FromResult(oneDriveAccounts);

return Task.FromResult<IEnumerable<ICloudProvider>>(oneDriveAccounts);
}

private static Task<List<CloudProvider>> DetectSharepoint()
private static Task<IEnumerable<ICloudProvider>> DetectSharepoint()
{
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");

if (oneDriveAccountsKey == null)
if (oneDriveAccountsKey is null)
{
return Task.FromResult<List<CloudProvider>>(null);
return Task.FromResult<IEnumerable<ICloudProvider>>(null);
}

var sharepointAccounts = new List<CloudProvider>();

var sharepointAccounts = new List<ICloudProvider>();
foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
{
var accountKeyName = @$"{oneDriveAccountsKey.Name}\{account}";
Expand All @@ -159,7 +165,7 @@ private static Task<List<CloudProvider>> DetectSharepoint()
var mountPointKeyName = @$"SOFTWARE\Microsoft\OneDrive\Accounts\{account}\ScopeIdToMountPointPathCache";
using (var mountPointsKey = Registry.CurrentUser.OpenSubKey(mountPointKeyName))
{
if (mountPointsKey == null)
if (mountPointsKey is null)
{
continue;
}
Expand All @@ -179,19 +185,19 @@ private static Task<List<CloudProvider>> DetectSharepoint()
foreach (var sharePointSyncFolder in sharePointSyncFolders)
{
var parentFolder = Directory.GetParent(sharePointSyncFolder)?.FullName ?? string.Empty;
if (!sharepointAccounts.Any(acc => string.Equals(acc.Name, accountName, StringComparison.OrdinalIgnoreCase)) && !string.IsNullOrWhiteSpace(parentFolder))
if (!sharepointAccounts.Any(acc =>
string.Equals(acc.Name, accountName, StringComparison.OrdinalIgnoreCase)) && !string.IsNullOrWhiteSpace(parentFolder))
{
sharepointAccounts.Add(new CloudProvider()
sharepointAccounts.Add(new CloudProvider(CloudProviders.OneDriveCommercial)
{
ID = CloudProviders.OneDriveCommercial,
Name = accountName,
SyncFolder = parentFolder
SyncFolder = parentFolder,
});
}
}
}

return Task.FromResult(sharepointAccounts);
return Task.FromResult<IEnumerable<ICloudProvider>>(sharepointAccounts);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Files.Shared.Enums
namespace Files.Shared.Cloud
{
public enum CloudDriveSyncStatus
{
Expand Down
17 changes: 17 additions & 0 deletions src/Files.Shared/Cloud/CloudProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Files.Shared.Cloud
{
public class CloudProvider : ICloudProvider
{
public CloudProviders ID { get; }

public string Name { get; init; } = string.Empty;
public string SyncFolder { get; init; } = string.Empty;
public byte[]? IconData { get; init; }

public CloudProvider(CloudProviders id) => ID = id;

public override int GetHashCode() => (ID, SyncFolder).GetHashCode();
public override bool Equals(object o) => o is ICloudProvider other && Equals(other);
public bool Equals(ICloudProvider other) => other is not null && other.ID == ID && other.SyncFolder == SyncFolder;
}
}
18 changes: 18 additions & 0 deletions src/Files.Shared/Cloud/CloudProviders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Files.Shared.Cloud
{
public enum CloudProviders
{
OneDrive,
OneDriveCommercial,
Mega,
GoogleDrive,
DropBox,
AppleCloud,
AmazonDrive,
Nextcloud,
Yandex,
Box,
Jottacloud,
SynologyDrive,
}
}
10 changes: 10 additions & 0 deletions src/Files.Shared/Cloud/ICloudDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Files.Shared.Cloud
{
public interface ICloudDetector
{
Task<IEnumerable<ICloudProvider>> DetectCloudProvidersAsync();
}
}
13 changes: 13 additions & 0 deletions src/Files.Shared/Cloud/ICloudProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Files.Shared.Cloud
{
public interface ICloudProvider : IEquatable<ICloudProvider>
{
public CloudProviders ID { get; }

public string Name { get; }
public string SyncFolder { get; }
public byte[]? IconData { get; }
}
}
50 changes: 0 additions & 50 deletions src/Files.Shared/CloudProvider.cs

This file was deleted.

7 changes: 7 additions & 0 deletions src/Files.Shared/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.ComponentModel;

namespace System.Runtime.CompilerServices
{
[EditorBrowsable(EditorBrowsableState.Never)]
internal sealed class IsExternalInit {}
}
3 changes: 3 additions & 0 deletions src/Files.Uwp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using Files.Backend.Services.Settings;
using Files.Backend.Services.SizeProvider;
using Files.Shared;
using Files.Shared.Cloud;
using Files.Shared.Extensions;
using Files.Shared.Services.DateTimeFormatter;
using Files.Uwp.CommandLine;
using Files.Uwp.Controllers;
using Files.Uwp.Filesystem;
using Files.Uwp.Filesystem.Cloud;
using Files.Uwp.Filesystem.FilesystemHistory;
using Files.Uwp.Helpers;
using Files.Uwp.ServicesImplementation;
Expand Down Expand Up @@ -124,6 +126,7 @@ private IServiceProvider ConfigureServices()
.AddSingleton<IImagingService, ImagingService>()
.AddSingleton<IThreadingService, ThreadingService>()
.AddSingleton<ILocalizationService, LocalizationService>()
.AddSingleton<ICloudDetector, CloudDetector>()
#if SIDELOAD
.AddSingleton<IUpdateService, SideloadUpdateService>()
#else
Expand Down
Loading