~moshe-wagner/orayta/Orayta-QT

« back to all changes in this revision

Viewing changes to filedownloader.h

  • Committer: moshe.wagner
  • Date: 2012-05-05 21:48:52 UTC
  • Revision ID: git-v1:70e09345355d8f7ecaf4ec2176a47d02dea9bcb7
Making the android branch into the main one

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This program is free software; you can redistribute it and/or modify
 
2
* it under the terms of the GNU General Public License version 2
 
3
* as published by the Free Software Foundation.
 
4
*
 
5
* This program is distributed in the hope that it will be useful,
 
6
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
7
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
8
* GNU General Public License for more details.
 
9
*
 
10
* You should have received a copy of the GNU General Public License
 
11
* along with this program; if not, write to the Free Software
 
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
13
*
 
14
* Author: Moshe Wagner. <moshe.wagner@gmail.com>
 
15
*/
 
16
 
 
17
#ifndef FILEDOWNLOADER_H
 
18
#define FILEDOWNLOADER_H
 
19
 
 
20
#include <QObject>
 
21
#include <QFile>
 
22
#include <QtNetwork/QHttp>
 
23
/*
 
24
  This class provides a simple method to download requested files;
 
25
  The download is saved into $path+".part" untill download is succesfull,
 
26
  and only then it is moved to the given path.
 
27
 
 
28
  ( If a file exists in the given position, it is asuumed to be a successfull previous download,
 
29
     and the download is instantly done. )
 
30
 
 
31
  It emits these signalls:
 
32
  -> downloadProgress(int) - gives a integer between 1 to 100 representing the current dowload progress.
 
33
                              (or zero as long as it didn't start yet)
 
34
  -> done() - emitted when the download is finished succesfully
 
35
  -> Error() - emitted when the download failed due to an error, but not if it was aborted
 
36
 
 
37
  And accepts these slots:
 
38
  -> abort() - stops the download, removes the ".part" file, but does not emmit a "error" signal
 
39
*/
 
40
 
 
41
class FileDownloader : public QObject
 
42
{
 
43
    Q_OBJECT
 
44
 
 
45
public:
 
46
    FileDownloader();
 
47
 
 
48
    //Downloads the file at the given url to the given target.
 
49
    // Returns nothing, but emits signalls representing what happened
 
50
    void Download(QString url, QString target, bool overwrite);
 
51
 
 
52
    //Returns the filename of the last download (NOTE: also if it was unsuccessful)
 
53
    QString getFileName();
 
54
 
 
55
    //Returns true if a download is running (at any state), and false if not
 
56
    bool isInProgress();
 
57
 
 
58
private:
 
59
    //The class's Http object
 
60
    QHttp mHttpObject;
 
61
    //The file handling the download proccess
 
62
    QFile mTargetFile;
 
63
    //File name for the finished download
 
64
        // (Until it's done it's saved to the same name with ".part" after it)
 
65
    QString mFileName;
 
66
 
 
67
public slots:
 
68
    //Aborts the download, and emits no error
 
69
    void abort();
 
70
 
 
71
private slots:
 
72
    //Connects to the Http object's done() signall
 
73
    void downloadDone(bool error);
 
74
    //Connects to the Http object's download progress() signall
 
75
    void downloadProgress(int,int);
 
76
 
 
77
signals:
 
78
    //Emitted when download WAS successfuly done
 
79
    void done();
 
80
    //Emitted when download WASN'T successfuly done
 
81
    void downloadError();
 
82
    //Emitted as the download continues, giving it's progress in percents.
 
83
    void downloadProgress(int);
 
84
};
 
85
 
 
86
#endif // FILEDOWNLOADER_H