Fix broken download/remove/install.

This commit is contained in:
Adam Treat 2023-07-05 20:12:37 -04:00
parent eab92a9d73
commit 27981c0d21
4 changed files with 57 additions and 26 deletions

View File

@ -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<QSslError> &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<double>(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()

View File

@ -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<QString> 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);

View File

@ -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(); }

View File

@ -197,6 +197,7 @@ Dialog {
: (isDownloading ? qsTr("Downloading") : qsTr("Available")))))
+ "</strong></font>"
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")