forked from jmroot/UnRarX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.m
executable file
·1094 lines (952 loc) · 35.9 KB
/
Controller.m
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
/*
UnRarX, Mac OS X GUI for rar and par file extraction.
Copyright © 2003 Peter Noriega
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
RAR and unRAR are copyright Eugene Roshal.
THE UNRAR UTILITY ARE DISTRIBUTED "AS IS". NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED. YOU USE AT YOUR OWN RISK. THE AUTHORS WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING THIS SOFTWARE.
Thanks for using UnRarX for Mac OS X.
________________________________________
*/
#import "Controller.h"
#import "AboutBox.h"
@implementation Controller
//Loading Extractions Locations from Prefs
- (void)_loadLocations
{
int count, total = [locations count];
[locationPullDown removeAllItems];
[locationPullDown addItemWithTitle:NSLocalizedStringFromTable(@"File Directory", @"Localized", @"Label for File Directory Option in Pulldown")];
[locationPullDown addItemWithTitle:NSLocalizedStringFromTable(@"Desktop", @"Localized", @"Label for Desktop Option in Pulldown")];
[locationPullDown addItemWithTitle:NSLocalizedStringFromTable(@"Other...", @"Localized", @"Label for Other Option in Pulldown")];
for(count = 0; count < total; count++)
{
[locationPullDown insertItemWithTitle:[[locations objectAtIndex:count] objectForKey:@"Name"] atIndex:count+2];
}
[[locationPullDown menu] insertItem:[NSMenuItem separatorItem] atIndex:total+2];
[locationPullDown addItemWithTitle:NSLocalizedStringFromTable(@"Manage...", @"Localized", @"Label for Manage Option in Pulldown")];
}
- (void) awakeFromNib
{
NSToolbarItem *item;
NSRect rect = [window frame];
NSRect eRect = [extractionView frame];
NSRect tRect = [textScrollView frame];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishedTask:)name:NSTaskDidTerminateNotification object:nil];
filePath = nil;
extractPath = nil;
extractPathDisplay = nil;
filename = nil;
items = nil;
unrar = nil;
par = nil;
items=[[NSMutableDictionary alloc] init];
item=[[NSToolbarItem alloc] initWithItemIdentifier:@"Browse"];
[item setPaletteLabel:NSLocalizedStringFromTable(@"Browse", @"Localized", @"Toolbar Palette Label for Browse for Files")];
[item setLabel:NSLocalizedStringFromTable(@"Browse Files", @"Localized", @"Toolbar Label for Browse for Files")];
[item setToolTip:NSLocalizedStringFromTable(@"Browse for Files", @"Localized", @"Toolbar Tooltip for Browse for Files")];
[item setImage:[NSImage imageNamed:@"browse"]];
[item setTarget:self];
[item setAction:@selector(browse:)];
[items setObject:item forKey:@"Browse"];
item=[[NSToolbarItem alloc] initWithItemIdentifier:@"Extract"];
[item setPaletteLabel:NSLocalizedStringFromTable(@"Extract", @"Localized", @"Toolbar Palette Label for Extract Files")];
[item setLabel:NSLocalizedStringFromTable(@"Extract Files", @"Localized", @"Toolbar Label for Extract Files")];
//[item setEnabled:YES];
[item setToolTip:NSLocalizedStringFromTable(@"Extract Files to Disk", @"Localized", @"Toolbar Tooltip for Extract Files")];
[item setImage:[NSImage imageNamed:@"extract"]];
[item setTarget:self];
[item setAction:@selector(action:)];
[items setObject:item forKey:@"Extract"];
item=[[NSToolbarItem alloc] initWithItemIdentifier:@"Test"];
[item setPaletteLabel:NSLocalizedStringFromTable(@"Test", @"Localized", @"Toolbar Palette Label for Test Files")];
[item setLabel:NSLocalizedStringFromTable(@"Test Files", @"Localized", @"Toolbar Label for Test Files")];
[item setToolTip:NSLocalizedStringFromTable(@"Test RAR Files", @"Localized", @"Toolbar Tooltip for Test Files")];
[item setImage:[NSImage imageNamed:@"test"]];
[item setTarget:self];
[item setAction:@selector(test:)];
[items setObject:item forKey:@"Test"];
item=[[NSToolbarItem alloc] initWithItemIdentifier:@"Info"];
[item setPaletteLabel:NSLocalizedStringFromTable(@"Info", @"Localized", @"Toolbar Palette Label for Info on RAR Archive")];
[item setLabel:NSLocalizedStringFromTable(@"Get Info", @"Localized", @"Toolbar Label for Info on RAR Archive")];
[item setToolTip:NSLocalizedStringFromTable(@"List Contents of Archive", @"Localized", @"Toolbar Tooltip for Info on RAR Archive")];
[item setImage:[NSImage imageNamed:@"info"]];
[item setTarget:self];
[item setAction:@selector(list:)];
[items setObject:item forKey:@"Info"];
item=[[NSToolbarItem alloc] initWithItemIdentifier:@"Password"];
[item setPaletteLabel:NSLocalizedStringFromTable(@"Password", @"Localized", @"Toolbar Palette Label for Password Option")];
[item setLabel:NSLocalizedStringFromTable(@" Use Password", @"Localized", @"Toolbar Label for Password Option")];
[item setToolTip:NSLocalizedStringFromTable(@"Use Password for RAR", @"Localized", @"Toolbar Tooltip for Password Option")];
[item setImage:[NSImage imageNamed:@"password"]];
[item setTarget:self];
[item setAction:@selector(openPasswordSheet:)];
[items setObject:item forKey:@"Password"];
item=[[NSToolbarItem alloc] initWithItemIdentifier:@"Filename"];
[item setPaletteLabel:NSLocalizedStringFromTable(@"Filename", @"Localized", @"Toolbar Palette Label for Filename")];
[item setLabel:NSLocalizedStringFromTable(@"RAR Filename", @"Localized", @"Toolbar Label for Filename")];
[item setToolTip:NSLocalizedStringFromTable(@"RAR Filename Display", @"Localized", @"Toolbar Tooltip for Filename")];
[item setView: fileView];
[item setMinSize:NSMakeSize(30, NSHeight([fileView frame]))];
[item setMaxSize:NSMakeSize(600, NSHeight([fileView frame]))];
[item setTarget:self];
[items setObject:item forKey:@"Filename"];
[item release];
toolbar=[[NSToolbar alloc] initWithIdentifier:@"toolbar"];
[toolbar setDelegate:self];
[toolbar setAllowsUserCustomization:YES];
[toolbar setDisplayMode:NSToolbarDisplayModeLabelOnly];
[toolbar setAutosavesConfiguration:YES];
[dropView registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
[[extractionView retain] removeFromSuperview];
rect.origin.y += eRect.size.height;
rect.size.height -= eRect.size.height;
tRect = [textScrollView frame];
tRect.origin.y = 0.0;
[window setFrame:rect display:NO animate:NO];
[textScrollView setFrame:tRect];
[textScrollView display];
[window setToolbar:toolbar];
[window makeKeyAndOrderFront:nil];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self checkPrefs];
}
- (void)dealloc
{
[self saveData];
[toolbar release];
[items release];
[filePath release];
[extractPath release];
[extractPathDisplay release];
[filename release];
[unrar release];
[par release];
[locations release];
[prefs release];
}
- (void)checkPrefs
{
prefs = [[NSUserDefaults standardUserDefaults] retain];
if([prefs boolForKey:@"Overwrite"] == nil)
{
[prefs setObject:@"YES" forKey:@"Overwrite"];
}
if([prefs boolForKey:@"Overwrite"] == YES)
{
[overwriteCB setState:NSOnState];
}
if([prefs boolForKey:@"Recurse"] == nil)
{
[prefs setObject:@"NO" forKey:@"Recurse"];
}
if([prefs boolForKey:@"Recurse"] == YES)
{
[recurseCB setState:NSOnState];
}
if([prefs boolForKey:@"Assume Yes"] == nil)
{
[prefs setObject:@"NO" forKey:@"Assume Yes"];
}
if([prefs boolForKey:@"Assume Yes"] == YES)
{
[assumeCB setState:NSOnState];
}
if([prefs boolForKey:@"Keep Broken"] == nil)
{
[prefs setObject:@"NO" forKey:@"Keep Broken"];
}
if([prefs boolForKey:@"Keep Broken"] == YES)
{
[keepCB setState:NSOnState];
}
if([prefs boolForKey:@"Partorar"] == nil)
{
[prefs setObject:@"NO" forKey:@"Partorar"];
}
if([prefs boolForKey:@"Partorar"] == YES)
{
[partorarCB setState:NSOnState];
}
if ([prefs arrayForKey:@"Locations"] != nil )
{
locations = [[NSMutableArray alloc] initWithArray:[prefs arrayForKey:@"Locations"]];
}
else
{
locations = [[NSMutableArray alloc] init];
}
[self _loadLocations];
}
- (void)_doextract:(id)op
{
NSFileManager *manager = [NSFileManager defaultManager];
if([[op filenames] count] == 0)
return;
filePath = [[op filenames] lastObject];
[filePath retain];
filename = [manager displayNameAtPath:filePath];
[filename retain];
[fileField setStringValue:filename];
[[items objectForKey:@"Filename"] setLabel:filename];
[[items objectForKey:@"Extract"] setLabel:@"Extract"];
[[items objectForKey:@"Extract"] setImage:[NSImage imageNamed:@"extract"]]; // image
[[items objectForKey:@"Extract"] setEnabled:YES];
//if(extractPath == nil)
//[self setLocation:nil];
}
- (void)_setpath:(id)op
{
NSString *extension;
if([[op filenames] count] == 0)
{
return;
}
extractPath = [[op filenames] lastObject];
extension = [extractPath pathExtension];
if(![[extractPath pathExtension] isEqualTo:@""])
{
//NSLog(@"This is not a Directory");
extractPath = [extractPath stringByDeletingLastPathComponent];
[extractPath retain];
return;
}
[extractPath retain];
[self addRecord:nil];
//NSLog(@"Extract Path is %@", extractPath);
}
- (IBAction)browse:(id)sender
{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op beginSheetForDirectory:NSHomeDirectory() file:nil types:[NSArray arrayWithObject:@"rar"] modalForWindow:window modalDelegate:self didEndSelector:@selector(_doextract:) contextInfo:op];
}
- (IBAction)action:(id)sender;
{
if([[[items objectForKey:@"Extract"] label] isEqualTo:@"Extract"] == true)
{
[self extract:nil];
return;
}
if([[[items objectForKey:@"Extract"] label] isEqualTo:@"Stop"] == true)
{
[self stop:nil];
return;
}
else
{
[[items objectForKey:@"Extract"] setEnabled:NO];
return;
}
}
- (IBAction)extract:(id)sender
{
if(![[fileField stringValue] isEqualTo:@""])
{
[self setLocation:nil];
//NSLog(@"Extract Path is %@", extractPath);
if(extractPath == nil)
{
NSBeginAlertSheet(EXTRACT_PROBLEM, OK, nil, nil, window, self, nil, nil, nil, EXTRACT_NO_LOCATION_ERROR, nil);
return;
}
[pathDisplay setStringValue:@" "];
[pathDisplay setStringValue:extractPath];
[[items objectForKey:@"Extract"] setLabel:@"Stop"];
[[items objectForKey:@"Extract"] setImage:[NSImage imageNamed:@"stop"]]; // image
[[items objectForKey:@"Extract"] setToolTip:[NSString stringWithFormat:@"Stop Current Task"]];
[[items objectForKey:@"Extract"] setEnabled:YES];
[self processFile:filePath toPath:extractPath];
}
else
{
NSBeginAlertSheet(EXTRACT_PROBLEM, OK, nil, nil, window, self, nil, nil, nil, EXTRACT_NO_FILE_ERROR, nil);
}
}
- (IBAction)test:(id)sender
{
if(![[fileField stringValue] isEqualTo:@""])
{
[self testFile:filePath];
}
else
{
NSBeginAlertSheet(VERIFY_PROBLEM, OK, nil, nil, window, self, nil, nil, nil, TEST_NO_FILE_ERROR, nil);
}
}
- (IBAction)list:(id)sender
{
if(![[fileField stringValue] isEqualTo:@""])
{
[self listFile:filePath];
}
else
{
NSBeginAlertSheet(VERIFY_PROBLEM, OK, nil, nil, window, self, nil, nil, nil, INFO_NO_FILE_ERROR, nil);
}
}
- (IBAction)clearLog:(id)sender
{
}
- (IBAction)stop:(id)sender;
{
//NSLog(@"Stopping");
[unrar terminate];
}
- (void)examineFilenames:(NSArray *) filenames
{
NSString *name;
NSString *path;
NSString *extension;
NSString *subextension;
NSFileManager *manager = [NSFileManager defaultManager];
//NSLog(@"Examining Filenames");
if([filenames count] > 0)
{
path = [filenames objectAtIndex:0];
name = [manager displayNameAtPath:path];
extension = [name pathExtension];
filePath = [filenames objectAtIndex:0];
[filePath retain];
//NSLog(@"FilePath is %@", filePath);
filename = [manager displayNameAtPath:filePath];
[filename retain];
subextension = [extension substringToIndex:1];
//NSLog(@"FileName is %@", filename);
[fileField setStringValue:filename];
[fileField setTextColor:[NSColor blueColor]];
if([extension compare:@"rar" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
//NSLog(@"Valid RAR File");
[[items objectForKey:@"Filename"] setLabel:filename];
[[items objectForKey:@"Extract"] setLabel:@"Extract"];
[[items objectForKey:@"Extract"] setImage:[NSImage imageNamed:@"extract"]]; // image
[[items objectForKey:@"Extract"] setEnabled:YES];
//if(extractPath == nil)
//[self setLocation:nil];
[self extract:nil];
return;
}
if([extension compare:@"ace" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
//NSLog(@"Valid ACE File");
//[self extractAce:filePath];
//return;
}
if([extension compare:@"par" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
//NSLog(@"Valid PAR File");
[self recoverFile:filePath];
return;
}
if([subextension compare:@"p" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
//NSLog(@"Valid PAR File");
[self recoverFile:filePath];
return;
}
else
{
//NSLog(@"Still Attempt to Extract");
[[items objectForKey:@"Filename"] setLabel:filename];
[[items objectForKey:@"Extract"] setLabel:@"Extract"];
[[items objectForKey:@"Extract"] setImage:[NSImage imageNamed:@"extract"]]; // image
[[items objectForKey:@"Extract"] setEnabled:YES];
[self extract:nil];
return;
}
}
else
{
return;
}
}
- (void)processFile:(NSString *)path toPath:(NSString *)location
{
NSMutableArray *args = [NSMutableArray array];
NSPipe *taskPipe = [NSPipe pipe];
NSString *command = @"x";
NSString *overwrite = @"-o+";
NSString *recurse = @"-r";
NSString *assumeYes = @"-y";
NSString *keepBroken = @"-kb";
NSString *password = @"-p";
NSRect rect = [window frame];
NSRect eRect = [extractionView frame];
NSRect tRect = [textScrollView frame];
int emask = [textScrollView autoresizingMask];
unrar = [[NSTask alloc] init];
rect.origin.y -= eRect.size.height;
rect.size.height += eRect.size.height;
emask = [extractionView autoresizingMask];
[textScrollView setAutoresizingMask:NSViewMinYMargin];
tRect = [textScrollView frame];
[window setFrame:rect display:YES animate:YES];
[textScrollView setAutoresizingMask:emask];
[[window contentView] addSubview:extractionView];
tRect.origin.y = eRect.size.height;
[textScrollView setFrame:tRect];
[extractionView display];
[textScrollView display];
/* set standard I/O, here to a NSPipe */
[unrar setStandardOutput:taskPipe];
[unrar setStandardError:taskPipe];
/* set arguments */
[args addObject:command];
if([prefs boolForKey:@"Overwrite"] == YES)
{
//NSLog(@"Overwrite Files");
[args addObject:overwrite];
}
if([prefs boolForKey:@"Recurse"] == YES)
{
[args addObject:recurse];
}
if([prefs boolForKey:@"Assume YES"] == YES)
{
[args addObject:assumeYes];
}
if([prefs boolForKey:@"Keep Broken"] == YES)
{
//NSLog(@"Keep Broken Extracted Files");
[args addObject:keepBroken];
}
//NSLog(@"Checking Password");
if(![[passwordField stringValue] isEqualTo:@""])
{
//NSLog(@"Password is not null");
password = [password stringByAppendingString:[passwordField stringValue]];
//NSLog(@"Password is %@", password);
[args addObject:password];
[passwordField setStringValue:@""];
}
//NSLog(@"Path is %@", path);
//NSLog(@"Location is %@", location);
[args addObject:path];
[args addObject:location];
[unrar setArguments:args];
/* set the path of the executable */
[unrar setLaunchPath:[[NSBundle mainBundle] pathForResource:@"unrar" ofType:nil]];
/* we want taskPipe to send notifications to be able to grab the output */
[[taskPipe fileHandleForReading] readInBackgroundAndNotify];
[progressBar startAnimation:self];
[unrar launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printTaskOutput:) name:NSFileHandleReadCompletionNotification object:[[unrar standardOutput] fileHandleForReading]];
//[unrar waitUntilExit];
}
- (void)testFile:(NSString *)path
{
NSMutableArray *args = [NSMutableArray array];
NSPipe *taskPipe = [NSPipe pipe];
NSString *command = @"t";
NSRect rect = [window frame];
NSRect eRect = [extractionView frame];
NSRect tRect = [textScrollView frame];
int emask = [textScrollView autoresizingMask];
unrar = [[NSTask alloc] init];
rect.origin.y -= eRect.size.height;
rect.size.height += eRect.size.height;
emask = [extractionView autoresizingMask];
[textScrollView setAutoresizingMask:NSViewMinYMargin];
tRect = [textScrollView frame];
[window setFrame:rect display:YES animate:YES];
[textScrollView setAutoresizingMask:emask];
[[window contentView] addSubview:extractionView];
tRect.origin.y = eRect.size.height;
[textScrollView setFrame:tRect];
[extractionView display];
[textScrollView display];
/* set standard I/O, here to a NSPipe */
[unrar setStandardOutput:taskPipe];
[unrar setStandardError:taskPipe];
/* set arguments */
[args addObject:command];
[args addObject:path];
[unrar setArguments:args];
/* set the path of the executable */
[unrar setLaunchPath:[[NSBundle mainBundle] pathForResource:@"unrar" ofType:nil]];
/* we want taskPipe to send notifications to be able to grab the output */
[[taskPipe fileHandleForReading] readInBackgroundAndNotify];
[progressBar startAnimation:self];
[unrar launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printTaskOutput:) name:NSFileHandleReadCompletionNotification object:[[unrar standardOutput] fileHandleForReading]];
//[unrar waitUntilExit];
}
- (void)listFile:(NSString *)path
{
NSMutableArray *args = [NSMutableArray array];
NSPipe *taskPipe = [NSPipe pipe];
NSString *command = @"v";
NSRect rect = [window frame];
NSRect eRect = [extractionView frame];
NSRect tRect = [textScrollView frame];
int emask = [textScrollView autoresizingMask];
unrar = [[NSTask alloc] init];
rect.origin.y -= eRect.size.height;
rect.size.height += eRect.size.height;
emask = [extractionView autoresizingMask];
[textScrollView setAutoresizingMask:NSViewMinYMargin];
tRect = [textScrollView frame];
[window setFrame:rect display:YES animate:YES];
[textScrollView setAutoresizingMask:emask];
[[window contentView] addSubview:extractionView];
tRect.origin.y = eRect.size.height;
[textScrollView setFrame:tRect];
[extractionView display];
[textScrollView display];
/* set standard I/O, here to a NSPipe */
[unrar setStandardOutput:taskPipe];
[unrar setStandardError:taskPipe];
/* set arguments */
[args addObject:command];
[args addObject:path];
[unrar setArguments:args];
/* set the path of the executable */
[unrar setLaunchPath:[[NSBundle mainBundle] pathForResource:@"unrar" ofType:nil]];
/* we want taskPipe to send notifications to be able to grab the output */
[[taskPipe fileHandleForReading] readInBackgroundAndNotify];
[progressBar startAnimation:self];
[unrar launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printTaskOutput:) name:NSFileHandleReadCompletionNotification object:[[unrar standardOutput] fileHandleForReading]];
//[unrar waitUntilExit];
}
- (void)recoverFile:(NSString *)path
{
NSMutableArray *args = [NSMutableArray array];
NSPipe *taskPipe = [NSPipe pipe];
NSString *command = @"r";
//NSString *move = @"-m";
//NSString *fix = @"-f";
//NSString *ignorecase = @"+C";
NSRect rect = [window frame];
NSRect eRect = [extractionView frame];
NSRect tRect = [textScrollView frame];
int emask = [textScrollView autoresizingMask];
par = [[NSTask alloc] init];
rect.origin.y -= eRect.size.height;
rect.size.height += eRect.size.height;
emask = [extractionView autoresizingMask];
[textScrollView setAutoresizingMask:NSViewMinYMargin];
tRect = [textScrollView frame];
[window setFrame:rect display:YES animate:YES];
[textScrollView setAutoresizingMask:emask];
[[window contentView] addSubview:extractionView];
tRect.origin.y = eRect.size.height;
[textScrollView setFrame:tRect];
[extractionView display];
[textScrollView display];
/* set standard I/O, here to a NSPipe */
[par setStandardOutput:taskPipe];
[par setStandardError:taskPipe];
/* set arguments */
[args addObject:command];
//[args addObject:move];
//[args addObject:fix];
//[args addObject:ignorecase];
[args addObject:path];
//[args addObject:location];
[par setArguments:args];
/* set the path of the executable */
[par setLaunchPath:[[NSBundle mainBundle] pathForResource:@"par2" ofType:nil]];
[par setCurrentDirectoryPath:[path stringByDeletingLastPathComponent]];
/* we want taskPipe to send notifications to be able to grab the output */
[[taskPipe fileHandleForReading] readInBackgroundAndNotify];
[progressBar startAnimation:self];
[par launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printTaskOutput:) name:NSFileHandleReadCompletionNotification object:[[par standardOutput] fileHandleForReading]];
//[par waitUntilExit];
}
- (IBAction)setLocation:(id)sender
{
if([locationPullDown indexOfSelectedItem] == 0)
{
//NSLog(@"Location is File Directory");
extractPath = [filePath stringByDeletingLastPathComponent];
//NSLog(@"Extract Path is %@", extractPath);
return;
}
if([locationPullDown indexOfSelectedItem] == 1)
{
//NSLog(@"Location is Desktop");
extractPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"];
//NSLog(@"Extract Path is %@", extractPath);
return;
}
if([[[locationPullDown selectedItem] title] isEqualTo:@"Other..."])
{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setCanChooseFiles:NO];
[op setAllowsMultipleSelection:NO];
[op setCanChooseDirectories:YES];
[op beginSheetForDirectory:NSHomeDirectory() file:nil types:nil modalForWindow:window modalDelegate:self didEndSelector:@selector(_setpath:) contextInfo:op];
return;
}
if([[[locationPullDown selectedItem] title] isEqualTo:@"Manage..."])
{
//NSLog(@"Managing");
[locationPullDown selectItemWithTitle:@"File Directory"];
[self openLocationsSheet:self];
return;
}
else
{
extractPath = [[locations objectAtIndex:[locationPullDown indexOfSelectedItem] - 2] objectForKey:@"Path"];
}
}
- (void)printTaskOutput:(NSNotification *)aNotification
{
// there is Data from the task output or error file to print in the Sheet Window
NSString *outputString;
NSData *data = [[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem];
if (data && [data length])
{
outputString = [NSString stringWithCString:[data bytes] length:[data length]];
[self appendOutput:outputString];
//[self addToTextViewContentOfString:outputString];
[[aNotification object] readInBackgroundAndNotify];
}
}
- (void)appendOutput:(NSString *)output
{
// add the string (a chunk of the results from locate) to the NSTextView's
// backing store, in the form of an attributed string
[[taskTextField textStorage] appendAttributedString: [[[NSAttributedString alloc]
initWithString: output] autorelease]];
// setup a selector to be called the next time through the event loop to scroll
// the view to the just pasted text. We don't want to scroll right now,
// because of a bug in Mac OS X version 10.1 that causes scrolling in the context
// of a text storage update to starve the app of events
[self performSelector:@selector(scrollToVisible:) withObject:nil afterDelay:0.0];
}
// This routine is called after adding new results to the text view's backing store.
// We now need to scroll the NSScrollView in which the NSTextView sits to the part
// that we just added at the end
- (void)scrollToVisible:(id)ignore
{
[taskTextField scrollRangeToVisible:NSMakeRange([[taskTextField string] length], 0)];
}
- (void)addToTextViewContentOfString:(NSString *)theString
{
NSRange theRange;
theRange.length = 0;
theRange.location = [[taskTextField string] length];
[taskTextField setEditable:YES];
[taskTextField setSelectedRange:theRange];
[taskTextField insertText:theString];
[taskTextField setEditable:NO];
}
- (NSString*)_lookForCompanionRar
{
int loop;
NSString *path = [filePath stringByDeletingLastPathComponent];
NSArray *array = [[NSFileManager defaultManager] directoryContentsAtPath:path];
NSEnumerator *enumerator = [array objectEnumerator];
//NSArray *pathComponents = [path pathComponents];
NSString *localFile;
NSString *aFile;
//NSLog(@"Filename is %@", filename);
//NSLog(@"File path is %@", path);
//NSLog(@"Filename w/o Extension is %@", [filename stringByDeletingPathExtension]);
//NSLog(@"Extension is %@", [filename pathExtension]);
localFile = [filename stringByDeletingPathExtension];
if([[filename pathExtension] isEqualToString:@"PAR2"] == true)
{
for(loop = 1; loop <= 1; loop++)
{
if(([localFile rangeOfString:@"."]).location != NSNotFound)
{
localFile = [localFile stringByDeletingPathExtension];
//NSLog(@"Local File is %@", localFile);
}
}
//localFile = [localFile stringByAppendingString:@".part01"] ;
//NSLog(@"Local File is %@", localFile);
}
while(aFile = [enumerator nextObject])
{
//NSLog(@"Entering While Loop");
if([[aFile pathExtension] rangeOfString:@"rar" options:NSCaseInsensitiveSearch|NSBackwardsSearch].location != NSNotFound && [[aFile lastPathComponent] rangeOfString:localFile options:NSCaseInsensitiveSearch|NSBackwardsSearch].location != NSNotFound)
{
//return [path stringByAppendingString:aFile];
//NSLog(@"Found Matching RAR File");
return [NSString stringWithFormat:@"%@/%@", path, aFile];
}
}
return nil;
}
- (void)finishedTask:(NSNotification *)aNotification {
NSString *confirmation = @" Task Complete.";
NSString *finalConfirmation = [filename stringByAppendingString:confirmation];
NSRect rect = [window frame];
NSRect eRect = [extractionView frame];
NSRect tRect = [textScrollView frame];
NSTask *task = [aNotification object];
[pathDisplay setStringValue:finalConfirmation];
[progressBar stopAnimation:self];
[[items objectForKey:@"Extract"] setLabel:@"Extract"];
[[items objectForKey:@"Extract"] setImage:[NSImage imageNamed:@"extract"]]; // image
[[items objectForKey:@"Extract"] setToolTip:[NSString stringWithFormat:@"Extract"]]; // tooltip
[[items objectForKey:@"Extract"] setEnabled:YES];
[[extractionView retain] removeFromSuperview];
rect.origin.y += eRect.size.height;
rect.size.height -= eRect.size.height;
tRect = [textScrollView frame];
tRect.origin.y = 0.0;
[window setFrame:rect display:NO animate:NO];
[textScrollView setFrame:tRect];
[textScrollView display];
if([prefs boolForKey:@"Partorar"] == YES)
{
//NSLog(@"Using Par to rar");
if([[[task launchPath] lastPathComponent] isEqualTo:@"par"])
{
NSString *rarFile = [self _lookForCompanionRar];
//NSLog(@"RAR File is %@", rarFile);
if(rarFile)
{
[self examineFilenames:[NSArray arrayWithObject:rarFile]];
//[self processFile:rarFile toPath:[filePath stringByDeletingLastPathComponent]];
}
}
if([[[task launchPath] lastPathComponent] isEqualTo:@"par2"])
{
//NSLog(@"Hello");
NSString *rarFile = [self _lookForCompanionRar];
//NSLog(@"RAR File is %@", rarFile);
if(rarFile)
{
[self examineFilenames:[NSArray arrayWithObject:rarFile]];
//[self processFile:rarFile toPath:[filePath stringByDeletingLastPathComponent]];
}
}
}
else
{
//[progressBar stopAnimation:self];
//NSLog(@"Not using Par to rar");
}
}
- (void)windowWillClose:(NSNotification *)aNotification
{
[NSApp terminate:self];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
return YES;
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
return [items objectForKey:itemIdentifier];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
NSMutableArray* identifiers = [NSMutableArray new];
[identifiers addObject:@"Filename"];
[identifiers addObject:NSToolbarFlexibleSpaceItemIdentifier];
[identifiers addObject:@"Password"];
[identifiers addObject:@"Test"];
[identifiers addObject:@"Browse"];
[identifiers addObject:@"Extract"];
//NSLog(@"Default Items");
return identifiers;
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
NSMutableArray* allowedItems = [NSMutableArray new];
[allowedItems addObject:@"Browse"];
[allowedItems addObject:@"Test"];
[allowedItems addObject:@"Info"];
[allowedItems addObject:@"Password"];
[allowedItems addObject:@"Extract"];
[allowedItems addObject:@"Filename"];
[allowedItems addObject:NSToolbarCustomizeToolbarItemIdentifier];
[allowedItems addObject:NSToolbarSeparatorItemIdentifier];
[allowedItems addObject:NSToolbarSpaceItemIdentifier];
[allowedItems addObject:NSToolbarFlexibleSpaceItemIdentifier];
//NSLog(@"Allowed Items");
return allowedItems;
}
- (int)count {
return [items count];
}
- (IBAction)customize:(id)sender {
[toolbar runCustomizationPalette:sender];
}
- (IBAction)showhide:(id)sender {
[toolbar setVisible:![toolbar isVisible]];
}
- (IBAction)openPrefsSheet:(id)sender
{
[NSApp beginSheet:prefsWindow
modalForWindow:window
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];
[NSApp runModalForWindow:prefsWindow];
[NSApp endSheet:prefsWindow];
[prefsWindow orderOut:self];
}
- (IBAction)closePrefsSheet:(id)sender
{
//[self setPrefs:nil];
[NSApp stopModal];
}
- (IBAction)setPrefs:(id)sender
{
if([overwriteCB state] == NSOnState)
{
[prefs setObject:@"YES" forKey:@"Overwrite"];
}
else if([overwriteCB state] == NSOffState)
{
[prefs setObject:@"NO" forKey:@"Overwrite"];
}
if([recurseCB state] == NSOnState)
{
[prefs setObject:@"YES" forKey:@"Recurse"];
}
else if([recurseCB state] == NSOffState)
{
[prefs setObject:@"NO" forKey:@"Recurse"];
}
if([assumeCB state] == NSOnState)
{
[prefs setObject:@"YES" forKey:@"Assume Yes"];
}
else if([assumeCB state] == NSOffState)
{
[prefs setObject:@"NO" forKey:@"Assume Yes"];
}
if([keepCB state] == NSOnState)
{
[prefs setObject:@"YES" forKey:@"Keep Broken"];
}
else if([keepCB state] == NSOffState)
{
[prefs setObject:@"NO" forKey:@"Keep Broken"];
}
if([partorarCB state] == NSOnState)
{
[prefs setObject:@"YES" forKey:@"Partorar"];
}
else if([partorarCB state] == NSOffState)
{
[prefs setObject:@"NO" forKey:@"Partorar"];
}
}
- (void)saveData
{
//NSLog(@"Saving Data");
[prefs setObject:locations forKey:@"Locations"];
[prefs synchronize];
}
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)name;
{
[self checkPrefs];
[self examineFilenames:[NSArray arrayWithObject:name]];
return YES;
}
- (IBAction)showAboutBox:(id)sender
{
[[AboutBox sharedInstance] showPanel:sender];
}
- (IBAction)openLocationsSheet:(id)sender
{
[NSApp beginSheet:locationWindow
modalForWindow:window
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];
[NSApp runModalForWindow:locationWindow];
[NSApp endSheet:locationWindow];
[locationWindow orderOut:self];
}
- (IBAction)closeLocationsSheet:(id)sender
{
[NSApp stopModal];
}
- (NSDictionary *)createRecord
{
NSMutableDictionary *location = [[NSMutableDictionary alloc] init];