My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
/* Copyright (c) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//
// DocsSampleWindowController.m
//

#import "DocsSampleWindowController.h"

#import "GData/GTMOAuth2WindowController.h"

enum {
// upload pop-up menu items
kUploadAsGoogleDoc = 0,
kUploadOriginal = 1,
kUploadOCR = 2,
kUploadDE = 3,
kUploadJA = 4,
kUploadEN = 5
};

@interface DocsSampleWindowController (PrivateMethods)
- (void)updateUI;
- (void)updateChangeFolderPopup;
- (void)updateSelectedDocumentThumbnailImage;
- (void)imageFetcher:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *)error;

- (void)fetchDocList;
- (void)fetchRevisionsForSelectedDoc;

- (void)uploadFileAtPath:(NSString *)path;
- (void)showDownloadPanelForEntry:(GDataEntryBase *)entry suggestedTitle:(NSString *)title;
- (void)saveDocumentEntry:(GDataEntryBase *)docEntry toPath:(NSString *)path;
- (void)saveDocEntry:(GDataEntryBase *)entry toPath:(NSString *)savePath exportFormat:(NSString *)exportFormat authService:(GDataServiceGoogle *)service;

- (GDataServiceGoogleDocs *)docsService;
- (GDataEntryDocBase *)selectedDoc;
- (GDataEntryDocRevision *)selectedRevision;

- (GDataFeedDocList *)docListFeed;
- (void)setDocListFeed:(GDataFeedDocList *)feed;
- (NSError *)docListFetchError;
- (void)setDocListFetchError:(NSError *)error;
- (GDataServiceTicket *)docListFetchTicket;
- (void)setDocListFetchTicket:(GDataServiceTicket *)ticket;

- (GDataFeedDocRevision *)revisionFeed;
- (void)setRevisionFeed:(GDataFeedDocRevision *)feed;
- (NSError *)revisionFetchError;
- (void)setRevisionFetchError:(NSError *)error;
- (GDataServiceTicket *)revisionFetchTicket;
- (void)setRevisionFetchTicket:(GDataServiceTicket *)ticket;

- (GDataEntryDocListMetadata *)metadataEntry;
- (void)setMetadataEntry:(GDataEntryDocListMetadata *)entry;

- (GDataServiceTicket *)uploadTicket;
- (void)setUploadTicket:(GDataServiceTicket *)ticket;

- (void)displayAlert:(NSString *)title format:(NSString *)format, ...;
@end

@implementation DocsSampleWindowController

static NSString *const kKeychainItemName = @"DocsSample: Google Docs";

static DocsSampleWindowController* gDocsSampleWindowController = nil;

+ (DocsSampleWindowController *)sharedDocsSampleWindowController {

if (!gDocsSampleWindowController) {
gDocsSampleWindowController = [[DocsSampleWindowController alloc] init];
}
return gDocsSampleWindowController;
}


- (id)init {
return [self initWithWindowNibName:@"DocsSampleWindow"];
}

- (void)awakeFromNib {
// Load the OAuth token from the keychain, if it was previously saved
NSString *clientID = [mClientIDField stringValue];
NSString *clientSecret = [mClientSecretField stringValue];

GTMOAuth2Authentication *auth;
auth = [GTMOAuth2WindowController authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
[[self docsService] setAuthorizer:auth];

// Set the result text field to have a distinctive color and mono-spaced font
// to aid in understanding of each operation.
[mDocListResultTextField setTextColor:[NSColor darkGrayColor]];

NSFont *resultTextFont = [NSFont fontWithName:@"Monaco" size:9];
[mDocListResultTextField setFont:resultTextFont];

[self updateUI];
}

- (void)dealloc {
[mDocListFeed release];
[mDocListFetchTicket release];
[mDocListFetchError release];

[mRevisionFeed release];
[mRevisionFetchTicket release];
[mRevisionFetchError release];

[mMetadataEntry release];

[mUploadTicket cancelTicket];
[mUploadTicket release];

[super dealloc];
}

#pragma mark -

- (NSString *)signedInUsername {
// Get the email address of the signed-in user
GTMOAuth2Authentication *auth = [[self docsService] authorizer];
BOOL isSignedIn = auth.canAuthorize;
if (isSignedIn) {
return auth.userEmail;
} else {
return nil;
}
}

- (BOOL)isSignedIn {
NSString *name = [self signedInUsername];
return (name != nil);
}

- (void)runSigninThenInvokeSelector:(SEL)signInDoneSel {
// Applications should have client ID and client secret strings
// hardcoded into the source, but the sample application asks the
// developer for the strings
NSString *clientID = [mClientIDField stringValue];
NSString *clientSecret = [mClientSecretField stringValue];

if ([clientID length] == 0 || [clientSecret length] == 0) {
// Remind the developer that client ID and client secret are needed
[mClientIDButton performSelector:@selector(performClick:)
withObject:self
afterDelay:0.5];
return;
}

// Show the OAuth 2 sign-in controller
NSString *scope = [GTMOAuth2Authentication scopeWithStrings:
[GDataServiceGoogleDocs authorizationScope],
[GDataServiceGoogleSpreadsheet authorizationScope],
nil];

NSBundle *frameworkBundle = [NSBundle bundleForClass:[GTMOAuth2WindowController class]];
GTMOAuth2WindowController *windowController;
windowController = [GTMOAuth2WindowController controllerWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:kKeychainItemName
resourceBundle:frameworkBundle];

[windowController setUserData:NSStringFromSelector(signInDoneSel)];
[windowController signInSheetModalForWindow:[self window]
completionHandler:^(GTMOAuth2Authentication *auth, NSError *error) {
// callback
if (error == nil) {
[[self docsService] setAuthorizer:auth];

NSString *selStr = [windowController userData];
if (selStr) {
[self performSelector:NSSelectorFromString(selStr)];
}
} else {
[self setDocListFetchError:error];
[self updateUI];
}
}];
}

#pragma mark -

- (void)updateUI {
BOOL isSignedIn = [self isSignedIn];
NSString *username = [self signedInUsername];
[mSignedInButton setTitle:(isSignedIn ? @"Sign Out" : @"Sign In")];
[mSignedInField setStringValue:(isSignedIn ? username : @"No")];

// docList list display
[mDocListTable reloadData];

GDataEntryDocBase *selectedDoc = [self selectedDoc];

// spin indicator when retrieving feed
BOOL isFetchingDocList = (mDocListFetchTicket != nil);
if (isFetchingDocList) {
[mDocListProgressIndicator startAnimation:self];
} else {
[mDocListProgressIndicator stopAnimation:self];
}
[mDocListCancelButton setEnabled:isFetchingDocList];

// show the doclist feed fetch result error or the selected entry
NSString *docResultStr = @"";
if (mDocListFetchError) {
docResultStr = [mDocListFetchError description];
} else {
if (selectedDoc) {
docResultStr = [selectedDoc description];
}
}
[mDocListResultTextField setString:docResultStr];

[self updateSelectedDocumentThumbnailImage];

// revision list display
[mRevisionsTable reloadData];

GDataEntryDocRevision *selectedRevision = [self selectedRevision];

// spin indicator when retrieving feed
BOOL isFetchingRevisions = (mRevisionFetchTicket != nil);
if (isFetchingRevisions) {
[mRevisionsProgressIndicator startAnimation:self];
} else {
[mRevisionsProgressIndicator stopAnimation:self];
}
[mRevisionsCancelButton setEnabled:isFetchingRevisions];

// show the revision feed fetch result error or the selected entry
NSString *revisionsResultStr = @"";
if (mRevisionFetchError) {
revisionsResultStr = [mRevisionFetchError description];
} else {
if (selectedRevision) {
revisionsResultStr = [selectedRevision description];
}
}
[mRevisionsResultTextField setString:revisionsResultStr];

BOOL isSelectedDocAStandardGDocsType =
[selectedDoc isKindOfClass:[GDataEntryStandardDoc class]]
|| [selectedDoc isKindOfClass:[GDataEntrySpreadsheetDoc class]]
|| [selectedDoc isKindOfClass:[GDataEntryPresentationDoc class]];

// enable the button for viewing the selected doc in a browser
BOOL doesDocHaveHTMLLink = ([selectedDoc HTMLLink] != nil);
[mViewSelectedDocButton setEnabled:doesDocHaveHTMLLink];

BOOL doesRevisionHaveExportURL = ([[[selectedRevision content] sourceURI] length] > 0);
[mDownloadSelectedRevisionButton setEnabled:doesRevisionHaveExportURL];

BOOL doesDocHaveExportURL = ([[[selectedDoc content] sourceURI] length] > 0);
[mDownloadSelectedDocButton setEnabled:doesDocHaveExportURL];

BOOL doesDocHaveEditLink = ([selectedDoc editLink] != nil);
[mDeleteSelectedDocButton setEnabled:doesDocHaveEditLink];

[mDuplicateSelectedDocButton setEnabled:isSelectedDocAStandardGDocsType];

// enable the "Show Changes" button
BOOL hasFeed = (mDocListFeed != nil);
[mShowChangesButton setEnabled:hasFeed];

// enable the publishing checkboxes when a publishable revision is selected
BOOL isRevisionSelected = (selectedRevision != nil);
BOOL isRevisionPublishable = isRevisionSelected
&& isSelectedDocAStandardGDocsType;

[mPublishCheckbox setEnabled:isRevisionPublishable];
[mAutoRepublishCheckbox setEnabled:isRevisionPublishable];
[mPublishOutsideDomainCheckbox setEnabled:isRevisionPublishable];

// enable the "Update Publishing" button when the selected revision is
// publishable and the checkbox settings differ from the current publishing
// setting for the selected revision
BOOL isPublished = [[selectedRevision publish] boolValue];
BOOL isPublishedChecked = ([mPublishCheckbox state] == NSOnState);

BOOL isAutoRepublished = [[selectedRevision publishAuto] boolValue];
BOOL isAutoRepublishedChecked = ([mAutoRepublishCheckbox state] == NSOnState);

BOOL isExternalPublished = [[selectedRevision publishOutsideDomain] boolValue];
BOOL isExternalPublishedChecked = ([mPublishOutsideDomainCheckbox state] == NSOnState);

BOOL canUpdatePublishing = isRevisionPublishable
&& ((isPublished != isPublishedChecked)
|| (isAutoRepublished != isAutoRepublishedChecked)
|| (isExternalPublished != isExternalPublishedChecked));

[mUpdatePublishingButton setEnabled:canUpdatePublishing];

// enable uploading buttons
BOOL isUploading = (mUploadTicket != nil);
BOOL canPostToFeed = ([mDocListFeed postLink] != nil);

[mUploadFileButton setEnabled:(canPostToFeed && !isUploading)];
[mStopUploadButton setEnabled:isUploading];
[mPauseUploadButton setEnabled:isUploading];
[mCreateFolderButton setEnabled:canPostToFeed];

BOOL isUploadPaused = [mUploadTicket isUploadPaused];
NSString *pauseTitle = (isUploadPaused ? @"Resume" : @"Pause");
[mPauseUploadButton setTitle:pauseTitle];

// enable the "Upload Original Document" menu item only if the user metadata
// indicates support for generic file uploads
GDataDocFeature *feature = [mMetadataEntry featureForName:kGDataDocsFeatureNameUploadAny];
BOOL canUploadGenericDocs = (feature != nil);

NSMenuItem *genericMenuItem = [[mUploadPopup menu] itemWithTag:kUploadOriginal];
[genericMenuItem setEnabled:canUploadGenericDocs];

// fill in the add-to-folder pop-up for the selected doc
[self updateChangeFolderPopup];

// show the title of the file currently uploading
NSString *uploadingStr = @"";
NSString *uploadingTitle = [[(GDataEntryBase *)
[mDocListFetchTicket postedObject] title] stringValue];

if (uploadingTitle) {
uploadingStr = [NSString stringWithFormat:@"Uploading: %@", uploadingTitle];
}
[mUploadingTextField setStringValue:uploadingStr];

// Show or hide the text indicating that the client ID or client secret are
// needed
BOOL hasClientIDStrings = [[mClientIDField stringValue] length] > 0
&& [[mClientSecretField stringValue] length] > 0;
[mClientIDRequiredTextField setHidden:hasClientIDStrings];
}

- (void)updateChangeFolderPopup {

// replace all menu items in the button with the folder titles and pointers
// of the feed's folder entries, but preserve the pop-up's "Change Folder"
// title as the first item

NSString *title = [mFolderMembershipPopup title];

NSMenu *addMenu = [[[NSMenu alloc] initWithTitle:title] autorelease];
[addMenu setAutoenablesItems:NO];
[addMenu addItemWithTitle:title action:nil keyEquivalent:@""];
[mFolderMembershipPopup setMenu:addMenu];

// get all folder entries
NSArray *folderEntries = [mDocListFeed entriesWithCategoryKind:kGDataCategoryFolderDoc];

// get hrefs of folders that already contain the selected doc
GDataEntryDocBase *doc = [self selectedDoc];
NSArray *parentLinks = [doc parentLinks];
NSArray *parentHrefs = [parentLinks valueForKey:@"href"];

// disable the pop-up if a folder entry is selected
BOOL isMovableDocSelected = (doc != nil)
&& ![doc isKindOfClass:[GDataEntryFolderDoc class]];
[mFolderMembershipPopup setEnabled:isMovableDocSelected];

if (isMovableDocSelected) {
// step through the folders in this feed, add them to the
// pop-up, and add a checkmark to the names of folders that
// contain the selected document
NSEnumerator *folderEnum = [folderEntries objectEnumerator];
GDataEntryFolderDoc *folderEntry;
while ((folderEntry = [folderEnum nextObject]) != nil) {

NSString *title = [[folderEntry title] stringValue];
NSMenuItem *item = [addMenu addItemWithTitle:title
action:@selector(changeFolderSelected:)
keyEquivalent:@""];
[item setTarget:self];
[item setRepresentedObject:folderEntry];

NSString *folderHref = [[folderEntry selfLink] href];

BOOL shouldCheckItem = (folderHref != nil)
&& [parentHrefs containsObject:folderHref];
[item setState:shouldCheckItem];
}
}
}

- (void)updateSelectedDocumentThumbnailImage {
static NSString* priorImageURLStr = nil;

GDataEntryDocBase *doc = [self selectedDoc];
GDataLink *thumbnailLink = [doc thumbnailLink];
NSString *newImageURLStr = [thumbnailLink href];

if (!AreEqualOrBothNil(newImageURLStr, priorImageURLStr)) {
// the image has changed
priorImageURLStr = newImageURLStr;

[mDocListImageView setImage:nil];

if ([newImageURLStr length] > 0) {
// We need an authorized fetcher to download the document thumbnail, as
// the fetch requires authentication.
//
// We could attach an authorizer to the fetcher, but it's simpler to
// use the GData service's fetcherService to make the new fetcher, as
// that will already have the authorizer attached.
GTMHTTPFetcherService *fetcherService = [[self docsService] fetcherService];
GTMHTTPFetcher *fetcher = [fetcherService fetcherWithURLString:newImageURLStr];
[fetcher setCommentWithFormat:@"thumbnail for \"%@\"", [[doc title] stringValue]];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
// callback
if (error == nil) {
NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
[mDocListImageView setImage:image];
} else {
NSLog(@"Error %@ loading image %@",
error, [[fetcher mutableRequest] URL]);
}
}];
}
}
}

- (void)displayAlert:(NSString *)title format:(NSString *)format, ... {
NSString *result = format;
if (format) {
va_list argList;
va_start(argList, format);
result = [[[NSString alloc] initWithFormat:format
arguments:argList] autorelease];
va_end(argList);
}
NSBeginAlertSheet(title, nil, nil, nil, [self window], nil, nil,
nil, nil, result);
}

#pragma mark IBActions

- (IBAction)signInClicked:(id)sender {
if (![self isSignedIn]) {
// Sign in
[self runSigninThenInvokeSelector:@selector(updateUI)];
} else {
// Sign out
GDataServiceGoogleDocs *service = [self docsService];

[GTMOAuth2WindowController removeAuthFromKeychainForName:kKeychainItemName];
[service setAuthorizer:nil];
[self updateUI];
}
}

- (IBAction)getDocListClicked:(id)sender {
if (![self isSignedIn]) {
[self runSigninThenInvokeSelector:@selector(fetchDocList)];
} else {
[self fetchDocList];
}
}

- (IBAction)cancelDocListFetchClicked:(id)sender {
[mDocListFetchTicket cancelTicket];
[self setDocListFetchTicket:nil];
[self updateUI];
}

- (IBAction)cancelRevisionsFetchClicked:(id)sender {
[mRevisionFetchTicket cancelTicket];
[self setRevisionFetchTicket:nil];
[self updateUI];
}

- (IBAction)viewSelectedDocClicked:(id)sender {

NSURL *docURL = [[[self selectedDoc] HTMLLink] URL];

if (docURL) {
[[NSWorkspace sharedWorkspace] openURL:docURL];
} else {
NSBeep();
}
}

#pragma mark -

- (IBAction)downloadSelectedDocClicked:(id)sender {

GDataEntryDocBase *docEntry = [self selectedDoc];

NSString *saveTitle = [[docEntry title] stringValue];

[self showDownloadPanelForEntry:docEntry
suggestedTitle:saveTitle];
}

- (IBAction)downloadSelectedRevisionClicked:(id)sender {

GDataEntryDocRevision *revisionEntry = [self selectedRevision];

GDataEntryDocBase *docEntry = [self selectedDoc];

NSString *docName = [[docEntry title] stringValue];
NSString *revisionName = [[revisionEntry title] stringValue];
NSString *saveTitle = [NSString stringWithFormat:@"%@ (%@)",
docName, revisionName];

// the revision entry doesn't tell us the kind of document being saved, so
// we'll explicitly put it into a property of the entry
Class documentClass = [docEntry class];
[revisionEntry setProperty:documentClass
forKey:@"document class"];

[self showDownloadPanelForEntry:revisionEntry
suggestedTitle:saveTitle];
}

- (void)showDownloadPanelForEntry:(GDataEntryBase *)entry
suggestedTitle:(NSString *)title {

NSString *sourceURI = [[entry content] sourceURI];
if (sourceURI) {
// We will download drawings as pdf and other files as text
BOOL isDrawing = [entry isKindOfClass:[GDataEntryDrawingDoc class]];
NSString *filename = title;
NSString *fileExtension = (isDrawing ? @"pdf" : @"txt");
if (![[title pathExtension] isEqual:fileExtension]) {
// The title string needs the file extension to be a file name
filename = [title stringByAppendingPathExtension:fileExtension];
}

NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setNameFieldStringValue:filename];
[savePanel beginSheetModalForWindow:[self window]
completionHandler:^(NSInteger result) {
// callback
if (result == NSOKButton) {
// user clicked OK
NSString *savePath = [[savePanel URL] path];
[self saveDocumentEntry:entry
toPath:savePath];
}
}];
} else {
NSBeep();
}
}

// formerly saveSelectedDocumentToPath:
- (void)saveDocumentEntry:(GDataEntryBase *)docEntry
toPath:(NSString *)savePath {
// downloading docs, per
// http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#DownloadingDocs

// when downloading a revision entry, we've added a property above indicating
// the class of document for which this is a revision
Class classProperty = [docEntry propertyForKey:@"document class"];
if (!classProperty) {
classProperty = [docEntry class];
}

// since the user has already fetched the doc list, the service object
// has the proper authentication token.
GDataServiceGoogleDocs *docsService = [self docsService];

BOOL isDrawing = [classProperty isEqual:[GDataEntryDrawingDoc class]];
NSString *exportFormat = (isDrawing ? @"pdf" : @"txt");
[self saveDocEntry:docEntry
toPath:savePath
exportFormat:exportFormat
authService:docsService];
}

- (void)saveDocEntry:(GDataEntryBase *)entry
toPath:(NSString *)savePath
exportFormat:(NSString *)exportFormat
authService:(GDataServiceGoogle *)service {

// the content src attribute is used for downloading
NSURL *exportURL = [[entry content] sourceURL];
if (exportURL != nil) {
// we'll use GDataQuery as a convenient way to append the exportFormat
// parameter of the docs export API to the content src URL
GDataQuery *query = [GDataQuery queryWithFeedURL:exportURL];
[query addCustomParameterWithName:@"exportFormat"
value:exportFormat];
NSURL *downloadURL = [query URL];

// Read the document's contents asynchronously from the network

// requestForURL:ETag:httpMethod: sets the user agent header of the
// request and, when using ClientLogin, adds the authorization header
NSURLRequest *request = [service requestForURL:downloadURL
ETag:nil
httpMethod:nil];

GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[fetcher setAuthorizer:[service authorizer]];
[fetcher setDownloadPath:savePath];
[fetcher setCommentWithFormat:@"downloading \"%@\"", [[entry title] stringValue]];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
// callback
if (error == nil) {
// Successfully saved the document
//
// Since a downloadPath property was specified, the data argument is
// nil, and the file data has been written to disk.
} else {
NSLog(@"Error saving document: %@", error);
NSBeep();
}
}];
}
}

/* When signing in with ClientLogin, we need to create a SpreadsheetService
instance to do an authenticated download of spreadsheet documents.

Since this sample signs in with OAuth 2, which allows multiple scopes,
we do not need to use a SpreadsheetService, but here is what it looks
like for ClientLogin.

- (void)saveSpreadsheet:(GDataEntrySpreadsheetDoc *)docEntry
toPath:(NSString *)savePath {
// to download a spreadsheet document, we need a spreadsheet service object,
// and we first need to fetch a feed or entry with the service object so that
// it has a valid auth token
GDataServiceGoogleSpreadsheet *spreadsheetService;
spreadsheetService = [[[GDataServiceGoogleSpreadsheet alloc] init] autorelease];

GDataServiceGoogleDocs *docsService = [self docsService];
[spreadsheetService setUserAgent:[docsService userAgent]];
[spreadsheetService setUserCredentialsWithUsername:[docsService username]
password:[docsService password]];
GDataServiceTicket *ticket;
ticket = [spreadsheetService authenticateWithDelegate:self
didAuthenticateSelector:@selector(spreadsheetTicket:authenticatedWithError:)];

// we'll hang on to the spreadsheet service object with a ticket property
// since we need it to create an authorized NSURLRequest
[ticket setProperty:docEntry forKey:@"docEntry"];
[ticket setProperty:savePath forKey:@"savePath"];
}

- (void)spreadsheetTicket:(GDataServiceTicket *)ticket
authenticatedWithError:(NSError *)error {
if (error == nil) {
GDataEntrySpreadsheetDoc *docEntry = [ticket propertyForKey:@"docEntry"];
NSString *savePath = [ticket propertyForKey:@"savePath"];

[self saveDocEntry:docEntry
toPath:savePath
exportFormat:@"tsv"
authService:[ticket service]];
} else {
// failed to authenticate; give up
NSLog(@"Spreadsheet authentication error: %@", error);
return;
}
}
*/

