url scheme: register ring in URL schemes

This commits register the ring:<hash> and ring://<hash> pattern system wide
to automatically launch a call to the clicked ringID.

Issue: #78605
Change-Id: I6a0837c14dc329f0ca299a2238a149c2a479ef8f
diff --git a/src/AppDelegate.mm b/src/AppDelegate.mm
index 08ed94e..a4bdc16 100644
--- a/src/AppDelegate.mm
+++ b/src/AppDelegate.mm
@@ -136,6 +136,47 @@
     return foundRingAcc;
 }
 
+-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
+{
+    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
+    [appleEventManager setEventHandler:self
+                           andSelector:@selector(handleGetURLEvent:withReplyEvent:)
+                         forEventClass:kInternetEventClass andEventID:kAEGetURL];
+}
+
+/**
+ * Recognized patterns:
+ *   - ring:<hash>
+ *   - ring://<hash>
+ */
+- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
+{
+    NSString* query = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
+    NSURL* url = [[NSURL alloc] initWithString:query];
+    NSString* ringID = [url host];
+    if (!ringID) {
+        //not a valid NSURL, try to parse query directly
+        ringID = [query substringFromIndex:@"ring:".length];
+    }
+
+    // check for a valid ring hash
+    NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
+    BOOL valid = [[ringID stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
+
+    if(valid && ringID.length == 40) {
+        Call* c = CallModel::instance()->dialingCall();
+        c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",ringID]));
+        c << Call::Action::ACCEPT;
+    } else {
+        NSAlert *alert = [[NSAlert alloc] init];
+        [alert addButtonWithTitle:@"OK"];
+        [alert setMessageText:@"Error"];
+        [alert setInformativeText:@"ringID cannot be read from this URL."];
+        [alert setAlertStyle:NSWarningAlertStyle];
+        [alert runModal];
+    }
+}
+
 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
 {
     if([self checkForRingAccount]) {