forked from dstuecken/sshfs-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecentServersProvider.m
151 lines (106 loc) · 3.66 KB
/
RecentServersProvider.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
//
// RecentServersProvider.m
// SSHFS GUI
//
// Created by Юрий Насретдинов on 07.02.10.
// Copyright 2010 МФТИ. All rights reserved.
//
#import "RecentServersProvider.h"
@implementation RecentServersProvider
- (id)init
{
NSArray *oldEntries;
NSMutableArray *newEntries;
NSMutableDictionary *row;
self = [super init];
_def = [NSUserDefaults standardUserDefaults];
oldEntries = [_def objectForKey:@"recentServers"];
if(oldEntries)
{
NSString *oldEntry;
newEntries = [NSMutableArray array];
for(int i = 0; i < [oldEntries count]; i++)
{
oldEntry = [oldEntries objectAtIndex:i];
NSString *srv = oldEntry;
NSRange rng;
rng = [srv rangeOfString:@":"];
int port = 22;
if(rng.location != NSNotFound )
{
port = [[srv substringFromIndex:rng.location+1] intValue];
srv = [srv substringToIndex:rng.location];
}
rng = [srv rangeOfString:@"@"];
NSString *log = [srv substringToIndex:rng.location];
NSString *host = [srv substringFromIndex:rng.location+1];
row = [NSMutableDictionary dictionary];
[row setValue:host forKey:@"server"];
[row setValue:[NSNumber numberWithInt:port] forKey:@"port"];
[row setValue:log forKey:@"login"];
[row setValue:@"" forKey:@"dir"];
[row setValue:@"" forKey:@"arguments"];
[newEntries insertObject:row atIndex:i];
}
//NSLog(@"%@", newEntries);
[_def removeObjectForKey:@"recentServers"];
[_def setObject:newEntries forKey:@"servers"];
}
if(![_def objectForKey:@"servers"])
{
[_def setObject:[NSArray array] forKey:@"servers"];
}
// if(! (_entries = [_def
return self;
}
- (NSDictionary *)getDictAtIndex:(NSUInteger)rowIndex
{
return [[_def objectForKey:@"servers"] objectAtIndex:rowIndex];
}
- (void)deleteDictAtIndex:(NSUInteger)rowIndex
{
NSMutableArray *servers = [NSMutableArray arrayWithArray:[_def objectForKey:@"servers"]];
[servers removeObjectAtIndex:rowIndex];
[_def setObject:servers forKey:@"servers"];
}
- (void)addEntryWithServer:(NSString *)server port:(int)port login:(NSString *)login directory:(NSString *)directory cmdOpt:(NSString *)cmdOpt localDirectory: (NSString *)localDirectory
{
NSMutableArray *servers = [NSMutableArray arrayWithArray:[_def objectForKey:@"servers"]];
NSMutableDictionary *row = [NSMutableDictionary dictionary];
[row setValue:server forKey:@"server"];
[row setValue:[NSNumber numberWithInt:port] forKey:@"port"];
[row setValue:login forKey:@"login"];
[row setValue:directory forKey:@"dir"];
[row setValue:cmdOpt forKey:@"arguments"];
[row setValue:localDirectory forKey:@"localDir"];
[servers removeObject:row];
[servers insertObject:row atIndex:0];
[_def setObject:servers forKey:@"servers"];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [[_def objectForKey:@"servers"] count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
//NSLog(@"rowIndex — %d\n", rowIndex);
// numbering starts with 0
NSDictionary *dict = [self getDictAtIndex:rowIndex];
NSMutableString *str = [NSMutableString stringWithString:@""];
NSString *login = [dict objectForKey:@"login"];
NSString *server = [dict objectForKey:@"server"];
[str appendFormat:@"%@@%@", login, server];
int port = [[dict objectForKey:@"port"] intValue];
if(port != 22) {
//[str appendFormat:@":%d", port];
}
NSString *dir = [dict objectForKey:@"dir"];
if([dir length] > 0)
{
if([dir characterAtIndex:0] == '/') [str appendFormat:@"%@", dir];
else [str appendFormat:@" on %@", dir];
}
[str appendFormat:@" -> %@", [dict objectForKey:@"localDir"]];
return str;
}
@end