-
Notifications
You must be signed in to change notification settings - Fork 41
/
AMServiceViewController.m
93 lines (74 loc) · 2.45 KB
/
AMServiceViewController.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
#import "AMServiceViewController.h"
@implementation AMServiceViewController
@synthesize services;
#pragma mark -
#pragma mark Initilisations
- (id) init
{
self = [super init];
return self;
}
- (void) awakeFromNib
{
NSFileManager *f = [NSFileManager defaultManager];
serviceSavePath = [[[NSUserDefaults standardUserDefaults] stringForKey:@"applicationSupportFolder"] stringByAppendingString:@"/services.sst"];
if ([f fileExistsAtPath:[serviceSavePath stringByExpandingTildeInPath]] == YES)
{
@try
{
[self setServices:[NSKeyedUnarchiver unarchiveObjectWithFile:[serviceSavePath stringByExpandingTildeInPath]]];
NSLog(@"Services state recovered");
}
@catch (NSException *e)
{
int rep = NSRunAlertPanel(@"Error while loading datas", @"SSHTunnel was unable to load its saved state. Would you like to revert to the factory presets ? ", @"Yes", @"No", nil);
if (rep == NSAlertDefaultReturn)
[[NSNotificationCenter defaultCenter] postNotificationName:AMErrorLoadingSavedState object:nil];
else
exit(0);
}
}
else
{
NSMutableArray *tmp = [[NSMutableArray alloc] init];
[self setServices:tmp];
}
f = nil;
[self createObservers];
}
- (void) createObservers
{
[serviceArrayController addObserver:self
forKeyPath:@"selection.serviceName"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:nil];
[serviceArrayController addObserver:self
forKeyPath:@"selection.servicePort"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:nil];
[serviceArrayController addObserver:self
forKeyPath:@"selection.serviceDescription"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:nil];
}
#pragma mark -
#pragma mark Obeservers and delegates
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self saveState];
}
#pragma mark -
#pragma mark Saveing processes
- (void) saveState
{
if (pingDelayer != nil)
[pingDelayer invalidate];
NSLog(@"Service saving processes programmed.");
pingDelayer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(performSaveProcess:) userInfo:nil repeats:NO];
}
- (void) performSaveProcess:(NSTimer *)theTimer
{
NSLog(@"Services status saved.");
[NSKeyedArchiver archiveRootObject:[self services] toFile:[serviceSavePath stringByExpandingTildeInPath]];
}
@end