def delete_note(self): if len(store.get_value("notepad")["notes"]) > 1: dialog = Gtk.MessageDialog( transient_for=self.get_toplevel(), message_type=Gtk.MessageType.QUESTION, buttons=Gtk.ButtonsType.YES_NO, text="Delete Note" ) dialog.format_secondary_text("Are you sure you want to delete this note?") response = dialog.run() dialog.destroy() if response == Gtk.ResponseType.YES: notepad = store.get_value("notepad") del notepad["notes"][self.notebook.get_current_page()] self.notebook.remove_page(self.notebook.get_current_page()) store.set_value("notepad", notepad) else: dialog = Gtk.MessageDialog( transient_for=self.get_toplevel(), message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK, text="Cannot Delete Note" ) dialog.format_secondary_text("You cannot delete the final note.") dialog.run() dialog.destroy()
def create_notepad(self): password = self.password_entry.get_text() if password != self.password_copy_entry.get_text(): dialog = Gtk.MessageDialog(transient_for=self.get_toplevel(), message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK, text="Cannot Create Notepad") dialog.format_secondary_text("Passwords do not match.") dialog.run() dialog.destroy() elif len(password) < 8: dialog = Gtk.MessageDialog(transient_for=self.get_toplevel(), message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK, text="Cannot Create Notepad") dialog.format_secondary_text( "Passwords must be at least 8 characters long.") dialog.run() dialog.destroy() else: store.set_value("password", password) store.set_value( "notepad", { "untitled_number": 1, "notes": [{ "name": "Untitled Note 1", "content": "Your first note." }] }) self.clear() store.get_value("notepad_editor").open_notepad() store.get_value("stack").set_visible_child_name("notepad_editor")
def unlock(self): notepad = store.get_value("notepad") decrypted_notes = decrypt(notepad["notes"], self.password_entry.get_text()) if decrypted_notes == None: dialog = Gtk.MessageDialog(transient_for=self.get_toplevel(), message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK, text="Bad Decrypt") dialog.format_secondary_text( "You may have entered an incorrect password.") dialog.run() dialog.destroy() self.password_entry.set_text("") else: notepad["notes"] = loads(decrypted_notes) store.set_value("password", self.password_entry.get_text()) store.set_value("notepad", notepad) store.get_value("notepad_editor").open_notepad() store.get_value("stack").set_visible_child_name("notepad_editor")
def save(self): unencrypted_notepad = store.get_value("notepad") encrypted_notepad = b64encode( bytes( dumps({ "version": store.get_value("VERSION"), "untitled_number": unencrypted_notepad["untitled_number"], "notes": encrypt( dumps(unencrypted_notepad["notes"]), store.get_value("password") ) }), "utf-8" ) ).decode("utf-8") file_chooser = Gtk.FileChooserDialog( title="Encpad", parent=self.get_toplevel(), action=Gtk.FileChooserAction.SAVE, buttons=("Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.OK) ) file_chooser.set_current_name("Untitled.encpad") response = file_chooser.run() filename = file_chooser.get_filename() file_chooser.destroy() if response == Gtk.ResponseType.OK: with open(filename, "w") as file: file.write( store.get_value("ENCRYPTED_DISCLAIMER") + encrypted_notepad ) sys.exit(0)
def load_notepad(self): file_chooser = Gtk.FileChooserDialog( title="Encpad", parent=self.get_toplevel(), action=Gtk.FileChooserAction.OPEN, buttons=("Cancel", Gtk.ResponseType.CANCEL, "Open", Gtk.ResponseType.OK)) response = file_chooser.run() filename = file_chooser.get_filename() file_chooser.destroy() if response == Gtk.ResponseType.OK: try: with open(filename, "r") as file: parsed_notepad = loads( b64decode( bytes( file.read().replace( store.get_value("ENCRYPTED_DISCLAIMER"), "", 1), "utf-8")).decode("utf-8")) if parsed_notepad["version"] != store.get_value("VERSION"): dialog = Gtk.MessageDialog( transient_for=self.get_toplevel(), message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.YES_NO, text="Version Mismatch") dialog.format_secondary_text( ("This notepad was saved in version %s, " + "but you are using %s.\n" + "Are you sure you want to proceed? " + "Doing so may cause notepad corruption.") % (parsed_notepad["version"], store.get_value("VERSION"))) response = dialog.run() dialog.destroy() if response == Gtk.ResponseType.NO: return store.set_value("notepad", parsed_notepad) store.get_value("stack").set_visible_child_name( "unlock_notepad") except Exception: dialog = Gtk.MessageDialog( transient_for=self.get_toplevel(), message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK, text="Loading Failed") dialog.format_secondary_text("Could not load notepad!") response = dialog.run() dialog.destroy()
def new_note(self): untitled_number = store.get_value("notepad")["untitled_number"] + 1 store.get_value("notepad")["untitled_number"] += 1 note = { "name": "Untitled Note %d" % untitled_number, "content": "A new note" } index = self.init_editor(note) store.get_value("notepad")["notes"].append(note) store.set_value("confirm_close", True) self.notebook.set_current_page(index)
def __init__(self): Gtk.Window.__init__(self, title="Encpad") store.set_value("password", None) store.set_value("notepad", None) self.set_border_width(20) self.set_default_size(360, 560) self.set_resizable(False) stack = Gtk.Stack() stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT) stack.set_transition_duration(350) store.set_value("stack", stack) store.set_value("notepad_editor", NotepadEditor()) stack.add_named(Welcome(), "welcome") stack.add_named(UnlockNotepad(), "unlock_notepad") stack.add_named(CreateNewNotepad(), "create_new_notepad") stack.add_named(store.get_value("notepad_editor"), "notepad_editor") stack.set_visible_child_name("welcome") self.connect("delete-event", self.window_delete) self.add(stack)
def window_delete(self, widget, event): if store.get_value("confirm_close"): dialog = Gtk.MessageDialog(transient_for=window, message_type=Gtk.MessageType.QUESTION, buttons=Gtk.ButtonsType.YES_NO, text="You have unsaved changes") dialog.format_secondary_text( "Are you sure you want to exit without saving them?") response = dialog.run() dialog.destroy() if response != Gtk.ResponseType.YES: return True return False
def __init__(self): Gtk.VBox.__init__(self) self.set_spacing(10) new_notepad_button = Gtk.Button("New Notepad") load_notepad_button = Gtk.Button("Load Notepad") new_notepad_button.connect( "clicked", lambda btn: store.get_value("stack"). set_visible_child_name("create_new_notepad")) load_notepad_button.connect("clicked", lambda btn: self.load_notepad()) self.pack_start(new_notepad_button, True, True, 0) self.pack_start(load_notepad_button, True, True, 0)
def back(self): self.clear() store.get_value("stack").set_visible_child_name("welcome")
def back(self): self.password_entry.set_text("") store.get_value("stack").set_visible_child_name("welcome")
def open_notepad(self): for note in store.get_value("notepad")["notes"]: index = self.init_editor(note)