|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Files.App.Utils.Cloud; |
| 5 | +using Microsoft.Win32; |
| 6 | +using System.IO; |
| 7 | +using System.Text.Json; |
| 8 | +using Windows.Storage; |
| 9 | +using static Vanara.PInvoke.Gdi32; |
| 10 | + |
| 11 | +namespace Files.App.Utils.Cloud |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides an utility for OX Drive Cloud detection. |
| 15 | + /// </summary> |
| 16 | + public sealed class OXDriveCloudDetector : AbstractCloudDetector |
| 17 | + { |
| 18 | + protected override async IAsyncEnumerable<ICloudProvider> GetProviders() |
| 19 | + { |
| 20 | + var syncFolder = await GetOXDriveSyncFolder(); |
| 21 | + if (!string.IsNullOrEmpty(syncFolder)) |
| 22 | + { |
| 23 | + var iconFile = GetOXDriveIconFile(); |
| 24 | + yield return new CloudProvider(CloudProviders.OXDrive) |
| 25 | + { |
| 26 | + Name = "OX Drive", |
| 27 | + SyncFolder = syncFolder, |
| 28 | + IconData = iconFile?.IconData |
| 29 | + }; |
| 30 | + } |
| 31 | + } |
| 32 | + public static async Task<string?> GetOXDriveSyncFolder() |
| 33 | + { |
| 34 | + var jsonPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Open-Xchange", "OXDrive", "userConfig.json"); |
| 35 | + if (!File.Exists(jsonPath)) |
| 36 | + return null; |
| 37 | + |
| 38 | + var configFile = await StorageFile.GetFileFromPathAsync(jsonPath); |
| 39 | + using var jsonDoc = JsonDocument.Parse(await FileIO.ReadTextAsync(configFile)); |
| 40 | + var jsonElem = jsonDoc.RootElement; |
| 41 | + |
| 42 | + string? syncFolderPath = null; |
| 43 | + |
| 44 | + if (jsonElem.TryGetProperty("Accounts", out var accounts) && accounts.GetArrayLength() > 0) |
| 45 | + { |
| 46 | + var account = accounts[0]; |
| 47 | + |
| 48 | + if (account.TryGetProperty("MainFolderPath", out var folderPathElem)) |
| 49 | + syncFolderPath = folderPathElem.GetString(); |
| 50 | + } |
| 51 | + |
| 52 | + return syncFolderPath; |
| 53 | + } |
| 54 | + |
| 55 | + private static IconFileInfo? GetOXDriveIconFile() |
| 56 | + { |
| 57 | + var installPath = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Open-Xchange\OXDrive", "InstallDir", null) as string; |
| 58 | + |
| 59 | + // Fallback to default known path if not found in the registry. |
| 60 | + if (string.IsNullOrEmpty(installPath)) |
| 61 | + { |
| 62 | + var pfX86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)"); |
| 63 | + if (string.IsNullOrEmpty(pfX86)) |
| 64 | + return null; |
| 65 | + |
| 66 | + installPath = Path.Combine(pfX86, "Open-Xchange", "OXDrive"); |
| 67 | + } |
| 68 | + |
| 69 | + var oxDriveFilePath = Path.Combine(installPath, "OXDrive.exe"); |
| 70 | + if (!File.Exists(oxDriveFilePath)) |
| 71 | + { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + // Extract the icon from the OXDrive executable (though it is executable, it contains icons) |
| 76 | + var icons = Win32Helper.ExtractSelectedIconsFromDLL(oxDriveFilePath, new List<int> { 0 }, 32); |
| 77 | + return icons.FirstOrDefault(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments