Make the file save dialog async
Too often I got hangs because of the nested event loop started by exec(): therefore everything is moved to a QPointer-guarded QFileDialog pointer which is then used asynchronously.
This commit is contained in:
		
					parent
					
						
							
								bff3d33fc3
							
						
					
				
			
			
				commit
				
					
						2abc76e311
					
				
			
		
					 1 changed files with 22 additions and 29 deletions
				
			
		| 
						 | 
				
			
			@ -30,6 +30,7 @@
 | 
			
		|||
#include <QDockWidget>
 | 
			
		||||
#include <QQuickItem>
 | 
			
		||||
#include <QFileDialog>
 | 
			
		||||
#include <QPointer>
 | 
			
		||||
#include <QSortFilterProxyModel>
 | 
			
		||||
#include <QStatusBar>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -478,24 +479,27 @@ void DanbooruMainWindow::slotHandleDownload(const QUrl &url, const QVariant tags
 | 
			
		|||
 | 
			
		||||
    QStringList tagList = tags.toStringList();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    KIO::MimetypeJob *mimeJob = KIO::mimetype(url, KIO::HideProgressInfo);
 | 
			
		||||
 | 
			
		||||
    QString remoteFile = url.fileName();
 | 
			
		||||
 | 
			
		||||
    QFileDialog saveDialog(this);
 | 
			
		||||
    saveDialog.setAcceptMode(QFileDialog::AcceptSave);
 | 
			
		||||
    saveDialog.setFileMode(QFileDialog::AnyFile);
 | 
			
		||||
    saveDialog.setDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
 | 
			
		||||
    saveDialog.setOption(QFileDialog::DontConfirmOverwrite, false);
 | 
			
		||||
    QPointer<QFileDialog> saveDialog = new QFileDialog(this, i18n("Save image"));
 | 
			
		||||
 | 
			
		||||
    saveDialog->setAcceptMode(QFileDialog::AcceptSave);
 | 
			
		||||
    saveDialog->setFileMode(QFileDialog::AnyFile);
 | 
			
		||||
    saveDialog->setDirectory(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
 | 
			
		||||
    saveDialog->setOption(QFileDialog::DontConfirmOverwrite, false);
 | 
			
		||||
 | 
			
		||||
    QStringList filters;
 | 
			
		||||
 | 
			
		||||
    if (mimeJob->exec()) {
 | 
			
		||||
        filters << mimeJob->mimetype();
 | 
			
		||||
        saveDialog.setMimeTypeFilters(filters);
 | 
			
		||||
        saveDialog->setMimeTypeFilters(filters);
 | 
			
		||||
    }  else {
 | 
			
		||||
        filters.reserve(2);
 | 
			
		||||
        filters << "Images (*.png *.gif *.jpg)" << "All files (*.*)";
 | 
			
		||||
        saveDialog.setNameFilters(filters);
 | 
			
		||||
        saveDialog->setNameFilters(filters);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Prevent invalid characters (":" can be a tag in Danbooru)
 | 
			
		||||
| 
						 | 
				
			
			@ -503,10 +507,14 @@ void DanbooruMainWindow::slotHandleDownload(const QUrl &url, const QVariant tags
 | 
			
		|||
        remoteFile.replace(":", "_");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    saveDialog.selectFile(remoteFile);
 | 
			
		||||
    saveDialog->selectFile(remoteFile);
 | 
			
		||||
    saveDialog->open();
 | 
			
		||||
 | 
			
		||||
    if (saveDialog.exec()) {
 | 
			
		||||
        QUrl localFile = saveDialog.selectedUrls().first();
 | 
			
		||||
    connect(saveDialog, &QFileDialog::finished, [this, tagList, saveDialog, url](int result) {
 | 
			
		||||
 | 
			
		||||
       if (result) {
 | 
			
		||||
 | 
			
		||||
        QUrl localFile = saveDialog->selectedUrls().at(0);
 | 
			
		||||
        if (!localFile.isEmpty()) {
 | 
			
		||||
 | 
			
		||||
            KIO::FileCopyJob *job = KIO::file_copy(url, localFile, -1, KIO::DefaultFlags);
 | 
			
		||||
| 
						 | 
				
			
			@ -519,31 +527,16 @@ void DanbooruMainWindow::slotHandleDownload(const QUrl &url, const QVariant tags
 | 
			
		|||
                }
 | 
			
		||||
 | 
			
		||||
#ifdef WITH_KFILEMETADATA
 | 
			
		||||
            qCDebug(DANBOORU_CLIENT) << "Local file" << localFile.toLocalFile();
 | 
			
		||||
//             qCDebug(DANBOORU_CLIENT) << "Local file" << localFile.toLocalFile();
 | 
			
		||||
            KFileMetaData::UserMetaData meta(localFile.toLocalFile());
 | 
			
		||||
            meta.setTags(tagList);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    QUrl localFile = QFileDialog::getSaveFileUrl(
 | 
			
		||||
                         this,
 | 
			
		||||
                         i18n("Save file"),
 | 
			
		||||
                         QDir::homePath() + QLatin1Char('/') + remoteFile,
 | 
			
		||||
                         filters
 | 
			
		||||
 | 
			
		||||
                     );*/
 | 
			
		||||
 | 
			
		||||
    // TODO: Remember last user directory - settings?
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
       }
 | 
			
		||||
       saveDialog->deleteLater();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue