forked from SUPLA/supla-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuplaApp.m
376 lines (262 loc) · 9.62 KB
/
SuplaApp.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
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Author: Przemyslaw Zygmunt p.zygmunt@acsoftware.pl [AC SOFTWARE]
*/
#import "SuplaApp.h"
#import "Database.h"
static SAApp* _Globals = nil;
NSString *kSADataChangedNotification = @"kSA-N01";
NSString *kSAConnectingNotification = @"kSA-N02";
NSString *kSARegisteredNotification = @"kSA-N03";
NSString *kSARegisteringNotification = @"kSA-N04";
NSString *kSARegisterErrorNotification = @"kSA-N05";
NSString *kSADisconnectedNotification = @"kSA-N06";
NSString *kSAConnectedNotification = @"kSA-N07";
NSString *kSAVersionErrorNotification = @"kSA-N08";
NSString *kSAEventNotification = @"kSA-N09";
@implementation SAApp {
int _current_access_id;
NSString *_current_server;
SASuplaClient* _SuplaClient;
SADatabase *_DB;
SAUIHelper *_UI;
}
-(id)init {
self = [super init];
if ( self ) {
_current_access_id = 0;
_current_server = nil;
}
return self;
}
+(SAApp*)instance {
@synchronized(self) {
if ( _Globals == nil ) {
_Globals = [[SAApp alloc] init];
}
}
return _Globals;
}
+(void) getClientGUID:(char[SUPLA_GUID_SIZE])guid {
@synchronized(self) {
NSData *GUID = [[NSUserDefaults standardUserDefaults] dataForKey:@"client_guid"];
if ( GUID == nil || [GUID length] != SUPLA_GUID_SIZE ) {
NSMutableData* newGUID = [NSMutableData dataWithCapacity:SUPLA_GUID_SIZE];
for( int i = 0 ; i < SUPLA_GUID_SIZE; ++i ) {
Byte random = arc4random();
[newGUID appendBytes:(void*)&random length:1];
}
[[NSUserDefaults standardUserDefaults] setValue:newGUID forKey:@"client_guid"];
GUID = newGUID;
};
if ( GUID && [GUID length] == SUPLA_GUID_SIZE ) {
[GUID getBytes:guid length:SUPLA_GUID_SIZE];
} else {
memset(guid, 0, SUPLA_GUID_SIZE);
}
}
}
-(int) getAccessID {
@synchronized(self) {
if ( _current_access_id == 0 ) {
_current_access_id = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"access_id"];
}
}
return _current_access_id;
}
+(int) getAccessID {
return [[self instance] getAccessID];
}
-(void) setAccessID:(int)aid {
@synchronized(self) {
_current_access_id = aid;
[[NSUserDefaults standardUserDefaults] setInteger:aid forKey:@"access_id"];
}
}
+(void) setAccessID:(int)aid {
[[self instance] setAccessID:aid];
}
+(NSString*) getAccessIDpwd {
NSString *result = nil;
@synchronized(self) {
result = [[NSUserDefaults standardUserDefaults] stringForKey:@"access_id_pwd"];
}
return result == nil ? [[NSString alloc] init] : result;
}
+(void) setAccessIDpwd:(NSString *)pwd {
@synchronized(self) {
[[NSUserDefaults standardUserDefaults] setValue:pwd forKey:@"access_id_pwd"];
}
}
-(NSString*) getServerHostName {
@synchronized(self) {
if ( _current_server == nil ) {
_current_server = [[NSUserDefaults standardUserDefaults] stringForKey:@"server_host"];
}
}
return _current_server == nil ? [[NSString alloc] init] : _current_server;
}
+(NSString*) getServerHostName {
return [[self instance] getServerHostName];
}
-(void) setServerHostName:(NSString *)hostname {
@synchronized(self) {
[[NSUserDefaults standardUserDefaults] setValue:hostname forKey:@"server_host"];
_current_server = hostname;
}
}
+(void) setServerHostName:(NSString *)hostname {
[[self instance] setServerHostName:hostname];
}
+ (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
-(void)onInitTimer:(NSTimer *)timer {
[self SuplaClient];
}
-(void)initClientDelayed:(double)time {
@synchronized(self) {
if ( _SuplaClient == nil ) {
[NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:@selector(onInitTimer:)
userInfo:nil
repeats:NO];
}
}
}
+(void)initClientDelayed:(double)time {
[[self instance] initClientDelayed:time];
}
-(SASuplaClient*) SuplaClient {
SASuplaClient *result = nil;
@synchronized(self) {
if ( _SuplaClient == nil ) {
_SuplaClient = [[SASuplaClient alloc] init];
[_SuplaClient start];
}
result = _SuplaClient;
}
return result;
}
+(SASuplaClient*) SuplaClient {
return [[self instance] SuplaClient];
}
-(BOOL) SuplaClientTerminate {
BOOL result = NO;
@synchronized(self) {
if ( _SuplaClient != nil ) {
[_SuplaClient cancel];
result = YES;
}
}
return result;
}
-(void)onTerminated:(SASuplaClient*)sender {
@synchronized(self) {
if ( _SuplaClient == sender ) {
_SuplaClient = nil;
}
}
}
+(void) SuplaClientTerminate {
[[self instance] SuplaClientTerminate];
}
+(void) SuplaClientWaitForTerminate {
while([[self instance] SuplaClientTerminate]) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];
}
}
-(BOOL) SuplaClientConnected {
return _SuplaClient != nil && [_SuplaClient isConnected];
}
+(BOOL) SuplaClientConnected {
return [[self instance] SuplaClientConnected];
}
-(SADatabase*)DB {
if ( _DB == nil ) {
_DB = [[SADatabase alloc] init];
[_DB initSaveObserver];
}
return _DB;
}
-(SAUIHelper *)UI {
if ( _UI == nil ) {
_UI = [[SAUIHelper alloc] init];
}
return _UI;
}
+(SAUIHelper *)UI {
return [[self instance] UI];
}
+(SADatabase*)DB {
return [[self instance] DB];
}
-(void)onDisconnected {
[self.UI.StatusVC.progress setProgress:0];
[[NSNotificationCenter defaultCenter] postNotificationName:kSADisconnectedNotification object:self userInfo:nil];
}
-(void)onConnecting {
if ( self.UI.rootViewController == self.UI.SettingsVC ) {
[self.UI.StatusVC.progress setProgress:0.25];
} else {
[self.UI showStatusConnectingProgress:0.25];
}
[[NSNotificationCenter defaultCenter] postNotificationName:kSAConnectingNotification object:self userInfo:nil];
}
-(void)onConnected {
[self.UI showStatusConnectingProgress:0.5];
[[NSNotificationCenter defaultCenter] postNotificationName:kSAConnectedNotification object:self userInfo:nil];
}
-(void)onRegistering {
[self.UI showStatusConnectingProgress:0.75];
[[NSNotificationCenter defaultCenter] postNotificationName:kSARegisteringNotification object:self userInfo:nil];
}
-(void)onRegistered:(SARegResult*)result {
[self.UI showStatusConnectingProgress:1];
[self.UI showMainVC];
[[NSNotificationCenter defaultCenter] postNotificationName:kSARegisteredNotification object:self userInfo:[[NSDictionary alloc] initWithObjects:@[result] forKeys:@[@"result"]]];
}
-(void)onRegisterError:(NSNumber*)code {
[self SuplaClientTerminate];
NSString *msg = [NSString stringWithFormat:@"%@ (%@)", NSLocalizedString(@"Unknown error", nil), code];
switch([code intValue]) {
case SUPLA_RESULTCODE_TEMPORARILY_UNAVAILABLE:
msg = NSLocalizedString(@"Service temporarily unavailable", nil);
break;
case SUPLA_RESULTCODE_BAD_CREDENTIALS:
msg = NSLocalizedString(@"Bad credentials", nil);
break;
case SUPLA_RESULTCODE_CLIENT_LIMITEXCEEDED:
msg = NSLocalizedString(@"Client limit exceeded", nil);
break;
case SUPLA_RESULTCODE_CLIENT_DISABLED:
case SUPLA_RESULTCODE_ACCESSID_DISABLED:
msg = NSLocalizedString(@"Access disabled", nil);
break;
}
[self.UI showStatusError:msg];
[[NSNotificationCenter defaultCenter] postNotificationName:kSARegisterErrorNotification object:self userInfo:[[NSDictionary alloc] initWithObjects:@[code] forKeys:@[@"code"]]];
}
-(void)onVersionError:(SAVersionError*)ve {
[self SuplaClientTerminate];
[[NSNotificationCenter defaultCenter] postNotificationName:kSAVersionErrorNotification object:self userInfo:[[NSDictionary alloc] initWithObjects:@[ve] forKeys:@[@"version_error"]]];
[self.UI showStatusError:NSLocalizedString(@"Incompatible server version", nil)];
}
-(void)onEvent:(SAEvent*)event {
[[NSNotificationCenter defaultCenter] postNotificationName:kSAEventNotification object:self userInfo:[[NSDictionary alloc] initWithObjects:@[event] forKeys:@[@"event"]]];
}
-(void)onDataChanged {
[[NSNotificationCenter defaultCenter] postNotificationName:kSADataChangedNotification object:self userInfo:nil];
}
@end