-
Notifications
You must be signed in to change notification settings - Fork 1
/
appControl.m
1485 lines (1394 loc) · 59.8 KB
/
appControl.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
#import "appControl.h"
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#define NSAppKitVersionNumber10_1 620
#define NSAppKitVersionNumber10_2 663
#define kFixedDockMenuAppKitVersion 632
static NSString *JHDAutoQuit = @"Auto Quit On Close";
static NSString *JHDDockDefault = @"Default Dock Application";
static NSString *JHDSwitchArray = @"Switch Array";
static NSString *JHDSwitchApp = @"Switchable Application";
static NSString *JHDAppFiles = @"Switchable Application's Files";
static NSString *JHDSwitchFile = @"Switchable File";
static NSString *JHDSwitchSelected = @"Switchable Item Selected";
static NSString *JHDSymLinkUse = @"Use Symbolic Links";
@implementation appControl
+ (void)initialize
{
//Create a dictionary
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
// Put defaults in the dictionary
[defaultValues setObject:[NSNumber numberWithInt:0]
forKey:JHDAutoQuit];
[defaultValues setObject:[NSNumber numberWithInt:0]
forKey:JHDSymLinkUse];
[defaultValues setObject:[NSString stringWithString:@"Mail.app"]
forKey:JHDDockDefault];
[defaultValues setObject: [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"Mail.app", [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Preferences/com.apple.mail.plist",
[NSNumber numberWithInt:1], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Mail",
[NSNumber numberWithInt:1], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Mail Downloads",
[NSNumber numberWithInt:0], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]], nil],
[NSNumber numberWithInt:-1], nil]
forKeys:[NSMutableArray arrayWithObjects:JHDSwitchApp, JHDAppFiles, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"Address Book.app", [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Preferences/com.apple.AddressBook.plist",
[NSNumber numberWithInt:0], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Application Support/AddressBook",
[NSNumber numberWithInt:0], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Address Book Plug-Ins",
[NSNumber numberWithInt:0], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]], nil],
[NSNumber numberWithInt:0], nil]
forKeys:[NSMutableArray arrayWithObjects:JHDSwitchApp, JHDAppFiles, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"iCal.app", [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Preferences/com.apple.iCal.plist",
[NSNumber numberWithInt:0], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]],
[NSMutableDictionary dictionaryWithObjects:[NSMutableArray arrayWithObjects: @"~/Library/Application Support/iCal",
[NSNumber numberWithInt:0], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]], nil],
[NSNumber numberWithInt:0], nil]
forKeys:[NSMutableArray arrayWithObjects:JHDSwitchApp, JHDAppFiles, JHDSwitchSelected, nil]],
nil] forKey:JHDSwitchArray];
// Register the dictionary defaults
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
}
- (id)init
{
NSDictionary *tempAppDict;
NSArray *tempFileArr;
NSDictionary *tempFileDict;
NSMutableArray *mutFileArr;
int i, j;
if (self = [super init]) {
users = [[NSMutableArray alloc] init];
switches = [[NSMutableArray alloc] init];
for (i = 0; i < [[[NSUserDefaults standardUserDefaults] arrayForKey:JHDSwitchArray] count]; i++) {
tempAppDict = [[[NSUserDefaults standardUserDefaults] arrayForKey:JHDSwitchArray] objectAtIndex:i];
[switches addObject:[NSMutableDictionary dictionaryWithDictionary:tempAppDict]];
tempFileArr = [[switches objectAtIndex:i] objectForKey:JHDAppFiles];
mutFileArr = [[NSMutableArray alloc] init];
for (j = 0; j < [tempFileArr count]; j++) {
tempFileDict = [tempFileArr objectAtIndex:j];
[mutFileArr addObject:[tempFileDict mutableCopy]];
}
[[switches objectAtIndex:i] setObject:mutFileArr forKey:JHDAppFiles];
[mutFileArr release];
}
// NSLog(@"switches has class %@ and is \n%@", [switches className], [switches description]);
fileMan = [NSFileManager defaultManager];
fileManError = [[NSString alloc] initWithString:@""];
miniSwitchPath = [[NSString alloc] initWithString:[[NSHomeDirectory()
stringByAppendingPathComponent:@"Library"]
stringByAppendingPathComponent:@"MiniSwitch"]];
dockUserMenu = [[NSMenu alloc] initWithTitle:@"DockMenu"];
[dockUserMenu setAutoenablesItems:NO];
[dockPopUp setAutoenablesItems:NO];
runningApps = [[NSMutableArray alloc] init];
relaunchApps = [[NSMutableArray alloc] init];
// toolbar = [[NSToolbar alloc] initWithIdentifier:@"msToolbar"];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(switchAppDidQuit:)
name:NSWorkspaceDidTerminateApplicationNotification object:nil];
currentUser = nil;
}
return self;
}
- (void) dealloc {
[users release];
[switches release];
[miniSwitchPath release];
[dockUserMenu release];
[runningApps release];
[relaunchApps release];
// [toolbar release];
[fileManError release];
[super dealloc];
}
- (void)awakeFromNib
{
int row;
[self prepMSDir];
/* [toolbar setDelegate:self];
[toolbar setAllowsUserCustomization:YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
[miniSwitchWindow setToolbar:toolbar]; */
// prepare prefWindow
[autoQuitCB setState:[[NSUserDefaults standardUserDefaults] boolForKey:JHDAutoQuit]];
[symlinkCB setState:[[NSUserDefaults standardUserDefaults] boolForKey:JHDSymLinkUse]];
row = [users indexOfObject:currentUser];
if (row != NSNotFound) {
[userList selectRow:row byExtendingSelection:NO];
}
[addSwitchPU setAutoenablesItems:YES];
[switchableFiles setAutosaveExpandedItems:YES];
[[[switchableFiles tableColumnWithIdentifier:@"selects"] dataCell] setAllowsMixedState:YES];
[self updateUserWinBtnEnables];
[switchableFiles reloadData];
[self refreshDockPopUp];
// set tooltips
[addUserBtn setToolTip:NSLocalizedString(@"addUserTT", nil)];
[delUserBtn setToolTip:NSLocalizedString(@"delUserTT", nil)];
[switchUserBtn setToolTip:NSLocalizedString(@"switchUserTT", nil)];
[delSwitchBtn setToolTip:NSLocalizedString(@"delSwitchTT", nil)];
}
- (BOOL)validateMenuItem:(NSMenuItem *)anItem // DONE
{
// NSLog(@"anItem selector = %@", NSStringFromSelector([anItem action]));
if ([@"newUser:" isEqualToString:NSStringFromSelector([anItem action])]) {
return TRUE;
} else if ([@"deleteUsers:" isEqualToString:NSStringFromSelector([anItem action])]) {
if ([userList numberOfSelectedRows] >= 1) {
return TRUE;
} else return FALSE;
} else if ([@"switchAndLaunch:" isEqualToString:NSStringFromSelector([anItem action])]) {
if ([userList numberOfSelectedRows] == 1) {
if (([anItem tag] == -1) && ([users indexOfObject:currentUser] == [userList selectedRow])) return FALSE;
return TRUE;
} else return FALSE;
} else if ([@"showHelp:" isEqualToString:NSStringFromSelector([anItem action])]) {
return TRUE;
} else if ([@"addSwApp:" isEqualToString:NSStringFromSelector([anItem action])]) {
return TRUE;
} else if ([@"AddSwFiles:" isEqualToString:NSStringFromSelector([anItem action])]) {
if (([switchableFiles numberOfSelectedRows] == 1) &&
([[switchableFiles itemAtRow:[switchableFiles selectedRow]] objectForKey:JHDAppFiles] != nil)) {
return TRUE;
} else return FALSE;
} else if ([@"showMiniSwitchWindow:" isEqualToString:NSStringFromSelector([anItem action])]) {
return TRUE;
} else if ([@"openFromDock:" isEqualToString:NSStringFromSelector([anItem action])]) {
return TRUE;
} else if ([@"updateDockDefault:" isEqualToString:NSStringFromSelector([anItem action])]) {
return TRUE;
}
return [super validateMenuItem:anItem];
}
- (void)refreshDockPopUp { // DONE
int i;
[dockPopUp removeAllItems];
for (i = [cAppMenu numberOfItems] - 1; i > 1; i--) {
[cAppMenu removeItemAtIndex:i];
}
for (i = [mAppMenu numberOfItems] - 1; i > 1; i--) {
[mAppMenu removeItemAtIndex:i];
}
for (i = [wAppMenu numberOfItems] - 1; i > 2; i--) {
[wAppMenu removeItemAtIndex:i];
}
for (i = 0; i < [switches count]; i++) {
if ([[[switches objectAtIndex:i] objectForKey:JHDSwitchSelected] boolValue]) {
[dockPopUp addItemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]];
[cAppMenu addItemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]
action:@selector(switchAndLaunch:) keyEquivalent:@""];
[[cAppMenu itemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]] setTag:0];
[mAppMenu addItemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]
action:@selector(switchAndLaunch:) keyEquivalent:[NSString stringWithFormat:@"%d", (i + 1)]];
[[mAppMenu itemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]] setTag:0];
[[mAppMenu itemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]]
setKeyEquivalentModifierMask:NSCommandKeyMask];
[wAppMenu addItemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]
action:@selector(switchAndLaunch:) keyEquivalent:@""];
[[wAppMenu itemWithTitle:[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]] setTag:0];
// [toolbar insertItemWithItemIdentifier:[[dockPopUp itemAtIndex:i] title] atIndex:0];
}
}
if ([dockPopUp numberOfItems] == 0) {
[dockPopUp addItemWithTitle:NSLocalizedString(@"none", nil)];
[dockPopUp selectItemAtIndex:0];
[dockPopUp setEnabled:FALSE];
} else {
[dockPopUp setEnabled:TRUE];
if ([dockPopUp indexOfItemWithTitle:[[NSUserDefaults standardUserDefaults] stringForKey:JHDDockDefault]] == nil) {
[dockPopUp selectItemAtIndex:0];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:[dockPopUp titleOfSelectedItem]]
forKey:JHDDockDefault];
} else {
[dockPopUp selectItemWithTitle:[[NSUserDefaults standardUserDefaults] stringForKey:JHDDockDefault]];
}
}
}
- (void)prepMSDir // DONE
{
NSString *mailSwPath = [[NSString alloc] initWithString:[[NSHomeDirectory()
stringByAppendingPathComponent:@"Library"]
stringByAppendingPathComponent:@"MailSwitch"]];
BOOL firstRun = FALSE;
BOOL isDir;
int i, uCnt;
NSArray *msContent;
NSMutableArray *updateLog = [NSMutableArray array];
BOOL errorOut = FALSE;
NSString *userPath;
NSString *userLibPath;
NSString *userPrefPath;
NSString *userAppSupPath;
// verify MiniSwitch and currentUser exist
if ([fileMan fileExistsAtPath: miniSwitchPath]) {
// Set users to folders in miniSwitchPath
[self reloadUsers];
} else {
// see if old MailSwitch user
if ([fileMan fileExistsAtPath: mailSwPath isDirectory:&isDir] && isDir) {
// prompt user must change
if (NSRunAlertPanel(NSLocalizedString(@"mailSwfoundT", nil),
NSLocalizedString(@"mailSwfoundM", nil),
NSLocalizedString(@"okay", nil),
NSLocalizedString(@"quit", nil), nil) == NSAlertAlternateReturn) {
[NSApp terminate:self];
return;
} else {
// rename MailSwPath to miniSwitchPath
if (![fileMan movePath:mailSwPath toPath:miniSwitchPath handler:self]) {
NSRunCriticalAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"movePathFail", nil), fileManError],
NSLocalizedString(@"quit", nil),
nil, nil);
[NSApp terminate:self];
return;
}
[updateLog addObject:NSLocalizedString(@"updateRenameSuc", nil)];
// move files to appropriately added folders
msContent = [fileMan directoryContentsAtPath: miniSwitchPath];
uCnt = [msContent count];
for (i = 0; ((i < uCnt) && (!errorOut)); i++) {
userPath = [miniSwitchPath stringByAppendingPathComponent:
[msContent objectAtIndex: i]];
if ([fileMan fileExistsAtPath:userPath isDirectory:&isDir] && isDir) {
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogStartUser", nil),
[userPath lastPathComponent]]];
// add Library folder
userLibPath = [userPath stringByAppendingPathComponent:@"Library"];
if ((![fileMan fileExistsAtPath:userLibPath isDirectory:&isDir]) &&
(![fileMan createDirectoryAtPath:userLibPath attributes:nil])) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDirFail", nil),
userLibPath]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
// move Mail, if exists
if ((!errorOut) && ([fileMan fileExistsAtPath:[userPath stringByAppendingPathComponent:@"Mail"]]) &&
(![fileMan movePath:[userPath stringByAppendingPathComponent:@"Mail"]
toPath:[userLibPath stringByAppendingPathComponent:@"Mail"] handler:self])) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogMoveFail", nil),
[userPath stringByAppendingPathComponent:@"Mail"], [userLibPath stringByAppendingPathComponent:@"Mail"]]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
// add Preferences folder
userPrefPath = [userLibPath stringByAppendingPathComponent:@"Preferences"];
if ((!errorOut) && (![fileMan fileExistsAtPath:userPrefPath isDirectory:&isDir]) &&
(![fileMan createDirectoryAtPath:userPrefPath attributes:nil])) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDirFail", nil),
userPrefPath]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
// move Mail Pref, if exists
if ((!errorOut) && ([fileMan fileExistsAtPath:[userPath stringByAppendingPathComponent:@"com.apple.mail.plist"]])) {
if (![fileMan movePath:[userPath stringByAppendingPathComponent:@"com.apple.mail.plist"]
toPath:[userPrefPath stringByAppendingPathComponent:@"com.apple.mail.plist"] handler:self]) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogMoveFail", nil),
[userPath stringByAppendingPathComponent:@"com.apple.mail.plist"],
[userPrefPath stringByAppendingPathComponent:@"com.apple.mail.plist"]]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
}
// move AddressBook Pref, if exists
if ((!errorOut) && ([fileMan fileExistsAtPath:[userPath stringByAppendingPathComponent:@"com.apple.AddressBook.plist"]])) {
if (![fileMan movePath:[userPath stringByAppendingPathComponent:@"com.apple.AddressBook.plist"]
toPath:[userPrefPath stringByAppendingPathComponent:@"com.apple.AddressBook.plist"] handler:self]) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogMoveFail", nil),
[userPath stringByAppendingPathComponent:@"com.apple.AddressBook.plist"],
[userPrefPath stringByAppendingPathComponent:@"com.apple.AddressBook.plist"]]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
}
// move Addresses, if exists
if ((!errorOut) && ([fileMan fileExistsAtPath:[userPath stringByAppendingPathComponent:@"Addresses"]]) &&
(![fileMan movePath:[userPath stringByAppendingPathComponent:@"Addresses"]
toPath:[userLibPath stringByAppendingPathComponent:@"Addresses"] handler:self])) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogMoveFail", nil),
[userPath stringByAppendingPathComponent:@"Addresses"],
[userLibPath stringByAppendingPathComponent:@"Addresses"]]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
// move AddressBook, if exists
if ((!errorOut) && ([fileMan fileExistsAtPath:[userPath stringByAppendingPathComponent:@"AddressBook"]])) {
// add Application Support folder
userAppSupPath = [userLibPath stringByAppendingPathComponent:@"Application Support"];
if ((![fileMan fileExistsAtPath:userAppSupPath isDirectory:&isDir]) &&
(![fileMan createDirectoryAtPath:userAppSupPath attributes:nil])) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDirFail", nil),
userAppSupPath]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
if (![fileMan movePath:[userPath stringByAppendingPathComponent:@"AddressBook"]
toPath:[userAppSupPath stringByAppendingPathComponent:@"AddressBook"] handler:self]) {
// error
[updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogMoveFail", nil),
[userPath stringByAppendingPathComponent:@"AddressBook"],
[userAppSupPath stringByAppendingPathComponent:@"AddressBook"]]];
[updateLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
} //*/
}
if (!errorOut) [updateLog addObject:[NSString stringWithFormat:NSLocalizedString(@"updateLogFinUser", nil),
[userPath lastPathComponent]]];
}
}
if (errorOut) {
NSBeep();
NSRunCriticalAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"updateUserFail", nil), [updateLog componentsJoinedByString:@"\n\t"]],
NSLocalizedString(@"quit", nil),
nil, nil);
[NSApp terminate:self];
return;
}
[self reloadUsers];
firstRun = TRUE;
}
} else {
if (![fileMan createDirectoryAtPath: miniSwitchPath attributes:nil]) {
NSRunCriticalAlertPanel(NSLocalizedString(@"msErrorT", nil),
NSLocalizedString(@"createPathFail", nil),
NSLocalizedString(@"quit", nil),
nil, nil);
[NSApp terminate:self];
return;
}
[self newUser:self];
firstRun = TRUE;
}
}
if (firstRun) {
[prefWindow makeKeyAndOrderFront:self];
if (NSRunAlertPanel(NSLocalizedString(@"welcomeT", nil),
NSLocalizedString(@"welcomeM", nil),
NSLocalizedString(@"okay", nil),
NSLocalizedString(@"moreInfo", nil), nil) == NSAlertAlternateReturn)
NSRunAlertPanel(NSLocalizedString(@"explanationT", nil),
NSLocalizedString(@"explanationM", nil),
NSLocalizedString(@"close", nil),
nil, nil);
}
}
- (void)reloadUsers // DONE
{
BOOL isDir;
int i;
NSString *userFullPath;
NSArray *msContent = [fileMan directoryContentsAtPath: miniSwitchPath];
[users removeAllObjects];
for (i = 0; i < [msContent count]; i++) {
userFullPath = [miniSwitchPath stringByAppendingPathComponent:
[msContent objectAtIndex: i]];
if ([fileMan fileExistsAtPath:userFullPath isDirectory:&isDir] && isDir) {
[users addObject: userFullPath];
}
}
[userList reloadData];
[self readinCurrentUser];
// Clear out old dockUserMenu
for (i = [dockUserMenu numberOfItems]; i > 0; i--) {
if (NSAppKitVersionNumber < kFixedDockMenuAppKitVersion) {
[[[dockUserMenu itemAtIndex:0] target] release];
}
[dockUserMenu removeItemAtIndex:0];
}
// (re)build the dock Menu
[dockUserMenu insertItem:[self createItem:NSLocalizedString(@"userwindow", nil)
action:@selector(showMiniSwitchWindow:)] atIndex:0];
[dockUserMenu insertItem:[NSMenuItem separatorItem] atIndex:0];
for (i = ([users count] - 1); i >= 0; i--) {
[dockUserMenu insertItem:[self createItem:((NSString *)[[users objectAtIndex: i]
lastPathComponent]) action:@selector(openFromDock:)] atIndex:0];
}
}
- (void)readinCurrentUser // DONE?
{
NSString *cuPath;
int i;
if (currentUser != nil) {
cuPath = [currentUser stringByAppendingPathComponent:@"Current User"];
if (![fileMan fileExistsAtPath:cuPath]) {
[currentUser release];
currentUser = nil;
}
}
for (i = 0; i < [users count]; i++) {
cuPath = [[users objectAtIndex:i] stringByAppendingPathComponent:@"Current User"];
if ([fileMan fileExistsAtPath:cuPath]) {
if (currentUser != nil) {
if (![[users objectAtIndex:i] isEqualToString:currentUser]) {
// dispose of extra CUPs
if (![fileMan removeFileAtPath:cuPath handler:self]) {
// error failed delete
NSRunAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"delOtherCUP", nil), fileManError],
NSLocalizedString(@"Okay", nil),
nil, nil);
}
}
} else {
currentUser = [[users objectAtIndex:i] copy];
}
}
}
if (currentUser == nil) {
// NSLog(@"Warning: Could not find a \"Current User.\"");
NSRunAlertPanel(NSLocalizedString(@"msWarningT", nil),
NSLocalizedString(@"findCUFail", nil),
NSLocalizedString(@"Okay", nil),
nil, nil);
[self newUser:self];
}
}
/*- (int)currentUserIndex // CHANGED TO [users indexOfObject:currentUser]
{
int i, index = -1;
//[self readinCurrentUser];
if (currentUser == nil) return -1;
for (i = 0; i < [users count]; i++) {
if ([currentUser isEqualToString: [users objectAtIndex:i]]) {
index = i;
}
}
return index;
} */
- (NSMenuItem *)createItem:(NSString *)name action:(SEL)aSelector
{
NSMenuItem *newItem;
if (NSAppKitVersionNumber>=kFixedDockMenuAppKitVersion)
{
newItem = [[[NSMenuItem alloc] initWithTitle:name action:aSelector
keyEquivalent:@""] autorelease];
[newItem setTarget:self];
[newItem setEnabled:YES];
}
else {//we're running on an OS version that isn't fixed; use NSInvocation
//This invocation is going to be of the form aSelector
NSInvocation *myInv=[NSInvocation invocationWithMethodSignature:[self
methodSignatureForSelector:aSelector]];
newItem=[[[NSMenuItem alloc] initWithTitle:name
action:@selector(invoke) keyEquivalent:@""] autorelease];
[myInv setSelector:aSelector];
[myInv setTarget:self];
[myInv setArgument:&newItem atIndex:2];
[newItem setTarget:[myInv retain]];
[newItem setEnabled:YES];
}
return newItem;
}
- (IBAction)switchAndLaunch:(id)sender // DONE?
{
NSArray *runningApplications;
NSDictionary *applInfo;
NSEnumerator *enumerator;
int i;
AEDesc addressDesc;
AppleEvent event, reply;
OSErr err;
pid_t aePid;
NSMutableArray *aeErrs = [NSMutableArray array];
[relaunchApps removeAllObjects];
if (sender == nil) {
// launch dock default
[relaunchApps addObject:[NSString stringWithString:[dockPopUp titleOfSelectedItem]]];
} else if ([sender tag] == 1) {
// launch all switchable applications
for (i = 0; i < [dockPopUp numberOfItems]; i++) {
[relaunchApps addObject:[NSString stringWithString:[[dockPopUp itemAtIndex:i] title]]];
}
} else if ([sender tag] == 0) {
// launch specific application
[relaunchApps addObject:[NSString stringWithString:[sender title]]];
} else if ([sender tag] == -2) {
[relaunchApps addObject:[NSString stringWithString:[sender itemIdentifier]]];
} // no relaunch
if ([users indexOfObject:currentUser] != [userList selectedRow]) {
// prep runningApps for quiting
[runningApps removeAllObjects];
for (i = 0; i < [dockPopUp numberOfItems]; i++) {
[runningApps addObject:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString
stringWithString:[[dockPopUp itemAtIndex:i] title]], [NSNumber numberWithInt:-1], nil]
forKeys:[NSArray arrayWithObjects: @"appName", @"pid", nil]]];
}
runningApplications = [[NSWorkspace sharedWorkspace] launchedApplications];
enumerator = [runningApplications objectEnumerator];
while(applInfo = [enumerator nextObject]) {
for (i = 0; i < [runningApps count]; i++) {
if([[[applInfo objectForKey:@"NSApplicationPath"] lastPathComponent] isEqualToString:[[runningApps objectAtIndex:i]
objectForKey:@"appName"]]) {
[[runningApps objectAtIndex:i] setObject:[applInfo objectForKey:@"NSApplicationProcessIdentifier"]
forKey:@"pid"];
}
}
}
for (i = [runningApps count] - 1; i >= 0; i--) {
if([[[runningApps objectAtIndex:i] objectForKey:@"pid"] intValue] == -1) {
[runningApps removeObjectAtIndex:i];
}
}
if ([runningApps count] > 0) {
// start quitTimer
quitTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(quitTimedOut:) userInfo:nil repeats:NO];
for (i = 0; i < [runningApps count]; i++) {
aePid = [[[runningApps objectAtIndex:i] objectForKey:@"pid"] intValue];
err = AECreateDesc(typeKernelProcessID, &aePid, sizeof(pid_t), &addressDesc);
if (err == noErr) {
err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &addressDesc,
kAutoGenerateReturnID, kAnyTransactionID, &event);
}
if (err == noErr) {
// err = AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
err = AESendMessage(&event, &reply, kAENoReply, kAEDefaultTimeout);
}
// AESend(<#const AppleEvent * theAppleEvent#>,<#AppleEvent * reply#>,<#AESendMode sendMode#>,<#AESendPriority sendPriority#>,<#long timeOutInTicks#>,<#AEIdleUPP idleProc#>,<#AEFilterUPP filterProc#>)
// AESendMessage(<#const AppleEvent * event#>,<#AppleEvent * reply#>,<#AESendMode sendMode#>,<#long timeOutInTicks#>)
if (err != noErr) {
[aeErrs addObject:[NSString stringWithFormat:NSLocalizedString(@"aeErrFail", nil), aePid, err]];
}
}
// if errors cancel timer
if ([aeErrs count] > 0) {
[quitTimer invalidate];
// notify user
NSRunAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"quitAppFail", nil), [aeErrs componentsJoinedByString:@"\n\t"]],
NSLocalizedString(@"cancel", nil),
nil, nil);
}
} else [self switchUser];
} else [self launchRelaunchApps];
}
- (void)switchAppDidQuit:(NSNotification *)aNotification // DONE
{
NSDictionary *userDict;
NSString *name;
int i;
if ([runningApps count] > 0) {
userDict = [aNotification userInfo];
name = [[userDict objectForKey:@"NSApplicationPath"] lastPathComponent];
for (i = [runningApps count] - 1; i >= 0; i--) {
if([name isEqualToString:[[runningApps objectAtIndex:i] objectForKey:@"appName"]]) {
[runningApps removeObjectAtIndex:i];
}
}
if (([runningApps count] == 0) && ([quitTimer isValid])) {
[quitTimer invalidate];
[self switchUser];
}
}
}
- (void)quitTimedOut:(NSTimer*)theTimer{
NSMutableArray *apps = [NSMutableArray array];
int i;
if ([runningApps count] > 0) {
for (i = 0; i < [runningApps count]; i++) {
[apps addObject:[[runningApps objectAtIndex:i] objectForKey:@"appName"]];
}
// error: following applications did not quit.
NSRunAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"quitAppTimeOut", nil), [apps componentsJoinedByString:@", "]],
NSLocalizedString(@"cancel", nil),
nil, nil);
[quitTimer invalidate];
[runningApps removeAllObjects];
}
}
- (void)switchUser // DONE
{
int i, j;
BOOL errorOut = FALSE;
BOOL isDir;
NSMutableArray *switchFiles = [NSMutableArray array];
NSMutableArray *moveLog = [NSMutableArray array];
NSMutableArray *pathArray = [NSMutableArray array];
NSString *destPath;
NSString *srcPath;
NSMutableString *dirPath;
// struct stat linkstat;
// if not current user
if ([users indexOfObject:currentUser] != [userList selectedRow]) {
// build list of files to switch
for (i = 0; i < [switches count]; i++) {
for (j = 0; j < [[[switches objectAtIndex:i] objectForKey:JHDAppFiles] count]; j++) {
if ([[[[[switches objectAtIndex:i] objectForKey:JHDAppFiles] objectAtIndex:j]
objectForKey:JHDSwitchSelected] intValue] == 1) {
[switchFiles addObject:[[[[[switches objectAtIndex:i] objectForKey:JHDAppFiles]
objectAtIndex:j] objectForKey:JHDSwitchFile] copy]];
}
}
}
// put back current users files
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogRetCUStart", nil),
[currentUser lastPathComponent]]];
for (i = 0; ((i < [switchFiles count]) && (!errorOut)); i++) {
srcPath = [[switchFiles objectAtIndex:i] stringByExpandingTildeInPath];
[pathArray removeAllObjects];
[pathArray addObjectsFromArray:[[switchFiles objectAtIndex:i] pathComponents]];
[pathArray removeObjectAtIndex:0];
destPath = [NSString stringWithString:[currentUser
stringByAppendingPathComponent:[pathArray componentsJoinedByString:@"/"]]];
if ([fileMan fileExistsAtPath:srcPath]) {
// if ((lstat([srcPath fileSystemRepresentation], &linkstat) != -1) && (S_ISLNK(linkstat.st_mode))) {
if ([[fileMan pathContentOfSymbolicLinkAtPath:srcPath] isEqualToString:destPath]) {
// delete symlink
if (![fileMan removeFileAtPath:srcPath handler:self]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDelSLFail", nil), srcPath]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
} else {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDelSLSuc", nil), srcPath]];
}
} else { // move files
if (![fileMan fileExistsAtPath:[destPath stringByDeletingLastPathComponent] isDirectory:&isDir]) {
dirPath = [NSMutableString stringWithString:currentUser];
for (j = 0; j < [pathArray count] - 1; j++) {
[dirPath appendFormat:@"/%@", [pathArray objectAtIndex:j]];
if (![fileMan fileExistsAtPath:dirPath isDirectory:&isDir]) {
if (![fileMan createDirectoryAtPath:dirPath attributes:nil]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDirFail", nil), dirPath]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
}
}
}
if (!errorOut) {
if (![fileMan movePath:srcPath toPath:destPath handler:self]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogRetCUFileFail", nil), srcPath]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
} else {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogRetCUFileSuc", nil), srcPath]];
}
}
}
}
}
if (!errorOut) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogRetCUFin", nil),
[currentUser lastPathComponent]]];
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogPutNewStart", nil),
[[users objectAtIndex:[userList selectedRow]] lastPathComponent]]];
}
// place new users files
for (i = 0; ((i < [switchFiles count]) && (!errorOut)); i++) {
destPath = [[switchFiles objectAtIndex:i] stringByExpandingTildeInPath];
[pathArray removeAllObjects];
[pathArray addObjectsFromArray:[[switchFiles objectAtIndex:i] pathComponents]];
[pathArray removeObjectAtIndex:0];
srcPath = [NSString stringWithString:[[users objectAtIndex:[userList selectedRow]]
stringByAppendingPathComponent:[pathArray componentsJoinedByString:@"/"]]];
if ([fileMan fileExistsAtPath:srcPath isDirectory:&isDir]) {
if (([symlinkCB state]) && isDir) {
// create symlink for folders
if (![fileMan createSymbolicLinkAtPath:destPath pathContent:srcPath]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogCreSLFail", nil), srcPath]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
} else {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogCreSLSuc", nil), srcPath]];
}
} else { // move file
if (![fileMan fileExistsAtPath:[destPath stringByDeletingLastPathComponent] isDirectory:&isDir]) {
dirPath = [NSMutableString stringWithString:NSHomeDirectory()];
for (j = 0; j < [pathArray count] - 1; j++) {
[dirPath appendFormat:@"/%@", [pathArray objectAtIndex:j]];
if (![fileMan fileExistsAtPath:dirPath isDirectory:&isDir]) {
if (![fileMan createDirectoryAtPath:dirPath attributes:nil]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDirFail", nil), dirPath]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
}
}
}
if (!errorOut) {
if (![fileMan movePath:srcPath toPath:destPath handler:self]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogPutNewFileFail", nil), srcPath]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
} else {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogPutNewFileSuc", nil), srcPath]];
}
}
}
}
}
}
if (!errorOut) {
//- change CUP to new user
if (![fileMan removeFileAtPath:[currentUser stringByAppendingPathComponent:@"Current User"] handler:self]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogDelCUFail", nil), [currentUser lastPathComponent]]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
if (![fileMan createFileAtPath:[[users objectAtIndex:[userList selectedRow]]
stringByAppendingPathComponent:@"Current User"] contents:nil attributes:nil]) {
[moveLog addObject:[NSString stringWithFormat:NSLocalizedString(@"moveLogCreateNewCUFail", nil),
[[users objectAtIndex:[userList selectedRow]] lastPathComponent]]];
[moveLog addObject:[NSString stringWithString:fileManError]];
errorOut = TRUE;
}
}
if (!errorOut) {
[self readinCurrentUser];
[self updateUserWinBtnEnables];
[self launchRelaunchApps];
[userList reloadData];
} else {
// notify user of error
NSBeep();
NSRunCriticalAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"switchUserFail", nil), [moveLog componentsJoinedByString:@"\n\t"]],
NSLocalizedString(@"cancel", nil),
nil,
nil);
}
}
- (void)launchRelaunchApps // DONE
{
int i, j;
BOOL appLaunched = FALSE;
for (i = 0; (i < [relaunchApps count]); i++) {
for (j = 0; (!appLaunched && (j < 5)); j++) {
appLaunched = [[NSWorkspace sharedWorkspace] launchApplication:[relaunchApps objectAtIndex:i]];
}
if (!appLaunched) {
// failed to launch
NSBeep();
NSRunAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"launchAppFail", nil), [relaunchApps objectAtIndex:i],
[relaunchApps objectAtIndex:i]], NSLocalizedString(@"okay", nil),
nil, nil);
} else appLaunched = FALSE;
}
}
-(BOOL)fileManager:(NSFileManager *)manager shouldProceedAfterError:(NSDictionary *)errorDict
{
[fileManError release];
fileManError = [[NSString alloc] initWithString:[errorDict description]];
return NO;
}
- (IBAction)addSwApp:(id)sender // DONE
{
BOOL i, appExists = 0;
NSOpenPanel *selAppPanel = [NSOpenPanel openPanel];
[selAppPanel setPrompt:NSLocalizedString(@"select", nil)];
[selAppPanel setTitle:NSLocalizedString(@"selAppT", nil)];
[selAppPanel setAllowsMultipleSelection:NO];
if ([selAppPanel runModalForDirectory:@"/Applications" file:nil types:[NSArray
arrayWithObjects:@"app", NSFileTypeForHFSTypeCode('APPL'), nil]] == NSOKButton) {
for (i = 0; i < [switches count]; i++) {
if ([[[switches objectAtIndex:i] objectForKey:JHDSwitchApp]
isEqualToString:[[[selAppPanel filenames] objectAtIndex:0] lastPathComponent]]) appExists = 1;
}
if (appExists) {
NSRunAlertPanel(NSLocalizedString(@"appExistsT", nil),
[NSString stringWithFormat: NSLocalizedString(@"appExistsM", nil),
[[[selAppPanel filenames] objectAtIndex:0] lastPathComponent]],
NSLocalizedString(@"okay", nil), nil, nil);
} else {
[switches addObject:[NSMutableDictionary dictionaryWithObjects:[NSMutableArray
arrayWithObjects:[[[selAppPanel filenames] objectAtIndex:0] lastPathComponent], [NSMutableArray array],
[NSNumber numberWithInt:0], nil]
forKeys:[NSMutableArray arrayWithObjects:JHDSwitchApp, JHDAppFiles, JHDSwitchSelected, nil]]];
[switchableFiles reloadData];
[[NSUserDefaults standardUserDefaults] setObject:switches forKey:JHDSwitchArray];
}
}
}
- (IBAction)AddSwFiles:(id)sender
{
BOOL filesSkipped = FALSE;
int i, j, cnt;
NSOpenPanel *selFilesPanel = [NSOpenPanel openPanel];
NSMutableArray *swFilesAll = [NSMutableArray array];
[selFilesPanel setPrompt:NSLocalizedString(@"select", nil)];
[selFilesPanel setTitle:[NSString stringWithFormat:NSLocalizedString(@"selFilesT", nil),
[[switchableFiles itemAtRow:[switchableFiles selectedRow]] objectForKey:JHDSwitchApp]]];
[selFilesPanel setAllowsMultipleSelection:YES];
[selFilesPanel setCanChooseDirectories:YES];
if ([selFilesPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil] == NSOKButton) {
for (i = 0; i < [switches count]; i++) {
for (j = 0; j < [[[switches objectAtIndex:i] objectForKey:JHDAppFiles] count]; j++) {
[swFilesAll addObject:[[[[switches objectAtIndex:i] objectForKey:JHDAppFiles]
objectAtIndex:j] objectForKey:JHDSwitchFile]];
}
}
cnt = [[selFilesPanel filenames] count];
for (i = 0; i < cnt; i++) {
if ([swFilesAll indexOfObject:[[[selFilesPanel filenames] objectAtIndex:i]
stringByAbbreviatingWithTildeInPath]] != NSNotFound) {
filesSkipped = TRUE;
} else {
[[[switchableFiles itemAtRow:[switchableFiles selectedRow]] objectForKey:JHDAppFiles]
addObject:[NSMutableDictionary dictionaryWithObjects:[NSMutableArray
arrayWithObjects: [[[selFilesPanel filenames] objectAtIndex:i] stringByAbbreviatingWithTildeInPath],
[NSNumber numberWithInt:1], nil] forKeys:[NSMutableArray
arrayWithObjects:JHDSwitchFile, JHDSwitchSelected, nil]]];
}
}
[self updateAppSwitchSelected];
[switchableFiles reloadData];
[self refreshDockPopUp];
[[NSUserDefaults standardUserDefaults] setObject:switches forKey:JHDSwitchArray];
if (filesSkipped) {
NSRunAlertPanel(NSLocalizedString(@"fileExistsT", nil),
NSLocalizedString(@"fileExistsM", nil), NSLocalizedString(@"okay", nil), nil, nil);
}
}
}
- (IBAction)deleteSelectedSw:(id)sender
{
NSIndexSet *iSet;
BOOL removedSW;
int i, j;
if (NSRunAlertPanel(NSLocalizedString(@"delSwT", nil), NSLocalizedString(@"delSwM", nil),
NSLocalizedString(@"okay", nil), NSLocalizedString(@"cancel", nil), nil) == NSAlertDefaultReturn) {
iSet = [switchableFiles selectedRowIndexes];
i = [iSet lastIndex];
while (i != NSNotFound) {
removedSW = FALSE;
if ([[switchableFiles itemAtRow:i] objectForKey:JHDAppFiles] != nil) {
// remove application
[switches removeObject:[switchableFiles itemAtRow:i]];
} else {
// remove file from application
for (j = [switches count] - 1; (j >= 0) && (!removedSW); j--) {
if ([[[switches objectAtIndex:j] objectForKey:JHDAppFiles]
indexOfObject:[switchableFiles itemAtRow:i]] != NSNotFound) {
//NSLog(@"%@", [[switchableFiles itemAtRow:i] description]);
[[[switches objectAtIndex:j] objectForKey:JHDAppFiles]
removeObject:[switchableFiles itemAtRow:i]];
removedSW = TRUE;
}
}
}
i = [iSet indexLessThanIndex:i];
}
[self updateAppSwitchSelected];
[switchableFiles reloadData];
[self refreshDockPopUp];
[[NSUserDefaults standardUserDefaults] setObject:switches forKey:JHDSwitchArray];
}
}
- (IBAction)deleteUsers:(id)sender // DONE
{
NSMutableArray *trashableUsers = [NSMutableArray array];
NSString *trashedUser;
NSIndexSet *iSet = [userList selectedRowIndexes];
int j, i = [iSet firstIndex];
// build trashableUsers
while (i != NSNotFound) {
if ([users indexOfObject:currentUser] == i) {
if ([iSet count] > 1) {
if (NSRunAlertPanel(NSLocalizedString(@"msWarningT", nil),
[NSString stringWithFormat:NSLocalizedString(@"delCUSelected1", nil), [[users objectAtIndex:i] lastPathComponent]],
NSLocalizedString(@"skip", nil), NSLocalizedString(@"cancel", nil), nil) == NSAlertAlternateReturn) return;
} else {
NSRunAlertPanel(NSLocalizedString(@"msWarningT", nil),
[NSString stringWithFormat:NSLocalizedString(@"delCUSelected2", nil), [[users objectAtIndex:i] lastPathComponent]],
NSLocalizedString(@"cancel", nil), nil, nil);
return;
}
} else {
[trashableUsers addObject:[users objectAtIndex:i]];
}
i = [iSet indexGreaterThanIndex:i];
}
if ([trashableUsers count] > 0) {
if (NSRunAlertPanel(NSLocalizedString(@"msWarningT", nil),
[NSString stringWithFormat:NSLocalizedString(@"delUsersConf", nil), [trashableUsers count]],
NSLocalizedString(@"continue", nil), NSLocalizedString(@"cancel", nil), nil) == NSAlertAlternateReturn) return;
// delete trashableUsers
for (i = 0; i < [trashableUsers count]; i++) {
trashedUser = [[NSHomeDirectory() stringByAppendingPathComponent:@".Trash"]
stringByAppendingPathComponent:[[trashableUsers objectAtIndex:i] lastPathComponent]];
j = 1;
while ([fileMan fileExistsAtPath:trashedUser]) {
trashedUser = [NSString stringWithFormat:@"%@ %d", [[NSHomeDirectory() stringByAppendingPathComponent:@".Trash"]
stringByAppendingPathComponent:[[trashableUsers objectAtIndex:i] lastPathComponent]], j];
j++;
}
if (![fileMan movePath:[trashableUsers objectAtIndex:i] toPath:trashedUser handler:self]) {
NSBeep();
NSRunAlertPanel(NSLocalizedString(@"msErrorT", nil),
[NSString stringWithFormat:NSLocalizedString(@"delUserFail",
nil), [[trashableUsers objectAtIndex:i] lastPathComponent], fileManError],
NSLocalizedString(@"okay", nil),
nil, nil);
}
}
}
[self updateUserWinBtnEnables];
[self reloadUsers];