#pragma mark -

- (IBAction)uploadFileClicked:(id)sender {
// ask the user to choose a file
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setPrompt:@"Upload"];
[openPanel beginSheetModalForWindow:[self window]
completionHandler:^(NSInteger result) {
// callback
if (result == NSOKButton) {
// user chose a file and clicked OK
//
// start uploading (deferred to the main thread since
// we currently have a sheet displayed)
NSString *path = [[openPanel URL] path];
[self performSelectorOnMainThread:@selector(uploadFileAtPath:)
withObject:path
waitUntilDone:NO];
}
}];
}

- (IBAction)pauseUploadClicked:(id)sender {
if ([mUploadTicket isUploadPaused]) {
[mUploadTicket resumeUpload];
} else {
[mUploadTicket pauseUpload];
}
[self updateUI];
}

- (IBAction)stopUploadClicked:(id)sender {
[mUploadTicket cancelTicket];
[self setUploadTicket:nil];

[mUploadProgressIndicator setDoubleValue:0.0];
[self updateUI];
}

#pragma mark -

- (IBAction)publishCheckboxClicked:(id)sender {
// enable or disable the Update Publishing button
[self updateUI];
}

- (IBAction)updatePublishingClicked:(id)sender {
GDataServiceGoogleDocs *service = [self docsService];

GDataEntryDocRevision *revisionEntry = [self selectedRevision];

// update the revision elements to match the checkboxes
//
// we'll modify a copy of the selected entry so we don't leave an inaccurate
// entry in the feed if our fetch fails
GDataEntryDocRevision *revisionCopy = [[revisionEntry copy] autorelease];

BOOL shouldPublish = ([mPublishCheckbox state] == NSOnState);
[revisionCopy setPublish:[NSNumber numberWithBool:shouldPublish]];

BOOL shouldAutoRepublish = ([mAutoRepublishCheckbox state] == NSOnState);
[revisionCopy setPublishAuto:[NSNumber numberWithBool:shouldAutoRepublish]];

BOOL shouldPublishExternally = ([mPublishOutsideDomainCheckbox state] == NSOnState);
[revisionCopy setPublishOutsideDomain:[NSNumber numberWithBool:shouldPublishExternally]];

[service fetchEntryByUpdatingEntry:revisionCopy
completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {
// callback
if (error == nil) {
[self displayAlert:@"Updated"
format:@"Updated publish status for \"%@\"",
[[entry title] stringValue]];

// re-fetch the document list
[self fetchRevisionsForSelectedDoc];
} else {
[self displayAlert:@"Updated failed"
format:@"Failed to update publish status: %@",
error];
}
}];
}

