Skip to content

Commit e833399

Browse files
saturnericCopilot
andcommitted
feat(ui): add type-aware algo selection helpers
* Adds type-aware algorithm lookup helpers to avoid ambiguous matches when multiple algorithms share a name * Introduces a combo box population utility that sorts entries, keeps `none` first, and de-duplicates labels for a cleaner selection UI * Provides id+type lookup with id-only fallback when type is empty to preserve backward compatibility Co-authored-by: Copilot <copilot@github.com>
1 parent 767ff52 commit e833399

2 files changed

Lines changed: 103 additions & 9 deletions

File tree

src/ui/function/KeyGenerateHelper.cpp

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,40 @@ auto GetAlgoById(const QString& id, const QContainer<KeyAlgo>& algos)
4040
return {};
4141
}
4242

43-
auto SearchAlgoByName(const QString& name, const QContainer<KeyAlgo>& algos)
43+
auto SearchAlgoByNameType(const QString& name, const QString& type,
44+
const QContainer<KeyAlgo>& algos)
4445
-> QContainer<KeyAlgo> {
4546
QContainer<KeyAlgo> res;
4647

4748
for (const auto& algo : algos) {
4849
if (algo.Name() != name) continue;
50+
if (algo.Type() != type) continue;
4951
res.append(algo);
5052
}
5153

5254
return res;
5355
}
5456

55-
auto GetAlgoByNameAndKeyLength(const QString& name, int key_length,
56-
const QContainer<KeyAlgo>& algos)
57+
auto GetAlgoByNameTypeAndKeyLength(const QString& name, const QString& type,
58+
int key_length,
59+
const QContainer<KeyAlgo>& algos)
5760
-> std::tuple<bool, KeyAlgo> {
5861
for (const auto& algo : algos) {
5962
if (algo.Name() != name) continue;
63+
if (algo.Type() != type) continue;
6064
if (algo.KeyLength() != key_length) continue;
6165
return {true, algo};
6266
}
6367

6468
return {};
6569
}
6670

67-
auto GetAlgoByName(const QString& name, const QContainer<KeyAlgo>& algos)
71+
auto GetAlgoByNameType(const QString& name, const QString& type,
72+
const QContainer<KeyAlgo>& algos)
6873
-> std::tuple<bool, KeyAlgo> {
6974
for (const auto& algo : algos) {
7075
if (algo.Name() != name) continue;
76+
if (algo.Type() != type) continue;
7177
return {true, algo};
7278
}
7379

@@ -91,4 +97,63 @@ void SetKeyLengthComboxBoxByAlgo(QComboBox* combo,
9197

9298
combo->addItems(key_lengths);
9399
}
100+
101+
void PopulateAlgoComboBox(QComboBox* combo_box,
102+
const QContainer<KeyAlgo>& algos) {
103+
if (combo_box == nullptr) return;
104+
105+
auto sorted_algos = algos;
106+
107+
std::sort(sorted_algos.begin(), sorted_algos.end(),
108+
[](const KeyAlgo& a, const KeyAlgo& b) -> bool {
109+
if (a.Id() == "none" && b.Id() != "none") return true;
110+
if (b.Id() == "none" && a.Id() != "none") return false;
111+
112+
if (a.Name() != b.Name()) return a.Name() < b.Name();
113+
if (a.Type() != b.Type()) return a.Type() < b.Type();
114+
if (a.KeyLength() != b.KeyLength()) {
115+
return a.KeyLength() < b.KeyLength();
116+
}
117+
return a.Id() < b.Id();
118+
});
119+
120+
combo_box->clear();
121+
122+
QString last_label;
123+
QString last_type;
124+
125+
for (const auto& algo : sorted_algos) {
126+
QString label;
127+
128+
if (algo.Id() == "none") {
129+
label = algo.Name();
130+
} else {
131+
label = QString("%1 (%2)").arg(algo.Name(), algo.Type());
132+
}
133+
134+
if (label == last_label && algo.Type() == last_type) continue;
135+
last_label = label;
136+
last_type = algo.Type();
137+
138+
QVariantMap data;
139+
data["id"] = algo.Id();
140+
data["name"] = algo.Name();
141+
data["type"] = algo.Type();
142+
143+
combo_box->addItem(label, data);
144+
}
145+
}
146+
147+
auto GetAlgoByIdType(const QString& id, const QString& type,
148+
const QContainer<KeyAlgo>& algos)
149+
-> std::tuple<bool, KeyAlgo> {
150+
for (const auto& algo : algos) {
151+
if (algo.Id() != id) continue;
152+
if (!type.isEmpty() && algo.Type() != type) continue;
153+
return {true, algo};
154+
}
155+
156+
return {};
157+
}
158+
94159
} // namespace GpgFrontend::UI

