PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/Samples/EngineRunner/ERRunUpdateTicketCommand.m

http://macfuse.googlecode.com/
Objective C | 157 lines | 76 code | 50 blank | 31 comment | 5 complexity | 15a6fd77fd403b4d8378b9531e50976b MD5 | raw file
  1// Copyright 2008 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 "ERRunUpdateTicketCommand.h"
 16
 17#import "GTMLogger.h"
 18#import "KSUpdateEngine.h"
 19
 20
 21@implementation ERRunUpdateTicketCommand
 22
 23- (NSString *)name {
 24  return @"runticket";
 25}  // name
 26
 27
 28- (NSString *)blurb {
 29  return @"Update all of the products in a ticket store";
 30}  // blurb
 31
 32
 33- (NSDictionary *)requiredArguments {
 34  return [NSDictionary dictionaryWithObjectsAndKeys:
 35                       @"Path to the ticket store", @"store",
 36                       nil];
 37
 38}  // requiredArguments
 39
 40
 41- (NSDictionary *)optionalArguments {
 42  return [NSDictionary dictionaryWithObjectsAndKeys:
 43                       @"Only update this product ID", @"productid",
 44                       nil];
 45
 46}  // optionalArguments
 47
 48
 49- (BOOL)runWithArguments:(NSDictionary *)args {
 50  success_ = YES;  // Innocent until proven guilty
 51
 52  // Pick up the ticket store.
 53  NSString *storePath = [args objectForKey:@"store"];
 54  KSTicketStore *ticketStore = [KSTicketStore ticketStoreWithPath:storePath];
 55
 56  // Make the engine to run things.
 57  KSUpdateEngine *vroom = [KSUpdateEngine engineWithTicketStore:ticketStore
 58                                                       delegate:self];
 59
 60  // Optional argument, if we want to do the update check for just one product
 61  // rather than everything in the store.
 62  NSString *productID = [args objectForKey:@"productid"];
 63
 64  if (productID == nil) {
 65    [vroom updateAllProducts];
 66  } else {
 67    [vroom updateProductWithProductID:productID];
 68  }
 69
 70  // Let Update Engine do its thing.
 71  while ([vroom isUpdating]) {
 72    NSDate *spin = [NSDate dateWithTimeIntervalSinceNow:1];
 73    [[NSRunLoop currentRunLoop] runUntilDate:spin];
 74  }
 75
 76  // |success_| has an accumulated YES/NO value as each update completes.
 77  return success_;
 78
 79}  // runWithArguments
 80
 81@end  // ERRunUpdateTicketCommand
 82
 83
 84@implementation ERRunUpdateTicketCommand (UpdateEngineDelegateMethods)
 85
 86// We've actually implemented a lot more of these than we need, but
 87// it's fun to see them in action.  Feel free to set breakpoints on all
 88// of these and poke around the data that flows through them.
 89
 90- (void)engineStarted:(KSUpdateEngine *)engine {
 91  GTMLoggerInfo(@"starting engine");
 92}  // engineStarted
 93
 94
 95- (NSArray *)engine:(KSUpdateEngine *)engine
 96  shouldPrefetchProducts:(NSArray *)products {
 97  NSArray *prefetch = products;
 98
 99  // Go ahead and download all of the products before updating them.
100  return prefetch;
101
102}  // shouldPrefetchProducts
103
104
105- (NSArray *)engine:(KSUpdateEngine *)engine
106  shouldSilentlyUpdateProducts:(NSArray *)products {
107
108  // Since we're planning on being run as a command-line program from
109  // launchd or by an administrator, no need to wait and ask for the
110  // user's permission.
111  return products;
112
113}  // shouldSilentlyUpdateProducts
114
115
116- (id<KSCommandRunner>)commandRunnerForEngine:(KSUpdateEngine *)engine {
117  return [KSTaskCommandRunner commandRunner];
118}  // commandRunnerForEngine
119
120
121- (void)engine:(KSUpdateEngine *)engine
122      starting:(KSUpdateInfo *)updateInfo {
123  GTMLoggerInfo(@"starting update of %@", [updateInfo productID]);
124}  // starting
125
126
127- (NSArray *)engine:(KSUpdateEngine *)engine
128  shouldUpdateProducts:(NSArray *)products {
129  // If we had a user interface, this is where we'd ask the user
130  // about updating products, and filter out all the things that the
131  // user rejected.
132
133  return products;
134
135}  // shouldUpdateProducts
136
137
138- (void)engine:(KSUpdateEngine *)engine
139      finished:(KSUpdateInfo *)updateInfo
140    wasSuccess:(BOOL)wasSuccess
141   wantsReboot:(BOOL)wantsReboot {
142
143  fprintf(stdout, "finished update of %s:  %s\n", 
144          [[updateInfo productID] UTF8String],
145          (wasSuccess) ? "Success" : "Failure");
146
147  // Once we get a single |wasSuccess| == NO, |success_| will be NO
148  // from here on out.
149  success_ = success_ && wasSuccess;
150}  // finished
151
152
153- (void)engineFinished:(KSUpdateEngine *)engine wasSuccess:(BOOL)wasSuccess {
154  GTMLoggerInfo(@"engine finished and is shutting down");
155}  // engineFinished
156
157@end  // UpdateEngineDelegateMethods