Code clean up

This commit is contained in:
Christophe Dumez 2010-12-30 19:12:03 +00:00
parent a9fbef62b6
commit 5696944c6f
7 changed files with 57 additions and 69 deletions

View file

@ -779,10 +779,13 @@ void MainWindow::dropEvent(QDropEvent *event) {
if(event->mimeData()->hasUrls()) { if(event->mimeData()->hasUrls()) {
const QList<QUrl> urls = event->mimeData()->urls(); const QList<QUrl> urls = event->mimeData()->urls();
foreach(const QUrl &url, urls) { foreach(const QUrl &url, urls) {
const QString tmp = url.toString().trimmed(); if(!url.isEmpty()) {
if(!tmp.isEmpty()) if(url.scheme().compare("file", Qt::CaseInsensitive) == 0)
files << url.toLocalFile();
else
files << url.toString(); files << url.toString();
} }
}
} else { } else {
files = event->mimeData()->text().split(QString::fromUtf8("\n")); files = event->mimeData()->text().split(QString::fromUtf8("\n"));
} }
@ -790,13 +793,8 @@ void MainWindow::dropEvent(QDropEvent *event) {
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool(); const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool();
foreach(QString file, files) { foreach(QString file, files) {
#ifdef Q_WS_WIN qDebug("Dropped file %s on download list", qPrintable(file));
file = file.trimmed().replace(QString::fromUtf8("file:///"), QString::fromUtf8(""), Qt::CaseInsensitive); if(misc::isUrl(file)) {
#else
file = file.trimmed().replace(QString::fromUtf8("file://"), QString::fromUtf8(""), Qt::CaseInsensitive);
#endif
qDebug("Dropped file %s on download list", file.toLocal8Bit().data());
if(file.startsWith(QString::fromUtf8("http://"), Qt::CaseInsensitive) || file.startsWith(QString::fromUtf8("ftp://"), Qt::CaseInsensitive) || file.startsWith(QString::fromUtf8("https://"), Qt::CaseInsensitive)) {
QBtSession::instance()->downloadFromUrl(file); QBtSession::instance()->downloadFromUrl(file);
continue; continue;
} }
@ -874,7 +872,7 @@ void MainWindow::processParams(const QStringList& params) {
const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool(); const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool();
foreach(QString param, params) { foreach(QString param, params) {
param = param.trimmed(); param = param.trimmed();
if(param.startsWith(QString::fromUtf8("http://"), Qt::CaseInsensitive) || param.startsWith(QString::fromUtf8("ftp://"), Qt::CaseInsensitive) || param.startsWith(QString::fromUtf8("https://"), Qt::CaseInsensitive)) { if(misc::isUrl(param)) {
QBtSession::instance()->downloadFromUrl(param); QBtSession::instance()->downloadFromUrl(param);
}else{ }else{
if(param.startsWith("bc://bt/", Qt::CaseInsensitive)) { if(param.startsWith("bc://bt/", Qt::CaseInsensitive)) {

View file

@ -752,3 +752,19 @@ QString misc::branchPath(QString file_path)
file_path.replace("\\", "/"); file_path.replace("\\", "/");
return file_path.left(file_path.lastIndexOf('/')); return file_path.left(file_path.lastIndexOf('/'));
} }
bool misc::isUrl(const QString &s)
{
const QString scheme = QUrl(s).scheme();
QRegExp is_url("http[s]?|ftp", Qt::CaseInsensitive);
return is_url.exactMatch(scheme);
}
QString misc::fileName(QString file_path)
{
file_path.replace("\\", "/");
const int slash_index = file_path.lastIndexOf('/');
if(slash_index == -1)
return file_path;
return file_path.mid(slash_index);
}

View file

@ -156,6 +156,7 @@ public:
static QString updateLabelInSavePath(const QString& defaultSavePath, const QString &save_path, const QString &old_label, const QString &new_label); static QString updateLabelInSavePath(const QString& defaultSavePath, const QString &save_path, const QString &old_label, const QString &new_label);
static bool sameFiles(const QString &path1, const QString &path2); static bool sameFiles(const QString &path1, const QString &path2);
static bool isUrl(const QString &s);
static void copyDir(QString src_path, QString dst_path); static void copyDir(QString src_path, QString dst_path);
static QString toValidFileSystemName(QString filename); static QString toValidFileSystemName(QString filename);
static bool isValidFileSystemName(QString filename); static bool isValidFileSystemName(QString filename);
@ -180,6 +181,7 @@ public:
static QString friendlyUnit(double val); static QString friendlyUnit(double val);
static bool isPreviewable(QString extension); static bool isPreviewable(QString extension);
static QString branchPath(QString file_path); static QString branchPath(QString file_path);
static QString fileName(QString file_path);
static QString magnetUriToName(QString magnet_uri); static QString magnetUriToName(QString magnet_uri);
static QString magnetUriToHash(QString magnet_uri); static QString magnetUriToHash(QString magnet_uri);
static QString bcLinkToMagnet(QString bc_link); static QString bcLinkToMagnet(QString bc_link);

View file

@ -451,11 +451,7 @@ void PropertiesWidget::openDoubleClickedFile(QModelIndex index) {
h.flush_cache(); h.flush_cache();
#endif #endif
if(QFile::exists(file_path)) { if(QFile::exists(file_path)) {
#ifdef Q_WS_WIN QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
QDesktopServices::openUrl(QUrl("file:///"+file_path));
#else
QDesktopServices::openUrl(QUrl("file://"+file_path));
#endif
} else { } else {
QMessageBox::warning(this, tr("I/O Error"), tr("This file does not exist yet.")); QMessageBox::warning(this, tr("I/O Error"), tr("This file does not exist yet."));
} }
@ -477,11 +473,7 @@ void PropertiesWidget::openDoubleClickedFile(QModelIndex index) {
h.flush_cache(); h.flush_cache();
#endif #endif
if(QFile::exists(file_path)) { if(QFile::exists(file_path)) {
#ifdef Q_WS_WIN QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
QDesktopServices::openUrl(QUrl("file:///"+file_path));
#else
QDesktopServices::openUrl(QUrl("file://"+file_path));
#endif
} else { } else {
QMessageBox::warning(this, tr("I/O Error"), tr("This folder does not exist yet.")); QMessageBox::warning(this, tr("I/O Error"), tr("This folder does not exist yet."));
} }

View file

@ -856,36 +856,32 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
if(!path.endsWith(".torrent")) if(!path.endsWith(".torrent"))
if(QFile::rename(path, path+".torrent")) path += ".torrent"; if(QFile::rename(path, path+".torrent")) path += ".torrent";
#endif #endif
#ifdef Q_WS_WIN if(path.startsWith("file:", Qt::CaseInsensitive))
const QString file = path.trimmed().replace("file:///", "", Qt::CaseInsensitive); path = QUrl(path).toLocalFile();
#else if(path.isEmpty()) return h;
const QString file = path.trimmed().replace("file://", "", Qt::CaseInsensitive);
#endif
if(file.isEmpty()) return h;
Q_ASSERT(!file.startsWith("http://", Qt::CaseInsensitive) && !file.startsWith("https://", Qt::CaseInsensitive) Q_ASSERT(!misc::isUrl(path));
&& !file.startsWith("ftp://", Qt::CaseInsensitive));
qDebug("Adding %s to download list", qPrintable(file)); qDebug("Adding %s to download list", qPrintable(path));
boost::intrusive_ptr<torrent_info> t; boost::intrusive_ptr<torrent_info> t;
try { try {
// Getting torrent file informations // Getting torrent file informations
t = new torrent_info(file.toUtf8().constData()); t = new torrent_info(path.toUtf8().constData());
if(!t->is_valid()) if(!t->is_valid())
throw std::exception(); throw std::exception();
} catch(std::exception&) { } catch(std::exception&) {
if(!from_url.isNull()) { if(!from_url.isNull()) {
addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(from_url), QString::fromUtf8("red")); addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(from_url), QString::fromUtf8("red"));
//emit invalidTorrent(from_url); //emit invalidTorrent(from_url);
misc::safeRemove(file); misc::safeRemove(path);
}else{ }else{
addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(file), QString::fromUtf8("red")); addConsoleMessage(tr("Unable to decode torrent file: '%1'", "e.g: Unable to decode torrent file: '/home/y/xxx.torrent'").arg(path), QString::fromUtf8("red"));
//emit invalidTorrent(file); //emit invalidTorrent(path);
} }
addConsoleMessage(tr("This file is either corrupted or this isn't a torrent."),QString::fromUtf8("red")); addConsoleMessage(tr("This file is either corrupted or this isn't a torrent."),QString::fromUtf8("red"));
if(fromScanDir) { if(fromScanDir) {
// Remove file // Remove file
misc::safeRemove(file); misc::safeRemove(path);
} }
return h; return h;
} }
@ -902,14 +898,14 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
if(!from_url.isNull()) { if(!from_url.isNull()) {
addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(from_url)); addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(from_url));
}else{ }else{
addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(file)); addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(path));
} }
// Check if the torrent contains trackers or url seeds we don't know about // Check if the torrent contains trackers or url seeds we don't know about
// and add them // and add them
mergeTorrents(getTorrentHandle(hash), t); mergeTorrents(getTorrentHandle(hash), t);
// Delete file if temporary // Delete file if temporary
if(!from_url.isNull() || fromScanDir) misc::safeRemove(file); if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
return h; return h;
} }
@ -917,7 +913,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
if(t->num_files() < 1) { if(t->num_files() < 1) {
addConsoleMessage(tr("Error: The torrent %1 does not contain any file.").arg(misc::toQStringU(t->name()))); addConsoleMessage(tr("Error: The torrent %1 does not contain any file.").arg(misc::toQStringU(t->name())));
// Delete file if temporary // Delete file if temporary
if(!from_url.isNull() || fromScanDir) misc::safeRemove(file); if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
return h; return h;
} }
@ -978,7 +974,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
// Check if it worked // Check if it worked
if(!h.is_valid()) { if(!h.is_valid()) {
qDebug("/!\\ Error: Invalid handle"); qDebug("/!\\ Error: Invalid handle");
if(!from_url.isNull()) misc::safeRemove(file); if(!from_url.isNull()) misc::safeRemove(path);
return h; return h;
} }
// Remember root folder // Remember root folder
@ -1010,8 +1006,8 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
#endif #endif
// Backup torrent file // Backup torrent file
const QString newFile = torrentBackup.absoluteFilePath(hash + ".torrent"); const QString newFile = torrentBackup.absoluteFilePath(hash + ".torrent");
if(file != newFile) if(path != newFile)
QFile::copy(file, newFile); QFile::copy(path, newFile);
// Copy the torrent file to the export folder // Copy the torrent file to the export folder
if(torrentExport) if(torrentExport)
exportTorrentFile(h); exportTorrentFile(h);
@ -1023,7 +1019,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
} }
// If temporary file, remove it // If temporary file, remove it
if(!from_url.isNull() || fromScanDir) misc::safeRemove(file); if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
// Display console message // Display console message
if(!from_url.isNull()) { if(!from_url.isNull()) {
@ -1033,9 +1029,9 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(from_url)); addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(from_url));
}else{ }else{
if(fastResume) if(fastResume)
addConsoleMessage(tr("'%1' resumed. (fast resume)", "'/home/y/xxx.torrent' was resumed. (fast resume)").arg(file)); addConsoleMessage(tr("'%1' resumed. (fast resume)", "'/home/y/xxx.torrent' was resumed. (fast resume)").arg(path));
else else
addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(file)); addConsoleMessage(tr("'%1' added to download list.", "'/home/y/xxx.torrent' was added to download list.").arg(path));
} }
// Send torrent addition signal // Send torrent addition signal

