class EL_FILE_SAVE_DIALOG

(source code)

Client examples: APPLICATION_ROOT

description

File save dialog

note
	description: "File save dialog"

	author: "Finnian Reilly"
	copyright: "Copyright (c) 2001-2022 Finnian Reilly"
	contact: "finnian at eiffel hyphen loop dot com"

	license: "MIT license (See: en.wikipedia.org/wiki/MIT_License)"
	date: "2023-08-17 14:58:34 GMT (Thursday 17th August 2023)"
	revision: "12"

class
	EL_FILE_SAVE_DIALOG

inherit
	EV_FILE_SAVE_DIALOG
		rename
			file_path as file_path_string
		redefine
			show_modal_to_window
		end

	EL_MODULE_ACTION

	EL_ZSTRING_CONSTANTS; EL_CHARACTER_32_CONSTANTS

create
	make

feature {NONE} -- Initialization

	make (a_name, description, extension: READABLE_STRING_GENERAL; a_last_saved_dir: DIR_PATH; a_save: like save)
		local
			file_path: FILE_PATH; name: ZSTRING
		do
			make_with_title ("Save as " + description)
			create filter_extensions.make (2)
			save := a_save; last_saved_dir := a_last_saved_dir
			add_filter (description, extension)
			create name.make_from_general (a_name)
			file_path := last_saved_dir + name
			if not file_path.has_extension (extension) then
				file_path.add_extension (extension)
			end
			set_full_file_path (file_path)
		end

feature -- Element change

	add_filter (description, extension: READABLE_STRING_GENERAL)
		local
			wild_card, l_extension, l_description: ZSTRING
		do
			create l_extension.make_from_general (extension)
			filter_extensions.extend (l_extension)
			filter_extensions.last.to_lower
			wild_card := "*."
			wild_card.append_string_general (extension)
			l_description := space.joined (l_extension.as_upper, description)
			filters.extend ([wild_card, Filter_template.substituted_tuple ([l_description, extension]).to_unicode])
		end

feature -- Access

	filter_extensions: EL_ZSTRING_LIST

feature -- Status setting

	show_modal_to_window (a_window: EV_WINDOW)
		do
			save_actions.extend (agent on_save (a_window))
			Precursor (a_window)
		end

feature {NONE} -- Event handling

	on_save (a_window: EV_WINDOW)
		local
			dialog: EL_CONFIRMATION_DIALOG; confirmed: BOOLEAN
			file_path: FILE_PATH; extension, format_extension: ZSTRING
		do
			file_path := full_file_path; extension := file_path.extension.as_lower
			if filters.valid_index (selected_filter_index) then
				create format_extension.make_from_general (filters.i_th (selected_filter_index).filter)
				format_extension.remove_head (2)
				format_extension.to_lower
				if extension /~ format_extension then
					if filter_extensions.has (extension) then
						file_path := file_path.with_new_extension (format_extension)
					else
						file_path.add_extension (format_extension)
					end
				end
			end
			if file_path.exists then
				create dialog.make_with_text (Confirmation_template #$ [file_path.base])
				dialog.show_modal_to_window (a_window)
				confirmed := dialog.ok_selected
			else
				confirmed := True
			end
			if confirmed then
				last_saved_dir.set_path (file_path.parent)
				save (file_path)
			end
		end

feature {NONE} -- Internal attributes

	last_saved_dir: DIR_PATH

	save: PROCEDURE [FILE_PATH]

feature {NONE} -- Constants

	Confirmation_template: ZSTRING
		once
			Result := "[
				The file '#' already exists.
				Do you wish to overwrite it?
			]"
		end

	Filter_template: ZSTRING
		once
			Result := "%S (*.%S)"
		end
end