def __init__(self, mimatemp_model, parent=None):
        super().__init__(mimatemp_model, parent)

        self.setMinimumWidth(425)
        self.setWindowTitle('Add')
        self.box = MimaBox()

        super()._init_ui_()
    def recover(self):
        modelIndex = self.tableView.currentIndex()

        if not modelIndex.model():
            self.show_not_selected_messagebox()
            return

        answer = confirm_deletion(self, 'Recover the selected record?')

        if answer == QtWidgets.QMessageBox.Yes:
            nonceIndex = modelIndex.sibling(modelIndex.row(),
                                            MIMA_COLUMNS['nonce'])
            nonce = nonceIndex.data()
            box = MimaBox(nonce)
            box.restore_by_nonce()
            box.deleted = EPOCH
            while True:
                if box.is_unique():
                    box.recover()
                    modelIndex.model().submitAll()
                    self.homeTableModel.submitAll()
                    return
                else:
                    title, ok = QtWidgets.QInputDialog.getText(
                        self,
                        'Not Unique',
                        'The (title, username) pair already exists.\n'
                        'Please input a new title.',
                        text=box.title)
                    if not ok:
                        return
                    box.title = title
def main():
    key = hashlib.sha256(OLD_PASSWORD.encode()).digest()
    old_secretbox = nacl.secret.SecretBox(key)

    key = hashlib.sha256(NEW_PASSWORD.encode()).digest()
    new_secretbox = nacl.secret.SecretBox(key)

    MimaBox.secretbox = new_secretbox

    # Similar to connection.populate_temp_tables
    sql1 = 'SELECT nonce, encrypted FROM mima'
    sql2 = 'SELECT nonce, encrypted FROM history'

    query = QtSql.QSqlQuery()
    query.setForwardOnly(True)

    for sql in (sql1, sql2):
        query.exec_(sql)
        while query.next():
            nonce = query.value('nonce')
            encrypted = query.value('encrypted').data()
            mima_dict = json.loads(old_secretbox.decrypt(encrypted).decode())
            if 'history' in sql:
                box = HistoryBox(nonce=nonce, **mima_dict)
            else:
                box = MimaBox(nonce=nonce, **mima_dict)
            box.update_database()
    def show_edit_dialog(self):
        self.autoCloseTab.reset_timer()
        modelIndex = self.tableView.currentIndex()

        if not modelIndex.model():
            self.show_not_selected_messagebox()
            return

        nonceIndex = modelIndex.sibling(modelIndex.row(),
                                        MIMA_COLUMNS['nonce'])
        nonce = nonceIndex.data()
        box = MimaBox(nonce)
        box.restore_by_nonce()

        editDialog = EditDialog(box, self.model, self)
        editDialog.exec_()
Example #5
0
def create_a_random_box():
    """
    Let title and username be blank,
    and fill other text fields with random text.
    """
    return MimaBox(website=random_text(),
                   password=random_text(),
                   notes=random_text())
