Subversion Repositories Projects

Rev

Rev 805 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
805 - 1
// ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (C) 2010, Frank Blumenberg
3
//
4
// See License.txt for complete licensing and attribution information.
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
//
23
// ///////////////////////////////////////////////////////////////////////////////
24
 
25
#import "MKHosts.h"
26
#import "MKHost.h"
27
 
28
#import "TTGlobalCorePaths.h"
29
#import "TTCorePreprocessorMacros.h"
30
 
31
@interface MKHosts (Private)
32
-(void) load;
33
-(void) save;
34
@end
35
 
36
@implementation MKHosts
37
 
38
- (id) init
39
{
40
  self = [super init];
41
  if (self != nil) {
42
    [self load];
43
 
44
    if(!hosts) {
45
      hosts = [[NSMutableArray alloc]init];
46
 
47
      MKHost* h;
48
 
49
      h = [[MKHost alloc]init];
50
      h.name = @"Quadkopter WLAN";
51
      h.address = @"192.168.0.74";
52
      h.port = 23;
53
      h.connectionClass = @"MKIpConnection";
54
      [hosts addObject:h];
55
      [h release];
56
 
57
      h = [[MKHost alloc]init];
58
      h.name = @"Quadkopter QMK";
59
      h.address = @"127.0.0.1";
60
      h.port = 64400;
61
      h.connectionClass = @"MKQmkIpConnection";
62
      [hosts addObject:h];
63
      [h release];
64
 
65
      h = [[MKHost alloc]init];
66
      h.name = @"Quadkopter Fake";
67
      h.address = @"192.168.0.74";
68
      h.port = 23;
69
      h.connectionClass = @"MKFakeConnection";
70
      [hosts addObject:h];
71
      [h release];
72
 
806 - 73
      h = [[MKHost alloc]init];
74
      h.name = @"Quadkopter Serproxy";
75
      h.address = @"127.0.0.1";
76
      h.port = 64400;
77
      h.connectionClass = @"MKIpConnection";
78
      [hosts addObject:h];
79
      [h release];
80
 
805 - 81
 
82
      [self save];
83
    }
84
  }
85
  return self;
86
}
87
 
88
- (void) dealloc
89
{
90
  [hosts release];
91
  [super dealloc];
92
}
93
 
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
 
96
#define kFilename        @"mkhosts"
97
#define kDataKey         @"Data"
98
 
99
-(void) load {
100
 
101
  NSString *filePath = TTPathForDocumentsResource(kFilename);
102
 
103
  if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
104
 
105
    DLog(@"Load the hosts from %@",filePath);
106
 
107
    NSData *data = [[NSMutableData alloc]
108
                    initWithContentsOfFile:filePath];
109
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
110
 
111
    TT_RELEASE_SAFELY(hosts);
112
    hosts = [unarchiver decodeObjectForKey:kDataKey];
113
    [hosts retain];
114
 
115
    [unarchiver finishDecoding];
116
 
117
    [unarchiver release];
118
    [data release];        
119
 
120
    DLog(@"Loaded the hosts %@",hosts);
121
 
122
  }
123
}
124
 
125
-(void) save {
126
 
127
  NSString *filePath = TTPathForDocumentsResource(kFilename);
128
  NSMutableData *data = [[NSMutableData alloc] init];
129
  NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
130
 
131
  [archiver encodeObject:hosts forKey:kDataKey];
132
  [archiver finishEncoding];
133
  [data writeToFile:filePath atomically:YES];
134
 
135
  [archiver release];
136
  [data release];    
137
}
138
 
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
 
141
-(NSUInteger) count {
142
  return [hosts count];
143
}
144
 
145
-(MKHost*) hostAtIndexPath:(NSIndexPath *)indexPath {
146
  NSUInteger row = [indexPath row];
147
  return [hosts objectAtIndex:row];
148
}
149
 
150
-(NSIndexPath*) addHost {
151
  MKHost* h = [[MKHost alloc]init];
152
  h.connectionClass = @"MKFakeConnection";
153
  [hosts addObject:h];
154
  [h release];
155
 
156
  [self save];
157
  return [NSIndexPath indexPathForRow:[hosts count]-1 inSection:0];
158
}
159
 
160
-(void) moveHostAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
161
  NSLog(@"moveHostAtIndexPath %@ -> %@",fromIndexPath,toIndexPath);
162
  NSLog(@"%@",hosts);
163
 
164
  NSUInteger fromRow = [fromIndexPath row];
165
  NSUInteger toRow = [toIndexPath row];
166
 
167
  id object = [[hosts objectAtIndex:fromRow] retain];
168
  [hosts removeObjectAtIndex:fromRow];
169
  [hosts insertObject:object atIndex:toRow];
170
  [object release];
171
 
172
  NSLog(@"%@",hosts);
173
  [self save];
174
}
175
 
176
-(void) deleteHostAtIndexPath:(NSIndexPath*)indexPath {
177
  [hosts removeObjectAtIndex:[indexPath row]];
178
  [self save];
179
}
180
 
181
@end
182