#include "browse-page.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ui-kit.h" BrowsePage::BrowsePage(AppCore* core, QWidget* parent) : Page(core, parent) { QVBoxLayout* layout = new QVBoxLayout(this); layout->setContentsMargins(24, 20, 24, 20); layout->setSpacing(12); // Search row (search mode only) m_searchRow = new QWidget(); QHBoxLayout* searchRow = new QHBoxLayout(m_searchRow); searchRow->setContentsMargins(0, 0, 0, 0); searchRow->setSpacing(10); m_searchBar = new QLineEdit(); // Drops belong to the window: a .flatpakref let go over the search field // must install, not paste its path (see MainWindow::dragEnterEvent). m_searchBar->setAcceptDrops(false); m_searchBar->setPlaceholderText(tr("Rechercher un paquet (ex : firefox)...")); m_searchBar->setMinimumHeight(40); m_searchBar->addAction(ui::icon("search"), QLineEdit::LeadingPosition); connect(m_searchBar, &QLineEdit::returnPressed, this, &BrowsePage::onSearch); searchRow->addWidget(m_searchBar, 1); m_searchButton = new QPushButton(tr("Rechercher")); m_searchButton->setProperty("variant", "primary"); m_searchButton->setMinimumHeight(40); m_searchButton->setMinimumWidth(130); connect(m_searchButton, &QPushButton::clicked, this, &BrowsePage::onSearch); searchRow->addWidget(m_searchButton); layout->addWidget(m_searchRow); // Filter row (installed mode only) m_filterRow = new QWidget(); QHBoxLayout* filterRow = new QHBoxLayout(m_filterRow); filterRow->setContentsMargins(0, 0, 0, 0); filterRow->setSpacing(10); m_filterBar = new QLineEdit(); m_filterBar->setAcceptDrops(false); // drops go to the window (see above) m_filterBar->setPlaceholderText(tr("Filtrer par nom...")); m_filterBar->setMinimumHeight(36); m_filterBar->addAction(ui::icon("filter"), QLineEdit::LeadingPosition); m_filterBar->setClearButtonEnabled(true); connect(m_filterBar, &QLineEdit::textChanged, this, &BrowsePage::onFilterChanged); filterRow->addWidget(m_filterBar, 1); QLabel* filterHint = new QLabel(tr("Filtre local, instantané")); filterHint->setStyleSheet("font-size: 12px; color: #64748b;"); filterRow->addWidget(filterHint); m_filterRow->setVisible(false); layout->addWidget(m_filterRow); // Thin progress bar, shown while the core works on a listing. m_progressBar = new QProgressBar(); m_progressBar->setVisible(false); m_progressBar->setFixedHeight(6); m_progressBar->setTextVisible(false); layout->addWidget(m_progressBar); // Action toolbar QHBoxLayout* actionBar = new QHBoxLayout(); actionBar->setSpacing(8); m_installButton = new QPushButton(tr("Installer")); m_installButton->setProperty("variant", "success"); m_installButton->setIcon(ui::icon("package")); m_installButton->setEnabled(false); connect(m_installButton, &QPushButton::clicked, this, [this, core] { if (m_selected.name.empty()) return; m_pendingSelect = QString::fromStdString(m_selected.name); start(core->install(m_selected), tr("Installation")); }); actionBar->addWidget(m_installButton); m_removeButton = new QPushButton(tr("Supprimer")); m_removeButton->setProperty("variant", "danger"); m_removeButton->setIcon(ui::icon("trash")); m_removeButton->setEnabled(false); connect(m_removeButton, &QPushButton::clicked, this, [this, core] { if (m_selected.name.empty()) return; m_pendingSelect = QString::fromStdString(m_selected.name); // One row stands for one installation, or for a provider that only ever // has one: there is nothing to choose, so the row is what gets removed. if (m_selected.instances.size() < 2) { start(core->remove(m_selected), tr("Suppression")); return; } // Several copies of the same application: the user says which ones go. // A cancelled chooser aborts, an empty choice says so in the status bar. const auto targets = chooseInstancesToRemove(); if (!targets.has_value()) return; if (targets->empty()) { emit message(tr("Aucune installation sélectionnée : cochez les copies " "à supprimer, ou annulez.")); return; } start(core->removeInstances(m_selected, *targets), tr("Suppression")); }); actionBar->addWidget(m_removeButton); m_updateButton = new QPushButton(tr("Mettre à jour")); m_updateButton->setProperty("variant", "warning"); m_updateButton->setIcon(ui::icon("refresh")); m_updateButton->setEnabled(false); connect(m_updateButton, &QPushButton::clicked, this, [this, core] { if (m_selected.name.empty()) return; m_pendingSelect = QString::fromStdString(m_selected.name); start(core->update(m_selected), tr("Mise à jour")); }); actionBar->addWidget(m_updateButton); m_updateAllButton = new QPushButton(tr("Tout mettre à jour")); m_updateAllButton->setProperty("variant", "info"); m_updateAllButton->setIcon(ui::icon("arrow-up")); m_updateAllButton->setEnabled(false); connect(m_updateAllButton, &QPushButton::clicked, this, [this, core] { start(core->updateAll(), tr("Mise à jour globale")); }); actionBar->addWidget(m_updateAllButton); QPushButton* exportButton = new QPushButton(tr("Exporter")); exportButton->setToolTip(tr("Exporter la liste affichée en CSV")); connect(exportButton, &QPushButton::clicked, this, &BrowsePage::exportCsv); actionBar->addWidget(exportButton); actionBar->addStretch(); m_resultCount = ui::countLabel(); actionBar->addWidget(m_resultCount); layout->addLayout(actionBar); // Splitter: package table | details QSplitter* splitter = new QSplitter(Qt::Horizontal); splitter->setHandleWidth(10); QFrame* listCard = ui::card(); QVBoxLayout* listLayout = new QVBoxLayout(listCard); listLayout->setContentsMargins(8, 8, 8, 8); listLayout->setSpacing(6); QLabel* listHeader = new QLabel("PAQUETS"); listHeader->setObjectName("cardTitle"); listLayout->addWidget(listHeader); m_table = ui::table({tr("Nom"), tr("Version"), tr("Dépôt"), tr("Fournisseur"), tr("Statut")}); connect(m_table, &QTableWidget::currentCellChanged, this, [this](int currentRow, int, int, int) { onPackageSelected(currentRow); }); m_tableStack = ui::withEmptyState( m_table, tr("Aucun paquet à afficher.\nLancez une recherche ou ouvrez la liste des " "paquets installés.")); // The "Tout mettre à jour" button only makes sense on the updates tab, but // there it must be usable without selecting a row first; the per-package // buttons stay gated on the selection. m_updateAllButton->setToolTip(tr("Mettre à jour tous les paquets listés")); listLayout->addWidget(m_tableStack); splitter->addWidget(listCard); QFrame* detailsCard = ui::card(); QVBoxLayout* detailsLayout = new QVBoxLayout(detailsCard); detailsLayout->setContentsMargins(20, 18, 20, 18); detailsLayout->setSpacing(10); m_pkgName = new QLabel(tr("Sélectionnez un paquet")); m_pkgName->setWordWrap(true); // Serif like the site's package headings. m_pkgName->setStyleSheet( "font-family: serif; font-size: 22px; font-weight: 700; color: #e2e8f0;" "background: transparent;"); detailsLayout->addWidget(m_pkgName); m_pkgVersion = new QLabel(""); m_pkgVersion->setStyleSheet( "font-size: 13px; color: #94a3b8; background: transparent;"); detailsLayout->addWidget(m_pkgVersion); QHBoxLayout* metaRow = new QHBoxLayout(); metaRow->setSpacing(8); auto makeMetaBadge = [](QLabel** out) { QLabel* label = new QLabel("—"); label->setStyleSheet( "background: #273449; color: #94a3b8; border: 1px solid #303b4e;" "border-radius: 999px; padding: 3px 12px; font-size: 12px;"); *out = label; return label; }; metaRow->addWidget(makeMetaBadge(&m_pkgRepo)); metaRow->addWidget(makeMetaBadge(&m_pkgSize)); metaRow->addWidget(makeMetaBadge(&m_pkgProvider)); metaRow->addStretch(); detailsLayout->addLayout(metaRow); // Only shown for a package the provider holds several times over. m_pkgInstances = new QLabel(); m_pkgInstances->setWordWrap(true); m_pkgInstances->setStyleSheet( "font-size: 13px; color: #7dd3fc; background: transparent;"); m_pkgInstances->setVisible(false); detailsLayout->addWidget(m_pkgInstances); QFrame* separator = new QFrame(); separator->setFrameShape(QFrame::HLine); separator->setStyleSheet( "color: #303b4e; background: #303b4e; max-height: 1px; border: none;"); detailsLayout->addWidget(separator); m_pkgDescription = new QLabel(""); m_pkgDescription->setWordWrap(true); m_pkgDescription->setAlignment(Qt::AlignTop | Qt::AlignLeft); m_pkgDescription->setStyleSheet( "font-size: 14px; color: #94a3b8; background: transparent;"); detailsLayout->addWidget(m_pkgDescription, 1); detailsLayout->addStretch(); splitter->addWidget(detailsCard); splitter->setSizes({560, 420}); layout->addWidget(splitter, 1); // ---- Core wiring ---- connect(core, &AppCore::searchChunk, this, [this](std::vector results) { appendRows(results); }); connect(core, &AppCore::searchFinished, this, [this](std::vector results) { populateTable(results); }); connect(core, &AppCore::listFinished, this, [this](AppCore::ListMode mode, std::vector results) { if (mode == AppCore::ListMode::Installed) { m_installed = results; if (m_mode == Mode::Installed) onFilterChanged(m_filterBar->text()); } else if (mode == AppCore::ListMode::Updates) { // The "searching" hint is replaced by the outcome: an empty // result means the system is up to date, not that the check // is still running. if (results.empty()) { ui::setEmptyStateText( m_tableStack, tr("Aucune mise à jour disponible.\nVotre système est à " "jour.")); } if (m_mode == Mode::Updates) populateTable(results); } }); connect(core, &AppCore::busyChanged, this, [this](bool busy) { // A listing started from elsewhere (dashboard) while the user was // already on the updates tab would leave it without data: reload now // that the core is free again. if (!busy && m_mode == Mode::Updates && isVisible()) reloadCurrentView(); }); connect(core, &AppCore::busyChanged, this, [this](bool busy) { m_progressBar->setVisible(busy); if (!busy) { // A band stopped just before the end reads as a cancellation: when // real progress was shown, it is completed first. if (m_progressBar->maximum() > 0) m_progressBar->setValue(100); return; } m_progressValue = 0; // Start indeterminate; the first real percentage reported by the tool // turns the band into a determinate fill. m_progressBar->setRange(0, 0); m_progressBar->setValue(0); }); connect(core, &AppCore::actionProgress, this, [this](const QString&, int percent) { if (percent < 0) return; // Providers (and the commands inside one operation) each count // from 0 again, so the band only ever moves forward. if (percent > m_progressValue) m_progressValue = percent; m_progressBar->setRange(0, 100); m_progressBar->setValue(m_progressValue); }); connect(core, &AppCore::actionFinished, this, [this](const QString& action, const QString&, bool success, const QString&, bool) { // The installed state changed: show the fresh state of what the // user is looking at, but only when they are looking at it. if (!success || !isVisible()) return; if (action == QLatin1String("install") || action == QLatin1String("remove") || action == QLatin1String("update") || action == QLatin1String("update-all") || action == QLatin1String("refresh")) { reloadCurrentView(); } }); } QString BrowsePage::navTitle() const { if (m_mode == Mode::Installed) return tr("Paquets installés"); if (m_mode == Mode::Updates) return tr("Mises à jour disponibles"); return QStringLiteral("Recherche"); } QString BrowsePage::navSubtitle() const { if (m_mode == Mode::Installed) return tr("Parcourez les paquets de votre système"); if (m_mode == Mode::Updates) return tr("Maintenez votre système à jour"); return tr("Trouvez et installez des paquets"); } void BrowsePage::refresh() { reloadCurrentView(); } void BrowsePage::showSearch() { setMode(Mode::Search); m_searchBar->setFocus(); } void BrowsePage::focusSearch() { setMode(Mode::Search); m_searchBar->setFocus(); m_searchBar->selectAll(); } void BrowsePage::showInstalled(const QString& select) { setMode(Mode::Installed); if (!select.isEmpty()) m_pendingSelect = select; start(core()->loadInstalled(), tr("Paquets installés")); } void BrowsePage::showUpdates() { setMode(Mode::Updates); // The update check is what the user came for: run it on every arrival on // the tab (pacman caches its queries for a few seconds, so a click right // after the dashboard loaded reuses that answer instead of respawning it). // The "searching" hint replaces the empty-state text for the duration, so // the tab never looks stuck on an empty table. ui::setEmptyStateText(m_tableStack, tr("Recherche des mises à jour en cours...\nLes résultats " "s'afficheront automatiquement.")); if (core()->busy()) { // Something else is loading (e.g. the dashboard asked for the counts): // the busyChanged handler below reloads when the core frees up. return; } start(core()->loadUpdates(), tr("Mises à jour")); } void BrowsePage::setMode(Mode mode) { m_mode = mode; m_searchRow->setVisible(mode == Mode::Search); m_filterRow->setVisible(mode == Mode::Installed); m_installButton->setVisible(mode == Mode::Search); // "Tout mettre à jour" acts on the whole list, so it is only offered on the // tab that displays pending updates; there it needs no row selection. if (m_updateAllButton) { m_updateAllButton->setVisible(mode == Mode::Updates); m_updateAllButton->setEnabled(mode == Mode::Updates && core()->hasAvailableProvider()); } // The updates tab rewrote the empty-state hint for its own states; restore // the generic one so the other tabs never show a stale updates message. if (mode != Mode::Updates) { ui::setEmptyStateText( m_tableStack, tr("Aucun paquet à afficher.\nLancez une recherche ou ouvrez la liste des " "paquets installés.")); } // Clear any stale selection and reset the details panel m_table->clearSelection(); resetDetails(); } void BrowsePage::reloadCurrentView() { if (m_mode == Mode::Installed) { start(core()->loadInstalled(), tr("Paquets installés")); } else if (m_mode == Mode::Updates) { start(core()->loadUpdates(), tr("Mises à jour")); } else if (!m_searchBar->text().trimmed().isEmpty()) { onSearch(); } } void BrowsePage::onSearch() { if (m_searchBar->text().trimmed().isEmpty()) { // Refocus the field instead of interrupting with a dialog. m_searchBar->setFocus(); emit message(tr("Entrez un terme de recherche.")); return; } m_table->setRowCount(0); m_results.clear(); start(core()->search(m_searchBar->text()), tr("Recherche")); } void BrowsePage::onFilterChanged(const QString& text) { if (m_mode != Mode::Installed) return; std::vector filtered; if (text.isEmpty()) { filtered = m_installed; } else { const QString needle = text.toLower(); for (const auto& pkg : m_installed) { if (QString::fromStdString(pkg.name).toLower().contains(needle)) { filtered.push_back(pkg); } } } populateTable(filtered); } void BrowsePage::populateTable(const std::vector& packages) { m_results = packages; ui::showEmptyState(m_tableStack, packages.empty()); { ui::TableFillBatch batch(m_table); m_table->setRowCount(0); m_table->setRowCount(static_cast(packages.size())); for (int i = 0; i < static_cast(packages.size()); ++i) { addRow(i, packages[i]); } } m_resultCount->setText(QString(tr("%1 paquet(s)")).arg(packages.size())); if (packages.empty()) { resetDetails(); return; } // If we came here for a specific package (dashboard history, or the one an // operation just touched), select it. if (!m_pendingSelect.isEmpty()) { const QString target = m_pendingSelect; m_pendingSelect.clear(); for (int i = 0; i < static_cast(m_results.size()); ++i) { if (QString::fromStdString(m_results[i].name) == target) { m_table->selectRow(i); onPackageSelected(i); // update the details panel immediately return; } } } m_table->selectRow(0); } // Appends an incoming search chunk to the rows already displayed, so the first // matches appear while the remaining providers are still searching. void BrowsePage::appendRows(const std::vector& packages) { const int startRow = static_cast(m_results.size()); m_results.insert(m_results.end(), packages.begin(), packages.end()); ui::TableFillBatch batch(m_table); m_table->setRowCount(static_cast(m_results.size())); for (int i = 0; i < static_cast(packages.size()); ++i) { addRow(startRow + i, packages[i]); } ui::showEmptyState(m_tableStack, m_results.empty()); m_resultCount->setText(QString(tr("%1 paquet(s)")).arg(m_results.size())); } void BrowsePage::addRow(int row, const PackageInfo& pkg) { QFont nameFont; nameFont.setWeight(QFont::DemiBold); QTableWidgetItem* nameItem = new QTableWidgetItem(QString::fromStdString(pkg.name)); nameItem->setFont(nameFont); m_table->setItem(row, 0, nameItem); m_table->setItem(row, 1, new QTableWidgetItem(QString::fromStdString(pkg.version))); m_table->setItem(row, 2, new QTableWidgetItem(pkg.repository.empty() ? QStringLiteral("—") : QString::fromStdString(pkg.repository))); QTableWidgetItem* providerItem = new QTableWidgetItem(ui::providersLabel(core()->plugins(), pkg)); providerItem->setForeground(QColor("#94a3b8")); m_table->setItem(row, 3, providerItem); QTableWidgetItem* statusItem = new QTableWidgetItem(ui::packageStatusText(pkg)); if (pkg.installed && pkg.updateAvailable) { statusItem->setForeground(QColor("#fbbf24")); statusItem->setIcon(ui::icon("arrow-up")); } else if (pkg.installed) { statusItem->setForeground(QColor("#34d399")); statusItem->setIcon(ui::icon("check")); } else { statusItem->setIcon(ui::icon("package")); } m_table->setItem(row, 4, statusItem); } void BrowsePage::onPackageSelected(int row) { if (row < 0 || row >= static_cast(m_results.size())) { resetDetails(); return; } m_selected = m_results[row]; updateDetails(m_selected); IPackageManager* provider = core()->providerFor(QString::fromStdString(m_selected.provider)); m_installButton->setEnabled(provider && provider->canInstall() && !m_selected.installed); m_removeButton->setEnabled(provider && provider->canRemove() && m_selected.installed); m_updateButton->setEnabled(provider && provider->canUpdate() && m_selected.updateAvailable); } void BrowsePage::updateDetails(const PackageInfo& pkg) { m_pkgName->setText(QString::fromStdString(pkg.name)); QString versionText = QString(tr("Version : %1")).arg(QString::fromStdString(pkg.version)); if (pkg.updateAvailable) versionText += tr(" (mise à jour disponible)"); m_pkgVersion->setText(versionText); m_pkgRepo->setText(pkg.repository.empty() ? tr("Dépôt : —") : QString(tr("Dépôt : %1")).arg(QString::fromStdString(pkg.repository))); m_pkgSize->setText(pkg.size.empty() ? tr("Taille : —") : QString(tr("Taille : %1")).arg(QString::fromStdString(pkg.size))); m_pkgProvider->setText( QString(tr("Fournisseur : %1")).arg(ui::providersLabel(core()->plugins(), pkg))); // A provider can hold several copies of the same application (flatpak: one // per installation, per remote or per branch). The row stands for the // application, so the copies it is made of are listed here — and the remove // button asks which of them to act on. if (pkg.instances.size() > 1) { QStringList copies; for (const PackageInstance& instance : pkg.instances) { copies << QString::fromStdString(instance.label); } m_pkgInstances->setText( QString(tr("%1 installation(s) :\n• %2")) .arg(copies.size()) .arg(copies.join(QStringLiteral("\n• ")))); m_pkgInstances->setVisible(true); } else { m_pkgInstances->setVisible(false); } // Rich details (dependencies, required-by, install date, update size): the // core queries pacman on demand and caches the answer, so selecting a row // stays instant even when pacman is slow. QString details = QString::fromStdString(pkg.description); const QString rich = core()->packageDetails(pkg); if (!rich.isEmpty()) { if (!details.isEmpty()) details += "\n\n"; details += rich; } m_pkgDescription->setText(details); } // Asks which copies of the selected application to remove, when the provider // holds it more than once (flatpak: a user and a system-wide installation, or // two remotes or branches of the same id). One line per copy, each with the // provider's own target kept aside, so the core is handed exactly what it must // act on. Returns nothing when the user cancels, and an empty list when they // ticked nothing — which is why the caller only asks when there are at least // two copies: with one, the row is the thing to remove. std::optional> BrowsePage::chooseInstancesToRemove() { QDialog dialog(this); dialog.setWindowTitle(tr("Supprimer %1").arg(QString::fromStdString(m_selected.name))); QVBoxLayout* layout = new QVBoxLayout(&dialog); layout->setSpacing(10); QLabel* prompt = new QLabel( tr("%1 est installé plusieurs fois. Choisissez les installations à " "supprimer.") .arg(QString::fromStdString(m_selected.name)), &dialog); prompt->setWordWrap(true); layout->addWidget(prompt); QListWidget* copies = new QListWidget(&dialog); for (const PackageInstance& instance : m_selected.instances) { QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(instance.label), copies); // The target never reaches the screen: it is the provider's own syntax, // kept beside the label and handed straight back. item->setData(Qt::UserRole, QString::fromStdString(instance.target)); item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(Qt::Unchecked); } layout->addWidget(copies); QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog); buttons->button(QDialogButtonBox::Ok)->setText(tr("Supprimer")); buttons->button(QDialogButtonBox::Cancel)->setText(tr("Annuler")); connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); layout->addWidget(buttons); if (dialog.exec() != QDialog::Accepted) return std::nullopt; std::vector targets; for (int row = 0; row < copies->count(); ++row) { if (copies->item(row)->checkState() != Qt::Checked) continue; targets.push_back(copies->item(row)->data(Qt::UserRole).toString().toStdString()); } return targets; } void BrowsePage::resetDetails() { m_selected = PackageInfo{}; m_pkgName->setText(tr("Sélectionnez un paquet")); m_pkgVersion->setText(""); m_pkgRepo->setText("—"); m_pkgSize->setText("—"); m_pkgProvider->setText("—"); m_pkgInstances->setVisible(false); m_pkgDescription->setText(""); m_installButton->setEnabled(false); m_removeButton->setEnabled(false); m_updateButton->setEnabled(false); } void BrowsePage::exportCsv() { if (m_results.empty()) { emit message(tr("Aucune liste à exporter.")); return; } const QString path = QFileDialog::getSaveFileName( this, tr("Exporter la liste des paquets"), QDir::homePath() + "/pacmanager-paquets.csv", "CSV (*.csv)"); if (path.isEmpty()) return; QFile file(path); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Export"), QString(tr("Impossible d'écrire dans %1")).arg(path)); return; } QTextStream out(&file); out << "Nom;Version;Depot;Fournisseur;Statut\n"; for (const auto& pkg : m_results) { out << ui::csvField(QString::fromStdString(pkg.name)) << ';' << ui::csvField(QString::fromStdString(pkg.version)) << ';' << ui::csvField(QString::fromStdString(pkg.repository)) << ';' << ui::csvField(ui::providersLabel(core()->plugins(), pkg)) << ';' << ui::csvField(ui::packageStatusText(pkg)) << '\n'; } emit message(QString(tr("%1 paquet(s) exporté(s) vers %2")) .arg(m_results.size()) .arg(path)); }