annotate data/fileio/RemoteFile.cpp @ 282:e2fdcf9d35c5

* Make QuickTime file reader work (has been tested)
author Chris Cannam
date Wed, 08 Aug 2007 12:03:35 +0000
parents 7802b0e2b0ff
children 557e00480279
rev   line source
Chris@208 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@208 2
Chris@208 3 /*
Chris@208 4 Sonic Visualiser
Chris@208 5 An audio file viewer and annotation editor.
Chris@208 6 Centre for Digital Music, Queen Mary, University of London.
Chris@208 7 This file copyright 2007 QMUL.
Chris@208 8
Chris@208 9 This program is free software; you can redistribute it and/or
Chris@208 10 modify it under the terms of the GNU General Public License as
Chris@208 11 published by the Free Software Foundation; either version 2 of the
Chris@208 12 License, or (at your option) any later version. See the file
Chris@208 13 COPYING included with this distribution for more information.
Chris@208 14 */
Chris@208 15
Chris@208 16 #include "RemoteFile.h"
Chris@208 17 #include "base/TempDirectory.h"
Chris@208 18 #include "base/Exceptions.h"
Chris@208 19
Chris@208 20 #include <QHttp>
Chris@208 21 #include <QFtp>
Chris@208 22 #include <QFileInfo>
Chris@208 23 #include <QDir>
Chris@208 24 #include <QApplication>
Chris@208 25 #include <QProgressDialog>
Chris@210 26 #include <QHttpResponseHeader>
Chris@208 27
Chris@208 28 #include <iostream>
Chris@208 29
Chris@208 30 int
Chris@208 31 RemoteFile::m_count = 0;
Chris@208 32
Chris@208 33 QMutex
Chris@208 34 RemoteFile::m_fileCreationMutex;
Chris@208 35
Chris@208 36 RemoteFile::RemoteFile(QUrl url) :
Chris@208 37 m_ftp(0),
Chris@208 38 m_http(0),
Chris@208 39 m_localFile(0),
Chris@208 40 m_ok(false),
Chris@210 41 m_lastStatus(0),
Chris@208 42 m_done(false),
Chris@210 43 m_progressDialog(0),
Chris@210 44 m_progressShowTimer(this)
Chris@208 45 {
Chris@208 46 if (!canHandleScheme(url)) {
Chris@208 47 std::cerr << "RemoteFile::RemoteFile: ERROR: Unsupported scheme in URL \"" << url.toString().toStdString() << "\"" << std::endl;
Chris@208 48 return;
Chris@208 49 }
Chris@208 50
Chris@208 51 m_localFilename = createLocalFile(url);
Chris@208 52 if (m_localFilename == "") return;
Chris@208 53 m_localFile = new QFile(m_localFilename);
Chris@208 54 m_localFile->open(QFile::WriteOnly);
Chris@208 55
Chris@208 56 QString scheme = url.scheme().toLower();
Chris@208 57
Chris@208 58 if (scheme == "http") {
Chris@208 59
Chris@211 60 m_ok = true;
Chris@208 61 m_http = new QHttp(url.host(), url.port(80));
Chris@208 62 connect(m_http, SIGNAL(done(bool)), this, SLOT(done(bool)));
Chris@208 63 connect(m_http, SIGNAL(dataReadProgress(int, int)),
Chris@208 64 this, SLOT(dataReadProgress(int, int)));
Chris@210 65 connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
Chris@214 66 this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &)));
Chris@279 67
Chris@279 68 // I don't quite understand this. url.path() returns a path
Chris@279 69 // without percent encoding; for example, spaces appear as
Chris@279 70 // literal spaces. This generally won't work if sent to the
Chris@279 71 // server directly. You can retrieve a correctly encoded URL
Chris@279 72 // from QUrl using url.toEncoded(), but that gives you the
Chris@279 73 // whole URL; there doesn't seem to be any way to retrieve
Chris@279 74 // only an encoded path. Furthermore there doesn't seem to be
Chris@279 75 // any way to convert a retrieved path into an encoded path
Chris@279 76 // without explicitly specifying that you don't want the path
Chris@279 77 // separators ("/") to be encoded. (Besides being painful to
Chris@279 78 // manage, I don't see how this can work correctly in any case
Chris@279 79 // where a percent-encoded "/" is supposed to appear within a
Chris@279 80 // path element?) There also seems to be no way to retrieve
Chris@279 81 // the path plus query string, i.e. everything that I need to
Chris@279 82 // send to the HTTP server. And no way for QHttp to take a
Chris@279 83 // QUrl argument. I'm obviously missing something.
Chris@279 84
Chris@279 85 // So, two ways to do this: query the bits from the URL,
Chris@279 86 // encode them individually, and glue them back together
Chris@279 87 // again...
Chris@279 88 /*
Chris@279 89 QString path = QUrl::toPercentEncoding(url.path(), "/");
Chris@279 90 QList<QPair<QString, QString> > query = url.queryItems();
Chris@279 91 if (!query.empty()) {
Chris@279 92 QStringList q2;
Chris@279 93 for (QList<QPair<QString, QString> >::iterator i = query.begin();
Chris@279 94 i != query.end(); ++i) {
Chris@279 95 q2.push_back(QString("%1=%3")
Chris@279 96 .arg(QString(QUrl::toPercentEncoding(i->first)))
Chris@279 97 .arg(QString(QUrl::toPercentEncoding(i->second))));
Chris@279 98 }
Chris@279 99 path = QString("%1%2%3")
Chris@279 100 .arg(path).arg("?")
Chris@279 101 .arg(q2.join("&"));
Chris@279 102 }
Chris@279 103 */
Chris@279 104
Chris@279 105 // ...or, much simpler but relying on knowledge about the
Chris@279 106 // scheme://host/path/path/query etc format of the URL, we can
Chris@279 107 // get the whole URL ready-encoded and then split it on "/" as
Chris@279 108 // appropriate...
Chris@279 109
Chris@279 110 QString path = "/" + QString(url.toEncoded()).section('/', 3);
Chris@279 111
Chris@279 112 std::cerr << "RemoteFile: path is \""
Chris@279 113 << path.toStdString() << "\"" << std::endl;
Chris@279 114
Chris@278 115 m_http->get(path, m_localFile);
Chris@208 116
Chris@208 117 } else if (scheme == "ftp") {
Chris@208 118
Chris@211 119 m_ok = true;
Chris@208 120 m_ftp = new QFtp;
Chris@208 121 connect(m_ftp, SIGNAL(done(bool)), this, SLOT(done(bool)));
Chris@214 122 connect(m_ftp, SIGNAL(commandFinished(int, bool)),
Chris@214 123 this, SLOT(ftpCommandFinished(int, bool)));
Chris@208 124 connect(m_ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
Chris@208 125 this, SLOT(dataTransferProgress(qint64, qint64)));
Chris@208 126 m_ftp->connectToHost(url.host(), url.port(21));
Chris@208 127
Chris@208 128 QString username = url.userName();
Chris@208 129 if (username == "") {
Chris@208 130 username = "anonymous";
Chris@208 131 }
Chris@208 132
Chris@208 133 QString password = url.password();
Chris@208 134 if (password == "") {
Chris@208 135 password = QString("%1@%2").arg(getenv("USER")).arg(getenv("HOST"));
Chris@208 136 }
Chris@208 137
Chris@214 138 m_ftp->login(username, password);
Chris@214 139
Chris@214 140 QString dirpath = url.path().section('/', 0, -2);
Chris@214 141 QString filename = url.path().section('/', -1);
Chris@214 142
Chris@214 143 if (dirpath == "") dirpath = "/";
Chris@214 144 m_ftp->cd(dirpath);
Chris@214 145 m_ftp->get(filename, m_localFile);
Chris@208 146 }
Chris@208 147
Chris@208 148 if (m_ok) {
Chris@208 149 m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(url.toString()), tr("Cancel"), 0, 100);
Chris@210 150 m_progressDialog->hide();
Chris@210 151 connect(&m_progressShowTimer, SIGNAL(timeout()),
Chris@210 152 this, SLOT(showProgressDialog()));
Chris@210 153 connect(m_progressDialog, SIGNAL(canceled()), this, SLOT(cancelled()));
Chris@210 154 m_progressShowTimer.setSingleShot(true);
Chris@210 155 m_progressShowTimer.start(2000);
Chris@208 156 }
Chris@208 157 }
Chris@208 158
Chris@208 159 RemoteFile::~RemoteFile()
Chris@208 160 {
Chris@211 161 cleanup();
Chris@211 162 }
Chris@211 163
Chris@211 164 void
Chris@211 165 RemoteFile::cleanup()
Chris@211 166 {
Chris@211 167 // std::cerr << "RemoteFile::cleanup" << std::endl;
Chris@211 168 m_done = true;
Chris@214 169 if (m_http) {
Chris@214 170 delete m_http;
Chris@214 171 m_http = 0;
Chris@214 172 }
Chris@214 173 if (m_ftp) {
Chris@214 174 m_ftp->abort();
Chris@214 175 m_ftp->deleteLater();
Chris@214 176 m_ftp = 0;
Chris@214 177 }
Chris@211 178 delete m_progressDialog;
Chris@211 179 m_progressDialog = 0;
Chris@208 180 delete m_localFile;
Chris@211 181 m_localFile = 0;
Chris@208 182 }
Chris@208 183
Chris@208 184 bool
Chris@208 185 RemoteFile::canHandleScheme(QUrl url)
Chris@208 186 {
Chris@208 187 QString scheme = url.scheme().toLower();
Chris@208 188 return (scheme == "http" || scheme == "ftp");
Chris@208 189 }
Chris@208 190
Chris@210 191 bool
Chris@210 192 RemoteFile::isAvailable()
Chris@210 193 {
Chris@211 194 while (m_ok && (!m_done && m_lastStatus == 0)) {
Chris@210 195 QApplication::processEvents();
Chris@210 196 }
Chris@211 197 bool available = true;
Chris@211 198 if (!m_ok) available = false;
Chris@211 199 else available = (m_lastStatus / 100 == 2);
Chris@211 200 std::cerr << "RemoteFile::isAvailable: " << (available ? "yes" : "no")
Chris@211 201 << std::endl;
Chris@211 202 return available;
Chris@210 203 }
Chris@210 204
Chris@208 205 void
Chris@208 206 RemoteFile::wait()
Chris@208 207 {
Chris@211 208 while (m_ok && !m_done) {
Chris@208 209 QApplication::processEvents();
Chris@208 210 }
Chris@208 211 }
Chris@208 212
Chris@208 213 bool
Chris@208 214 RemoteFile::isOK() const
Chris@208 215 {
Chris@208 216 return m_ok;
Chris@208 217 }
Chris@208 218
Chris@208 219 bool
Chris@208 220 RemoteFile::isDone() const
Chris@208 221 {
Chris@208 222 return m_done;
Chris@208 223 }
Chris@208 224
Chris@208 225 QString
Chris@208 226 RemoteFile::getLocalFilename() const
Chris@208 227 {
Chris@208 228 return m_localFilename;
Chris@208 229 }
Chris@208 230
Chris@208 231 QString
Chris@208 232 RemoteFile::getErrorString() const
Chris@208 233 {
Chris@208 234 return m_errorString;
Chris@208 235 }
Chris@208 236
Chris@208 237 void
Chris@208 238 RemoteFile::dataReadProgress(int done, int total)
Chris@208 239 {
Chris@208 240 dataTransferProgress(done, total);
Chris@208 241 }
Chris@208 242
Chris@208 243 void
Chris@214 244 RemoteFile::httpResponseHeaderReceived(const QHttpResponseHeader &resp)
Chris@210 245 {
Chris@210 246 m_lastStatus = resp.statusCode();
Chris@210 247 if (m_lastStatus / 100 >= 4) {
Chris@210 248 m_errorString = QString("%1 %2")
Chris@210 249 .arg(resp.statusCode()).arg(resp.reasonPhrase());
Chris@211 250 std::cerr << "RemoteFile::responseHeaderReceived: "
Chris@211 251 << m_errorString.toStdString() << std::endl;
Chris@211 252 } else {
Chris@211 253 std::cerr << "RemoteFile::responseHeaderReceived: "
Chris@211 254 << m_lastStatus << std::endl;
Chris@211 255 }
Chris@210 256 }
Chris@210 257
Chris@210 258 void
Chris@214 259 RemoteFile::ftpCommandFinished(int id, bool error)
Chris@214 260 {
Chris@214 261 std::cerr << "RemoteFile::ftpCommandFinished(" << id << ", " << error << ")" << std::endl;
Chris@214 262
Chris@214 263 if (!m_ftp) return;
Chris@214 264
Chris@214 265 QFtp::Command command = m_ftp->currentCommand();
Chris@214 266
Chris@214 267 if (!error) {
Chris@214 268 std::cerr << "RemoteFile::ftpCommandFinished: success for command "
Chris@214 269 << command << std::endl;
Chris@214 270 return;
Chris@214 271 }
Chris@214 272
Chris@214 273 if (command == QFtp::ConnectToHost) {
Chris@214 274 m_errorString = tr("Failed to connect to FTP server");
Chris@214 275 } else if (command == QFtp::Login) {
Chris@214 276 m_errorString = tr("Login failed");
Chris@214 277 } else if (command == QFtp::Cd) {
Chris@214 278 m_errorString = tr("Failed to change to correct directory");
Chris@214 279 } else if (command == QFtp::Get) {
Chris@214 280 m_errorString = tr("FTP download aborted");
Chris@214 281 }
Chris@214 282
Chris@214 283 m_lastStatus = 400; // for done()
Chris@214 284 }
Chris@214 285
Chris@214 286 void
Chris@208 287 RemoteFile::dataTransferProgress(qint64 done, qint64 total)
Chris@208 288 {
Chris@211 289 if (!m_progressDialog) return;
Chris@211 290
Chris@208 291 int percent = int((double(done) / double(total)) * 100.0 - 0.1);
Chris@208 292 emit progress(percent);
Chris@208 293
Chris@211 294 if (percent > 0) {
Chris@211 295 m_progressDialog->setValue(percent);
Chris@211 296 m_progressDialog->show();
Chris@211 297 }
Chris@210 298 }
Chris@210 299
Chris@210 300 void
Chris@210 301 RemoteFile::cancelled()
Chris@210 302 {
Chris@211 303 deleteLocalFile();
Chris@210 304 m_done = true;
Chris@210 305 m_ok = false;
Chris@210 306 m_errorString = tr("Download cancelled");
Chris@208 307 }
Chris@208 308
Chris@208 309 void
Chris@208 310 RemoteFile::done(bool error)
Chris@208 311 {
Chris@214 312 std::cerr << "RemoteFile::done(" << error << ")" << std::endl;
Chris@211 313
Chris@211 314 if (m_done) return;
Chris@211 315
Chris@208 316 emit progress(100);
Chris@210 317
Chris@208 318 if (error) {
Chris@208 319 if (m_http) {
Chris@208 320 m_errorString = m_http->errorString();
Chris@208 321 } else if (m_ftp) {
Chris@208 322 m_errorString = m_ftp->errorString();
Chris@208 323 }
Chris@208 324 }
Chris@208 325
Chris@210 326 if (m_lastStatus / 100 >= 4) {
Chris@211 327 error = true;
Chris@210 328 }
Chris@210 329
Chris@211 330 cleanup();
Chris@208 331
Chris@211 332 if (!error) {
Chris@208 333 QFileInfo fi(m_localFilename);
Chris@208 334 if (!fi.exists()) {
Chris@208 335 m_errorString = tr("Failed to create local file %1").arg(m_localFilename);
Chris@211 336 error = true;
Chris@208 337 } else if (fi.size() == 0) {
Chris@208 338 m_errorString = tr("File contains no data!");
Chris@211 339 error = true;
Chris@208 340 }
Chris@208 341 }
Chris@211 342
Chris@211 343 if (error) {
Chris@211 344 deleteLocalFile();
Chris@211 345 }
Chris@211 346
Chris@211 347 m_ok = !error;
Chris@211 348 m_done = true;
Chris@211 349 }
Chris@211 350
Chris@211 351 void
Chris@211 352 RemoteFile::deleteLocalFile()
Chris@211 353 {
Chris@211 354 // std::cerr << "RemoteFile::deleteLocalFile" << std::endl;
Chris@211 355
Chris@211 356 cleanup();
Chris@211 357
Chris@211 358 if (m_localFilename == "") return;
Chris@211 359
Chris@211 360 m_fileCreationMutex.lock();
Chris@211 361
Chris@211 362 if (!QFile(m_localFilename).remove()) {
Chris@211 363 std::cerr << "RemoteFile::deleteLocalFile: ERROR: Failed to delete file \"" << m_localFilename.toStdString() << "\"" << std::endl;
Chris@211 364 } else {
Chris@211 365 m_localFilename = "";
Chris@211 366 }
Chris@211 367
Chris@211 368 m_fileCreationMutex.unlock();
Chris@211 369
Chris@208 370 m_done = true;
Chris@208 371 }
Chris@208 372
Chris@210 373 void
Chris@210 374 RemoteFile::showProgressDialog()
Chris@210 375 {
Chris@210 376 if (m_progressDialog) m_progressDialog->show();
Chris@210 377 }
Chris@210 378
Chris@208 379 QString
Chris@208 380 RemoteFile::createLocalFile(QUrl url)
Chris@208 381 {
Chris@208 382 QDir dir;
Chris@208 383 try {
Chris@208 384 dir = TempDirectory::getInstance()->getSubDirectoryPath("download");
Chris@208 385 } catch (DirectoryCreationFailed f) {
Chris@208 386 std::cerr << "RemoteFile::createLocalFile: ERROR: Failed to create temporary directory: " << f.what() << std::endl;
Chris@208 387 return "";
Chris@208 388 }
Chris@208 389
Chris@208 390 QString filepart = url.path().section('/', -1, -1,
Chris@208 391 QString::SectionSkipEmpty);
Chris@208 392
Chris@208 393 QString extension = filepart.section('.', -1);
Chris@208 394 QString base = filepart;
Chris@208 395 if (extension != "") {
Chris@208 396 base = base.left(base.length() - extension.length() - 1);
Chris@208 397 }
Chris@208 398 if (base == "") base = "remote";
Chris@208 399
Chris@208 400 QString filename;
Chris@208 401
Chris@208 402 if (extension == "") {
Chris@208 403 filename = base;
Chris@208 404 } else {
Chris@208 405 filename = QString("%1.%2").arg(base).arg(extension);
Chris@208 406 }
Chris@208 407
Chris@208 408 QString filepath(dir.filePath(filename));
Chris@208 409
Chris@208 410 std::cerr << "RemoteFile::createLocalFile: URL is \"" << url.toString().toStdString() << "\", dir is \"" << dir.path().toStdString() << "\", base \"" << base.toStdString() << "\", extension \"" << extension.toStdString() << "\", filebase \"" << filename.toStdString() << "\", filename \"" << filepath.toStdString() << "\"" << std::endl;
Chris@208 411
Chris@208 412 m_fileCreationMutex.lock();
Chris@208 413 ++m_count;
Chris@208 414
Chris@208 415 if (QFileInfo(filepath).exists() ||
Chris@208 416 !QFile(filepath).open(QFile::WriteOnly)) {
Chris@208 417
Chris@208 418 std::cerr << "RemoteFile::createLocalFile: Failed to create local file \""
Chris@208 419 << filepath.toStdString() << "\" for URL \""
Chris@208 420 << url.toString().toStdString() << "\" (or file already exists): appending suffix instead" << std::endl;
Chris@208 421
Chris@208 422
Chris@208 423 if (extension == "") {
Chris@208 424 filename = QString("%1_%2").arg(base).arg(m_count);
Chris@208 425 } else {
Chris@208 426 filename = QString("%1_%2.%3").arg(base).arg(m_count).arg(extension);
Chris@208 427 }
Chris@208 428 filepath = dir.filePath(filename);
Chris@208 429
Chris@208 430 if (QFileInfo(filepath).exists() ||
Chris@208 431 !QFile(filepath).open(QFile::WriteOnly)) {
Chris@208 432
Chris@208 433 std::cerr << "RemoteFile::createLocalFile: ERROR: Failed to create local file \""
Chris@208 434 << filepath.toStdString() << "\" for URL \""
Chris@208 435 << url.toString().toStdString() << "\" (or file already exists)" << std::endl;
Chris@208 436
Chris@208 437 m_fileCreationMutex.unlock();
Chris@208 438 return "";
Chris@208 439 }
Chris@208 440 }
Chris@208 441
Chris@208 442 m_fileCreationMutex.unlock();
Chris@208 443
Chris@208 444 std::cerr << "RemoteFile::createLocalFile: url "
Chris@208 445 << url.toString().toStdString() << " -> local filename "
Chris@208 446 << filepath.toStdString() << std::endl;
Chris@208 447
Chris@208 448 return filepath;
Chris@208 449 }