/* Copyright (C) 2007 Marcus Mueller and Helge Hess This file is part of Resiprocate. SIPDialTool is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. SIPDialTool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "AppDelegate.h" #import @implementation AppDelegate - (void)_registerDefaults { NSDictionary *bundleInfo; NSString *bundleInfoPath; bundleInfoPath = [[NSBundle mainBundle] pathForResource:[[NSBundle mainBundle] bundleIdentifier] ofType:@"plist"]; bundleInfo = [NSDictionary dictionaryWithContentsOfFile:bundleInfoPath]; [[NSUserDefaults standardUserDefaults] registerDefaults:bundleInfo]; } - (void)_registerForAppleEvents { [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; } - (void)applicationWillFinishLaunching:(NSNotification *) notification { [self _registerDefaults]; [self _registerForAppleEvents]; /* quit the application now and then */ [NSTimer scheduledTimerWithTimeInterval:(60.0 * 5.0) // every five minutes target:self selector:@selector(quit:) userInfo:nil repeats:NO]; } - (void)quit:(NSTimer *)_timer { [[NSApplication sharedApplication] terminate:nil]; } - (NSString *)toolPath { NSString *toolPath; toolPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"sipdialer" ofType:nil]; if ([toolPath length] > 3) return toolPath; toolPath = @"/usr/local/bin/sipdialer"; if ([[NSFileManager defaultManager] isExecutableFileAtPath:toolPath]) return toolPath; NSLog(@"ERROR: did not find sipdialer tool inside the bundle nor " @"in /usr/local/bin!"); return nil; } /* AppleEvents glue code */ - (void)handleURLEvent:(NSAppleEventDescriptor *) event withReplyEvent:(NSAppleEventDescriptor *) replyEvent { NSURL *url = [NSURL URLWithString:[[event descriptorAtIndex:1] stringValue]]; [self performActionForURL:url]; } /* the actual method to invoke the sipdialer */ - (void)performActionForURL:(NSURL *)_url { NSTask *task; NSArray *args; NSString *toolPath; if ((toolPath = [self toolPath]) == nil) return; args = [NSArray arrayWithObject:[_url absoluteString]]; NSLog(@"invoking sipdialer with arguments: %@", args); task = [[NSTask alloc] init]; [task setLaunchPath:toolPath]; [task setArguments:args]; [task launch]; /* Somehow the sipdialer does not properly shut down, so we just wait a bit and then kill it. Possibly we need to reset stdio to /dev/null or something? (setting stdio to nil raises an exception) (and no, waitFor... doesn't solve it ;-) */ sleep(2); if ([task isRunning]) { NSLog(@"sipdialer still running after 2s, sending terminate ..."); [task terminate]; } [task release]; task = nil; /* Enable this to quit the application after each request during development. Otherwise it continues running in the background. */ //exit(0); } @end /* AppDelegate */