PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Mobile/Controllers/CQHelpTopicsViewController.m

https://github.com/HHRy/Colloquy
Objective C | 267 lines | 194 code | 73 blank | 0 comment | 35 complexity | 12893fef156a4dd2a5b133dfa91818b6 MD5 | raw file
  1. #import "CQHelpTopicsViewController.h"
  2. #import "CQColloquyApplication.h"
  3. #import "CQHelpTopicViewController.h"
  4. #import <MediaPlayer/MPMoviePlayerController.h>
  5. static NSString *CQHelpTopicsURLFormatString = @"http://colloquy.mobi/help.php?locale=%@";
  6. @interface CQHelpTopicsViewController (CQHelpTopicsViewControllerPrivate)
  7. - (void) _generateSectionsFromHelpContent:(NSArray *) help;
  8. @end
  9. #pragma mark -
  10. @implementation CQHelpTopicsViewController
  11. - (id) init {
  12. if (!(self = [super initWithStyle:UITableViewStyleGrouped]))
  13. return nil;
  14. self.title = NSLocalizedString(@"Help", @"Help view title");
  15. [self loadHelpContent];
  16. return self;
  17. }
  18. - (void) dealloc {
  19. [[NSNotificationCenter defaultCenter] removeObserver:self];
  20. [_helpSections release];
  21. [_helpData release];
  22. [_moviePlayer release];
  23. [super dealloc];
  24. }
  25. #pragma mark -
  26. - (void) loadHelpContent {
  27. if (_loading)
  28. return;
  29. _loading = YES;
  30. id old = _helpData;
  31. _helpData = [[NSMutableData alloc] initWithCapacity:4096];
  32. [old release];
  33. NSString *urlString = [NSString stringWithFormat:CQHelpTopicsURLFormatString, [[NSLocale autoupdatingCurrentLocale] localeIdentifier]];
  34. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.];
  35. [NSURLConnection connectionWithRequest:request delegate:self];
  36. }
  37. - (void) loadDefaultHelpContent {
  38. NSArray *help = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Help" ofType:@"plist"]];
  39. [self _generateSectionsFromHelpContent:help];
  40. }
  41. #pragma mark -
  42. - (void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
  43. [_helpData appendData:data];
  44. }
  45. - (void) connectionDidFinishLoading:(NSURLConnection *) connection {
  46. _loading = NO;
  47. NSArray *help = [NSPropertyListSerialization propertyListFromData:_helpData mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
  48. [_helpData release];
  49. _helpData = nil;
  50. if (help.count)
  51. [self _generateSectionsFromHelpContent:help];
  52. else [self loadDefaultHelpContent];
  53. }
  54. - (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
  55. _loading = NO;
  56. [_helpData release];
  57. _helpData = nil;
  58. [self loadDefaultHelpContent];
  59. }
  60. #pragma mark -
  61. - (NSInteger) numberOfSectionsInTableView:(UITableView *) tableView {
  62. return (_helpSections.count ? _helpSections.count : 1);
  63. }
  64. - (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section {
  65. if (!_helpSections.count)
  66. return 1;
  67. return ((NSArray *)[_helpSections objectAtIndex:section]).count;
  68. }
  69. - (NSString *) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger) section {
  70. if (_helpSections.count) {
  71. NSArray *sectionItems = [_helpSections objectAtIndex:section];
  72. NSDictionary *info = [sectionItems objectAtIndex:0];
  73. return [info objectForKey:@"SectionHeader"];
  74. }
  75. return nil;
  76. }
  77. - (NSString *) tableView:(UITableView *) tableView titleForFooterInSection:(NSInteger) section {
  78. if (_helpSections.count) {
  79. NSArray *sectionItems = [_helpSections objectAtIndex:section];
  80. NSDictionary *info = [sectionItems lastObject];
  81. return [info objectForKey:@"SectionFooter"];
  82. }
  83. return nil;
  84. }
  85. - (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
  86. if (!_helpSections.count) {
  87. UITableViewCell *cell = [UITableViewCell reusableTableViewCellInTableView:tableView withIdentifier:@"Updating"];
  88. cell.textLabel.text = NSLocalizedString(@"Updating Help Topics...", @"Updating help topics label");
  89. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  90. UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  91. [spinner startAnimating];
  92. cell.accessoryView = spinner;
  93. [spinner release];
  94. return cell;
  95. }
  96. UITableViewCell *cell = [UITableViewCell reusableTableViewCellInTableView:tableView];
  97. NSArray *sectionItems = [_helpSections objectAtIndex:indexPath.section];
  98. NSDictionary *info = [sectionItems objectAtIndex:indexPath.row];
  99. cell.textLabel.text = [info objectForKey:@"Title"];
  100. if ([info objectForKey:@"Content"]) {
  101. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  102. cell.accessoryView = nil;
  103. } else if ([info objectForKey:@"Screencast"]) {
  104. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"screencast.png"] highlightedImage:[UIImage imageNamed:@"screencastSelected.png"]];
  105. cell.accessoryView = imageView;
  106. [imageView release];
  107. } else if ([info objectForKey:@"Link"]) {
  108. UIImageView *imageView = nil;
  109. NSURL *url = [NSURL URLWithString:[info objectForKey:@"Link"]];
  110. if ([[CQColloquyApplication sharedApplication].handledURLSchemes containsObject:[url.scheme lowercaseString]])
  111. imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chatBubble.png"] highlightedImage:[UIImage imageNamed:@"chatBubbleSelected.png"]];
  112. else imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"web.png"] highlightedImage:[UIImage imageNamed:@"webSelected.png"]];
  113. cell.accessoryView = imageView;
  114. [imageView release];
  115. }
  116. return cell;
  117. }
  118. - (NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *) indexPath {
  119. if (!_helpSections.count)
  120. return nil;
  121. return indexPath;
  122. }
  123. - (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
  124. if (!_helpSections.count)
  125. return;
  126. NSArray *sectionItems = [_helpSections objectAtIndex:indexPath.section];
  127. NSDictionary *info = [sectionItems objectAtIndex:indexPath.row];
  128. if ([info objectForKey:@"Content"]) {
  129. CQHelpTopicViewController *helpTopicController = [[CQHelpTopicViewController alloc] initWithHTMLContent:[info objectForKey:@"Content"]];
  130. helpTopicController.navigationItem.rightBarButtonItem = self.navigationItem.rightBarButtonItem;
  131. helpTopicController.title = [info objectForKey:@"Title"];
  132. [self.navigationController pushViewController:helpTopicController animated:YES];
  133. [helpTopicController release];
  134. } else if ([info objectForKey:@"Screencast"]) {
  135. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
  136. [_moviePlayer release];
  137. _moviePlayer = nil;
  138. @try {
  139. _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[info objectForKey:@"Screencast"]]];
  140. _moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
  141. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_screencastDidFinishPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
  142. [_moviePlayer play];
  143. } @catch (NSException *exception) {
  144. [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
  145. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
  146. [_moviePlayer release];
  147. _moviePlayer = nil;
  148. }
  149. } else if ([info objectForKey:@"Link"]) {
  150. NSURL *url = [NSURL URLWithString:[info objectForKey:@"Link"]];
  151. if ([[NSUserDefaults standardUserDefaults] boolForKey:@"CQDisableBuiltInBrowser"] && url) {
  152. [[UIApplication sharedApplication] openURL:url];
  153. } else if (url) {
  154. [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
  155. [[CQColloquyApplication sharedApplication] dismissModalViewControllerAnimated:YES];
  156. [[UIApplication sharedApplication] performSelector:@selector(openURL:) withObject:url afterDelay:0.5];
  157. } else {
  158. [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
  159. }
  160. } else {
  161. [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
  162. }
  163. }
  164. #pragma mark -
  165. - (void) _screencastDidFinishPlaying {
  166. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
  167. [_moviePlayer stop];
  168. [_moviePlayer release];
  169. _moviePlayer = nil;
  170. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  171. }
  172. - (void) _generateSectionsFromHelpContent:(NSArray *) help {
  173. id old = _helpSections;
  174. _helpSections = [[NSMutableArray alloc] initWithCapacity:5];
  175. [old release];
  176. NSUInteger i = 0;
  177. NSUInteger sectionStart = 0;
  178. for (id item in help) {
  179. if ([item isKindOfClass:[NSString class]] && [item isEqualToString:@"Space"]) {
  180. if (i == sectionStart)
  181. continue;
  182. NSArray *section = [help subarrayWithRange:NSMakeRange(sectionStart, (i - sectionStart))];
  183. [_helpSections addObject:section];
  184. sectionStart = (i + 1);
  185. }
  186. ++i;
  187. }
  188. if (i != sectionStart) {
  189. NSArray *section = [help subarrayWithRange:NSMakeRange(sectionStart, (i - sectionStart))];
  190. [_helpSections addObject:section];
  191. }
  192. [self.tableView reloadData];
  193. }
  194. @end