class AddDialog(MyDialog):
    def __init__(self, mimatemp_model, parent=None):
        super().__init__(mimatemp_model, parent)

        self.setMinimumWidth(425)
        self.setWindowTitle('Add')
        self.box = MimaBox()

        super()._init_ui_()

    def really_accept(self):
        self.box.title = self.titleEdit.text().strip()
        self.box.username = self.usernameEdit.text().strip()
        self.box.website = self.websiteEdit.text().strip()
        self.box.password = self.passwordEdit.toPlainText().strip()
        self.box.notes = self.notesEdit.toPlainText().strip()

        if self.box.is_unique():
            self.box.insert_into_database_and_temp()
            self.mimatemp_model.submitAll()
            QDialog.accept(self)
        else:
            self.show_not_unique_message()
    def delete(self):

        modelIndex = self.tableView.currentIndex()
        if not modelIndex.model():
            self.show_not_selected_messagebox()
            return

        answer = confirm_deletion(self, self.confirm_message)
        if answer == QtWidgets.QMessageBox.Yes:
            nonceIndex = modelIndex.sibling(modelIndex.row(),
                                            MIMA_COLUMNS['nonce'])
            nonce = nonceIndex.data()
            box = MimaBox(nonce)

            self.move_to_trash_or_delete(box)
    def really_accept(self):
        has_changed = False

        old_values = [
            self.box.title, self.box.username, self.box.website,
            self.box.password, self.box.notes
        ]
        new_values = [
            self.titleEdit.text().strip(),
            self.usernameEdit.text().strip(),
            self.websiteEdit.text().strip(),
            self.passwordEdit.toPlainText().strip(),
            self.notesEdit.toPlainText().strip()
        ]
        for i in range(len(old_values)):
            if old_values[i] != new_values[i]:
                has_changed = True
                break

        if not has_changed:
            QDialog.reject(self)
            return
        has_changed = False

        new_box_dict = dict(title=self.titleEdit.text().strip(),
                            username=self.usernameEdit.text().strip(),
                            website=self.websiteEdit.text().strip(),
                            password=self.passwordEdit.toPlainText().strip(),
                            notes=self.notesEdit.toPlainText().strip(),
                            favorite=self.box.favorite)
        new_box = MimaBox(nonce=self.box.nonce, **new_box_dict)

        if new_box.is_unique_except_itself():
            historyBox = HistoryBox()
            historyBox.get_values_from_mimabox(self.box.nonce)
            historyBox.deleted = datetime.datetime.now().isoformat(sep=' ')
            historyBox.insert_into_database_and_temp()
            self.model.submitAll()

            new_box.update_temp()
            new_box.update_database()
            self.mimatemp_model.submitAll()

            QDialog.accept(self)
        else:
            self.show_not_unique_message()
def populate_temp_tables():
    sql1 = 'SELECT nonce, encrypted FROM mima'
    sql2 = 'SELECT nonce, mimanonce, encrypted FROM history'

    query = QtSql.QSqlQuery()
    query.setForwardOnly(True)

    for sql in (sql1, sql2):
        query.exec_(sql)
        while query.next():
            nonce = query.value('nonce')
            encrypted = query.value('encrypted').data()
            mima_dict = json.loads(
                MimaBox.secretbox.decrypt(encrypted).decode())
            if 'history' in sql:
                box = HistoryBox(nonce=nonce, **mima_dict)
                box.mimanonce = query.value('mimanonce')
            else:
                box = MimaBox(nonce=nonce, **mima_dict)
            box.insert_into_temp()
 def copy_or_favorite(self, modelIndex):
     if modelIndex.column() == MIMA_COLUMNS['favorite']:
         nonceIndex = modelIndex.sibling(modelIndex.row(),
                                         MIMA_COLUMNS['nonce'])
         nonce = nonceIndex.data()
         box = MimaBox(nonce)
         box.restore_by_nonce()
         box.toggle_favorite()
         modelIndex.model().submitAll()
     elif modelIndex.data():
         # if modelIndex.column() == self.PASSWORD:
         #     self.clipboard.setText(modelIndex.data(QtCore.Qt.UserRole))
         self.clipboard.setText(modelIndex.data())
         TimerMessageBox(QMessageBox.Information,
                         'Copied',
                         'Copied',
                         parent=self).exec_()
if __name__ == '__main__':
    # from PyQt5.QtCore import QByteArray

    app = QtWidgets.QApplication(sys.argv)

    if not create_connection():
        sys.exit(1)

    masterpassword = '******'
    key = hashlib.sha256(masterpassword.encode()).digest()
    secretbox = nacl.secret.SecretBox(key)
    MimaBox.secretbox = secretbox

    populate_temp_tables()

    box = MimaBox(title='aaa', username='******')
    print('UNIQUE: ', box.is_unique())
    box.insert_into_database_and_temp()

    query = QtSql.QSqlQuery()
    query.prepare('select nonce from mimatemp where nonce=?')
    query.addBindValue(box.nonce)
    query.exec_()
    query.next()
    print(query.value(0))  # QByteArray
    print(query.value(0).data())  # bytes

    # ID, NAME, CITY = range(3)

    # query = QtSql.QSqlQuery()
    # query.exec_('SELECT id, name, city FROM employee')