Skip to content

Commit

Permalink
Merge pull request #1915 from ModOrganizer2/download_issues
Browse files Browse the repository at this point in the history
Download issue fixes
  • Loading branch information
Silarn authored Nov 5, 2023
2 parents 8f77fff + c48b7d8 commit 8c562fe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
48 changes: 22 additions & 26 deletions src/downloadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ DownloadManager::DownloadInfo::createFromMeta(const QString& filePath, bool show
return info;
}

ScopedDisableDirWatcher::ScopedDisableDirWatcher(DownloadManager* downloadManager)
{
m_downloadManager = downloadManager;
m_downloadManager->startDisableDirWatcher();
log::debug("Scoped Disable DirWatcher: Started");
}

ScopedDisableDirWatcher::~ScopedDisableDirWatcher()
{
m_downloadManager->endDisableDirWatcher();
m_downloadManager = nullptr;
log::debug("Scoped Disable DirWatcher: Stopped");
}

void DownloadManager::startDisableDirWatcher()
{
DownloadManager::m_DirWatcherDisabler++;
Expand Down Expand Up @@ -319,7 +333,7 @@ void DownloadManager::refreshList()

try {
// avoid triggering other refreshes
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

int downloadsBefore = m_ActiveDownloads.size();

Expand Down Expand Up @@ -421,9 +435,6 @@ void DownloadManager::refreshList()

emit update(-1);

// let watcher trigger refreshes again
endDisableDirWatcher();

} catch (const std::bad_alloc&) {
reportError(tr("Memory allocation error (in refreshing directory)."));
}
Expand Down Expand Up @@ -758,7 +769,7 @@ void DownloadManager::addNXMDownload(const QString& url)
void DownloadManager::removeFile(int index, bool deleteFile)
{
// Avoid triggering refreshes from DirWatcher
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

if (index >= m_ActiveDownloads.size()) {
throw MyException(tr("remove: invalid download index %1").arg(index));
Expand All @@ -770,7 +781,6 @@ void DownloadManager::removeFile(int index, bool deleteFile)
(download->m_State == STATE_DOWNLOADING)) {
// shouldn't have been possible
log::error("tried to remove active download");
endDisableDirWatcher();
return;
}

Expand All @@ -781,7 +791,6 @@ void DownloadManager::removeFile(int index, bool deleteFile)
if (deleteFile) {
if (!shellDelete(QStringList(filePath), true)) {
reportError(tr("failed to delete %1").arg(filePath));
endDisableDirWatcher();
return;
}

Expand All @@ -795,8 +804,6 @@ void DownloadManager::removeFile(int index, bool deleteFile)
metaSettings.setValue("removed", true);
}
m_DownloadRemoved(index);

endDisableDirWatcher();
}

class LessThanWrapper
Expand Down Expand Up @@ -871,7 +878,7 @@ void DownloadManager::removeDownload(int index, bool deleteFile)
{
try {
// avoid dirWatcher triggering refreshes
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

emit aboutToUpdate();

Expand All @@ -897,7 +904,6 @@ void DownloadManager::removeDownload(int index, bool deleteFile)
if (index >= m_ActiveDownloads.size()) {
reportError(tr("remove: invalid download index %1").arg(index));
// emit update(-1);
endDisableDirWatcher();
return;
}

Expand All @@ -906,7 +912,6 @@ void DownloadManager::removeDownload(int index, bool deleteFile)
m_ActiveDownloads.erase(m_ActiveDownloads.begin() + index);
}
emit update(-1);
endDisableDirWatcher();
} catch (const std::exception& e) {
log::error("failed to remove download: {}", e.what());
}
Expand Down Expand Up @@ -1434,15 +1439,13 @@ void DownloadManager::markInstalled(int index)
}

// Avoid triggering refreshes from DirWatcher
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

DownloadInfo* info = m_ActiveDownloads.at(index);
QSettings metaFile(info->m_Output.fileName() + ".meta", QSettings::IniFormat);
metaFile.setValue("installed", true);
metaFile.setValue("uninstalled", false);

