Skip to content

Commit 5117ff6

Browse files
committed
Add basic file read/write capabilities. Currently supports text/string based files, images will be added soon.
1 parent 9318db2 commit 5117ff6

File tree

7 files changed

+133
-2
lines changed

7 files changed

+133
-2
lines changed

MG.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
2237E680192B018D001E1393 /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2237E67F192B018D001E1393 /* JavaScriptCore.framework */; };
1313
2237E6EF192BAF85001E1393 /* public in Resources */ = {isa = PBXBuildFile; fileRef = 2237E6EE192BAF85001E1393 /* public */; };
1414
226AE65D193AA8FC00B15FB7 /* Sound.m in Sources */ = {isa = PBXBuildFile; fileRef = 226AE65C193AA8FC00B15FB7 /* Sound.m */; };
15+
22A676E71944B140002F81FB /* File.m in Sources */ = {isa = PBXBuildFile; fileRef = 22A676E61944B140002F81FB /* File.m */; };
1516
22B1CF2B1937734B00D2C294 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 22B1CEF51937734B00D2C294 /* AppDelegate.m */; };
1617
22B1CF2C1937734B00D2C294 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 22B1CEF61937734B00D2C294 /* MainMenu.xib */; };
1718
22B1CF2D1937734B00D2C294 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 22B1CEF81937734B00D2C294 /* MainWindow.xib */; };
@@ -53,6 +54,8 @@
5354
2237E6EE192BAF85001E1393 /* public */ = {isa = PBXFileReference; lastKnownFileType = folder; path = public; sourceTree = "<group>"; };
5455
226AE65B193AA8FC00B15FB7 /* Sound.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Sound.h; sourceTree = "<group>"; };
5556
226AE65C193AA8FC00B15FB7 /* Sound.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Sound.m; sourceTree = "<group>"; };
57+
22A676E51944B140002F81FB /* File.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = File.h; sourceTree = "<group>"; };
58+
22A676E61944B140002F81FB /* File.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = File.m; sourceTree = "<group>"; };
5659
22B1CEF41937734B00D2C294 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
5760
22B1CEF51937734B00D2C294 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
5861
22B1CEF71937734B00D2C294 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
@@ -232,6 +235,8 @@
232235
226AE65C193AA8FC00B15FB7 /* Sound.m */,
233236
22BE36EB193B7F8800D8199F /* Defaults.h */,
234237
22BE36EC193B7F8800D8199F /* Defaults.m */,
238+
22A676E51944B140002F81FB /* File.h */,
239+
22A676E61944B140002F81FB /* File.m */,
235240
);
236241
path = Commands;
237242
sourceTree = "<group>";
@@ -333,6 +338,7 @@
333338
22B1CF381937734B00D2C294 /* MenuItem.m in Sources */,
334339
22B1CF301937734B00D2C294 /* App.m in Sources */,
335340
22B1CF371937734B00D2C294 /* Menu.m in Sources */,
341+
22A676E71944B140002F81FB /* File.m in Sources */,
336342
22B1CF3C1937734B00D2C294 /* Window.m in Sources */,
337343
22B1CF3F1937734B00D2C294 /* WindowController.m in Sources */,
338344
22B1CF3D1937734B00D2C294 /* Utils.m in Sources */,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
6+
<true/>
7+
<key>SnapshotAutomaticallyBeforeSignificantChanges</key>
8+
<true/>
9+
</dict>
10+
</plist>

MacGap/Classes/Commands/Event.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
+ (void) triggerEvent:(NSString *)event forWebView:(WebView *)webView;
2020
+ (void) triggerEvent:(NSString *)event withArgs:(NSDictionary *)args forWebView:(WebView *)webView;
2121
+ (void) triggerEvent:(NSString *)event withArgs:(NSDictionary *)args forObject:(NSString *)objName forWebView:(WebView *)webView;
22-
22+
+ (void) triggerDomEvent:(NSString *)event withArgs:(NSDictionary *)args forWebView:(WebView *)webView;
2323

2424
@end

