File: Requirements.cpp

package info (click to toggle)
apper 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,868 kB
  • sloc: cpp: 14,381; xml: 941; makefile: 15; sh: 4
file content (265 lines) | stat: -rw-r--r-- 10,700 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/***************************************************************************
 *   Copyright (C) 2008-2018 by Daniel Nicoletti                           *
 *   dantti12@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; see the file COPYING. If not, write to       *
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 *   Boston, MA 02110-1301, USA.                                           *
 ***************************************************************************/

#include "Requirements.h"
#include "ui_Requirements.h"

#include "PkIcons.h"
#include "PackageModel.h"
#include "ApplicationSortFilterModel.h"

#include <QToolButton>
#include <QPushButton>
#include <QLoggingCategory>
#include <KFormat>
#include <KConfig>
#include <KConfigGroup>

Requirements::Requirements(PackageModel *model, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Requirements)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    connect(ui->confirmCB, &QCheckBox::toggled, this, &Requirements::confirmCBChanged);

    ApplicationSortFilterModel *proxy = new ApplicationSortFilterModel(this);
    proxy->setSourceModel(model);
    ui->packageView->setModel(proxy);
    ui->packageView->header()->setSectionResizeMode(PackageModel::NameCol, QHeaderView::ResizeToContents);
    ui->packageView->header()->hideSection(PackageModel::ActionCol);
    ui->packageView->header()->hideSection(PackageModel::ArchCol);
    ui->packageView->header()->hideSection(PackageModel::CurrentVersionCol);
    ui->packageView->header()->hideSection(PackageModel::OriginCol);
    ui->packageView->header()->hideSection(PackageModel::SizeCol);

    setWindowTitle(i18n("Additional changes"));
    setWindowIcon(QIcon::fromTheme(QLatin1String("dialog-warning")));
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Continue"));

    // restore size
    setMinimumSize(QSize(600,480));
//    setInitialSize(QSize(600,600));
    KConfig config(QLatin1String("apper"));
    KConfigGroup requirementsDialog(&config, "requirementsDialog");
//    restoreGeometry(requirementsDialog.readEntry("geometry").toByteArray());
//    restoreDialogSize(requirementsDialog);

    ui->downloadT->hide();
    ui->downloadI->hide();
    ui->downloadI->setPixmap(QIcon::fromTheme(QLatin1String("download")).pixmap(32, 32));

    m_buttonGroup = new QButtonGroup(this);
    connect(m_buttonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &Requirements::actionClicked);

    int count = 0;
    if (int c = model->countInfo(Transaction::InfoRemoving)) {
        auto button = new QToolButton(this);
        button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        button->setCheckable(true);
        button->setAutoRaise(true);
        button->setIconSize(QSize(32, 32));
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        button->setText(i18np("1 package to remove", "%1 packages to remove", c));
        button->setIcon(PkIcons::actionIcon(Transaction::RoleRemovePackages));
        m_buttonGroup->addButton(button, Transaction::InfoRemoving);
        ui->verticalLayout->insertWidget(count++, button);

        m_hideAutoConfirm = true;
    }

    if (int c = model->countInfo(Transaction::InfoDowngrading)) {
        auto button = new QToolButton(this);
        button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        button->setCheckable(true);
        button->setAutoRaise(true);
        button->setIconSize(QSize(32, 32));
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        button->setText(i18np("1 package to downgrade", "%1 packages to downgrade", c));
        button->setIcon(PkIcons::actionIcon(Transaction::RoleRepairSystem));
        m_buttonGroup->addButton(button, Transaction::InfoDowngrading);
        ui->verticalLayout->insertWidget(count++, button);

        m_hideAutoConfirm = true;
    }

    if (int c = model->countInfo(Transaction::InfoReinstalling)) {
        auto button = new QToolButton(this);
        button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        button->setCheckable(true);
        button->setAutoRaise(true);
        button->setIconSize(QSize(32, 32));
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        button->setText(i18np("1 package to reinstall", "%1 packages to reinstall", c));
        button->setIcon(PkIcons::actionIcon(Transaction::RoleRemovePackages));
        m_buttonGroup->addButton(button, Transaction::InfoReinstalling);
        ui->verticalLayout->insertWidget(count++, button);
    }

    if (int c = model->countInfo(Transaction::InfoInstalling)) {
        auto button = new QToolButton(this);
        button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        button->setCheckable(true);
        button->setAutoRaise(true);
        button->setIconSize(QSize(32, 32));
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        button->setText(i18np("1 package to install", "%1 packages to install", c));
        button->setIcon(PkIcons::actionIcon(Transaction::RoleInstallPackages));
        m_buttonGroup->addButton(button, Transaction::InfoInstalling);
        ui->verticalLayout->insertWidget(count++, button);
    }

    if (int c = model->countInfo(Transaction::InfoUpdating)) {
        auto button = new QToolButton(this);
        button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        button->setCheckable(true);
        button->setAutoRaise(true);
        button->setIconSize(QSize(32, 32));
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        button->setText(i18np("1 package to update", "%1 packages to update", c));
        button->setIcon(PkIcons::actionIcon(Transaction::RoleUpdatePackages));
        m_buttonGroup->addButton(button, Transaction::InfoUpdating);
        ui->verticalLayout->insertWidget(count++, button);
    }

    if (int c = model->countInfo(Transaction::InfoUntrusted)) {
        m_untrustedButton = new QToolButton(this);
        m_untrustedButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
        m_untrustedButton->setCheckable(true);
        m_untrustedButton->setAutoRaise(true);
        m_untrustedButton->setIconSize(QSize(32, 32));
        m_untrustedButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        m_untrustedButton->setText(i18np("1 untrusted package", "%1 untrusted packages", c));
        m_untrustedButton->setIcon(QIcon::fromTheme(QLatin1String("security-low")));
        m_untrustedButton->setVisible(false);
        ui->verticalLayout->insertWidget(count++, m_untrustedButton);
    }

    if (!m_buttonGroup->buttons().isEmpty()) {
        m_buttonGroup->buttons().first()->click();

        if (m_hideAutoConfirm) {
            ui->confirmCB->setVisible(false);
        } else {
            // if the confirmCB is visible means that we can skip this
            // dialog, but only if the user previusly set so
            ui->confirmCB->setChecked(requirementsDialog.readEntry("autoConfirm", false));
        }
    } else if (m_untrustedButton) {
        showUntrustedButton();
    } else {
        // set this as false so the dialog is not shown
        m_shouldShow = false;
    }
}

