From 27981c0d21d7c1bb9346b87573849604aa58008a Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Wed, 5 Jul 2023 20:12:37 -0400 Subject: [PATCH] Fix broken download/remove/install. --- gpt4all-chat/download.cpp | 52 +++++++++++----------- gpt4all-chat/modellist.cpp | 28 ++++++++++++ gpt4all-chat/modellist.h | 2 + gpt4all-chat/qml/ModelDownloaderDialog.qml | 1 + 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/gpt4all-chat/download.cpp b/gpt4all-chat/download.cpp index dc8f81dc..dbcaf646 100644 --- a/gpt4all-chat/download.cpp +++ b/gpt4all-chat/download.cpp @@ -107,7 +107,7 @@ void Download::downloadModel(const QString &modelFile) const QString error = QString("ERROR: Could not open temp file: %1 %2").arg(tempFile->fileName()).arg(modelFile); qWarning() << error; - ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadErrorRole, error); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadErrorRole, error); return; } tempFile->flush(); @@ -121,13 +121,13 @@ void Download::downloadModel(const QString &modelFile) } } - if (!ModelList::globalInstance()->contains(modelFile)) { + if (!ModelList::globalInstance()->containsByFilename(modelFile)) { qWarning() << "ERROR: Could not find file:" << modelFile; return; } - ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadingRole, true); - ModelInfo info = ModelList::globalInstance()->modelInfo(modelFile); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadingRole, true); + ModelInfo info = ModelList::globalInstance()->modelInfoByFilename(modelFile); QString url = !info.url.isEmpty() ? info.url : "http://gpt4all.io/models/" + modelFile; Network::globalInstance()->sendDownloadStarted(modelFile); QNetworkRequest request(url); @@ -162,7 +162,7 @@ void Download::cancelDownload(const QString &modelFile) tempFile->deleteLater(); m_activeDownloads.remove(modelReply); - ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadingRole, false); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadingRole, false); break; } } @@ -198,11 +198,11 @@ void Download::removeModel(const QString &modelFile) file.remove(); } - ModelList::globalInstance()->updateData(modelFile, ModelList::BytesReceivedRole, 0); - ModelList::globalInstance()->updateData(modelFile, ModelList::BytesTotalRole, 0); - ModelList::globalInstance()->updateData(modelFile, ModelList::TimestampRole, 0); - ModelList::globalInstance()->updateData(modelFile, ModelList::SpeedRole, QString()); - ModelList::globalInstance()->updateData(modelFile, ModelList::DownloadErrorRole, QString()); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::BytesReceivedRole, 0); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::BytesTotalRole, 0); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::TimestampRole, 0); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::SpeedRole, QString()); + ModelList::globalInstance()->updateDataByFilename(modelFile, ModelList::DownloadErrorRole, QString()); } void Download::handleSslErrors(QNetworkReply *reply, const QList &errors) @@ -265,7 +265,7 @@ void Download::handleErrorOccurred(QNetworkReply::NetworkError code) .arg(code) .arg(modelReply->errorString()); qWarning() << error; - ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, error); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, error); Network::globalInstance()->sendDownloadError(modelFilename, (int)code, modelReply->errorString()); cancelDownload(modelFilename); } @@ -285,12 +285,12 @@ void Download::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) } const QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString(); - const qint64 lastUpdate = ModelList::globalInstance()->data(modelFilename, ModelList::TimestampRole).toLongLong(); + const qint64 lastUpdate = ModelList::globalInstance()->dataByFilename(modelFilename, ModelList::TimestampRole).toLongLong(); const qint64 currentUpdate = QDateTime::currentMSecsSinceEpoch(); if (currentUpdate - lastUpdate < 1000) return; - const qint64 lastBytesReceived = ModelList::globalInstance()->data(modelFilename, ModelList::BytesReceivedRole).toLongLong(); + const qint64 lastBytesReceived = ModelList::globalInstance()->dataByFilename(modelFilename, ModelList::BytesReceivedRole).toLongLong(); const qint64 currentBytesReceived = tempFile->pos(); qint64 timeDifference = currentUpdate - lastUpdate; @@ -304,10 +304,10 @@ void Download::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) else speedText = QString::number(static_cast(speed / (1024.0 * 1024.0)), 'f', 2) + " MB/s"; - ModelList::globalInstance()->updateData(modelFilename, ModelList::BytesReceivedRole, currentBytesReceived); - ModelList::globalInstance()->updateData(modelFilename, ModelList::BytesTotalRole, bytesTotal); - ModelList::globalInstance()->updateData(modelFilename, ModelList::SpeedRole, speedText); - ModelList::globalInstance()->updateData(modelFilename, ModelList::TimestampRole, currentUpdate); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::BytesReceivedRole, currentBytesReceived); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::BytesTotalRole, bytesTotal); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::SpeedRole, speedText); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::TimestampRole, currentUpdate); } HashAndSaveFile::HashAndSaveFile() @@ -403,15 +403,15 @@ void Download::handleModelDownloadFinished() qWarning() << errorString; modelReply->deleteLater(); tempFile->deleteLater(); - ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadingRole, false); - ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, errorString); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadingRole, false); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, errorString); return; } // The hash and save needs the tempFile closed tempFile->close(); - if (!ModelList::globalInstance()->contains(modelFilename)) { + if (!ModelList::globalInstance()->containsByFilename(modelFilename)) { qWarning() << "ERROR: downloading no such file:" << modelFilename; modelReply->deleteLater(); tempFile->deleteLater(); @@ -419,8 +419,8 @@ void Download::handleModelDownloadFinished() } // Notify that we are calculating hash - ModelList::globalInstance()->updateData(modelFilename, ModelList::CalcHashRole, true); - QByteArray md5sum = ModelList::globalInstance()->modelInfo(modelFilename).md5sum; + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::CalcHashRole, true); + QByteArray md5sum = ModelList::globalInstance()->modelInfoByFilename(modelFilename).md5sum; const QString saveFilePath = MySettings::globalInstance()->modelPath() + modelFilename; emit requestHashAndSave(md5sum, saveFilePath, tempFile, modelReply); } @@ -432,15 +432,15 @@ void Download::handleHashAndSaveFinished(bool success, const QString &error, Q_ASSERT(!tempFile->isOpen()); QString modelFilename = modelReply->request().attribute(QNetworkRequest::User).toString(); Network::globalInstance()->sendDownloadFinished(modelFilename, success); - ModelList::globalInstance()->updateData(modelFilename, ModelList::CalcHashRole, false); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::CalcHashRole, false); modelReply->deleteLater(); tempFile->deleteLater(); - ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadingRole, false); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadingRole, false); if (!success) - ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, error); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, error); else - ModelList::globalInstance()->updateData(modelFilename, ModelList::DownloadErrorRole, QString()); + ModelList::globalInstance()->updateDataByFilename(modelFilename, ModelList::DownloadErrorRole, QString()); } void Download::handleReadyRead() diff --git a/gpt4all-chat/modellist.cpp b/gpt4all-chat/modellist.cpp index c1f5a089..b705a4bf 100644 --- a/gpt4all-chat/modellist.cpp +++ b/gpt4all-chat/modellist.cpp @@ -486,6 +486,15 @@ QVariant ModelList::data(const QString &id, int role) const return dataInternal(info, role); } +QVariant ModelList::dataByFilename(const QString &filename, int role) const +{ + QMutexLocker locker(&m_mutex); + for (ModelInfo *info : m_models) + if (info->filename() == filename) + return dataInternal(info, role); + return QVariant(); +} + QVariant ModelList::data(const QModelIndex &index, int role) const { QMutexLocker locker(&m_mutex); @@ -611,6 +620,25 @@ void ModelList::updateData(const QString &id, int role, const QVariant &value) emit userDefaultModelListChanged(); } +void ModelList::updateDataByFilename(const QString &filename, int role, const QVariant &value) +{ + QVector modelsById; + { + QMutexLocker locker(&m_mutex); + for (ModelInfo *info : m_models) + if (info->filename() == filename) + modelsById.append(info->id()); + } + + if (modelsById.isEmpty()) { + qWarning() << "ERROR: cannot update model as list does not contain file" << filename; + return; + } + + for (const QString &id : modelsById) + updateData(id, role, value);; +} + ModelInfo ModelList::modelInfo(const QString &id) const { QMutexLocker locker(&m_mutex); diff --git a/gpt4all-chat/modellist.h b/gpt4all-chat/modellist.h index dc3452ce..89c68229 100644 --- a/gpt4all-chat/modellist.h +++ b/gpt4all-chat/modellist.h @@ -259,7 +259,9 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QString &id, int role) const; + QVariant dataByFilename(const QString &filename, int role) const; void updateData(const QString &id, int role, const QVariant &value); + void updateDataByFilename(const QString &filename, int role, const QVariant &value); int count() const { return m_models.size(); } diff --git a/gpt4all-chat/qml/ModelDownloaderDialog.qml b/gpt4all-chat/qml/ModelDownloaderDialog.qml index e2839c76..cd7d7484 100644 --- a/gpt4all-chat/qml/ModelDownloaderDialog.qml +++ b/gpt4all-chat/qml/ModelDownloaderDialog.qml @@ -197,6 +197,7 @@ Dialog { : (isDownloading ? qsTr("Downloading") : qsTr("Available"))))) + "" color: theme.textColor + linkColor: theme.textErrorColor Accessible.role: Accessible.Paragraph Accessible.name: text Accessible.description: qsTr("Whether the file is already installed on your system")