| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "packagemanagerdialog.h" |
| 7 |
#include "packagemodel.h" |
| 8 |
#include "ui_packagemanagerdialog.h" |
| 9 |
#include "zipextractor.h" |
| 10 |
|
| 11 |
#include <avogadro/qtgui/packagemanager.h> |
| 12 |
|
| 13 |
#include <QtCore/QDir> |
| 14 |
#include <QtCore/QFile> |
| 15 |
#include <QtCore/QFileInfo> |
| 16 |
#include <QtCore/QItemSelectionModel> |
| 17 |
#include <QtCore/QSortFilterProxyModel> |
| 18 |
#include <QtCore/QStandardPaths> |
| 19 |
#include <QtCore/QStringView> |
| 20 |
|
| 21 |
#include <QtGui/QCursor> |
| 22 |
|
| 23 |
#include <QtNetwork/QNetworkAccessManager> |
| 24 |
#include <QtNetwork/QNetworkReply> |
| 25 |
#include <QtNetwork/QNetworkRequest> |
| 26 |
|
| 27 |
#include <QtWidgets/QApplication> |
| 28 |
#include <QtWidgets/QFileDialog> |
| 29 |
#include <QtWidgets/QHeaderView> |
| 30 |
#include <QtWidgets/QLineEdit> |
| 31 |
#include <QtWidgets/QMessageBox> |
| 32 |
#include <QtWidgets/QPushButton> |
| 33 |
#include <QtWidgets/QTableView> |
| 34 |
#include <QtWidgets/QToolTip> |
| 35 |
|
| 36 |
#include <nlohmann/json.hpp> |
| 37 |
|
| 38 |
namespace Avogadro::QtPlugins { |
| 39 |
|
| 40 |
static void setRawHeaders(QNetworkRequest* request) |
| 41 |
{ |
| 42 |
request->setRawHeader("Accept", "text/html,application/xhtml+xml,application/" |
| 43 |
"xml;q=0.9,image/webp,*/*;q=0.8"); |
| 44 |
request->setRawHeader("User-Agent", "Avogadro/2.0 PackageManager"); |
| 45 |
request->setRawHeader("Accept-Language", "en-US,en;q=0.8"); |
| 46 |
} |
| 47 |
|
| 48 |
// --------------------------------------------------------------------------- |
| 49 |
// Constructor / Destructor |
| 50 |
// --------------------------------------------------------------------------- |
| 51 |
|
| 52 |
PackageManagerDialog::PackageManagerDialog(QWidget* parent) |
| 53 |
: QDialog(parent), m_ui(new Ui::PackageManagerDialog) |
| 54 |
{ |
| 55 |
m_filePath = |
| 56 |
QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + |
| 57 |
QStringLiteral("/plugins"); |
| 58 |
|
| 59 |
m_ui->setupUi(this); |
| 60 |
m_network = new QNetworkAccessManager(this); |
| 61 |
|
| 62 |
m_model = new PackageModel(this); |
| 63 |
m_proxyModel = new QSortFilterProxyModel(this); |
| 64 |
m_proxyModel->setSourceModel(m_model); |
| 65 |
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); |
| 66 |
m_proxyModel->setFilterKeyColumn(-1); // search all columns |
| 67 |
m_proxyModel->sort(PackageModel::NameColumn, Qt::AscendingOrder); |
| 68 |
|
| 69 |
m_ui->packageTable->setModel(m_proxyModel); |
| 70 |
m_ui->packageTable->setSortingEnabled(true); |
| 71 |
m_ui->packageTable->setMouseTracking(true); |
| 72 |
m_ui->packageTable->viewport()->setMouseTracking(true); |
| 73 |
m_ui->packageTable->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 74 |
m_ui->packageTable->horizontalHeader()->setStretchLastSection(true); |
| 75 |
m_ui->packageTable->horizontalHeader()->setSectionResizeMode( |
| 76 |
QHeaderView::ResizeToContents); |
| 77 |
m_ui->packageTable->verticalHeader()->hide(); |
| 78 |
|
| 79 |
connect(m_ui->searchEdit, &QLineEdit::textChanged, m_proxyModel, |
| 80 |
&QSortFilterProxyModel::setFilterFixedString); |
| 81 |
connect(m_ui->refreshButton, &QPushButton::clicked, this, |
| 82 |
&PackageManagerDialog::refreshOnlineCatalog); |
| 83 |
connect(m_ui->packageTable->selectionModel(), |
| 84 |
&QItemSelectionModel::currentRowChanged, this, |
| 85 |
&PackageManagerDialog::onCurrentRowChanged); |
| 86 |
connect(m_ui->packageTable, &QAbstractItemView::entered, this, |
| 87 |
&PackageManagerDialog::showCellTooltip); |
| 88 |
connect(m_ui->installButton, &QPushButton::clicked, this, |
| 89 |
&PackageManagerDialog::installSelected); |
| 90 |
connect(m_ui->removeButton, &QPushButton::clicked, this, |
| 91 |
&PackageManagerDialog::removeSelected); |
| 92 |
connect(m_ui->installLocalButton, &QPushButton::clicked, this, |
| 93 |
&PackageManagerDialog::installFromDirectory); |
| 94 |
connect(QtGui::PackageManager::instance(), |
| 95 |
&QtGui::PackageManager::packagesInstalled, this, |
| 96 |
&PackageManagerDialog::onPackagesInstalled); |
| 97 |
|
| 98 |
// Load installed packages immediately (without waiting for the network) |
| 99 |
m_model->mergeInstalledPackages(); |
| 100 |
// Then fetch the online catalog |
| 101 |
getRepoData(); |
| 102 |
} |
| 103 |
|
| 104 |
PackageManagerDialog::~PackageManagerDialog() |
| 105 |
{ |
| 106 |
delete m_ui; |
| 107 |
} |
| 108 |
|
| 109 |
// --------------------------------------------------------------------------- |
| 110 |
// Online catalog |
| 111 |
// --------------------------------------------------------------------------- |
| 112 |
|
| 113 |
void PackageManagerDialog::getRepoData(const QString& url) |
| 114 |
{ |
| 115 |
QNetworkRequest request; |
| 116 |
setRawHeaders(&request); |
| 117 |
request.setUrl(QUrl(url)); |
| 118 |
QNetworkReply* reply = m_network->get(request); |
| 119 |
connect(reply, &QNetworkReply::finished, this, |
| 120 |
&PackageManagerDialog::onCatalogReply); |
| 121 |
} |
| 122 |
|
| 123 |
void PackageManagerDialog::refreshOnlineCatalog() |
| 124 |
{ |
| 125 |
m_ui->readmeBrowser->clear(); |
| 126 |
getRepoData(); |
| 127 |
} |
| 128 |
|
| 129 |
void PackageManagerDialog::onCatalogReply() |
| 130 |
{ |
| 131 |
auto* reply = qobject_cast<QNetworkReply*>(sender()); |
| 132 |
if (reply == nullptr) |
| 133 |
return; |
| 134 |
|
| 135 |
if (reply->error() != QNetworkReply::NoError) { |
| 136 |
m_ui->readmeBrowser->append( |
| 137 |
tr("Error downloading plugin list: %1").arg(reply->errorString())); |
| 138 |
reply->deleteLater(); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
QByteArray bytes = reply->readAll(); |
| 143 |
reply->deleteLater(); |
| 144 |
|
| 145 |
if (bytes.isEmpty()) { |
| 146 |
m_ui->readmeBrowser->append(tr("Error: empty response from server.")); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
m_model->loadOnlineCatalog(bytes); |
| 151 |
m_model->mergeInstalledPackages(); |
| 152 |
} |
| 153 |
|
| 154 |
// --------------------------------------------------------------------------- |
| 155 |
// README display |
| 156 |
// --------------------------------------------------------------------------- |
| 157 |
|
| 158 |
void PackageManagerDialog::showCellTooltip(const QModelIndex& proxyIndex) |
| 159 |
{ |
| 160 |
const QString tooltip = |
| 161 |
m_proxyModel->data(proxyIndex, Qt::ToolTipRole).toString(); |
| 162 |
m_ui->packageTable->viewport()->setToolTip(tooltip); |
| 163 |
|
| 164 |
if (tooltip.isEmpty()) { |
| 165 |
QToolTip::hideText(); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
int pointSize = QApplication::font().pointSize(); |
| 170 |
if (pointSize <= 0) |
| 171 |
pointSize = 10; |
| 172 |
++pointSize; |
| 173 |
|
| 174 |
QString html = tooltip.toHtmlEscaped(); |
| 175 |
html.replace(QLatin1Char('\n'), QStringLiteral("<br/>")); |
| 176 |
|
| 177 |
QToolTip::showText(QCursor::pos(), |
| 178 |
QStringLiteral("<span style=\"font-size:%1pt;\">%2</span>") |
| 179 |
.arg(pointSize) |
| 180 |
.arg(html), |
| 181 |
m_ui->packageTable->viewport(), |
| 182 |
m_ui->packageTable->visualRect(proxyIndex)); |
| 183 |
} |
| 184 |
|
| 185 |
void PackageManagerDialog::onCurrentRowChanged(const QModelIndex& current, |
| 186 |
const QModelIndex& previous) |
| 187 |
{ |
| 188 |
Q_UNUSED(previous) |
| 189 |
if (!current.isValid()) |
| 190 |
return; |
| 191 |
|
| 192 |
QModelIndex sourceIndex = m_proxyModel->mapToSource(current); |
| 193 |
QString url = m_model->readmeUrl(sourceIndex.row()); |
| 194 |
|
| 195 |
m_ui->readmeBrowser->clear(); |
| 196 |
|
| 197 |
// For locally-installed packages with no online README URL, try the local |
| 198 |
// file |
| 199 |
if (url.isEmpty()) { |
| 200 |
const QString& dir = m_model->entry(sourceIndex.row()).installedDir; |
| 201 |
if (!dir.isEmpty()) { |
| 202 |
QFile localReadme(dir + QStringLiteral("/README.md")); |
| 203 |
if (localReadme.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 204 |
m_ui->readmeBrowser->setMarkdown( |
| 205 |
QString::fromUtf8(localReadme.readAll())); |
| 206 |
} |
| 207 |
} |
| 208 |
return; |
| 209 |
} |
| 210 |
QNetworkRequest request; |
| 211 |
setRawHeaders(&request); |
| 212 |
request.setUrl(QUrl(url)); |
| 213 |
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, |
| 214 |
QNetworkRequest::NoLessSafeRedirectPolicy); |
| 215 |
QNetworkReply* reply = m_network->get(request); |
| 216 |
reply->setProperty("readmeUrl", url); |
| 217 |
connect(reply, &QNetworkReply::finished, this, |
| 218 |
&PackageManagerDialog::onReadmeReply); |
| 219 |
} |
| 220 |
|
| 221 |
void PackageManagerDialog::onReadmeReply() |
| 222 |
{ |
| 223 |
auto* reply = qobject_cast<QNetworkReply*>(sender()); |
| 224 |
if (reply == nullptr) |
| 225 |
return; |
| 226 |
|
| 227 |
// Discard replies that no longer match the current selection |
| 228 |
const QString requestedUrl = reply->property("readmeUrl").toString(); |
| 229 |
const QModelIndex currentIdx = |
| 230 |
m_ui->packageTable->selectionModel()->currentIndex(); |
| 231 |
if (currentIdx.isValid()) { |
| 232 |
const QModelIndex sourceIdx = m_proxyModel->mapToSource(currentIdx); |
| 233 |
if (requestedUrl != m_model->readmeUrl(sourceIdx.row())) { |
| 234 |
reply->deleteLater(); |
| 235 |
return; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if (reply->error() != QNetworkReply::NoError) { |
| 240 |
m_ui->readmeBrowser->setPlainText( |
| 241 |
tr("Error fetching README: %1").arg(reply->errorString())); |
| 242 |
reply->deleteLater(); |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
QByteArray bytes = reply->readAll(); |
| 247 |
reply->deleteLater(); |
| 248 |
|
| 249 |
// GitHub API returns JSON with base64-encoded content |
| 250 |
if (nlohmann::json::accept(bytes.data())) { |
| 251 |
nlohmann::json root = nlohmann::json::parse(bytes.data()); |
| 252 |
if (root.contains("content") && root["content"].is_string()) { |
| 253 |
QByteArray content = |
| 254 |
QByteArray::fromBase64(root["content"].get<std::string>().c_str()); |
| 255 |
m_ui->readmeBrowser->setMarkdown(QString::fromUtf8(content)); |
| 256 |
return; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// Fall back: treat the response as raw markdown |
| 261 |
m_ui->readmeBrowser->setMarkdown(QString::fromUtf8(bytes)); |
| 262 |
} |
| 263 |
|
| 264 |
// --------------------------------------------------------------------------- |
| 265 |
// Install / Update |
| 266 |
// --------------------------------------------------------------------------- |
| 267 |
|
| 268 |
void PackageManagerDialog::installSelected() |
| 269 |
{ |
| 270 |
m_downloadQueue.clear(); |
| 271 |
m_ui->readmeBrowser->clear(); |
| 272 |
|
| 273 |
QList<int> rows; |
| 274 |
const QModelIndexList selected = |
| 275 |
m_ui->packageTable->selectionModel()->selectedRows(); |
| 276 |
for (const QModelIndex& proxyIdx : selected) |
| 277 |
rows.append(m_proxyModel->mapToSource(proxyIdx).row()); |
| 278 |
|
| 279 |
// TODO: Fix this since we don't use checkboxes |
| 280 |
if (rows.isEmpty()) { |
| 281 |
QMessageBox::information( |
| 282 |
this, tr("Nothing Selected"), |
| 283 |
tr("Check the box next to a plugin to install or update it.")); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
// Warn about any packages that require a newer Avogadro version |
| 288 |
QStringList incompatible; |
| 289 |
for (int row : rows) { |
| 290 |
const PackageModel::PackageEntry& e = m_model->entry(row); |
| 291 |
if (!PackageModel::versionCompatible(e)) |
| 292 |
incompatible.append( |
| 293 |
tr("%1 (requires %2)").arg(e.name, e.minimumAvogadroVersion)); |
| 294 |
} |
| 295 |
if (!incompatible.isEmpty()) { |
| 296 |
const int ret = QMessageBox::warning( |
| 297 |
this, tr("Version Incompatibility"), |
| 298 |
tr("The following plugins require a newer version of Avogadro:\n\n" |
| 299 |
"%1\n\nInstall anyway?") |
| 300 |
.arg(incompatible.join('\n')), |
| 301 |
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel); |
| 302 |
if (ret != QMessageBox::Yes) |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
for (int row : rows) { |
| 307 |
const PackageModel::PackageEntry& e = m_model->entry(row); |
| 308 |
if (e.zipballUrl.isEmpty()) |
| 309 |
continue; |
| 310 |
DownloadEntry de; |
| 311 |
de.url = e.zipballUrl; |
| 312 |
de.name = e.name; |
| 313 |
m_downloadQueue.append(de); |
| 314 |
} |
| 315 |
|
| 316 |
if (m_downloadQueue.isEmpty()) { |
| 317 |
QStringList unavailable; |
| 318 |
unavailable.reserve(rows.size()); |
| 319 |
for (int row : rows) { |
| 320 |
const PackageModel::PackageEntry& e = m_model->entry(row); |
| 321 |
if (e.zipballUrl.isEmpty()) |
| 322 |
unavailable.append(e.name); |
| 323 |
} |
| 324 |
QMessageBox::information( |
| 325 |
this, tr("No Download Available"), |
| 326 |
tr("The selected plugin(s) do not provide a downloadable source URL:\n\n" |
| 327 |
"%1") |
| 328 |
.arg(unavailable.join(QLatin1Char('\n')))); |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
downloadNext(); |
| 333 |
} |
| 334 |
|
| 335 |
void PackageManagerDialog::downloadNext() |
| 336 |
{ |
| 337 |
if (m_downloadQueue.isEmpty()) |
| 338 |
return; |
| 339 |
|
| 340 |
const QString url = m_downloadQueue.last().url; |
| 341 |
QNetworkRequest request; |
| 342 |
setRawHeaders(&request); |
| 343 |
request.setUrl(QUrl(url)); |
| 344 |
QNetworkReply* reply = m_network->get(request); |
| 345 |
connect(reply, &QNetworkReply::finished, this, |
| 346 |
&PackageManagerDialog::handleRedirect); |
| 347 |
} |
| 348 |
|
| 349 |
void PackageManagerDialog::handleRedirect() |
| 350 |
{ |
| 351 |
auto* reply = qobject_cast<QNetworkReply*>(sender()); |
| 352 |
if (reply == nullptr) |
| 353 |
return; |
| 354 |
if (m_downloadQueue.isEmpty()) { |
| 355 |
reply->deleteLater(); |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
int statusCode = |
| 360 |
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
| 361 |
|
| 362 |
if (reply->error() == QNetworkReply::NoError) { |
| 363 |
if (statusCode == 301 || statusCode == 302 || statusCode == 307 || |
| 364 |
statusCode == 308) { |
| 365 |
QUrl redirectUrl = |
| 366 |
reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); |
| 367 |
if (redirectUrl.isRelative()) |
| 368 |
redirectUrl = reply->url().resolved(redirectUrl); |
| 369 |
reply->deleteLater(); |
| 370 |
|
| 371 |
if (!redirectUrl.isValid()) { |
| 372 |
m_ui->readmeBrowser->append( |
| 373 |
tr("Failed to follow redirect while downloading %1.\n") |
| 374 |
.arg(m_downloadQueue.last().name)); |
| 375 |
m_downloadQueue.removeLast(); |
| 376 |
downloadNext(); |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
QNetworkRequest request; |
| 381 |
setRawHeaders(&request); |
| 382 |
request.setUrl(redirectUrl); |
| 383 |
QNetworkReply* redirectReply = m_network->get(request); |
| 384 |
connect(redirectReply, &QNetworkReply::finished, this, |
| 385 |
&PackageManagerDialog::handleRedirect); |
| 386 |
} else if (statusCode == 200) { |
| 387 |
unzipPlugin(reply); |
| 388 |
} else { |
| 389 |
m_ui->readmeBrowser->append( |
| 390 |
tr("Failed to download from %1: status %2, %3\n") |
| 391 |
.arg(reply->url().toString()) |
| 392 |
.arg(statusCode) |
| 393 |
.arg(reply->errorString())); |
| 394 |
reply->deleteLater(); |
| 395 |
m_downloadQueue.removeLast(); |
| 396 |
downloadNext(); |
| 397 |
} |
| 398 |
} else { |
| 399 |
m_ui->readmeBrowser->append(tr("Failed to download from %1: %2\n") |
| 400 |
.arg(reply->url().toString()) |
| 401 |
.arg(reply->errorString())); |
| 402 |
reply->deleteLater(); |
| 403 |
m_downloadQueue.removeLast(); |
| 404 |
downloadNext(); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
void PackageManagerDialog::unzipPlugin(QNetworkReply* reply) |
| 409 |
{ |
| 410 |
if (reply == nullptr) |
| 411 |
return; |
| 412 |
if (m_downloadQueue.isEmpty()) { |
| 413 |
reply->deleteLater(); |
| 414 |
return; |
| 415 |
} |
| 416 |
|
| 417 |
if (reply->error() != QNetworkReply::NoError) { |
| 418 |
reply->deleteLater(); |
| 419 |
m_downloadQueue.removeLast(); |
| 420 |
downloadNext(); |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
QByteArray fileData = reply->readAll(); |
| 425 |
reply->deleteLater(); |
| 426 |
|
| 427 |
QDir().mkpath(m_filePath); |
| 428 |
|
| 429 |
const QString repoName = m_downloadQueue.last().name; |
| 430 |
const QString filename = repoName + QStringLiteral(".zip"); |
| 431 |
const QString absolutePath = m_filePath + '/' + filename; |
| 432 |
|
| 433 |
m_ui->readmeBrowser->append( |
| 434 |
tr("Downloading %1 to %2\n").arg(filename).arg(m_filePath)); |
| 435 |
|
| 436 |
QFile out(absolutePath); |
| 437 |
if (!out.open(QIODevice::WriteOnly)) { |
| 438 |
QMessageBox::critical(this, tr("Error"), |
| 439 |
tr("Cannot save file %1.").arg(absolutePath)); |
| 440 |
m_downloadQueue.removeLast(); |
| 441 |
downloadNext(); |
| 442 |
return; |
| 443 |
} |
| 444 |
out.write(fileData); |
| 445 |
out.close(); |
| 446 |
|
| 447 |
ZipExtractor unzip; |
| 448 |
m_ui->readmeBrowser->append(tr("Extracting %1…\n").arg(filename)); |
| 449 |
|
| 450 |
QList<QString> newFiles = unzip.listFiles(absolutePath.toStdString()); |
| 451 |
QList<QString> errors = |
| 452 |
unzip.extract(m_filePath.toStdString() + '/', absolutePath.toStdString()); |
| 453 |
|
| 454 |
if (errors.empty() && !newFiles.isEmpty()) { |
| 455 |
m_ui->readmeBrowser->append( |
| 456 |
tr("Extraction complete (%1 files)\n").arg(newFiles.size())); |
| 457 |
|
| 458 |
// Derive component name from GitHub archive directory names like: |
| 459 |
// "OpenChemistry-crystals-a7c672d" → "crystals" |
| 460 |
// "ghutchis-avogadro-rdkit-2e9abcd" → "avogadro-rdkit" |
| 461 |
// Pattern is always {org}-{repo}-{sha}, strip org prefix and SHA suffix. |
| 462 |
QString rawName = newFiles[0]; |
| 463 |
if (rawName.endsWith('/')) |
| 464 |
rawName.chop(1); |
| 465 |
|
| 466 |
QString component = rawName; |
| 467 |
// Strip trailing SHA: last segment if it looks like a hex hash (7+ chars) |
| 468 |
int lastDash = component.lastIndexOf('-'); |
| 469 |
if (lastDash != -1) { |
| 470 |
QStringView tail = QStringView(component).mid(lastDash + 1); |
| 471 |
bool isHex = tail.size() >= 7; |
| 472 |
for (qsizetype i = 0; isHex && i < tail.size(); ++i) { |
| 473 |
QChar c = tail[i]; |
| 474 |
isHex = c.isDigit() || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
| 475 |
} |
| 476 |
if (isHex) |
| 477 |
component = component.left(lastDash); |
| 478 |
} |
| 479 |
// Strip leading org/owner prefix (everything before the first dash) |
| 480 |
int firstDash = component.indexOf('-'); |
| 481 |
if (firstDash != -1) |
| 482 |
component = component.mid(firstDash + 1); |
| 483 |
const QString source = QDir::cleanPath(m_filePath + '/' + newFiles[0]); |
| 484 |
const QString dest = QDir::cleanPath(m_filePath + '/' + component); |
| 485 |
|
| 486 |
if (source != dest) { |
| 487 |
QDir(dest).removeRecursively(); |
| 488 |
|
| 489 |
if (!QDir().rename(source, dest)) { |
| 490 |
m_ui->readmeBrowser->append( |
| 491 |
tr("Error: could not move extracted package %1 to %2\n") |
| 492 |
.arg(source) |
| 493 |
.arg(dest)); |
| 494 |
out.remove(); |
| 495 |
m_downloadQueue.removeLast(); |
| 496 |
downloadNext(); |
| 497 |
return; |
| 498 |
} |
| 499 |
} |
| 500 |
QtGui::PackageManager::instance()->installPackages({ dest }); |
| 501 |
} else { |
| 502 |
if (!errors.isEmpty()) |
| 503 |
m_ui->readmeBrowser->append( |
| 504 |
tr("Extraction error: %1").arg(errors.first())); |
| 505 |
else |
| 506 |
m_ui->readmeBrowser->append(tr("Extraction yielded no files.\n")); |
| 507 |
} |
| 508 |
|
| 509 |
out.remove(); |
| 510 |
m_downloadQueue.removeLast(); |
| 511 |
downloadNext(); |
| 512 |
} |
| 513 |
|
| 514 |
void PackageManagerDialog::onPackagesInstalled() |
| 515 |
{ |
| 516 |
m_ui->readmeBrowser->append(tr("Installation complete.\n")); |
| 517 |
m_model->mergeInstalledPackages(); |
| 518 |
} |
| 519 |
|
| 520 |
// --------------------------------------------------------------------------- |
| 521 |
// Remove |
| 522 |
// --------------------------------------------------------------------------- |
| 523 |
|
| 524 |
void PackageManagerDialog::removeSelected() |
| 525 |
{ |
| 526 |
// Collect installed selected rows (map proxy → source) |
| 527 |
QList<int> toRemove; |
| 528 |
const QModelIndexList selected = |
| 529 |
m_ui->packageTable->selectionModel()->selectedRows(); |
| 530 |
for (const QModelIndex& proxyIdx : selected) { |
| 531 |
int row = m_proxyModel->mapToSource(proxyIdx).row(); |
| 532 |
const PackageModel::PackageEntry& e = m_model->entry(row); |
| 533 |
if (e.status != PackageModel::PackageStatus::NotInstalled) |
| 534 |
toRemove.append(row); |
| 535 |
} |
| 536 |
|
| 537 |
// Symlinks are created for local-disk installs — just remove the link |
| 538 |
QMutableListIterator<int> it(toRemove); |
| 539 |
while (it.hasNext()) { |
| 540 |
int row = it.next(); |
| 541 |
const PackageModel::PackageEntry& e = m_model->entry(row); |
| 542 |
QFileInfo fi(e.installedDir); |
| 543 |
if (fi.isSymLink()) { |
| 544 |
QFile::remove(e.installedDir); |
| 545 |
const QString packageKey = e.packageKey.isEmpty() ? e.name : e.packageKey; |
| 546 |
QtGui::PackageManager::instance()->unregisterPackage(packageKey); |
| 547 |
it.remove(); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
// TODO - fix this string since we don't use checkboxes |
| 552 |
if (toRemove.isEmpty()) { |
| 553 |
QMessageBox::information( |
| 554 |
this, tr("Nothing to Remove"), |
| 555 |
tr("Check the box next to an installed plugin to remove it.")); |
| 556 |
return; |
| 557 |
} |
| 558 |
|
| 559 |
// Ask user whether to delete files |
| 560 |
QMessageBox msgBox(this); |
| 561 |
msgBox.setWindowTitle(tr("Remove Plugins")); |
| 562 |
msgBox.setText(tr("Remove %n selected plugin(s)?", "", toRemove.size())); |
| 563 |
msgBox.setIcon(QMessageBox::Question); |
| 564 |
msgBox.setStandardButtons(QMessageBox::Cancel); |
| 565 |
|
| 566 |
msgBox.addButton(tr("Keep Files"), QMessageBox::NoRole); |
| 567 |
QPushButton* deleteBtn = |
| 568 |
msgBox.addButton(tr("Delete Files"), QMessageBox::DestructiveRole); |
| 569 |
|
| 570 |
msgBox.exec(); |
| 571 |
|
| 572 |
if (msgBox.clickedButton() == msgBox.button(QMessageBox::Cancel)) |
| 573 |
return; |
| 574 |
|
| 575 |
bool deleteFiles = (msgBox.clickedButton() == deleteBtn); |
| 576 |
|
| 577 |
for (int row : toRemove) { |
| 578 |
PackageModel::PackageEntry& e = m_model->entry(row); |
| 579 |
const QString packageKey = e.packageKey.isEmpty() ? e.name : e.packageKey; |
| 580 |
QtGui::PackageManager::instance()->unregisterPackage(packageKey); |
| 581 |
if (deleteFiles && !e.installedDir.isEmpty()) { |
| 582 |
if (QFileInfo{ e.installedDir }.isSymLink()) |
| 583 |
QFile(e.installedDir).remove(); |
| 584 |
else |
| 585 |
QDir(e.installedDir).removeRecursively(); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
// Refresh table state |
| 590 |
m_model->mergeInstalledPackages(); |
| 591 |
} |
| 592 |
|
| 593 |
// --------------------------------------------------------------------------- |
| 594 |
// Install from local directory |
| 595 |
// --------------------------------------------------------------------------- |
| 596 |
|
| 597 |
void PackageManagerDialog::installFromDirectory() |
| 598 |
{ |
| 599 |
QString dir = QFileDialog::getExistingDirectory( |
| 600 |
this, tr("Select Plugin Directory"), QDir::homePath()); |
| 601 |
if (dir.isEmpty()) |
| 602 |
return; |
| 603 |
|
| 604 |
if (!QFileInfo(dir + QStringLiteral("/pyproject.toml")).exists()) { |
| 605 |
QMessageBox::warning( |
| 606 |
this, tr("Invalid Directory"), |
| 607 |
tr("The selected directory does not contain a pyproject.toml file.")); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
QDir().mkpath(m_filePath); |
| 612 |
|
| 613 |
const QString baseName = QFileInfo(dir).fileName(); |
| 614 |
const QString linkPath = m_filePath + '/' + baseName; |
| 615 |
|
| 616 |
QFileInfo existing(linkPath); |
| 617 |
if (existing.isSymLink()) { |
| 618 |
if (existing.symLinkTarget() == dir) { |
| 619 |
// Already pointing at the right directory — nothing to do |
| 620 |
m_ui->readmeBrowser->append( |
| 621 |
tr("Plugin already installed as symlink: %1\n").arg(linkPath)); |
| 622 |
QtGui::PackageManager::instance()->installPackages({ linkPath }); |
| 623 |
return; |
| 624 |
} |
| 625 |
// Stale symlink pointing elsewhere — remove and reinstall |
| 626 |
QFile::remove(linkPath); |
| 627 |
} else if (existing.isDir()) { |
| 628 |
// Remove any previous non-symlink installation at that path |
| 629 |
QDir(linkPath).removeRecursively(); |
| 630 |
} |
| 631 |
|
| 632 |
// Attempt to create a symlink; fall back to a recursive copy |
| 633 |
bool linked = QFile::link(dir, linkPath); |
| 634 |
if (linked) { |
| 635 |
m_ui->readmeBrowser->append( |
| 636 |
tr("Created symlink: %1 → %2\n").arg(linkPath).arg(dir)); |
| 637 |
} else { |
| 638 |
m_ui->readmeBrowser->append( |
| 639 |
tr("Symlink creation failed; copying files instead…\n")); |
| 640 |
if (!copyDir(dir, linkPath)) { |
| 641 |
QMessageBox::critical(this, tr("Error"), |
| 642 |
tr("Failed to copy package to %1.").arg(linkPath)); |
| 643 |
return; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
m_ui->readmeBrowser->append(tr("Installing plugin from %1…\n").arg(dir)); |
| 648 |
QtGui::PackageManager::instance()->installPackages({ linkPath }); |
| 649 |
} |
| 650 |
|
| 651 |
bool PackageManagerDialog::copyDir(const QString& src, const QString& dst) |
| 652 |
{ |
| 653 |
QDir srcDir(src); |
| 654 |
if (!srcDir.exists()) |
| 655 |
return false; |
| 656 |
|
| 657 |
QDir().mkpath(dst); |
| 658 |
|
| 659 |
const QFileInfoList entries = |
| 660 |
srcDir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); |
| 661 |
for (const QFileInfo& fi : entries) { |
| 662 |
const QString target = dst + '/' + fi.fileName(); |
| 663 |
if (fi.isDir()) { |
| 664 |
if (!copyDir(fi.absoluteFilePath(), target)) |
| 665 |
return false; |
| 666 |
} else { |
| 667 |
if (!QFile::copy(fi.absoluteFilePath(), target)) |
| 668 |
return false; |
| 669 |
} |
| 670 |
} |
| 671 |
return true; |
| 672 |
} |
| 673 |
|
| 674 |
} // namespace Avogadro::QtPlugins |