Skip to content

Commit ce04232

Browse files
committed
feat(ui): periodically update harddisk menu
* Add periodic refresh for the harddisk selection menu using a timer * Track mounted volumes and log changes when devices are added or removed * Improve UI responsiveness to dynamic storage changes
1 parent 0dd8d3e commit ce04232

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/ui/widgets/FilePage.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
#include "ui/main_window/MainWindow.h"
3434
#include "ui_FilePage.h"
3535

36+
namespace {
37+
38+
auto VolumeKey(const QStorageInfo& s) -> QString {
39+
return QCryptographicHash::hash(
40+
(s.device() + "|" + s.rootPath() + "|" + s.displayName()).toUtf8(),
41+
QCryptographicHash::Sha1)
42+
.toHex();
43+
}
44+
45+
} // namespace
46+
3647
namespace GpgFrontend::UI {
3748

3849
FilePage::FilePage(QWidget* parent, const QString& target_path)
@@ -85,8 +96,6 @@ FilePage::FilePage(QWidget* parent, const QString& target_path)
8596
GetSettings().value("gnupg/non_ascii_at_file_operation", true).toBool());
8697
switch_asc_mode_act->setChecked(ascii_mode_);
8798

88-
update_harddisk_menu();
89-
9099
connect(ui_->pathEdit, &QLineEdit::textChanged, [=]() {
91100
auto path = ui_->pathEdit->text();
92101
auto dir = QDir(path);
@@ -122,6 +131,8 @@ FilePage::FilePage(QWidget* parent, const QString& target_path)
122131
&UISignalStation::SignalMainWindowUpdateBasicOperaMenu);
123132
connect(ui_->batchModeButton, &QToolButton::toggled, ui_->treeView,
124133
&FileTreeView::SlotSwitchBatchMode);
134+
135+
QTimer::singleShot(200, this, &FilePage::update_harddisk_menu_periodic);
125136
}
126137

127138
auto FilePage::GetSelected() const -> QStringList {
@@ -215,14 +226,38 @@ auto FilePage::IsBatchMode() const -> bool {
215226
auto FilePage::IsASCIIMode() const -> bool { return ascii_mode_; }
216227

217228
auto FilePage::update_harddisk_menu() -> void {
229+
const auto vols = QStorageInfo::mountedVolumes();
230+
231+
QSet<QString> keys;
232+
keys.reserve(vols.size());
233+
for (const auto& s : vols) {
234+
if (!s.isValid() || !s.isReady()) continue;
235+
const auto key = VolumeKey(s);
236+
keys.insert(key);
237+
}
238+
239+
if (keys == last_volume_keys_) return;
240+
241+
const QSet<QString> added = keys - last_volume_keys_;
242+
const QSet<QString> removed = last_volume_keys_ - keys;
243+
244+
if (!added.isEmpty() || !removed.isEmpty()) {
245+
for (const auto& k : added) LOG_D() << "mounted: " << k;
246+
for (const auto& k : removed) LOG_D() << "unmounted: " << k;
247+
}
248+
249+
last_volume_keys_ = std::move(keys);
250+
251+
LOG_D() << "updating harddisk menu...";
252+
218253
if (harddisk_popup_menu_ != nullptr) {
219254
harddisk_popup_menu_->deleteLater();
220255
harddisk_popup_menu_ = nullptr;
221256
}
222257

223258
harddisk_popup_menu_ = new QMenu(this);
224259

225-
for (const auto& storage_device : QStorageInfo::mountedVolumes()) {
260+
for (const auto& storage_device : vols) {
226261
LOG_D() << "found storage device: " << storage_device.rootPath() << " "
227262
<< storage_device.displayName() << " " << storage_device.isRoot();
228263

@@ -239,4 +274,9 @@ auto FilePage::update_harddisk_menu() -> void {
239274
ui_->hardDiskButton->setMenu(harddisk_popup_menu_);
240275
}
241276

277+
auto FilePage::update_harddisk_menu_periodic() -> void {
278+
update_harddisk_menu();
279+
QTimer::singleShot(3000, this, &FilePage::update_harddisk_menu_periodic);
280+
}
281+
242282
} // namespace GpgFrontend::UI

src/ui/widgets/FilePage.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class FilePage : public QWidget {
133133
QMenu* harddisk_popup_menu_{}; ///<
134134
bool ascii_mode_;
135135

136+
QSet<QString> last_volume_keys_; ///<
137+
136138
private slots:
137139

138140
/**
@@ -141,7 +143,17 @@ class FilePage : public QWidget {
141143
*/
142144
void update_main_basic_opera_menu(const QStringList&);
143145

146+
/**
147+
* @brief
148+
*
149+
*/
144150
void update_harddisk_menu();
151+
152+
/**
153+
* @brief
154+
*
155+
*/
156+
void update_harddisk_menu_periodic();
145157
};
146158

147159
} // namespace GpgFrontend::UI

0 commit comments

Comments
 (0)