]> git.sur5r.net Git - minitube/blob - src/downloaditem.h
Imported Upstream version 1.9
[minitube] / src / downloaditem.h
1 #ifndef DOWNLOADITEM_H
2 #define DOWNLOADITEM_H
3
4 #include <QtCore>
5 #include <QNetworkReply>
6
7 class Video;
8
9 enum DownloadItemStatus {
10     Idle = 0,
11     Starting,
12     Downloading,
13     Finished,
14     Failed
15 };
16
17 class DownloadItem : public QObject {
18
19     Q_OBJECT
20
21 signals:
22     void statusChanged();
23     void bufferProgress(int percent);
24     void progress(int percent);
25     void finished();
26     void error(QString);
27
28 public:
29     DownloadItem(Video *video, QUrl url, QString filename, QObject *parent = 0);
30     ~DownloadItem();
31     qint64 bytesTotal() const;
32     qint64 bytesReceived() const;
33     double remainingTime() const;
34     double totalTime() { return m_totalTime; }
35     double currentSpeed() const;
36     int currentPercent() const { return percent; }
37     Video* getVideo() const { return video; }
38     QString currentFilename() const { return m_file.fileName(); }
39     DownloadItemStatus status() const { return m_status; }
40     static QString formattedFilesize(qint64 size);
41     static QString formattedSpeed(double speed);
42     static QString formattedTime(double time, bool remaining = true);
43     QString errorMessage() const;
44
45 public slots:
46     void start();
47     void stop();
48     void tryAgain();
49     void open();
50     void openFolder();
51
52 private slots:
53     void downloadReadyRead();
54     void error(QNetworkReply::NetworkError code);
55     void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
56     void metaDataChanged();
57     void requestFinished();
58     void gotStreamUrl(QUrl streamUrl);
59     void speedCheck();
60
61 private:
62     void init();
63     int initialBufferSize();
64
65     qint64 m_bytesReceived;
66     QTime m_downloadTime;
67     bool m_startedSaving;
68     bool m_finishedDownloading;
69     QTime m_lastProgressTime;
70     int percent;
71     double m_totalTime;
72
73     QUrl m_url;
74
75     QFile m_file;
76     QNetworkReply *m_reply;
77     Video *video;
78
79     DownloadItemStatus m_status;
80     QString m_errorMessage;
81
82     QTimer *speedCheckTimer;
83
84 };
85
86 // This is required in order to use QPointer<DownloadItem> as a QVariant
87 // as used by the Model/View playlist
88 typedef QPointer<DownloadItem> DownloadItemPointer;
89 Q_DECLARE_METATYPE(DownloadItemPointer)
90
91 #endif // DOWNLOADITEM_H