File indexing completed on 2024-03-24 17:22:07

0001 /***************************************************************************
0002  *   Copyright (C) 2008-2018 by Daniel Nicoletti                           *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "Requirements.h"
0022 #include "ui_Requirements.h"
0023 
0024 #include "PkIcons.h"
0025 #include "PackageModel.h"
0026 #include "ApplicationSortFilterModel.h"
0027 
0028 #include <QToolButton>
0029 #include <QPushButton>
0030 #include <QLoggingCategory>
0031 #include <KFormat>
0032 #include <KConfig>
0033 #include <KConfigGroup>
0034 
0035 Requirements::Requirements(PackageModel *model, QWidget *parent) :
0036     QDialog(parent),
0037     ui(new Ui::Requirements)
0038 {
0039     ui->setupUi(this);
0040 
0041     setAttribute(Qt::WA_DeleteOnClose);
0042 
0043     connect(ui->confirmCB, &QCheckBox::toggled, this, &Requirements::confirmCBChanged);
0044 
0045     ApplicationSortFilterModel *proxy = new ApplicationSortFilterModel(this);
0046     proxy->setSourceModel(model);
0047     ui->packageView->setModel(proxy);
0048     ui->packageView->header()->setSectionResizeMode(PackageModel::NameCol, QHeaderView::ResizeToContents);
0049     ui->packageView->header()->hideSection(PackageModel::ActionCol);
0050     ui->packageView->header()->hideSection(PackageModel::ArchCol);
0051     ui->packageView->header()->hideSection(PackageModel::CurrentVersionCol);
0052     ui->packageView->header()->hideSection(PackageModel::OriginCol);
0053     ui->packageView->header()->hideSection(PackageModel::SizeCol);
0054 
0055     setWindowTitle(i18n("Additional changes"));
0056     setWindowIcon(QIcon::fromTheme(QLatin1String("dialog-warning")));
0057     ui->buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Continue"));
0058 
0059     // restore size
0060     setMinimumSize(QSize(600,480));
0061 //    setInitialSize(QSize(600,600));
0062     KConfig config(QLatin1String("apper"));
0063     KConfigGroup requirementsDialog(&config, "requirementsDialog");
0064 //    restoreGeometry(requirementsDialog.readEntry("geometry").toByteArray());
0065 //    restoreDialogSize(requirementsDialog);
0066 
0067     ui->downloadT->hide();
0068     ui->downloadI->hide();
0069     ui->downloadI->setPixmap(QIcon::fromTheme(QLatin1String("download")).pixmap(32, 32));
0070 
0071     m_buttonGroup = new QButtonGroup(this);
0072     connect(m_buttonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &Requirements::actionClicked);
0073 
0074     int count = 0;
0075     if (int c = model->countInfo(Transaction::InfoRemoving)) {
0076         auto button = new QToolButton(this);
0077         button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
0078         button->setCheckable(true);
0079         button->setAutoRaise(true);
0080         button->setIconSize(QSize(32, 32));
0081         button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0082         button->setText(i18np("1 package to remove", "%1 packages to remove", c));
0083         button->setIcon(PkIcons::actionIcon(Transaction::RoleRemovePackages));
0084         m_buttonGroup->addButton(button, Transaction::InfoRemoving);
0085         ui->verticalLayout->insertWidget(count++, button);
0086 
0087         m_hideAutoConfirm = true;
0088     }
0089 
0090     if (int c = model->countInfo(Transaction::InfoDowngrading)) {
0091         auto button = new QToolButton(this);
0092         button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
0093         button->setCheckable(true);
0094         button->setAutoRaise(true);
0095         button->setIconSize(QSize(32, 32));
0096         button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0097         button->setText(i18np("1 package to downgrade", "%1 packages to downgrade", c));
0098         button->setIcon(PkIcons::actionIcon(Transaction::RoleRepairSystem));
0099         m_buttonGroup->addButton(button, Transaction::InfoDowngrading);
0100         ui->verticalLayout->insertWidget(count++, button);
0101 
0102         m_hideAutoConfirm = true;
0103     }
0104 
0105     if (int c = model->countInfo(Transaction::InfoReinstalling)) {
0106         auto button = new QToolButton(this);
0107         button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
0108         button->setCheckable(true);
0109         button->setAutoRaise(true);
0110         button->setIconSize(QSize(32, 32));
0111         button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0112         button->setText(i18np("1 package to reinstall", "%1 packages to reinstall", c));
0113         button->setIcon(PkIcons::actionIcon(Transaction::RoleRemovePackages));
0114         m_buttonGroup->addButton(button, Transaction::InfoReinstalling);
0115         ui->verticalLayout->insertWidget(count++, button);
0116     }
0117 
0118     if (int c = model->countInfo(Transaction::InfoInstalling)) {
0119         auto button = new QToolButton(this);
0120         button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
0121         button->setCheckable(true);
0122         button->setAutoRaise(true);
0123         button->setIconSize(QSize(32, 32));
0124         button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0125         button->setText(i18np("1 package to install", "%1 packages to install", c));
0126         button->setIcon(PkIcons::actionIcon(Transaction::RoleInstallPackages));
0127         m_buttonGroup->addButton(button, Transaction::InfoInstalling);
0128         ui->verticalLayout->insertWidget(count++, button);
0129     }
0130 
0131     if (int c = model->countInfo(Transaction::InfoUpdating)) {
0132         auto button = new QToolButton(this);
0133         button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
0134         button->setCheckable(true);
0135         button->setAutoRaise(true);
0136         button->setIconSize(QSize(32, 32));
0137         button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0138         button->setText(i18np("1 package to update", "%1 packages to update", c));
0139         button->setIcon(PkIcons::actionIcon(Transaction::RoleUpdatePackages));
0140         m_buttonGroup->addButton(button, Transaction::InfoUpdating);
0141         ui->verticalLayout->insertWidget(count++, button);
0142     }
0143 
0144     if (int c = model->countInfo(Transaction::InfoUntrusted)) {
0145         m_untrustedButton = new QToolButton(this);
0146         m_untrustedButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
0147         m_untrustedButton->setCheckable(true);
0148         m_untrustedButton->setAutoRaise(true);
0149         m_untrustedButton->setIconSize(QSize(32, 32));
0150         m_untrustedButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0151         m_untrustedButton->setText(i18np("1 untrusted package", "%1 untrusted packages", c));
0152         m_untrustedButton->setIcon(QIcon::fromTheme(QLatin1String("security-low")));
0153         m_untrustedButton->setVisible(false);
0154         ui->verticalLayout->insertWidget(count++, m_untrustedButton);
0155     }
0156 
0157     if (!m_buttonGroup->buttons().isEmpty()) {
0158         m_buttonGroup->buttons().first()->click();
0159 
0160         if (m_hideAutoConfirm) {
0161             ui->confirmCB->setVisible(false);
0162         } else {
0163             // if the confirmCB is visible means that we can skip this
0164             // dialog, but only if the user previusly set so
0165             ui->confirmCB->setChecked(requirementsDialog.readEntry("autoConfirm", false));
0166         }
0167     } else if (m_untrustedButton) {
0168         showUntrustedButton();
0169     } else {
0170         // set this as false so the dialog is not shown
0171         m_shouldShow = false;
0172     }
0173 }
0174 
0175 Requirements::~Requirements()
0176 {
0177     KConfig config(QLatin1String("apper"));
0178     KConfigGroup requirementsDialog(&config, "requirementsDialog");
0179     requirementsDialog.writeEntry("geometry", saveGeometry());
0180 
0181     delete ui;
0182 }
0183 
0184 bool Requirements::embedded() const
0185 {
0186     return m_embed;
0187 }
0188 
0189 void Requirements::setEmbedded(bool embedded)
0190 {
0191     m_embed = embedded;
0192     ui->label->setVisible(!embedded);
0193 }
0194 
0195 void Requirements::setDownloadSizeRemaining(qulonglong size)
0196 {
0197     if (size) {
0198         QString text = i18nc("how many bytes are required for download",
0199                              "Need to get %1 of archives",
0200                              KFormat().formatByteSize(size));
0201         ui->downloadT->setText(text);
0202         ui->downloadT->show();
0203         ui->downloadI->show();
0204     } else {
0205         ui->downloadT->hide();
0206         ui->downloadI->hide();
0207     }
0208 }
0209 
0210 bool Requirements::trusted() const
0211 {
0212     // There are untrusted packages if the button was created...
0213     return !m_untrustedButton;
0214 }
0215 
0216 bool Requirements::shouldShow() const
0217 {
0218     return (m_shouldShow && !ui->confirmCB->isChecked());
0219 }
0220 
0221 void Requirements::slotButtonClicked(int)
0222 {
0223     // FIXME
0224 //    if (button == KDialog::Ok &&
0225 //            m_untrustedButton &&
0226 //            !m_untrustedButton->isVisible()) {
0227 //        showUntrustedButton();
0228 //    } else {
0229 //        KDialog::slotButtonClicked(button);
0230 //    }
0231 }
0232 
0233 void Requirements::confirmCBChanged(bool checked)
0234 {
0235     KConfig config(QLatin1String("apper"));
0236     KConfigGroup requirementsDialog(&config, "requirementsDialog");
0237 
0238     if (!m_hideAutoConfirm) {
0239         requirementsDialog.writeEntry("autoConfirm", checked);
0240     }
0241     config.sync();
0242 }
0243 
0244 void Requirements::actionClicked(int type)
0245 {
0246     auto proxy = qobject_cast<ApplicationSortFilterModel*>(ui->packageView->model());
0247     proxy->setInfoFilter(static_cast<Transaction::Info>(type));
0248 }
0249 
0250 void Requirements::showUntrustedButton()
0251 {
0252     // Clear the other buttons
0253     qDeleteAll(m_buttonGroup->buttons());
0254 
0255     // Hide the auto confirm button since we will be showing this dialog anyway
0256     ui->confirmCB->setVisible(false);
0257 
0258     ui->label->setText(i18n("You are about to install unsigned packages that can compromise your system, "
0259                             "as it is impossible to verify if the software came from a trusted source."));
0260     m_untrustedButton->setVisible(true);
0261     m_buttonGroup->addButton(m_untrustedButton, Transaction::InfoUntrusted);
0262     m_untrustedButton->click();
0263 }
0264 
0265 #include "moc_Requirements.cpp"