summaryrefslogtreecommitdiff
path: root/ext/mac-ui-macgap1-wrapper/src/MacGap/Classes/Utils.m
blob: 8d85c29496699b59555394fa0a99cb48f0750b0a (plain)
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
#import "Utils.h"
#import <Webkit/WebScriptObject.h>

static Utils* sharedInstance = nil;

@implementation Utils

- (float) titleBarHeight:(NSWindow*)aWindow
{
    NSRect frame = [aWindow frame];
    NSRect contentRect = [NSWindow contentRectForFrameRect: frame
												 styleMask: NSTitledWindowMask];
	
    return (frame.size.height - contentRect.size.height);
}

- (NSString*) pathForResource:(NSString*)resourcepath
{
    NSBundle * mainBundle = [NSBundle mainBundle];
    NSMutableArray *directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]];
    NSString       *filename       = [directoryParts lastObject];
    [directoryParts removeLastObject];
	
    NSString *directoryStr = [NSString stringWithFormat:@"%@/%@", kStartFolder, [directoryParts componentsJoinedByString:@"/"]];
    return [mainBundle pathForResource:filename
								ofType:@""
						   inDirectory:directoryStr];
}

- (NSString*) convertDictionaryToJSON:(NSDictionary*)dict {
    // Convert defaults Dictionary to JSON.
    NSError *error;
    NSData *jsonData = [NSJSONSerialization
                        dataWithJSONObject:dict
                        options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                        error:&error];
    
    NSString *jsonString;
    if (! jsonData) {
        NSLog(@"Got an error converting to JSON: %@", error);
    }
    else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    return jsonString;
}

// Convert JavaScript array (arrives as a WebScriptObject) into an NSArray of strings.
- (NSArray*) convertJSarrayToNSArray:(WebScriptObject*)jsArray {
    NSInteger count = [[jsArray valueForKey:@"length"] integerValue];
    
    NSMutableArray *args = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        NSString *item = [jsArray webScriptValueAtIndex:i];
        if ([item isKindOfClass:[NSString class]]) {
            [args addObject:item];
        }
    }
    
    return args;
}

#pragma mark -
#pragma mark Singleton methods

+ (Utils*) sharedInstance
{
    @synchronized(self)
    {
        if (sharedInstance == nil){
			sharedInstance = [[Utils alloc] init];
		 }
    }
    return sharedInstance;
}

+ (id) allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [super allocWithZone:zone];
            return sharedInstance;  // assignment and return on first allocation
        }
    }
    return nil; // on subsequent allocation attempts return nil
}

- (id) copyWithZone:(NSZone *)zone
{
    return self;
}

@end