#pragma mark -

- (IBAction)createFolderClicked:(id)sender {
GDataServiceGoogleDocs *service = [self docsService];

GDataEntryFolderDoc *docEntry = [GDataEntryFolderDoc documentEntry];

NSString *title = [NSString stringWithFormat:@"New Folder %@", [NSDate date]];
[docEntry setTitleWithString:title];

NSURL *postURL = [[mDocListFeed postLink] URL];

[service fetchEntryByInsertingEntry:docEntry
forFeedURL:postURL
completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {
// callback
if (error == nil) {
[self displayAlert:@"Created folder"
format:@"Created folder \"%@\"",
[[entry title] stringValue]];

// re-fetch the document list
[self fetchDocList];
[self updateUI];
} else {
[self displayAlert:@"Create failed"
format:@"Folder create failed: %@", error];
}
}];
}

#pragma mark -

static long long gLargestPriorChangestamp = 0;

- (IBAction)showChangesClicked:(id)sender {
NSURL *changesFeedURL = [GDataServiceGoogleDocs changesFeedURLForUserID:kGDataServiceDefaultUser];
GDataServiceGoogleDocs *service = [self docsService];

if (gLargestPriorChangestamp == 0) {
// First click
//
// We have not previously fetched the changes feed, so request it without
// entries to determine a benchmark changestamp
GDataQueryDocs *query = [GDataQueryDocs documentQueryWithFeedURL:changesFeedURL];

// The server currently ignores zero as a max-results value (b/5027926),
// so we'll request one entry and ignore it
[query setMaxResults:1];

GDataServiceTicket *ticket;
ticket = [service fetchFeedWithQuery:query
completionHandler:^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
// callback
if (error == nil) {
GDataFeedDocChange *changeFeed = (GDataFeedDocChange *)feed;
NSNumber *num = [changeFeed largestChangestamp];
[self displayAlert:@"Initial changestamp obtained"
format:@"Value: %@", num];
gLargestPriorChangestamp = [num longLongValue];
} else {
[self displayAlert:@"Fetch failed"
format:@"Fetch of changes failed: %@",
error];
}
}];
// We don't want additional pages of this feed, since we only care about
// the changestamp benchmark
[ticket setShouldFollowNextLinks:NO];
} else {
// Second and later clicks
//
// We have previously fetched the changes feed, so request all changes
// since that benchmark changestamp
GDataQueryDocs *query = [GDataQueryDocs documentQueryWithFeedURL:changesFeedURL];
[query setStartIndex:(1 + gLargestPriorChangestamp)];

// We'll reduce the number pages fetches needed to obtain the entire feed
// by requesting a large page size. A large page size and automatic next
// link following are not really practical on mobile devices, though, as
// the entries of the changes feed are big.
[query setMaxResults:100];

[service fetchFeedWithQuery:query
completionHandler:^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
// callback
if (error == nil) {
// We obtained a feed of changes
//
// Report the titles of added and updated docs, and the
// entry identifier of removed docs
GDataFeedDocChange *changeFeed = (GDataFeedDocChange *)feed;

NSMutableString *output = [NSMutableString stringWithFormat:
@"Changed entries (%lu):",
(unsigned long) [[feed entries] count]];
for (GDataEntryDocBase *entry in changeFeed) {
if ([entry isRemoved]) {
// Removed
[output appendFormat:@"\nRemoved (%@)", [entry identifier]];
} else {
// Added or updated
[output appendFormat:@"\n%@", [[entry title] stringValue]];
}
}
[self displayAlert:@"Changed entries"
format:@"%@", output];

// Update the benchmark value
gLargestPriorChangestamp = [[changeFeed largestChangestamp] longLongValue];
} else {
[self displayAlert:@"Fetch failed"
format:@"Fetch of changes since %lld failed: %@",
gLargestPriorChangestamp, error];
}
}];
}
}

