-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCWArrayController.m
More file actions
1088 lines (910 loc) · 33.2 KB
/
Copy pathCWArrayController.m
File metadata and controls
1088 lines (910 loc) · 33.2 KB
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
#import "CWArrayController.h"
#import "DownloadItem.h"
@interface CWArrayController (Private)
-(void)updateLogView:(DownloadItem*)item;
-(void)timerFired;
-(void)loadDrawerSize;
-(void)saveDrawerSize;
-(void)updateButton;
@end
@implementation CWArrayController
-(void)awakeFromNib
{
[self loadList];
[tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSPasteboardTypeString,nil]];
// load apple script
NSString *filePath=[[[NSBundle mainBundle]resourcePath]
stringByAppendingPathComponent : @"GetURLFromSafari.scpt" ];
NSURL *fileURL=[NSURL fileURLWithPath:filePath];
NSDictionary *err;
appleScript=[[NSAppleScript alloc]initWithContentsOfURL:fileURL error:&err];
if(! appleScript){
NSLOG(@"%@",fileURL);
NSLOG(@"NSAppleScript::load err = %@", err);
}
if (! [appleScript compileAndReturnError: &err]) {
NSLOG(@"NSAppleScript::compileAndReturnError err = %@", err);
[appleScript release];
}
[self loadDrawerSize];
//start timer
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
BOOL autoDownload=[[userDefaults valueForKey:@"autoDownload"]boolValue];
if(autoDownload) [self startTimer];
}
-(void)clear
{
NSLOG(@"CWArrayController::clear");
[self saveDrawerSize];
[self stopTimer];
//remove files
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
int removeType=[[userDefaults valueForKey:@"removeDownloadOption"]intValue];
if(removeType==1) {
[self removeFinishedDownload];
}
[self saveList];
//clean list
NSMutableArray *list=[self content];
int i;
for(i=0;i<[list count];i++){
DownloadItem *data=[list objectAtIndex:i];
if([data isDownloading]){
[data stopDownload];
}
}
[list removeAllObjects];
[appleScript release];
}
-(void)dealloc
{
NSLOG(@"CWArrayController::dealloc");
[self clear];
[super dealloc];
}
#pragma mark ------------------ timer --------------------
-(void)startTimer
{
NSLOG(@"CWArrayController::startTimer");
[timer release];
// start timer
timer = [[NSTimer scheduledTimerWithTimeInterval:5.0
target:self // Target is this object
selector:@selector(timerFired) // What function are we calling
userInfo:nil repeats:YES] // No userinfo / repeat infinitely
retain]; // No autorelease
// Add our timers to the EventTracking loop
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSEventTrackingRunLoopMode];
}
-(void)stopTimer
{
NSLOG(@"CWArrayController::stopTimer");
[timer invalidate];
[timer release];
timer=nil;
}
-(BOOL)isTimerRunning
{
if(!timer) return NO;
else return [timer isValid];
}
-(void)loadList
{
NSLOG(@"CWArrayController::loadList");
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
if(userDefaults){
NSString *version=[userDefaults valueForKey:@"version"];
if(version && [version isEqualToString:@"2.0"]){
[userDefaults setValue:@"" forKey:@"url"];
[userDefaults setValue:@"" forKey:@"referer"];
NSArray *list=[userDefaults valueForKey:@"list"];
if(list){
//NSLOG("%@",[list class]);
int i;
for(i=0;i<[list count];i++){
id listItem=[list objectAtIndex:i];
DownloadItem *data=[[[DownloadItem alloc]init]autorelease];
[data setDelegate:self];
[self copyValueForKey:@"url" from:listItem to:data];
[self copyValueForKey:@"httpUser" from:listItem to:data];
[self copyValueForKey:@"httpPassword" from:listItem to:data];
[self copyValueForKey:@"referer" from:listItem to:data];
[self copyValueForKey:@"resume" from:listItem to:data];
[self copyValueForKey:@"checkTimeStamp" from:listItem to:data];
[self copyValueForKey:@"recursive" from:listItem to:data];
[self copyValueForKey:@"recursiveType" from:listItem to:data];
[self copyValueForKey:@"recursiveLevel" from:listItem to:data];
//[self copyValueForKey:@"status" from:listItem to:data];
[self addObject:data];
}
}
}else{
NSLOG(@"version error");
[self createUserDefaults];
}
}else{
NSLOG(@"userDefaults is nil");
}
}
-(void)saveList
{
NSLOG(@"CWArrayController::saveList");
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
if(userDefaults){
id list=[self content];
int i;
NSMutableArray *saveList=[NSMutableArray arrayWithCapacity:4];
for(i=0;i<[list count];i++){
NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithCapacity:8];
id data=[list objectAtIndex:i];
[self copyValueForKey:@"url" from:data to:dict];
[self copyValueForKey:@"httpUser" from:data to:dict];
[self copyValueForKey:@"httpPassword" from:data to:dict];
[self copyValueForKey:@"referer" from:data to:dict];
[self copyValueForKey:@"resume" from:data to:dict];
[self copyValueForKey:@"checkTimeStamp" from:data to:dict];
[self copyValueForKey:@"recursive" from:data to:dict];
[self copyValueForKey:@"recursiveType" from:data to:dict];
[self copyValueForKey:@"recursiveLevel" from:data to:dict];
//[self copyValueForKey:@"status" from:data to:dict];
[saveList addObject:dict];
}
//NSLOG(@"%@",saveList);
[userDefaults setValue:saveList forKey:@"list"];
}
}
-(void)createUserDefaults
{
//default parameters
NSLOG(@"CWArrayController::createUserDefaults");
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
if(userDefaults){
[userDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"resume"];
[userDefaults setValue:@"0" forKey:@"recursiveType"];
[userDefaults setValue:@"~/Desktop" forKey:@"downloadFolder"];
[userDefaults setValue:@"2" forKey:@"removeDownloadOption"];
[userDefaults setValue:@"5" forKey:@"maxConnections"];
[userDefaults setValue:@"2" forKey:@"recursiveLevel"];
[userDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"usePassiveFTP"];
[userDefaults setValue:@"2.0" forKey:@"version"];
}
}
/*
http://homepage.mac.com/mkino2/cocoaProg/Foundation/NSString/NSString.html#parseStringAsLines
*/
- (NSArray*)parseAsLines:(NSString*)string
{
NSString* parsedString;
NSRange range, subrange,subrange2;
int length;
NSMutableArray* parsedStrings=[[NSMutableArray alloc]initWithCapacity:1];
length = [string length];
range = NSMakeRange(0, length);
while(range.length > 0) {
subrange = [string lineRangeForRange:
NSMakeRange(range.location, 0)];
if(subrange.length!=range.length){
subrange2.location=subrange.location;
subrange2.length=subrange.length-1;
}else{
subrange2=subrange;
}
//NSLOG(@"%d %d %d %d",subrange.location,subrange.length,range.location,range.length);
parsedString = [string substringWithRange:subrange2];
[parsedStrings addObject:[parsedString copy] ];
//printf("line: %s\n", [string cString]);
range.location = NSMaxRange(subrange);
range.length -= subrange.length;
}
return parsedStrings;
}
- (NSString*) validateURL:(NSString*)originalURL
{
NSString* convertedURL=originalURL;
/* convret ttp:// => http:// */
if((originalURL)&&([originalURL hasPrefix:@"ttp"])){
convertedURL=[NSString stringWithFormat:@"h%@",originalURL];
}
return convertedURL;
}
/*
http://domain/image[000-999].jpg
->
http://domain/image000.jpg
http://domain/image001.jpg
...
http://domain/image999.jpg
*/
// return array of NSString
-(NSArray*) expandSequencialURL:(NSString*)url
{
NSUInteger pos1=[url rangeOfString:@"[" options:NSLiteralSearch].location;
NSUInteger pos2=[url rangeOfString:@"]" options:NSLiteralSearch].location;
NSUInteger pos3=[url rangeOfString:@"-" options:NSLiteralSearch].location;
//NSLOG(@"pos %d %d %d",pos1,pos2,pos3);
NSMutableArray* array=nil;
if(pos1<pos3 && pos3<pos2){
int length1=pos3-pos1-1;
int length2=pos2-pos3-1;
int length=length2;
if(length1>length2) length=length1;
NSString* s1=[url substringWithRange:NSMakeRange(pos1+1,length1)];
NSString* s2=[url substringWithRange:NSMakeRange(pos3+1,length2)];
int startNum=[s1 intValue];
int endNum=[s2 intValue];
if(startNum>endNum){
int tmp=startNum;
startNum=endNum;
endNum=tmp;
}
//NSLOG(@"%d %d",startNum,endNum);
if(startNum!=0 && endNum!=0){
NSString* header=[url substringToIndex:pos1];
NSString* footer=[url substringFromIndex:pos2+1];
NSLOG(@"%@",header);
NSLOG(@"%@",footer);
char formatStr[4096];
char digitStr[4096];
sprintf(formatStr,"%%.%dd",length);
array=[NSMutableArray arrayWithCapacity:100];
for(int i=startNum;i<=endNum;i++){
sprintf(digitStr,formatStr,i);
NSString* digit=[NSString stringWithCString:digitStr encoding:NSUTF8StringEncoding];
NSString* aURL=[header stringByAppendingString:digit];
aURL=[aURL stringByAppendingString:footer];
[array addObject:aURL];
}
}
}
if(array==nil || [array count]==0){
array=[NSArray arrayWithObject:url];
}
return array;
}
-(NSArray*)parseURL:(NSString*)str
{
NSMutableArray *array=[NSMutableArray arrayWithCapacity:1];
NSArray *lines=[self parseAsLines:str];
int i,j;
for(i=0;i<[lines count];i++){
NSString* line=[lines objectAtIndex:i];
NSArray *urls=[self expandSequencialURL:line];
for(j=0;j<[urls count];j++){
NSString *url=[urls objectAtIndex:j];
url=[self validateURL:url];
[array addObject:url];
}//j
}//i
return array;
}
- (IBAction)addToList:(id)sender
{
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
//NSString *url=[userDefaults valueForKey:@"url"];
NSString *url=[urlTextField stringValue];
[userDefaults setValue:url forKey:@"url"];
if((!url)||([url isEqualToString:@""])) return;
NSArray *urls=[self parseURL:url];
int i=0;
int urlCount=[urls count];
for(i=0;i<urlCount;i++){
NSString *u=[urls objectAtIndex:i];
if(urlCount>1) [self addURLWithOutChecking:u];
else [self addURL:u];
}
}
-(void)copyValueForKey:(NSString*)key from:(id)srcObj to:(id)dstObj
{
if(srcObj==dstObj) return;
id value=[srcObj valueForKey:key];
if(value) [dstObj setValue:[value copy] forKey:key];
}
-(BOOL)isURLInList:(NSString *)urlString
{
//check same url
NSArray* list=[self content];
//NSLOG(@"%@",[list class]);
int i=0;
for(i=0;i<[list count];i++){
NSString *urlInList=[[list objectAtIndex:i] valueForKey:@"url"];
//NSLOG(urlInList);
if([urlString isEqualToString:urlInList]) return YES;
}
return NO;
}
-(DownloadItem *)downloadItem:(NSString *)urlString
{
DownloadItem *data=[[[DownloadItem alloc]init]autorelease];
[data setDelegate:self];
[data setValue:urlString forKey:@"url"];
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
[self copyValueForKey:@"httpUser" from:userDefaults to:data];
[self copyValueForKey:@"httpPassword" from:userDefaults to:data];
[self copyValueForKey:@"referer" from:userDefaults to:data];
[self copyValueForKey:@"resume" from:userDefaults to:data];
[self copyValueForKey:@"checkTimeStamp" from:userDefaults to:data];
[self copyValueForKey:@"recursive" from:userDefaults to:data];
[self copyValueForKey:@"recursiveType" from:userDefaults to:data];
[self copyValueForKey:@"recursiveLevel" from:userDefaults to:data];
BOOL autoDownload=[[userDefaults valueForKey:@"autoDownload"]boolValue];
/*
if((autoDownload)&&([self downloadShouldStart:data])){
[data startDownload];
}
*/
if(!autoDownload){
[self setSelectionIndexes:nil];//deselect
}
return data;
}
-(NSString*)domain:(NSString*)url
{
NSURL *aURL=[NSURL URLWithString:url];
if(!aURL) return url;
return [aURL host];
}
-(BOOL)downloadShouldStart:(DownloadItem*)data
{
int downloadingCount=[self downloadingCount];
int maxConnections=1+[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"maxConnections"] intValue];
NSString *domain=[self domain:[data valueForKey:@"url"]];
//NSLOG(@"%@",domain);
if(
(downloadingCount<maxConnections) &&
([[data valueForKey:@"status"] isEqualToString:WAITING] ) &&
([self downloadingCountInDomain:domain]==0)
)
{
return YES;
}
return NO;
}
-(void)addURLWithOutChecking:(NSString *)urlString
{
if([urlString isEqualToString:@""]) return;
if([urlString isEqualToString:@"\n"]) return;
DownloadItem *data=[self downloadItem:urlString];
[self addObject:data];
}
-(void)addURL:(NSString *)urlString
{
if([urlString isEqualToString:@""]) return;
if([urlString isEqualToString:@"\n"]) return;
if([self isURLInList:urlString]) return;
DownloadItem *data=[self downloadItem:urlString];
[self addObject:data];
}
-(void)insertURL:(NSString *)urlString atArrangedObjectIndex:(unsigned int)index
{
if([self isURLInList:urlString]) return;
DownloadItem *data=[self downloadItem:urlString];
[self insertObject:data atArrangedObjectIndex:index];
}
- (IBAction)delete:(id)sender
{
[self remove:sender];
[self tableViewSelectionDidChange:nil];
}
- (IBAction)startStop:(id)sender
{
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
BOOL autoDownload=[[userDefaults valueForKey:@"autoDownload"]boolValue];
if(autoDownload){
NSArray *selectedObjects=[self selectedObjects];
int i;
for(i=0;i<[selectedObjects count];i++){
id downloadItem=[selectedObjects objectAtIndex:i];
[downloadItem startStop];
}
int selectedCount=[selectedObjects count];
if(selectedCount==1){
id downloadItem=[selectedObjects objectAtIndex:0];
if([downloadItem isDownloading]){
[startButton setTitle:NSLocalizedString(@"Stop",@"")];
}else{
[startButton setTitle:NSLocalizedString(@"Start",@"")];
}
}else if(selectedCount==0){
int downloadingCount=[self downloadingCount];
int totalCount=[[self content]count];
if(downloadingCount==0){
[startButton setTitle:NSLocalizedString(@"Stop",@"")];
}else if(downloadingCount==totalCount){
[startButton setTitle:NSLocalizedString(@"Start",@"")];
}else{
}
id list=[self content];
for(i=0;i<[list count];i++){
DownloadItem *downloadItem=[list objectAtIndex:i];
if([[downloadItem valueForKey:@"status"] isEqualToString:FINISHED]==NO){
if(downloadingCount==0) [downloadItem startDownload];
else if(downloadingCount==totalCount) [downloadItem stopDownload];
}
}
}
}else{ //autodownload ==NO
if([self isTimerRunning]) {
[self stopTimer];
[self stopAll];
}else {
[self startTimer];
[self timerFired];
}
}
[self tableViewSelectionDidChange:nil];
}
-(void)stopAll
{
id list=[self content];
int i;
for(i=0;i<[list count];i++){
DownloadItem *downloadItem=[list objectAtIndex:i];
if([downloadItem isDownloading]) [downloadItem stopDownload];
if([[downloadItem valueForKey:@"status"] isEqualToString:FINISHED]==NO){
[downloadItem setValue:WAITING forKey:@"status"];
}
}
}
- (IBAction)setURL:(id)sender
{
//NSLOG(@"CWArrayController::setURL");
/*NSArray *selectedObjects=[self selectedObjects];
if([selectedObjects count]==1){
id data=[selectedObjects objectAtIndex:0];
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
[self copyValueForKey:@"url" from:userDefaults to:data];
}*/
[self setSelectionIndexes:nil];//deselect
}
-(void)copyUserDefaultToSelectedDataForKey:(NSString*)key
{
NSArray *selectedObjects=[self selectedObjects];
if([selectedObjects count]==1){
id data=[selectedObjects objectAtIndex:0];
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
[self copyValueForKey:key from:userDefaults to:data];
}
}
- (IBAction)setHTTPUser:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"httpUser"];
}
- (IBAction)setHTTPPassword:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"httpPassword"];
}
- (IBAction)setReferer:(id)sender;
{
[self copyUserDefaultToSelectedDataForKey:@"referer"];
}
- (IBAction)setResume:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"resume"];
}
- (IBAction)setCheckTimeStamp:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"checkTimeStamp"];
}
- (IBAction)setRecursive:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"recursive"];
}
- (IBAction)setRecursiveType:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"recursiveType"];
}
- (IBAction)setRecursiveLevel:(id)sender
{
[self copyUserDefaultToSelectedDataForKey:@"recursiveLevel"];
}
- (IBAction)setAutoDownload:(id)sender
{
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
BOOL autoDownload=[[userDefaults valueForKey:@"autoDownload"]boolValue];
if(autoDownload) {
if([self isTimerRunning]==NO) [self startTimer];
}else{
if([self isTimerRunning]) {
[self stopTimer];
[self stopAll];
}
[startButton setEnabled:YES];
}
}
- (IBAction)showLog:(id)sender
{
//NSLog(@"showLog");
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
BOOL showLog=[[userDefaults valueForKey:@"showLog"]boolValue];
//[userDefaults setValue:[NSNumber numberWithBool:!showLog] forKey:@"showLog"];
if(showLog) {
[(NSDrawer*)drawer open];
}else{
[(NSDrawer*)drawer close];
}
}
-(int)downloadingCount
{
NSArray* list=[self content];
int count=0;
int i;
for(i=0;i<[list count];i++){
DownloadItem *data=[list objectAtIndex:i];
if([data isDownloading]){
count++;
}
}
return count;
}
-(int)downloadingCountInDomain:(NSString*)domain
{
NSArray* list=[self content];
int count=0;
int i;
for(i=0;i<[list count];i++){
DownloadItem *data=[list objectAtIndex:i];
if([data isDownloading]){
NSString *dataDomain=[self domain:[data valueForKey:@"url"]];
if([dataDomain isEqualToString:domain]){
count++;
}
}
}
return count;
}
-(void)removeFinishedDownload
{
NSLOG(@"CWArrayController::removeFinishedDownload");
NSMutableArray* list=[self content];
int i;
for(i=0;i<[list count];i++){
DownloadItem *data=[list objectAtIndex:i];
if([[data valueForKey:@"status"] isEqualToString:FINISHED]){
NSLOG(@"remove:%d",i);
[list removeObjectAtIndex:i];
i--;
}
}
}
-(void)timerFired
{
//NSLOG(@"timerFired");
if(![self isTimerRunning]) return;
if(isChecking) return;
isChecking=YES;
NSArray* list=[self content];
int i;
for(i=0;i<[list count];i++){
DownloadItem *data=[list objectAtIndex:i];
if([self downloadShouldStart:data]){
[data startDownload];
break;
}
}
isChecking=NO;
}
-(BOOL)isSarariRunning
{
BOOL flag=NO;
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in runningApps) {
if ([[app localizedName] isEqualToString:@"Safari"]) {
flag=YES;
break;
}
}
return flag;
}
-(void)getRefererFromSafari
{
if([self isSarariRunning]){
//NSLOG(@"getRefererFromSafari");
NSString *referer=nil;
NSDictionary *err;
NSAppleEventDescriptor *result;
result = [appleScript executeAndReturnError: &err];
if(result){
referer=[result stringValue];
NSLOG(@"%@",referer);
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
if(referer)[userDefaults setValue:referer forKey:@"referer"];
}
//NSLOG(@"getRefererFromSafari end");
}
}
//DownloadItem delegate
-(void)logUpdated:(DownloadItem*)item
{
[self updateLogView:item];
}
-(void)downloadFinished:(DownloadItem*)item
{
NSLOG(@"CWArrayController::downloadFinished");
[self saveList];
//[self showLog:nil];
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
BOOL openFile=[[userDefaults valueForKey:@"openFilesAfterDownloading"]boolValue];
BOOL isRecursive=[[item valueForKey:@"recursive"]boolValue];
NSString* downloadFolder=[[userDefaults valueForKey:@"downloadFolder"] stringByExpandingTildeInPath];
NSString* url=[item valueForKey:@"url"];
NSString* fileName=[url lastPathComponent];
NSString* filePath=[item valueForKey:@"downloadedFilePath"];
NSLOG(@"filePath:%@",filePath);
if((filePath==nil)||([[NSFileManager defaultManager]fileExistsAtPath:filePath]==NO)){
filePath=[downloadFolder stringByAppendingPathComponent:fileName];
}
//write comment
BOOL saveURLAsComment=[[userDefaults valueForKey:@"saveURLAsComment"]boolValue];
if((saveURLAsComment)&&(isRecursive==NO)){
if([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
[self writeComment:url toFile:filePath];
}
}
//open file
if((openFile)/*&&(isRecursive==NO)*/){
NSLOG(@"openFile: %@",filePath);
if([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:filePath]];
}
}
//remove finished file
int removeType=[[userDefaults valueForKey:@"removeDownloadOption"]intValue];
if((removeType==2) && ([[item valueForKey:@"status"] isEqualToString:FINISHED])){
[self removeObject:item];
}
BOOL autoDownload=[[userDefaults valueForKey:@"autoDownload"]boolValue];
if(autoDownload==NO){
if([[self content]count]==0){
if([self isTimerRunning]) [self stopTimer];
}
}
[self tableViewSelectionDidChange:nil];
}
-(void)writeComment:(NSString*) comment toFile:(NSString*)filePath
{
if((!comment)||(!filePath)) return;
NSString *scriptText;
NSAppleScript *script;
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)filePath, kCFURLPOSIXPathStyle, NO);
NSString *hfsPath = (NSString *)CFBridgingRelease(CFURLCopyFileSystemPath(fileURL, kCFURLHFSPathStyle));
CFRelease(fileURL);
scriptText = [NSString stringWithFormat:@"tell application \"Finder\" to set comment of item \"%@\" to \"%@\"", hfsPath, comment];
//NSLOG(scriptText);
script = [[[NSAppleScript alloc] initWithSource:scriptText] autorelease];
[script executeAndReturnError:nil];
}
-(void)updateDownloadInfo
{
NSArray *selectedObjects=[self selectedObjects];
if([selectedObjects count]==1){
id data=[selectedObjects objectAtIndex:0];
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
[self copyValueForKey:@"url" from:data to:userDefaults];
[self copyValueForKey:@"httpUser" from:data to:userDefaults];
[self copyValueForKey:@"httpPassword" from:data to:userDefaults];
[self copyValueForKey:@"referer" from:data to:userDefaults];
[self copyValueForKey:@"resume" from:data to:userDefaults];
[self copyValueForKey:@"checkTimeStamp" from:data to:userDefaults];
[self copyValueForKey:@"recursive" from:data to:userDefaults];
[self copyValueForKey:@"recursiveType" from:data to:userDefaults];
[self copyValueForKey:@"recursiveLevel" from:data to:userDefaults];
}
}
-(void)updateLogView:(DownloadItem*)item
{
//NSLog(@"CWArrayController::updateLogView");
if([drawer state]==NSDrawerOpenState){
NSArray *selectedObjects=[self selectedObjects];
if([selectedObjects count]==1){
DownloadItem* downloadItem=[selectedObjects objectAtIndex:0];
if(item==downloadItem || item==nil){
if(downloadItem){
NSString *log=[downloadItem logString];
[logView setString:log];
int length=[log length];
[logView scrollRangeToVisible: NSMakeRange(length,0)];
}
}
}else{
[logView setString:@""];//clear log
}
}
}
-(void)updateButton
{
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
BOOL autoDownload=[[userDefaults valueForKey:@"autoDownload"]boolValue];
if(autoDownload){
NSArray *selectedObjects=[self selectedObjects];
if([selectedObjects count]==1){
//update GUI
DownloadItem* data=[selectedObjects objectAtIndex:0];
[startButton setEnabled:YES];
if([data isDownloading]){
[startButton setTitle:NSLocalizedString(@"Stop",@"")];
}else{
[startButton setTitle:NSLocalizedString(@"Start",@"")];
}
}else if([selectedObjects count]>1){
id data=[selectedObjects objectAtIndex:0];
BOOL isDownloading=[data isDownloading];
BOOL sameState=YES;
unsigned int i;
for(i=1;i<[selectedObjects count];i++){
if(isDownloading!=[[selectedObjects objectAtIndex:i]isDownloading]){
sameState=NO;
break;
}
}
if(sameState){
if(isDownloading){
[startButton setTitle:NSLocalizedString(@"Stop",@"")];
}else{
[startButton setTitle:NSLocalizedString(@"Start",@"")];
}
[startButton setEnabled:YES];
}else{
[startButton setEnabled:NO];
}
}else{
// no item
int downloadingCount=[self downloadingCount];
int totalCount=[[self content]count];
if(downloadingCount==0){
[startButton setTitle:NSLocalizedString(@"Start",@"")];
[startButton setEnabled:YES];
}else if(downloadingCount==totalCount){
[startButton setTitle:NSLocalizedString(@"Stop",@"")];
[startButton setEnabled:YES];
}else{
[startButton setEnabled:NO];
}
}
}else{ //autodownload =NO
[startButton setEnabled:YES];
if([self isTimerRunning]) [startButton setTitle:NSLocalizedString(@"Stop",@"")];
else [startButton setTitle:NSLocalizedString(@"Start",@"")];
}
}
#pragma mark ------------------ drawer --------------------
- (void)drawerDidOpen:(NSNotification *)notification
{
[self updateLogView:nil];
}
- (void)drawerDidClose:(NSNotification *)notification
{
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
[userDefaults setValue:[NSNumber numberWithBool:NO] forKey:@"showLog"];
}
-(void)loadDrawerSize
{
//update drawer size
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
int w=[[userDefaults valueForKey:@"drawerWidth"]intValue];
int h=[[userDefaults valueForKey:@"drawerHeight"]intValue];
if(h==0) h=40;
[drawer setContentSize:NSMakeSize(w,h)];
[self showLog:nil];
}
-(void)saveDrawerSize
{
NSSize size=[drawer contentSize];
int w=size.width;
int h=size.height;
id userDefaults=[[NSUserDefaultsController sharedUserDefaultsController] values];
[userDefaults setValue:[NSNumber numberWithInt:w] forKey:@"drawerWidth"];
[userDefaults setValue:[NSNumber numberWithInt:h] forKey:@"drawerHeight"];
}
#pragma mark ------------------ tableView --------------------
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
//NSLOG(@"tableViewSelectionDidChange");
[self updateButton];
[self updateDownloadInfo];
[self updateLogView:nil];
}
//-----------------------------------------------------
//tableView source for drag and drop
- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
{
//NSLOG(@"validateDrop %d",row);
int numberOfRows=[tv numberOfRows];
if(row<0){
[tv setDropRow:numberOfRows-1 dropOperation:NSTableViewDropAbove];
}else{
if(row>=numberOfRows) row=numberOfRows-1;
[tv setDropRow:row dropOperation:NSTableViewDropOn];
}
if([info draggingSource]==tv) return NSDragOperationMove;
else return NSDragOperationCopy;
}
- (BOOL)tableView:(NSTableView*)tv acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)op
{
//NSLOG(@"acceptDrop %d->%d",draggingIndex,row);
id draggingSource=[info draggingSource];
NSPasteboard *myPasteboard=[info draggingPasteboard];
NSArray *typeArray=[NSArray arrayWithObjects:NSPasteboardTypeString,nil];
NSString *availableType;
NSArray *filesList;
// find the best match of the types we'll accept and what's actually on the pasteboard
availableType=[myPasteboard availableTypeFromArray:typeArray];
//NSLOG(@"availableType:%@",availableType);
// In the file format type that we're working with, get all data on the pasteboard
filesList=[myPasteboard propertyListForType:availableType];
// Insert url
BOOL isAccepted=NO;
if(draggingSource==tv){
//swap object
/*
id obj1=[[self content]objectAtIndex:draggingIndex];
[obj1 retain];
id obj2=[[self content]objectAtIndex:row];
[obj2 retain];