1 module webkit2.FileChooserRequest; 2 3 private import glib.Str; 4 private import gobject.ObjectG; 5 private import gtk.FileFilter; 6 private import webkit2.c.functions; 7 public import webkit2.c.types; 8 9 10 /** 11 * Whenever the user interacts with an <input type='file' /> 12 * HTML element, WebKit will need to show a dialog to choose one or 13 * more files to be uploaded to the server along with the rest of the 14 * form data. For that to happen in a general way, instead of just 15 * opening a #GtkFileChooserDialog (which might be not desirable in 16 * some cases, which could prefer to use their own file chooser 17 * dialog), WebKit will fire the #WebKitWebView::run-file-chooser 18 * signal with a #WebKitFileChooserRequest object, which will allow 19 * the client application to specify the files to be selected, to 20 * inspect the details of the request (e.g. if multiple selection 21 * should be allowed) and to cancel the request, in case nothing was 22 * selected. 23 * 24 * In case the client application does not wish to handle this signal, 25 * WebKit will provide a default handler which will asynchronously run 26 * a regular #GtkFileChooserDialog for the user to interact with. 27 */ 28 public class FileChooserRequest : ObjectG 29 { 30 /** the main Gtk struct */ 31 protected WebKitFileChooserRequest* webKitFileChooserRequest; 32 33 /** Get the main Gtk struct */ 34 public WebKitFileChooserRequest* getFileChooserRequestStruct(bool transferOwnership = false) 35 { 36 if (transferOwnership) 37 ownedRef = false; 38 return webKitFileChooserRequest; 39 } 40 41 /** the main Gtk struct as a void* */ 42 protected override void* getStruct() 43 { 44 return cast(void*)webKitFileChooserRequest; 45 } 46 47 /** 48 * Sets our main struct and passes it to the parent class. 49 */ 50 public this (WebKitFileChooserRequest* webKitFileChooserRequest, bool ownedRef = false) 51 { 52 this.webKitFileChooserRequest = webKitFileChooserRequest; 53 super(cast(GObject*)webKitFileChooserRequest, ownedRef); 54 } 55 56 57 /** */ 58 public static GType getType() 59 { 60 return webkit_file_chooser_request_get_type(); 61 } 62 63 /** 64 * Ask WebKit to cancel the request. It's important to do this in case 65 * no selection has been made in the client, otherwise the request 66 * won't be properly completed and the browser will keep the request 67 * pending forever, which might cause the browser to hang. 68 */ 69 public void cancel() 70 { 71 webkit_file_chooser_request_cancel(webKitFileChooserRequest); 72 } 73 74 /** 75 * Get the list of MIME types the file chooser dialog should handle, 76 * in the format specified in RFC 2046 for "media types". Its contents 77 * depend on the value of the 'accept' attribute for HTML input 78 * elements. This function should normally be called before presenting 79 * the file chooser dialog to the user, to decide whether to allow the 80 * user to select multiple files at once or only one. 81 * 82 * Returns: a 83 * %NULL-terminated array of strings if a list of accepted MIME types 84 * is defined or %NULL otherwise, meaning that any MIME type should be 85 * accepted. This array and its contents are owned by WebKit and 86 * should not be modified or freed. 87 */ 88 public string[] getMimeTypes() 89 { 90 return Str.toStringArray(webkit_file_chooser_request_get_mime_types(webKitFileChooserRequest)); 91 } 92 93 /** 94 * Get the filter currently associated with the request, ready to be 95 * used by #GtkFileChooser. This function should normally be called 96 * before presenting the file chooser dialog to the user, to decide 97 * whether to apply a filter so the user would not be allowed to 98 * select files with other MIME types. 99 * 100 * See webkit_file_chooser_request_get_mime_types() if you are 101 * interested in getting the list of accepted MIME types. 102 * 103 * Returns: a #GtkFileFilter if a list of accepted 104 * MIME types is defined or %NULL otherwise. The returned object is 105 * owned by WebKit should not be modified or freed. 106 */ 107 public FileFilter getMimeTypesFilter() 108 { 109 auto __p = webkit_file_chooser_request_get_mime_types_filter(webKitFileChooserRequest); 110 111 if(__p is null) 112 { 113 return null; 114 } 115 116 return ObjectG.getDObject!(FileFilter)(cast(GtkFileFilter*) __p); 117 } 118 119 /** 120 * Determine whether the file chooser associated to this 121 * #WebKitFileChooserRequest should allow selecting multiple files, 122 * which depends on the HTML input element having a 'multiple' 123 * attribute defined. 124 * 125 * Returns: %TRUE if the file chooser should allow selecting multiple files or %FALSE otherwise. 126 */ 127 public bool getSelectMultiple() 128 { 129 return webkit_file_chooser_request_get_select_multiple(webKitFileChooserRequest) != 0; 130 } 131 132 /** 133 * Get the list of selected files currently associated to the 134 * request. Initially, the return value of this method contains any 135 * files selected in previous file chooser requests for this HTML 136 * input element. Once webkit_file_chooser_request_select_files, the 137 * value will reflect whatever files are given. 138 * 139 * This function should normally be called only before presenting the 140 * file chooser dialog to the user, to decide whether to perform some 141 * extra action, like pre-selecting the files from a previous request. 142 * 143 * Returns: a 144 * %NULL-terminated array of strings if there are selected files 145 * associated with the request or %NULL otherwise. This array and its 146 * contents are owned by WebKit and should not be modified or 147 * freed. 148 */ 149 public string[] getSelectedFiles() 150 { 151 return Str.toStringArray(webkit_file_chooser_request_get_selected_files(webKitFileChooserRequest)); 152 } 153 154 /** 155 * Ask WebKit to select local files for upload and complete the 156 * request. 157 * 158 * Params: 159 * files = a 160 * %NULL-terminated array of strings, containing paths to local files. 161 */ 162 public void selectFiles(string[] files) 163 { 164 webkit_file_chooser_request_select_files(webKitFileChooserRequest, Str.toStringzArray(files)); 165 } 166 }