#pragma mark -

- (IBAction)deleteSelectedDocClicked:(id)sender {

GDataEntryDocBase *doc = [self selectedDoc];
if (doc) {
// make the user confirm that the selected doc should be deleted
NSBeginAlertSheet(@"Delete Document", @"Delete", @"Cancel", nil,
[self window], self,
@selector(deleteDocSheetDidEnd:returnCode:contextInfo:),
nil, nil, @"Delete the document \"%@\"?",
[[doc title] stringValue]);
}
}

// delete dialog callback
- (void)deleteDocSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {

if (returnCode == NSAlertDefaultReturn) {

// delete the document entry
GDataEntryDocBase *entry = [self selectedDoc];

if (entry) {
GDataServiceGoogleDocs *service = [self docsService];
[service deleteEntry:entry
delegate:self
didFinishSelector:@selector(deleteDocEntryTicket:deletedEntry:error:)];
}
}
}

// entry delete callback
- (void)deleteDocEntryTicket:(GDataServiceTicket *)ticket
deletedEntry:(GDataEntryDocBase *)object
error:(NSError *)error {
if (error == nil) {
// note: object is nil in the delete callback
[self displayAlert:@"Deleted Doc"
format:@"Document deleted"];

// re-fetch the document list
[self fetchDocList];
[self updateUI];
} else {
[self displayAlert:@"Delete failed"
format:@"Document delete failed: %@", error];
}
}

