/
AppManager.m
1187 lines (958 loc) · 40.2 KB
/
AppManager.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------
* (The MIT License)
*
* Copyright (c) 2008-2011 Rhomobile, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* http://rhomobile.com
*------------------------------------------------------------------------*/
#import <SystemConfiguration/SystemConfiguration.h>
#import <Foundation/Foundation.h>
#include "common/RhoPort.h"
#include "unzip/unzip.h"
#import "AppManager.h"
//#import "HttpContext.h"
//#import "HttpMessage.h"
//#import "Dispatcher.h"
#import "AppLoader.h"
#import "common/RhoConf.h"
#import "common/RhodesApp.h"
#import "logging/RhoLogConf.h"
#include "ruby/ext/rho/rhoruby.h"
#import "logging/RhoLog.h"
#import "../Event/Event.h"
#import "api_generator/iphone/CRubyConverter.h"
#import "Rhodes.h"
#import "common/app_build_configs.h"
#include <sys/xattr.h>
#undef DEFAULT_LOGCATEGORY
#define DEFAULT_LOGCATEGORY "RhodesApp"
static bool UnzipApplication(const char* appRoot, const void* zipbuf, unsigned int ziplen);
//const char* RhoGetRootPath();
VALUE rho_sys_has_wifi_network();
VALUE rho_sys_has_cell_network();
BOOL isPathIsSymLink(NSFileManager *fileManager, NSString* path) {
NSError *error;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:&error];
if (attributes == nil) {
//NSLog(@" SymLink NO : %@", path);
return NO;
}
NSString* fileType = [attributes objectForKey:NSFileType];
if (fileType == nil) {
//NSLog(@" SymLink NO : %@", path);
return NO;
}
BOOL res = [NSFileTypeSymbolicLink isEqualToString:fileType];
if (res) {
//NSLog(@" SymLink YES : %@", path);
}
else {
//NSLog(@" SymLink NO : %@", path);
}
return res;
}
@interface RhoFileManagerDelegate_RemoveOnly_SymLinks : NSObject {
@public
}
-(BOOL)fileManager:(NSFileManager *)fileManager shouldRemoveItemAtPath:(NSString *)path;
@end
@implementation RhoFileManagerDelegate_RemoveOnly_SymLinks
-(BOOL)fileManager:(NSFileManager *)fileManager shouldRemoveItemAtPath:(NSString *)path {
return isPathIsSymLink(fileManager, path);
}
@end
@implementation AppManager
+ (AppManager *)instance
{
static AppManager *gInstance = NULL;
@synchronized(self)
{
if (gInstance == NULL)
gInstance = [[self alloc] init];
}
return(gInstance);
}
/*
* Gets root folder of the site
* Application folders located undern the root
*/
+ (NSString *) getApplicationsRootPath {
NSString *documentsDirectory = [NSString stringWithUTF8String:rho_native_rhopath()];
return [documentsDirectory stringByAppendingPathComponent:@"apps"];
}
+ (NSString *) getDbPath {
NSString *documentsDirectory = [NSString stringWithUTF8String:rho_native_rhodbpath()];
return [documentsDirectory stringByAppendingPathComponent:@"db"];
}
+ (NSString *) getApplicationsUserPath {
return [NSString stringWithUTF8String:rho_native_rhouserpath()];
}
+ (NSString *) getApplicationsRosterUrl {
return @"http://dev.rhomobile.com/vlad/";
}
+ (bool) installApplication:(NSString*)appName data:(NSData*)appData {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *appPath = [[AppManager getApplicationsRootPath] stringByAppendingPathComponent:appName];
if ([fileManager fileExistsAtPath:appPath]) {
NSError *error;
[fileManager removeItemAtPath:appPath error:&error];
}
[fileManager createDirectoryAtPath:appPath attributes:NULL];
static char appRoot[FILENAME_MAX];
[appPath getFileSystemRepresentation:appRoot maxLength:sizeof(appRoot)];
return UnzipApplication( appRoot, [appData bytes], [appData length]);
}
- (void) copyFromMainBundle:(NSFileManager*)fileManager fromPath:(NSString *)source
toPath:(NSString*)target remove:(BOOL)remove {
BOOL dir;
if(![fileManager fileExistsAtPath:source isDirectory:&dir]) {
NSAssert1(0, @"Source item '%@' does not exists in bundle", source);
return;
}
if (!remove && dir) {
NSError *error;
if (![fileManager fileExistsAtPath:target])
[fileManager createDirectoryAtPath:target withIntermediateDirectories:YES attributes:nil error:&error];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:source];
NSString *child;
while (child = [enumerator nextObject]) {
[self copyFromMainBundle:fileManager fromPath:[source stringByAppendingPathComponent:child]
toPath:[target stringByAppendingPathComponent:child] remove:NO];
}
}
else {
NSError *error;
if ([fileManager fileExistsAtPath:target] && ![fileManager removeItemAtPath:target error:&error]) {
NSAssert2(0, @"Failed to remove '%@': %@", target, [error localizedDescription]);
return;
}
if (![fileManager copyItemAtPath:source toPath:target error:&error]) {
NSAssert3(0, @"Failed to copy '%@' to '%@': %@", source, target, [error localizedDescription]);
return;
}
}
}
- (BOOL)isContentsEqual:(NSFileManager*)fileManager first:(NSString*)filePath1 second:(NSString*)filePath2 {
NSLog(@"filePath1: %@", filePath1);
NSLog(@"filePath2: %@", filePath2);
if (![fileManager fileExistsAtPath:filePath1] || ![fileManager fileExistsAtPath:filePath2])
return NO;
NSString *content1 = [[NSString alloc] initWithData:[fileManager contentsAtPath:filePath1]
encoding:NSUTF8StringEncoding];
NSString *content2 = [[NSString alloc] initWithData:[fileManager contentsAtPath:filePath2]
encoding:NSUTF8StringEncoding];
BOOL result = [content1 isEqualToString:content2];
[content1 release];
[content2 release];
return result;
}
/*
* Configures AppManager
*/
- (void) configure {
[self configure:YES force_update_content:NO only_apps:NO];
}
- (void) configure:(BOOL)make_sym_links force_update_content:(BOOL)force_update_content only_apps:(BOOL)only_apps {
//#define RHO_DONT_COPY_ON_START
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundleRoot = [[NSBundle mainBundle] resourcePath];
NSString *rhoRoot = [NSString stringWithUTF8String:rho_native_rhopath()];
NSString *rhoUserRoot = [NSString stringWithUTF8String:rho_native_rhouserpath()];
NSString *rhoDBRoot = [NSString stringWithUTF8String:rho_native_rhodbpath()];
NSString *filePathNew = [bundleRoot stringByAppendingPathComponent:@"name"];
NSString *filePathOld = [rhoRoot stringByAppendingPathComponent:@"name"];
//#ifndef RHO_DONT_COPY_ON_START
BOOL hasOldName = [fileManager fileExistsAtPath:filePathOld];
//#endif
BOOL nameChanged = ![self isContentsEqual:fileManager first:filePathNew second:filePathOld];
BOOL restoreSymLinks_only = NO;
BOOL contentChanged = force_update_content;
if (nameChanged)
contentChanged = YES;
else {
filePathNew = [bundleRoot stringByAppendingPathComponent:@"hash"];
filePathOld = [rhoRoot stringByAppendingPathComponent:@"hash"];
contentChanged = ![self isContentsEqual:fileManager first:filePathNew second:filePathOld];
// check for lost sym-links (upgrade OS or reinstall application without change version)
if (!contentChanged) {
// check exist of sym-link
NSString* testName = [rhoRoot stringByAppendingPathComponent:@"lib"];
if (![fileManager fileExistsAtPath:testName]) {
NSLog(@" Can not found main Sym-Link - we should restore all sym-links !");
contentChanged = YES;
restoreSymLinks_only = YES;
}
else {
NSLog(@" Main Sym-Link founded - disable restoring !");
}
}
}
NSString* testName = [rhoRoot stringByAppendingPathComponent:@"lib"];
BOOL libExist = [fileManager fileExistsAtPath:testName];
if (libExist) {
NSLog(@" Lib File is Exist: %@", testName);
}
else {
NSLog(@" Lib File is NOT Exist: %@", testName);
}
if (contentChanged) {
if (make_sym_links) {
//#ifdef RHO_DONT_COPY_ON_START
// we have next situations when we should remove old content:
// 1. we upgrade old version (where we copy all files)
// we should remove all files
// 2. we upgrade version with symlinks
// we should remove only symlinks
// 3. we should only restore sym-lins after that was cleared - OS upgrade/reinstall app with the same version/restore from bakup etc.
// we check old "lib" file - if it is SymLink then we have new version of Rhodes (with SymLinks instead of files)
BOOL isNewVersion = isPathIsSymLink(fileManager, testName);
RhoFileManagerDelegate_RemoveOnly_SymLinks* myDelegate = nil;
if (isNewVersion) {
myDelegate = [[RhoFileManagerDelegate_RemoveOnly_SymLinks alloc] init];
[fileManager setDelegate:myDelegate];
}
NSError *error;
NSString *appsDocDir = [rhoUserRoot stringByAppendingPathComponent:@"apps"];
[fileManager createDirectoryAtPath:rhoRoot withIntermediateDirectories:YES attributes:nil error:&error];
[fileManager createDirectoryAtPath:appsDocDir withIntermediateDirectories:YES attributes:nil error:&error];
// Create symlink to "lib"
NSString *src = [bundleRoot stringByAppendingPathComponent:@"lib"];
NSLog(@"src: %@", src);
NSString *dst = [rhoRoot stringByAppendingPathComponent:@"lib"];
NSLog(@"dst: %@", dst);
[fileManager removeItemAtPath:dst error:&error];
[fileManager createSymbolicLinkAtPath:dst withDestinationPath:src error:&error];
//[self copyFromMainBundle:fileManager fromPath:src toPath:dst remove:YES];
NSString *dirs[] = {@"apps", @"db"};
for (int i = 0, lim = sizeof(dirs)/sizeof(dirs[0]); i < lim; ++i) {
// Create directory
src = [bundleRoot stringByAppendingPathComponent:dirs[i]];
NSLog(@"src: %@", src);
dst = [rhoRoot stringByAppendingPathComponent:dirs[i]];
NSLog(@"dst: %@", dst);
if (![fileManager fileExistsAtPath:dst])
[fileManager createDirectoryAtPath:dst withIntermediateDirectories:YES attributes:nil error:&error];
// And make symlinks from its content
NSArray *subelements = [fileManager contentsOfDirectoryAtPath:src error:&error];
for (int i = 0, lim = [subelements count]; i < lim; ++i) {
NSString *child = [subelements objectAtIndex:i];
NSString *fchild = [src stringByAppendingPathComponent:child];
NSLog(@" .. src: %@", fchild);
NSString *target = [dst stringByAppendingPathComponent:child];
NSLog(@" .. dst: %@", target);
[fileManager removeItemAtPath:target error:&error];
if ([child isEqualToString:@"rhoconfig.txt"]) {
[fileManager setDelegate:nil];
[fileManager removeItemAtPath:target error:&error];
[fileManager setDelegate:myDelegate];
[fileManager copyItemAtPath:fchild toPath:target error:&error];
}
else {
[fileManager createSymbolicLinkAtPath:target withDestinationPath:fchild error:&error];
}
//[self addSkipBackupAttributeToItemAtURL:target];
}
}
// make symlinks for db files
[fileManager setDelegate:nil];
if (myDelegate != nil) {
[myDelegate release];
}
// copy "db"
NSString* exclude_db[] = {@"syncdb.schema", @"syncdb.triggers", @"syncdb_java.triggers"};
if (!restoreSymLinks_only && !only_apps) {
NSString *copy_dirs[] = {@"db"};
for (int i = 0, lim = sizeof(copy_dirs)/sizeof(copy_dirs[0]); i < lim; ++i) {
BOOL remove = nameChanged;
if ([copy_dirs[i] isEqualToString:@"db"] && !hasOldName)
remove = NO;
NSString *src = [bundleRoot stringByAppendingPathComponent:copy_dirs[i]];
NSLog(@"copy src: %@", src);
NSString *dst = [rhoDBRoot stringByAppendingPathComponent:copy_dirs[i]];
NSLog(@"copy dst: %@", dst);
//[self copyFromMainBundle:fileManager fromPath:src toPath:dst remove:remove];
NSArray *subelements = [fileManager contentsOfDirectoryAtPath:src error:&error];
for (int i = 0, lim = [subelements count]; i < lim; ++i) {
NSString *child = [subelements objectAtIndex:i];
NSString *fchild = [src stringByAppendingPathComponent:child];
NSLog(@" .. copy src: %@", fchild);
NSString *target = [dst stringByAppendingPathComponent:child];
NSLog(@" .. copy dst: %@", target);
BOOL copyit = YES;
int j, jlim;
for (j = 0, jlim = sizeof(exclude_db)/sizeof(exclude_db[0]); j < jlim; j++) {
if ([child isEqualToString:exclude_db[j]]) {
copyit = NO;
}
}
if (copyit) {
[fileManager removeItemAtPath:target error:&error];
[fileManager copyItemAtPath:fchild toPath:target error:&error];
}
}
}
// Finally, copy "hash" and "name" files
NSString *items[] = {@"hash", @"name"};
for (int i = 0, lim = sizeof(items)/sizeof(items[0]); i < lim; ++i) {
NSString *src = [bundleRoot stringByAppendingPathComponent:items[i]];
NSLog(@"copy src: %@", src);
NSString *dst = [rhoRoot stringByAppendingPathComponent:items[i]];
NSLog(@"copy dst: %@", dst);
[fileManager removeItemAtPath:dst error:&error];
[fileManager copyItemAtPath:src toPath:dst error:&error];
//[self addSkipBackupAttributeToItemAtURL:dst];
}
}
}
else {
NSString *dirs[] = {@"apps", @"lib", @"db", @"hash", @"name"};
for (int i = 0, lim = sizeof(dirs)/sizeof(dirs[0]); i < lim; ++i) {
if (!only_apps || [dirs[i] isEqualToString:@"apps"]) {
BOOL remove = nameChanged;
if ([dirs[i] isEqualToString:@"db"] && !hasOldName)
remove = NO;
NSString *src = [bundleRoot stringByAppendingPathComponent:dirs[i]];
NSLog(@"src: %@", src);
NSString *dst = [rhoRoot stringByAppendingPathComponent:dirs[i]];
NSLog(@"dst: %@", dst);
[self copyFromMainBundle:fileManager fromPath:src toPath:dst remove:remove];
}
}
}
}
rho_logconf_Init_with_separate_user_path(rho_native_rhopath(), rho_native_rhopath(), "", rho_native_rhouserpath());
rho_rhodesapp_create_with_separate_user_path(rho_native_rhopath(), rho_native_rhouserpath());
RAWLOG_INFO("Rhodes started");
}
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return [[Rhodes sharedInstance] baseViewController];
}
- (void)openDocInteractCommand:(NSString*)url {
if (NSClassFromString(@"UIDocumentInteractionController")) {
NSURL *fileURL = [NSURL fileURLWithPath:url];
UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;//[AppManager instance];
BOOL result = [docController presentPreviewAnimated:YES];
if (!result) {
CGPoint centerPoint = [Rhodes sharedInstance].window.center;
CGRect centerRec = CGRectMake(centerPoint.x, centerPoint.y, 0, 0);
BOOL isValid = [docController presentOpenInMenuFromRect:centerRec inView:[Rhodes sharedInstance].window animated:YES];
}
}
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)docController
{
//[docController release];
//docController = nil;
}
- (void)openDocInteract:(NSString*)url {
[self performSelectorOnMainThread:@selector(openDocInteractCommand:) withObject:url waitUntilDone:NO];
}
- (void) openURLComand:(NSString*)url {
RAWLOG_INFO1("rho_sys_open_url: %s", url);
NSString* strUrl = url;//[NSString stringWithUTF8String:url];
BOOL res = FALSE;
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:strUrl];
if (!fileExists) {
NSString *fixed_path = [NSString stringWithUTF8String:rho_rhodesapp_getapprootpath()];
fixed_path = [fixed_path stringByAppendingString:strUrl];
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fixed_path];
if (fileExists) {
strUrl = fixed_path;
}
}
if (fileExists) {
res = TRUE;
[[AppManager instance] openDocInteract:strUrl];
}
else {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strUrl]]) {
res = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strUrl]];
}
}
if ( res)
RAWLOG_INFO("rho_sys_open_url suceeded.");
else
RAWLOG_INFO("rho_sys_open_url failed.");
}
- (void) openURL:(NSString*)url {
[self performSelectorOnMainThread:@selector(openURLComand:) withObject:[url retain] waitUntilDone:NO];
}
@end
const char* getUserPath() {
static bool loaded = FALSE;
static char root[FILENAME_MAX];
if (!loaded){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = //[paths objectAtIndex:0];
[ [paths objectAtIndex:0] stringByAppendingString:@"/"];
[documentsDirectory getFileSystemRepresentation:root maxLength:sizeof(root)];
loaded = TRUE;
}
return root;
}
const char* rho_native_rhopath()
{
static bool loaded = FALSE;
static char root[FILENAME_MAX];
if (!loaded){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *rootDirectory = [ [paths objectAtIndex:0] stringByAppendingString:@"/Private Documents/"];
const char* svalue = get_app_build_config_item("iphone_set_approot");
if (svalue != NULL) {
NSString* value = [NSString stringWithUTF8String:svalue];
if ([value compare:@"Documents" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
rootDirectory = [ [paths objectAtIndex:0] stringByAppendingString:@"/"];
}
else if ([value compare:@"Library_Caches" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
rootDirectory = [ [paths objectAtIndex:0] stringByAppendingString:@"/Private Documents/"];
}
else if ([value compare:@"Library_Private_Documents" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
rootDirectory = [ [paths objectAtIndex:0] stringByAppendingString:@"/Private Documents/"];
}
}
[rootDirectory getFileSystemRepresentation:root maxLength:sizeof(root)];
loaded = TRUE;
}
return root;
}
const char* rho_native_reruntimepath()
{
return rho_native_rhopath();
}
const char* rho_native_rhouserpath()
{
BOOL user_path_in_root = NO;
const char* svalue = get_app_build_config_item("iphone_userpath_in_approot");
if (svalue != NULL) {
user_path_in_root = svalue[0] != '0';
}
if (user_path_in_root) {
return rho_native_rhopath();
}
return getUserPath();
}
const char* rho_native_rhodbpath()
{
BOOL db_path_in_root = NO;
const char* svalue = get_app_build_config_item("iphone_db_in_approot");
if (svalue != NULL) {
db_path_in_root = svalue[0] != '0';
}
if (db_path_in_root) {
return rho_native_rhopath();
}
return rho_native_rhouserpath();
}
NSString* rho_sys_get_locale_iphone()
{
NSString *preferredLang = [[NSLocale preferredLanguages] objectAtIndex:0];
return preferredLang;
}
VALUE rho_sys_get_locale()
{
NSString *preferredLang = rho_sys_get_locale_iphone();
return rho_ruby_create_string( [preferredLang UTF8String] );
}
int rho_sys_get_screen_width()
{
CGRect rect = [[UIScreen mainScreen] bounds];
UIInterfaceOrientation current_orientation = [[UIApplication sharedApplication] statusBarOrientation];
if ((current_orientation == UIInterfaceOrientationLandscapeLeft) || (current_orientation == UIInterfaceOrientationLandscapeRight)) {
return rect.size.height;
}
return rect.size.width;
}
int rho_sys_get_screen_height()
{
CGRect rect = [[UIScreen mainScreen] bounds];
UIInterfaceOrientation current_orientation = [[UIApplication sharedApplication] statusBarOrientation];
if ((current_orientation == UIInterfaceOrientationLandscapeLeft) || (current_orientation == UIInterfaceOrientationLandscapeRight)) {
return rect.size.width;
}
return rect.size.height;
}
int rho_sys_set_sleeping(int sleeping)
{
int ret = [[UIApplication sharedApplication] isIdleTimerDisabled] ? 0 : 1;
[[UIApplication sharedApplication] setIdleTimerDisabled: (!sleeping ? YES : NO)];
return ret;
}
void rho_sys_app_exit() {
if (([Rhodes sharedInstance] != nil) && (![Rhodes sharedInstance].mBlockExit)) {
exit(EXIT_SUCCESS);
}
}
int rho_sys_is_app_installed(const char *appname) {
NSString* app_name = [NSString stringWithUTF8String:appname];
app_name = [app_name stringByAppendingString:@":check_for_exist"];
NSURL* nsurl = [NSURL URLWithString:app_name];
if ([[UIApplication sharedApplication] canOpenURL:nsurl]) {
return 1;
}
return 0;
}
void rho_sys_app_uninstall(const char *appname) {
NSLog(@"ALERT: Uninstall of applications is unsupported on iOS platfrom !!!");
}
void rho_sys_open_url(const char* url)
{
[[AppManager instance] openURL:[NSString stringWithUTF8String:url]];
}
void rho_sys_app_install(const char *url) {
rho_sys_open_url(url);
}
void rho_sys_run_app_iphone(const char* appname, char* params) {
NSString* app_name = [NSString stringWithUTF8String:appname];
app_name = [app_name stringByAppendingString:@":"];
if (params != NULL) {
NSString* param = [NSString stringWithUTF8String:params];
app_name = [app_name stringByAppendingString:param];
}
const char* full_url = [app_name UTF8String];
RAWLOG_INFO1("rho_sys_run_app: %s", full_url);
BOOL res = FALSE;
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:app_name]]) {
res = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:app_name]];
}
if ( res)
RAWLOG_INFO("rho_sys_run_app suceeded.");
else
RAWLOG_INFO("rho_sys_run_app failed.");
}
void rho_sys_run_app(const char* appname, VALUE params)
{
char* str_params = NULL;
if (params != 0) {
char* parameter = getStringFromValue(params);
if (parameter != NULL) {
str_params = parameter;
}
}
rho_sys_run_app_iphone(appname, str_params);
}
void rho_sys_bring_to_front()
{
RAWLOG_INFO("rho_sys_bring_to_front has no implementation on iPhone.");
}
void rho_sys_report_app_started()
{
RAWLOG_INFO("rho_sys_report_app_started has no implementation on iPhone.");
}
extern VALUE rho_sys_has_network();
// http://www.apple.com/iphone/specs.html
static const double RHO_IPHONE_PPI = 163.0;
static const double RHO_IPHONE4_PPI = 326.0;
// http://www.apple.com/ipad/specs/
static const double RHO_IPAD_PPI = 132.0;
static const double RHO_NEW_IPAD_PPI = 264.0;
static float get_scale() {
float scales = 1;//[[UIScreen mainScreen] scale];
#ifdef __IPHONE_4_0
if ( [[UIScreen mainScreen] respondsToSelector:@selector(scale)] ) {
scales = [[UIScreen mainScreen] scale];
}
#endif
return scales;
}
extern BOOL rho_sys_has_wifi_network_iphone();
extern BOOL rho_sys_has_cell_network_iphone();
extern BOOL rho_sys_has_network_iphone();
int rho_sysimpl_get_property_iphone(char* szPropName, NSObject** resValue)
{
if (strcasecmp("platform", szPropName) == 0)
{*resValue = [NSString stringWithUTF8String:"APPLE"]; return 1;}
else if (strcasecmp("locale", szPropName) == 0)
{*resValue = rho_sys_get_locale_iphone(); return 1; }
else if (strcasecmp("country", szPropName) == 0) {
NSLocale *locale = [NSLocale currentLocale];
NSString *cl = [locale objectForKey:NSLocaleCountryCode];
*resValue = cl;
return 1;
}
else if (strcasecmp("screen_width", szPropName) == 0)
{*resValue = [NSNumber numberWithInt:rho_sys_get_screen_width()]; return 1; }
else if (strcasecmp("screen_height", szPropName) == 0)
{*resValue = [NSNumber numberWithInt:rho_sys_get_screen_height()]; return 1; }
else if (strcasecmp("real_screen_height", szPropName) == 0)
{
*resValue = [NSNumber numberWithInt:((int)(rho_sys_get_screen_height()*get_scale()))];
return 1;
}
else if (strcasecmp("real_screen_width", szPropName) == 0)
{
*resValue = [NSNumber numberWithInt:((int)(rho_sys_get_screen_width()*get_scale()))];
return 1;
}
else if (strcasecmp("screen_orientation", szPropName) == 0) {
UIInterfaceOrientation current_orientation = [[UIApplication sharedApplication] statusBarOrientation];
if ((current_orientation == UIInterfaceOrientationLandscapeLeft) || (current_orientation == UIInterfaceOrientationLandscapeRight)) {
*resValue = @"landscape";
}
else {
*resValue = @"portrait";
}
return 1;
}
else if (strcasecmp("has_network", szPropName) == 0)
{*resValue = [NSNumber numberWithBool:rho_sys_has_network_iphone()]; return 1; }
else if (strcasecmp("has_wifi_network", szPropName) == 0)
{*resValue = [NSNumber numberWithBool:rho_sys_has_wifi_network_iphone()]; return 1; }
else if (strcasecmp("has_cell_network", szPropName) == 0)
{*resValue = [NSNumber numberWithBool:rho_sys_has_cell_network_iphone()]; return 1; }
else if (strcasecmp("has_camera", szPropName) == 0) {
int has_camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
*resValue = [NSNumber numberWithBool:(has_camera != 0)];
return 1;
}
else if (strcasecmp("ppi_x", szPropName) == 0 ||
strcasecmp("ppi_y", szPropName) == 0) {
#ifdef __IPHONE_3_2
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (get_scale() > 1.2) {
*resValue = [NSNumber numberWithDouble:RHO_NEW_IPAD_PPI];
}
else {
*resValue = [NSNumber numberWithDouble:RHO_IPAD_PPI];
}
return 1;
}
#endif
if (get_scale() > 1.2) {
*resValue = [NSNumber numberWithDouble:RHO_IPHONE4_PPI];
}
else {
*resValue = [NSNumber numberWithDouble:RHO_IPHONE_PPI];
}
return 1;
}
else if (strcasecmp("device_name", szPropName) == 0) {
NSString *model = [[UIDevice currentDevice] model];
*resValue = model;
return 1;
}
else if (strcasecmp("os_version", szPropName) == 0) {
NSString *version = [[UIDevice currentDevice] systemVersion];
*resValue = version;
return 1;
}
else if (strcasecmp("webview_framework", szPropName) == 0) {
NSString *version = [[UIDevice currentDevice] systemVersion];
NSString *wvf = @"WEBKIT/";
wvf = [wvf stringByAppendingString:version];
*resValue = wvf;
return 1;
}
else if (strcasecmp("is_emulator", szPropName) == 0) {
int bSim = 0;
#if TARGET_IPHONE_SIMULATOR
bSim = 1;
#endif
*resValue = [NSNumber numberWithBool:(bSim != 0)];
return 1;
}else if (strcasecmp("has_calendar", szPropName) == 0) {
int bCal = 0;
if (is_rho_calendar_supported())
bCal = 1;
*resValue = [NSNumber numberWithBool:(bCal != 0)];
return 1;
}
return 0;
}
int rho_sysimpl_get_property(char* szPropName, VALUE* resValue)
{
NSObject* result = nil;
if (rho_sysimpl_get_property_iphone(szPropName, &result)) {
*resValue = [CRubyConverter convertToRuby:result];
return 1;
}
return 0;
}
/*
int rho_sysimpl_get_property(char* szPropName, VALUE* resValue)
{
if (strcasecmp("platform", szPropName) == 0)
{*resValue = rho_ruby_create_string("APPLE"); return 1;}
else if (strcasecmp("locale", szPropName) == 0)
{*resValue = rho_sys_get_locale(); return 1; }
else if (strcasecmp("country", szPropName) == 0) {
NSLocale *locale = [NSLocale currentLocale];
NSString *cl = [locale objectForKey:NSLocaleCountryCode];
*resValue = rho_ruby_create_string([cl UTF8String]);
return 1;
}
else if (strcasecmp("screen_width", szPropName) == 0)
{*resValue = rho_ruby_create_integer(rho_sys_get_screen_width()); return 1; }
else if (strcasecmp("screen_height", szPropName) == 0)
{*resValue = rho_ruby_create_integer(rho_sys_get_screen_height()); return 1; }
else if (strcasecmp("real_screen_height", szPropName) == 0)
{
*resValue = rho_ruby_create_integer((int)(rho_sys_get_screen_height()*get_scale()));
return 1;
}
else if (strcasecmp("real_screen_width", szPropName) == 0)
{
*resValue = rho_ruby_create_integer((int)(rho_sys_get_screen_width()*get_scale()));
return 1;
}
else if (strcasecmp("screen_orientation", szPropName) == 0) {
UIInterfaceOrientation current_orientation = [[UIApplication sharedApplication] statusBarOrientation];
if ((current_orientation == UIInterfaceOrientationLandscapeLeft) || (current_orientation == UIInterfaceOrientationLandscapeRight)) {
*resValue = rho_ruby_create_string("landscape");
}
else {
*resValue = rho_ruby_create_string("portrait");
}
return 1;
}
else if (strcasecmp("has_network", szPropName) == 0)
{*resValue = rho_sys_has_network(); return 1; }
else if (strcasecmp("has_wifi_network", szPropName) == 0)
{*resValue = rho_sys_has_wifi_network(); return 1; }
else if (strcasecmp("has_cell_network", szPropName) == 0)
{*resValue = rho_sys_has_cell_network(); return 1; }
else if (strcasecmp("has_camera", szPropName) == 0) {
int has_camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
*resValue = rho_ruby_create_boolean(has_camera);
return 1;
}
else if (strcasecmp("ppi_x", szPropName) == 0 ||
strcasecmp("ppi_y", szPropName) == 0) {
#ifdef __IPHONE_3_2
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (get_scale() > 1.2) {
*resValue = rho_ruby_create_double(RHO_NEW_IPAD_PPI);
}
else {
*resValue = rho_ruby_create_double(RHO_IPAD_PPI);
}
return 1;
}
#endif
if (get_scale() > 1.2) {
*resValue = rho_ruby_create_double(RHO_IPHONE4_PPI);
}
else {
*resValue = rho_ruby_create_double(RHO_IPHONE_PPI);
}
return 1;
}
else if (strcasecmp("device_name", szPropName) == 0) {
NSString *model = [[UIDevice currentDevice] model];
*resValue = rho_ruby_create_string([model UTF8String]);
return 1;
}
else if (strcasecmp("os_version", szPropName) == 0) {
NSString *version = [[UIDevice currentDevice] systemVersion];
*resValue = rho_ruby_create_string([version UTF8String]);
return 1;
}
else if (strcasecmp("webview_framework", szPropName) == 0) {
NSString *version = [[UIDevice currentDevice] systemVersion];
NSString *wvf = @"WEBKIT/";
wvf = [wvf stringByAppendingString:version];
*resValue = rho_ruby_create_string([wvf UTF8String]);
return 1;
}
else if (strcasecmp("is_emulator", szPropName) == 0) {
int bSim = 0;
#if TARGET_IPHONE_SIMULATOR
bSim = 1;
#endif
*resValue = rho_ruby_create_boolean(bSim);
return 1;
}else if (strcasecmp("has_calendar", szPropName) == 0) {
int bCal = 0;
if (is_rho_calendar_supported())
bCal = 1;
*resValue = rho_ruby_create_boolean(bCal);
return 1;
}
//// "device_id" property used only for PUSH technology !
// else if (strcasecmp("device_id", szPropName) == 0) {
// NSString* uuid = [[UIDevice currentDevice] uniqueIdentifier];
// *resValue = rho_ruby_create_string([uuid UTF8String]);
// return 1;
//}
// [[UIDevice currentDevice] uniqueIdentifier]
//// Removed because it's possibly dangerous: Apple could reject application
//// used such approach from its AppStore
//else if (strcasecmp("phone_number", szPropName) == 0) {
// NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"];
// if (!num)
// return 0;
// *resValue = rho_ruby_create_string([num UTF8String]);
// return 1;
//}
return 0;
}
*/