Subversion Repositories Projects

Rev

Go to most recent revision | Details | 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
 
73
 
74
 
75
      [self save];
76
    }
77
  }
78
  return self;
79
}
80
 
81
- (void) dealloc
82
{
83
  [hosts release];
84
  [super dealloc];
85
}
86
 
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
 
89
#define kFilename        @"mkhosts"
90
#define kDataKey         @"Data"
91
 
92
-(void) load {
93
 
94
  NSString *filePath = TTPathForDocumentsResource(kFilename);
95
 
96
  if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
97
 
98
    DLog(@"Load the hosts from %@",filePath);
99
 
100
    NSData *data = [[NSMutableData alloc]
101
                    initWithContentsOfFile:filePath];
102
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
103
 
104
    TT_RELEASE_SAFELY(hosts);
105
    hosts = [unarchiver decodeObjectForKey:kDataKey];
106
    [hosts retain];
107
 
108
    [unarchiver finishDecoding];
109
 
110
    [unarchiver release];
111
    [data release];        
112
 
113
    DLog(@"Loaded the hosts %@",hosts);
114
 
115
  }
116
}
117
 
118
-(void) save {
119
 
120
  NSString *filePath = TTPathForDocumentsResource(kFilename);
121
  NSMutableData *data = [[NSMutableData alloc] init];
122
  NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
123
 
124
  [archiver encodeObject:hosts forKey:kDataKey];
125
  [archiver finishEncoding];
126
  [data writeToFile:filePath atomically:YES];
127
 
128
  [archiver release];
129
  [data release];    
130
}
131
 
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
 
134
-(NSUInteger) count {
135
  return [hosts count];
136
}
137
 
138
-(MKHost*) hostAtIndexPath:(NSIndexPath *)indexPath {
139
  NSUInteger row = [indexPath row];
140
  return [hosts objectAtIndex:row];
141
}
142
 
143
-(NSIndexPath*) addHost {
144
  MKHost* h = [[MKHost alloc]init];
145
  h.connectionClass = @"MKFakeConnection";
146
  [hosts addObject:h];
147
  [h release];
148
 
149
  [self save];
150
  return [NSIndexPath indexPathForRow:[hosts count]-1 inSection:0];
151
}
152
 
153
-(void) moveHostAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
154
  NSLog(@"moveHostAtIndexPath %@ -> %@",fromIndexPath,toIndexPath);
155
  NSLog(@"%@",hosts);
156
 
157
  NSUInteger fromRow = [fromIndexPath row];
158
  NSUInteger toRow = [toIndexPath row];
159
 
160
  id object = [[hosts objectAtIndex:fromRow] retain];
161
  [hosts removeObjectAtIndex:fromRow];
162
  [hosts insertObject:object atIndex:toRow];
163
  [object release];
164
 
165
  NSLog(@"%@",hosts);
166
  [self save];
167
}
168
 
169
-(void) deleteHostAtIndexPath:(NSIndexPath*)indexPath {
170
  [hosts removeObjectAtIndex:[indexPath row]];
171
  [self save];
172
}
173
 
174
@end
175