diff --git a/src/Waehlhilfe.App/CompositionRoot.cs b/src/Waehlhilfe.App/CompositionRoot.cs new file mode 100644 index 0000000..437039e --- /dev/null +++ b/src/Waehlhilfe.App/CompositionRoot.cs @@ -0,0 +1,34 @@ +using Waehlhilfe.App.Platform; +using Waehlhilfe.AppLogic; +using Waehlhilfe.AppLogic.ViewModels; +using Waehlhilfe.Modem; + +namespace Waehlhilfe.App; + +/// Wires Wählhilfe by hand: no container. +internal sealed class CompositionRoot +{ + private readonly JsonSettingsStore _store = new JsonSettingsStore(JsonSettingsStore.DefaultPath()); + private readonly AtDialer _dialer = new AtDialer(new SerialPorts()); + private readonly ITelHandlerRegistration _registration = CreateRegistration(); + + public SettingsViewModel Settings() => new(_store, _dialer, SerialPorts.List, _registration); + + // Settings are read for every dial, so a change in the settings window applies to the next click. + public DialViewModel Dial(string link) => new(link, _store.Load(), _dialer); + + private static ITelHandlerRegistration CreateRegistration() + { + if (OperatingSystem.IsWindows()) + { + return new WindowsTelHandlerRegistration(); + } + + if (OperatingSystem.IsMacOS()) + { + return new MacTelHandlerRegistration(); + } + + return new UnsupportedTelHandlerRegistration(); + } +} diff --git a/src/Waehlhilfe.App/LaunchArguments.cs b/src/Waehlhilfe.App/LaunchArguments.cs new file mode 100644 index 0000000..8746874 --- /dev/null +++ b/src/Waehlhilfe.App/LaunchArguments.cs @@ -0,0 +1,12 @@ +namespace Waehlhilfe.App; + +internal static class LaunchArguments +{ + /// + /// The tel: link Windows and Linux pass on the command line when a link is clicked, + /// or null when Wählhilfe was started on its own. macOS delivers links as an Apple + /// event instead, which arrives through IActivatableLifetime. + /// + public static string? TelLink(IEnumerable args) => + args.FirstOrDefault(a => a.StartsWith("tel:", StringComparison.OrdinalIgnoreCase)); +} diff --git a/src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs b/src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs new file mode 100644 index 0000000..aee22cd --- /dev/null +++ b/src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs @@ -0,0 +1,94 @@ +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Text; +using Waehlhilfe.AppLogic; + +namespace Waehlhilfe.App.Platform; + +/// The bundle declares tel in CFBundleURLTypes; Launch Services picks the default. +[SupportedOSPlatform("macos")] +internal sealed partial class MacTelHandlerRegistration : ITelHandlerRegistration +{ + /// Must match CFBundleIdentifier in packaging/macos/build-dmg.sh. + internal const string BundleId = "io.github.deepdiver1975.waehlhilfe"; + + private const string CoreFoundation = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"; + private const string CoreServices = "/System/Library/Frameworks/CoreServices.framework/CoreServices"; + private const uint Utf8 = 0x08000100; + + public bool IsDefaultHandler() + { + var scheme = CFStringCreateWithCString(IntPtr.Zero, "tel", Utf8); + try + { + var handler = LSCopyDefaultHandlerForURLScheme(scheme); + try + { + return string.Equals(ToManaged(handler), BundleId, StringComparison.OrdinalIgnoreCase); + } + finally + { + Release(handler); + } + } + finally + { + Release(scheme); + } + } + + public Task MakeDefaultAsync(CancellationToken cancellationToken = default) + { + var scheme = CFStringCreateWithCString(IntPtr.Zero, "tel", Utf8); + var bundle = CFStringCreateWithCString(IntPtr.Zero, BundleId, Utf8); + try + { + var status = LSSetDefaultHandlerForURLScheme(scheme, bundle); + return status == 0 + ? Task.CompletedTask + : throw new InvalidOperationException($"LSSetDefaultHandlerForURLScheme returned {status}."); + } + finally + { + Release(bundle); + Release(scheme); + } + } + + private static string? ToManaged(IntPtr text) + { + if (text == IntPtr.Zero) + { + return null; + } + + var buffer = new byte[256]; + return CFStringGetCString(text, buffer, buffer.Length, Utf8) + ? Encoding.UTF8.GetString(buffer, 0, Array.IndexOf(buffer, (byte)0) is var end and >= 0 ? end : buffer.Length) + : null; + } + + private static void Release(IntPtr reference) + { + if (reference != IntPtr.Zero) + { + CFRelease(reference); + } + } + + [LibraryImport(CoreFoundation, StringMarshalling = StringMarshalling.Utf8)] + private static partial IntPtr CFStringCreateWithCString(IntPtr allocator, string text, uint encoding); + + [LibraryImport(CoreFoundation)] + [return: MarshalAs(UnmanagedType.U1)] + private static partial bool CFStringGetCString(IntPtr text, [Out] byte[] buffer, nint bufferSize, uint encoding); + + [LibraryImport(CoreFoundation)] + private static partial void CFRelease(IntPtr reference); + + [LibraryImport(CoreServices)] + private static partial IntPtr LSCopyDefaultHandlerForURLScheme(IntPtr scheme); + + [LibraryImport(CoreServices)] + private static partial int LSSetDefaultHandlerForURLScheme(IntPtr scheme, IntPtr bundleId); +} diff --git a/src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs b/src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs new file mode 100644 index 0000000..6e08ddd --- /dev/null +++ b/src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs @@ -0,0 +1,12 @@ +using Waehlhilfe.AppLogic; + +namespace Waehlhilfe.App.Platform; + +/// Linux: Wählhilfe dials there (it is where the DL500A is tested), but ships no handler registration. +internal sealed class UnsupportedTelHandlerRegistration : ITelHandlerRegistration +{ + public bool IsDefaultHandler() => false; + + public Task MakeDefaultAsync(CancellationToken cancellationToken = default) => + throw new PlatformNotSupportedException("Wählhilfe registers as the tel: handler on Windows and macOS only."); +} diff --git a/src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs b/src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs new file mode 100644 index 0000000..d1e837a --- /dev/null +++ b/src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs @@ -0,0 +1,38 @@ +using System.Diagnostics; +using System.Runtime.Versioning; +using Microsoft.Win32; +using Waehlhilfe.AppLogic; + +namespace Waehlhilfe.App.Platform; + +/// +/// The MSI registers Wählhilfe as *capable* of handling tel: (ProgId, Capabilities, +/// RegisteredApplications). Windows lets no program make itself the default, so this opens the +/// Settings page where the user picks it once. +/// +[SupportedOSPlatform("windows")] +internal sealed class WindowsTelHandlerRegistration : ITelHandlerRegistration +{ + /// Must match the ProgId and the RegisteredApplications name in packaging/windows/Waehlhilfe.wxs. + internal const string ProgId = "Waehlhilfe.tel"; + + internal const string RegisteredApplicationName = "Waehlhilfe"; + + private const string UserChoiceKey = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\tel\UserChoice"; + + public bool IsDefaultHandler() + { + using var key = Registry.CurrentUser.OpenSubKey(UserChoiceKey); + return string.Equals(key?.GetValue("ProgId") as string, ProgId, StringComparison.Ordinal); + } + + public Task MakeDefaultAsync(CancellationToken cancellationToken = default) + { + // A fixed URI, nothing from input: the Settings page that lists Wählhilfe's link types. + using var _ = Process.Start(new ProcessStartInfo($"ms-settings:defaultapps?registeredAppMachine={RegisteredApplicationName}") + { + UseShellExecute = true, + }); + return Task.CompletedTask; + } +} diff --git a/src/Waehlhilfe.App/Views/DialWindow.axaml b/src/Waehlhilfe.App/Views/DialWindow.axaml new file mode 100644 index 0000000..60f4847 --- /dev/null +++ b/src/Waehlhilfe.App/Views/DialWindow.axaml @@ -0,0 +1,32 @@ + + + + + + + + + + + +