/core/externals/update-engine/Samples/Actions/AppController.m

http://macfuse.googlecode.com/ · Objective C · 195 lines · 102 code · 57 blank · 36 comment · 6 complexity · 368657f439088512e6e4b3eae73c0191 MD5 · raw file

  1. // Copyright 2009 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "AppController.h"
  15. // Update Engine action classes.
  16. #import "KSActionProcessor.h"
  17. #import "KSActionPipe.h"
  18. // Action sample classes.
  19. #import "UECatalogDownloadAction.h"
  20. #import "UECatalogFilterAction.h"
  21. #import "UECatalogLoaderAction.h"
  22. #import "UEImageDownloadAction.h"
  23. #import "UENotifications.h"
  24. @implementation AppController
  25. - (id)init {
  26. if ((self = [super init])) {
  27. images_ = [[NSMutableArray alloc] init];
  28. names_ = [[NSMutableArray alloc] init];
  29. }
  30. return self;
  31. } // init
  32. - (void)stopActionProcessor {
  33. [actionProcessor_ stopProcessing];
  34. [actionProcessor_ release];
  35. actionProcessor_ = nil;
  36. } // stopActionProcessor
  37. - (void)dealloc {
  38. [self stopActionProcessor];
  39. [images_ release];
  40. [names_ release];
  41. [[NSNotificationCenter defaultCenter] removeObserver:self];
  42. [super dealloc];
  43. } // dealloc
  44. - (void)awakeFromNib {
  45. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  46. // Register for the message logging notification. Messages get appended
  47. // to an NSTextView as they come in.
  48. [center addObserver:self
  49. selector:@selector(logMessage:)
  50. name:kUEMessageNotification
  51. object:nil];
  52. // A notification is broadcast when a new image has been downloaded.
  53. // Pick up the image and display it.
  54. [center addObserver:self
  55. selector:@selector(imageDownloadSuccessNotification:)
  56. name:kImageDownloadSuccessNotification
  57. object:nil];
  58. } // awakeFromNib
  59. - (IBAction)start:(id)sender {
  60. // Cancel any previous run.
  61. [self stopActionProcessor];
  62. [images_ removeAllObjects];
  63. [names_ removeAllObjects];
  64. [statusTextView_ setString:@""];
  65. // Read the various knobs in the UI.
  66. NSString *catalogURLString = [catalogURLField_ stringValue];
  67. NSURL *catalogURL = [NSURL URLWithString:catalogURLString];
  68. NSString *predicateString = [predicateField_ stringValue];
  69. NSPredicate *predicate = nil;
  70. if ([predicateString length] > 0) {
  71. predicate = [NSPredicate predicateWithFormat:predicateString];
  72. }
  73. // Build the actions we're going to be using:
  74. // catalog loader -> filter -> downloader
  75. UECatalogLoaderAction *catalogLoader =
  76. [[[UECatalogLoaderAction alloc] initWithCatalogURL:catalogURL] autorelease];
  77. UECatalogFilterAction *filter =
  78. [[[UECatalogFilterAction alloc] initWithPredicate:predicate] autorelease];
  79. UECatalogDownloadAction *downloader =
  80. [[[UECatalogDownloadAction alloc] init] autorelease];
  81. // Set up the pipes between the actions
  82. [KSActionPipe bondFrom:catalogLoader to:filter];
  83. [KSActionPipe bondFrom:filter to:downloader];
  84. // Create the processor and enqueue the actions.
  85. actionProcessor_ = [[KSActionProcessor alloc] initWithDelegate:self];
  86. [actionProcessor_ enqueueAction:catalogLoader];
  87. [actionProcessor_ enqueueAction:filter];
  88. [actionProcessor_ enqueueAction:downloader];
  89. // Woo! time to actually do some work!
  90. [actionProcessor_ startProcessing];
  91. } // start
  92. // --------------------------------------------------
  93. // KSActionProcessor delegate method
  94. - (void)processingDone:(KSActionProcessor *)processor {
  95. UEPostMessage(@"that's all, folks...");
  96. } // processingDone
  97. // --------------------------------------------------
  98. // Notification methods
  99. - (void)logMessage:(NSNotification *)notification {
  100. NSString *message = [[notification userInfo] valueForKey:kUEMessageKey];
  101. if (message) {
  102. // Append the message (plus newline) to the end of the text view.
  103. [[[statusTextView_ textStorage] mutableString] appendString:message];
  104. [[[statusTextView_ textStorage] mutableString] appendString:@"\n"];
  105. NSRange range = NSMakeRange ([[statusTextView_ string] length], 0);
  106. [statusTextView_ scrollRangeToVisible:range];
  107. }
  108. } // logMessage
  109. - (void)imageDownloadSuccessNotification:(NSNotification *)notification {
  110. UEPostMessage(@"got an image!");
  111. NSImage *image = [[notification userInfo] valueForKey:kImageInfoKey];
  112. NSString *name = [[notification userInfo] valueForKey:kImageNameKey];
  113. if (image) {
  114. // Put the image at the front of the array so it will appear at the top
  115. // of the table view. Makes it more interesting to watch while images
  116. // are loading.
  117. [images_ insertObject:image atIndex:0];
  118. [names_ insertObject:name atIndex:0];
  119. [imageTableView_ reloadData];
  120. }
  121. } // imageDownloadSuccessNotification
  122. // --------------------------------------------------
  123. // NSTableView data source methods.
  124. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
  125. return [images_ count];
  126. } // numberOfRowsInTableView
  127. - (id)tableView:(NSTableView *)tableView
  128. objectValueForTableColumn:(NSTableColumn *)tableColumn
  129. row:(NSInteger)row {
  130. if ([[tableColumn identifier] isEqualToString:@"name"]) {
  131. NSString *name = [names_ objectAtIndex:row];
  132. return name;
  133. } else {
  134. NSImage *image = [images_ objectAtIndex:row];
  135. return image;
  136. }
  137. } // objectValueForTableColumn
  138. @end // AppController