# Path: meld/misc.py
# def get_modal_parent(widget: Optional[Gtk.Widget] = None) -> Gtk.Window:
#     parent: Gtk.Window
#     if not widget:
#         parent = Gtk.Application.get_default().get_active_window()
#     elif not isinstance(widget, Gtk.Window):
#         parent = widget.get_toplevel()
#     else:
#         parent = widget
#     return parent
# 
# def modal_dialog(
#     primary: str,
#     secondary: str,
#     buttons: Union[Gtk.ButtonsType, Sequence[Tuple[str, int]]],
#     parent: Optional[Gtk.Window] = None,
#     messagetype: Gtk.MessageType = Gtk.MessageType.WARNING
# ) -> Gtk.ResponseType:
#     """A common message dialog handler for Meld
# 
#     This should only ever be used for interactions that must be resolved
#     before the application flow can continue.
# 
#     Primary must be plain text. Secondary must be valid markup.
#     """
# 
#     custom_buttons: Sequence[Tuple[str, int]] = []
#     if not isinstance(buttons, Gtk.ButtonsType):
#         custom_buttons, buttons = buttons, Gtk.ButtonsType.NONE
# 
#     dialog = Gtk.MessageDialog(
#         transient_for=get_modal_parent(parent),
#         modal=True,
#         destroy_with_parent=True,
#         message_type=messagetype,
#         buttons=buttons,
#         text=primary)
#     dialog.format_secondary_markup(secondary)
# 
#     for label, response_id in custom_buttons:
#         dialog.add_button(label, response_id)
# 
#     response = dialog.run()
#     dialog.destroy()
#     return response


import pathlib
from typing import Optional, Sequence
from gi.repository import Gio, GLib, Gtk
from meld.conf import _
from meld.misc import get_modal_parent, modal_dialog
        title: str, parent: Optional[Gtk.Widget] = None) -> Optional[Gio.File]:

    dialog = Gtk.FileChooserNative(
        title=title,
        transient_for=get_modal_parent(parent),
        action=Gtk.FileChooserAction.SAVE,
    )
    response = dialog.run()
    gfile = dialog.get_file()
    dialog.destroy()

    if response != Gtk.ResponseType.ACCEPT or not gfile:
        return None

    try:
        file_info = gfile.query_info(
            'standard::name,standard::display-name',
            Gio.FileQueryInfoFlags.NONE,
            None,
        )
    except GLib.Error as err:
        if err.code == Gio.IOErrorEnum.NOT_FOUND:
            return gfile
        raise

    # The selected file exists, so we need to prompt for overwrite.
    parent_folder = gfile.get_parent()
    parent_name = parent_folder.get_parse_name() if parent_folder else ''
    file_name = file_info.get_display_name()


# Path: meld/misc.py
# def get_modal_parent(widget: Optional[Gtk.Widget] = None) -> Gtk.Window:
#     parent: Gtk.Window
#     if not widget:
#         parent = Gtk.Application.get_default().get_active_window()
#     elif not isinstance(widget, Gtk.Window):
#         parent = widget.get_toplevel()
#     else:
#         parent = widget
#     return parent
# 
# def modal_dialog(
#     primary: str,
#     secondary: str,
#     buttons: Union[Gtk.ButtonsType, Sequence[Tuple[str, int]]],
#     parent: Optional[Gtk.Window] = None,
#     messagetype: Gtk.MessageType = Gtk.MessageType.WARNING
# ) -> Gtk.ResponseType:
#     """A common message dialog handler for Meld
# 
#     This should only ever be used for interactions that must be resolved
#     before the application flow can continue.
# 
#     Primary must be plain text. Secondary must be valid markup.
#     """
# 
#     custom_buttons: Sequence[Tuple[str, int]] = []
#     if not isinstance(buttons, Gtk.ButtonsType):
#         custom_buttons, buttons = buttons, Gtk.ButtonsType.NONE
# 
#     dialog = Gtk.MessageDialog(
#         transient_for=get_modal_parent(parent),
#         modal=True,
#         destroy_with_parent=True,
#         message_type=messagetype,
#         buttons=buttons,
#         text=primary)
#     dialog.format_secondary_markup(secondary)
# 
#     for label, response_id in custom_buttons:
#         dialog.add_button(label, response_id)
# 
#     response = dialog.run()
#     dialog.destroy()
#     return response


# Path: meld/iohelpers.py
import pathlib
from typing import Optional, Sequence
from gi.repository import Gio, GLib, Gtk
from meld.conf import _
from meld.misc import get_modal_parent, modal_dialog

        title: str, parent: Optional[Gtk.Widget] = None) -> Optional[Gio.File]:

    dialog = Gtk.FileChooserNative(
        title=title,
        transient_for=get_modal_parent(parent),
        action=Gtk.FileChooserAction.SAVE,
    )
    response = dialog.run()
    gfile = dialog.get_file()
    dialog.destroy()

    if response != Gtk.ResponseType.ACCEPT or not gfile:
        return None

    try:
        file_info = gfile.query_info(
            'standard::name,standard::display-name',
            Gio.FileQueryInfoFlags.NONE,
            None,
        )
    except GLib.Error as err:
        if err.code == Gio.IOErrorEnum.NOT_FOUND:
            return gfile
        raise

    # The selected file exists, so we need to prompt for overwrite.
    parent_folder = gfile.get_parent()
    parent_name = parent_folder.get_parse_name() if parent_folder else ''
    file_name = file_info.get_display_name()


    replace = modal_dialog(