Skip to content

Commit

Permalink
Merge pull request #1646 from ghutchis/fix-pdb-invalid-crash
Browse files Browse the repository at this point in the history
Add more error checking for Fetch PDB
  • Loading branch information
ghutchis authored Mar 7, 2024
2 parents 7cc9f19 + df5ada6 commit f2f1ff2
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions avogadro/qtplugins/fetchpdb/fetchpdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ bool FetchPDB::readMolecule(QtGui::Molecule& mol)

void FetchPDB::showDialog()
{
if (!m_network) {
m_network = new QNetworkAccessManager(this);
connect(m_network, SIGNAL(finished(QNetworkReply*)), this,
SLOT(replyFinished(QNetworkReply*)));
}
if (!m_progressDialog) {
m_progressDialog = new QProgressDialog(qobject_cast<QWidget*>(parent()));
}
// Prompt for a chemical structure name
bool ok;
QString pdbCode = QInputDialog::getText(
Expand All @@ -83,10 +75,36 @@ void FetchPDB::showDialog()
if (!ok || pdbCode.isEmpty())
return;

// check if the PDB code matches the expected format
if (pdbCode.length() != 4) {
QMessageBox::warning(qobject_cast<QWidget*>(parent()),
tr("Invalid PDB Code"),
tr("The PDB code must be exactly 4 characters long."));
return;
}

// first character should be 1-9
if (!pdbCode.at(0).isDigit() || pdbCode.at(0).toLatin1() == '0') {
QMessageBox::warning(
qobject_cast<QWidget*>(parent()), tr("Invalid PDB Code"),
tr("The first character of the PDB code must be 1-9."));
return;
}

if (!m_network) {
m_network = new QNetworkAccessManager(this);
connect(m_network, SIGNAL(finished(QNetworkReply*)), this,
SLOT(replyFinished(QNetworkReply*)));
}

// Hard coding the PDB download URL
m_network->get(QNetworkRequest(
QUrl("https://files.rcsb.org/download/" + pdbCode + ".pdb")));

if (!m_progressDialog) {
m_progressDialog = new QProgressDialog(qobject_cast<QWidget*>(parent()));
}

m_moleculeName = pdbCode;
m_progressDialog->setLabelText(tr("Querying for %1").arg(pdbCode));
m_progressDialog->setRange(0, 0);
Expand All @@ -106,23 +124,27 @@ void FetchPDB::replyFinished(QNetworkReply* reply)
}

m_moleculeData = reply->readAll();
m_tempFileName =
QDir::tempPath() + QDir::separator() + m_moleculeName + ".pdb";
QFile out(m_tempFileName);
out.open(QIODevice::WriteOnly);
out.write(m_moleculeData);
out.close();

// Check if the file was successfully downloaded
if (m_moleculeData.contains("Error report") ||
if (m_moleculeData.contains("Not Found") ||
m_moleculeData.contains("Error report") ||
m_moleculeData.contains("Page not found (404)")) {
QMessageBox::warning(
qobject_cast<QWidget*>(parent()), tr("Network Download Failed"),
tr("Specified molecule could not be found: %1").arg(m_moleculeName));
reply->deleteLater();
return;
}

m_tempFileName =
QDir::tempPath() + QDir::separator() + m_moleculeName + ".pdb";
QFile out(m_tempFileName);
out.open(QIODevice::WriteOnly);
out.write(m_moleculeData);
out.close();

emit moleculeReady(1);
reply->deleteLater();
}

} // namespace Avogadro::QtPlugins

0 comments on commit f2f1ff2

Please sign in to comment.