summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Tanner <thosrtanner2@users.sourceforge.net>2015-04-26 09:44:57 +0100
committerTom Tanner <thosrtanner2@users.sourceforge.net>2015-04-26 09:44:57 +0100
commitce925bc73e077fea813e509af4e9ad19f4306960 (patch)
treee028eb1942e1e3a11b4b9f7af0e20707baf44ce6
parent927504cbd947a0cd794ed4777933df12bd0cd7e8 (diff)
Allows drag-and-drop into the download tab
-rw-r--r--src/downloadmanager.h14
-rw-r--r--src/mainwindow.cpp117
-rw-r--r--src/mainwindow.h2
-rw-r--r--src/mainwindow.ui8
4 files changed, 140 insertions, 1 deletions
diff --git a/src/downloadmanager.h b/src/downloadmanager.h
index c077ae79..5d047766 100644
--- a/src/downloadmanager.h
+++ b/src/downloadmanager.h
@@ -426,8 +426,22 @@ private slots:
private:
void createMetaFile(DownloadInfo *info);
+
+public:
+
+ /** Get a unique filename for a download.
+ *
+ * This allows you multiple versions of download files, useful if the file
+ * comes from a web site with no version control
+ *
+ * @param basename: Name of the file
+ *
+ * @return Unique(ish) name
+ */
QString getDownloadFileName(const QString &baseName) const;
+private:
+
void startDownload(QNetworkReply *reply, DownloadInfo *newDownload, bool resume);
void resumeDownloadInt(int index);
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 1566bc8a..945b258c 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -110,6 +110,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
+#include <QMimeData>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtConcurrent/QtConcurrentRun>
#else
@@ -4547,3 +4548,119 @@ void MainWindow::on_manageArchivesBox_toggled(bool)
{
m_OrganizerCore.refreshBSAList();
}
+
+void MainWindow::dragEnterEvent(QDragEnterEvent *event)
+{
+ //Accept copy or move drags to the download window. Link drags are not
+ //meaningful (Well, they are - we could drop a link in the download folder,
+ //but you need privileges to do that).
+ if (ui->downloadTab->isVisible() &&
+ (event->proposedAction() == Qt::CopyAction ||
+ event->proposedAction() == Qt::MoveAction) &&
+ event->answerRect().intersects(ui->downloadTab->rect())) {
+
+ //If I read the documentation right, this won't work under a motif windows
+ //manager and the check needs to be done at the drop. However, that means
+ //the user might be allowed to drop things which we can't sanely process
+ QMimeData const *data = event->mimeData();
+
+ if (data->hasUrls()) {
+ QStringList extensions =
+ m_OrganizerCore.installationManager()->getSupportedExtensions();
+
+ //This is probably OK - scan to see if these are moderately sane archive
+ //types
+ QList<QUrl> urls = data->urls();
+ bool ok = true;
+ for (auto url : urls) {
+ if (url.isLocalFile()) {
+ QString local = url.toLocalFile();
+ bool fok = false;
+ for (auto ext : extensions) {
+ if (local.endsWith(ext, Qt::CaseInsensitive)) {
+ fok = true;
+ break;
+ }
+ }
+ if (! fok) {
+ ok = false;
+ break;
+ }
+ }
+ //We should probably allow the user to drop urls here and remember the
+ //url for installing later.
+ }
+ if (ok) {
+ event->accept();
+ }
+ }
+ }
+}
+
+void MainWindow::dropEvent(QDropEvent *event)
+{
+ Qt::DropAction action = event->proposedAction();
+ QString output_dir = m_OrganizerCore.downloadManager()->getOutputDirectory();
+ if (action == Qt::MoveAction) {
+ //Tell windows I'm taking control and will delete the source of a move.
+ event->setDropAction(Qt::TargetMoveAction);
+ }
+ for (auto url : event->mimeData()->urls()) {
+ QFileInfo file(url.toLocalFile());
+ QString target = output_dir + "/" + file.fileName();
+ //FIXME: Check here if the file exists and offer a rename
+ if (QFile::exists(target)) {
+ //Make this box have 2 buttons:
+ //'overwrite' and 'rename'
+ //for copy it's ok to add a 'do nothing' button
+ QMessageBox box(QMessageBox::Question,
+ file.fileName(),
+ tr("A file with the same name has already been downloaded. "
+ "What would you like to do?"));
+ box.addButton(tr("Overwrite"), QMessageBox::ActionRole);
+ box.addButton(tr("Rename new file"), QMessageBox::YesRole);
+ box.addButton(tr("Ignore file"), QMessageBox::RejectRole);
+
+ box.exec();
+ QMessageBox::ButtonRole role = box.buttonRole(box.clickedButton());
+ switch (role)
+ {
+ case QMessageBox::RejectRole:
+ continue;
+
+ case QMessageBox::ActionRole:
+ break;
+
+ default:
+ case QMessageBox::YesRole:
+ target = m_OrganizerCore.downloadManager()->getDownloadFileName(file.fileName());
+ break;
+
+ }
+ }
+
+ //We should really show progress and add a cancel button for these in case
+ //the operation takes a long time.
+ if (action == Qt::MoveAction) {
+ if (MoveFileExW(file.absoluteFilePath().toStdWString().c_str(),
+ target.toStdWString().c_str(),
+ MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) {
+ continue;
+ }
+ }
+ else {
+ if (CopyFileW(file.absoluteFilePath().toStdWString().c_str(),
+ target.toStdWString().c_str(),
+ FALSE) != 0) {
+ continue;
+ }
+ }
+ //Something has gone horribly wrong here
+ qDebug("error %d", GetLastError());
+ if (action == Qt::MoveAction) {
+ //Don't let the original source get deleted on a move
+ event->setDropAction(Qt::TargetMoveAction);
+ }
+ }
+ event->accept();
+}
diff --git a/src/mainwindow.h b/src/mainwindow.h
index 88419594..ddea1010 100644
--- a/src/mainwindow.h
+++ b/src/mainwindow.h
@@ -157,6 +157,8 @@ protected:
virtual void closeEvent(QCloseEvent *event);
virtual bool eventFilter(QObject *obj, QEvent *event);
virtual void resizeEvent(QResizeEvent *event);
+ virtual void dragEnterEvent(QDragEnterEvent *event);
+ virtual void dropEvent(QDropEvent *event);
private:
diff --git a/src/mainwindow.ui b/src/mainwindow.ui
index 4201babf..ca93ea54 100644
--- a/src/mainwindow.ui
+++ b/src/mainwindow.ui
@@ -10,6 +10,9 @@
<height>710</height>
</rect>
</property>
+ <property name="acceptDrops">
+ <bool>true</bool>
+ </property>
<property name="windowTitle">
<string notr="true">Mod Organizer</string>
</property>
@@ -1140,11 +1143,14 @@ p, li { white-space: pre-wrap; }
<property name="contextMenuPolicy">
<enum>Qt::PreventContextMenu</enum>
</property>
+ <property name="acceptDrops">
+ <bool>true</bool>
+ </property>
<property name="toolTip">
<string/>
</property>
<property name="whatsThis">
- <string>This is a list of mods you downloaded from Nexus. Double click one to install it.</string>
+ <string>This is a list of mods you downloaded from Nexus. Double click one to install it. You can also drag an archive into here.</string>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>