2022-04-28 01:02:50 -07:00
/ *
* Copyright 2022 Anthony Cohn - Richardby < anthonyc @ gmx . co . uk >
*
* 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 / > .
* /
2020-11-17 12:14:34 -08:00
# import < Cocoa / Cocoa . h >
2021-02-09 11:19:30 -08:00
# include < string . h >
2020-11-17 12:14:34 -08:00
# import "netsurf/netsurf.h"
# import "netsurf/download.h"
2020-12-09 12:18:22 -08:00
# import "desktop/download.h"
2021-02-09 11:19:30 -08:00
# import "utils/nsurl.h"
2020-12-09 12:18:22 -08:00
# import "DownloadManager.h"
# import "AppDelegate.h"
2021-02-09 11:19:30 -08:00
# import "Preferences.h"
# define CMP_HEAD ( MIME , IN , EXT ) if ( strncmp ( MIME , IN , 40 ) = = 0 ) { return EXT ; }
# define CMP ( MIME , IN , EXT ) else if ( strncmp ( MIME , IN , 40 ) = = 0 ) { return EXT ; }
static const char * getext ( const char * mime ) {
CMP_HEAD ( "image/jpeg" , mime , "jpeg" )
CMP ( "text/css" , mime , "css" )
CMP ( "text/html" , mime , "html" )
CMP ( "image/gif" , mime , "gif" )
CMP ( "image/png" , mime , "png" )
CMP ( "application/zip" , mime , "zip" )
CMP ( "application/gzip" , mime , "gz" )
CMP ( "image/webm" , mime , "webm" )
CMP ( "application/pdf" , mime , "pdf" )
CMP ( "audio/mpeg" , mime , "mp3" )
CMP ( "audio/wav" , mime , "wav" )
CMP ( "audio/webm" , mime , "webm" )
CMP ( "application/octet-stream" , mime , "bin" )
CMP ( "image/tiff" , mime , "tiff" )
CMP ( "audio/aac" , mime , "aac" )
CMP ( "audio/ogg" , mime , "ogg" )
CMP ( "application/x-7z-compressed" , mime , "7z" )
CMP ( "application/x-bzip" , mime , "bz" )
CMP ( "application/x-bzip2" , mime , "bz2" )
CMP ( "image/bmp" , mime , "bmp" )
CMP ( "text/csv" , mime , "csv" )
CMP ( "application/epub+zip" , mime , "epub" )
CMP ( "image/vnd.microsoft.icon" , mime , "ico" )
CMP ( "text/javascript" , mime , "js" )
CMP ( "application/json" , mime , "json" )
CMP ( "video/mpeg" , mime , "mpeg" )
CMP ( "application/rtf" , mime , "rtf" )
return NULL ;
}
static BOOL askwrite ( NSString * filename ) {
NSAlert * alert = [ [ NSAlert alloc ] init ] ;
NSString * informativeText = [ NSString stringWithFormat : @ "A file named '%@' already exists, do you wish to overwrite it?\nThe existing file will be deleted." , filename ] ;
[ alert setMessageText : @ "File Exists" ] ;
[ alert setInformativeText : informativeText ] ;
[ alert addButtonWithTitle : @ "Overwrite" ] ;
[ alert addButtonWithTitle : @ "Cancel" ] ;
[ alert setAlertStyle : NSWarningAlertStyle ] ;
NSInteger result = [ alert runModal ] ;
[ alert release ] ;
return result = = NSAlertFirstButtonReturn ;
}
2020-11-17 12:14:34 -08:00
/ * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * Download * * * * * * /
/ * * * * * * * * * * * * * * * * * * * * * * /
2020-12-08 12:25:10 -08:00
// This won ' t really return a window ref , but a ref to a download item .
2020-11-17 12:14:34 -08:00
static struct gui_download _window * gnustep_download _create ( struct download_context * ctx , struct gui_window * parent ) {
NSLog ( @ "gnustep_download_create" ) ;
2021-02-09 11:19:30 -08:00
const char * name = download_context _get _filename ( ctx ) ;
const char * mime = download_context _get _mime _type ( ctx ) ;
const char * ext = getext ( mime ) ;
NSString * filename ;
if ( ext ! = NULL ) {
filename = [ NSString stringWithFormat : @ "%s.%s" , name , ext ] ;
} else {
filename = [ NSString stringWithCString : name ] ;
}
if ( [ filename length ] < 1 ) {
NSLog ( @ "Filename was empty" ) ;
return NULL ;
}
NSString * destination = [ [ [ Preferences defaultPreferences ] downloadLocationPath ]
stringByAppendingPathComponent : filename ] ;
NSURL * url = [ NSURL URLWithString : destination ] ;
if ( url = = nil ) {
NSLog ( @ "Failed to create url with path %@" , destination ) ;
return NULL ;
}
BOOL exists = [ [ NSFileManager defaultManager ] fileExistsAtPath : [ url absoluteString ] ] ;
BOOL confirmOverwrites = [ [ Preferences defaultPreferences ] confirmBeforeOverwriting ] ;
if ( confirmOverwrites ) {
NSLog ( @ "Will confirm before overwriting..." ) ;
}
BOOL shouldProceed = ! exists || ! confirmOverwrites || askwrite ( filename ) ;
if ( ! shouldProceed ) {
NSLog ( @ "Won't continue..." ) ;
2021-01-23 05:13:57 -08:00
return NULL ;
2021-02-09 11:19:30 -08:00
}
if ( exists ) {
NSError * err = nil ;
[ [ NSFileManager defaultManager ] removeItemAtURL : url error : & err ] ;
if ( err ! = nil ) {
NSLog ( @ "Error deleting existing file at %@" , url ) ;
return NULL ;
}
}
2020-12-09 12:18:22 -08:00
DownloadItem * download = [ [ DownloadManager defaultDownloadManager ]
2020-12-18 12:43:26 -08:00
createDownloadForDestination : url withContext : ctx ] ;
2020-12-10 12:06:23 -08:00
[ [ NSApp delegate ] showDownloadsWindow : nil ] ;
2020-12-09 12:18:22 -08:00
return ( struct gui_download _window * ) download ;
2020-11-17 12:14:34 -08:00
}
// ? ?
static nserror gnustep_download _data ( struct gui_download _window * dw , const char * data , unsigned int size ) {
2020-12-09 12:18:22 -08:00
BOOL success = [ ( id ) dw appendToDownload : [ NSData dataWithBytesNoCopy : ( void * ) data
2020-12-10 11:06:02 -08:00
length : size freeWhenDone : NO ] ] ;
2020-12-09 12:18:22 -08:00
if ( success ) {
return NSERROR_OK ;
} else {
return NSERROR_SAVE _FAILED ;
}
2020-11-17 12:14:34 -08:00
}
// Error occurred during download
static void gnustep_download _error ( struct gui_download _window * dw , const char * error_msg ) {
NSLog ( @ "gnustep_download_error" ) ;
2020-12-09 12:18:22 -08:00
[ ( id ) dw failWithMessage : [ NSString stringWithCString : error_msg ] ] ;
2020-11-17 12:14:34 -08:00
}
// Download completed
static void gnustep_download _done ( struct gui_download _window * dw ) {
NSLog ( @ "gnustep_download_done" ) ;
2020-12-09 12:18:22 -08:00
[ ( id ) dw complete ] ;
2020-11-17 12:14:34 -08:00
}
struct gui_download _table gnustep_download _table = {
. create = gnustep_download _create ,
. data = gnustep_download _data ,
. error = gnustep_download _error ,
. done = gnustep_download _done
} ;