2003-08-28 23:21:27 +04:00
|
|
|
/*
|
2004-02-26 03:44:42 +03:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
|
|
|
* Copyright 2003 Rob Jackson <jacko@xms.ms>
|
2005-05-02 06:11:06 +04:00
|
|
|
* Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-08-28 23:21:27 +04:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/** \file
|
|
|
|
* Download windows (RISC OS implementation).
|
|
|
|
*
|
|
|
|
* This file implements the interface given by desktop/gui.h for download
|
|
|
|
* windows. Each download window has an associated fetch. Downloads start by
|
|
|
|
* writing received data to a temporary file. At some point the user chooses
|
|
|
|
* a destination (by drag & drop), and the temporary file is then moved to the
|
|
|
|
* destination and the download continues until complete.
|
|
|
|
*/
|
|
|
|
|
2003-08-28 23:21:27 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2004-06-28 03:24:11 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
2008-05-24 05:19:21 +04:00
|
|
|
#include <curl/curl.h>
|
2003-08-28 23:21:27 +04:00
|
|
|
#include "oslib/mimemap.h"
|
2004-06-28 03:24:11 +04:00
|
|
|
#include "oslib/osargs.h"
|
2004-02-26 03:44:42 +03:00
|
|
|
#include "oslib/osfile.h"
|
2004-06-28 03:24:11 +04:00
|
|
|
#include "oslib/osfind.h"
|
|
|
|
#include "oslib/osfscontrol.h"
|
|
|
|
#include "oslib/osgbpb.h"
|
2003-08-28 23:21:27 +04:00
|
|
|
#include "oslib/wimp.h"
|
|
|
|
#include "oslib/wimpspriteop.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "content/fetch.h"
|
|
|
|
#include "desktop/gui.h"
|
|
|
|
#include "desktop/netsurf.h"
|
|
|
|
#include "riscos/dialog.h"
|
|
|
|
#include "riscos/options.h"
|
|
|
|
#include "riscos/save.h"
|
|
|
|
#include "riscos/query.h"
|
|
|
|
#include "riscos/wimp.h"
|
|
|
|
#include "riscos/wimp_event.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/url.h"
|
|
|
|
#include "utils/utf8.h"
|
|
|
|
#include "utils/utils.h"
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
#define ICON_DOWNLOAD_ICON 0
|
|
|
|
#define ICON_DOWNLOAD_URL 1
|
|
|
|
#define ICON_DOWNLOAD_PATH 2
|
|
|
|
#define ICON_DOWNLOAD_DESTINATION 3
|
|
|
|
#define ICON_DOWNLOAD_PROGRESS 5
|
|
|
|
#define ICON_DOWNLOAD_STATUS 6
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
QueryRsn_Quit,
|
|
|
|
QueryRsn_Abort,
|
|
|
|
QueryRsn_Overwrite
|
|
|
|
} query_reason;
|
|
|
|
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/** Data for a download window. */
|
|
|
|
struct gui_download_window {
|
|
|
|
/** Associated fetch, or 0 if the fetch has completed or aborted. */
|
|
|
|
struct fetch *fetch;
|
|
|
|
unsigned int received; /**< Amount of data received so far. */
|
|
|
|
unsigned int total_size; /**< Size of resource, or 0 if unknown. */
|
|
|
|
|
|
|
|
wimp_w window; /**< RISC OS window handle. */
|
|
|
|
bits file_type; /**< RISC OS file type. */
|
|
|
|
|
|
|
|
char url[256]; /**< Buffer for URL icon. */
|
|
|
|
char sprite_name[20]; /**< Buffer for sprite icon. */
|
|
|
|
char path[256]; /**< Buffer for pathname icon. */
|
|
|
|
char status[256]; /**< Buffer for status icon. */
|
|
|
|
|
|
|
|
/** User has chosen the destination, and it is being written. */
|
|
|
|
bool saved;
|
2005-04-23 06:58:27 +04:00
|
|
|
bool close_confirmed;
|
2004-06-28 03:24:11 +04:00
|
|
|
bool error; /**< Error occurred, aborted. */
|
2006-02-07 19:16:23 +03:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/** RISC OS file handle, of temporary file when !saved, and of
|
|
|
|
* destination when saved. */
|
|
|
|
os_fw file;
|
|
|
|
|
2005-04-23 06:58:27 +04:00
|
|
|
query_id query;
|
2006-02-07 19:16:23 +03:00
|
|
|
query_reason query_rsn;
|
2005-04-23 06:58:27 +04:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
struct timeval start_time; /**< Time download started. */
|
|
|
|
struct timeval last_time; /**< Time status was last updated. */
|
|
|
|
unsigned int last_received; /**< Value of received at last_time. */
|
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
bool send_dataload; /**< Should send DataLoad message when finished */
|
|
|
|
wimp_message save_message; /**< Copy of wimp DataSaveAck message */
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
struct gui_download_window *prev; /**< Previous in linked list. */
|
|
|
|
struct gui_download_window *next; /**< Next in linked list. */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** List of all download windows. */
|
|
|
|
static struct gui_download_window *download_window_list = 0;
|
|
|
|
/** Download window with current save operation. */
|
|
|
|
static struct gui_download_window *download_window_current = 0;
|
|
|
|
|
|
|
|
/** Template for a download window. */
|
2003-08-28 23:21:27 +04:00
|
|
|
static wimp_window *download_template;
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/** Width of progress bar at 100%. */
|
|
|
|
static int download_progress_width;
|
|
|
|
/** Coordinates of progress bar. */
|
|
|
|
static int download_progress_x0;
|
|
|
|
static int download_progress_y0;
|
|
|
|
static int download_progress_y1;
|
|
|
|
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
static const char *ro_gui_download_temp_name(struct gui_download_window *dw);
|
2004-06-28 03:24:11 +04:00
|
|
|
static void ro_gui_download_update_status(struct gui_download_window *dw);
|
|
|
|
static void ro_gui_download_update_status_wrapper(void *p);
|
2005-07-08 18:53:53 +04:00
|
|
|
static void ro_gui_download_window_hide_caret(struct gui_download_window *dw);
|
|
|
|
static char *ro_gui_download_canonicalise(const char *path);
|
|
|
|
static bool ro_gui_download_check_space(struct gui_download_window *dw,
|
|
|
|
const char *dest_file, const char *orig_file);
|
|
|
|
static os_error *ro_gui_download_move(struct gui_download_window *dw,
|
|
|
|
const char *dest_file, const char *src_file);
|
2006-02-07 19:16:23 +03:00
|
|
|
static bool ro_gui_download_save(struct gui_download_window *dw,
|
|
|
|
const char *file_name, bool force_overwrite);
|
2005-05-02 06:11:06 +04:00
|
|
|
static void ro_gui_download_send_dataload(struct gui_download_window *dw);
|
2004-06-28 03:24:11 +04:00
|
|
|
static void ro_gui_download_window_destroy_wrapper(void *p);
|
2006-09-06 18:44:42 +04:00
|
|
|
static bool ro_gui_download_window_destroy(struct gui_download_window *dw, bool quit);
|
2005-04-23 06:58:27 +04:00
|
|
|
static void ro_gui_download_close_confirmed(query_id, enum query_response res, void *p);
|
|
|
|
static void ro_gui_download_close_cancelled(query_id, enum query_response res, void *p);
|
2006-02-07 19:16:23 +03:00
|
|
|
static void ro_gui_download_overwrite_confirmed(query_id, enum query_response res, void *p);
|
|
|
|
static void ro_gui_download_overwrite_cancelled(query_id, enum query_response res, void *p);
|
2005-04-23 06:58:27 +04:00
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
static bool ro_gui_download_click(wimp_pointer *pointer);
|
|
|
|
static bool ro_gui_download_keypress(wimp_key *key);
|
|
|
|
static void ro_gui_download_close(wimp_w w);
|
|
|
|
|
2005-04-23 06:58:27 +04:00
|
|
|
static const query_callback close_funcs =
|
|
|
|
{
|
|
|
|
ro_gui_download_close_confirmed,
|
|
|
|
ro_gui_download_close_cancelled
|
|
|
|
};
|
2004-06-28 03:24:11 +04:00
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
static const query_callback overwrite_funcs =
|
|
|
|
{
|
|
|
|
ro_gui_download_overwrite_confirmed,
|
|
|
|
ro_gui_download_overwrite_cancelled
|
|
|
|
};
|
|
|
|
|
2003-08-28 23:21:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the download window template.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_init(void)
|
|
|
|
{
|
2004-05-09 00:44:00 +04:00
|
|
|
download_template = ro_gui_dialog_load_template("download");
|
2004-06-28 03:24:11 +04:00
|
|
|
download_progress_width =
|
|
|
|
download_template->icons[ICON_DOWNLOAD_STATUS].extent.x1 -
|
|
|
|
download_template->icons[ICON_DOWNLOAD_STATUS].extent.x0;
|
|
|
|
download_progress_x0 =
|
|
|
|
download_template->icons[ICON_DOWNLOAD_PROGRESS].extent.x0;
|
|
|
|
download_progress_y0 =
|
|
|
|
download_template->icons[ICON_DOWNLOAD_PROGRESS].extent.y0;
|
|
|
|
download_progress_y1 =
|
|
|
|
download_template->icons[ICON_DOWNLOAD_PROGRESS].extent.y1;
|
2003-08-28 23:21:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
/**
|
|
|
|
* Returns the pathname of a temporary file for this download.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
* \return ptr to pathname
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *ro_gui_download_temp_name(struct gui_download_window *dw)
|
|
|
|
{
|
|
|
|
static char temp_name[40];
|
|
|
|
snprintf(temp_name, sizeof temp_name, "<Wimp$ScrapDir>.ns%x",
|
|
|
|
(unsigned int) dw);
|
|
|
|
return temp_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 23:21:27 +04:00
|
|
|
/**
|
|
|
|
* Create and open a download progress window.
|
2004-06-28 03:24:11 +04:00
|
|
|
*
|
|
|
|
* \param url URL of download
|
|
|
|
* \param mime_type MIME type sent by server
|
|
|
|
* \param fetch fetch structure
|
|
|
|
* \param total_size size of resource, or 0 if unknown
|
|
|
|
* \return a new gui_download_window structure, or 0 on error and error
|
|
|
|
* reported
|
2003-08-28 23:21:27 +04:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
struct gui_download_window *gui_download_window_create(const char *url,
|
|
|
|
const char *mime_type, struct fetch *fetch,
|
Merged revisions 4282-4285,4288-4293,4297-4298,4307,4309-4313,4322,4324-4680 via svnmerge from
svn://svn.netsurf-browser.org/branches/mikeL/netsurf
........
r4432 | mikeL | 2008-06-24 04:00:36 +0100 (Tue, 24 Jun 2008) | 1 line
Drag events are now emited from where the press originated, instead of from where they became a drag
........
r4433 | mikeL | 2008-06-24 04:25:33 +0100 (Tue, 24 Jun 2008) | 1 line
Added accelerator to 'Select All'
........
r4495 | mikeL | 2008-07-02 21:36:32 +0100 (Wed, 02 Jul 2008) | 1 line
Selections are now deleted and replaced when a key is typed in a text area or text box. All input box behavior while a selection is active is now implemented (ex: pressing the right arrow key moves the caret to the end of the selection). Cut now works properly in both versions. Fixed discrepancy between where the caret was placed and selection began when starting a drag-select. Fixed bug with calculation of a selections end box.
........
r4496 | mikeL | 2008-07-02 22:11:24 +0100 (Wed, 02 Jul 2008) | 1 line
Added support for cut in input boxes
........
r4497 | mikeL | 2008-07-02 22:21:35 +0100 (Wed, 02 Jul 2008) | 1 line
Removed unused variables (Thanks tlsa)
........
r4498 | mikeL | 2008-07-02 23:30:30 +0100 (Wed, 02 Jul 2008) | 1 line
Modified selection clearing behavior to allow for drag-saves
........
r4499 | mikeL | 2008-07-03 00:51:50 +0100 (Thu, 03 Jul 2008) | 1 line
Fixed regression where it would take two clicks to place caret in an input (Thanks tlsa)
........
r4509 | mikeL | 2008-07-06 07:55:09 +0100 (Sun, 06 Jul 2008) | 1 line
Basic download support implemented. Only downloading of text files works
........
r4510 | mikeL | 2008-07-06 18:55:31 +0100 (Sun, 06 Jul 2008) | 1 line
Downloading of non-text files is now possible. Progress bar and size downloaded are now updated
........
r4511 | mikeL | 2008-07-06 20:46:00 +0100 (Sun, 06 Jul 2008) | 1 line
Added downloads glade file
........
r4512 | mikeL | 2008-07-06 20:47:39 +0100 (Sun, 06 Jul 2008) | 1 line
Downloads window now spawns in the center of the parent browser window
........
r4513 | mikeL | 2008-07-06 20:56:12 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed bug where backspace would be ignored if the selection began at the beginning on an input
........
r4514 | mikeL | 2008-07-06 21:26:45 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed compiler warnings by adding casts
........
r4516 | mikeL | 2008-07-06 21:32:41 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed initialization of size string, added initialization of progress
........
r4518 | mikeL | 2008-07-06 21:51:08 +0100 (Sun, 06 Jul 2008) | 1 line
Added an option for short units (with space) to human_friendly_bytesize
........
r4519 | mikeL | 2008-07-06 21:52:05 +0100 (Sun, 06 Jul 2008) | 1 line
Removed function size_to_string
........
r4520 | mikeL | 2008-07-06 22:03:11 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed pedantic error (kB instead of KB). Added missing necessary parameters to human_friendly_bytesize. Fixed incorrect bool types
........
r4521 | mikeL | 2008-07-06 22:08:15 +0100 (Sun, 06 Jul 2008) | 1 line
Removed unnecessary parameter and units list from human_friendly_bytesize
........
r4522 | mikeL | 2008-07-06 22:57:03 +0100 (Sun, 06 Jul 2008) | 1 line
Removed unnused variable
........
r4523 | mikeL | 2008-07-06 23:03:46 +0100 (Sun, 06 Jul 2008) | 1 line
Fixed url parsing by replacing url_parse_filename with url_nice. Total size and size downloaded are now in human readable form. Speed is now calculated (roughly)
........
r4524 | mikeL | 2008-07-07 01:19:01 +0100 (Mon, 07 Jul 2008) | 1 line
Added file overwrite confirmation. Changed speed to a double
........
r4546 | mikeL | 2008-07-09 17:21:43 +0100 (Wed, 09 Jul 2008) | 1 line
Changed parameter of selection_get_end/start to a size_t instead of int and changed all offset variables to size_t as well
........
r4547 | mikeL | 2008-07-09 17:30:47 +0100 (Wed, 09 Jul 2008) | 1 line
Added action buttons to the bottom toolbar. Added ability to clear selected (and completed) downloads with a framework for other actions.
........
r4556 | jmb | 2008-07-10 00:17:24 +0100 (Thu, 10 Jul 2008) | 3 lines
A large bunch of tidying and general fixes to text input handling.
Make selection code treat password fields as inputs, too.
........
r4557 | mikeL | 2008-07-10 00:24:46 +0100 (Thu, 10 Jul 2008) | 1 line
Added functionality to gui_empty_clipboard and gui_start_selection (Thanks jmb)
........
r4573 | mikeL | 2008-07-10 16:33:27 +0100 (Thu, 10 Jul 2008) | 1 line
Removed example download. Made the list store row aware of its download and vise versa. Added new way of handling actions from the dialog (e.g. buttons) which handles all rows in the selection. Added a few memory management function calls to clean up better
........
r4577 | mikeL | 2008-07-10 21:11:51 +0100 (Thu, 10 Jul 2008) | 1 line
Download write channels now close properly. Added status column to the tree store which will change the progress bar text if a download is canceled or completed. Implemented cancel button functionality.
........
r4578 | mikeL | 2008-07-10 21:17:51 +0100 (Thu, 10 Jul 2008) | 1 line
Speed is now displayed as '-' when 0 or download has stopped
........
r4580 | mikeL | 2008-07-11 02:10:57 +0100 (Fri, 11 Jul 2008) | 1 line
Added two download related options (Download directory & Clear completed downloads) and a Downloads tab to the preferences dialog. Also moved the option to ask when overwriting files to Downloads tab. Added another option to the pre-download dialog, Save, which downloads the file immediately to your 'Download directory'
........
r4581 | mikeL | 2008-07-11 02:26:00 +0100 (Fri, 11 Jul 2008) | 1 line
Rearranged pre-download dialog buttons to conform to the HIG
........
r4616 | mikeL | 2008-07-11 19:54:12 +0100 (Fri, 11 Jul 2008) | 1 line
Limited download window updates to a user-defined variable that can be set in options (default is .5). Updates are now only done if the download window is visible. This greatly reduces the cpu usage.
........
r4617 | mikeL | 2008-07-11 20:07:48 +0100 (Fri, 11 Jul 2008) | 1 line
Removed unnecessary update limit option (it is now fixed at .5)
........
r4629 | mikeL | 2008-07-13 04:21:07 +0100 (Sun, 13 Jul 2008) | 1 line
Reorganized button sensitivity functions. Sensitivities are now updated when the selection changes as well as when a selected download's state changes.
........
r4635 | mikeL | 2008-07-13 17:00:05 +0100 (Sun, 13 Jul 2008) | 1 line
Added error handling. Added word-wrap to the "info" cell renderer so that to keep everything under control. Fixed bug where downloads would always go to you default folder (missing breaks).
........
r4642 | mikeL | 2008-07-13 21:46:09 +0100 (Sun, 13 Jul 2008) | 1 line
Added time remaining column. Fixed regression where the download info would be erased upon completion/cancelation.
........
r4655 | mikeL | 2008-07-14 23:20:33 +0100 (Mon, 14 Jul 2008) | 1 line
Downloads dialog is now initialized in gtk_gui.c with no parent window. The parent windows are now set when a download is created (through an extra parameter in gui_download_window_create) and when nsgtk_download_show is called. When it is closed (when NS shuts down) all incomplete downloads are canceled (and the files deleted). Added a warning dialog when netsurf tries to close with incomplete downloads. Fixed bug where default save directory would initialize to NULL.
........
r4676 | mikeL | 2008-07-15 21:01:17 +0100 (Tue, 15 Jul 2008) | 1 line
Downloads dialog is now initialized in gtk_gui.c with no parent window. The parent windows are now set when a download is created (through an extra parameter in gui_download_window_create) and when nsgtk_download_show is called. (This is the second half of the patch, last commit was only partial for some reason)
........
r4678 | mikeL | 2008-07-16 01:18:52 +0100 (Wed, 16 Jul 2008) | 1 line
Addresses almost all of rjek and jmb's concerns, fixes most of the sloppiness that was present earlier. Downloads without a total_size are now handled correctly (thanks tlsa). Changes to the default download directly are now saved correctly. Billions of other small bugs fixed
........
svn path=/trunk/netsurf/; revision=4681
2008-07-16 14:19:30 +04:00
|
|
|
unsigned int total_size, struct gui_window *gui)
|
2003-08-28 23:21:27 +04:00
|
|
|
{
|
2005-07-08 18:53:53 +04:00
|
|
|
const char *temp_name;
|
2008-05-23 17:29:37 +04:00
|
|
|
char *nice, *scheme = NULL;
|
2004-06-28 03:24:11 +04:00
|
|
|
struct gui_download_window *dw;
|
2005-07-08 18:53:53 +04:00
|
|
|
bool space_warning = false;
|
2004-06-28 03:24:11 +04:00
|
|
|
os_error *error;
|
2004-08-09 20:11:58 +04:00
|
|
|
url_func_result res;
|
2005-07-17 01:46:47 +04:00
|
|
|
char *local_path;
|
|
|
|
utf8_convert_ret err;
|
2005-07-24 00:43:37 +04:00
|
|
|
size_t i;
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
dw = malloc(sizeof *dw);
|
|
|
|
if (!dw) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->fetch = fetch;
|
|
|
|
dw->saved = false;
|
2005-04-23 06:58:27 +04:00
|
|
|
dw->close_confirmed = false;
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->error = false;
|
2005-04-23 06:58:27 +04:00
|
|
|
dw->query = QUERY_INVALID;
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->received = 0;
|
|
|
|
dw->total_size = total_size;
|
|
|
|
strncpy(dw->url, url, sizeof dw->url);
|
|
|
|
dw->url[sizeof dw->url - 1] = 0;
|
|
|
|
dw->status[0] = 0;
|
|
|
|
gettimeofday(&dw->start_time, 0);
|
|
|
|
dw->last_time = dw->start_time;
|
|
|
|
dw->last_received = 0;
|
2008-05-23 17:29:37 +04:00
|
|
|
dw->file_type = 0;
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2008-05-23 17:29:37 +04:00
|
|
|
/* Get scheme from URL */
|
|
|
|
res = url_scheme(url, &scheme);
|
|
|
|
if (res == URL_FUNC_NOMEM) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
free(dw);
|
|
|
|
return 0;
|
|
|
|
} else if (res == URL_FUNC_OK) {
|
2008-05-24 05:19:21 +04:00
|
|
|
/* If we have a scheme and it's "file", then
|
2008-05-23 17:29:37 +04:00
|
|
|
* attempt to use the local filetype directly */
|
|
|
|
if (strcasecmp(scheme, "file") == 0) {
|
|
|
|
char *path = NULL;
|
|
|
|
res = url_path(url, &path);
|
|
|
|
if (res == URL_FUNC_NOMEM) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
free(scheme);
|
|
|
|
free(dw);
|
|
|
|
return 0;
|
|
|
|
} else if (res == URL_FUNC_OK) {
|
2008-05-24 05:19:21 +04:00
|
|
|
char *raw_path = curl_unescape(path,
|
|
|
|
strlen(path));
|
|
|
|
if (raw_path == NULL) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
free(path);
|
|
|
|
free(scheme);
|
|
|
|
free(dw);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dw->file_type =
|
|
|
|
ro_filetype_from_unix_path(raw_path);
|
|
|
|
curl_free(raw_path);
|
2008-05-23 17:29:37 +04:00
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(scheme);
|
|
|
|
}
|
|
|
|
|
2008-05-24 05:19:21 +04:00
|
|
|
/* If we still don't have a filetype (i.e. failed reading local
|
2008-05-23 17:29:37 +04:00
|
|
|
* one or fetching a remote object), then use the MIME type */
|
|
|
|
if (dw->file_type == 0) {
|
|
|
|
/* convert MIME type to RISC OS file type */
|
|
|
|
error = xmimemaptranslate_mime_type_to_filetype(mime_type,
|
|
|
|
&(dw->file_type));
|
|
|
|
if (error) {
|
|
|
|
LOG(("xmimemaptranslate_mime_type_to_filetype: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("MiscError", error->errmess);
|
|
|
|
dw->file_type = 0xffd;
|
|
|
|
}
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open temporary output file */
|
2005-07-08 18:53:53 +04:00
|
|
|
temp_name = ro_gui_download_temp_name(dw);
|
|
|
|
if (!ro_gui_download_check_space(dw, temp_name, NULL)) {
|
|
|
|
/* issue a warning but continue with the download because the
|
|
|
|
user can save it to another medium whilst it's downloading */
|
|
|
|
space_warning = true;
|
|
|
|
}
|
2004-06-28 03:24:11 +04:00
|
|
|
error = xosfind_openoutw(osfind_NO_PATH | osfind_ERROR_IF_DIR,
|
|
|
|
temp_name, 0, &dw->file);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfind_openoutw: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
free(dw);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-08-28 23:21:27 +04:00
|
|
|
|
|
|
|
/* fill in download window icons */
|
|
|
|
download_template->icons[ICON_DOWNLOAD_URL].data.indirected_text.text =
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->url;
|
2003-08-28 23:21:27 +04:00
|
|
|
download_template->icons[ICON_DOWNLOAD_URL].data.indirected_text.size =
|
2004-06-28 03:24:11 +04:00
|
|
|
sizeof dw->url;
|
|
|
|
|
|
|
|
download_template->icons[ICON_DOWNLOAD_STATUS].data.indirected_text.
|
|
|
|
text = dw->status;
|
|
|
|
download_template->icons[ICON_DOWNLOAD_STATUS].data.indirected_text.
|
|
|
|
size = sizeof dw->status;
|
|
|
|
|
|
|
|
sprintf(dw->sprite_name, "file_%.3x", dw->file_type);
|
2004-07-11 17:05:38 +04:00
|
|
|
if (!ro_gui_wimp_sprite_exists(dw->sprite_name))
|
2004-06-28 03:24:11 +04:00
|
|
|
strcpy(dw->sprite_name, "file_xxx");
|
2004-02-26 03:44:42 +03:00
|
|
|
download_template->icons[ICON_DOWNLOAD_ICON].data.indirected_sprite.id =
|
2004-06-28 03:24:11 +04:00
|
|
|
(osspriteop_id) dw->sprite_name;
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
if ((res = url_nice(url, &nice, option_strip_extensions)) ==
|
|
|
|
URL_FUNC_OK) {
|
|
|
|
for (i = 0; nice[i]; i++) {
|
|
|
|
if (nice[i] == '.')
|
|
|
|
nice[i] = '/';
|
|
|
|
else if (nice[i] <= ' ' ||
|
|
|
|
strchr(":*#$&@^%\\", nice[i]))
|
|
|
|
nice[i] = '_';
|
|
|
|
}
|
|
|
|
strncpy(dw->path, nice, sizeof dw->path);
|
2004-06-06 23:57:16 +04:00
|
|
|
free(nice);
|
|
|
|
}
|
2005-05-02 06:11:06 +04:00
|
|
|
else
|
|
|
|
strcpy(dw->path, messages_get("SaveObject"));
|
|
|
|
|
2005-07-17 01:46:47 +04:00
|
|
|
err = utf8_to_local_encoding(dw->path, 0, &local_path);
|
|
|
|
if (err != UTF8_CONVERT_OK) {
|
|
|
|
/* badenc should never happen */
|
|
|
|
assert(err != UTF8_CONVERT_BADENC);
|
|
|
|
LOG(("utf8_to_local_encoding failed"));
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
free(dw);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strncpy(dw->path, local_path, sizeof dw->path);
|
|
|
|
free(local_path);
|
|
|
|
}
|
|
|
|
|
2003-08-28 23:21:27 +04:00
|
|
|
download_template->icons[ICON_DOWNLOAD_PATH].data.indirected_text.text =
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->path;
|
2003-08-28 23:21:27 +04:00
|
|
|
download_template->icons[ICON_DOWNLOAD_PATH].data.indirected_text.size =
|
2004-06-28 03:24:11 +04:00
|
|
|
sizeof dw->path;
|
|
|
|
|
|
|
|
download_template->icons[ICON_DOWNLOAD_DESTINATION].data.
|
|
|
|
indirected_text.text = dw->path;
|
|
|
|
download_template->icons[ICON_DOWNLOAD_DESTINATION].data.
|
|
|
|
indirected_text.size = sizeof dw->path;
|
|
|
|
|
|
|
|
download_template->icons[ICON_DOWNLOAD_DESTINATION].flags |=
|
|
|
|
wimp_ICON_DELETED;
|
2003-08-28 23:21:27 +04:00
|
|
|
|
|
|
|
/* create and open the download window */
|
2004-06-28 03:24:11 +04:00
|
|
|
error = xwimp_create_window(download_template, &dw->window);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_create_window: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
free(dw);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dw->prev = 0;
|
|
|
|
dw->next = download_window_list;
|
2004-07-18 21:38:01 +04:00
|
|
|
if (download_window_list)
|
|
|
|
download_window_list->prev = dw;
|
2004-06-28 03:24:11 +04:00
|
|
|
download_window_list = dw;
|
|
|
|
|
|
|
|
ro_gui_download_update_status(dw);
|
|
|
|
|
|
|
|
ro_gui_dialog_open(dw->window);
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
ro_gui_wimp_event_set_user_data(dw->window, dw);
|
|
|
|
ro_gui_wimp_event_register_mouse_click(dw->window, ro_gui_download_click);
|
|
|
|
ro_gui_wimp_event_register_keypress(dw->window, ro_gui_download_keypress);
|
|
|
|
ro_gui_wimp_event_register_close_window(dw->window, ro_gui_download_close);
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
/* issue the warning now, so that it appears in front of the download
|
|
|
|
* window! */
|
2005-07-08 18:53:53 +04:00
|
|
|
if (space_warning)
|
|
|
|
warn_user("DownloadWarn", messages_get("NoDiscSpace"));
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
return dw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle received download data.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
* \param data pointer to block of data received
|
|
|
|
* \param size size of data
|
|
|
|
*/
|
|
|
|
|
|
|
|
void gui_download_window_data(struct gui_download_window *dw, const char *data,
|
|
|
|
unsigned int size)
|
|
|
|
{
|
2005-07-08 18:53:53 +04:00
|
|
|
while (true) {
|
|
|
|
const char *msg;
|
|
|
|
int unwritten;
|
|
|
|
os_error *error;
|
2004-06-28 03:24:11 +04:00
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
error = xosgbpb_writew(dw->file, data, size, &unwritten);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosgbpb_writew: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
msg = error->errmess;
|
|
|
|
|
|
|
|
} else if (unwritten) {
|
|
|
|
LOG(("xosgbpb_writew: unwritten %i", unwritten));
|
|
|
|
msg = messages_get("Unwritten");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dw->received += size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
warn_user("SaveError", msg);
|
|
|
|
|
|
|
|
if (dw->saved) {
|
|
|
|
/* try to continue with the temporary file */
|
|
|
|
const char *temp_name = ro_gui_download_temp_name(dw);
|
|
|
|
|
|
|
|
error = ro_gui_download_move(dw, temp_name, dw->path);
|
|
|
|
if (!error) {
|
|
|
|
|
|
|
|
/* re-allow saving */
|
|
|
|
dw->saved = false;
|
|
|
|
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_ICON,
|
|
|
|
wimp_ICON_SHADED, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_DESTINATION,
|
|
|
|
wimp_ICON_DELETED, wimp_ICON_DELETED);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
error = xwimp_set_icon_state(dw->window,
|
|
|
|
ICON_DOWNLOAD_PATH, wimp_ICON_DELETED, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* give up then */
|
|
|
|
assert(dw->fetch);
|
2004-06-28 03:24:11 +04:00
|
|
|
fetch_abort(dw->fetch);
|
2005-07-08 18:53:53 +04:00
|
|
|
gui_download_window_error(dw, msg);
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the status text and progress bar.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_update_status(struct gui_download_window *dw)
|
|
|
|
{
|
|
|
|
char *received;
|
|
|
|
char *total_size;
|
|
|
|
char *speed;
|
|
|
|
char time[20] = "?";
|
|
|
|
struct timeval t;
|
|
|
|
float dt;
|
|
|
|
unsigned int left;
|
|
|
|
float rate;
|
|
|
|
os_error *error;
|
2005-07-08 18:53:53 +04:00
|
|
|
int width;
|
2005-07-16 18:35:25 +04:00
|
|
|
char *local_status;
|
|
|
|
utf8_convert_ret err;
|
2004-06-28 03:24:11 +04:00
|
|
|
|
|
|
|
gettimeofday(&t, 0);
|
|
|
|
dt = (t.tv_sec + 0.000001 * t.tv_usec) - (dw->last_time.tv_sec +
|
|
|
|
0.000001 * dw->last_time.tv_usec);
|
|
|
|
if (dt == 0)
|
|
|
|
dt = 0.001;
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
total_size = human_friendly_bytesize(max(dw->received, dw->total_size));
|
2004-06-28 03:24:11 +04:00
|
|
|
|
|
|
|
if (dw->fetch) {
|
|
|
|
rate = (dw->received - dw->last_received) / dt;
|
|
|
|
received = human_friendly_bytesize(dw->received);
|
|
|
|
speed = human_friendly_bytesize(rate);
|
|
|
|
if (dw->total_size) {
|
2005-07-08 18:53:53 +04:00
|
|
|
float f;
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
if (rate) {
|
|
|
|
left = (dw->total_size - dw->received) / rate;
|
|
|
|
sprintf(time, "%u:%.2u", left / 60, left % 60);
|
|
|
|
}
|
2005-07-16 18:35:25 +04:00
|
|
|
|
|
|
|
/* convert to local encoding */
|
|
|
|
err = utf8_to_local_encoding(
|
|
|
|
messages_get("Download"), 0, &local_status);
|
|
|
|
if (err != UTF8_CONVERT_OK) {
|
|
|
|
/* badenc should never happen */
|
|
|
|
assert(err != UTF8_CONVERT_BADENC);
|
|
|
|
/* hide nomem error */
|
|
|
|
snprintf(dw->status, sizeof dw->status,
|
2004-06-28 03:24:11 +04:00
|
|
|
messages_get("Download"),
|
|
|
|
received, total_size, speed, time);
|
2005-07-16 18:35:25 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(dw->status, sizeof dw->status,
|
|
|
|
local_status,
|
|
|
|
received, total_size, speed, time);
|
|
|
|
free(local_status);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
f = (float) dw->received / (float) dw->total_size;
|
|
|
|
width = download_progress_width * f;
|
2004-06-28 03:24:11 +04:00
|
|
|
} else {
|
|
|
|
left = t.tv_sec - dw->start_time.tv_sec;
|
|
|
|
sprintf(time, "%u:%.2u", left / 60, left % 60);
|
2005-07-16 18:35:25 +04:00
|
|
|
|
|
|
|
err = utf8_to_local_encoding(
|
|
|
|
messages_get("DownloadU"), 0, &local_status);
|
|
|
|
if (err != UTF8_CONVERT_OK) {
|
|
|
|
/* badenc should never happen */
|
|
|
|
assert(err != UTF8_CONVERT_BADENC);
|
|
|
|
/* hide nomem error */
|
|
|
|
snprintf(dw->status, sizeof dw->status,
|
2004-06-28 03:24:11 +04:00
|
|
|
messages_get("DownloadU"),
|
|
|
|
received, speed, time);
|
2005-07-16 18:35:25 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(dw->status, sizeof dw->status,
|
|
|
|
local_status,
|
|
|
|
received, speed, time);
|
|
|
|
free(local_status);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
/* length unknown, stay at 0 til finished */
|
|
|
|
width = 0;
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
left = dw->last_time.tv_sec - dw->start_time.tv_sec;
|
2004-06-28 03:29:02 +04:00
|
|
|
if (left == 0)
|
|
|
|
left = 1;
|
2004-06-28 03:24:11 +04:00
|
|
|
rate = (float) dw->received / (float) left;
|
|
|
|
sprintf(time, "%u:%.2u", left / 60, left % 60);
|
|
|
|
speed = human_friendly_bytesize(rate);
|
2005-07-16 18:35:25 +04:00
|
|
|
|
2008-12-26 12:25:30 +03:00
|
|
|
err = utf8_to_local_encoding(messages_get("Downloaded"), 0,
|
2005-07-16 18:35:25 +04:00
|
|
|
&local_status);
|
|
|
|
if (err != UTF8_CONVERT_OK) {
|
|
|
|
/* badenc should never happen */
|
|
|
|
assert(err != UTF8_CONVERT_BADENC);
|
|
|
|
/* hide nomem error */
|
|
|
|
snprintf(dw->status, sizeof dw->status,
|
2004-06-28 03:24:11 +04:00
|
|
|
messages_get("Downloaded"),
|
|
|
|
total_size, speed, time);
|
2005-07-16 18:35:25 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(dw->status, sizeof dw->status, local_status,
|
|
|
|
total_size, speed, time);
|
|
|
|
free(local_status);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
/* all done */
|
|
|
|
width = download_progress_width;
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dw->last_time = t;
|
|
|
|
dw->last_received = dw->received;
|
|
|
|
|
|
|
|
error = xwimp_resize_icon(dw->window, ICON_DOWNLOAD_PROGRESS,
|
|
|
|
download_progress_x0,
|
|
|
|
download_progress_y0,
|
2005-07-08 18:53:53 +04:00
|
|
|
download_progress_x0 + width,
|
2004-06-28 03:24:11 +04:00
|
|
|
download_progress_y1);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_resize_icon: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_STATUS, 0, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2003-10-06 03:30:21 +04:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
if (dw->fetch)
|
|
|
|
schedule(100, ro_gui_download_update_status_wrapper, dw);
|
|
|
|
else
|
|
|
|
schedule_remove(ro_gui_download_update_status_wrapper, dw);
|
2003-08-28 23:21:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-06-28 03:24:11 +04:00
|
|
|
* Wrapper for ro_gui_download_update_status(), suitable for schedule().
|
2003-08-28 23:21:27 +04:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
void ro_gui_download_update_status_wrapper(void *p)
|
2003-08-28 23:21:27 +04:00
|
|
|
{
|
2004-06-28 03:24:11 +04:00
|
|
|
ro_gui_download_update_status((struct gui_download_window *) p);
|
2003-08-28 23:21:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the caret but preserve input focus.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_window_hide_caret(struct gui_download_window *dw)
|
|
|
|
{
|
|
|
|
wimp_caret caret;
|
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
error = xwimp_get_caret_position(&caret);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_get_caret_position: 0x%x : %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
else if (caret.w == dw->window) {
|
|
|
|
error = xwimp_set_caret_position(dw->window, (wimp_i)-1, 0, 0, 1 << 25, -1);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_get_caret_position: 0x%x : %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 23:21:27 +04:00
|
|
|
/**
|
|
|
|
* Handle failed downloads.
|
2004-06-28 03:24:11 +04:00
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
* \param error_msg error message
|
2003-08-28 23:21:27 +04:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
void gui_download_window_error(struct gui_download_window *dw,
|
|
|
|
const char *error_msg)
|
2003-08-28 23:21:27 +04:00
|
|
|
{
|
2004-06-28 03:24:11 +04:00
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
dw->fetch = 0;
|
|
|
|
dw->error = true;
|
|
|
|
|
|
|
|
schedule_remove(ro_gui_download_update_status_wrapper, dw);
|
2003-08-28 23:21:27 +04:00
|
|
|
|
|
|
|
/* place error message in status icon in red */
|
2004-06-28 03:24:11 +04:00
|
|
|
strncpy(dw->status, error_msg, sizeof dw->status);
|
|
|
|
error = xwimp_set_icon_state(dw->window,
|
2003-08-28 23:21:27 +04:00
|
|
|
ICON_DOWNLOAD_STATUS,
|
|
|
|
wimp_COLOUR_RED << wimp_ICON_FG_COLOUR_SHIFT,
|
|
|
|
wimp_ICON_FG_COLOUR);
|
2004-06-28 03:24:11 +04:00
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2003-08-28 23:21:27 +04:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/* grey out pathname icon */
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_PATH,
|
|
|
|
wimp_ICON_SHADED, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
/* grey out file icon */
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_ICON,
|
|
|
|
wimp_ICON_SHADED, wimp_ICON_SHADED);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
|
|
|
|
|
|
|
ro_gui_download_window_hide_caret(dw);
|
2003-08-28 23:21:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle completed downloads.
|
2004-06-28 03:24:11 +04:00
|
|
|
*
|
|
|
|
* \param dw download window
|
2003-08-28 23:21:27 +04:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
void gui_download_window_done(struct gui_download_window *dw)
|
2003-08-28 23:21:27 +04:00
|
|
|
{
|
2004-06-28 03:24:11 +04:00
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
dw->fetch = 0;
|
|
|
|
ro_gui_download_update_status(dw);
|
|
|
|
|
|
|
|
error = xosfind_closew(dw->file);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfind_closew: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
}
|
|
|
|
dw->file = 0;
|
|
|
|
|
|
|
|
if (dw->saved) {
|
|
|
|
error = xosfile_set_type(dw->path,
|
|
|
|
dw->file_type);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_set_type: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
}
|
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
if (dw->send_dataload)
|
|
|
|
ro_gui_download_send_dataload(dw);
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
schedule(200, ro_gui_download_window_destroy_wrapper, dw);
|
|
|
|
}
|
2003-08-28 23:21:27 +04:00
|
|
|
}
|
|
|
|
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/**
|
|
|
|
* Handle Mouse_Click events in a download window.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
* \param pointer block returned by Wimp_Poll
|
|
|
|
*/
|
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
bool ro_gui_download_click(wimp_pointer *pointer)
|
2004-06-28 03:24:11 +04:00
|
|
|
{
|
2006-09-06 18:44:42 +04:00
|
|
|
struct gui_download_window *dw;
|
2004-06-28 03:24:11 +04:00
|
|
|
char command[256] = "Filer_OpenDir ";
|
|
|
|
char *dot;
|
|
|
|
os_error *error;
|
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
dw = (struct gui_download_window *)ro_gui_wimp_event_get_user_data(pointer->w);
|
2004-06-28 03:24:11 +04:00
|
|
|
if (pointer->i == ICON_DOWNLOAD_ICON && !dw->error &&
|
|
|
|
!dw->saved) {
|
2005-04-15 09:54:44 +04:00
|
|
|
const char *sprite = ro_gui_get_icon_string(pointer->w, pointer->i);
|
2004-06-28 03:24:11 +04:00
|
|
|
gui_current_drag_type = GUI_DRAG_DOWNLOAD_SAVE;
|
|
|
|
download_window_current = dw;
|
2005-04-15 09:54:44 +04:00
|
|
|
ro_gui_drag_icon(pointer->pos.x, pointer->pos.y, sprite);
|
2004-06-28 03:24:11 +04:00
|
|
|
|
|
|
|
} else if (pointer->i == ICON_DOWNLOAD_DESTINATION) {
|
|
|
|
strncpy(command + 14, dw->path, 242);
|
|
|
|
command[255] = 0;
|
|
|
|
dot = strrchr(command, '.');
|
|
|
|
if (dot) {
|
|
|
|
*dot = 0;
|
|
|
|
error = xos_cli(command);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xos_cli: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("MiscError", error->errmess);
|
2004-02-26 03:44:42 +03:00
|
|
|
}
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
2004-02-26 03:44:42 +03:00
|
|
|
}
|
2006-09-06 18:44:42 +04:00
|
|
|
return true;
|
2004-02-26 03:44:42 +03:00
|
|
|
}
|
2003-10-06 03:30:21 +04:00
|
|
|
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
/**
|
|
|
|
* Handler Key_Press events in a download window.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
* \param key key press returned by Wimp_Poll
|
|
|
|
* \return true iff key press handled
|
|
|
|
*/
|
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
bool ro_gui_download_keypress(wimp_key *key)
|
2005-05-02 06:11:06 +04:00
|
|
|
{
|
2006-09-06 18:44:42 +04:00
|
|
|
struct gui_download_window *dw;
|
|
|
|
|
|
|
|
dw = (struct gui_download_window *)ro_gui_wimp_event_get_user_data(key->w);
|
2005-05-02 06:11:06 +04:00
|
|
|
switch (key->c)
|
|
|
|
{
|
|
|
|
case wimp_KEY_ESCAPE:
|
|
|
|
ro_gui_download_window_destroy(dw, false);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case wimp_KEY_RETURN: {
|
2008-08-05 05:23:04 +04:00
|
|
|
const char *name = ro_gui_get_icon_string(dw->window,
|
|
|
|
ICON_DOWNLOAD_PATH);
|
|
|
|
if (!strrchr(name, '.')) {
|
2006-02-07 19:16:23 +03:00
|
|
|
warn_user("NoPathError", NULL);
|
2005-05-02 06:11:06 +04:00
|
|
|
return true;
|
|
|
|
}
|
2006-02-07 19:16:23 +03:00
|
|
|
ro_gui_convert_save_path(dw->path, sizeof dw->path, name);
|
|
|
|
|
|
|
|
dw->send_dataload = false;
|
|
|
|
if (ro_gui_download_save(dw, dw->path,
|
|
|
|
!option_confirm_overwrite) && !dw->fetch)
|
|
|
|
{
|
|
|
|
/* finished already */
|
|
|
|
schedule(200, ro_gui_download_window_destroy_wrapper, dw);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2005-05-02 06:11:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ignore all other keypresses (F12 etc) */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-26 03:44:42 +03:00
|
|
|
/**
|
|
|
|
* Handle User_Drag_Box event for a drag from a download window.
|
2004-06-28 03:24:11 +04:00
|
|
|
*
|
|
|
|
* \param drag block returned by Wimp_Poll
|
2004-02-26 03:44:42 +03:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
void ro_gui_download_drag_end(wimp_dragged *drag)
|
2004-02-26 03:44:42 +03:00
|
|
|
{
|
|
|
|
wimp_pointer pointer;
|
|
|
|
wimp_message message;
|
2004-06-28 03:24:11 +04:00
|
|
|
struct gui_download_window *dw = download_window_current;
|
2005-05-02 06:11:06 +04:00
|
|
|
const char *leaf;
|
2004-06-28 03:24:11 +04:00
|
|
|
os_error *error;
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
if (dw->saved || dw->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
error = xwimp_get_pointer_info(&pointer);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_get_pointer_info: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
return;
|
|
|
|
}
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2005-04-17 01:26:15 +04:00
|
|
|
/* ignore drags to the download window itself */
|
|
|
|
if (pointer.w == dw->window) return;
|
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
leaf = strrchr(dw->path, '.');
|
|
|
|
if (leaf)
|
|
|
|
leaf++;
|
|
|
|
else
|
|
|
|
leaf = dw->path;
|
|
|
|
ro_gui_convert_save_path(message.data.data_xfer.file_name, 212, leaf);
|
|
|
|
|
2004-02-26 03:44:42 +03:00
|
|
|
message.your_ref = 0;
|
|
|
|
message.action = message_DATA_SAVE;
|
|
|
|
message.data.data_xfer.w = pointer.w;
|
|
|
|
message.data.data_xfer.i = pointer.i;
|
|
|
|
message.data.data_xfer.pos.x = pointer.pos.x;
|
|
|
|
message.data.data_xfer.pos.y = pointer.pos.y;
|
2004-06-28 03:24:11 +04:00
|
|
|
message.data.data_xfer.est_size = dw->total_size ? dw->total_size :
|
|
|
|
dw->received;
|
|
|
|
message.data.data_xfer.file_type = dw->file_type;
|
2004-02-26 03:44:42 +03:00
|
|
|
message.size = 44 + ((strlen(message.data.data_xfer.file_name) + 4) &
|
|
|
|
(~3u));
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
error = xwimp_send_message_to_window(wimp_USER_MESSAGE, &message,
|
|
|
|
pointer.w, pointer.i, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_send_message_to_window: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2003-10-06 03:30:21 +04:00
|
|
|
}
|
|
|
|
|
2004-02-26 03:44:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Message_DataSaveAck for a drag from a download window.
|
2004-06-28 03:24:11 +04:00
|
|
|
*
|
|
|
|
* \param message block returned by Wimp_Poll
|
2004-02-26 03:44:42 +03:00
|
|
|
*/
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
void ro_gui_download_datasave_ack(wimp_message *message)
|
2004-02-26 03:44:42 +03:00
|
|
|
{
|
2004-06-28 03:24:11 +04:00
|
|
|
struct gui_download_window *dw = download_window_current;
|
2005-05-02 06:11:06 +04:00
|
|
|
|
|
|
|
dw->send_dataload = true;
|
|
|
|
memcpy(&dw->save_message, message, sizeof(wimp_message));
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
if (!ro_gui_download_save(dw, message->data.data_xfer.file_name,
|
|
|
|
!option_confirm_overwrite))
|
2005-05-02 06:11:06 +04:00
|
|
|
return;
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
if (!dw->fetch) {
|
2005-05-02 06:11:06 +04:00
|
|
|
/* Ack successful completed save with message_DATA_LOAD immediately
|
|
|
|
to reduce the chance of the target app getting confused by it
|
|
|
|
being delayed */
|
|
|
|
|
|
|
|
ro_gui_download_send_dataload(dw);
|
|
|
|
|
|
|
|
schedule(200, ro_gui_download_window_destroy_wrapper, dw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-07-08 18:53:53 +04:00
|
|
|
* Return a pathname in canonical form
|
|
|
|
*
|
|
|
|
* \param path pathnamee to be canonicalised
|
|
|
|
* \return ptr to pathname in malloc block, or NULL
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *ro_gui_download_canonicalise(const char *path)
|
|
|
|
{
|
|
|
|
os_error *error;
|
|
|
|
int spare = 0;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
error = xosfscontrol_canonicalise_path(path, NULL, NULL, NULL, 0, &spare);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfscontrol_canonicalise_path: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc(1 - spare);
|
|
|
|
if (buf) {
|
|
|
|
error = xosfscontrol_canonicalise_path(path, buf, NULL, NULL,
|
|
|
|
1 - spare, NULL);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfscontrol_canonicalise_path: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the available space on the medium containing the destination file,
|
|
|
|
* taking into account any space currently occupied by the file at its
|
|
|
|
* original location.
|
2005-05-02 06:11:06 +04:00
|
|
|
*
|
|
|
|
* \param dw download window
|
2005-07-08 18:53:53 +04:00
|
|
|
* \param dest_file destination pathname
|
|
|
|
* \param orig_file current pathname, NULL if no existing file
|
|
|
|
* \return true iff there's enough space
|
2005-05-02 06:11:06 +04:00
|
|
|
*/
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
bool ro_gui_download_check_space(struct gui_download_window *dw,
|
|
|
|
const char *dest_file, const char *orig_file)
|
2005-05-02 06:11:06 +04:00
|
|
|
{
|
2005-07-08 18:53:53 +04:00
|
|
|
/* is there enough free space for this file? */
|
|
|
|
int dest_len = strlen(dest_file);
|
2004-02-26 03:44:42 +03:00
|
|
|
os_error *error;
|
2005-07-08 18:53:53 +04:00
|
|
|
int max_file;
|
|
|
|
bits free_lo;
|
|
|
|
int free_hi;
|
|
|
|
char *dir;
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
dir = malloc(dest_len + 1);
|
|
|
|
if (!dir) return true;
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
while (dest_len > 0 && dest_file[--dest_len] != '.');
|
|
|
|
|
|
|
|
memcpy(dir, dest_file, dest_len);
|
|
|
|
dir[dest_len] = '\0';
|
|
|
|
|
|
|
|
/* try the 64-bit variant first (RO 3.6+) */
|
|
|
|
error = xosfscontrol_free_space64(dir, &free_lo, &free_hi,
|
|
|
|
&max_file, NULL, NULL);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfscontrol_free_space64: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
|
|
|
|
free_hi = 0;
|
|
|
|
error = xosfscontrol_free_space(dir, (int*)&free_lo,
|
|
|
|
&max_file, NULL);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfscontrol_free_space: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
/* close our eyes and hope */
|
|
|
|
free(dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
|
|
|
|
if ((bits)max_file < dw->total_size || (!free_hi && free_lo < dw->total_size)) {
|
|
|
|
char *dest_canon, *orig_canon;
|
|
|
|
bits space;
|
|
|
|
|
|
|
|
if (!orig_file || !dw->file) {
|
|
|
|
/* no original file to take into account */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
space = min((bits)max_file, free_lo);
|
|
|
|
|
|
|
|
dest_canon = ro_gui_download_canonicalise(dest_file);
|
|
|
|
if (!dest_canon) dest_canon = (char*)dest_file;
|
|
|
|
|
|
|
|
orig_canon = ro_gui_download_canonicalise(orig_file);
|
|
|
|
if (!orig_canon) orig_canon = (char*)orig_file;
|
|
|
|
|
|
|
|
/* not enough space; allow for the file's original location
|
2005-07-09 22:30:28 +04:00
|
|
|
when space is tight by comparing the first part of the two
|
|
|
|
pathnames (and assuming the FS isn't brain damaged!) */
|
2005-07-08 18:53:53 +04:00
|
|
|
|
2005-07-09 22:30:28 +04:00
|
|
|
char *dot = strchr(orig_canon, '.');
|
|
|
|
if (dot && !strncasecmp(dest_canon, orig_canon, (dot + 1) - orig_canon)) {
|
|
|
|
int allocation;
|
2005-07-08 18:53:53 +04:00
|
|
|
|
2005-07-09 22:30:28 +04:00
|
|
|
error = xosargs_read_allocation(dw->file,
|
|
|
|
&allocation);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosargs_read_allocation: 0x%x : %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
space += allocation;
|
2005-07-08 18:53:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest_canon != dest_file) free(dest_canon);
|
|
|
|
if (orig_canon != orig_file) free(orig_canon);
|
|
|
|
|
|
|
|
if (space >= dw->total_size) {
|
|
|
|
/* OK, renaming should work */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the downloading file to a new location and continue downloading there.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
* \param dest_file new location
|
|
|
|
* \param src_file old location
|
|
|
|
* \return error iff failed to move file
|
|
|
|
*/
|
|
|
|
|
|
|
|
os_error *ro_gui_download_move(struct gui_download_window *dw,
|
|
|
|
const char *dest_file, const char *src_file)
|
|
|
|
{
|
|
|
|
os_error *error;
|
2004-06-28 03:24:11 +04:00
|
|
|
|
|
|
|
/* close temporary file */
|
|
|
|
if (dw->file) {
|
|
|
|
error = xosfind_closew(dw->file);
|
|
|
|
dw->file = 0;
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfind_closew: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
2005-07-08 18:53:53 +04:00
|
|
|
return error;
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
}
|
2004-02-26 03:44:42 +03:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/* move or copy temporary file to destination file */
|
2005-07-08 18:53:53 +04:00
|
|
|
error = xosfscontrol_rename(src_file, dest_file);
|
2004-08-26 02:11:14 +04:00
|
|
|
/* Errors from a filing system have number 0x1XXnn, where XX is the FS
|
|
|
|
* number, and nn the error number. 0x9F is "Not same disc". */
|
|
|
|
if (error && (error->errnum == error_BAD_RENAME ||
|
|
|
|
(error->errnum & 0xFF00FFu) == 0x1009Fu)) {
|
2004-06-28 03:24:11 +04:00
|
|
|
/* rename failed: copy with delete */
|
2005-07-08 18:53:53 +04:00
|
|
|
error = xosfscontrol_copy(src_file, dest_file,
|
2004-06-28 03:24:11 +04:00
|
|
|
osfscontrol_COPY_FORCE |
|
|
|
|
osfscontrol_COPY_DELETE |
|
|
|
|
osfscontrol_COPY_LOOK,
|
|
|
|
0, 0, 0, 0, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfscontrol_copy: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
2005-07-08 18:53:53 +04:00
|
|
|
return error;
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
} else if (error) {
|
|
|
|
LOG(("xosfscontrol_rename: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
2005-07-08 18:53:53 +04:00
|
|
|
return error;
|
2004-02-26 03:44:42 +03:00
|
|
|
}
|
2004-05-02 04:16:23 +04:00
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
if (dw->fetch) {
|
|
|
|
/* open new destination file if still fetching */
|
2005-07-08 18:53:53 +04:00
|
|
|
error = xosfile_write(dest_file, 0xdeaddead, 0xdeaddead,
|
2004-06-28 03:24:11 +04:00
|
|
|
fileswitch_ATTR_OWNER_READ |
|
|
|
|
fileswitch_ATTR_OWNER_WRITE);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_write: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xosfind_openupw(osfind_NO_PATH | osfind_ERROR_IF_DIR,
|
2005-07-08 18:53:53 +04:00
|
|
|
dest_file, 0, &dw->file);
|
2004-06-28 03:24:11 +04:00
|
|
|
if (error) {
|
|
|
|
LOG(("xosfind_openupw: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
2005-07-08 18:53:53 +04:00
|
|
|
return error;
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
error = xosargs_set_ptrw(dw->file, dw->received);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosargs_set_ptrw: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
2005-07-08 18:53:53 +04:00
|
|
|
return error;
|
2004-06-28 03:24:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* otherwise just set the file type */
|
2005-07-08 18:53:53 +04:00
|
|
|
error = xosfile_set_type(dest_file,
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->file_type);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_set_type: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
/* success */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start of save operation, user has specified where the file should be saved.
|
|
|
|
*
|
2006-02-07 19:16:23 +03:00
|
|
|
* \param dw download window
|
|
|
|
* \param file_name pathname of destination file
|
|
|
|
& \param force_overwrite true iff required to overwrite without prompting
|
2005-07-08 18:53:53 +04:00
|
|
|
* \return true iff save successfully initiated
|
|
|
|
*/
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
bool ro_gui_download_save(struct gui_download_window *dw,
|
|
|
|
const char *file_name, bool force_overwrite)
|
2005-07-08 18:53:53 +04:00
|
|
|
{
|
2006-02-07 19:16:23 +03:00
|
|
|
fileswitch_object_type obj_type;
|
2005-07-08 18:53:53 +04:00
|
|
|
const char *temp_name;
|
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
if (dw->saved || dw->error)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
temp_name = ro_gui_download_temp_name(dw);
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
/* does the user want to check for collisions when saving? */
|
|
|
|
if (true && !force_overwrite) {
|
|
|
|
/* check whether the destination file/dir already exists */
|
|
|
|
error = xosfile_read_stamped(file_name, &obj_type,
|
|
|
|
NULL, NULL, NULL, NULL, NULL);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_read_stamped: 0x%x:%s", error->errnum, error->errmess));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (obj_type) {
|
|
|
|
case osfile_NOT_FOUND:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case osfile_IS_FILE:
|
|
|
|
dw->query = query_user("OverwriteFile", NULL, &overwrite_funcs, dw,
|
|
|
|
messages_get("Replace"), messages_get("DontReplace"));
|
|
|
|
dw->query_rsn = QueryRsn_Overwrite;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = xosfile_make_error(file_name, obj_type);
|
|
|
|
assert(error);
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
if (!ro_gui_download_check_space(dw, file_name, temp_name)) {
|
|
|
|
warn_user("SaveError", messages_get("NoDiscSpace"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = ro_gui_download_move(dw, file_name, temp_name);
|
|
|
|
if (error) {
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
|
|
|
|
/* try to reopen at old location so that the download can continue
|
|
|
|
to the temporary file */
|
|
|
|
error = xosfind_openupw(osfind_NO_PATH | osfind_ERROR_IF_DIR,
|
|
|
|
temp_name, 0, &dw->file);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfind_openupw: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
error = xosargs_set_ptrw(dw->file, dw->received);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosargs_set_ptrw: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (dw->fetch) fetch_abort(dw->fetch);
|
|
|
|
gui_download_window_error(dw, error->errmess);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
dw->saved = true;
|
|
|
|
strncpy(dw->path, file_name, sizeof dw->path);
|
|
|
|
|
2005-07-08 18:53:53 +04:00
|
|
|
/* grey out file icon */
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_ICON,
|
|
|
|
wimp_ICON_SHADED, wimp_ICON_SHADED);
|
2004-06-28 03:24:11 +04:00
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
/* hide writeable path icon and show destination icon
|
|
|
|
Note: must redraw icon bounding box because the destination icon
|
|
|
|
has rounded edges on RISC OS Select/Adjust and doesn't
|
|
|
|
completely cover the writeable icon */
|
|
|
|
|
|
|
|
ro_gui_force_redraw_icon(dw->window, ICON_DOWNLOAD_PATH);
|
|
|
|
error = xwimp_set_icon_state(dw->window, ICON_DOWNLOAD_PATH,
|
|
|
|
wimp_ICON_DELETED, wimp_ICON_DELETED);
|
2004-06-28 03:24:11 +04:00
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
error = xwimp_set_icon_state(dw->window,
|
|
|
|
ICON_DOWNLOAD_DESTINATION, wimp_ICON_DELETED, 0);
|
2005-04-23 06:58:27 +04:00
|
|
|
if (error) {
|
2005-07-08 18:53:53 +04:00
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
2005-04-23 06:58:27 +04:00
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2005-07-08 18:53:53 +04:00
|
|
|
|
|
|
|
ro_gui_download_window_hide_caret(dw);
|
2005-04-23 06:58:27 +04:00
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send DataLoad message in response to DataSaveAck, informing the
|
|
|
|
* target application that the transfer is complete.
|
|
|
|
*
|
|
|
|
* \param dw download window
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_send_dataload(struct gui_download_window *dw)
|
|
|
|
{
|
2004-05-02 04:16:23 +04:00
|
|
|
/* Ack successful save with message_DATA_LOAD */
|
2005-05-02 06:11:06 +04:00
|
|
|
wimp_message *message = &dw->save_message;
|
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
assert(dw->send_dataload);
|
|
|
|
dw->send_dataload = false;
|
|
|
|
|
2004-05-02 04:16:23 +04:00
|
|
|
message->action = message_DATA_LOAD;
|
|
|
|
message->your_ref = message->my_ref;
|
2004-06-28 03:24:11 +04:00
|
|
|
error = xwimp_send_message_to_window(wimp_USER_MESSAGE, message,
|
|
|
|
message->data.data_xfer.w,
|
|
|
|
message->data.data_xfer.i, 0);
|
2006-01-03 08:13:23 +03:00
|
|
|
/* The window we just attempted to send a message to may
|
|
|
|
* have been closed before the message was sent. As we've
|
|
|
|
* no clean way of detecting this, we'll just detect the
|
|
|
|
* error return from the message send attempt and judiciously
|
|
|
|
* ignore it.
|
|
|
|
*
|
|
|
|
* Ideally, we would have registered to receive Message_WindowClosed
|
|
|
|
* and then cleared dw->send_dataload flag for the appropriate
|
|
|
|
* window. Unfortunately, however, a long-standing bug in the
|
|
|
|
* Pinboard module prevents this from being a viable solution.
|
|
|
|
*
|
|
|
|
* See http://groups.google.co.uk/group/comp.sys.acorn.tech/msg/e3fbf70d8393e6cf?dmode=source&hl=en
|
|
|
|
* for the rather depressing details.
|
|
|
|
*/
|
|
|
|
if (error && error->errnum != error_WIMP_BAD_HANDLE) {
|
2004-06-28 03:24:11 +04:00
|
|
|
LOG(("xwimp_set_icon_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2004-05-02 04:16:23 +04:00
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
schedule(200, ro_gui_download_window_destroy_wrapper, dw);
|
2004-02-26 03:44:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-06 18:44:42 +04:00
|
|
|
/**
|
|
|
|
* Handle closing of download window
|
|
|
|
*/
|
|
|
|
void ro_gui_download_close(wimp_w w)
|
|
|
|
{
|
|
|
|
struct gui_download_window *dw;
|
|
|
|
|
|
|
|
dw = (struct gui_download_window *)ro_gui_wimp_event_get_user_data(w);
|
|
|
|
ro_gui_download_window_destroy(dw, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/**
|
|
|
|
* Close a download window and free any related resources.
|
|
|
|
*
|
2005-05-02 06:11:06 +04:00
|
|
|
* \param dw download window
|
|
|
|
* \param quit destroying because we're quitting the whole app
|
2006-09-06 18:44:42 +04:00
|
|
|
* \return true if window destroyed, not waiting for user confirmation
|
2004-06-28 03:24:11 +04:00
|
|
|
*/
|
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
bool ro_gui_download_window_destroy(struct gui_download_window *dw, bool quit)
|
2003-10-06 03:30:21 +04:00
|
|
|
{
|
2005-05-02 06:11:06 +04:00
|
|
|
bool safe = dw->saved && !dw->fetch;
|
2004-06-28 03:24:11 +04:00
|
|
|
os_error *error;
|
|
|
|
|
2005-05-02 06:11:06 +04:00
|
|
|
if (!safe && !dw->close_confirmed)
|
2005-04-23 06:58:27 +04:00
|
|
|
{
|
2006-02-07 19:16:23 +03:00
|
|
|
query_reason rsn = quit ? QueryRsn_Quit : QueryRsn_Abort;
|
|
|
|
|
|
|
|
if (dw->query != QUERY_INVALID) {
|
|
|
|
|
|
|
|
/* can we just reuse the existing query? */
|
|
|
|
if (rsn == dw->query_rsn) {
|
|
|
|
ro_gui_query_window_bring_to_front(dw->query);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-23 06:58:27 +04:00
|
|
|
query_close(dw->query);
|
|
|
|
dw->query = QUERY_INVALID;
|
|
|
|
}
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
if (quit) {
|
|
|
|
/* bring all download windows to the front of the desktop as
|
|
|
|
a convenience if there are lots of windows open */
|
|
|
|
|
|
|
|
struct gui_download_window *d = download_window_list;
|
|
|
|
while (d) {
|
2006-02-16 02:09:55 +03:00
|
|
|
ro_gui_dialog_open_top(d->window, NULL, 0, 0);
|
2006-02-07 19:16:23 +03:00
|
|
|
d = d->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dw->query_rsn = rsn;
|
|
|
|
dw->query = query_user(quit ? "QuitDownload" : "AbortDownload",
|
|
|
|
NULL, &close_funcs, dw, NULL, NULL);
|
2005-05-02 06:11:06 +04:00
|
|
|
|
|
|
|
return false;
|
2005-04-23 06:58:27 +04:00
|
|
|
}
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
schedule_remove(ro_gui_download_update_status_wrapper, dw);
|
2004-06-29 16:37:06 +04:00
|
|
|
schedule_remove(ro_gui_download_window_destroy_wrapper, dw);
|
2004-06-28 03:24:11 +04:00
|
|
|
|
|
|
|
/* remove from list */
|
|
|
|
if (dw->prev)
|
|
|
|
dw->prev->next = dw->next;
|
|
|
|
else
|
|
|
|
download_window_list = dw->next;
|
|
|
|
if (dw->next)
|
|
|
|
dw->next->prev = dw->prev;
|
|
|
|
|
|
|
|
/* delete window */
|
|
|
|
error = xwimp_delete_window(dw->window);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_delete_window: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
}
|
2006-09-06 18:44:42 +04:00
|
|
|
ro_gui_wimp_event_finalise(dw->window);
|
2004-06-28 03:24:11 +04:00
|
|
|
|
|
|
|
/* close download file */
|
|
|
|
if (dw->file) {
|
|
|
|
error = xosfind_closew(dw->file);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfind_closew: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete temporary file */
|
|
|
|
if (!dw->saved) {
|
2005-07-08 18:53:53 +04:00
|
|
|
const char *temp_name = ro_gui_download_temp_name(dw);
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
error = xosfile_delete(temp_name, 0, 0, 0, 0, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xosfile_delete: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("SaveError", error->errmess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dw->fetch)
|
|
|
|
fetch_abort(dw->fetch);
|
|
|
|
|
|
|
|
free(dw);
|
2005-05-02 06:11:06 +04:00
|
|
|
|
|
|
|
return true;
|
2003-10-06 03:30:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-28 03:24:11 +04:00
|
|
|
/**
|
|
|
|
* Wrapper for ro_gui_download_window_destroy(), suitable for schedule().
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_window_destroy_wrapper(void *p)
|
|
|
|
{
|
2005-04-23 06:58:27 +04:00
|
|
|
struct gui_download_window *dw = p;
|
|
|
|
if (dw->query != QUERY_INVALID)
|
|
|
|
query_close(dw->query);
|
|
|
|
dw->query = QUERY_INVALID;
|
2005-05-02 06:11:06 +04:00
|
|
|
dw->close_confirmed = true;
|
|
|
|
ro_gui_download_window_destroy(dw, false);
|
2005-04-23 06:58:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User has opted to cancel the close, leaving the download to continue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_close_cancelled(query_id id, enum query_response res, void *p)
|
|
|
|
{
|
|
|
|
struct gui_download_window *dw = p;
|
|
|
|
dw->query = QUERY_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download aborted, close window and tidy up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_close_confirmed(query_id id, enum query_response res, void *p)
|
|
|
|
{
|
2006-02-07 19:16:23 +03:00
|
|
|
struct gui_download_window *dw = p;
|
2005-04-23 06:58:27 +04:00
|
|
|
dw->query = QUERY_INVALID;
|
|
|
|
dw->close_confirmed = true;
|
2006-02-07 19:16:23 +03:00
|
|
|
if (dw->query_rsn == QueryRsn_Quit) {
|
2005-04-23 06:58:27 +04:00
|
|
|
|
|
|
|
/* destroy all our downloads */
|
|
|
|
while (download_window_list)
|
|
|
|
ro_gui_download_window_destroy_wrapper(download_window_list);
|
|
|
|
|
|
|
|
/* and restart the shutdown */
|
|
|
|
if (ro_gui_prequit())
|
|
|
|
netsurf_quit = true;
|
|
|
|
}
|
|
|
|
else
|
2005-05-02 06:11:06 +04:00
|
|
|
ro_gui_download_window_destroy(dw, false);
|
2005-04-23 06:58:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-07 19:16:23 +03:00
|
|
|
/**
|
|
|
|
* User has opted not to overwrite the existing file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_overwrite_cancelled(query_id id, enum query_response res, void *p)
|
|
|
|
{
|
|
|
|
struct gui_download_window *dw = p;
|
|
|
|
dw->query = QUERY_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite of existing file confirmed, proceed with the save.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ro_gui_download_overwrite_confirmed(query_id id, enum query_response res, void *p)
|
|
|
|
{
|
|
|
|
struct gui_download_window *dw = p;
|
|
|
|
dw->query = QUERY_INVALID;
|
|
|
|
|
|
|
|
if (!ro_gui_download_save(dw, dw->save_message.data.data_xfer.file_name, true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!dw->fetch) {
|
|
|
|
/* Ack successful completed save with message_DATA_LOAD immediately
|
|
|
|
to reduce the chance of the target app getting confused by it
|
|
|
|
being delayed */
|
|
|
|
|
|
|
|
ro_gui_download_send_dataload(dw);
|
|
|
|
|
|
|
|
schedule(200, ro_gui_download_window_destroy_wrapper, dw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-23 06:58:27 +04:00
|
|
|
/**
|
|
|
|
* Respond to PreQuit message, displaying a prompt message if we need
|
|
|
|
* the user to confirm the shutdown.
|
|
|
|
*
|
2006-09-06 18:44:42 +04:00
|
|
|
* \return true if we can shutdown straightaway
|
2005-04-23 06:58:27 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool ro_gui_download_prequit(void)
|
|
|
|
{
|
2005-05-02 06:11:06 +04:00
|
|
|
while (download_window_list)
|
|
|
|
{
|
|
|
|
if (!ro_gui_download_window_destroy(download_window_list, true))
|
|
|
|
return false; /* awaiting user confirmation */
|
2005-04-23 06:58:27 +04:00
|
|
|
}
|
|
|
|
return true;
|
2003-10-06 03:30:21 +04:00
|
|
|
}
|