#pragma mark -

- (IBAction)duplicateSelectedDocClicked:(id)sender {

GDataEntryDocBase *selectedDoc = [self selectedDoc];
if (selectedDoc) {
// make a new entry of the same class as the selected document entry,
// with just the title set and an identifier equal to the selected
// doc's resource ID
GDataEntryDocBase *newEntry = [[selectedDoc class] documentEntry];

[newEntry setIdentifier:[selectedDoc resourceID]];

NSString *oldTitle = [[selectedDoc title] stringValue];
NSString *newTitle = [oldTitle stringByAppendingString:@" copy"];
[newEntry setTitleWithString:newTitle];

GDataServiceGoogleDocs *service = [self docsService];
NSURL *postURL = [[mDocListFeed postLink] URL];

[service fetchEntryByInsertingEntry:newEntry
forFeedURL:postURL
completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {
// callback
if (error == nil) {
[self displayAlert:@"Copied Doc"
format:@"Document duplicate \"%@\" created", [[newEntry title] stringValue]];

// re-fetch the document list
[self fetchDocList];
[self updateUI];
} else {
[self displayAlert:@"Copy failed"
format:@"Document duplicate failed: %@", error];
}
}];
}
}

#pragma mark -

- (IBAction)changeFolderSelected:(id)sender {

// the selected menu item represents a folder; fetch the folder's feed
//
// with the folder's feed, we can insert or remove the selected document
// entry in the folder's feed

GDataEntryFolderDoc *folderEntry = [sender representedObject];
NSURL *folderFeedURL = [[folderEntry content] sourceURL];
if (folderFeedURL != nil) {

GDataServiceGoogleDocs *service = [self docsService];

GDataServiceTicket *ticket;
ticket = [service fetchFeedWithURL:folderFeedURL
delegate:self
didFinishSelector:@selector(fetchFolderTicket:finishedWithFeed:error:)];

// save the selected doc in the ticket's userData
GDataEntryDocBase *doc = [self selectedDoc];
[ticket setUserData:doc];
}
}

