PageRenderTime 35ms CodeModel.GetById 1ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 119 lines | 60 code | 37 blank | 22 comment | 2 complexity | 16ea56d12bc64bf64cf92b0b5c1bee86 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 "UEImageDownloadAction.h"
 16
 17// Update Engine action class
 18#import "KSActionProcessor.h"
 19
 20// Action sample class.
 21#import "UENotifications.h"
 22
 23// Other
 24#import "GDataHTTPFetcher.h"
 25
 26
 27@implementation UEImageDownloadAction
 28
 29- (id)initWithImageURL:(NSURL *)imageURL
 30                 index:(int)index {
 31  if ((self = [super init])) {
 32    imageURL_ = [imageURL retain];
 33    index_ = index;
 34  }
 35
 36  return self;
 37
 38}  // initWithImageURL
 39
 40
 41- (void)dealloc {
 42  [imageURL_ release];
 43  [httpFetcher_ release];
 44
 45  [super dealloc];
 46
 47}  // dealloc
 48
 49
 50- (void)performAction {
 51  // Download the image from the URL provided to us at init time.
 52  UEPostMessage(@"Performing UEImageLoaderAction (%d)", index_);
 53
 54  NSURLRequest *request = [NSURLRequest requestWithURL:imageURL_];
 55  httpFetcher_ = [[GDataHTTPFetcher alloc] initWithRequest:request];
 56  [httpFetcher_ beginFetchWithDelegate:self
 57                     didFinishSelector:@selector(fetcher:epicWinWithData:)
 58                       didFailSelector:@selector(fetcher:epicFailWithError:)];
 59}  // performAction
 60
 61
 62- (void)terminateAction {
 63  [httpFetcher_ release];
 64  httpFetcher_ = nil;
 65
 66}  // terminateAction
 67
 68
 69- (void)publishImageFromData:(NSData *)data {
 70  // We've received image data.  Create an NSImage, and then post a notification
 71  // for anyone interested.
 72  NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
 73  // Figure out the name of the image.
 74  NSString *name = [[imageURL_ path] lastPathComponent];
 75
 76  if (image) {
 77    NSDictionary *userInfo =
 78      [NSDictionary dictionaryWithObjectsAndKeys:image, kImageInfoKey,
 79                    name, kImageNameKey, nil];
 80
 81    [[NSNotificationCenter defaultCenter]
 82      postNotificationName:kImageDownloadSuccessNotification
 83                    object:nil
 84                  userInfo:userInfo];
 85  }
 86
 87}  // publishImageFromData
 88
 89
 90// --------------------------------------------------
 91// GDtaaHTTPFetcher callback methods
 92
 93- (void)done {
 94  [[self processor] finishedProcessing:self successfully:YES];
 95
 96}  // done
 97
 98
 99- (void)fetcher:(GDataHTTPFetcher *)fetcher epicWinWithData:(NSData *)data {
100  UEPostMessage(@"UEImageDownloadAction loaded image (%d)", index_);
101  [self publishImageFromData:data];
102
103  [self performSelectorOnMainThread:@selector(done)
104                         withObject:nil
105                      waitUntilDone:NO];
106
107}  // epicWinWithData
108
109
110- (void)fetcher:(GDataHTTPFetcher *)fetcher epicFailWithError:(NSError *)error {
111  UEPostMessage(@"UEImageDownloadAction could not load image: %@", imageURL_);
112
113  [self performSelectorOnMainThread:@selector(done)
114                         withObject:nil
115                      waitUntilDone:NO];
116
117}  // epicFailWithError
118
119@end  // UEImageDownloadAction