-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add download support for MacGap #100
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
#import <Cocoa/Cocoa.h> | ||
#import <WebKit/WebKit.h> | ||
#import "DownloadDelegate.h" | ||
|
||
@class WebViewDelegate; | ||
|
||
@interface ContentView : NSView { | ||
IBOutlet WebView* webView; | ||
WebViewDelegate* delegate; | ||
DownloadDelegate* downloadDelegate; | ||
} | ||
|
||
@property (retain) WebView* webView; | ||
@property (retain) WebViewDelegate* delegate; | ||
@property (retain) DownloadDelegate* downloadDelegate; | ||
@property (strong) IBOutlet NSMenu *mainMenu; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// DownloadDelegate.h | ||
// MacGap | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface DownloadDelegate : NSObject { | ||
NSString *suggestedFilename; | ||
|
||
NSSavePanel *panel; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// DownloadDelegate.m | ||
// MacGap | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
#import "DownloadDelegate.h" | ||
#import "DownloadInfo.h" | ||
|
||
|
||
@implementation DownloadDelegate { | ||
NSMapTable *downloads; | ||
} | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self && NSClassFromString(@"NSProgress")) { | ||
downloads = [NSMapTable weakToStrongObjectsMapTable]; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename | ||
{ | ||
NSString *downloadDir = [NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | ||
NSString *destinationFilename = [downloadDir stringByAppendingPathComponent:filename]; | ||
[download setDestination:destinationFilename allowOverwrite:NO]; | ||
} | ||
|
||
-(void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path | ||
{ | ||
if (downloads) { | ||
NSURL *downloadUrl = [NSURL fileURLWithPath:path]; | ||
DownloadInfo *info = [downloads objectForKey:download]; | ||
[info setFilename:path]; | ||
|
||
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: | ||
NSProgressFileOperationKindDownloading, | ||
NSProgressFileOperationKindKey, | ||
nil]; | ||
NSProgress *progress = [NSProgress progressWithTotalUnitCount:0]; | ||
progress = [progress initWithParent:nil userInfo:userInfo]; | ||
[progress setKind:NSProgressKindFile]; | ||
[progress setUserInfoObject:downloadUrl forKey:NSProgressFileURLKey]; | ||
[progress publish]; | ||
[info setProgress:progress]; | ||
} | ||
} | ||
|
||
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response | ||
{ | ||
if (downloads) { | ||
DownloadInfo *info = [[DownloadInfo alloc] init]; | ||
[info setResponse:response]; | ||
[downloads setObject:info forKey:download]; | ||
} | ||
} | ||
|
||
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(unsigned)length { | ||
if (downloads) { | ||
DownloadInfo *info = [downloads objectForKey:download]; | ||
long long expectedLength = [[info response] expectedContentLength]; | ||
int64_t completed = [[info progress] completedUnitCount]; | ||
[[info progress] setCompletedUnitCount:(completed + length)]; | ||
if (expectedLength != NSURLResponseUnknownLength) [[info progress] setTotalUnitCount:expectedLength]; | ||
} | ||
} | ||
|
||
- (void)downloadDidFinish:(NSURLDownload *)download { | ||
if (downloads) { | ||
DownloadInfo *info = [downloads objectForKey:download]; | ||
[[info progress] unpublish]; | ||
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.DownloadFileFinished" object:[info filename]]; | ||
[downloads removeObjectForKey:download]; | ||
} | ||
} | ||
|
||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// A wrapper class for storing all the information relevant to a particular download. | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface DownloadInfo : NSObject<NSCopying> | ||
|
||
@property NSProgress *progress; | ||
@property NSURLResponse *response; | ||
@property NSString *filename; | ||
|
||
- (id)copyWithZone:(NSZone *)zone; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
|
||
#import "DownloadInfo.h" | ||
|
||
@implementation DownloadInfo | ||
|
||
- (id)copyWithZone:(NSZone *)zone | ||
{ | ||
DownloadInfo *copy = [[[self class] alloc] init]; | ||
|
||
if (copy) { | ||
copy.progress = self.progress; | ||
copy.response = self.response; | ||
copy.filename = self.filename; | ||
} | ||
|
||
return copy; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,7 @@ - (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary | |
switch (tag) | ||
{ | ||
case WebMenuItemTagOpenLinkInNewWindow: | ||
case WebMenuItemTagDownloadLinkToDisk: | ||
//case WebMenuItemTagDownloadLinkToDisk: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to remove commented line. |
||
case WebMenuItemTagCopyLinkToClipboard: | ||
case WebMenuItemTagOpenImageInNewWindow: | ||
case WebMenuItemTagDownloadImageToDisk: | ||
|
@@ -196,4 +196,25 @@ + (BOOL) isKeyExcludedFromWebScript:(const char*)name | |
} | ||
|
||
|
||
/* | ||
* Download everything except HTML pages. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this needs to be mandatory for all use-cases? |
||
*/ | ||
- (void)webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type | ||
request:(NSURLRequest *)request | ||
frame:(WebFrame *)frame | ||
decisionListener:(id < WebPolicyDecisionListener >)listener | ||
{ | ||
NSLog (@"%@", type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to remove NSLog. |
||
if([type isEqualToString:@"text/html"]) | ||
{ | ||
|
||
[listener use]; | ||
} | ||
else{ | ||
[listener download]; | ||
}; | ||
} | ||
|
||
|
||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to see some use of our JSEventHelper's triggerEvent — I think there are actually several places in your code where it could be really handy to fire off some JS events.