File: acqpkitstatus.cpp

package info (click to toggle)
packagekit 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,704 kB
  • sloc: ansic: 56,209; cpp: 13,919; python: 4,970; xml: 4,945; sh: 313; perl: 60; makefile: 57
file content (230 lines) | stat: -rw-r--r-- 7,902 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
/* acqpkitstatus.cpp
 *
 * Copyright (c) 2009 Daniel Nicoletti <dantti@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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "acqpkitstatus.h"

#include "apt-job.h"

#include <math.h>
#include <apt-pkg/acquire-worker.h>
#include <apt-pkg/error.h>

// AcqPackageKitStatus::AcqPackageKitStatus - Constructor
// ---------------------------------------------------------------------
AcqPackageKitStatus::AcqPackageKitStatus(AptJob *apt)
    : m_lastPercent(PK_BACKEND_PERCENTAGE_INVALID),
      m_lastCPS(0),
      m_apt(apt),
      m_job(apt->pkJob())
{
}

// AcqPackageKitStatus::Start - Downloading has started
// ---------------------------------------------------------------------
void AcqPackageKitStatus::Start()
{
    PkRoleEnum role = pk_backend_job_get_role(m_job);
    PkStatusEnum status;

    switch (role) {
    case PK_ROLE_ENUM_GET_UPDATE_DETAIL:
        status = PK_STATUS_ENUM_DOWNLOAD_CHANGELOG;
        break;
    case PK_ROLE_ENUM_REFRESH_CACHE:
        status = PK_STATUS_ENUM_DOWNLOAD_UPDATEINFO;
        break;
    default:
        status = PK_STATUS_ENUM_DOWNLOAD;
    }
    pk_backend_job_set_status(m_job, status);

    pkgAcquireStatus::Start();
}

// AcqPackageKitStatus::Stop - Downloading has stopped
// ---------------------------------------------------------------------
void AcqPackageKitStatus::Stop()
{
    pk_backend_job_set_status(m_job, PK_STATUS_ENUM_RUNNING);
    pkgAcquireStatus::Stop();
}

// AcqPackageKitStatus::IMSHit - Called when an item got a HIT response	/*{{{*/
// ---------------------------------------------------------------------
void AcqPackageKitStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
{
    PkRoleEnum role = pk_backend_job_get_role(m_job);
    if (role == PK_ROLE_ENUM_REFRESH_CACHE) {
        pk_backend_job_repo_detail(m_job, "", Itm.Description.c_str(), true);
    } else {
        updateStatus(Itm, 100);
    }
}

// AcqPackageKitStatus::Fetch - An item has started to download
// ---------------------------------------------------------------------
/* This prints out the short description and the expected size */
void AcqPackageKitStatus::Fetch(pkgAcquire::ItemDesc &Itm)
{
    // Download queued
    updateStatus(Itm, 0);
}

// AcqPackageKitStatus::Done - Completed a download
// ---------------------------------------------------------------------
/* We don't display anything... */
void AcqPackageKitStatus::Done(pkgAcquire::ItemDesc &Itm)
{
    PkRoleEnum role = pk_backend_job_get_role(m_job);
    if (role == PK_ROLE_ENUM_REFRESH_CACHE) {
        pk_backend_job_repo_detail(m_job, "", Itm.Description.c_str(), true);
    }
    // Download completed
    updateStatus(Itm, 100);
}

// AcqPackageKitStatus::Fail - Called when an item fails to download
// ---------------------------------------------------------------------
/* We print out the error text  */
void AcqPackageKitStatus::Fail(pkgAcquire::ItemDesc &Itm)
{
    // TODO download failed
    updateStatus(Itm, 0);

    // Ignore certain kinds of transient failures (bad code)
    if (Itm.Owner->Status == pkgAcquire::Item::StatIdle) {
        return;
    }

    if (Itm.Owner->Status == pkgAcquire::Item::StatDone) {
        PkRoleEnum role = pk_backend_job_get_role(m_job);
        if (role == PK_ROLE_ENUM_REFRESH_CACHE) {
            pk_backend_job_repo_detail(m_job, "", Itm.Description.c_str(), false);
        }
    } else {
        // an error was found (maybe 404, 403...)
        // the item that got the error and the error text
        _error->Error("%s is not (yet) available (%s)", Itm.Description.c_str(), Itm.Owner->ErrorText.c_str());
    }
}

