]> git.sur5r.net Git - minitube/blob - src/downloaditem.h
Imported Upstream version 1.4.1
[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 currentSpeed() const;
35     int currentPercent() const { return percent; }
36     Video* getVideo() const { return video; }
37     QString currentFilename() const { return m_file.fileName(); }
38     DownloadItemStatus status() const { return m_status; }
39     static QString formattedFilesize(qint64 size);
40     static QString formattedSpeed(double speed);
41     static QString formattedTime(double time);
42     QString errorMessage() const;
43
44 public slots:
45     void start();
46     void stop();
47     void tryAgain();
48     void open();
49     void openFolder();
50
51 private slots:
52     void downloadReadyRead();
53     void error(QNetworkReply::NetworkError code);
54     void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
55     void metaDataChanged();
56     void requestFinished();
57     void gotStreamUrl(QUrl streamUrl);
58     void speedCheck();
59
60 private:
61     void init();
62     int initialBufferSize();
63
64     qint64 m_bytesReceived;
65     QTime m_downloadTime;
66     bool m_startedSaving;
67     bool m_finishedDownloading;
68     QTime m_lastProgressTime;
69     int percent;
70
71     QUrl m_url;
72
73     QFile m_file;
74     QNetworkReply *m_reply;
75     Video *video;
76
77     DownloadItemStatus m_status;
78     QString m_errorMessage;
79
80     QTimer *speedCheckTimer;
81
82 };
83
84 // This is required in order to use QPointer<DownloadItem> as a QVariant
85 // as used by the Model/View playlist
86 typedef QPointer<DownloadItem> DownloadItemPointer;
87 Q_DECLARE_METATYPE(DownloadItemPointer)
88
89 #endif // DOWNLOADITEM_H