NetSurf
download.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 Adrien Destugues <pulkomandy@pulkomandy.tk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define __STDBOOL_H__ 1
20#include <stdbool.h>
21#include <sys/types.h>
22#include <stdlib.h>
23
24extern "C" {
25#include "desktop/download.h"
26#include "netsurf/download.h"
27#include "utils/utils.h"
28#include "utils/string.h"
29}
30#include "beos/download.h"
31
32#include <File.h>
33#include <FilePanel.h>
34#include <Locker.h>
35#include <Messenger.h>
36#include <StatusBar.h>
37#include <Window.h>
38
39class NSDownloadWindow: public BWindow
40{
41 public:
44
45 void MessageReceived(BMessage* message);
46
47 void Progress(int size);
48 void Failure(const char* error);
49 void Success();
50 private:
52 BStatusBar* bar;
53 unsigned long progress;
54 bool success;
55};
56
57
61
62 BLocker* storageLock;
63 BDataIO* storage;
64};
65
66
68 : BWindow(BRect(30, 30, 400, 200), "Downloads", B_TITLED_WINDOW,
69 B_NOT_RESIZABLE)
70 , ctx(ctx)
71 , progress(0)
72 , success(false)
73{
74 unsigned long dlsize = download_context_get_total_length(ctx);
75 char* buffer = human_friendly_bytesize(dlsize);
76
77 // Create the status bar
78 BRect rect = Bounds();
79 rect.InsetBy(3, 3);
80 bar = new BStatusBar(rect, "progress",
82 bar->SetMaxValue(dlsize);
83
84 // Create the backgroundview (just so that the area around the progress bar
85 // is B_PANEL_BACKGROUND_COLOR instead of white)
86 BView* back = new BView(Bounds(), "back", B_FOLLOW_ALL_SIDES, B_WILL_DRAW);
87 back->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
88
89 // Add the views to the window
90 back->AddChild(bar);
91 AddChild(back);
92
93 // Resize the window to leave a margin around the progress bar
94 BRect size = bar->Bounds();
95 ResizeTo(size.Width() + 6, size.Height() + 6);
96 Show();
97}
98
99
101{
104}
105
106
107void
109{
110 switch(message->what)
111 {
112 case B_SAVE_REQUESTED:
113 {
114 entry_ref directory;
115 const char* name;
116 struct gui_download_window* dw;
117 BFilePanel* source;
118
119 message->FindRef("directory", &directory);
120 message->FindString("name", &name);
121 message->FindPointer("dw", (void**)&dw);
122
123 BDirectory dir(&directory);
124 BFile* storage = new BFile(&dir, name,
125 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
126 dw->storageLock->Lock();
127
128 BMallocIO* tempstore = dynamic_cast<BMallocIO*>(dw->storage);
129
130 storage->Write(tempstore->Buffer(), tempstore->BufferLength());
131 delete dw->storage;
132
133 if (success)
134 delete storage; // File is already finished downloading !
135 else
136 dw->storage = storage;
137 dw->storageLock->Unlock();
138
139 message->FindPointer("source", (void**)&source);
140 delete source;
141
142 break;
143 }
144 default:
145 BWindow::MessageReceived(message);
146 }
147}
148
149
150void
152{
153 progress += size;
154
156 strcat(buffer, "/");
157
158 bar->LockLooper();
159 bar->Update(size, NULL, buffer);
160 bar->Invalidate();
161 bar->UnlockLooper();
162}
163
164
165void
167{
168 bar->LockLooper();
169 bar->SetBarColor(ui_color(B_SUCCESS_COLOR));
170 bar->UnlockLooper();
171
172 success = true;
173}
174
175
176void
178{
179 bar->LockLooper();
180 bar->Update(0, NULL, error);
181 bar->SetBarColor(ui_color(B_FAILURE_COLOR));
182 bar->UnlockLooper();
183}
184
185
187 struct gui_window *parent)
188{
189 struct gui_download_window *download = (struct gui_download_window*)malloc(sizeof *download);
190 if (download == NULL)
191 return NULL;
192
193 download->storageLock = new BLocker("storage_lock");
194 download->storage = new BMallocIO();
195 download->ctx = ctx;
196
197 download->window = new NSDownloadWindow(ctx);
198
199 // Also ask the user where to save the file
200 BMessage* msg = new BMessage(B_SAVE_REQUESTED);
201
202 BFilePanel* panel = new BFilePanel(B_SAVE_PANEL,
203 new BMessenger(download->window), NULL, 0, false);
204
205 panel->SetSaveText(download_context_get_filename(ctx));
206
207 msg->AddPointer("source", panel);
208 msg->AddPointer("dw", download);
209 panel->SetMessage(msg);
210
211 panel->Show();
212
213 return download;
214}
215
216
218 const char *data, unsigned int size)
219{
220 dw->window->Progress(size);
221
222 dw->storageLock->Lock();
223 dw->storage->Write(data, size);
224 dw->storageLock->Unlock();
225
226 return NSERROR_OK;
227}
228
229
231 const char *error_msg)
232{
233 dw->window->Failure(error_msg);
234
235 delete dw->storageLock;
236 delete dw->storage;
237}
238
239
241{
242 dw->window->Success();
243
244 dw->storageLock->Lock();
245
246 // Only delete if the storage is already a file. Else, we must wait for the
247 // user to select something in the BFilePanel!
248 BFile* file = dynamic_cast<BFile*>(dw->storage);
249 delete file;
250 if (file)
251 delete dw->storageLock;
252 else
253 dw->storageLock->Unlock();
254}
255
261};
262
264
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
void MessageReceived(BMessage *message)
Definition: download.cpp:108
NSDownloadWindow(download_context *ctx)
Definition: download.cpp:67
BStatusBar * bar
Definition: download.cpp:52
download_context * ctx
Definition: download.cpp:51
unsigned long progress
Definition: download.cpp:53
void Failure(const char *error)
Definition: download.cpp:177
void Progress(int size)
Definition: download.cpp:151
void download_context_destroy(download_context *ctx)
Destroy a download context.
Definition: download.c:270
const char * download_context_get_filename(const download_context *ctx)
Retrieve the filename for a download.
Definition: download.c:310
void download_context_abort(download_context *ctx)
Abort a download fetch.
Definition: download.c:285
unsigned long long int download_context_get_total_length(const download_context *ctx)
Retrieve total byte length of download.
Definition: download.c:304
Core download context (interface)
wimp_w parent
Definition: dialog.c:88
struct gui_download_table * beos_download_table
Definition: download.cpp:263
static struct gui_download_table download_table
Definition: download.cpp:256
static nserror gui_download_window_data(struct gui_download_window *dw, const char *data, unsigned int size)
Definition: download.cpp:217
static void gui_download_window_error(struct gui_download_window *dw, const char *error_msg)
Definition: download.cpp:230
static void gui_download_window_done(struct gui_download_window *dw)
Definition: download.cpp:240
static struct gui_download_window * gui_download_window_create(download_context *ctx, struct gui_window *parent)
Definition: download.cpp:186
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
#define B_SUCCESS_COLOR
Definition: gui.cpp:417
#define B_FAILURE_COLOR
Definition: gui.cpp:418
Interface to platform-specific download operations.
Interface to utility string handling.
char * human_friendly_bytesize(unsigned long long int bytesize)
Create a human readable representation of a size in bytes.
Definition: utils.c:209
A context for a download.
Definition: download.c:40
function table for download windows.
Definition: download.h:34
context for each download.
Definition: download.c:91
BLocker * storageLock
Definition: download.cpp:62
os_fw file
RISC OS file handle, of temporary file when !saved, and of destination when saved.
Definition: download.c:109
download_context * ctx
Associated context, or 0 if the fetch has completed or aborted.
Definition: download.cpp:59
BDataIO * storage
Definition: download.cpp:63
struct download_context * ctx
Associated context, or 0 if the fetch has completed or aborted.
Definition: download.c:101
NSDownloadWindow * window
Definition: download.cpp:60
GString * name
Definition: download.c:90
first entry in window list
Definition: gui.c:296
Rectangle coordinates.
Definition: types.h:40
Interface to a number of general purpose functionality.