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