1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/***************************************************************************
sq_downloader.cpp - description
-------------------
begin : Fri Jun 07 2007
copyright : (C) 2007 by Baryshev Dmitry
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <tqfile.h>
#include <tdeio/job.h>
#include <tdefileitem.h>
#include <tdetempfile.h>
#include "sq_libraryhandler.h"
#include "sq_downloader.h"
#ifndef KSQUIRREL_PART
#include "sq_archivehandler.h"
#endif
#define SQ_PREDOWNLOAD_SIZE 50
SQ_Downloader::SQ_Downloader(TQObject *parent, const char *name) : TQObject(parent, name), job(0), m_error(false)
{
tmp = new KTempFile;
tmp->setAutoDelete(true);
tmp->close();
}
SQ_Downloader::~SQ_Downloader()
{
clean();
delete tmp;
}
void SQ_Downloader::start(KFileItem *fi)
{
m_error = false;
mURL = fi->url();
emitPercents = false;
startTime = TQTime::currentTime();
size = 0;
totalSize = fi->size();
#ifndef KSQUIRREL_PART
nomime = SQ_ArchiveHandler::instance()->findProtocolByMime(fi->mimetype()).isEmpty();
#else
nomime = true;
#endif
job = TDEIO::get(mURL, false, false);
clean();
continueDownload = false;
connect(job, TQ_SIGNAL(data(TDEIO::Job *, const TQByteArray &)), this, TQ_SLOT(slotData(TDEIO::Job *, const TQByteArray &)));
connect(job, TQ_SIGNAL(result(TDEIO::Job *)), this, TQ_SLOT(slotDataResult(TDEIO::Job *)));
}
void SQ_Downloader::slotData(TDEIO::Job *job, const TQByteArray &ba)
{
size += ba.size();
TQFile f(tmp->name());
if(f.open(IO_WriteOnly | IO_Append))
{
f.writeBlock(ba);
f.close();
}
if(emitPercents || startTime.msecsTo(TQTime::currentTime()) > 1000)
{
emit percents(size);
emitPercents = true;
}
// 50 bytes are enough to determine file type
if(size >= SQ_PREDOWNLOAD_SIZE && !continueDownload && totalSize != size)
{
// cancel download (file type is not supported)
SQ_LIBRARY *lib = SQ_LibraryHandler::instance()->libraryForFile(tmp->name());
if(nomime && !lib)
{
job->kill(false); // kill job & emit result
}
else
{
// nice, we can open this image/archive - continue download
continueDownload = true;
}
}
}
void SQ_Downloader::slotDataResult(TDEIO::Job *cpjob)
{
job = 0;
// job error
if(cpjob->error() && cpjob->error() != TDEIO::ERR_USER_CANCELED)
{
m_error = true;
emit result(mEmptyURL);
}
else if(cpjob->error() == TDEIO::ERR_USER_CANCELED) // not supported image/archive type -
// emit empty url without errors
{
emit result(mEmptyURL);
}
else // supported image type/archive type (no errors or job killed)
{
emit result(KURL::fromPathOrURL(tmp->name()));
}
}
void SQ_Downloader::clean()
{
TQFile f(tmp->name());
if(f.open(IO_WriteOnly))
f.close();
}
void SQ_Downloader::kill()
{
if(job) job->kill();
}
#include "sq_downloader.moc"
|