Пример #1
0
    def __show_edit_secret_dialog(self):
        """Show the dialog that allows to modify the secret selected in the list.
        An exception is raised if no selection has been made or the selection
        does not correspond to any secret in self.secrets
        """
        selected_secret = self.get_selected_secret()
        if selected_secret is None:
            raise Exception("Could not find a valid selected secret in self.secrets "
                            "(this method should not have been called)")

        # A copy of the selected secret is passed to the dialog, so that changes can be reverted
        secret_copy = model.Secret.copy(selected_secret)
        dialog = EditSecretDialog(parent=self, pyluggage_config=self.pyluggage_config, previous_secret=secret_copy)
        dialog.setModal(True)
        result = dialog.exec_()
        if result == QDialog.Accepted:
            if secret_copy.has_same_data(selected_secret):
                # Don't need to save, no changes have been performed
                return

            # Check that the name did not exist
            if secret_copy.name != selected_secret.name:
                valid_name = False
                while not valid_name:
                    chosen_name = secret_copy.name.strip().lower()
                    matching_secrets = [s for s in self.secret_list if s.name.strip().lower() == chosen_name]
                    if len(matching_secrets) > 0:
                        message = _translate("A secret with the same name already exists.\nPlease provide a unique name:\n")
                        title = _translate("Duplicated secret name")
                        chosen_name, ok = QInputDialog.getText(self, title, message)
                        chosen_name = unicode(chosen_name).strip()
                        if chosen_name == "" or not ok:
                            chosen_name = selected_secret.name
                        if chosen_name == selected_secret.name:
                            valid_name = True
                        secret_copy.name = chosen_name
                    else:
                        valid_name = True

            model.Secret.copy(secret_copy, selected_secret)
            self.secret_list = sorted(self.secret_list, key=lambda s:s.name.lower())
            self.secrets_changed.emit()
            self.__update_tags_combo_model()
            self.__update_listwidget_contents()
            self.select_secret(dialog.accepted_secret)
Пример #2
0
 def __show_new_secret_dialog(self):
     """Show the dialog that allows to create a new secret
     """
     dialog = EditSecretDialog(parent=self, pyluggage_config=self.pyluggage_config)
     dialog.setModal(True)
     if self.combo_tags.currentIndex() != 0:
         dialog.lineedit_tags.setText(self.combo_tags.currentText())
     result = dialog.exec_()
     if result == QDialog.Accepted:
         matching_secrets = [s for s in self.secret_list
                             if s.name.strip().lower() == dialog.accepted_secret.name.strip().lower()]
         if len(matching_secrets) > 0:
             # A secret with the same name existed: merge or ignore
             message = _translate("A secret with the same name already exists.\nMerge into the existing secret or discard the new secret?")
             title = _translate("Duplicated secret name")
             reply = QMessageBox.question(
                 self, title, message,
                  QMessageBox.Save, QMessageBox.Discard)
             if reply == QMessageBox.Save:
                 matching_secrets[0].merge(dialog.accepted_secret)
                 self.__update_listwidget_contents()
                 self.__update_tags_combo_model()
                 self.select_secret(matching_secrets[0])
                 self.secrets_changed.emit()
             if len(matching_secrets) > 1:
                 raise Exception("Warning! More than one match for the new secret {} found: {}".format(
                     dialog.accepted_secret, map(str, matching_secrets)))
         else:
             # Add the secret to the list
             self.secret_list.append(dialog.accepted_secret)
             self.secret_list = sorted(self.secret_list, key=lambda s:s.name.strip().lower())
             self.secrets_changed.emit()
             self.__update_tags_combo_model()
             self.__update_listwidget_contents()
             try:
                 self.select_secret(dialog.accepted_secret)
             except KeyError:
                 # The new item was filtered out, no problem
                 pass