static char * define_destination_uri (EphyDownload *download, const char *suggested_filename) { char *dest_dir; char *dest_name; char *destination_filename; char *destination_uri; dest_dir = ephy_file_get_downloads_dir (); /* Make sure the download directory exists */ if (g_mkdir_with_parents (dest_dir, 0700) == -1) { g_critical ("Could not create downloads directory \"%s\": %s", dest_dir, strerror (errno)); g_free (dest_dir); return NULL; } if (suggested_filename != NULL) { dest_name = g_strdup (suggested_filename); } else { dest_name = ephy_file_tmp_filename ("ephy-download-XXXXXX", NULL); } destination_filename = g_build_filename (dest_dir, dest_name, NULL); g_free (dest_dir); g_free (dest_name); /* Append (n) as needed. */ if (g_file_test (destination_filename, G_FILE_TEST_EXISTS)) { int i = 1; const char *dot_pos; gssize position; char *serial = NULL; GString *tmp_filename; dot_pos = parse_extension (destination_filename); if (dot_pos) position = dot_pos - destination_filename; else position = strlen (destination_filename); tmp_filename = g_string_new (NULL); do { serial = g_strdup_printf ("(%d)", i++); g_string_assign (tmp_filename, destination_filename); g_string_insert (tmp_filename, position, serial); g_free (serial); } while (g_file_test (tmp_filename->str, G_FILE_TEST_EXISTS)); destination_filename = g_strdup (tmp_filename->str); g_string_free (tmp_filename, TRUE); } destination_uri = g_filename_to_uri (destination_filename, NULL, NULL); g_free (destination_filename); g_assert (destination_uri); return destination_uri; }
GtkFileChooser * ephy_create_file_chooser (const char *title, GtkWidget *parent, GtkFileChooserAction action, EphyFileFilterDefault default_filter) { GtkFileChooser *dialog; GtkFileFilter *filter[EPHY_FILE_FILTER_LAST]; g_autofree char *downloads_dir = NULL; GtkWidget *preview = gtk_image_new (); g_assert (GTK_IS_WINDOW (parent)); g_assert (default_filter >= 0 && default_filter <= EPHY_FILE_FILTER_LAST); dialog = GTK_FILE_CHOOSER (gtk_file_chooser_native_new (title, GTK_WINDOW (parent), action, NULL, _("_Cancel"))); gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE); downloads_dir = ephy_file_get_downloads_dir (); gtk_file_chooser_add_shortcut_folder (dialog, downloads_dir, NULL); if (action == GTK_FILE_CHOOSER_ACTION_OPEN || action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER) { gtk_file_chooser_native_set_accept_label (GTK_FILE_CHOOSER_NATIVE (dialog), _("_Open")); } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) { gtk_file_chooser_native_set_accept_label (GTK_FILE_CHOOSER_NATIVE (dialog), _("_Save")); } gtk_file_chooser_set_preview_widget (dialog, preview); gtk_file_chooser_set_use_preview_label (dialog, FALSE); g_signal_connect (dialog, "update-preview", G_CALLBACK (update_preview_cb), preview); if (default_filter != EPHY_FILE_FILTER_NONE) { filter[EPHY_FILE_FILTER_ALL_SUPPORTED] = ephy_file_chooser_add_mime_filter (dialog, _("All supported types"), "text/html", "application/xhtml+xml", "text/xml", "message/rfc822", /* MHTML */ "multipart/related", /* MHTML */ "application/x-mimearchive", /* MHTML */ "image/png", "image/jpeg", "image/gif", "image/webp", NULL); filter[EPHY_FILE_FILTER_WEBPAGES] = ephy_file_chooser_add_mime_filter (dialog, _("Web pages"), "text/html", "application/xhtml+xml", "text/xml", "message/rfc822", /* MHTML */ "multipart/related", /* MHTML */ "application/x-mimearchive", /* MHTML */ NULL); filter[EPHY_FILE_FILTER_IMAGES] = ephy_file_chooser_add_mime_filter (dialog, _("Images"), "image/png", "image/jpeg", "image/gif", "image/webp", NULL); filter[EPHY_FILE_FILTER_ALL] = ephy_file_chooser_add_pattern_filter (dialog, _("All files"), "*", NULL); gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter[default_filter]); } return dialog; }