// folder feed fetch callback
- (void)fetchFolderTicket:(GDataServiceTicket *)ticket
finishedWithFeed:(GDataFeedDocList *)feed
error:(NSError *)error {

if (error == nil) {
GDataEntryDocBase *docEntry = [ticket userData];

GDataServiceGoogleDocs *service = [self docsService];
GDataServiceTicket *ticket2;

// if the entry is not in the folder's feed, insert it; otherwise, delete
// it from the folder's feed
GDataEntryDocBase *foundEntry = [feed entryForIdentifier:[docEntry identifier]];
if (foundEntry == nil) {
// the doc isn't currently in this folder's feed
//
// post the doc to the folder's feed
NSURL *postURL = [[feed postLink] URL];

ticket2 = [service fetchEntryByInsertingEntry:docEntry
forFeedURL:postURL
completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {
// callback
if (error == nil) {
[self displayAlert:@"Added"
format:@"Added document \"%@\" to feed \"%@\"",
[[entry title] stringValue],
[[feed title] stringValue]];

// re-fetch the document list
[self fetchDocList];
[self updateUI];
} else {
[self displayAlert:@"Insert failed"
format:@"Insert to folder feed failed: %@", error];
}
}];
} else {
// the doc is alrady in the folder's feed, so remove it
ticket2 = [service deleteEntry:foundEntry
completionHandler:^(GDataServiceTicket *ticket, id nilObject, NSError *error) {
// callback
if (error == nil) {
[self displayAlert:@"Removed"
format:@"Removed document from feed \"%@\"", [[feed title] stringValue]];

// re-fetch the document list
[self fetchDocList];
[self updateUI];
} else {
[self displayAlert:@"Fetch failed"
format:@"Remove from folder feed failed: %@",
error];
}
}];
}
} else {
// failed to fetch feed of folders
[self displayAlert:@"Fetch failed"
format:@"Fetch of folder feed failed: %@", error];
}
}

#pragma mark -

- (IBAction)APIConsoleClicked:(id)sender {
NSURL *url = [NSURL URLWithString:@"https://code.google.com/apis/console"];
[[NSWorkspace sharedWorkspace] openURL:url];
}

- (IBAction)loggingCheckboxClicked:(id)sender {
[GTMHTTPFetcher setLoggingEnabled:[sender state]];
}

#pragma mark -

// get an docList service object with the current username/password
//
// A "service" object handles networking tasks. Service objects
// contain user authentication information as well as networking
// state information (such as cookies and the "last modified" date for
// fetched data.)

- (GDataServiceGoogleDocs *)docsService {

static GDataServiceGoogleDocs* service = nil;

if (!service) {
service = [[GDataServiceGoogleDocs alloc] init];

[service setShouldCacheResponseData:YES];
[service setServiceShouldFollowNextLinks:YES];
[service setIsServiceRetryEnabled:YES];
}

return service;
}

// get the doc selected in the list, or nil if none
- (GDataEntryDocBase *)selectedDoc {

int rowIndex = [mDocListTable selectedRow];
if (rowIndex > -1) {
GDataEntryDocBase *doc = [mDocListFeed entryAtIndex:rowIndex];
return doc;
}
return nil;
}

// get the doc revision in the list, or nil if none
- (GDataEntryDocRevision *)selectedRevision {

int rowIndex = [mRevisionsTable selectedRow];
if (rowIndex > -1) {
GDataEntryDocRevision *entry = [mRevisionFeed entryAtIndex:rowIndex];
return entry;
}
return nil;
}

#pragma mark Fetch doc list user metadata

- (void)fetchMetadataEntry {
[self setMetadataEntry:nil];

NSURL *entryURL = [GDataServiceGoogleDocs metadataEntryURLForUserID:kGDataServiceDefaultUser];
GDataServiceGoogleDocs *service = [self docsService];
[service fetchEntryWithURL:entryURL
completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {
// callback
[self setMetadataEntry:(GDataEntryDocListMetadata *)entry];

// enable or disable features
[self updateUI];

if (error != nil) {
NSLog(@"Error fetching user metadata: %@", error);
}
}];
}

#pragma mark Fetch doc list

// begin retrieving the list of the user's docs
- (void)fetchDocList {

[self setDocListFeed:nil];
[self setDocListFetchError:nil];
[self setDocListFetchTicket:nil];

GDataServiceGoogleDocs *service = [self docsService];
GDataServiceTicket *ticket;

// Fetching a feed gives us 25 responses by default. We need to use
// the feed's "next" link to get any more responses. If we want more than 25
// at a time, instead of calling fetchDocsFeedWithURL, we can create a
// GDataQueryDocs object, as shown here.

NSURL *feedURL = [GDataServiceGoogleDocs docsFeedURL];

GDataQueryDocs *query = [GDataQueryDocs documentQueryWithFeedURL:feedURL];
[query setMaxResults:1000];
[query setShouldShowFolders:YES];

ticket = [service fetchFeedWithQuery:query
completionHandler:^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
// callback
[self setDocListFeed:(GDataFeedDocList *)feed];
[self setDocListFetchError:error];
[self setDocListFetchTicket:nil];

[self updateUI];
}];

[self setDocListFetchTicket:ticket];

// update our metadata entry for this user
[self fetchMetadataEntry];

[self updateUI];
}

#pragma mark Fetch revisions or content feed

- (void)fetchRevisionsForSelectedDoc {

[self setRevisionFeed:nil];
[self setRevisionFetchError:nil];
[self setRevisionFetchTicket:nil];

GDataEntryDocBase *selectedDoc = [self selectedDoc];
GDataFeedLink *revisionFeedLink = [selectedDoc revisionFeedLink];
NSURL *revisionFeedURL = [revisionFeedLink URL];
if (revisionFeedURL) {

GDataServiceGoogleDocs *service = [self docsService];
GDataServiceTicket *ticket;
ticket = [service fetchFeedWithURL:revisionFeedURL
completionHandler:^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {
// callback
[self setRevisionFeed:(GDataFeedDocRevision *)feed];
[self setRevisionFetchError:error];
[self setRevisionFetchTicket:nil];

[self updateUI];
}];

[self setRevisionFetchTicket:ticket];

}

[self updateUI];
}

