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,9 +779,12 @@ void MainWindow::dropEvent(QDropEvent *event) {
if(event->mimeData()->hasUrls()) {
const QList<QUrl> urls = event->mimeData()->urls();
foreach(const QUrl &url, urls) {
const QString tmp = url.toString().trimmed();
if(!tmp.isEmpty())
files << url.toString();
if(!url.isEmpty()) {
if(url.scheme().compare("file", Qt::CaseInsensitive) == 0)
files << url.toLocalFile();
else
files << url.toString();
}
}
} else {
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"));
const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool();
foreach(QString file, files) {
#ifdef Q_WS_WIN
file = file.trimmed().replace(QString::fromUtf8("file:///"), QString::fromUtf8(""), Qt::CaseInsensitive);
#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)) {
qDebug("Dropped file %s on download list", qPrintable(file));
if(misc::isUrl(file)) {
QBtSession::instance()->downloadFromUrl(file);
continue;
}
@ -874,7 +872,7 @@ void MainWindow::processParams(const QStringList& params) {
const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool();
foreach(QString param, params) {
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);
}else{
if(param.startsWith("bc://bt/", Qt::CaseInsensitive)) {

View file

@ -752,3 +752,19 @@ QString misc::branchPath(QString file_path)
file_path.replace("\\", "/");
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 bool sameFiles(const QString &path1, const QString &path2);
static bool isUrl(const QString &s);
static void copyDir(QString src_path, QString dst_path);
static QString toValidFileSystemName(QString filename);
static bool isValidFileSystemName(QString filename);
@ -180,6 +181,7 @@ public:
static QString friendlyUnit(double val);
static bool isPreviewable(QString extension);
static QString branchPath(QString file_path);
static QString fileName(QString file_path);
static QString magnetUriToName(QString magnet_uri);
static QString magnetUriToHash(QString magnet_uri);
static QString bcLinkToMagnet(QString bc_link);

View file

@ -451,11 +451,7 @@ void PropertiesWidget::openDoubleClickedFile(QModelIndex index) {
h.flush_cache();
#endif
if(QFile::exists(file_path)) {
#ifdef Q_WS_WIN
QDesktopServices::openUrl(QUrl("file:///"+file_path));
#else
QDesktopServices::openUrl(QUrl("file://"+file_path));
#endif
QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
} else {
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();
#endif
if(QFile::exists(file_path)) {
#ifdef Q_WS_WIN
QDesktopServices::openUrl(QUrl("file:///"+file_path));
#else
QDesktopServices::openUrl(QUrl("file://"+file_path));
#endif
QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
} else {
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(QFile::rename(path, path+".torrent")) path += ".torrent";
#endif
#ifdef Q_WS_WIN
const QString file = path.trimmed().replace("file:///", "", Qt::CaseInsensitive);
#else
const QString file = path.trimmed().replace("file://", "", Qt::CaseInsensitive);
#endif
if(file.isEmpty()) return h;
if(path.startsWith("file:", Qt::CaseInsensitive))
path = QUrl(path).toLocalFile();
if(path.isEmpty()) return h;
Q_ASSERT(!file.startsWith("http://", Qt::CaseInsensitive) && !file.startsWith("https://", Qt::CaseInsensitive)
&& !file.startsWith("ftp://", Qt::CaseInsensitive));
Q_ASSERT(!misc::isUrl(path));
qDebug("Adding %s to download list", qPrintable(file));
qDebug("Adding %s to download list", qPrintable(path));
boost::intrusive_ptr<torrent_info> t;
try {
// Getting torrent file informations
t = new torrent_info(file.toUtf8().constData());
t = new torrent_info(path.toUtf8().constData());
if(!t->is_valid())
throw std::exception();
} catch(std::exception&) {
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"));
//emit invalidTorrent(from_url);
misc::safeRemove(file);
misc::safeRemove(path);
}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"));
//emit invalidTorrent(file);
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(path);
}
addConsoleMessage(tr("This file is either corrupted or this isn't a torrent."),QString::fromUtf8("red"));
if(fromScanDir) {
// Remove file
misc::safeRemove(file);
misc::safeRemove(path);
}
return h;
}
@ -902,14 +898,14 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
if(!from_url.isNull()) {
addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(from_url));
}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
// and add them
mergeTorrents(getTorrentHandle(hash), t);
// Delete file if temporary
if(!from_url.isNull() || fromScanDir) misc::safeRemove(file);
if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
return h;
}
@ -917,7 +913,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
if(t->num_files() < 1) {
addConsoleMessage(tr("Error: The torrent %1 does not contain any file.").arg(misc::toQStringU(t->name())));
// Delete file if temporary
if(!from_url.isNull() || fromScanDir) misc::safeRemove(file);
if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
return h;
}
@ -978,7 +974,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
// Check if it worked
if(!h.is_valid()) {
qDebug("/!\\ Error: Invalid handle");
if(!from_url.isNull()) misc::safeRemove(file);
if(!from_url.isNull()) misc::safeRemove(path);
return h;
}
// Remember root folder
@ -1010,8 +1006,8 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
#endif
// Backup torrent file
const QString newFile = torrentBackup.absoluteFilePath(hash + ".torrent");
if(file != newFile)
QFile::copy(file, newFile);
if(path != newFile)
QFile::copy(path, newFile);
// Copy the torrent file to the export folder
if(torrentExport)
exportTorrentFile(h);
@ -1023,7 +1019,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
}
// If temporary file, remove it
if(!from_url.isNull() || fromScanDir) misc::safeRemove(file);
if(!from_url.isNull() || fromScanDir) misc::safeRemove(path);
// Display console message
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));
}else{
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
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

View file

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

View file

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