summaryrefslogtreecommitdiff
path: root/src/generic/views/download_progress.h
blob: 3e4114954a7698c76a94913ad399e1c2c34d5d9d (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/** \file download_progress.h */    // -*-c++-*-

// Copyright (C) 2010-2011 Daniel Burrows
//
// 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.

#ifndef APTITUDE_GENERIC_VIEWS_DOWNLOAD_PROGRESS_H
#define APTITUDE_GENERIC_VIEWS_DOWNLOAD_PROGRESS_H

// System includes:
#include <boost/optional.hpp>
#include <boost/variant.hpp>

#include <sigc++/slot.h>

#include <string>
#include <vector>

namespace aptitude
{
  namespace views
  {
    /** \brief Interface for objects that can display the progress of
     *  a download.
     *
     *  Operates at a higher level and more cleanly than AcqProgress.
     *  See src/generic/controllers/acquire_download_progress.h for a
     *  bridge between the two worlds.
     */
    class download_progress
    {
    public:
      virtual ~download_progress();

      /** \brief Represents the download progress of a single file. */
      class file_progress
      {
        unsigned long long current_size;
        unsigned long long total_size;

        bool complete;
        std::string description;
        boost::optional<unsigned long> id;
        std::string mode;

      public:
        file_progress(unsigned long long _current_size,
                      unsigned long long _total_size,
                      bool _complete,
                      const std::string &_description,
                      const boost::optional<unsigned long> &_id,
                      const std::string &_mode)
          : current_size(_current_size),
            total_size(_total_size),

            complete(_complete),
            description(_description),
            id(_id),
            mode(_mode)
        {
        }

        /** \brief Get the number of bytes that have been downloaded. */
        unsigned long long get_current_size() const { return current_size; }

        /** \brief Get the total size of the file. */
        unsigned long long get_total_size() const { return total_size; }

        /** \return \b true if the file has been successfully fetched
         *  according to the download backend.
         */
        bool get_complete() const { return complete; }

        /** \brief Get a brief description of this file. */
        const std::string &get_description() const { return description; }

        /** \brief Get an integer that identifies this item.
         *
         *  May be unset if the item doesn't have an ID yet.
         */
        const boost::optional<unsigned long> &get_id() const { return id; }

        /** \brief Retrieve the current mode string for the file.
         *
         *  If there is no mode, this will be an empty string.
         */
        const std::string &get_mode() const { return mode; }

        bool operator==(const file_progress &other) const;
        bool operator!=(const file_progress &other) const
        {
          return ! (*this == other);
        }
      };

      /** \brief Represents the current progress of a download. */
      class status
      {
      public:
        typedef boost::variant<file_progress, std::string> worker_status;

      private:
        const unsigned long long download_rate;
        const std::vector<worker_status> active_downloads;
        const double fraction_complete;
        const unsigned long long time_remaining;

      public:
        status(const unsigned long long _download_rate,
               const std::vector<worker_status> &_active_downloads,
               const double _fraction_complete,
               const unsigned long long _time_remaining)
          : download_rate(_download_rate),
            active_downloads(_active_downloads),
            fraction_complete(_fraction_complete),
            time_remaining(_time_remaining)
        {
        }

        /** \brief Get the current download speed in bytes per second. */
        unsigned long long get_download_rate() const { return download_rate; }

        /** \brief Get the currently active download processes. */
        const std::vector<worker_status> &get_active_downloads() const
        {
          return active_downloads;
        }

        /** \brief Get the proportional completion of the download (scale
         *  of 0 to 1).
         */
        double get_fraction_complete() const { return fraction_complete; }

        /** \brief Get the estimated number of seconds until the download
         *  completes.
         */
        unsigned long long get_time_remaining() const { return time_remaining; }

        bool operator==(const status &other) const;
        bool operator!=(const status &other) const
        {
          return ! (*this == other);
        }
      };

      /** \brief Update the download progress indicator.
       *
       *  \param current_status    The current status of the download.
       *
       *  \return \b true to continue the download; \b false to abort it.
       */
      virtual bool update_progress(const status &current_status) = 0;

      /** \brief Invoked when a file is starting to be downloaded.
       *
       *  \param description    A brief description of the file.
       *  \param id             An integer identifying this file, or
       *                        unset if it hasn't been assigned yet.
       *  \param file_size      The size of the file; invalid if the
       *                        file size isn't known.
       */
      virtual void file_started(const std::string &description,
                                const boost::optional<unsigned long> &id,
                                const boost::optional<unsigned long long> &file_size) = 0;

      /** \brief Invoked when a file isn't even started because it was
       *  already downloaded.
       *
       *  \param description    A brief description of the file.
       *  \param id             An integer identifying this file, or
       *                        unset if it hasn't been assigned yet.
       *  \param file_size      The size of the file; invalid if the
       *                        file size isn't known.
       */

      virtual void file_already_downloaded(const std::string &description,
                                           const boost::optional<unsigned long> &id,
                                           const boost::optional<unsigned long long> &file_size) = 0;

      /** \brief Invoked when a file fails to download.
       *
       *  \param ignored        True if the file was successfully fetched
       *                        anyway.
       *
       *                        I"m not sure what this means, but it
       *                        matters to existing download UIs.
       *  \param error          A textual description of the error.
       *  \param description    A brief description of the file.
       *  \param id             An integer identifying this file, or
       *                        unset if it hasn't been assigned yet.
       */
      virtual void error(bool ignored,
                         const std::string &error,
                         const std::string &description,
                         const boost::optional<unsigned long> &id) = 0;

      /** \brief Invoked when something is done being downloaded.
       *
       *  \param description    A brief description of the file.
       *  \param id             An integer identifying this file, or
       *                        unset if it hasn't been assigned yet.
       */
      virtual void file_finished(const std::string &description,
                                 const boost::optional<unsigned long> &id) = 0;

      /** \brief Invoked when each stage of the download is complete.
       *
       *  The whole download process might not be done; for instance,
       *  we might need to change to a new CD.  complete() is invoked
       *  after the entire download finishes.
       *
       *  \param fetched_bytes  The number of bytes that were downloaded.
       *
       *  \param elapsed_time   How long (in seconds) the download lasted.
       *
       *  \param latest_download_rate  The final estimated download rate.
       *
       * \todo Should the parameters be incorporated into a status
       * snapshot so that can be used instead?
       */
      virtual void done(unsigned long long fetched_bytes,
                        unsigned long long elapsed_time,
                        unsigned long long latest_download_rate) = 0;

      /** \brief Invoked when the install media should be replaced.
       *
       *  \param media          The label of the media to insert.
       *  \param drive          The name of the drive in which the media
       *                        should be placed.
       *  \param k              A continuation that must be invoked when
       *                        the user has said whether it's OK to
       *                        continue (possibly after media_change has
       *                        returned).  Pass \b true to continue the
       *                        installation, or \b false to abort it.
       */
      virtual void media_change(const std::string &media,
                                const std::string &drive,
                                const sigc::slot1<void, bool> &k) = 0;

      /** \brief Invoked when the whole download finishes.
       *
       *  \param fetched_bytes  The number of bytes that were downloaded.
       *
       *  \param elapsed_time   How long (in seconds) the download lasted.
       *
       *  \param latest_download_rate  The final estimated download rate.
       */
      virtual void complete(unsigned long long fetched_bytes,
                            unsigned long long elapsed_time,
                            unsigned long long latest_download_rate) = 0;
    };

    std::ostream &operator<<(std::ostream &out,
                             const download_progress::file_progress &progress);

    std::ostream &operator<<(std::ostream &out,
                             const download_progress::status &status);
  }
}

#endif // APTITUDE_GENERIC_VIEWS_DOWNLOAD_PROGRESS_H