forked from onflapp/gs-webbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadPanel.m
157 lines (113 loc) · 4.2 KB
/
DownloadPanel.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
/*
Project: WebBrowser
Copyright (C) 2020 Free Software Foundation
Author: onflapp
Created: 2020-07-22 12:41:08 +0300 by root
This application 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 application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#import "DownloadPanel.h"
@implementation DownloadPanel
- (id) initWithURL:(NSURL*)url {
self = [super init];
[NSBundle loadNibNamed:@"DownloadPanel" owner:self];
status = 0;
downloadURL = [url retain];
[openButton setEnabled:NO];
[saveButton setEnabled:NO];
[window setTitle:@"Downloading..."];
[addressField setStringValue:[downloadURL description]];
[statusField setStringValue:@"waiting for download to start..."];
NSURLRequest* req = [NSURLRequest requestWithURL:downloadURL];
downloadConnection = [NSURLConnection connectionWithRequest:req delegate:self];
[downloadConnection retain];
return self;
}
- (void) windowWillClose:(NSNotification*)notification {
status = -1;
[downloadConnection cancel];
[window close];
[self release];
}
- (NSWindow*) window {
return window;
}
- (NSString*) __suggestName:(NSURLResponse*) resp {
NSString* fname = [resp suggestedFilename];
if ([[fname pathExtension] length]) return fname;
NSString* ext = [[resp URL] pathExtension];
if ([ext length]) return [NSString stringWithFormat:@"%@.%@", fname, ext];
NSString* mime = [resp MIMEType];
if ([mime length]) {
NSRange r = [mime rangeOfString:@"/"];
if (r.location != NSNotFound) {
ext = [mime substringFromIndex:r.location+1];
return [NSString stringWithFormat:@"%@.%@", fname, ext];
}
}
return nil;
}
- (IBAction) openFile:(id) sender {
NSWorkspace* ws = [NSWorkspace sharedWorkspace];
if ([tempFile hasSuffix:@".tmp"]) {
NSString* dir = [tempFile stringByDeletingLastPathComponent];
[ws selectFile:tempFile inFileViewerRootedAtPath:dir];
}
else {
[ws openFile:tempFile];
}
[window close];
}
- (IBAction) saveFile:(id) sender {
NSString* dir = [NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"];
NSSavePanel* save = [NSSavePanel savePanel];
NSFileManager* fm = [NSFileManager defaultManager];
NSWorkspace* ws = [NSWorkspace sharedWorkspace];
[save setDirectory:dir];
if ([save runModal]) {
NSString* fl = [save filename];
[fm moveItemAtPath:tempFile toPath:fl error:nil];
[ws selectFile:fl inFileViewerRootedAtPath:dir];
[window close];
}
}
- (void) connection:(NSURLConnection*) con didReceiveResponse:(NSURLResponse*) resp {
NSString* name = [self __suggestName:resp];
if (!name) name = @"download.tmp";
tempFile = [NSString stringWithFormat:@"/tmp/%lx-%@", [self hash], name];
[tempFile retain];
[[NSFileManager defaultManager] createFileAtPath:tempFile contents:nil attributes:nil];
tempFileHandle = [NSFileHandle fileHandleForWritingAtPath:tempFile];
[tempFileHandle retain];
[window setTitle:[NSString stringWithFormat:@"downloading: %@...", [tempFile lastPathComponent]]];
}
- (void) connection:(NSURLConnection*) con didReceiveData:(NSData*) data {
[tempFileHandle writeData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection*) con {
[tempFileHandle closeFile];
[window setTitle:[NSString stringWithFormat:@"downloaded: %@", [tempFile lastPathComponent]]];
[statusField setStringValue:@""];
[openButton setEnabled:YES];
[saveButton setEnabled:YES];
}
- (void) dealloc {
NSLog(@"download dealloc");
status = -1;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[downloadURL release];
[tempFile release];
[tempFileHandle release];
[downloadConnection release];
[super dealloc];
}
@end