Verified Commit d8e26d22 authored by M. Sandoval's avatar M. Sandoval 🥑
Browse files

native_filechooser: Minor tweaks

Do not store a reference, but rather pass it along the callback, some
var names were trimmed down, make use of GFile directly rather than
passing to the filepath.
parent 78f75291
......@@ -33,7 +33,6 @@ class CreateDatabase(Gtk.Stack):
open_safe_button = Gtk.Template.Child()
composite = False
_filechooser = None
def __init__(self, window, widget, dbm, back_button):
super().__init__()
......@@ -127,15 +126,15 @@ class CreateDatabase(Gtk.Stack):
keyfile_dlg.set_modal(True)
keyfile_dlg.add_filter(KeyFileFilter())
# We need to hold a reference, otherwise the app crashes.
self._filechooser = keyfile_dlg
keyfile_dlg.connect("response", self._on_filechooser_response)
keyfile_dlg.connect(
"response", self._on_filechooser_response, keyfile_dlg
)
keyfile_dlg.show()
def _on_filechooser_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType) -> None:
self._filechooser = None
response: Gtk.ResponseType,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
self.generate_keyfile_button.set_sensitive(False)
self.generate_keyfile_button.set_label(_("Generating…"))
......
......@@ -27,8 +27,6 @@ class DatabaseSettingsDialog:
new_encryption_algorithm = NotImplemented
new_derivation_algorithm = NotImplemented
_filechooser = None
def __init__(self, unlocked_database):
self.unlocked_database = unlocked_database
self.database_manager = unlocked_database.database_manager
......@@ -192,16 +190,16 @@ class DatabaseSettingsDialog:
select_dialog.add_filter(ffilter)
# We need to hold a reference, otherwise the app crashes.
self._filechooser = select_dialog
select_dialog.connect("response", self._on_select_filechooser_response, button)
select_dialog.connect(
"response", self._on_select_filechooser_response, button, select_dialog
)
select_dialog.show()
def _on_select_filechooser_response(self,
select_dialog: Gtk.Dialog,
response: Gtk.ResponseType,
button: Gtk.Button) -> None:
self._filechooser = None
button: Gtk.Button,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
selected_keyfile = select_dialog.get_file().get_path()
keyfile_hash: str = self.database_manager.create_keyfile_hash(
......@@ -242,16 +240,14 @@ class DatabaseSettingsDialog:
ffilter.add_mime_type("text/plain")
ffilter.add_mime_type("application/x-iwork-keynote-sffkey")
# We need to hold a reference, otherwise the app crashes.
self._filechooser = save_dialog
save_dialog.add_filter(ffilter)
save_dialog.connect("response", self._on_filechooser_response)
save_dialog.connect("response", self._on_filechooser_response, save_dialog)
save_dialog.show()
def _on_filechooser_response(self,
save_dialog: Gtk.Dialog,
response: Gtk.ResponseType) -> None:
self._filechooser = None
response: Gtk.ResponseType,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
self.generate_keyfile_button.set_sensitive(False)
......
......@@ -26,8 +26,6 @@ class EntryPage(Gtk.ScrolledWindow):
__gtype_name__ = "EntryPage"
_filechooser = None
name_property_value_entry = Gtk.Template.Child()
username_property_value_entry = Gtk.Template.Child()
......@@ -341,20 +339,16 @@ class EntryPage(Gtk.ScrolledWindow):
select_dialog.set_modal(True)
select_dialog.set_select_multiple(True)
# We need to hold a reference, otherwise the app crashes.
self._filechooser = select_dialog
select_dialog.connect("response", self._on_select_filechooser_response)
select_dialog.connect("response", self._on_select_filechooser_response, select_dialog)
select_dialog.show()
def _on_select_filechooser_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType) -> None:
self._filechooser = None
response: Gtk.ResponseType,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
safe_entry: SafeEntry = self.unlocked_database.current_element
for attachment in dialog.get_files():
uri = attachment.get_uri()
attachment = Gio.File.new_for_uri(uri)
byte_buffer = attachment.load_bytes()[0].get_data()
filename = attachment.get_basename()
new_attachment = safe_entry.add_attachment(byte_buffer, filename)
......@@ -390,19 +384,26 @@ class EntryPage(Gtk.ScrolledWindow):
save_dialog.set_current_name(attachment.filename)
save_dialog.set_modal(True)
self._filechooser = save_dialog
save_dialog.connect("response", self._on_save_filechooser_response, attachment)
save_dialog.connect(
"response", self._on_save_filechooser_response, attachment, save_dialog
)
save_dialog.show()
def _on_save_filechooser_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType,
attachment: Attachment) -> None:
self._filechooser = None
attachment: Attachment,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
safe_entry: SafeEntry = self.unlocked_database.current_element
bytes_buffer = safe_entry.get_attachment_content(attachment)
self.save_to_disk(dialog.get_file().get_path(), bytes_buffer)
stream = Gio.File.replace(
dialog.get_file(), None, False,
Gio.FileCreateFlags.PRIVATE | Gio.FileCreateFlags.REPLACE_DESTINATION,
None
)
Gio.OutputStream.write_bytes(stream, GLib.Bytes.new(bytes_buffer), None)
stream.close()
def on_attachment_delete_button_clicked(
self, _button, attachment_to_delete, attachment_row):
......@@ -411,19 +412,6 @@ class EntryPage(Gtk.ScrolledWindow):
self.attachment_list_box.remove(attachment_row)
#
# Helper
#
def save_to_disk(self, filepath, byte_buffer):
file = Gio.File.new_for_path(filepath)
stream = Gio.File.replace(
file, None, False,
Gio.FileCreateFlags.PRIVATE
| Gio.FileCreateFlags.REPLACE_DESTINATION, None)
Gio.OutputStream.write_bytes(stream, GLib.Bytes.new(byte_buffer), None)
stream.close()
@Gtk.Template.Callback()
def _on_copy_secondary_button_clicked(
self, widget, _position=None, _eventbutton=None):
......
......@@ -28,7 +28,6 @@ class MainWindow(Handy.ApplicationWindow):
database_manager = NotImplemented
opened_databases: list[UnlockedDatabase] = []
databases_to_save: list[UnlockedDatabase] = []
_filechooser = None
_notification = Notification()
container = Gtk.Template.Child()
......@@ -198,7 +197,7 @@ class MainWindow(Handy.ApplicationWindow):
) -> None:
"""Callback function to open a safe."""
# pylint: disable=too-many-locals
filechooser_opening_dialog = Gtk.FileChooserNative.new(
opening_dialog = Gtk.FileChooserNative.new(
# NOTE: Filechooser title for opening an existing keepass safe kdbx file
_("Choose a Keepass safe"), self, Gtk.FileChooserAction.OPEN,
None, None)
......@@ -213,23 +212,23 @@ class MainWindow(Handy.ApplicationWindow):
filter_text.set_name(_("KeePass 3.1/4 Database"))
for mime_type in supported_mime_types:
filter_text.add_mime_type(mime_type)
filechooser_opening_dialog.add_filter(filter_text)
opening_dialog.add_filter(filter_text)
# We need to hold a reference, otherwise the app crashes.
self._filechooser = filechooser_opening_dialog
filechooser_opening_dialog.connect("response", self._on_open_filechooser_response, supported_mime_types)
filechooser_opening_dialog.show()
opening_dialog.connect(
"response", self._on_open_filechooser_response, supported_mime_types, opening_dialog
)
opening_dialog.show()
def _on_open_filechooser_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType,
supported_mime_types: list[str]) -> None:
self._filechooser = None
supported_mime_types: list[str],
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
db_filename = dialog.get_file().get_path()
db_gfile = dialog.get_file()
db_filename = db_gfile.get_path()
logging.debug("File selected: %s", db_filename)
db_gfile = Gio.File.new_for_path(db_filename)
try:
db_file_info = db_gfile.query_info(
# https://gitlab.gnome.org/GNOME/gvfs/-/issues/529
......@@ -282,7 +281,7 @@ class MainWindow(Handy.ApplicationWindow):
self, _action: Gio.Action, _parameter: GLib.Variant
) -> None:
"""Callback function to create a new safe."""
filechooser_creation_dialog = Gtk.FileChooserNative.new(
creation_dialog = Gtk.FileChooserNative.new(
# NOTE: Filechooser title for creating a new keepass safe kdbx file
_("Choose location for Keepass safe"),
self,
......@@ -290,24 +289,24 @@ class MainWindow(Handy.ApplicationWindow):
_("_Create"),
None,
)
filechooser_creation_dialog.set_current_name(_("Safe") + ".kdbx")
filechooser_creation_dialog.set_modal(True)
creation_dialog.set_current_name(_("Safe") + ".kdbx")
creation_dialog.set_modal(True)
filter_text = Gtk.FileFilter()
# NOTE: KeePass + version number is a proper name, do not translate
filter_text.set_name(_("KeePass 3.1/4 Database"))
filter_text.add_mime_type("application/x-keepass2")
filechooser_creation_dialog.add_filter(filter_text)
creation_dialog.add_filter(filter_text)
# We need to hold a reference, otherwise the app crashes.
self._filechooser = filechooser_creation_dialog
filechooser_creation_dialog.connect("response", self._on_create_filechooser_response)
filechooser_creation_dialog.show()
creation_dialog.connect(
"response", self._on_create_filechooser_response, creation_dialog
)
creation_dialog.show()
def _on_create_filechooser_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType) -> None:
self._filechooser = None
response: Gtk.ResponseType,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
filepath = dialog.get_file().get_path()
......
......@@ -48,8 +48,6 @@ class UnlockDatabase:
overlay = NotImplemented
unlock_thread = NotImplemented
_filechooser = None
def __init__(self, window, widget, filepath):
self.window = window
self.parent_widget = widget
......@@ -211,18 +209,20 @@ class UnlockDatabase:
def _on_keyfile_unlock_select_button_clicked(self, _widget):
"""cb invoked when we unlock a database via keyfile"""
keyfile_chooser_dialog = Gtk.FileChooserNative.new(_("Choose a keyfile"), self.window, Gtk.FileChooserAction.OPEN, None, None)
keyfile_chooser_dialog = Gtk.FileChooserNative.new(
_("Choose a keyfile"), self.window, Gtk.FileChooserAction.OPEN, None, None
)
keyfile_chooser_dialog.add_filter(KeyFileFilter())
# We need to hold a reference, otherwise the app crashes.
self._filechooser = keyfile_chooser_dialog
keyfile_chooser_dialog.connect("response", self.on_dialog_response)
keyfile_chooser_dialog.connect(
"response", self.on_dialog_response, keyfile_chooser_dialog
)
keyfile_chooser_dialog.show()
def on_dialog_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType) -> None:
self._filechooser = None
response: Gtk.ResponseType,
_dialog: Gtk.Dialog) -> None:
if response == Gtk.ResponseType.ACCEPT:
self.keyfile_path = dialog.get_file().get_path()
logging.debug("Keyfile selected: %s", self.keyfile_path)
......@@ -241,7 +241,6 @@ class UnlockDatabase:
logging.debug("File selection canceled")
def _on_keyfile_unlock_button_clicked(self, _widget):
self._filechooser = None
if self.database_manager:
if (self.keyfile_path is not NotImplemented
and self.database_manager.keyfile_hash == self.database_manager.create_keyfile_hash(self.keyfile_path)):
......@@ -257,20 +256,20 @@ class UnlockDatabase:
# Composite Unlock
def _on_composite_unlock_select_button_clicked(self, _widget):
filechooser_opening_dialog = Gtk.FileChooserNative.new(
opening_dialog = Gtk.FileChooserNative.new(
_("Choose Keyfile"), self.window, Gtk.FileChooserAction.OPEN,
None, None)
filechooser_opening_dialog.add_filter(KeyFileFilter())
opening_dialog.add_filter(KeyFileFilter())
# We need to hold a reference, otherwise the app crashes.
self._filechooser = filechooser_opening_dialog
filechooser_opening_dialog.connect("response", self._on_composite_filechooser_response)
filechooser_opening_dialog.show()
opening_dialog.connect(
"response", self._on_composite_filechooser_response, opening_dialog
)
opening_dialog.show()
def _on_composite_filechooser_response(self,
dialog: Gtk.Dialog,
response: Gtk.ResponseType) -> None:
self._filechooser = None
response: Gtk.ResponseType,
_dialog: Gtk.Dialog) -> None:
composite_unlock_select_button = self.builder.get_object("composite_unlock_select_button")
if response == Gtk.ResponseType.ACCEPT:
logging.debug("File selected: %s", dialog.get_file().get_path)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment