Mercurial > hg > svcore
comparison data/fileio/RemoteFile.cpp @ 214:ce6f65ab3327
* Add large chunks of context help in the optional status bar
* Add an extra overlay mode in which even the centre frame is disabled
* Fixes to FTP retrieval
author | Chris Cannam |
---|---|
date | Fri, 19 Jan 2007 13:13:14 +0000 |
parents | e2bbb58e6df6 |
children | 9a13687c078b |
comparison
equal
deleted
inserted
replaced
213:e0e7f6c5fda9 | 214:ce6f65ab3327 |
---|---|
61 m_http = new QHttp(url.host(), url.port(80)); | 61 m_http = new QHttp(url.host(), url.port(80)); |
62 connect(m_http, SIGNAL(done(bool)), this, SLOT(done(bool))); | 62 connect(m_http, SIGNAL(done(bool)), this, SLOT(done(bool))); |
63 connect(m_http, SIGNAL(dataReadProgress(int, int)), | 63 connect(m_http, SIGNAL(dataReadProgress(int, int)), |
64 this, SLOT(dataReadProgress(int, int))); | 64 this, SLOT(dataReadProgress(int, int))); |
65 connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), | 65 connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), |
66 this, SLOT(responseHeaderReceived(const QHttpResponseHeader &))); | 66 this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &))); |
67 m_http->get(url.path(), m_localFile); | 67 m_http->get(url.path(), m_localFile); |
68 | 68 |
69 } else if (scheme == "ftp") { | 69 } else if (scheme == "ftp") { |
70 | 70 |
71 m_ok = true; | 71 m_ok = true; |
72 m_ftp = new QFtp; | 72 m_ftp = new QFtp; |
73 connect(m_ftp, SIGNAL(done(bool)), this, SLOT(done(bool))); | 73 connect(m_ftp, SIGNAL(done(bool)), this, SLOT(done(bool))); |
74 connect(m_ftp, SIGNAL(commandFinished(int, bool)), | |
75 this, SLOT(ftpCommandFinished(int, bool))); | |
74 connect(m_ftp, SIGNAL(dataTransferProgress(qint64, qint64)), | 76 connect(m_ftp, SIGNAL(dataTransferProgress(qint64, qint64)), |
75 this, SLOT(dataTransferProgress(qint64, qint64))); | 77 this, SLOT(dataTransferProgress(qint64, qint64))); |
76 m_ftp->connectToHost(url.host(), url.port(21)); | 78 m_ftp->connectToHost(url.host(), url.port(21)); |
77 | 79 |
78 QString username = url.userName(); | 80 QString username = url.userName(); |
83 QString password = url.password(); | 85 QString password = url.password(); |
84 if (password == "") { | 86 if (password == "") { |
85 password = QString("%1@%2").arg(getenv("USER")).arg(getenv("HOST")); | 87 password = QString("%1@%2").arg(getenv("USER")).arg(getenv("HOST")); |
86 } | 88 } |
87 | 89 |
88 QStringList path = url.path().split('/'); | 90 m_ftp->login(username, password); |
89 for (QStringList::iterator i = path.begin(); i != path.end(); ) { | 91 |
90 QString bit = *i; | 92 QString dirpath = url.path().section('/', 0, -2); |
91 ++i; | 93 QString filename = url.path().section('/', -1); |
92 if (i != path.end()) { | 94 |
93 m_ftp->cd(*i); | 95 if (dirpath == "") dirpath = "/"; |
94 } else { | 96 m_ftp->cd(dirpath); |
95 m_ftp->get(*i, m_localFile); | 97 m_ftp->get(filename, m_localFile); |
96 } | |
97 } | |
98 } | 98 } |
99 | 99 |
100 if (m_ok) { | 100 if (m_ok) { |
101 m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(url.toString()), tr("Cancel"), 0, 100); | 101 m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(url.toString()), tr("Cancel"), 0, 100); |
102 m_progressDialog->hide(); | 102 m_progressDialog->hide(); |
116 void | 116 void |
117 RemoteFile::cleanup() | 117 RemoteFile::cleanup() |
118 { | 118 { |
119 // std::cerr << "RemoteFile::cleanup" << std::endl; | 119 // std::cerr << "RemoteFile::cleanup" << std::endl; |
120 m_done = true; | 120 m_done = true; |
121 delete m_http; | 121 if (m_http) { |
122 m_http = 0; | 122 delete m_http; |
123 delete m_ftp; | 123 m_http = 0; |
124 m_ftp = 0; | 124 } |
125 if (m_ftp) { | |
126 m_ftp->abort(); | |
127 m_ftp->deleteLater(); | |
128 m_ftp = 0; | |
129 } | |
125 delete m_progressDialog; | 130 delete m_progressDialog; |
126 m_progressDialog = 0; | 131 m_progressDialog = 0; |
127 delete m_localFile; | 132 delete m_localFile; |
128 m_localFile = 0; | 133 m_localFile = 0; |
129 } | 134 } |
186 { | 191 { |
187 dataTransferProgress(done, total); | 192 dataTransferProgress(done, total); |
188 } | 193 } |
189 | 194 |
190 void | 195 void |
191 RemoteFile::responseHeaderReceived(const QHttpResponseHeader &resp) | 196 RemoteFile::httpResponseHeaderReceived(const QHttpResponseHeader &resp) |
192 { | 197 { |
193 m_lastStatus = resp.statusCode(); | 198 m_lastStatus = resp.statusCode(); |
194 if (m_lastStatus / 100 >= 4) { | 199 if (m_lastStatus / 100 >= 4) { |
195 m_errorString = QString("%1 %2") | 200 m_errorString = QString("%1 %2") |
196 .arg(resp.statusCode()).arg(resp.reasonPhrase()); | 201 .arg(resp.statusCode()).arg(resp.reasonPhrase()); |
201 << m_lastStatus << std::endl; | 206 << m_lastStatus << std::endl; |
202 } | 207 } |
203 } | 208 } |
204 | 209 |
205 void | 210 void |
211 RemoteFile::ftpCommandFinished(int id, bool error) | |
212 { | |
213 std::cerr << "RemoteFile::ftpCommandFinished(" << id << ", " << error << ")" << std::endl; | |
214 | |
215 if (!m_ftp) return; | |
216 | |
217 QFtp::Command command = m_ftp->currentCommand(); | |
218 | |
219 if (!error) { | |
220 std::cerr << "RemoteFile::ftpCommandFinished: success for command " | |
221 << command << std::endl; | |
222 return; | |
223 } | |
224 | |
225 if (command == QFtp::ConnectToHost) { | |
226 m_errorString = tr("Failed to connect to FTP server"); | |
227 } else if (command == QFtp::Login) { | |
228 m_errorString = tr("Login failed"); | |
229 } else if (command == QFtp::Cd) { | |
230 m_errorString = tr("Failed to change to correct directory"); | |
231 } else if (command == QFtp::Get) { | |
232 m_errorString = tr("FTP download aborted"); | |
233 } | |
234 | |
235 m_lastStatus = 400; // for done() | |
236 } | |
237 | |
238 void | |
206 RemoteFile::dataTransferProgress(qint64 done, qint64 total) | 239 RemoteFile::dataTransferProgress(qint64 done, qint64 total) |
207 { | 240 { |
208 if (!m_progressDialog) return; | 241 if (!m_progressDialog) return; |
209 | 242 |
210 int percent = int((double(done) / double(total)) * 100.0 - 0.1); | 243 int percent = int((double(done) / double(total)) * 100.0 - 0.1); |
226 } | 259 } |
227 | 260 |
228 void | 261 void |
229 RemoteFile::done(bool error) | 262 RemoteFile::done(bool error) |
230 { | 263 { |
231 // std::cerr << "RemoteFile::done(" << error << ")" << std::endl; | 264 std::cerr << "RemoteFile::done(" << error << ")" << std::endl; |
232 | 265 |
233 if (m_done) return; | 266 if (m_done) return; |
234 | 267 |
235 emit progress(100); | 268 emit progress(100); |
236 | 269 |