Skip to content

Commit 5d75a8d

Browse files
author
Paweł Salawa
committed
#5712 Fixed Tip of the Day display race condition.
1 parent 24b25c4 commit 5d75a8d

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

Letos/gui/mainwindow.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,14 @@ void MainWindow::saveSession(MdiWindow* currWindow)
550550

551551
void MainWindow::restoreSession()
552552
{
553-
auto cleanup = qScopeGuard([] { sessionRestoringFinished = true;});
553+
auto cleanup = qScopeGuard([this]
554+
{
555+
sessionRestoringFinished = true;
556+
if (statusField->hasMessages())
557+
statusField->setVisible(true);
558+
559+
emit sessionInitiallyRestored();
560+
});
554561

555562
if (safeModeEnabled)
556563
{
@@ -617,9 +624,6 @@ void MainWindow::restoreSession()
617624
}
618625
}
619626

620-
if (statusField->hasMessages())
621-
statusField->setVisible(true);
622-
623627
updateCornerDocking();
624628
updateWindowActions();
625629

Letos/gui/mainwindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class GUI_API_EXPORT MainWindow : public QMainWindow, public ExtActionContainer
356356

357357
signals:
358358
void sessionValueChanged();
359+
void sessionInitiallyRestored();
359360
};
360361

361362
template <class T, typename... Args>

Plugins/TipOfTheDay/tipofthedayplugin.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ bool TipOfTheDayPlugin::init()
2424
netManager = new QNetworkAccessManager(this);
2525

2626
connect(this, SIGNAL(tipsAvailable()), this, SLOT(showOnStartup()));
27+
connect(MAINWINDOW, SIGNAL(sessionInitiallyRestored()), this, SLOT(showOnStartup()));
2728
connect(STATUSFIELD, SIGNAL(linkActivated(const QString&)), this, SLOT(openTotdFromLink(const QString&)));
2829
fetchtTips();
2930

@@ -150,7 +151,7 @@ void TipOfTheDayPlugin::fetchtTips()
150151

151152
void TipOfTheDayPlugin::showOnStartup()
152153
{
153-
if (!cfg.Totd.ShowOnStartup.get() || tips.isEmpty())
154+
if (!cfg.Totd.ShowOnStartup.get() || tips.isEmpty() || !MAINWINDOW->isSessionRestoringFinished())
154155
return;
155156

156157
QStringList shownList = cfg.Totd.RecentlyShown.get();
@@ -163,17 +164,23 @@ void TipOfTheDayPlugin::showOnStartup()
163164
shownList.clear();
164165
}
165166

166-
// Prepare list of tips not shown yet and use random tip from that list,
167-
// then find what index is has in the tips list and use it
168-
QList<Tip> notShownList;
169-
for (const Tip& tip : tips)
167+
// For first run the first tip should be shown (it's just good for the 1st tip).
168+
// Then randomize the rest.
169+
if (!shownList.isEmpty())
170170
{
171-
if (!shownList.contains(tip.summary))
172-
notShownList << tip;
171+
// Prepare list of tips not shown yet and use random tip from that list,
172+
// then find what index is has in the tips list and use it
173+
QList<Tip> notShownList;
174+
for (const Tip& tip : tips)
175+
{
176+
if (!shownList.contains(tip.summary))
177+
notShownList << tip;
178+
}
179+
180+
quint32 randInt = QRandomGenerator::securelySeeded().bounded(notShownList.size());
181+
Tip tipToUse = notShownList[randInt];
182+
idx = tips.indexOf(tipToUse);
173183
}
174-
QRandomGenerator randGen;
175-
Tip tipToUse = notShownList[randGen.generate() % notShownList.size()];
176-
idx = tips.indexOf(tipToUse);
177184
}
178185

179186
notifyInfo(tr("<b>Quick tip:</b> %1 - <a href=\"%2\">Learn more</a>").arg(tips[idx].summary, openTotdUrl + QString::number(idx)));
@@ -197,6 +204,5 @@ void TipOfTheDayPlugin::markAsRead(const QString& summary)
197204
{
198205
shownList << summary;
199206
cfg.Totd.RecentlyShown.set(shownList);
200-
qDebug() << "marked, total" << shownList.size();
201207
}
202208
}

0 commit comments

Comments
 (0)