Skip to content

export: Make filename extension obvious

I want to close #18 (closed) with this:

The design is based on file-roller 43's new-archive-dialog: https://gitlab.gnome.org/GNOME/file-roller/-/blob/43.1/src/ui/new-archive-dialog.ui.


My idea here is, since panel-profiles does not support many file formats, allowing users to edit file extensions still requires us to check what their input is and warn users when needed, so simply disallow editing file extensions make things easier... 😂

Some of my other initial attempts

The following will allow editing file extensions:

def strip_ext_from_filename(filename):
    name = filename
    for ext in [".bz2", ".gz", ".tar"]:
        if name.endswith(ext):
            name = name[:-len(ext)]
    return name

class PanelExportDialog(Gtk.FileChooserDialog):
    '''Ask the user where to export the configuration, and what file format
    should be used.'''

    def __init__(self, parent=None):
        message = _("Export configuration as...")

        Gtk.FileChooserDialog.__init__(self, message, parent, Gtk.FileChooserAction.SAVE)

        # self.set_do_overwrite_confirmation(True)
        self.set_current_name(_("Untitled") + ".tar.bz2")
        self.add_buttons(
            _("Cancel"), Gtk.ResponseType.CANCEL,
            _("Save"), Gtk.ResponseType.ACCEPT
        )

        self.extras_box = Gtk.VBox(spacing=6)
        self.set_extra_widget(self.extras_box)
        self.extras_box.show()

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append([_("Tar compressed with bzip2"), ".tar.bz2"])
        self.liststore.append([_("Tar compressed with gzip"), ".tar.gz"])
        self.list = Gtk.TreeView(self.liststore)
        self.list.set_headers_visible(False)

        ext_desc, ext_name = Gtk.CellRendererText(), Gtk.CellRendererText()
        column = Gtk.TreeViewColumn()
        column.pack_start(ext_desc, True)
        column.pack_start(ext_name, False)
        column.set_attributes(ext_desc, text=0)
        column.set_attributes(ext_name, text=1)

        self.list.append_column(column)
        self.list.show_all()

        self.expander = Gtk.Expander.new(_('File format'))
        self.expander.set_expanded(True)
        self.expander.add(self.list)
        self.extras_box.pack_start(self.expander, False, False, 0)
        self.expander.show()

        selection = self.list.get_selection()
        selection.select_iter(self.liststore.get_iter_first())
        selection.connect('changed', self.on_selection_changed)

    def on_selection_changed(self, selection):
        model, iter = selection.get_selected()
        extension, = model.get(iter, 1)
        filename = strip_ext_from_filename(os.path.basename(self.get_filename()) if self.get_filename() else "") + extension
        self.set_current_name(filename)
Edited by Bobby Rong

Merge request reports

Loading