ATVDeviceController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #import "ATVDeviceController.h"
  2. #import <sys/types.h>
  3. #import <sys/socket.h>
  4. #import <netinet/in.h>
  5. #import <arpa/inet.h>
  6. #import "AppDelegate.h"
  7. #define APP_DELEGATE (AppDelegate *)[[NSApplication sharedApplication] delegate]
  8. @implementation ATVDeviceController
  9. @synthesize deviceController, theComboBox, delegate;
  10. - (NSString *)input: (NSString *)prompt defaultValue: (NSString *)defaultValue {
  11. NSAlert *alert = [NSAlert alertWithMessageText: prompt
  12. defaultButton:@"OK"
  13. alternateButton:@"Cancel"
  14. otherButton:nil
  15. informativeTextWithFormat:@""];
  16. NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
  17. [input setStringValue:defaultValue];
  18. [alert setAccessoryView:input];
  19. NSInteger button = [alert runModal];
  20. if (button == NSAlertDefaultReturn) {
  21. [input validateEditing];
  22. NSString *inputString = [input stringValue];
  23. return inputString;
  24. } else if (button == NSAlertAlternateReturn) {
  25. return nil;
  26. } else {
  27. NSAssert1(NO, @"Invalid input dialog button %d", button);
  28. //[input autorelease];
  29. return nil;
  30. }
  31. }
  32. - (IBAction)menuItemSelected:(id)sender
  33. {
  34. NSLog(@"sender: %@", sender);
  35. if (sender == nil)
  36. {
  37. return;
  38. }
  39. [APP_DELEGATE setAtvAvailable:NO];
  40. [[APP_DELEGATE progressInd] startAnimation:nil];
  41. NSInteger index = [sender indexOfSelectedItem];
  42. NSInteger itemCount = [sender numberOfItems];
  43. if (index == 0)
  44. {
  45. [APP_DELEGATE setAtvAvailable:FALSE];
  46. [APP_DELEGATE resetServerSettings];
  47. return;
  48. }
  49. if (index == itemCount-1)
  50. {
  51. //[sender setEditable:TRUE];
  52. NSString *output = [self input:@"Enter an Apple TV name or IP address" defaultValue:@""];
  53. [APP_DELEGATE setAtvAvailable:TRUE];
  54. [DEFAULTS setObject:output forKey:@"appleTVHost"];
  55. return;
  56. } else {
  57. // [sender setEditable:FALSE];
  58. }
  59. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND
  60. , 0), ^{
  61. NSNetService * clickedService = [services objectAtIndex:index];
  62. NSDictionary *finalDict = [self stringDictionaryFromService:clickedService];
  63. NSLog(@"finalDict: %@", finalDict);
  64. if ([[finalDict allKeys] count] > 0)
  65. {
  66. NSString *model = [finalDict objectForKey:@"model"];
  67. if ([model isEqualToString:@"AppleTV3,1"])
  68. {
  69. dispatch_async(dispatch_get_main_queue(), ^{
  70. [sender selectItemAtIndex:0];
  71. [APP_DELEGATE setAtvAvailable:FALSE];
  72. [APP_DELEGATE showATVWarning];
  73. [[APP_DELEGATE progressInd] stopAnimation:nil];
  74. });
  75. } else {
  76. NSString *ip;
  77. //int port;
  78. struct sockaddr_in *addr;
  79. addr = (struct sockaddr_in *) [[[clickedService addresses] objectAtIndex:0]
  80. bytes];
  81. ip = [NSString stringWithUTF8String:(char *) inet_ntoa(addr->sin_addr)];
  82. NSString *fullIP = [NSString stringWithFormat:@"%@:%i", ip, 22];
  83. //[APP_DELEGATE setAtvAvailable:TRUE];
  84. [DEFAULTS setObject:fullIP forKey:ATV_HOST];
  85. [APP_DELEGATE setStatusText:@"Checking device for jailbreak, please wait..."];
  86. NSLog(@"ATVADDRESS: %@", APPLE_TV_ADDRESS);
  87. BOOL jailbroken = [APP_DELEGATE isJailbroken];
  88. //hacky check here to see if its jailbroken
  89. dispatch_async(dispatch_get_main_queue(), ^{
  90. NSLog(@"is jailbroken: %lu", jailbroken);
  91. if (jailbroken == FALSE){
  92. [APP_DELEGATE showNotJailbrokenWarning];
  93. [DEFAULTS removeObjectForKey:ATV_HOST];
  94. [DEFAULTS removeObjectForKey:@"selectedValue"];
  95. [APP_DELEGATE setStatusText:@"Device is not jailbroken! :("];
  96. [[APP_DELEGATE progressInd] stopAnimation:nil];
  97. } else {
  98. [APP_DELEGATE setStatusText:@"Device is jailbroken!"];
  99. [APP_DELEGATE setAtvAvailable:YES];
  100. [[APP_DELEGATE progressInd] stopAnimation:nil];
  101. [APP_DELEGATE setDeviceDict:finalDict];
  102. }
  103. });
  104. }
  105. }
  106. });
  107. }
  108. - (NSString *)convertedName:(NSString *)inputName
  109. {
  110. NSMutableString *fixedNetLabel = [NSMutableString stringWithString:[inputName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@".#,<>/?\'\\\[]{}+=-~`\";:"]]];
  111. //NSLog(@"fixedNetLabel: %@", fixedNetLabel);
  112. [fixedNetLabel replaceOccurrencesOfString:@" " withString:@"-" options:0 range:NSMakeRange(0, [fixedNetLabel length])];
  113. //int nameLength = [fixedNetLabel length];
  114. //fixedNetLabel = [fixedNetLabel substringToIndex:(nameLength-1)];
  115. return [NSString stringWithString:fixedNetLabel];
  116. }
  117. - (NSString *)fixedName:(NSString *)inputName
  118. {
  119. NSInteger nameLength = [inputName length];
  120. NSString *newName = [inputName substringToIndex:(nameLength-1)];
  121. return newName;
  122. }
  123. - (NSDictionary *)stringDictionaryFromService:(NSNetService *)theService
  124. {
  125. NSData *txtRecordDict = [theService TXTRecordData];
  126. NSDictionary *theDict = [NSNetService dictionaryFromTXTRecordData:txtRecordDict];
  127. NSMutableDictionary *finalDict = [[NSMutableDictionary alloc] init];
  128. NSArray *keys = [theDict allKeys];
  129. for (NSString *theKey in keys)
  130. {
  131. NSString *currentString = [[NSString alloc] initWithData:[theDict valueForKey:theKey] encoding:NSUTF8StringEncoding];
  132. [finalDict setObject:currentString forKey:theKey];
  133. }
  134. NSString *ip;
  135. int port;
  136. struct sockaddr_in *addr;
  137. if ([[theService addresses] count] == 0)
  138. {
  139. NSLog(@"no addresses resolved!?!?");
  140. return nil;
  141. }
  142. addr = (struct sockaddr_in *) [[[theService addresses] objectAtIndex:0]
  143. bytes];
  144. ip = [NSString stringWithUTF8String:(char *) inet_ntoa(addr->sin_addr)];
  145. port = ntohs(((struct sockaddr_in *)addr)->sin_port);
  146. //NSLog(@"ipaddress: %@", ip);
  147. //NSLog(@"port: %i", port);
  148. NSString *fullIP = [NSString stringWithFormat:@"%@:%i", ip, port];
  149. finalDict[@"fullIP"] = fullIP;
  150. finalDict[@"hostname"] = theService.hostName;
  151. return finalDict;
  152. }
  153. - (id)init {
  154. browser = [[NSNetServiceBrowser alloc] init];
  155. services = [NSMutableArray array];
  156. [browser setDelegate:self];
  157. //NSLog(@"awake from nib");
  158. // Passing in "" for the domain causes us to browse in the default browse domain
  159. [browser searchForServicesOfType:@"_airplay._tcp." inDomain:@""];
  160. // [hostNameField setStringValue:@""];
  161. self = [super init];
  162. NSDictionary *catv = [NSDictionary dictionaryWithObject:@"Choose Apple TV" forKey:@"name"];
  163. NSDictionary *theDict = [NSDictionary dictionaryWithObject:@"Other..." forKey:@"name"];
  164. [services addObject:catv];
  165. [services addObject:theDict];
  166. return self;
  167. }
  168. - (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser
  169. {
  170. //NSLog(@"%@ %s", self, _cmd);
  171. searching = NO;
  172. //[_parentObject endServices:services];
  173. [self updateUI];
  174. }
  175. - (void)updateUI
  176. {
  177. //NSLog(@"%@ %s", self, _cmd);
  178. if(searching)
  179. {
  180. // Update the user interface to indicate searching
  181. // Also update any UI that lists available services
  182. }
  183. else
  184. {
  185. NSLog(@"services: %@", services);
  186. // Update the user interface to indicate not searching
  187. }
  188. }
  189. - (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser
  190. {
  191. //NSLog(@"%@ %s", self, _cmd);
  192. searching = YES;
  193. [self updateUI];
  194. }
  195. // Error handling code
  196. - (void)handleError:(NSNumber *)error
  197. {
  198. NSLog(@"An error occurred. Error code = %d", [error intValue]);
  199. // Handle error here
  200. }
  201. - (BOOL)searching {
  202. return searching;
  203. }
  204. // This object is the delegate of its NSNetServiceBrowser object. We're only interested in services-related methods, so that's what we'll call.
  205. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
  206. NSInteger servicesCount = [services count]-1;
  207. [services insertObject:aNetService atIndex:servicesCount];
  208. [aNetService setDelegate:self];
  209. [aNetService resolveWithTimeout:5.0];
  210. if(!moreComing) {
  211. [deviceController setContent:services];
  212. }
  213. }
  214. #if TARGET_OS_OSX
  215. /*
  216. - (NSString *)input: (NSString *)prompt defaultValue: (NSString *)defaultValue {
  217. NSAlert *alert = [NSAlert alertWithMessageText: prompt
  218. defaultButton:@"OK"
  219. alternateButton:@"Cancel"
  220. otherButton:nil
  221. informativeTextWithFormat:@""];
  222. NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
  223. [input setStringValue:defaultValue];
  224. [alert setAccessoryView:input];
  225. NSInteger button = [alert runModal];
  226. if (button == NSAlertDefaultReturn) {
  227. [input validateEditing];
  228. NSString *inputString = [input stringValue];
  229. return inputString;
  230. } else if (button == NSAlertAlternateReturn) {
  231. return nil;
  232. } else {
  233. NSAssert1(NO, @"Invalid input dialog button %d", button);
  234. return nil;
  235. }
  236. }
  237. */
  238. #endif
  239. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
  240. [services removeObject:aNetService];
  241. if(!moreComing) {
  242. }
  243. }
  244. - (NSDictionary *)deviceDictionary {
  245. return deviceDictionary;
  246. }
  247. - (void)setDeviceDictionary:(NSDictionary *)value {
  248. if (deviceDictionary != value) {
  249. deviceDictionary = [value copy];
  250. }
  251. }
  252. //resolution stuff
  253. - (BOOL)addressesComplete:(NSArray *)addresses
  254. forServiceType:(NSString *)serviceType
  255. {
  256. // Perform appropriate logic to ensure that [netService addresses]
  257. if ([self.delegate respondsToSelector:@selector(servicesFound:)])
  258. {
  259. NSMutableArray *fullServices = [NSMutableArray new];
  260. for (NSNetService *service in services)
  261. {
  262. NSDictionary *fullDict = [self stringDictionaryFromService:service];
  263. if (fullDict != nil)
  264. [fullServices addObject:fullDict];
  265. }
  266. [self.delegate servicesFound:fullServices];
  267. }
  268. return YES;
  269. }
  270. // Sent when addresses are resolved
  271. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  272. {
  273. NSDictionary *finalDict = [self stringDictionaryFromService:aNetService];
  274. if ([[finalDict allKeys] count] > 0)
  275. {
  276. NSString *model = [finalDict objectForKey:@"model"];
  277. if ([model isEqualToString:@"AppleTV5,3"])
  278. {
  279. //NSLog(@"should add service: %@", aNetService);
  280. // NSInteger servicesCount = [services count]-1;
  281. //[services insertObject:aNetService atIndex:servicesCount];
  282. } else {
  283. [services removeObject:aNetService];
  284. [deviceController setContent:services];
  285. }
  286. }
  287. /*
  288. if ([self addressesComplete:[netService addresses]
  289. forServiceType:[netService type]]) {
  290. }
  291. */
  292. }
  293. // Sent if resolution fails
  294. - (void)netService:(NSNetService *)netService
  295. didNotResolve:(NSDictionary *)errorDict
  296. {
  297. //NSLog(@"%@ %s", self, _cmd);
  298. [self handleError:[errorDict objectForKey:NSNetServicesErrorCode]];
  299. [services removeObject:netService];
  300. }
  301. #if TARGET_OS_OSX
  302. // This object is the data source of its NSTableView. servicesList is the NSArray containing all those services that have been discovered.
  303. - (int)numberOfRowsInTableView:(NSTableView *)theTableView {
  304. return [services count];
  305. }
  306. - (NSDictionary *)currentServiceDictionary {
  307. NSNetService * clickedService = [services objectAtIndex:[[self theComboBox] indexOfSelectedItem]];
  308. //NSLog(@"clickedService: %@" ,[self theComboBox]);
  309. return [self stringDictionaryFromService:clickedService];
  310. }
  311. - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex {
  312. NSString *fixedName = [[[[services objectAtIndex:rowIndex] name] componentsSeparatedByString:@"@"] lastObject];
  313. return fixedName;
  314. // return [[services objectAtIndex:rowIndex] name];
  315. }
  316. #endif
  317. @end