Skip to content

Commit 98da915

Browse files
authored
Refactor Cloud (#9427)
1 parent 72620da commit 98da915

30 files changed

+533
-619
lines changed
Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Files.Shared;
1+
using Files.Shared.Cloud;
22
using Files.Shared.Extensions;
33
using Microsoft.Win32;
44
using System;
@@ -13,9 +13,9 @@ namespace Files.FullTrust.Helpers
1313
[SupportedOSPlatform("Windows10.0.10240")]
1414
public class CloudDrivesDetector
1515
{
16-
public static async Task<List<CloudProvider>> DetectCloudDrives()
16+
public static async Task<IEnumerable<ICloudProvider>> DetectCloudDrives()
1717
{
18-
var tasks = new Task<List<CloudProvider>>[]
18+
var tasks = new Task<IEnumerable<ICloudProvider>>[]
1919
{
2020
SafetyExtensions.IgnoreExceptions(DetectOneDrive, Program.Logger),
2121
SafetyExtensions.IgnoreExceptions(DetectSharepoint, Program.Logger),
@@ -25,39 +25,46 @@ public static async Task<List<CloudProvider>> DetectCloudDrives()
2525

2626
await Task.WhenAll(tasks);
2727

28-
return tasks.Where(o => o.Result != null).SelectMany(o => o.Result).OrderBy(o => o.ID.ToString()).ThenBy(o => o.Name).Distinct().ToList();
28+
return tasks
29+
.Where(o => o.Result is not null)
30+
.SelectMany(o => o.Result)
31+
.OrderBy(o => o.ID.ToString())
32+
.ThenBy(o => o.Name)
33+
.Distinct();
2934
}
3035

31-
private static Task<List<CloudProvider>> DetectYandexDisk()
36+
private static Task<IEnumerable<ICloudProvider>> DetectYandexDisk()
3237
{
33-
var results = new List<CloudProvider>();
38+
var results = new List<ICloudProvider>();
3439
using var yandexKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Yandex\Yandex.Disk.2");
40+
3541
var syncedFolder = (string)yandexKey?.GetValue("RootFolder");
36-
if (syncedFolder != null)
42+
if (syncedFolder is not null)
3743
{
38-
results.Add(new CloudProvider()
44+
results.Add(new CloudProvider(CloudProviders.Yandex)
3945
{
40-
ID = CloudProviders.Yandex,
4146
Name = $"Yandex Disk",
42-
SyncFolder = syncedFolder
47+
SyncFolder = syncedFolder,
4348
});
4449
}
45-
return Task.FromResult(results);
50+
51+
return Task.FromResult<IEnumerable<ICloudProvider>>(results);
4652
}
4753

48-
private static Task<List<CloudProvider>> DetectGenericCloudDrive()
54+
private static Task<IEnumerable<ICloudProvider>> DetectGenericCloudDrive()
4955
{
50-
var results = new List<CloudProvider>();
56+
var results = new List<ICloudProvider>();
5157
using var clsidKey = Registry.ClassesRoot.OpenSubKey(@"CLSID");
5258
using var namespaceKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace");
59+
5360
foreach (var subKeyName in namespaceKey.GetSubKeyNames())
5461
{
5562
using var clsidSubKey = SafetyExtensions.IgnoreExceptions(() => clsidKey.OpenSubKey(subKeyName));
56-
if (clsidSubKey != null && (int?)clsidSubKey.GetValue("System.IsPinnedToNameSpaceTree") == 1)
63+
if (clsidSubKey is not null && (int?)clsidSubKey.GetValue("System.IsPinnedToNameSpaceTree") is 1)
5764
{
5865
using var namespaceSubKey = namespaceKey.OpenSubKey(subKeyName);
59-
var driveType = (string)namespaceSubKey?.GetValue("");
60-
if (driveType == null)
66+
var driveType = (string)namespaceSubKey?.GetValue(string.Empty);
67+
if (driveType is null)
6168
{
6269
continue;
6370
}
@@ -71,7 +78,7 @@ private static Task<List<CloudProvider>> DetectGenericCloudDrive()
7178

7279
using var bagKey = clsidSubKey.OpenSubKey(@"Instance\InitPropertyBag");
7380
var syncedFolder = (string)bagKey?.GetValue("TargetFolderPath");
74-
if (syncedFolder == null)
81+
if (syncedFolder is null)
7582
{
7683
continue;
7784
}
@@ -83,71 +90,70 @@ private static Task<List<CloudProvider>> DetectGenericCloudDrive()
8390
"Amazon Drive" => CloudProviders.AmazonDrive,
8491
"Nextcloud" => CloudProviders.Nextcloud,
8592
"Jottacloud" => CloudProviders.Jottacloud,
86-
_ => null
93+
_ => null,
8794
};
88-
if (driveID == null)
95+
if (driveID is null)
8996
{
9097
continue;
9198
}
9299

93-
results.Add(new CloudProvider()
100+
string nextCloudValue = (string)namespaceSubKey?.GetValue(string.Empty);
101+
results.Add(new CloudProvider(driveID.Value)
94102
{
95-
ID = driveID.Value,
96103
Name = driveID switch
97104
{
98105
CloudProviders.Mega => $"MEGA ({Path.GetFileName(syncedFolder.TrimEnd('\\'))})",
99106
CloudProviders.AmazonDrive => $"Amazon Drive",
100-
CloudProviders.Nextcloud => $"{ (!string.IsNullOrEmpty((string)namespaceSubKey?.GetValue("")) ? (string)namespaceSubKey?.GetValue(""):"Nextcloud")}",
107+
CloudProviders.Nextcloud => !string.IsNullOrEmpty(nextCloudValue) ? nextCloudValue : "Nextcloud",
101108
CloudProviders.Jottacloud => $"Jottacloud",
102109
_ => null
103110
},
104-
SyncFolder = syncedFolder
111+
SyncFolder = syncedFolder,
105112
});
106113
}
107114
}
108-
return Task.FromResult(results);
115+
116+
return Task.FromResult<IEnumerable<ICloudProvider>>(results);
109117
}
110118

111-
private static Task<List<CloudProvider>> DetectOneDrive()
119+
private static Task<IEnumerable<ICloudProvider>> DetectOneDrive()
112120
{
113121
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
114-
115-
if (oneDriveAccountsKey == null)
122+
if (oneDriveAccountsKey is null)
116123
{
117-
return Task.FromResult<List<CloudProvider>>(null);
124+
return Task.FromResult<IEnumerable<ICloudProvider>>(null);
118125
}
119126

120-
var oneDriveAccounts = new List<CloudProvider>();
127+
var oneDriveAccounts = new List<ICloudProvider>();
121128
foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
122129
{
123130
var accountKeyName = @$"{oneDriveAccountsKey.Name}\{account}";
124131
var displayName = (string)Registry.GetValue(accountKeyName, "DisplayName", null);
125132
var userFolder = (string)Registry.GetValue(accountKeyName, "UserFolder", null);
126133
var accountName = string.IsNullOrWhiteSpace(displayName) ? "OneDrive" : $"OneDrive - {displayName}";
134+
127135
if (!string.IsNullOrWhiteSpace(userFolder) && !oneDriveAccounts.Any(x => x.Name == accountName))
128136
{
129-
oneDriveAccounts.Add(new CloudProvider()
137+
oneDriveAccounts.Add(new CloudProvider(CloudProviders.OneDrive)
130138
{
131-
ID = CloudProviders.OneDrive,
132139
Name = accountName,
133-
SyncFolder = userFolder
140+
SyncFolder = userFolder,
134141
});
135142
}
136143
}
137-
return Task.FromResult(oneDriveAccounts);
144+
145+
return Task.FromResult<IEnumerable<ICloudProvider>>(oneDriveAccounts);
138146
}
139147

140-
private static Task<List<CloudProvider>> DetectSharepoint()
148+
private static Task<IEnumerable<ICloudProvider>> DetectSharepoint()
141149
{
142150
using var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
143-
144-
if (oneDriveAccountsKey == null)
151+
if (oneDriveAccountsKey is null)
145152
{
146-
return Task.FromResult<List<CloudProvider>>(null);
153+
return Task.FromResult<IEnumerable<ICloudProvider>>(null);
147154
}
148155

149-
var sharepointAccounts = new List<CloudProvider>();
150-
156+
var sharepointAccounts = new List<ICloudProvider>();
151157
foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
152158
{
153159
var accountKeyName = @$"{oneDriveAccountsKey.Name}\{account}";
@@ -159,7 +165,7 @@ private static Task<List<CloudProvider>> DetectSharepoint()
159165
var mountPointKeyName = @$"SOFTWARE\Microsoft\OneDrive\Accounts\{account}\ScopeIdToMountPointPathCache";
160166
using (var mountPointsKey = Registry.CurrentUser.OpenSubKey(mountPointKeyName))
161167
{
162-
if (mountPointsKey == null)
168+
if (mountPointsKey is null)
163169
{
164170
continue;
165171
}
@@ -179,19 +185,19 @@ private static Task<List<CloudProvider>> DetectSharepoint()
179185
foreach (var sharePointSyncFolder in sharePointSyncFolders)
180186
{
181187
var parentFolder = Directory.GetParent(sharePointSyncFolder)?.FullName ?? string.Empty;
182-
if (!sharepointAccounts.Any(acc => string.Equals(acc.Name, accountName, StringComparison.OrdinalIgnoreCase)) && !string.IsNullOrWhiteSpace(parentFolder))
188+
if (!sharepointAccounts.Any(acc =>
189+
string.Equals(acc.Name, accountName, StringComparison.OrdinalIgnoreCase)) && !string.IsNullOrWhiteSpace(parentFolder))
183190
{
184-
sharepointAccounts.Add(new CloudProvider()
191+
sharepointAccounts.Add(new CloudProvider(CloudProviders.OneDriveCommercial)
185192
{
186-
ID = CloudProviders.OneDriveCommercial,
187193
Name = accountName,
188-
SyncFolder = parentFolder
194+
SyncFolder = parentFolder,
189195
});
190196
}
191197
}
192198
}
193199

194-
return Task.FromResult(sharepointAccounts);
200+
return Task.FromResult<IEnumerable<ICloudProvider>>(sharepointAccounts);
195201
}
196202
}
197203
}

src/Files.Shared/Enums/CloudDriveSyncStatus.cs renamed to src/Files.Shared/Cloud/CloudDriveSyncStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Files.Shared.Enums
1+
namespace Files.Shared.Cloud
22
{
33
public enum CloudDriveSyncStatus
44
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Files.Shared.Cloud
2+
{
3+
public class CloudProvider : ICloudProvider
4+
{
5+
public CloudProviders ID { get; }
6+
7+
public string Name { get; init; } = string.Empty;
8+
public string SyncFolder { get; init; } = string.Empty;
9+
public byte[]? IconData { get; init; }
10+
11+
public CloudProvider(CloudProviders id) => ID = id;
12+
13+
public override int GetHashCode() => (ID, SyncFolder).GetHashCode();
14+
public override bool Equals(object o) => o is ICloudProvider other && Equals(other);
15+
public bool Equals(ICloudProvider other) => other is not null && other.ID == ID && other.SyncFolder == SyncFolder;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Files.Shared.Cloud
2+
{
3+
public enum CloudProviders
4+
{
5+
OneDrive,
6+
OneDriveCommercial,
7+
Mega,
8+
GoogleDrive,
9+
DropBox,
10+
AppleCloud,
11+
AmazonDrive,
12+
Nextcloud,
13+
Yandex,
14+
Box,
15+
Jottacloud,
16+
SynologyDrive,
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Files.Shared.Cloud
5+
{
6+
public interface ICloudDetector
7+
{
8+
Task<IEnumerable<ICloudProvider>> DetectCloudProvidersAsync();
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Files.Shared.Cloud
4+
{
5+
public interface ICloudProvider : IEquatable<ICloudProvider>
6+
{
7+
public CloudProviders ID { get; }
8+
9+
public string Name { get; }
10+
public string SyncFolder { get; }
11+
public byte[]? IconData { get; }
12+
}
13+
}

src/Files.Shared/CloudProvider.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Files.Shared/IsExternalInit.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.ComponentModel;
2+
3+
namespace System.Runtime.CompilerServices
4+
{
5+
[EditorBrowsable(EditorBrowsableState.Never)]
6+
internal sealed class IsExternalInit {}
7+
}

src/Files.Uwp/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
using Files.Backend.Services.Settings;
44
using Files.Backend.Services.SizeProvider;
55
using Files.Shared;
6+
using Files.Shared.Cloud;
67
using Files.Shared.Extensions;
78
using Files.Shared.Services.DateTimeFormatter;
89
using Files.Uwp.CommandLine;
910
using Files.Uwp.Controllers;
1011
using Files.Uwp.Filesystem;
12+
using Files.Uwp.Filesystem.Cloud;
1113
using Files.Uwp.Filesystem.FilesystemHistory;
1214
using Files.Uwp.Helpers;
1315
using Files.Uwp.ServicesImplementation;
@@ -124,6 +126,7 @@ private IServiceProvider ConfigureServices()
124126
.AddSingleton<IImagingService, ImagingService>()
125127
.AddSingleton<IThreadingService, ThreadingService>()
126128
.AddSingleton<ILocalizationService, LocalizationService>()
129+
.AddSingleton<ICloudDetector, CloudDetector>()
127130
#if SIDELOAD
128131
.AddSingleton<IUpdateService, SideloadUpdateService>()
129132
#else

0 commit comments

Comments
 (0)