PageRenderTime 39ms CodeModel.GetById 10ms app.highlight 25ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 15#import "AppController.h"
 16
 17// Update Engine action classes.
 18#import "KSActionProcessor.h"
 19#import "KSActionPipe.h"
 20
 21// Action sample classes.
 22#import "UECatalogDownloadAction.h"
 23#import "UECatalogFilterAction.h"
 24#import "UECatalogLoaderAction.h"
 25#import "UEImageDownloadAction.h"
 26#import "UENotifications.h"
 27
 28
 29@implementation AppController
 30
 31- (id)init {
 32  if ((self = [super init])) {
 33    images_ = [[NSMutableArray alloc] init];
 34    names_ = [[NSMutableArray alloc] init];
 35  }
 36
 37  return self;
 38
 39}  // init
 40
 41
 42- (void)stopActionProcessor {
 43  [actionProcessor_ stopProcessing];
 44  [actionProcessor_ release];
 45  actionProcessor_ = nil;
 46
 47}  // stopActionProcessor
 48
 49
 50- (void)dealloc {
 51  [self stopActionProcessor];
 52  [images_ release];
 53  [names_ release];
 54  [[NSNotificationCenter defaultCenter] removeObserver:self];
 55
 56  [super dealloc];
 57
 58}  // dealloc
 59
 60
 61- (void)awakeFromNib {
 62  NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 63
 64  // Register for the message logging notification.  Messages get appended
 65  // to an NSTextView as they come in.
 66  [center addObserver:self
 67             selector:@selector(logMessage:)
 68                 name:kUEMessageNotification
 69               object:nil];
 70
 71  // A notification is broadcast when a new image has been downloaded.
 72  // Pick up the image and display it.
 73  [center addObserver:self
 74             selector:@selector(imageDownloadSuccessNotification:)
 75                 name:kImageDownloadSuccessNotification
 76               object:nil];
 77
 78}  // awakeFromNib
 79
 80
 81- (IBAction)start:(id)sender {
 82  // Cancel any previous run.
 83  [self stopActionProcessor];
 84  [images_ removeAllObjects];
 85  [names_ removeAllObjects];
 86  [statusTextView_ setString:@""];
 87
 88  // Read the various knobs in the UI.
 89  NSString *catalogURLString = [catalogURLField_ stringValue];
 90  NSURL *catalogURL = [NSURL URLWithString:catalogURLString];
 91
 92  NSString *predicateString = [predicateField_ stringValue];
 93  NSPredicate *predicate = nil;
 94  if ([predicateString length] > 0) {
 95    predicate = [NSPredicate predicateWithFormat:predicateString];
 96  }
 97
 98  // Build the actions we're going to be using:
 99  //   catalog loader -> filter -> downloader
100
101  UECatalogLoaderAction *catalogLoader =
102    [[[UECatalogLoaderAction alloc] initWithCatalogURL:catalogURL] autorelease];
103
104  UECatalogFilterAction *filter =
105    [[[UECatalogFilterAction alloc] initWithPredicate:predicate] autorelease];
106
107  UECatalogDownloadAction *downloader =
108    [[[UECatalogDownloadAction alloc] init] autorelease];
109
110  // Set up the pipes between the actions
111  [KSActionPipe bondFrom:catalogLoader to:filter];
112  [KSActionPipe bondFrom:filter to:downloader];
113
114  // Create the processor and enqueue the actions.
115  actionProcessor_ = [[KSActionProcessor alloc] initWithDelegate:self];
116
117  [actionProcessor_ enqueueAction:catalogLoader];
118  [actionProcessor_ enqueueAction:filter];
119  [actionProcessor_ enqueueAction:downloader];
120
121  // Woo!  time to actually do some work!
122  [actionProcessor_ startProcessing];
123
124}  // start
125
126
127// --------------------------------------------------
128// KSActionProcessor delegate method
129
130- (void)processingDone:(KSActionProcessor *)processor {
131  UEPostMessage(@"that's all, folks...");
132
133}  // processingDone
134
135
136// --------------------------------------------------
137// Notification methods
138
139- (void)logMessage:(NSNotification *)notification {
140  NSString *message = [[notification userInfo] valueForKey:kUEMessageKey];
141
142  if (message) {
143    // Append the message (plus newline) to the end of the text view.
144    [[[statusTextView_ textStorage] mutableString] appendString:message];
145    [[[statusTextView_ textStorage] mutableString] appendString:@"\n"];
146
147    NSRange range = NSMakeRange ([[statusTextView_ string] length], 0);
148    [statusTextView_ scrollRangeToVisible:range];
149  }
150
151}  // logMessage
152
153
154- (void)imageDownloadSuccessNotification:(NSNotification *)notification {
155  UEPostMessage(@"got an image!");
156
157  NSImage *image = [[notification userInfo] valueForKey:kImageInfoKey];
158  NSString *name = [[notification userInfo] valueForKey:kImageNameKey];
159
160  if (image) {
161    // Put the image at the front of the array so it will appear at the top
162    // of the table view.  Makes it more interesting to watch while images
163    // are loading.
164    [images_ insertObject:image atIndex:0];
165    [names_ insertObject:name atIndex:0];
166    [imageTableView_ reloadData];
167  }
168
169}  // imageDownloadSuccessNotification
170
171
172// --------------------------------------------------
173// NSTableView data source methods.
174
175- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
176  return [images_ count];
177
178}  // numberOfRowsInTableView
179
180
181- (id)tableView:(NSTableView *)tableView 
182objectValueForTableColumn:(NSTableColumn *)tableColumn 
183            row:(NSInteger)row {
184
185  if ([[tableColumn identifier] isEqualToString:@"name"]) {
186    NSString *name = [names_ objectAtIndex:row];
187    return name;
188  } else {
189    NSImage *image = [images_ objectAtIndex:row];
190    return image;
191  }
192
193}  // objectValueForTableColumn
194
195@end // AppController