src/ui/function/KeyGenerateHelper.h

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ auto GetAlgoById(const QString& id, const QContainer<KeyAlgo>& algos)
4343
-> std::tuple<bool, KeyAlgo>;
4444

4545
/**
46-
* @brief
46+
* @brief Search for algorithms by name and type
4747
*
4848
* @param name
49+
* @param type
4950
* @param algos
5051
* @return QContainer<KeyAlgo>
5152
*/
52-
auto SearchAlgoByName(const QString& name, const QContainer<KeyAlgo>& algos)
53+
auto SearchAlgoByNameType(const QString& name, const QString& type,
54+
const QContainer<KeyAlgo>& algos)
5355
-> QContainer<KeyAlgo>;
5456

5557
/**
@@ -60,8 +62,9 @@ auto SearchAlgoByName(const QString& name, const QContainer<KeyAlgo>& algos)
6062
* @param algos
6163
* @return std::tuple<bool, KeyAlgo>
6264
*/
63-
auto GetAlgoByNameAndKeyLength(const QString& name, int key_length,
64-
const QContainer<KeyAlgo>& algos)
65+
auto GetAlgoByNameTypeAndKeyLength(const QString& name, const QString& type,
66+
int key_length,
67+
const QContainer<KeyAlgo>& algos)
6568
-> std::tuple<bool, KeyAlgo>;
6669

6770
/**
@@ -71,7 +74,8 @@ auto GetAlgoByNameAndKeyLength(const QString& name, int key_length,
7174
* @param algos
7275
* @return std::tuple<bool, KeyAlgo>
7376
*/
74-
auto GetAlgoByName(const QString& name, const QContainer<KeyAlgo>& algos)
77+
auto GetAlgoByNameType(const QString& name, const QString& type,
78+
const QContainer<KeyAlgo>& algos)
7579
-> std::tuple<bool, KeyAlgo>;
7680

7781
/**
@@ -82,4 +86,29 @@ auto GetAlgoByName(const QString& name, const QContainer<KeyAlgo>& algos)
8286
*/
8387
void SetKeyLengthComboxBoxByAlgo(QComboBox* combo,
8488
const QContainer<KeyAlgo>& algos);
89+
90+
/**
91+
* @brief
92+
*
93+
* @param combo_box
94+
* @param algos
95+
*/
96+
void PopulateAlgoComboBox(QComboBox* combo_box,
97+
const QContainer<KeyAlgo>& algos);
98+
99+
/**
100+
* @brief Get the Algo By Id And Type object
101+
*
102+
* If type is empty, this function falls back to id-only matching for backward
103+
* compatibility.
104+
*
105+
* @param id
106+
* @param type
107+
* @param algos
108+
* @return std::tuple<bool, KeyAlgo>
109+
*/
110+
auto GetAlgoByIdType(const QString& id, const QString& type,
111+
const QContainer<KeyAlgo>& algos)
112+
-> std::tuple<bool, KeyAlgo>;
113+
85114
} // namespace GpgFrontend::UI

0 commit comments

Comments
 (0)