]> git.sur5r.net Git - minitube/blob - src/downloaditem.h
Upload 3.9.3-2 to unstable
[minitube] / src / downloaditem.h
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #ifndef DOWNLOADITEM_H
22 #define DOWNLOADITEM_H
23
24 #include <QtCore>
25 #include <QNetworkReply>
26
27 class Video;
28
29 enum DownloadItemStatus {
30     Idle = 0,
31     Starting,
32     Downloading,
33     Finished,
34     Failed
35 };
36
37 class DownloadItem : public QObject {
38
39     Q_OBJECT
40
41 signals:
42     void statusChanged();
43     void bufferProgress(int percent);
44     void progress(int percent);
45     void finished();
46     void error(QString);
47
48 public:
49     DownloadItem(Video *video, QUrl url, QString filename, QObject *parent = 0);
50     ~DownloadItem();
51     qint64 bytesTotal() const;
52     qint64 bytesReceived() const;
53     double remainingTime() const;
54     double totalTime() { return m_totalTime; }
55     double currentSpeed() const;
56     int currentPercent() const { return percent; }
57     Video* getVideo() const { return video; }
58     QString currentFilename() const { return m_file.fileName(); }
59     DownloadItemStatus status() const { return m_status; }
60     static QString formattedFilesize(qint64 size);
61     static QString formattedSpeed(double speed);
62     static QString formattedTime(double time, bool remaining = true);
63     QString errorMessage() const;
64     qint64 offset() const { return m_offset; }
65     bool needsDownload(qint64 offset);
66     bool isBuffered(qint64 offset);
67     qint64 blankAtOffset(qint64 offset);
68     void seekTo(qint64 offset, bool sendStatusChanges = true);
69
70 public slots:
71     void start();
72     void stop();
73     void tryAgain();
74     void open();
75     void openFolder();
76
77 private slots:
78     void downloadReadyRead();
79     void error(QNetworkReply::NetworkError code);
80     void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
81     void metaDataChanged();
82     void requestFinished();
83     void gotStreamUrl(QUrl streamUrl);
84     void speedCheck();
85
86 private:
87     void init();
88     int initialBufferSize();
89
90     qint64 m_bytesReceived;
91     QTime m_downloadTime;
92     bool m_startedSaving;
93     bool m_finishedDownloading;
94     QTime m_lastProgressTime;
95     int percent;
96     double m_totalTime;
97
98     QUrl m_url;
99
100     qint64 m_offset;
101     bool sendStatusChanges;
102
103     QFile m_file;
104     QNetworkReply *m_reply;
105     Video *video;
106
107     DownloadItemStatus m_status;
108     QString m_errorMessage;
109
110     QTimer *speedCheckTimer;
111
112     QMap<qint64, qint64> buffers;
113 };
114
115 // This is required in order to use QPointer<DownloadItem> as a QVariant
116 // as used by the Model/View playlist
117 typedef QPointer<DownloadItem> DownloadItemPointer;
118 Q_DECLARE_METATYPE(DownloadItemPointer)
119
120 #endif // DOWNLOADITEM_H