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+
3647namespace GpgFrontend ::UI {
3748
3849FilePage::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
127138auto FilePage::GetSelected () const -> QStringList {
@@ -215,14 +226,38 @@ auto FilePage::IsBatchMode() const -> bool {
215226auto FilePage::IsASCIIMode () const -> bool { return ascii_mode_; }
216227
217228auto 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
0 commit comments