summaryrefslogtreecommitdiff
path: root/backends/aptcc/acqpkitstatus.cpp
blob: cdaa36df0ad278f28d3f754d22e08d3d4ca2e8bc (plain)
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
/* 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-intf.h"

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

// AcqPackageKitStatus::AcqPackageKitStatus - Constructor
// ---------------------------------------------------------------------
AcqPackageKitStatus::AcqPackageKitStatus(AptIntf *apt, PkBackend *backend, bool &cancelled) :
    m_apt(apt),
    m_backend(backend),
    _cancelled(cancelled),
    last_percent(PK_BACKEND_PERCENTAGE_INVALID),
    last_sub_percent(PK_BACKEND_PERCENTAGE_INVALID)
{
}

// AcqPackageKitStatus::Start - Downloading has started
// ---------------------------------------------------------------------
void AcqPackageKitStatus::Start()
{
    pkgAcquireStatus::Start();
    ID = 1;
}

// AcqPackageKitStatus::IMSHit - Called when an item got a HIT response	/*{{{*/
// ---------------------------------------------------------------------
void AcqPackageKitStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
{
    if (packages.size() == 0) {
        pk_backend_repo_detail(m_backend,
                               "",
                               Itm.Description.c_str(),
                               true);
    }
    Update = true;
}

// AcqPackageKitStatus::Fetch - An item has started to download
// ---------------------------------------------------------------------
/* This prints out the short description and the expected size */
void AcqPackageKitStatus::Fetch(pkgAcquire::ItemDesc &Itm)
{
    Update = true;
    if (Itm.Owner->Complete == true)
        return;

    Itm.Owner->ID = ID++;
}

// AcqPackageKitStatus::Done - Completed a download
// ---------------------------------------------------------------------
/* We don't display anything... */
void AcqPackageKitStatus::Done(pkgAcquire::ItemDesc &Itm)
{
    Update = true;
}

// AcqPackageKitStatus::Fail - Called when an item fails to download
// ---------------------------------------------------------------------
/* We print out the error text  */
void AcqPackageKitStatus::Fail(pkgAcquire::ItemDesc &Itm)
{
    // Ignore certain kinds of transient failures (bad code)
    if (Itm.Owner->Status == pkgAcquire::Item::StatIdle) {
        return;
    }

    if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
    {
        if (packages.size() == 0) {
            pk_backend_repo_detail(m_backend,
                                   "",
                                   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("Error %s\n  %s",
                      Itm.Description.c_str(),
                      Itm.Owner->ErrorText.c_str());
    }

    Update = true;
}


// AcqTextStatus::Stop - Finished downloading
// ---------------------------------------------------------------------
/* This prints out the bytes downloaded and the overall average line
   speed */
void AcqPackageKitStatus::Stop()
{
    pkgAcquireStatus::Stop();
    // the items that still on the set are finished
    for (set<string>::iterator it = currentPackages.begin();
         it != currentPackages.end();
         it++ )
    {
        emit_package(*it, true);
    }
}


// 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 (last_percent != percent_done) {
        if (last_percent < percent_done) {
            pk_backend_set_percentage(m_backend, percent_done);
        } else {
            pk_backend_set_percentage(m_backend, PK_BACKEND_PERCENTAGE_INVALID);
            pk_backend_set_percentage(m_backend, percent_done);
        }
        last_percent = percent_done;
    }

    set<string> localCurrentPackages = currentPackages;;
    for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
         I = Owner->WorkerStep(I))
    {
        // Check if there is no item running or if we don't have
        // any packages set we are probably refreshing the cache
        if (I->CurrentItem == 0 || packages.size() == 0)
        {
            continue;
        }
        emit_package(I->CurrentItem->ShortDesc, false);
        localCurrentPackages.erase(I->CurrentItem->ShortDesc);

        // Add the total size and percent
        if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
        {
            unsigned long sub_percent;
            // TODO PackageKit needs to emit package with progress.
            sub_percent = long(double(I->CurrentSize*100.0)/double(I->TotalSize));
            if (last_sub_percent != sub_percent) {
                if (last_sub_percent < sub_percent) {
                    pk_backend_set_sub_percentage(m_backend, sub_percent);
                } else {
                    pk_backend_set_sub_percentage(m_backend, PK_BACKEND_PERCENTAGE_INVALID);
                    pk_backend_set_sub_percentage(m_backend, sub_percent);
                }
                last_sub_percent = sub_percent;
            }
        }
    }

    // the items that still on the set are finished
    for (set<string>::iterator it = localCurrentPackages.begin();
         it != localCurrentPackages.end();
         it++ )
    {
        emit_package(*it, true);
    }

    double localCPS = (CurrentCPS >= 0) ? CurrentCPS : -1 * CurrentCPS;
    if (localCPS != last_CPS)
    {
        last_CPS = localCPS;
        pk_backend_set_speed(m_backend, (int) last_CPS);
    }

    Update = false;

    return !_cancelled;;
}

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

    char errorMsg[400];
    sprintf(errorMsg,
            "Media change: please insert the disc labeled"
            " '%s' "
            "in the drive '%s' and try again.",
            Media.c_str(),
            Drive.c_str());

    pk_backend_error_code(m_backend,
                          PK_ERROR_ENUM_MEDIA_CHANGE_REQUIRED,
                          errorMsg);

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


void AcqPackageKitStatus::addPackage(const pkgCache::VerIterator &ver)
{
    packages.push_back(ver);
}

void AcqPackageKitStatus::emit_package(const string &name, bool finished)
{
    if (name.compare(last_package_name) != 0 && packages.size()) {
        // find the package
        for (PkgList::iterator it = packages.begin(); it != packages.end(); ++it) {
            if (_cancelled) {
                break;
            }

            // try to see if any package matches
            if (name.compare(it->ParentPkg().Name()) == 0) {
                m_apt->emitPackage(*it, finished ? PK_INFO_ENUM_FINISHED : PK_INFO_ENUM_DOWNLOADING);
                last_package_name = name;

                // Find the downloading item
                if (finished) {
                    currentPackages.erase(name);
                } else {
                    currentPackages.insert(name);
                }
                break;
            }
        }
    }
}