Requirements::~Requirements()
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup requirementsDialog(&config, "requirementsDialog");
    requirementsDialog.writeEntry("geometry", saveGeometry());

    delete ui;
}

bool Requirements::embedded() const
{
    return m_embed;
}

void Requirements::setEmbedded(bool embedded)
{
    m_embed = embedded;
    ui->label->setVisible(!embedded);
}

void Requirements::setDownloadSizeRemaining(qulonglong size)
{
    if (size) {
        QString text = i18nc("how many bytes are required for download",
                             "Need to get %1 of archives",
                             KFormat().formatByteSize(size));
        ui->downloadT->setText(text);
        ui->downloadT->show();
        ui->downloadI->show();
    } else {
        ui->downloadT->hide();
        ui->downloadI->hide();
    }
}

bool Requirements::trusted() const
{
    // There are untrusted packages if the button was created...
    return !m_untrustedButton;
}

bool Requirements::shouldShow() const
{
    return (m_shouldShow && !ui->confirmCB->isChecked());
}

void Requirements::slotButtonClicked(int)
{
    // FIXME
//    if (button == KDialog::Ok &&
//            m_untrustedButton &&
//            !m_untrustedButton->isVisible()) {
//        showUntrustedButton();
//    } else {
//        KDialog::slotButtonClicked(button);
//    }
}

void Requirements::confirmCBChanged(bool checked)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup requirementsDialog(&config, "requirementsDialog");

    if (!m_hideAutoConfirm) {
        requirementsDialog.writeEntry("autoConfirm", checked);
    }
    config.sync();
}

void Requirements::actionClicked(int type)
{
    auto proxy = qobject_cast<ApplicationSortFilterModel*>(ui->packageView->model());
    proxy->setInfoFilter(static_cast<Transaction::Info>(type));
}

void Requirements::showUntrustedButton()
{
    // Clear the other buttons
    qDeleteAll(m_buttonGroup->buttons());

    // Hide the auto confirm button since we will be showing this dialog anyway
    ui->confirmCB->setVisible(false);

    ui->label->setText(i18n("You are about to install unsigned packages that can compromise your system, "
                            "as it is impossible to verify if the software came from a trusted source."));
    m_untrustedButton->setVisible(true);
    m_buttonGroup->addButton(m_untrustedButton, Transaction::InfoUntrusted);
    m_untrustedButton->click();
}

#include "moc_Requirements.cpp"