~ubuntu-branches/debian/stretch/apper/stretch

« back to all changes in this revision

Viewing changes to libapper/PkTransactionWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Matthias Klumpp
  • Date: 2013-06-24 14:32:36 UTC
  • mfrom: (8.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130624143236-8259lpdjgnrjxiik
Tags: 0.8.0-2
* Upload to unstable (Closes: #696585, #697996, #708090)
* Add patch to compile against new QPK API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008-2011 by Daniel Nicoletti                           *
 
3
 *   dantti12@gmail.com                                                    *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; see the file COPYING. If not, write to       *
 
17
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 
18
 *   Boston, MA 02110-1301, USA.                                           *
 
19
 ***************************************************************************/
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include "PkTransactionWidget.h"
 
24
#include "ui_PkTransactionWidget.h"
 
25
 
 
26
#include <KLocale>
 
27
#include <KMessageBox>
 
28
#include <KPushButton>
 
29
#include <KService>
 
30
#include <KPixmapSequence>
 
31
#include <KPixmapSequenceOverlayPainter>
 
32
 
 
33
#include <KDebug>
 
34
 
 
35
#include <QStringBuilder>
 
36
#include <QPropertyAnimation>
 
37
#include <QtDBus/QDBusMessage>
 
38
#include <QtDBus/QDBusConnection>
 
39
#include <QScrollBar>
 
40
 
 
41
#include <Daemon>
 
42
 
 
43
#include "Enum.h"
 
44
#include "PkStrings.h"
 
45
#include "RepoSig.h"
 
46
#include "LicenseAgreement.h"
 
47
#include "PkIcons.h"
 
48
#include "ApplicationLauncher.h"
 
49
#include "Requirements.h"
 
50
#include "PkTransaction.h"
 
51
#include "TransactionDelegate.h"
 
52
#include "PkTransactionProgressModel.h"
 
53
#include "PackageModel.h"
 
54
 
 
55
class PkTransactionWidgetPrivate
 
56
{
 
57
public:
 
58
    ApplicationLauncher *launcher;
 
59
    Transaction::Role role;
 
60
    KPixmapSequenceOverlayPainter *busySeq;
 
61
};
 
62
 
 
63
PkTransactionWidget::PkTransactionWidget(QWidget *parent) :
 
64
    QWidget(parent),
 
65
    m_trans(0),
 
66
    m_keepScrollBarAtBottom(true),
 
67
    m_handlingActionRequired(false),
 
68
    m_showingError(false),
 
69
    m_status(Transaction::StatusUnknown),
 
70
    ui(new Ui::PkTransactionWidget),
 
71
    d(new PkTransactionWidgetPrivate)
 
72
{
 
73
    ui->setupUi(this);
 
74
 
 
75
    // Setup the animation sequence
 
76
    d->busySeq = new KPixmapSequenceOverlayPainter(this);
 
77
    d->busySeq->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
 
78
    d->busySeq->setWidget(ui->label);
 
79
    ui->label->clear();
 
80
 
 
81
    // Connect stuff from the progressView
 
82
    QScrollBar *scrollBar = ui->progressView->verticalScrollBar();
 
83
    connect(scrollBar, SIGNAL(sliderMoved(int)),
 
84
            this, SLOT(followBottom(int)));
 
85
    connect(scrollBar, SIGNAL(valueChanged(int)),
 
86
            this, SLOT(followBottom(int)));
 
87
    connect(scrollBar, SIGNAL(rangeChanged(int,int)),
 
88
            this, SLOT(rangeChanged(int,int)));
 
89
 
 
90
    ui->progressView->setItemDelegate(new TransactionDelegate(this));
 
91
    
 
92
    connect(ui->cancelButton, SIGNAL(rejected()), this, SLOT(cancel()));
 
93
}
 
94
 
 
95
PkTransactionWidget::~PkTransactionWidget()
 
96
{
 
97
    // DO NOT disconnect the transaction here,
 
98
    // it might not exist when this happen
 
99
    delete d;
 
100
}
 
101
 
 
102
void PkTransactionWidget::hideCancelButton()
 
103
{
 
104
    // On the session installed we hide the
 
105
    // cancel button to use the KDialog main one
 
106
    ui->cancelButton->hide();
 
107
}
 
108
 
 
109
void PkTransactionWidget::cancel()
 
110
{
 
111
    if (m_trans) {
 
112
        m_trans->cancel();
 
113
    }
 
114
}
 
115
 
 
116
void PkTransactionWidget::setTransaction(PkTransaction *trans, Transaction::Role role)
 
117
{
 
118
    Q_ASSERT(trans);
 
119
 
 
120
    m_trans = trans;
 
121
    d->role = role;
 
122
 
 
123
    // This makes sure the Columns will properly resize to contents
 
124
        ui->progressView->header()->setStretchLastSection(false);
 
125
    if (role == Transaction::RoleRefreshCache) {
 
126
        trans->progressModel()->setColumnCount(1);
 
127
        ui->progressView->setModel(trans->progressModel());
 
128
        ui->progressView->header()->setResizeMode(0, QHeaderView::Stretch);
 
129
    } else {
 
130
        trans->progressModel()->setColumnCount(3);
 
131
        ui->progressView->setModel(trans->progressModel());
 
132
        ui->progressView->header()->reset();
 
133
        ui->progressView->header()->setResizeMode(0, QHeaderView::ResizeToContents);
 
134
        ui->progressView->header()->setResizeMode(1, QHeaderView::ResizeToContents);
 
135
        ui->progressView->header()->setResizeMode(2, QHeaderView::Stretch);
 
136
    }
 
137
 
 
138
    connect(trans, SIGNAL(changed()), this, SLOT(updateUi()));
 
139
 
 
140
    // Forward signals:
 
141
    connect(m_trans, SIGNAL(sorry(QString,QString,QString)),
 
142
            this, SIGNAL(sorry(QString,QString,QString)));
 
143
    connect(m_trans, SIGNAL(errorMessage(QString,QString,QString)),
 
144
            SIGNAL(error(QString,QString,QString)));
 
145
    connect(m_trans, SIGNAL(dialog(KDialog*)),
 
146
            this, SIGNAL(dialog(KDialog*)));
 
147
 
 
148
    // DISCONNECT ALL THESE SIGNALS BEFORE SETTING A NEW ONE
 
149
    connect(m_trans, SIGNAL(changed()),
 
150
            this, SLOT(updateUi()));
 
151
 
 
152
    // DISCONNECT ALL THESE SIGNALS BEFORE SETTING A NEW ONE
 
153
 
 
154
    // sets ui
 
155
    updateUi();
 
156
}
 
157
 
 
158
void PkTransactionWidget::unsetTransaction()
 
159
{
 
160
    if (m_trans == 0) {
 
161
        return;
 
162
    }
 
163
 
 
164
    disconnect(m_trans, SIGNAL(changed()),
 
165
               this, SLOT(updateUi()));
 
166
}
 
167
 
 
168
void PkTransactionWidget::updateUi()
 
169
{
 
170
    // sets the action icon to be the window icon
 
171
    Transaction *transaction = qobject_cast<Transaction*>(sender());
 
172
    if (transaction == 0 && (transaction = m_trans) == 0) {
 
173
        kWarning() << "no transaction object";
 
174
        return;
 
175
    }
 
176
 
 
177
    uint percentage = transaction->percentage();
 
178
    if (percentage <= 100) {
 
179
        ui->progressBar->setMaximum(100);
 
180
        ui->progressBar->setValue(percentage);
 
181
    } else if (ui->progressBar->maximum() != 0) {
 
182
        ui->progressBar->setMaximum(0);
 
183
        ui->progressBar->reset();
 
184
    }
 
185
 
 
186
    ui->progressBar->setRemaining(transaction->remainingTime());
 
187
 
 
188
    // Status & Speed
 
189
    Transaction::Status status = transaction->status();
 
190
    if (m_status != status) {
 
191
        m_status = status;
 
192
        ui->currentL->setText(PkStrings::status(status,
 
193
                                                transaction->speed(),
 
194
                                                transaction->downloadSizeRemaining()));
 
195
 
 
196
        KPixmapSequence sequence = KPixmapSequence(PkIcons::statusAnimation(status),
 
197
                                                   KIconLoader::SizeLarge);
 
198
        if (sequence.isValid()) {
 
199
            d->busySeq->setSequence(sequence);
 
200
            d->busySeq->start();
 
201
        }
 
202
    } else if (status == Transaction::StatusDownload) {
 
203
        ui->currentL->setText(PkStrings::status(status,
 
204
                                                transaction->speed(),
 
205
                                                transaction->downloadSizeRemaining()));
 
206
    }
 
207
 
 
208
    Transaction::Role role = transaction->role();
 
209
    if (d->role != role) {
 
210
        QString windowTitle;
 
211
        KIcon windowIcon;
 
212
        if (role == Transaction::RoleUnknown) {
 
213
            windowTitle  = PkStrings::status(Transaction::StatusSetup);
 
214
            windowIcon = PkIcons::statusIcon(Transaction::StatusSetup);
 
215
        } else {
 
216
            windowTitle = PkStrings::action(role);
 
217
            windowIcon = PkIcons::actionIcon(role);
 
218
        }
 
219
        d->role = role;
 
220
        setWindowIcon(PkIcons::actionIcon(role));
 
221
        setWindowTitle(windowTitle);
 
222
        emit titleChanged(windowTitle);
 
223
    }
 
224
 
 
225
    // check to see if we can cancel
 
226
    bool cancel = transaction->allowCancel();
 
227
    emit allowCancel(cancel);
 
228
    ui->cancelButton->setEnabled(cancel);
 
229
}
 
230
 
 
231
bool PkTransactionWidget::isFinished() const
 
232
{
 
233
//    return d->finished;
 
234
    return false;
 
235
}
 
236
 
 
237
bool PkTransactionWidget::isCancelVisible() const
 
238
{
 
239
    return ui->cancelButton->isVisible();
 
240
}
 
241
 
 
242
void PkTransactionWidget::reject()
 
243
{
 
244
//    d->finished = true;
 
245
    //    setExitStatus(Cancelled);
 
246
}
 
247
 
 
248
void PkTransactionWidget::followBottom(int value)
 
249
{
 
250
    // If the user moves the slider to the bottom
 
251
    // keep it there as the list expands
 
252
    QScrollBar *scrollBar = qobject_cast<QScrollBar*>(sender());
 
253
    m_keepScrollBarAtBottom = value == scrollBar->maximum();
 
254
}
 
255
 
 
256
void PkTransactionWidget::rangeChanged(int min, int max)
 
257
{
 
258
    Q_UNUSED(min)
 
259
    QScrollBar *scrollBar = qobject_cast<QScrollBar*>(sender());
 
260
    if (m_keepScrollBarAtBottom && scrollBar->value() != max) {
 
261
        scrollBar->setValue(max);
 
262
    }
 
263
}
 
264
 
 
265
QString PkTransactionWidget::title() const
 
266
{
 
267
    return PkStrings::action(d->role);
 
268
}
 
269
 
 
270
Transaction::Role PkTransactionWidget::role() const
 
271
{
 
272
    return d->role;
 
273
}
 
274
 
 
275
#include "PkTransactionWidget.moc"