#pragma mark Upload

- (void)getMIMEType:(NSString **)mimeType andEntryClass:(Class *)class forExtension:(NSString *)extension {

// Mac OS X's UTI database doesn't know MIME types for .doc and .xls
// so GDataEntryBase's MIMETypeForFileAtPath method isn't helpful here

struct MapEntry {
NSString *extension;
NSString *mimeType;
NSString *className;
};

static struct MapEntry sMap[] = {
{ @"csv", @"text/csv", @"GDataEntryStandardDoc" },
{ @"doc", @"application/msword", @"GDataEntryStandardDoc" },
{ @"docx", @"application/vnd.openxmlformats-officedocument.wordprocessingml.document", @"GDataEntryStandardDoc" },
{ @"ods", @"application/vnd.oasis.opendocument.spreadsheet", @"GDataEntrySpreadsheetDoc" },
{ @"odt", @"application/vnd.oasis.opendocument.text", @"GDataEntryStandardDoc" },
{ @"pps", @"application/vnd.ms-powerpoint", @"GDataEntryPresentationDoc" },
{ @"ppt", @"application/vnd.ms-powerpoint", @"GDataEntryPresentationDoc" },
{ @"rtf", @"application/rtf", @"GDataEntryStandardDoc" },
{ @"sxw", @"application/vnd.sun.xml.writer", @"GDataEntryStandardDoc" },
{ @"txt", @"text/plain", @"GDataEntryStandardDoc" },
{ @"xls", @"application/vnd.ms-excel", @"GDataEntrySpreadsheetDoc" },
{ @"xlsx", @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @"GDataEntrySpreadsheetDoc" },
{ @"jpg", @"image/jpeg", @"GDataEntryStandardDoc" },
{ @"jpeg", @"image/jpeg", @"GDataEntryStandardDoc" },
{ @"png", @"image/png", @"GDataEntryStandardDoc" },
{ @"bmp", @"image/bmp", @"GDataEntryStandardDoc" },
{ @"gif", @"image/gif", @"GDataEntryStandardDoc" },
{ @"html", @"text/html", @"GDataEntryStandardDoc" },
{ @"htm", @"text/html", @"GDataEntryStandardDoc" },
{ @"tsv", @"text/tab-separated-values", @"GDataEntryStandardDoc" },
{ @"tab", @"text/tab-separated-values", @"GDataEntryStandardDoc" },
{ @"pdf", @"application/pdf", @"GDataEntryPDFDoc" },
{ nil, nil, nil }
};

NSString *lowerExtn = [extension lowercaseString];

for (int idx = 0; sMap[idx].extension != nil; idx++) {
if ([lowerExtn isEqual:sMap[idx].extension]) {
*mimeType = sMap[idx].mimeType;
*class = NSClassFromString(sMap[idx].className);
return;
}
}

*mimeType = nil;
*class = nil;
return;
}

- (void)uploadFileAtPath:(NSString *)path {

NSString *errorMsg = nil;

// make a new entry for the file

NSString *mimeType = nil;
Class entryClass = nil;

NSString *extn = [path pathExtension];
[self getMIMEType:&mimeType andEntryClass:&entryClass forExtension:extn];

if (!mimeType) {
// for other file types, see if we can get the type from the Mac OS
// and use a generic file document entry class
mimeType = [GDataUtilities MIMETypeForFileAtPath:path
defaultMIMEType:nil];
entryClass = [GDataEntryFileDoc class];
}

if (!mimeType) {
errorMsg = [NSString stringWithFormat:@"need MIME type for file %@", path];
}

if (mimeType && entryClass) {

GDataEntryDocBase *newEntry = [entryClass documentEntry];

NSString *title = [[NSFileManager defaultManager] displayNameAtPath:path];
[newEntry setTitleWithString:title];

NSFileHandle *uploadFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
if (!uploadFileHandle) {
errorMsg = [NSString stringWithFormat:@"cannot read file %@", path];
}

if (uploadFileHandle) {
[newEntry setUploadFileHandle:uploadFileHandle];

[newEntry setUploadMIMEType:mimeType];
[newEntry setUploadSlug:[path lastPathComponent]];

NSURL *uploadURL = [GDataServiceGoogleDocs docsUploadURL];

// add the OCR or translation parameters, if the user set the pop-up
// button appropriately
int popupTag = [[mUploadPopup selectedItem] tag];
if (popupTag != 0) {
NSString *targetLanguage = nil;
BOOL shouldConvertToGoogleDoc = YES;
BOOL shouldOCR = NO;

switch (popupTag) {
// upload original file
case kUploadOriginal: shouldConvertToGoogleDoc = NO; break;

// OCR
case kUploadOCR: shouldOCR = YES; break;

// translation
case kUploadDE: targetLanguage = @"de"; break; // german
case kUploadJA: targetLanguage = @"ja"; break; // japanese
case kUploadEN: targetLanguage = @"en"; break; // english

default: break;
}

GDataQueryDocs *query = [GDataQueryDocs queryWithFeedURL:uploadURL];

[query setShouldConvertUpload:shouldConvertToGoogleDoc];
[query setShouldOCRUpload:shouldOCR];

// we'll leave out the sourceLanguage parameter to get
// auto-detection of the file's language
//
// language codes: http://www.loc.gov/standards/iso639-2/php/code_list.php
[query setTargetLanguage:targetLanguage];

uploadURL = [query URL];
}

// make service tickets call back into our upload progress selector
GDataServiceGoogleDocs *service = [self docsService];

// insert the entry into the docList feed
//
// to update (replace) an existing entry by uploading a new file,
// use the fetchEntryByUpdatingEntry:forEntryURL: with the URL from
// the entry's uploadEditLink
GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:newEntry
forFeedURL:uploadURL
delegate:self
didFinishSelector:@selector(uploadFileTicket:finishedWithEntry:error:)];

[ticket setUploadProgressHandler:^(GDataServiceTicketBase *ticket, unsigned long long numberOfBytesRead, unsigned long long dataLength) {
// progress callback
[mUploadProgressIndicator setMinValue:0.0];
[mUploadProgressIndicator setMaxValue:(double)dataLength];
[mUploadProgressIndicator setDoubleValue:(double)numberOfBytesRead];
}];

// we turned automatic retry on when we allocated the service, but we
// could also turn it on just for this ticket

[self setUploadTicket:ticket];
}
}

if (errorMsg) {
// we're currently in the middle of the file selection sheet, so defer our
// error sheet
[self displayAlert:@"Upload Error"
format:@"%@", errorMsg];
}

