|
20 | 20 | #include <cstdio> |
21 | 21 |
|
22 | 22 | namespace OCC { |
| 23 | +namespace { |
| 24 | +void raiseUpdaterDialog(QWidget *widget); |
| 25 | + |
| 26 | +QWidget *updaterDialogParent() |
| 27 | +{ |
| 28 | + if (auto *active = QApplication::activeWindow()) { |
| 29 | + return active; |
| 30 | + } |
| 31 | + |
| 32 | + if (auto *focus = QApplication::focusWidget()) { |
| 33 | + return focus->window(); |
| 34 | + } |
| 35 | + |
| 36 | + return nullptr; |
| 37 | +} |
| 38 | + |
| 39 | +bool attachAndOpenUpdaterDialog(QDialog *dialog, const bool removeContextHelpButton, const bool requireParent) |
| 40 | +{ |
| 41 | + if (!dialog || dialog->isVisible()) { |
| 42 | + return dialog != nullptr; |
| 43 | + } |
| 44 | + |
| 45 | + auto *parent = updaterDialogParent(); |
| 46 | + if (requireParent && !parent) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + if (parent) { |
| 51 | + dialog->setParent(parent, dialog->windowFlags()); |
| 52 | + dialog->setWindowModality(Qt::WindowModal); |
| 53 | + } else { |
| 54 | + dialog->setWindowModality(Qt::ApplicationModal); |
| 55 | + } |
| 56 | + |
| 57 | + auto flags = dialog->windowFlags() | Qt::WindowStaysOnTopHint; |
| 58 | + if (removeContextHelpButton) { |
| 59 | + flags &= ~Qt::WindowContextHelpButtonHint; |
| 60 | + } |
| 61 | + dialog->setWindowFlags(flags); |
| 62 | + dialog->open(); |
| 63 | + raiseUpdaterDialog(dialog); |
| 64 | + return true; |
| 65 | +} |
| 66 | + |
| 67 | +void presentUpdaterDialog(QDialog *dialog, const bool removeContextHelpButton) |
| 68 | +{ |
| 69 | + if (!dialog) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (attachAndOpenUpdaterDialog(dialog, removeContextHelpButton, true)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + auto *timer = new QTimer(dialog); |
| 78 | + timer->setInterval(150); |
| 79 | + timer->setSingleShot(false); |
| 80 | + QObject::connect(timer, &QTimer::timeout, dialog, [dialog, removeContextHelpButton, timer]() { |
| 81 | + if (!dialog) { |
| 82 | + timer->stop(); |
| 83 | + timer->deleteLater(); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (attachAndOpenUpdaterDialog(dialog, removeContextHelpButton, true)) { |
| 88 | + timer->stop(); |
| 89 | + timer->deleteLater(); |
| 90 | + } |
| 91 | + }); |
| 92 | + timer->start(); |
| 93 | + |
| 94 | + QTimer::singleShot(2000, dialog, [dialog, removeContextHelpButton, timer]() { |
| 95 | + if (!dialog || dialog->isVisible()) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + attachAndOpenUpdaterDialog(dialog, removeContextHelpButton, false); |
| 100 | + if (timer) { |
| 101 | + timer->stop(); |
| 102 | + timer->deleteLater(); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + QObject::connect(qApp, &QApplication::focusChanged, dialog, [dialog]() { |
| 107 | + if (!dialog || !dialog->isVisible()) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + raiseUpdaterDialog(dialog); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +void raiseUpdaterDialog(QWidget *widget) |
| 116 | +{ |
| 117 | + if (!widget) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + widget->showNormal(); |
| 122 | + widget->raise(); |
| 123 | + widget->activateWindow(); |
| 124 | +} |
| 125 | +} // namespace |
23 | 126 |
|
24 | 127 | UpdaterScheduler::UpdaterScheduler(QObject *parent) |
25 | 128 | : QObject(parent) |
@@ -104,7 +207,7 @@ bool OCUpdater::performUpdate() |
104 | 207 | slotStartInstaller(); |
105 | 208 | } |
106 | 209 | }); |
107 | | - messageBoxStartInstaller->open(); |
| 210 | + presentUpdaterDialog(messageBoxStartInstaller, false); |
108 | 211 | } |
109 | 212 | return false; |
110 | 213 | } |
@@ -359,7 +462,6 @@ void NSISUpdater::showNoUrlDialog(const UpdateInfo &info) |
359 | 462 | // if the version tag is set, there is a newer version. |
360 | 463 | auto *msgBox = new QDialog; |
361 | 464 | msgBox->setAttribute(Qt::WA_DeleteOnClose); |
362 | | - msgBox->setWindowFlags(msgBox->windowFlags() & ~Qt::WindowContextHelpButtonHint); |
363 | 465 |
|
364 | 466 | QIcon infoIcon = msgBox->style()->standardIcon(QStyle::SP_MessageBoxInformation); |
365 | 467 | int iconSize = msgBox->style()->pixelMetric(QStyle::PM_MessageBoxIconSize); |
@@ -399,14 +501,13 @@ void NSISUpdater::showNoUrlDialog(const UpdateInfo &info) |
399 | 501 |
|
400 | 502 | layout->addWidget(bb); |
401 | 503 |
|
402 | | - msgBox->open(); |
| 504 | + presentUpdaterDialog(msgBox, true); |
403 | 505 | } |
404 | 506 |
|
405 | 507 | void NSISUpdater::showUpdateErrorDialog(const QString &targetVersion) |
406 | 508 | { |
407 | 509 | auto msgBox = new QDialog; |
408 | 510 | msgBox->setAttribute(Qt::WA_DeleteOnClose); |
409 | | - msgBox->setWindowFlags(msgBox->windowFlags() & ~Qt::WindowContextHelpButtonHint); |
410 | 511 |
|
411 | 512 | QIcon infoIcon = msgBox->style()->standardIcon(QStyle::SP_MessageBoxInformation); |
412 | 513 | int iconSize = msgBox->style()->pixelMetric(QStyle::PM_MessageBoxIconSize); |
@@ -454,7 +555,7 @@ void NSISUpdater::showUpdateErrorDialog(const QString &targetVersion) |
454 | 555 |
|
455 | 556 | layout->addWidget(bb); |
456 | 557 |
|
457 | | - msgBox->open(); |
| 558 | + presentUpdaterDialog(msgBox, true); |
458 | 559 | } |
459 | 560 |
|
460 | 561 | bool NSISUpdater::handleStartup() |
|
0 commit comments