MacGap/Classes/Commands/Event.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ + (void) triggerEvent:(NSString *)event withArgs:(NSDictionary *)args forObject:
2525
[webView stringByEvaluatingJavaScriptFromString:str];
2626
}
2727

28+
+ (void) triggerDomEvent:(NSString *)event withArgs:(NSDictionary *)args forWebView:(WebView *)webView {
29+
30+
NSString * str = [NSString stringWithFormat:@"var e = new CustomEvent('%@', %@); document.dispatchEvent(e);", event, args.JSONString];
31+
[webView stringByEvaluatingJavaScriptFromString:str];
32+
}
33+
2834
@end

MacGap/Classes/Commands/File.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// File.h
3+
// MG
4+
//
5+
// Created by Tim Debo on 6/8/14.
6+
//
7+
//
8+
9+
#import "Command.h"
10+
@protocol FileExports <JSExport>
11+
12+
JSExportAs(write, - (void) writeFile:(NSString*)filePath withData: (JSValue*) data andType: (NSString*) type );
13+
JSExportAs(read, - (JSValue*) readFile:(NSString*)filePath ofType: (NSString*) type);
14+
15+
@end
16+
17+
@interface File : Command <FileExports>
18+
19+
@end

MacGap/Classes/Commands/File.m

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// File.m
3+
// MG
4+
//
5+
// Created by Tim Debo on 6/8/14.
6+
//
7+
//
8+
9+
#import "File.h"
10+
#import "WindowController.h"
11+
12+
@interface File ()
13+
14+
@property (nonatomic) NSArray* acceptedTypes;
15+
16+
@end
17+
18+
@implementation File
19+
20+
- (id) initWithWindowController:(WindowController *)aWindowController
21+
{
22+
self = [super init];
23+
24+
if(self) {
25+
self.windowController = aWindowController;
26+
self.webView = aWindowController.webView;
27+
_acceptedTypes = @[ @"string", @"json", @"image"];
28+
29+
}
30+
31+
return self;
32+
}
33+
34+
35+
- (void) writeFile:(NSString*)filePath withData: (JSValue*) data andType: (NSString*) type
36+
{
37+
38+
if([_acceptedTypes containsObject:type]){
39+
40+
if([type isEqualToString:@"string"]) {
41+
NSString* valToWrite = [data toString];
42+
[valToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
43+
} else if([type isEqualToString:@"json"]) {
44+
45+
NSDictionary* dictData = [data toDictionary];
46+
NSString* valToWrite = [dictData JSONString];
47+
48+
[valToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
49+
} else {
50+
//TODO
51+
// NSString* base64Data = [data toString];
52+
// NSData * valToWrite = [NSData dataFromBase64String:base64Data];
53+
}
54+
}
55+
56+
}
57+
58+
- (JSValue*) readFile:(NSString*)filePath ofType: (NSString*) type
59+
{
60+
61+
if([_acceptedTypes containsObject:type]) {
62+
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
63+
if([type isEqualToString:@"string"]) {
64+
65+
if(fileExists) {
66+
NSString* val = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
67+
return [JSValue valueWithObject:val inContext:[JSContext currentContext]];
68+
}
69+
70+
} else if([type isEqualToString:@"json"]) {
71+
72+
if(fileExists) {
73+
NSDictionary* val = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL] JSONObject];
74+
return [JSValue valueWithObject:val inContext:[JSContext currentContext]];
75+
}
76+
} else {
77+
//TODO
78+
// if(fileExists) {
79+
// NSDictionary* val = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL] JSONObject];
80+
// return [JSValue valueWithObject:val inContext:[JSContext currentContext]];
81+
// }
82+
}
83+
} else {
84+
return nil;
85+
}
86+
return nil;
87+
}
88+
89+
@end

public/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"StatusItem":"StatusItem",
1313
"Task" : "Task",
1414
"Sound" : "Sound",
15-
"Defaults" : "Defaults"
15+
"Defaults" : "Defaults",
16+
"File" : "File"
1617
},
1718
"window" : {
1819
"title" : "MacGap",

0 commit comments

Comments
 (0)