Enable drag n drop to create torrent on mainwindow

This commit is contained in:
Chocobo1 2016-04-28 19:06:02 +08:00
parent 386b93bb0f
commit 908481885c
2 changed files with 30 additions and 3 deletions

View file

@ -1068,11 +1068,16 @@ void MainWindow::closeEvent(QCloseEvent *e)
// Display window to create a torrent // Display window to create a torrent
void MainWindow::on_actionCreateTorrent_triggered() void MainWindow::on_actionCreateTorrent_triggered()
{
createTorrentTriggered();
}
void MainWindow::createTorrentTriggered(const QString &path)
{ {
if (m_createTorrentDlg) if (m_createTorrentDlg)
m_createTorrentDlg->setFocus(); m_createTorrentDlg->setFocus();
else else
m_createTorrentDlg = new TorrentCreatorDlg(this); m_createTorrentDlg = new TorrentCreatorDlg(this, path);
} }
bool MainWindow::event(QEvent *e) bool MainWindow::event(QEvent *e)
@ -1126,6 +1131,8 @@ bool MainWindow::event(QEvent *e)
void MainWindow::dropEvent(QDropEvent *event) void MainWindow::dropEvent(QDropEvent *event)
{ {
event->acceptProposedAction(); event->acceptProposedAction();
// remove scheme
QStringList files; QStringList files;
if (event->mimeData()->hasUrls()) { if (event->mimeData()->hasUrls()) {
const QList<QUrl> urls = event->mimeData()->urls(); const QList<QUrl> urls = event->mimeData()->urls();
@ -1142,15 +1149,34 @@ void MainWindow::dropEvent(QDropEvent *event)
files = event->mimeData()->text().split('\n'); 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(); const bool useTorrentAdditionDialog = AddNewTorrentDialog::isEnabled();
foreach (QString file, files) { foreach (const QString &file, torrentFiles) {
qDebug("Dropped file %s on download list", qPrintable(file)); qDebug("Dropped file %s on download list", qPrintable(file));
if (useTorrentAdditionDialog) if (useTorrentAdditionDialog)
AddNewTorrentDialog::show(file, this); AddNewTorrentDialog::show(file, this);
else else
BitTorrent::Session::instance()->addTorrent(file); 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 // Decode if we accept drag 'n drop or not

View file

@ -202,6 +202,7 @@ private:
bool event(QEvent *e) override; bool event(QEvent *e) override;
void displayRSSTab(bool enable); void displayRSSTab(bool enable);
void displaySearchTab(bool enable); void displaySearchTab(bool enable);
void createTorrentTriggered(const QString &path = QString());
Ui::MainWindow *m_ui; Ui::MainWindow *m_ui;