summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElvis Angelaccio <elvis.angelaccio@kde.org>2016-11-05 10:51:33 (GMT)
committerElvis Angelaccio <elvis.angelaccio@kde.org>2016-11-05 10:51:33 (GMT)
commit9f3e8511d9d92f41d3cfc01454b10c97fcc292ff (patch)
treeb9f43da2436e5b18ad2a1f4dbf6e530422a75e12
parent1b5af864b2255ea3cd7b77e61a25d00576af5b75 (diff)
Simplify Query usage in CLI plugins
The Query was still assuming that all plugins run from a secondary thread. Since CliInterface-based plugins run from the main thread, there is no need to emit the `userQuery` signal and then call `Query::waitForResponse()`. They can directly call `Query::execute()` instead. Differential Revision: D3217
-rw-r--r--kerfuffle/archiveinterface.h6
-rw-r--r--kerfuffle/cliinterface.cpp24
-rw-r--r--kerfuffle/jobs.cpp4
-rw-r--r--kerfuffle/queries.cpp3
-rw-r--r--kerfuffle/queries.h7
5 files changed, 20 insertions, 24 deletions
diff --git a/kerfuffle/archiveinterface.h b/kerfuffle/archiveinterface.h
index 4b009f3..effa451 100644
--- a/kerfuffle/archiveinterface.h
+++ b/kerfuffle/archiveinterface.h
@@ -166,10 +166,14 @@ signals:
void progress(double progress);
void info(const QString &info);
void finished(bool result);
- void userQuery(Query *query);
void testSuccess();
void compressionMethodFound(const QStringList);
+ /**
+ * Emitted when @p query needs to be executed on the GUI thread.
+ */
+ void userQuery(Query *query);
+
protected:
/**
diff --git a/kerfuffle/cliinterface.cpp b/kerfuffle/cliinterface.cpp
index 9482d7a..1b39370 100644
--- a/kerfuffle/cliinterface.cpp
+++ b/kerfuffle/cliinterface.cpp
@@ -354,8 +354,7 @@ void CliInterface::processFinished(int exitCode, QProcess::ExitStatus exitStatus
list();
} else if (m_operationMode == List && isCorrupt()) {
Kerfuffle::LoadCorruptQuery query(filename());
- emit userQuery(&query);
- query.waitForResponse();
+ query.execute();
if (!query.responseYes()) {
emit cancelled();
emit finished(false);
@@ -495,8 +494,7 @@ bool CliInterface::moveDroppedFilesToDest(const QVector<Archive::Entry*> &files,
Kerfuffle::OverwriteQuery query(absDestEntry.absoluteFilePath());
query.setNoRenameMode(true);
- emit userQuery(&query);
- query.waitForResponse();
+ query.execute();
if (query.responseOverwrite() || query.responseOverwriteAll()) {
if (query.responseOverwriteAll()) {
@@ -604,8 +602,7 @@ bool CliInterface::moveToDestination(const QDir &tempDir, const QDir &destDir, b
Kerfuffle::OverwriteQuery query(absDestEntry.absoluteFilePath());
query.setNoRenameMode(true);
- emit userQuery(&query);
- query.waitForResponse();
+ query.execute();
if (query.responseOverwrite() || query.responseOverwriteAll()) {
if (query.responseOverwriteAll()) {
@@ -727,8 +724,7 @@ void CliInterface::killProcess(bool emitFinished)
bool CliInterface::passwordQuery()
{
Kerfuffle::PasswordNeededQuery query(filename());
- emit userQuery(&query);
- query.waitForResponse();
+ query.execute();
if (query.responseCancelled()) {
emit cancelled();
@@ -866,8 +862,7 @@ bool CliInterface::handleLine(const QString& line)
qCDebug(ARK) << "Found a password prompt";
Kerfuffle::PasswordNeededQuery query(filename());
- emit userQuery(&query);
- query.waitForResponse();
+ query.execute();
if (query.responseCancelled()) {
emit cancelled();
@@ -911,8 +906,7 @@ bool CliInterface::handleLine(const QString& line)
qCDebug(ARK) << "Found a password prompt";
Kerfuffle::PasswordNeededQuery query(filename());
- emit userQuery(&query);
- query.waitForResponse();
+ query.execute();
if (query.responseCancelled()) {
emit cancelled();
@@ -992,11 +986,7 @@ bool CliInterface::handleFileExistsMessage(const QString& line)
Kerfuffle::OverwriteQuery query(QDir::current().path() + QLatin1Char( '/' ) + m_storedFileName);
query.setNoRenameMode(true);
- emit userQuery(&query);
- qCDebug(ARK) << "Waiting response";
- query.waitForResponse();
-
- qCDebug(ARK) << "Finished response";
+ query.execute();
QString responseToProcess;
const QStringList choices = m_cliProps->property("fileExistsInput").toStringList();
diff --git a/kerfuffle/jobs.cpp b/kerfuffle/jobs.cpp
index 5a4429e..13fd955 100644
--- a/kerfuffle/jobs.cpp
+++ b/kerfuffle/jobs.cpp
@@ -215,6 +215,10 @@ void Job::onFinished(bool result)
void Job::onUserQuery(Query *query)
{
+ if (archiveInterface()->waitForFinishedSignal()) {
+ qCWarning(ARK) << "Plugins run from the main thread should call directly query->execute()";
+ }
+
emit userQuery(query);
}
diff --git a/kerfuffle/queries.cpp b/kerfuffle/queries.cpp
index 4e6800d..cf51c88 100644
--- a/kerfuffle/queries.cpp
+++ b/kerfuffle/queries.cpp
@@ -44,7 +44,6 @@ namespace Kerfuffle
{
Query::Query()
{
- m_responseMutex.lock();
}
QVariant Query::response() const
@@ -54,11 +53,11 @@ QVariant Query::response() const
void Query::waitForResponse()
{
+ QMutexLocker locker(&m_responseMutex);
//if there is no response set yet, wait
if (!m_data.contains(QStringLiteral("response"))) {
m_responseCondition.wait(&m_responseMutex);
}
- m_responseMutex.unlock();
}
void Query::setResponse(const QVariant &response)
diff --git a/kerfuffle/queries.h b/kerfuffle/queries.h
index 1b9f8bb..074198e 100644
--- a/kerfuffle/queries.h
+++ b/kerfuffle/queries.h
@@ -46,14 +46,13 @@ class KERFUFFLE_EXPORT Query
{
public:
/**
- * Execute the response. Will happen in the GUI thread, so it's
- * safe to use widgets/gui elements here. Must call setResponse
- * when done.
+ * Execute the response. It needs to be called from the GUI thread.
*/
virtual void execute() = 0;
/**
- * Will block until the response have been set
+ * Will block until the response have been set.
+ * Useful for worker threads that need to show a dialog.
*/
void waitForResponse();