aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/KeyList.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-01-12 06:02:37 +0000
committersaturneric <[email protected]>2024-01-12 06:02:37 +0000
commitbf538056b24a68b8fd235b1c50991ee8eb46a776 (patch)
treee1bab54095b80df62b321fb5bd69453f9f951b05 /src/ui/widgets/KeyList.cpp
parentfeat: improve api and ui of keys import and export (diff)
downloadGpgFrontend-bf538056b24a68b8fd235b1c50991ee8eb46a776.tar.gz
GpgFrontend-bf538056b24a68b8fd235b1c50991ee8eb46a776.zip
refactor: use QString instead of std::string and improve threading system
Diffstat (limited to 'src/ui/widgets/KeyList.cpp')
-rw-r--r--src/ui/widgets/KeyList.cpp142
1 files changed, 67 insertions, 75 deletions
diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp
index 135a3c6d..0f97a2c9 100644
--- a/src/ui/widgets/KeyList.cpp
+++ b/src/ui/widgets/KeyList.cpp
@@ -28,7 +28,6 @@
#include "ui/widgets/KeyList.h"
-#include <boost/format.hpp>
#include <cstddef>
#include <mutex>
#include <utility>
@@ -36,11 +35,9 @@
#include "core/GpgCoreInit.h"
#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgKeyGetter.h"
-#include "core/model/GpgImportInformation.h"
#include "ui/UISignalStation.h"
#include "ui/UserInterfaceUtils.h"
#include "ui_KeyList.h"
-#include "widgets/TextEdit.h"
namespace GpgFrontend::UI {
@@ -54,7 +51,7 @@ KeyList::KeyList(KeyMenuAbility::AbilityType menu_ability, QWidget* parent)
void KeyList::init() {
ui_->setupUi(this);
- ui_->menuWidget->setHidden(!menu_ability_);
+ ui_->menuWidget->setHidden(menu_ability_ == 0U);
ui_->refreshKeyListButton->setHidden(~menu_ability_ &
KeyMenuAbility::REFRESH);
ui_->syncButton->setHidden(~menu_ability_ & KeyMenuAbility::SYNC_PUBLIC_KEY);
@@ -119,7 +116,7 @@ void KeyList::AddListGroupTab(const QString& name, const QString& id,
const KeyTable::KeyTableFilter filter) {
GF_UI_LOG_DEBUG("add tab: {}", name.toStdString());
- auto key_list = new QTableWidget(this);
+ auto* key_list = new QTableWidget(this);
if (m_key_list_ == nullptr) {
m_key_list_ = key_list;
}
@@ -146,22 +143,22 @@ void KeyList::AddListGroupTab(const QString& name, const QString& id,
key_list->setAlternatingRowColors(true);
// Hidden Column For Purpose
- if (!(infoType & KeyListColumn::TYPE)) {
+ if ((infoType & KeyListColumn::TYPE) == 0U) {
key_list->setColumnHidden(1, true);
}
- if (!(infoType & KeyListColumn::NAME)) {
+ if ((infoType & KeyListColumn::NAME) == 0U) {
key_list->setColumnHidden(2, true);
}
- if (!(infoType & KeyListColumn::EmailAddress)) {
+ if ((infoType & KeyListColumn::EmailAddress) == 0U) {
key_list->setColumnHidden(3, true);
}
- if (!(infoType & KeyListColumn::Usage)) {
+ if ((infoType & KeyListColumn::Usage) == 0U) {
key_list->setColumnHidden(4, true);
}
- if (!(infoType & KeyListColumn::Validity)) {
+ if ((infoType & KeyListColumn::Validity) == 0U) {
key_list->setColumnHidden(5, true);
}
- if (!(infoType & KeyListColumn::FingerPrint)) {
+ if ((infoType & KeyListColumn::FingerPrint) == 0U) {
key_list->setColumnHidden(6, true);
}
@@ -203,7 +200,7 @@ KeyIdArgsListPtr KeyList::GetChecked(const KeyTable& key_table) {
}
KeyIdArgsListPtr KeyList::GetChecked() {
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
const auto& buffered_keys =
m_key_tables_[ui_->keyGroupTab->currentIndex()].buffered_keys_;
@@ -217,13 +214,13 @@ KeyIdArgsListPtr KeyList::GetChecked() {
}
KeyIdArgsListPtr KeyList::GetAllPrivateKeys() {
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
const auto& buffered_keys =
m_key_tables_[ui_->keyGroupTab->currentIndex()].buffered_keys_;
auto ret = std::make_unique<KeyIdArgsList>();
for (int i = 0; i < key_list->rowCount(); i++) {
- if (key_list->item(i, 1) && buffered_keys[i].IsPrivateKey()) {
+ if ((key_list->item(i, 1) != nullptr) && buffered_keys[i].IsPrivateKey()) {
ret->push_back(buffered_keys[i].GetId());
}
}
@@ -234,14 +231,14 @@ KeyIdArgsListPtr KeyList::GetPrivateChecked() {
auto ret = std::make_unique<KeyIdArgsList>();
if (ui_->keyGroupTab->size().isEmpty()) return ret;
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
const auto& buffered_keys =
m_key_tables_[ui_->keyGroupTab->currentIndex()].buffered_keys_;
for (int i = 0; i < key_list->rowCount(); i++) {
if ((key_list->item(i, 0)->checkState() == Qt::Checked) &&
- (key_list->item(i, 1))) {
+ ((key_list->item(i, 1)) != nullptr)) {
ret->push_back(buffered_keys[i].GetId());
}
}
@@ -261,7 +258,7 @@ void KeyList::SetChecked(const KeyIdArgsListPtr& keyIds,
}
void KeyList::SetChecked(KeyIdArgsListPtr key_ids) {
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
if (key_list == nullptr) return;
if (!m_key_tables_.empty()) {
@@ -278,13 +275,13 @@ KeyIdArgsListPtr KeyList::GetSelected() {
auto ret = std::make_unique<KeyIdArgsList>();
if (ui_->keyGroupTab->size().isEmpty()) return ret;
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
const auto& buffered_keys =
m_key_tables_[ui_->keyGroupTab->currentIndex()].buffered_keys_;
for (int i = 0; i < key_list->rowCount(); i++) {
- if (key_list->item(i, 0)->isSelected() == 1) {
+ if (key_list->item(i, 0)->isSelected()) {
ret->push_back(buffered_keys[i].GetId());
}
}
@@ -296,7 +293,7 @@ KeyIdArgsListPtr KeyList::GetSelected() {
m_key_list_ = qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
for (int i = 0; i < m_key_list_->rowCount(); i++) {
- if (m_key_list_->item(i, 1)) {
+ if (m_key_list_->item(i, 1) != nullptr) {
return true;
}
}
@@ -359,22 +356,22 @@ void KeyList::dropEvent(QDropEvent* event) {
"\n");
// "always import keys"-CheckBox
- auto* checkBox = new QCheckBox(_("Always import without bothering."));
+ auto* check_box = new QCheckBox(_("Always import without bothering."));
bool confirm_import_keys = GlobalSettingStation::GetInstance().LookupSettings(
"general.confirm_import_keys", true);
- if (confirm_import_keys) checkBox->setCheckState(Qt::Checked);
+ if (confirm_import_keys) check_box->setCheckState(Qt::Checked);
// Buttons for ok and cancel
- auto* buttonBox =
+ auto* button_box =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
- connect(buttonBox, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
- connect(buttonBox, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
+ connect(button_box, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
+ connect(button_box, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
auto* vbox = new QVBoxLayout();
vbox->addWidget(label);
- vbox->addWidget(checkBox);
- vbox->addWidget(buttonBox);
+ vbox->addWidget(check_box);
+ vbox->addWidget(button_box);
dialog->setLayout(vbox);
@@ -385,14 +382,15 @@ void KeyList::dropEvent(QDropEvent* event) {
auto& settings = GlobalSettingStation::GetInstance().GetMainSettings();
if (!settings.exists("general") ||
- settings.lookup("general").getType() != libconfig::Setting::TypeGroup)
+ settings.lookup("general").getType() != libconfig::Setting::TypeGroup) {
settings.add("general", libconfig::Setting::TypeGroup);
+ }
auto& general = settings["general"];
- if (!general.exists("confirm_import_keys"))
+ if (!general.exists("confirm_import_keys")) {
general.add("confirm_import_keys", libconfig::Setting::TypeBoolean) =
- checkBox->isChecked();
- else {
- general["confirm_import_keys"] = checkBox->isChecked();
+ check_box->isChecked();
+ } else {
+ general["confirm_import_keys"] = check_box->isChecked();
}
GlobalSettingStation::GetInstance().SyncSettings();
}
@@ -404,13 +402,13 @@ void KeyList::dropEvent(QDropEvent* event) {
if (!file.open(QIODevice::ReadOnly)) {
GF_UI_LOG_ERROR("couldn't open file: {}", tmp.toString().toStdString());
}
- QByteArray inBuffer = file.readAll();
- this->import_keys(inBuffer);
+ QByteArray in_buffer = file.readAll();
+ this->import_keys(in_buffer);
file.close();
}
} else {
- QByteArray inBuffer(event->mimeData()->text().toUtf8());
- this->import_keys(inBuffer);
+ QByteArray in_buffer(event->mimeData()->text().toUtf8());
+ this->import_keys(in_buffer);
}
}
@@ -440,13 +438,13 @@ void KeyList::SetDoubleClickedAction(
this->m_action_ = std::move(action);
}
-std::string KeyList::GetSelectedKey() {
+QString KeyList::GetSelectedKey() {
if (ui_->keyGroupTab->size().isEmpty()) return {};
const auto& buffered_keys =
m_key_tables_[ui_->keyGroupTab->currentIndex()].buffered_keys_;
for (int i = 0; i < m_key_list_->rowCount(); i++) {
- if (m_key_list_->item(i, 0)->isSelected() == 1) {
+ if (m_key_list_->item(i, 0)->isSelected()) {
return buffered_keys[i].GetId();
}
}
@@ -473,8 +471,9 @@ void KeyList::slot_sync_with_key_server() {
{
std::lock_guard<std::mutex> guard(buffered_key_list_mutex_);
for (const auto& key : *buffered_keys_list_) {
- if (!(key.IsPrivateKey() && key.IsHasMasterKey()))
+ if (!(key.IsPrivateKey() && key.IsHasMasterKey())) {
key_ids.push_back(key.GetId());
+ }
}
}
@@ -485,16 +484,18 @@ void KeyList::slot_sync_with_key_server() {
emit SignalRefreshStatusBar(_("Syncing Key List..."), 3000);
CommonUtils::SlotImportKeyFromKeyServer(
- key_ids, [=](const std::string& key_id, const std::string& status,
+ key_ids, [=](const QString& key_id, const QString& status,
size_t current_index, size_t all_index) {
GF_UI_LOG_DEBUG("import key: {} {} {} {}", key_id, status,
current_index, all_index);
auto key = GpgKeyGetter::GetInstance().GetKey(key_id);
- boost::format status_str = boost::format(_("Sync [%1%/%2%] %3% %4%")) %
- current_index % all_index %
- key.GetUIDs()->front().GetUID() % status;
- emit SignalRefreshStatusBar(status_str.str().c_str(), 1500);
+ auto status_str = QString(_("Sync [%1/%2] %3 %4"))
+ .arg(current_index)
+ .arg(all_index)
+ .arg(key.GetUIDs()->front().GetUID())
+ .arg(status);
+ emit SignalRefreshStatusBar(status_str, 1500);
if (current_index == all_index) {
ui_->syncButton->setDisabled(false);
@@ -509,10 +510,10 @@ void KeyList::filter_by_keyword() {
auto keyword = ui_->searchBarEdit->text();
keyword = keyword.trimmed();
- GF_UI_LOG_DEBUG("get new keyword of search bar: {}", keyword.toStdString());
+ GF_UI_LOG_DEBUG("get new keyword of search bar: {}", keyword);
for (auto& table : m_key_tables_) {
// refresh arguments
- table.SetFilterKeyword(keyword.toLower().toStdString());
+ table.SetFilterKeyword(keyword.toLower());
table.SetMenuAbility(menu_ability_);
}
// refresh ui
@@ -520,7 +521,7 @@ void KeyList::filter_by_keyword() {
}
void KeyList::uncheck_all() {
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
if (key_list == nullptr) return;
if (!m_key_tables_.empty()) {
@@ -534,7 +535,7 @@ void KeyList::uncheck_all() {
}
void KeyList::check_all() {
- auto key_list =
+ auto* key_list =
qobject_cast<QTableWidget*>(ui_->keyGroupTab->currentWidget());
if (key_list == nullptr) return;
if (!m_key_tables_.empty()) {
@@ -548,8 +549,9 @@ void KeyList::check_all() {
}
KeyIdArgsListPtr& KeyTable::GetChecked() {
- if (checked_key_ids_ == nullptr)
+ if (checked_key_ids_ == nullptr) {
checked_key_ids_ = std::make_unique<KeyIdArgsList>();
+ }
auto& ret = checked_key_ids_;
for (size_t i = 0; i < buffered_keys_.size(); i++) {
auto key_id = buffered_keys_[i].GetId();
@@ -574,32 +576,24 @@ void KeyTable::Refresh(KeyLinkListPtr m_keys) {
// Optimization for copy
KeyLinkListPtr keys = nullptr;
- if (m_keys == nullptr)
+ if (m_keys == nullptr) {
keys = GpgKeyGetter::GetInstance().FetchKey();
- else
+ } else {
keys = std::move(m_keys);
+ }
auto it = keys->begin();
int row_count = 0;
while (it != keys->end()) {
// filter by search bar's keyword
- if (ability_ & KeyMenuAbility::SEARCH_BAR && !keyword_.empty()) {
- auto name = it->GetName();
- std::transform(name.begin(), name.end(), name.begin(),
- [](unsigned char c) { return std::tolower(c); });
-
- auto email = it->GetEmail();
- std::transform(email.begin(), email.end(), email.begin(),
- [](unsigned char c) { return std::tolower(c); });
-
- auto comment = it->GetComment();
- std::transform(comment.begin(), comment.end(), comment.begin(),
- [](unsigned char c) { return std::tolower(c); });
-
- if (name.find(keyword_) == std::string::npos &&
- email.find(keyword_) == std::string::npos &&
- comment.find(keyword_) == std::string::npos) {
+ if (ability_ & KeyMenuAbility::SEARCH_BAR && !keyword_.isEmpty()) {
+ auto name = it->GetName().toLower();
+ auto email = it->GetEmail().toLower();
+ auto comment = it->GetComment().toLower();
+
+ if (!name.contains(keyword_) && !email.contains(keyword_) &&
+ !comment.contains(keyword_)) {
it = keys->erase(it);
continue;
}
@@ -654,9 +648,9 @@ void KeyTable::Refresh(KeyLinkListPtr m_keys) {
auto* tmp1 = new QTableWidgetItem(type_str);
key_list_->setItem(row_index, 1, tmp1);
- auto* tmp2 = new QTableWidgetItem(QString::fromStdString(it->GetName()));
+ auto* tmp2 = new QTableWidgetItem(it->GetName());
key_list_->setItem(row_index, 2, tmp2);
- auto* tmp3 = new QTableWidgetItem(QString::fromStdString(it->GetEmail()));
+ auto* tmp3 = new QTableWidgetItem(it->GetEmail());
key_list_->setItem(row_index, 3, tmp3);
QString usage;
@@ -671,13 +665,11 @@ void KeyTable::Refresh(KeyLinkListPtr m_keys) {
temp_usage->setTextAlignment(Qt::AlignCenter);
key_list_->setItem(row_index, 4, temp_usage);
- auto* temp_validity =
- new QTableWidgetItem(QString::fromStdString(it->GetOwnerTrust()));
+ auto* temp_validity = new QTableWidgetItem(it->GetOwnerTrust());
temp_validity->setTextAlignment(Qt::AlignCenter);
key_list_->setItem(row_index, 5, temp_validity);
- auto* temp_fpr =
- new QTableWidgetItem(QString::fromStdString(it->GetFingerprint()));
+ auto* temp_fpr = new QTableWidgetItem(it->GetFingerprint());
temp_fpr->setTextAlignment(Qt::AlignCenter);
key_list_->setItem(row_index, 6, temp_fpr);
@@ -727,7 +719,7 @@ void KeyTable::SetMenuAbility(KeyMenuAbility::AbilityType ability) {
this->ability_ = ability;
}
-void KeyTable::SetFilterKeyword(std::string keyword) {
- this->keyword_ = keyword;
+void KeyTable::SetFilterKeyword(QString keyword) {
+ this->keyword_ = std::move(keyword);
}
} // namespace GpgFrontend::UI