[self updateUI];
}

// upload finished callback
- (void)uploadFileTicket:(GDataServiceTicket *)ticket
finishedWithEntry:(GDataEntryDocBase *)entry
error:(NSError *)error {

[self setUploadTicket:nil];
[mUploadProgressIndicator setDoubleValue:0.0];

if (error == nil) {
// refetch the current doc list
[self fetchDocList];

// tell the user that the add worked
[self displayAlert:@"Uploaded file"
format:@"File uploaded: %@", [[entry title] stringValue]];
} else {
[self displayAlert:@"Upload failed"
format:@"File upload failed: %@", error];
}
[self updateUI];
}

#pragma mark Client ID Sheet

// Client ID and Client Secret Sheet
//
// Sample apps need this sheet to ask for the client ID and client secret
// strings
//
// Your application will just hardcode the client ID and client secret strings
// into the source rather than ask the user for them.
//
// The string values are obtained from the API Console,
// https://code.google.com/apis/console

- (IBAction)clientIDClicked:(id)sender {
// Show the sheet for developers to enter their client ID and client secret
[NSApp beginSheet:mClientIDSheet
modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(clientIDSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}

- (IBAction)clientIDDoneClicked:(id)sender {
[NSApp endSheet:mClientIDSheet returnCode:NSOKButton];
}

- (void)clientIDSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
[sheet orderOut:self];
[self updateUI];
}

#pragma mark TableView delegate methods

//
// table view delegate methods
//

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
if ([notification object] == mDocListTable) {
// the user selected a document entry, so fetch its revisions
[self fetchRevisionsForSelectedDoc];
} else {
// the user selected a revision entry
//
// update the publishing checkboxes to match the newly-selected revision

GDataEntryDocRevision *selectedRevision = [self selectedRevision];

BOOL isPublished = [[selectedRevision publish] boolValue];
[mPublishCheckbox setState:(isPublished ? NSOnState : NSOffState)];

BOOL isAutoRepublished = [[selectedRevision publishAuto] boolValue];
[mAutoRepublishCheckbox setState:(isAutoRepublished ? NSOnState : NSOffState)];

BOOL isExternalPublished = [[selectedRevision publishOutsideDomain] boolValue];
[mPublishOutsideDomainCheckbox setState:(isExternalPublished ? NSOnState : NSOffState)];

[self updateUI];
}
}

// table view data source methods
- (int)numberOfRowsInTableView:(NSTableView *)tableView {
if (tableView == mDocListTable) {
return [[mDocListFeed entries] count];
}

if (tableView == mRevisionsTable) {
return [[mRevisionFeed entries] count];
}

return 0;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {

if (tableView == mDocListTable) {
// get the docList entry's title, and the kind of document
GDataEntryDocBase *doc = [mDocListFeed entryAtIndex:row];

NSString *docKind = @"unknown";

// the kind category for a doc entry includes a label like "document"
// or "spreadsheet"
NSArray *categories;
categories = [GDataCategory categoriesWithScheme:kGDataCategoryScheme
fromCategories:[doc categories]];
if ([categories count] >= 1) {
docKind = [[categories objectAtIndex:0] label];
}

// mark if the document is starred
if ([doc isStarred]) {
const UniChar kStarChar = 0x2605;
docKind = [NSString stringWithFormat:@"%C, %@", kStarChar, docKind];
}

NSString *displayStr = [NSString stringWithFormat:@"%@ (%@)",
[[doc title] stringValue], docKind];
return displayStr;
}

if (tableView == mRevisionsTable) {
// get the revision entry
GDataEntryDocRevision *revisionEntry;
revisionEntry = [mRevisionFeed entryAtIndex:row];

NSString *displayStr = [NSString stringWithFormat:@"%@ (edited %@)",
[[revisionEntry title] stringValue],
[[revisionEntry editedDate] date]];
return displayStr;
}
return nil;
}

#pragma mark Setters and Getters

- (GDataFeedDocList *)docListFeed {
return mDocListFeed;
}

- (void)setDocListFeed:(GDataFeedDocList *)feed {
[mDocListFeed autorelease];
mDocListFeed = [feed retain];
}

- (NSError *)docListFetchError {
return mDocListFetchError;
}

- (void)setDocListFetchError:(NSError *)error {
[mDocListFetchError release];
mDocListFetchError = [error retain];
}

- (GDataServiceTicket *)docListFetchTicket {
return mDocListFetchTicket;
}

- (void)setDocListFetchTicket:(GDataServiceTicket *)ticket {
[mDocListFetchTicket release];
mDocListFetchTicket = [ticket retain];
}


- (GDataFeedDocRevision *)revisionFeed {
return mRevisionFeed;
}

- (void)setRevisionFeed:(GDataFeedDocRevision *)feed {
[mRevisionFeed autorelease];
mRevisionFeed = [feed retain];
}

- (NSError *)revisionFetchError {
return mRevisionFetchError;
}

- (void)setRevisionFetchError:(NSError *)error {
[mRevisionFetchError release];
mRevisionFetchError = [error retain];
}

- (GDataServiceTicket *)revisionFetchTicket {
return mRevisionFetchTicket;
}

- (void)setRevisionFetchTicket:(GDataServiceTicket *)ticket {
[mRevisionFetchTicket release];
mRevisionFetchTicket = [ticket retain];
}


- (GDataEntryDocListMetadata *)metadataEntry {
return mMetadataEntry;
}

- (void)setMetadataEntry:(GDataEntryDocListMetadata *)entry {
[mMetadataEntry release];
mMetadataEntry = [entry retain];
}


- (GDataServiceTicket *)uploadTicket {
return mUploadTicket;
}

- (void)setUploadTicket:(GDataServiceTicket *)ticket {
[mUploadTicket release];
mUploadTicket = [ticket retain];
}

@end

Change log

r702 by gregrobbins on Sep 29, 2011   Diff
Added service method fetchEntryByUpdatingE
ntry:forEntryURL:completionHandler:
Go to: 
Project members, sign in to write a code review

Older revisions

r694 by gregrobbins on Aug 29, 2011   Diff
In DocsSample, us an authorized
fetcher to retrieve document
thumbnails
r693 by gregrobbins on Aug 26, 2011   Diff
Use OAuth 2's controllerWithScope:
r688 by gregrobbins on Aug 10, 2011   Diff
Update sample apps to use beginSheetMo
dalForWindow:completionHandler: for
10.7 compatibility
All revisions of this file

File info

Size: 56805 bytes, 1595 lines
Powered by Google Project Hosting