endDisableDirWatcher();

setState(m_ActiveDownloads.at(index), STATE_INSTALLED);
}

Expand All @@ -1455,14 +1458,12 @@ void DownloadManager::markInstalled(QString fileName)
DownloadInfo* info = getDownloadInfo(fileName);
if (info != nullptr) {
// Avoid triggering refreshes from DirWatcher
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

QSettings metaFile(info->m_Output.fileName() + ".meta", QSettings::IniFormat);
metaFile.setValue("installed", true);
metaFile.setValue("uninstalled", false);
delete info;

endDisableDirWatcher();
}
}
}
Expand All @@ -1479,14 +1480,12 @@ void DownloadManager::markUninstalled(int index)
}

// Avoid triggering refreshes from DirWatcher
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

DownloadInfo* info = m_ActiveDownloads.at(index);
QSettings metaFile(info->m_Output.fileName() + ".meta", QSettings::IniFormat);
metaFile.setValue("uninstalled", true);

endDisableDirWatcher();

setState(m_ActiveDownloads.at(index), STATE_UNINSTALLED);
}

Expand All @@ -1501,13 +1500,11 @@ void DownloadManager::markUninstalled(QString fileName)
if (info != nullptr) {

// Avoid triggering refreshes from DirWatcher
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

QSettings metaFile(info->m_Output.fileName() + ".meta", QSettings::IniFormat);
metaFile.setValue("uninstalled", true);
delete info;

endDisableDirWatcher();
}
}
}
Expand Down Expand Up @@ -1671,7 +1668,7 @@ void DownloadManager::downloadReadyRead()
void DownloadManager::createMetaFile(DownloadInfo* info)
{
// Avoid triggering refreshes from DirWatcher
startDisableDirWatcher();
ScopedDisableDirWatcher scopedDirWatcher(this);

QSettings metaFile(QString("%1.meta").arg(info->m_Output.fileName()),
QSettings::IniFormat);
Expand All @@ -1695,7 +1692,6 @@ void DownloadManager::createMetaFile(DownloadInfo* info)
(info->m_State == DownloadManager::STATE_ERROR));
metaFile.setValue("removed", info->m_Hidden);

endDisableDirWatcher();
// slightly hackish...
for (int i = 0; i < m_ActiveDownloads.size(); ++i) {
if (m_ActiveDownloads[i] == info) {
Expand Down
10 changes: 10 additions & 0 deletions src/downloadmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,14 @@ private slots:
QTimer m_TimeoutTimer;
};

class ScopedDisableDirWatcher
{
public:
ScopedDisableDirWatcher(DownloadManager* downloadManager);
~ScopedDisableDirWatcher();

private:
DownloadManager* m_downloadManager;
};

#endif // DOWNLOADMANAGER_H
9 changes: 5 additions & 4 deletions src/multiprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ void MOMultiProcess::sendMessage(const QString& message)
}

socket.write(message.toUtf8());
if (socket.bytesToWrite() && !socket.waitForBytesWritten(s_Timeout)) {
reportError(
tr("failed to communicate with running process: %1").arg(socket.errorString()));
return;
if (!socket.waitForBytesWritten(s_Timeout)) {
if (socket.bytesToWrite()) {
reportError(tr("failed to communicate with running process: %1")
.arg(socket.errorString()));
}
}

socket.disconnectFromServer();
Expand Down
2 changes: 2 additions & 0 deletions src/organizercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ OrganizerCore::doInstall(const QString& archivePath, GuessedValue<QString> modNa

ModInfo::Ptr OrganizerCore::installDownload(int index, int priority)
{
ScopedDisableDirWatcher scopedDirwatcher(&m_DownloadManager);

try {
QString fileName = m_DownloadManager.getFilePath(index);
QString gameName = m_DownloadManager.getGameName(index);
Expand Down

0 comments on commit 8c562fe

Please sign in to comment.