Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Philipp Erhardt
katarakt
Commits
c0714872
Commit
c0714872
authored
Mar 05, 2013
by
Philipp Erhardt
Browse files
Print progress when downloading from URL.
parent
e5bd4aa0
Changes
3
Hide whitespace changes
Inline
Side-by-side
download.cpp
View file @
c0714872
#include
"download.h"
#include
<iostream>
#include
<QEventLoop>
#include
<QUrl>
#include
<QNetworkRequest>
...
...
@@ -10,6 +8,8 @@
#include
<QDir>
#include
<QFileInfo>
using
namespace
std
;
Download
::
Download
()
:
manager
(
new
QNetworkAccessManager
()),
...
...
@@ -21,27 +21,23 @@ Download::~Download() {
delete
file
;
}
QString
Download
::
load
(
QString
f
)
{
QUrl
url
(
f
);
if
(
url
.
isLocalFile
()
||
url
.
isRelative
()
)
{
// found local file
// do not download
if
(
url
.
isLocalFile
()
||
url
.
isRelative
())
{
// found local file, do not download
return
QDir
::
toNativeSeparators
(
url
.
toLocalFile
());
}
QEventLoop
loop
;
QObject
::
connect
(
manager
,
SIGNAL
(
finished
(
QNetworkReply
*
)),
&
loop
,
SLOT
(
quit
()));
QNetworkRe
quest
r
equest
(
url
);
Q
NetworkReply
*
reply
=
manager
->
get
(
request
);
QObject
::
connect
(
manager
,
SIGNAL
(
finished
(
QNetworkReply
*
)),
&
loop
,
SLOT
(
quit
()));
QNetworkRe
ply
*
reply
=
manager
->
get
(
QNetworkR
equest
(
url
)
)
;
Q
Object
::
connect
(
reply
,
SIGNAL
(
downloadProgress
(
qint64
,
qint64
)),
this
,
SLOT
(
progress
(
qint64
,
qint64
))
);
loop
.
exec
();
#ifdef DEBUG
std
::
cerr
<<
"File downloaded"
<<
std
::
endl
;
cerr
<<
"File downloaded"
<<
endl
;
#endif
// find uniq temporary filename
// find uniq
ue
temporary filename
QFileInfo
fileInfo
(
url
.
path
());
QString
fileName
=
fileInfo
.
fileName
();
delete
file
;
...
...
@@ -49,18 +45,25 @@ QString Download::load(QString f) {
file
->
setAutoRemove
(
true
);
file
->
open
();
if
(
reply
->
error
()
!=
QNetworkReply
::
NoError
||
!
reply
->
isReadable
())
{
std
::
cerr
<<
"Download failed"
<<
std
::
cerr
;
if
(
reply
->
error
()
!=
QNetworkReply
::
NoError
||
!
reply
->
isReadable
())
{
cerr
<<
reply
->
errorString
().
toStdString
()
<<
endl
;
return
NULL
;
}
// store data in tempfile
QByteArray
data
=
reply
->
readAll
();
qint64
len
=
file
->
write
(
data
);
reply
->
deleteLater
();
file
->
write
(
data
);
file
->
close
();
#ifdef DEBUG
std
::
cerr
<<
"filename: "
<<
file
.
toStdString
()
<<
std
::
endl
;
cerr
<<
"filename: "
<<
file
.
toStdString
()
<<
endl
;
#endif
return
file
->
fileName
();
}
void
Download
::
progress
(
qint64
bytes_received
,
qint64
bytes_total
)
{
cout
.
precision
(
1
);
cout
<<
fixed
<<
(
bytes_received
/
1024.0
f
)
<<
"/"
;
cout
<<
(
bytes_total
/
1024.0
f
)
<<
"KB downloaded
\r
"
;
}
download.h
View file @
c0714872
...
...
@@ -6,7 +6,8 @@
#include
<QTemporaryFile>
class
Download
{
class
Download
:
public
QObject
{
Q_OBJECT
public:
Download
();
...
...
@@ -14,10 +15,13 @@ public:
QString
load
(
QString
);
private
slots
:
void
progress
(
qint64
bytes_received
,
qint64
bytes_total
);
private:
QNetworkAccessManager
*
manager
;
QTemporaryFile
*
file
;
};
#endif
main.cpp
View file @
c0714872
...
...
@@ -61,6 +61,9 @@ int main(int argc, char *argv[]) {
Download
download
;
QString
file
=
download
.
load
(
QString
::
fromUtf8
(
argv
[
optind
]));
if
(
file
==
NULL
)
{
return
1
;
}
Viewer
katarakt
(
file
);
if
(
!
katarakt
.
is_valid
())
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment