File: Updater.cpp

package info (click to toggle)
apper 1.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,244 kB
  • sloc: cpp: 14,383; xml: 970; makefile: 13; sh: 4
file content (351 lines) | stat: -rw-r--r-- 13,731 bytes parent folder | download | duplicates (2)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/***************************************************************************
 *   Copyright (C) 2008-2011 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 "Updater.h"
#include "ui_Updater.h"

#include "UpdateDetails.h"
#include "DistroUpgrade.h"
#include "CheckableHeader.h"

#include <QMenu>
//#include <KGenericFactory>
#include <KPixmapSequence>
#include <KAboutData>
#include <KIconLoader>
#include <KConfigGroup>

#include <ApplicationsDelegate.h>
#include <PkStrings.h>
#include <PkIcons.h>
#include <PackageModel.h>
#include <ApplicationSortFilterModel.h>

#include <QDBusConnection>

#include <KConfig>
#include <KFormat>
#include <KMessageBox>
#include <QLoggingCategory>

#include <Daemon>

Q_DECLARE_LOGGING_CATEGORY(APPER)

Updater::Updater(Transaction::Roles roles, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Updater),
    m_roles(roles)
{
    ui->setupUi(this);
    updatePallete();
//    connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
//            this, SLOT(updatePallete()));

    m_updatesModel = new PackageModel(this);
    m_updatesModel->setCheckable(true);
    auto proxyModel = new ApplicationSortFilterModel(this);
    proxyModel->setSourceModel(m_updatesModel);
    ui->packageView->setModel(proxyModel);

    m_delegate = new ApplicationsDelegate(ui->packageView);
    m_delegate->setCheckable(true);
    ui->packageView->setItemDelegate(m_delegate);
    ui->packageView->sortByColumn(PackageModel::NameCol, Qt::AscendingOrder);
    connect(m_updatesModel, &PackageModel::changed, this, &Updater::checkEnableUpdateButton);

    //initialize the model, delegate, client and  connect it's signals
    m_header = new CheckableHeader(Qt::Horizontal, this);
    connect(m_header, &CheckableHeader::toggled, m_updatesModel, &PackageModel::setAllChecked);
    m_header->setCheckBoxVisible(false);
    m_header->setDefaultAlignment(Qt::AlignCenter);
    ui->packageView->setHeaderHidden(false);
    ui->packageView->setHeader(m_header);

    // This must be set AFTER the model is set, otherwise it doesn't work
    m_header->setSectionResizeMode(PackageModel::NameCol, QHeaderView::Stretch);
    m_header->setSectionResizeMode(PackageModel::VersionCol, QHeaderView::ResizeToContents);
    m_header->setSectionResizeMode(PackageModel::CurrentVersionCol, QHeaderView::ResizeToContents);
    m_header->setSectionResizeMode(PackageModel::ArchCol, QHeaderView::ResizeToContents);
    m_header->setSectionResizeMode(PackageModel::OriginCol, QHeaderView::ResizeToContents);
    m_header->setSectionResizeMode(PackageModel::SizeCol, QHeaderView::ResizeToContents);
    m_header->setStretchLastSection(false);

    // Setup the busy cursor
    m_busySeq = new KPixmapSequenceOverlayPainter(this);
    m_busySeq->setSequence(KIconLoader::global()->loadPixmapSequence(QLatin1String("process-working"), KIconLoader::SizeSmallMedium));
    m_busySeq->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_busySeq->setWidget(ui->packageView->viewport());

    // hide distro Upgrade container and line
    ui->distroUpgrade->hide();

    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "UpdateView");

    // versions
    ui->packageView->header()->setSectionHidden(PackageModel::VersionCol, true);
    m_showPackageVersion = new QAction(i18n("Show Versions"), this);
    m_showPackageVersion->setCheckable(true);
    connect(m_showPackageVersion, &QAction::toggled, this, &Updater::showVersions);
    m_showPackageVersion->setChecked(viewGroup.readEntry("ShowVersions", true));

    // versions
    ui->packageView->header()->setSectionHidden(PackageModel::CurrentVersionCol, true);
    m_showPackageCurrentVersion = new QAction(i18n("Show Current Versions"), this);
    m_showPackageCurrentVersion->setCheckable(true);
    connect(m_showPackageCurrentVersion, &QAction::toggled, this, &Updater::showCurrentVersions);
    m_showPackageCurrentVersion->setChecked(viewGroup.readEntry("ShowCurrentVersions", false));

    // Arch
    ui->packageView->header()->setSectionHidden(PackageModel::ArchCol, true);
    m_showPackageArch = new QAction(i18n("Show Architectures"), this);
    m_showPackageArch->setCheckable(true);
    connect(m_showPackageArch, &QAction::toggled, this, &Updater::showArchs);
    m_showPackageArch->setChecked(viewGroup.readEntry("ShowArchs", false));

    // Origin
    ui->packageView->header()->setSectionHidden(PackageModel::OriginCol, true);
    m_showPackageOrigin = new QAction(i18n("Show Origins"), this);
    m_showPackageOrigin->setCheckable(true);
    connect(m_showPackageOrigin, &QAction::toggled, this, &Updater::showOrigins);
    m_showPackageOrigin->setChecked(viewGroup.readEntry("ShowOrigins", false));

    // Sizes
    ui->packageView->header()->setSectionHidden(PackageModel::SizeCol, true);
    m_showPackageSize = new QAction(i18n("Show Sizes"), this);
    m_showPackageSize->setCheckable(true);
    connect(m_showPackageSize, &QAction::toggled, this, &Updater::showSizes);
    m_showPackageSize->setChecked(viewGroup.readEntry("ShowSizes", true));
}

Updater::~Updater()
{
    delete ui;
}

void Updater::showVersions(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "UpdateView");
    viewGroup.writeEntry("ShowVersions", enabled);
    ui->packageView->header()->setSectionHidden(PackageModel::VersionCol, !enabled);
}

void Updater::showCurrentVersions(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "UpdateView");
    viewGroup.writeEntry("ShowCurrentVersions", enabled);
    ui->packageView->header()->setSectionHidden(PackageModel::CurrentVersionCol, !enabled);
    if (enabled) {
        m_updatesModel->fetchCurrentVersions();
    }
}

void Updater::showArchs(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "UpdateView");
    viewGroup.writeEntry("ShowArchs", enabled);
    ui->packageView->header()->setSectionHidden(PackageModel::ArchCol, !enabled);
}

void Updater::showOrigins(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "UpdateView");
    viewGroup.writeEntry("showOrigins", enabled);
    ui->packageView->header()->setSectionHidden(PackageModel::OriginCol, !enabled);
}

void Updater::showSizes(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "UpdateView");
    viewGroup.writeEntry("ShowSizes", enabled);
    ui->packageView->header()->setSectionHidden(PackageModel::SizeCol, !enabled);
    if (enabled) {
        m_updatesModel->fetchSizes();
    }
}

void Updater::updatePallete()
{
    QPalette pal;
    pal.setColor(QPalette::Window, pal.base().color());
    pal.setColor(QPalette::WindowText, pal.text().color());
    ui->backgroundFrame->setPalette(pal);
}

void Updater::on_packageView_clicked(const QModelIndex &index)
{
    QString pkgId = index.data(PackageModel::IdRole).toString();
    auto pkgInfo = index.data(PackageModel::InfoRole).value<Transaction::Info>();
    ui->updateDetails->setPackage(pkgId, pkgInfo);
}

//TODO: We should add some kind of configuration to let users show unstable distributions
//That way, by default, users only see stable ones.
void Updater::distroUpgrade(PackageKit::Transaction::DistroUpgrade type, const QString &name, const QString &description)
{
    // TODO name should be used to do a upgrade to a different type
    Q_UNUSED(name)
    if (type != Transaction::DistroUpgradeStable) {
        // Ignore unstable distros upgrades for now
        return;
    }

    ui->distroUpgrade->setName(description);
    ui->distroUpgrade->animatedShow();
}

bool Updater::hasChanges() const
{
    return m_updatesModel->hasChanges();
}

void Updater::checkEnableUpdateButton()
{
    qCDebug(APPER) << "updates has changes" << hasChanges();
    emit changed(hasChanges());
    int selectedSize = m_updatesModel->selectedPackagesToInstall().size();
    int updatesSize = m_updatesModel->rowCount();
    if (selectedSize == 0) {
        m_header->setCheckState(Qt::Unchecked);
    } else if (selectedSize == updatesSize) {
        m_header->setCheckState(Qt::Checked);
    } else {
        m_header->setCheckState(Qt::PartiallyChecked);
    }

    unsigned long dwSize = m_updatesModel->downloadSize();
    if (dwSize) {
        emit downloadSize(i18n("Estimated download size: %1",
                               KFormat().formatByteSize(dwSize)));
    } else {
        emit downloadSize(QString());
    }
    // if we don't have any upates let's disable the button
    m_header->setCheckBoxVisible(m_updatesModel->rowCount() != 0);
    ui->packageView->setHeaderHidden(m_updatesModel->rowCount() == 0);
}

void Updater::load()
{
    // set focus on the updates view
    ui->packageView->setFocus(Qt::OtherFocusReason);
    emit downloadSize(QString());
    // If the model already has some packages
    // let's just clear the selection
    if (m_updatesModel->rowCount()) {
        m_updatesModel->setAllChecked(false);
    } else {
        getUpdates();
    }
}

void Updater::getUpdatesFinished()
{
    m_updatesT = 0;
    m_updatesModel->clearSelectedNotPresent();
    checkEnableUpdateButton();
    if (m_updatesModel->rowCount() == 0) {
        // Set the info page
        ui->stackedWidget->setCurrentIndex(1);
        uint lastTime = Daemon::global()->getTimeSinceAction(Transaction::RoleRefreshCache);
        ui->titleL->setText(PkStrings::lastCacheRefreshTitle(lastTime));
        ui->descriptionL->setText(PkStrings::lastCacheRefreshSubTitle(lastTime));
        ui->iconL->setPixmap(QIcon::fromTheme(PkIcons::lastCacheRefreshIconName(lastTime)).pixmap(128, 128));
    }
}

QStringList Updater::packagesToUpdate() const
{
    return m_updatesModel->selectedPackagesToInstall();
}

void Updater::getUpdates()
{
    if (m_updatesT) {
        // There is a getUpdates running ignore this call
        return;
    }

    if (ui->stackedWidget->currentIndex() != 0) {
        ui->stackedWidget->setCurrentIndex(0);
    }

    // clears the model
    ui->packageView->setHeaderHidden(true);
    m_updatesModel->clear();
    ui->updateDetails->hide();
    m_updatesT = Daemon::getUpdates();
    connect(m_updatesT, &Transaction::package, m_updatesModel, &PackageModel::addSelectedPackage);
    connect(m_updatesT, &Transaction::errorCode, this, &Updater::errorCode);
    connect(m_updatesT, &Transaction::finished, m_busySeq, &KPixmapSequenceOverlayPainter::stop);
    connect(m_updatesT, &Transaction::finished, m_updatesModel, &PackageModel::finished);

    // This is required to estimate download size
    connect(m_updatesT, &Transaction::finished, m_updatesModel, &PackageModel::fetchSizes);

    if (m_showPackageCurrentVersion->isChecked()) {
        connect(m_updatesT, &Transaction::finished, m_updatesModel, &PackageModel::fetchCurrentVersions);
    }
    connect(m_updatesT, &Transaction::finished, this, &Updater::getUpdatesFinished);
    m_busySeq->start();

    // Hide the distribution upgrade information
    ui->distroUpgrade->animatedHide();

    if (m_roles & Transaction::RoleGetDistroUpgrades) {
        // Check for distribution Upgrades
        Transaction *t = Daemon::getDistroUpgrades();
        connect(m_updatesT, &Transaction::distroUpgrade, this, &Updater::distroUpgrade);
        connect(m_updatesT, &Transaction::finished, t, &Transaction::deleteLater);
    }
}

void Updater::on_packageView_customContextMenuRequested(const QPoint &pos)
{
    auto menu = new QMenu(this);
    menu->addAction(m_showPackageVersion);
    menu->addAction(m_showPackageCurrentVersion);
    menu->addAction(m_showPackageArch);
    menu->addAction(m_showPackageOrigin);
    menu->addAction(m_showPackageSize);
    QAction *action;
    action = menu->addAction(i18n("Check for new updates"));
    action->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
    connect(action, &QAction::triggered, this, &Updater::refreshCache);
    menu->exec(ui->packageView->viewport()->mapToGlobal(pos));
    delete menu;
}

void Updater::errorCode(PackageKit::Transaction::Error error, const QString &details)
{
    KMessageBox::detailedError(this,
                               PkStrings::errorMessage(error),
                               details,
                               PkStrings::error(error),
                               KMessageBox::Notify);
}

#include "moc_Updater.cpp"