PageRenderTime 84ms CodeModel.GetById 10ms app.highlight 68ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Examples/SpreadsheetSample/SpreadsheetSampleWindowController.m

http://macfuse.googlecode.com/
Objective C | 548 lines | 347 code | 139 blank | 62 comment | 58 complexity | 408fe80a6b12beae34b16580a5861092 MD5 | raw file
  1/* Copyright (c) 2007 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
 16//
 17//  SpreadsheetSampleWindowController.m
 18//
 19
 20#import "SpreadsheetSampleWindowController.h"
 21
 22@interface SpreadsheetSampleWindowController (PrivateMethods)
 23- (void)updateUI;
 24
 25- (void)fetchFeedOfSpreadsheets;
 26- (void)fetchSelectedSpreadsheet;
 27- (void)fetchSelectedWorksheet;
 28
 29- (GDataServiceGoogleSpreadsheet *)spreadsheetService;
 30- (GDataEntrySpreadsheet *)selectedSpreadsheet;
 31- (GDataEntryWorksheet *)selectedWorksheet;
 32- (GDataEntryBase *)selectedEntry;
 33
 34- (GDataFeedSpreadsheet *)spreadsheetFeed;
 35- (void)setSpreadsheetFeed:(GDataFeedSpreadsheet *)feed;
 36- (NSError *)spreadsheetFetchError;
 37- (void)setSpreadsheetFetchError:(NSError *)error;  
 38
 39- (GDataFeedWorksheet *)worksheetFeed;
 40- (void)setWorksheetFeed:(GDataFeedWorksheet *)feed;
 41- (NSError *)worksheetFetchError;
 42- (void)setWorksheetFetchError:(NSError *)error;
 43  
 44- (GDataFeedBase *)entryFeed;
 45- (void)setEntryFeed:(GDataFeedBase *)feed;
 46- (NSError *)entryFetchError;
 47- (void)setEntryFetchError:(NSError *)error;
 48
 49@end
 50
 51@implementation SpreadsheetSampleWindowController
 52
 53static SpreadsheetSampleWindowController* gSpreadsheetSampleWindowController = nil;
 54
 55
 56+ (SpreadsheetSampleWindowController *)sharedSpreadsheetSampleWindowController {
 57  
 58  if (!gSpreadsheetSampleWindowController) {
 59    gSpreadsheetSampleWindowController = [[SpreadsheetSampleWindowController alloc] init];
 60  }  
 61  return gSpreadsheetSampleWindowController;
 62}
 63
 64
 65- (id)init {
 66  return [self initWithWindowNibName:@"SpreadsheetSampleWindow"];
 67}
 68
 69- (void)windowDidLoad {
 70}
 71
 72- (void)awakeFromNib {
 73  // Set the result text fields to have a distinctive color and mono-spaced font
 74  // to aid in understanding of each query operation.
 75  [mSpreadsheetResultTextField setTextColor:[NSColor darkGrayColor]];
 76  [mWorksheetResultTextField setTextColor:[NSColor darkGrayColor]];
 77  [mEntryResultTextField setTextColor:[NSColor darkGrayColor]];
 78
 79  NSFont *resultTextFont = [NSFont fontWithName:@"Monaco" size:9];
 80  [mSpreadsheetResultTextField setFont:resultTextFont];
 81  [mWorksheetResultTextField setFont:resultTextFont];
 82  [mEntryResultTextField setFont:resultTextFont];
 83
 84  [self updateUI];
 85}
 86
 87- (void)dealloc {
 88  [mSpreadsheetFeed release];
 89  [mSpreadsheetFetchError release];
 90  
 91  [mWorksheetFeed release];
 92  [mWorksheetFetchError release];
 93  
 94  [mEntryFeed release];
 95  [mEntryFetchError release];
 96  
 97  [super dealloc];
 98}
 99
100#pragma mark -
101
102- (void)updateUI {
103  
104  // spreadsheet list display
105  [mSpreadsheetTable reloadData]; 
106  
107  if (mIsSpreadsheetFetchPending) {
108    [mSpreadsheetProgressIndicator startAnimation:self];  
109  } else {
110    [mSpreadsheetProgressIndicator stopAnimation:self];  
111  }
112  
113  // spreadsheet fetch result or selected item
114  NSString *spreadsheetResultStr = @"";
115  if (mSpreadsheetFetchError) {
116    spreadsheetResultStr = [mSpreadsheetFetchError description];
117  } else {
118    GDataEntrySpreadsheet *spreadsheet = [self selectedSpreadsheet];
119    if (spreadsheet) {
120      spreadsheetResultStr = [spreadsheet description];
121    } else {
122      
123    }
124  }
125  [mSpreadsheetResultTextField setString:spreadsheetResultStr];
126  
127  
128  // Worksheet list display
129  [mWorksheetTable reloadData]; 
130  
131  if (mIsWorksheetFetchPending) {
132    [mWorksheetProgressIndicator startAnimation:self];  
133  } else {
134    [mWorksheetProgressIndicator stopAnimation:self];  
135  }
136  
137  // Worksheet fetch result or selected item
138  NSString *worksheetResultStr = @"";
139  if (mWorksheetFetchError) {
140    worksheetResultStr = [mWorksheetFetchError description];
141  } else {
142    GDataEntryWorksheet *worksheet = [self selectedWorksheet];
143    if (worksheet) {
144      worksheetResultStr = [worksheet description];
145    }
146  }
147  [mWorksheetResultTextField setString:worksheetResultStr];
148  
149  
150  // cell/list entry display
151  [mEntryTable reloadData];
152  
153  if (mIsEntryFetchPending) {
154    [mEntryProgressIndicator startAnimation:self];  
155  } else {
156    [mEntryProgressIndicator stopAnimation:self];  
157  }
158  
159  // entry fetch result or selected item
160  NSString *entryResultStr = @"";
161  if (mEntryFetchError) {
162    entryResultStr = [mEntryFetchError description];
163  } else {
164    GDataEntryBase *entry = [self selectedEntry];
165    if (entry) {
166      entryResultStr = [entry description];
167    }
168  }
169  [mEntryResultTextField setString:entryResultStr];
170  
171}
172
173#pragma mark IBActions
174
175- (IBAction)getSpreadsheetClicked:(id)sender {
176  
177  NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
178  
179  NSString *username = [mUsernameField stringValue];
180  username = [username stringByTrimmingCharactersInSet:whitespace];
181  
182  if ([username rangeOfString:@"@"].location == NSNotFound) {
183    // if no domain was supplied, add @gmail.com
184    username = [username stringByAppendingString:@"@gmail.com"];
185  }
186  
187  [mUsernameField setStringValue:username];
188
189  [self fetchFeedOfSpreadsheets];
190}
191
192- (IBAction)feedSegmentClicked:(id)sender {
193  // user switched between cell and list feed
194  [self fetchSelectedWorksheet];
195}
196
197- (IBAction)loggingCheckboxClicked:(id)sender {
198  [GTMHTTPFetcher setLoggingEnabled:[sender state]]; 
199}
200
201#pragma mark -
202
203// get a spreadsheet service object with the current username/password
204//
205// A "service" object handles networking tasks.  Service objects
206// contain user authentication information as well as networking
207// state information (such as cookies and the "last modified" date for
208// fetched data.)
209
210- (GDataServiceGoogleSpreadsheet *)spreadsheetService {
211  
212  static GDataServiceGoogleSpreadsheet* service = nil;
213
214  if (!service) {
215    service = [[GDataServiceGoogleSpreadsheet alloc] init];
216
217    [service setShouldCacheResponseData:YES];
218    [service setServiceShouldFollowNextLinks:YES];
219  }
220
221  // username/password may change
222  NSString *username = [mUsernameField stringValue];
223  NSString *password = [mPasswordField stringValue];
224  
225  [service setUserCredentialsWithUsername:username
226                                 password:password];
227  
228  return service;
229}
230
231// get the spreadsheet selected in the top list, or nil if none
232- (GDataEntrySpreadsheet *)selectedSpreadsheet {
233  
234  NSArray *spreadsheets = [mSpreadsheetFeed entries];
235  int rowIndex = [mSpreadsheetTable selectedRow];
236  if ([spreadsheets count] > 0 && rowIndex > -1) {
237    
238    GDataEntrySpreadsheet *spreadsheet = [spreadsheets objectAtIndex:rowIndex];
239    return spreadsheet;
240  }
241  return nil;
242}
243
244// get the Worksheet selected in the second list, or nil if none
245- (GDataEntryWorksheet *)selectedWorksheet {
246  
247  NSArray *Worksheets = [mWorksheetFeed entries];
248  int rowIndex = [mWorksheetTable selectedRow];
249  if ([Worksheets count] > 0 && rowIndex > -1) {
250    
251    GDataEntryWorksheet *Worksheet = [Worksheets objectAtIndex:rowIndex];
252    return Worksheet;
253  }
254  return nil;
255}
256
257// get the cell or list entry selected in the bottom list
258- (GDataEntryBase *)selectedEntry {
259  
260  NSArray *entries = [mEntryFeed entries];
261  
262  int rowIndex = [mEntryTable selectedRow];
263  if ([entries count] > 0 && rowIndex > -1) {
264    
265    GDataEntryBase *entry = [entries objectAtIndex:rowIndex];
266    return entry;
267  }
268  return nil;
269}
270
271#pragma mark Fetch feed of all of the user's spreadsheets
272
273// begin retrieving the list of the user's spreadsheets
274- (void)fetchFeedOfSpreadsheets {
275
276  [self setSpreadsheetFeed:nil];
277  [self setSpreadsheetFetchError:nil];
278
279  [self setWorksheetFeed:nil];
280  [self setWorksheetFetchError:nil];
281
282  [self setEntryFeed:nil];
283  [self setEntryFetchError:nil];
284
285  mIsSpreadsheetFetchPending = YES;
286
287  GDataServiceGoogleSpreadsheet *service = [self spreadsheetService];
288  NSURL *feedURL = [NSURL URLWithString:kGDataGoogleSpreadsheetsPrivateFullFeed];
289  [service fetchFeedWithURL:feedURL
290                   delegate:self
291          didFinishSelector:@selector(feedTicket:finishedWithFeed:error:)];
292
293  [self updateUI];
294}
295
296// spreadsheet list fetch callback
297- (void)feedTicket:(GDataServiceTicket *)ticket
298  finishedWithFeed:(GDataFeedSpreadsheet *)feed
299             error:(NSError *)error {
300
301  [self setSpreadsheetFeed:feed];
302  [self setSpreadsheetFetchError:error];
303
304  mIsSpreadsheetFetchPending = NO;
305  [self updateUI];
306}
307
308#pragma mark Fetch a spreadsheet's Worksheets
309
310// for the spreadsheet selected in the top list, begin retrieving the list of
311// Worksheets
312- (void)fetchSelectedSpreadsheet {
313  
314  GDataEntrySpreadsheet *spreadsheet = [self selectedSpreadsheet];
315  if (spreadsheet) {
316    
317    NSURL *feedURL = [spreadsheet worksheetsFeedURL];
318    if (feedURL) {
319      
320      [self setWorksheetFeed:nil];
321      [self setWorksheetFetchError:nil];
322      mIsWorksheetFetchPending = YES;
323      
324      [self setEntryFeed:nil];
325      [self setEntryFetchError:nil];      
326
327      GDataServiceGoogleSpreadsheet *service = [self spreadsheetService];
328      [service fetchFeedWithURL:feedURL
329                       delegate:self
330              didFinishSelector:@selector(worksheetsTicket:finishedWithFeed:error:)];
331      [self updateUI];
332    }
333  }
334}
335
336// fetch worksheet feed callback
337- (void)worksheetsTicket:(GDataServiceTicket *)ticket
338        finishedWithFeed:(GDataFeedWorksheet *)feed
339                   error:(NSError *)error {
340
341  [self setWorksheetFeed:feed];
342  [self setWorksheetFetchError:error];
343
344  mIsWorksheetFetchPending = NO;
345
346  [self updateUI];
347}
348
349#pragma mark Fetch a worksheet's entries
350
351// for the worksheet selected, fetch either a cell feed or a list feed
352// of its contents, depending on the segmented control's setting
353
354- (void)fetchSelectedWorksheet {
355  
356  GDataEntryWorksheet *worksheet = [self selectedWorksheet];
357  if (worksheet) {
358    
359    // the segmented control lets the user retrieve cell entries (position 0)
360    // or list entries (position 1)
361    int segmentIndex = [mFeedSelectorSegments selectedSegment];
362    NSURL *feedURL;
363
364    if (segmentIndex == 0) {
365      feedURL = [[worksheet cellsLink] URL];
366
367    } else {
368      feedURL = [worksheet listFeedURL];
369    }
370
371    if (feedURL) {
372
373      [self setEntryFeed:nil];
374      [self setEntryFetchError:nil];
375
376      mIsEntryFetchPending = YES;
377
378      GDataServiceGoogleSpreadsheet *service = [self spreadsheetService];
379      [service fetchFeedWithURL:feedURL
380                       delegate:self
381              didFinishSelector:@selector(entriesTicket:finishedWithFeed:error:)];
382      [self updateUI];
383    }
384  }
385}
386
387// fetch entries callback
388- (void)entriesTicket:(GDataServiceTicket *)ticket
389     finishedWithFeed:(GDataFeedBase *)feed
390                error:(NSError *)error {
391
392  [self setEntryFeed:feed];
393  [self setEntryFetchError:error];
394
395  mIsEntryFetchPending = NO;
396
397  [self updateUI];
398}
399
400#pragma mark TableView delegate methods
401//
402// table view delegate methods
403//
404
405- (void)tableViewSelectionDidChange:(NSNotification *)notification {
406  id obj = [notification object];
407  if (obj == mSpreadsheetTable) {
408    // the user clicked on a spreadsheet, so fetch its Worksheets
409    [self fetchSelectedSpreadsheet];
410  } else if (obj == mWorksheetTable) {
411    [self fetchSelectedWorksheet];
412  } else {
413    [self updateUI];
414  }
415}
416
417// table view data source methods
418- (int)numberOfRowsInTableView:(NSTableView *)tableView {
419  if (tableView == mSpreadsheetTable) {
420    return [[mSpreadsheetFeed entries] count];
421  } else if (tableView == mWorksheetTable) {
422    return [[mWorksheetFeed entries] count];
423  } else {
424    return [[mEntryFeed entries] count]; 
425  }
426}
427
428- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
429  
430  if (tableView == mSpreadsheetTable) {
431    
432    // get the spreadsheet entry's title
433    GDataEntrySpreadsheet *spreadsheet = [[mSpreadsheetFeed entries] objectAtIndex:row];
434    return [[spreadsheet title] stringValue];
435    
436  } else if (tableView == mWorksheetTable) {
437    
438    // get the worksheet entry's title
439    GDataEntryWorksheet *worksheetEntry = [[mWorksheetFeed entries] objectAtIndex:row];
440    return [[worksheetEntry title] stringValue];
441    
442  } else {
443    
444    // entry table; get a string for the cell or the list item
445    GDataEntryBase *entry = [[mEntryFeed entries] objectAtIndex:row];
446    NSString *displayStr;
447    
448    if ([entry isKindOfClass:[GDataEntrySpreadsheetCell class]]) {
449      
450      // format cell entry data
451      GDataSpreadsheetCell *cell = [(GDataEntrySpreadsheetCell *)entry cell];
452      
453      NSString *resultStr = [cell resultString]; // like "3.1415926"
454      NSString *inputStr = [cell inputString]; // like "=pi()"
455      NSString *title = [[entry title] stringValue]; // like "A3"
456
457      // show the input string (like =pi()) only if it differs
458      // from the result string
459      if (!inputStr || (resultStr && [inputStr isEqual:resultStr])) {
460       inputStr = @""; 
461      }
462      
463      displayStr = [NSString stringWithFormat:@"%@: %@  %@",
464        title, inputStr, (resultStr ? resultStr : @"")];
465      
466    } else {
467      
468      // format list entry data
469      //
470      // a list entry we will show as a sequence of (name,value) items from
471      // the entry's custom elements
472      GDataEntrySpreadsheetList *listEntry = (GDataEntrySpreadsheetList *)entry;
473      NSDictionary *customElements = [listEntry customElementDictionary];
474
475      NSMutableArray *array = [NSMutableArray array];
476      NSEnumerator *enumerator = [customElements objectEnumerator];
477      GDataSpreadsheetCustomElement *element;
478      
479      while ((element = [enumerator nextObject]) != nil) {
480        
481        NSString *elemStr = [NSString stringWithFormat:@"(%@, %@)",
482          [element name], [element stringValue]];
483        [array addObject:elemStr];
484      }
485      displayStr = [array componentsJoinedByString:@", "];
486    }
487    return displayStr;
488  }
489}
490
491#pragma mark Setters and Getters
492
493- (GDataFeedSpreadsheet *)spreadsheetFeed {
494  return mSpreadsheetFeed; 
495}
496
497- (void)setSpreadsheetFeed:(GDataFeedSpreadsheet *)feed {
498  [mSpreadsheetFeed autorelease];
499  mSpreadsheetFeed = [feed retain];
500}
501
502- (NSError *)spreadsheetFetchError {
503  return mSpreadsheetFetchError; 
504}
505
506- (void)setSpreadsheetFetchError:(NSError *)error {
507  [mSpreadsheetFetchError release];
508  mSpreadsheetFetchError = [error retain];
509}
510
511
512- (GDataFeedWorksheet *)worksheetFeed {
513  return mWorksheetFeed; 
514}
515
516- (void)setWorksheetFeed:(GDataFeedWorksheet *)feed {
517  [mWorksheetFeed autorelease];
518  mWorksheetFeed = [feed retain];
519}
520
521- (NSError *)worksheetFetchError {
522  return mWorksheetFetchError; 
523}
524
525- (void)setWorksheetFetchError:(NSError *)error {
526  [mWorksheetFetchError release];
527  mWorksheetFetchError = [error retain];
528}
529
530- (GDataFeedBase *)entryFeed {
531  return mEntryFeed; 
532}
533
534- (void)setEntryFeed:(GDataFeedBase *)feed {
535  [mEntryFeed autorelease];
536  mEntryFeed = [feed retain];
537}
538
539- (NSError *)entryFetchError {
540  return mEntryFetchError; 
541}
542
543- (void)setEntryFetchError:(NSError *)error {
544  [mEntryFetchError release];
545  mEntryFetchError = [error retain];
546}
547
548@end