View file

@ -75,22 +75,18 @@ engineSelectDlg::~engineSelectDlg() {
void engineSelectDlg::dropEvent(QDropEvent *event) { void engineSelectDlg::dropEvent(QDropEvent *event) {
event->acceptProposedAction(); event->acceptProposedAction();
QStringList files=event->mimeData()->text().split(QString::fromUtf8("\n")); QStringList files=event->mimeData()->text().split(QString::fromUtf8("\n"));
QString file; foreach(QString file, files) {
foreach(file, files) {
qDebug("dropped %s", qPrintable(file)); qDebug("dropped %s", qPrintable(file));
#ifdef Q_WS_WIN if(misc::isUrl(file)) {
file = file.replace("file:///", "");
#else
file = file.replace("file://", "");
#endif
if(file.startsWith("http://", Qt::CaseInsensitive) || file.startsWith("https://", Qt::CaseInsensitive) || file.startsWith("ftp://", Qt::CaseInsensitive)) {
setCursor(QCursor(Qt::WaitCursor)); setCursor(QCursor(Qt::WaitCursor));
downloader->downloadUrl(file); downloader->downloadUrl(file);
continue; continue;
} }
if(file.endsWith(".py", Qt::CaseInsensitive)) { if(file.endsWith(".py", Qt::CaseInsensitive)) {
QString plugin_name = file.split(QDir::separator()).last(); if(file.startsWith("file:", Qt::CaseInsensitive))
plugin_name.replace(".py", ""); file = QUrl(file).toLocalFile();
QString plugin_name = misc::fileName(file);
plugin_name.chop(3); // Remove extension
installPlugin(file, plugin_name); installPlugin(file, plugin_name);
} }
} }

View file

@ -139,11 +139,7 @@ TorrentModel* TransferListWidget::getSourceModel() const {
} }
void TransferListWidget::previewFile(QString filePath) { void TransferListWidget::previewFile(QString filePath) {
#ifdef Q_WS_WIN QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
QDesktopServices::openUrl(QUrl(QString("file:///")+filePath));
#else
QDesktopServices::openUrl(QUrl(QString("file://")+filePath));
#endif
} }
void TransferListWidget::setRefreshInterval(int t) { void TransferListWidget::setRefreshInterval(int t) {
@ -201,11 +197,7 @@ void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) {
} }
break; break;
case OPEN_DEST: case OPEN_DEST:
#ifdef Q_WS_WIN QDesktopServices::openUrl(QUrl::fromLocalFile(h.save_path()));
QDesktopServices::openUrl(QUrl("file:///" + h.save_path()));
#else
QDesktopServices::openUrl(QUrl("file://" + h.save_path()));
#endif
break; break;
} }
} }
@ -413,11 +405,7 @@ void TransferListWidget::openSelectedTorrentsFolder() const {
qDebug("Opening path at %s", qPrintable(savePath)); qDebug("Opening path at %s", qPrintable(savePath));
if(!pathsList.contains(savePath)) { if(!pathsList.contains(savePath)) {
pathsList.append(savePath); pathsList.append(savePath);
#ifdef Q_WS_WIN QDesktopServices::openUrl(QUrl::fromLocalFile(savePath));
QDesktopServices::openUrl(QUrl(QString("file:///")+savePath));
#else
QDesktopServices::openUrl(QUrl(QString("file://")+savePath));
#endif
} }
} }
} }