From cd122b895961603c383284d0423eca2f70b2f448 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Wed, 29 Jul 2020 22:02:49 +0800 Subject: [PATCH] Refactor(Replace isKeyA with KeyType) --- module/mifare.cpp | 18 +++++++++--------- module/mifare.h | 10 ++++++++-- ui/mainwindow.cpp | 8 ++++---- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/module/mifare.cpp b/module/mifare.cpp index b132be5..fccc46f 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -282,7 +282,7 @@ void Mifare::readAll() if(result.indexOf("isOk:01") != -1) { keyBList->replace(i, tmpKey); - data_syncWithKeyWidget(false, i, false); + data_syncWithKeyWidget(false, i, KEY_B); } else { @@ -407,8 +407,8 @@ void Mifare::readAllC() } keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12)); keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12)); - data_syncWithKeyWidget(false, i, true); - data_syncWithKeyWidget(false, i, false); + data_syncWithKeyWidget(false, i, KEY_A); + data_syncWithKeyWidget(false, i, KEY_B); } } } @@ -559,8 +559,8 @@ void Mifare::readAllE() } keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12)); keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12)); - data_syncWithKeyWidget(false, i, true); - data_syncWithKeyWidget(false, i, false); + data_syncWithKeyWidget(false, i, KEY_A); + data_syncWithKeyWidget(false, i, KEY_B); } } @@ -626,7 +626,7 @@ void Mifare::data_syncWithDataWidget(bool syncAll, int block) } } -void Mifare::data_syncWithKeyWidget(bool syncAll, int sector, bool isKeyA) +void Mifare::data_syncWithKeyWidget(bool syncAll, int sector, KeyType keyType) { if(syncAll) { @@ -638,7 +638,7 @@ void Mifare::data_syncWithKeyWidget(bool syncAll, int sector, bool isKeyA) } else { - if(isKeyA) + if(keyType == KEY_A) ui->MF_keyWidget->item(sector, 1)->setText(keyAList->at(sector)); else ui->MF_keyWidget->item(sector, 2)->setText(keyBList->at(sector)); @@ -976,9 +976,9 @@ void Mifare::data_setData(int block, const QString &data) dataList->replace(block, data); } -void Mifare::data_setKey(int sector, bool isKeyA, const QString &key) +void Mifare::data_setKey(int sector, KeyType keyType, const QString &key) { - if(isKeyA) + if(keyType == KEY_A) keyAList->replace(sector, key); else keyBList->replace(sector, key); diff --git a/module/mifare.h b/module/mifare.h index 4d804b6..bb54d83 100644 --- a/module/mifare.h +++ b/module/mifare.h @@ -30,6 +30,12 @@ public: void dump(); void restore(); + enum KeyType + { + KEY_A, + KEY_B, + }; + enum DataType { DATA_INVALID, @@ -84,7 +90,7 @@ public: bool data_isKeyValid(const QString &key); Mifare::DataType data_isDataValid(QString data); void data_syncWithDataWidget(bool syncAll = true, int block = 0); - void data_syncWithKeyWidget(bool syncAll = true, int sector = 0, bool isKeyA = true); + void data_syncWithKeyWidget(bool syncAll = true, int sector = 0, KeyType keyType = KEY_A); CardType cardType; Mifare::CardType getCardType(); @@ -104,7 +110,7 @@ public: void data_data2Key(); void data_setData(int block, const QString &data); - void data_setKey(int sector, bool isKeyA, const QString &key); + void data_setKey(int sector, KeyType keyType, const QString &key); void lockC(); void writeAllE(); void readAllE(); diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 0ecf7b4..a6ba92f 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -337,26 +337,26 @@ void MainWindow::on_MF_keyWidget_itemChanged(QTableWidgetItem *item) QString key = item->text().replace(" ", "").toUpper(); if(key == "" || mifare->data_isKeyValid(key)) { - mifare->data_setKey(item->row(), true, key); + mifare->data_setKey(item->row(), Mifare::KEY_A, key); } else { QMessageBox::information(this, tr("Info"), tr("Key must consists of 12 Hex symbols(Whitespace is allowed)")); } - mifare->data_syncWithKeyWidget(false, item->row(), true); + mifare->data_syncWithKeyWidget(false, item->row(), Mifare::KEY_A); } else if(item->column() == 2) { QString key = item->text().replace(" ", "").toUpper(); if(key == "" || mifare->data_isKeyValid(key)) { - mifare->data_setKey(item->row(), false, key); + mifare->data_setKey(item->row(), Mifare::KEY_B, key); } else { QMessageBox::information(this, tr("Info"), tr("Key must consists of 12 Hex symbols(Whitespace is allowed)")); } - mifare->data_syncWithKeyWidget(false, item->row(), false); + mifare->data_syncWithKeyWidget(false, item->row(), Mifare::KEY_B); } }