From 908481885c879697dfcd0848837207ce1732e77b Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 28 Apr 2016 19:06:02 +0800 Subject: [PATCH] Enable drag n drop to create torrent on mainwindow --- src/gui/mainwindow.cpp | 32 +++++++++++++++++++++++++++++--- src/gui/mainwindow.h | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index e9be2b2bb..d9303eb97 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1068,11 +1068,16 @@ void MainWindow::closeEvent(QCloseEvent *e) // Display window to create a torrent void MainWindow::on_actionCreateTorrent_triggered() +{ + createTorrentTriggered(); +} + +void MainWindow::createTorrentTriggered(const QString &path) { if (m_createTorrentDlg) m_createTorrentDlg->setFocus(); else - m_createTorrentDlg = new TorrentCreatorDlg(this); + m_createTorrentDlg = new TorrentCreatorDlg(this, path); } bool MainWindow::event(QEvent *e) @@ -1126,6 +1131,8 @@ bool MainWindow::event(QEvent *e) void MainWindow::dropEvent(QDropEvent *event) { event->acceptProposedAction(); + + // remove scheme QStringList files; if (event->mimeData()->hasUrls()) { const QList urls = event->mimeData()->urls(); @@ -1142,15 +1149,34 @@ void MainWindow::dropEvent(QDropEvent *event) files = event->mimeData()->text().split('\n'); } - // Add file to download list + // differentiate ".torrent" files and others + QStringList torrentFiles, otherFiles; + foreach (const QString &file, files) { + if (file.endsWith(".torrent", Qt::CaseInsensitive)) + torrentFiles << file; + else + otherFiles << file; + } + + // Download torrents const bool useTorrentAdditionDialog = AddNewTorrentDialog::isEnabled(); - foreach (QString file, files) { + foreach (const QString &file, torrentFiles) { qDebug("Dropped file %s on download list", qPrintable(file)); if (useTorrentAdditionDialog) AddNewTorrentDialog::show(file, this); else BitTorrent::Session::instance()->addTorrent(file); } + if (!torrentFiles.isEmpty()) return; + + // Create torrent + foreach (const QString &file, otherFiles) { + createTorrentTriggered(file); + + // currently only hande the first entry + // this is a stub that can be expanded later to create many torrents at once + break; + } } // Decode if we accept drag 'n drop or not diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index f764bca29..a64af6041 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -202,6 +202,7 @@ private: bool event(QEvent *e) override; void displayRSSTab(bool enable); void displaySearchTab(bool enable); + void createTorrentTriggered(const QString &path = QString()); Ui::MainWindow *m_ui;