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