|
37 | 37 | #include <QJsonObject> |
38 | 38 | #include <QString> |
39 | 39 | #include <QStringList> |
| 40 | +#include <QTimer> |
40 | 41 |
|
41 | 42 | #include "base/global.h" |
42 | 43 | #include "base/logger.h" |
@@ -75,15 +76,29 @@ int SearchJobManager::startSearch(const QString &pattern, const QString &categor |
75 | 76 |
|
76 | 77 | connect(searchHandler.get(), &SearchHandler::searchFinished, this, [this, id] |
77 | 78 | { |
| 79 | + if (QTimer *timer = m_resultSaveTimers.take(id)) |
| 80 | + { |
| 81 | + timer->stop(); |
| 82 | + timer->deleteLater(); |
| 83 | + } |
78 | 84 | m_activeSearches.remove(id); |
79 | 85 | saveSession(); |
80 | 86 | saveSearchResults(id); |
81 | 87 | }); |
82 | 88 | connect(searchHandler.get(), &SearchHandler::searchFailed, this, [this, id]([[maybe_unused]] const QString &errorMessage) |
83 | 89 | { |
| 90 | + if (QTimer *timer = m_resultSaveTimers.take(id)) |
| 91 | + { |
| 92 | + timer->stop(); |
| 93 | + timer->deleteLater(); |
| 94 | + } |
84 | 95 | m_activeSearches.remove(id); |
85 | 96 | saveSession(); |
86 | 97 | }); |
| 98 | + connect(searchHandler.get(), &SearchHandler::newSearchResults, this, [this, id] |
| 99 | + { |
| 100 | + scheduleSaveResults(id); |
| 101 | + }); |
87 | 102 |
|
88 | 103 | m_searchHandlers.insert(id, searchHandler); |
89 | 104 | m_activeSearches.insert(id); |
@@ -131,6 +146,12 @@ bool SearchJobManager::deleteSearch(const int id) |
131 | 146 | if (!found) |
132 | 147 | return false; |
133 | 148 |
|
| 149 | + if (QTimer *timer = m_resultSaveTimers.take(id)) |
| 150 | + { |
| 151 | + timer->stop(); |
| 152 | + timer->deleteLater(); |
| 153 | + } |
| 154 | + |
134 | 155 | m_searchOrder.removeOne(id); |
135 | 156 | removeSearchResults(id); |
136 | 157 | saveSession(); |
@@ -325,6 +346,27 @@ void SearchJobManager::saveSession() const |
325 | 346 | } |
326 | 347 | } |
327 | 348 |
|
| 349 | +void SearchJobManager::scheduleSaveResults(const int searchId) |
| 350 | +{ |
| 351 | + if (!m_storeOpenedTabsResults) |
| 352 | + return; |
| 353 | + |
| 354 | + if (m_resultSaveTimers.contains(searchId)) |
| 355 | + return; // Timer already running, don't restart |
| 356 | + |
| 357 | + auto *timer = new QTimer(this); |
| 358 | + timer->setSingleShot(true); |
| 359 | + timer->setInterval(30000); // 30 seconds |
| 360 | + connect(timer, &QTimer::timeout, this, [this, searchId, timer] |
| 361 | + { |
| 362 | + m_resultSaveTimers.remove(searchId); |
| 363 | + timer->deleteLater(); |
| 364 | + saveSearchResults(searchId); |
| 365 | + }); |
| 366 | + m_resultSaveTimers.insert(searchId, timer); |
| 367 | + timer->start(); |
| 368 | +} |
| 369 | + |
328 | 370 | void SearchJobManager::saveSearchResults(const int searchId) const |
329 | 371 | { |
330 | 372 | const auto *pref = Preferences::instance(); |
|
0 commit comments