// AcqPackageKitStatus::Pulse - Regular event pulse
// ---------------------------------------------------------------------
/* This draws the current progress. Each line has an overall percent
   meter and a per active item status meter along with an overall
   bandwidth and ETA indicator. */
bool AcqPackageKitStatus::Pulse(pkgAcquire *Owner)
{
    pkgAcquireStatus::Pulse(Owner);

    unsigned long percent_done;
    percent_done = long(double((CurrentBytes + CurrentItems) * 100.0) / double(TotalBytes + TotalItems));

    // Emit the percent done
    if (m_lastPercent != percent_done) {
        if (m_lastPercent < percent_done) {
            pk_backend_job_set_percentage(m_job, percent_done);
        } else {
            pk_backend_job_set_percentage(m_job, PK_BACKEND_PERCENTAGE_INVALID);
            pk_backend_job_set_percentage(m_job, percent_done);
        }
        m_lastPercent = percent_done;
    }

    // Emit the download remaining size
    pk_backend_job_set_download_size_remaining(m_job, TotalBytes - CurrentBytes);

    for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0; I = Owner->WorkerStep(I)) {
        if (I->CurrentItem == 0) {
            continue;
        }

#if APT_PKG_ABI >= 590
        if (I->CurrentItem->TotalSize > 0) {
            updateStatus(
                *I->CurrentItem, long(double(I->CurrentItem->CurrentSize * 100.0) / double(I->CurrentItem->TotalSize)));
#else
        if (I->TotalSize > 0) {
            updateStatus(*I->CurrentItem, long(double(I->CurrentSize * 100.0) / double(I->TotalSize)));
#endif
        } else {
            updateStatus(*I->CurrentItem, 100);
        }
    }

    // calculate the overall speed
    if (fabs(CurrentCPS - m_lastCPS) > 0) {
        m_lastCPS = CurrentCPS;
        pk_backend_job_set_speed(m_job, static_cast<uint>(m_lastCPS));
    }

    Update = false;

    return !m_apt->cancelled();
}

// AcqPackageKitStatus::MediaChange - Media need to be swapped
// ---------------------------------------------------------------------
/* Prompt for a media swap */
bool AcqPackageKitStatus::MediaChange(string Media, string Drive)
{
    pk_backend_job_media_change_required(m_job, PK_MEDIA_TYPE_ENUM_DISC, Media.c_str(), Media.c_str());

    pk_backend_job_error_code(
        m_job,
        PK_ERROR_ENUM_MEDIA_CHANGE_REQUIRED,
        "Media change: please insert the disc labeled '%s' in the drive '%s' and try again.",
        Media.c_str(),
        Drive.c_str());

    // Set this so we can fail the transaction
    Update = true;
    return false;
}

void AcqPackageKitStatus::updateStatus(pkgAcquire::ItemDesc &Itm, int status)
{
    PkRoleEnum role = pk_backend_job_get_role(m_job);
    if ((role == PK_ROLE_ENUM_REFRESH_CACHE) || (role == PK_ROLE_ENUM_GET_UPDATE_DETAIL)) {
        // Ignore package update when refreshing the cache or fetching update details
        return;
    }

    // The pkgAcquire::Item had a version hiden on it's subclass
    // pkgAcqArchive but it was protected our subclass exposes that
    pkgAcqArchiveSane *archive = static_cast<pkgAcqArchiveSane *>(dynamic_cast<pkgAcqArchive *>(Itm.Owner));
    if (archive == nullptr) {
        return;
    }
    const pkgCache::VerIterator ver = archive->version();
    if (ver.end() == true) {
        return;
    }

    if (status == 100) {
        m_apt->emitPackage(ver, PK_INFO_ENUM_FINISHED);
    } else {
        // emit the package
        m_apt->emitPackage(ver, PK_INFO_ENUM_DOWNLOADING);

        // Emit the individual progress
        m_apt->emitPackageProgress(ver, PK_STATUS_ENUM_DOWNLOAD, status);
    }
}