diff options
author | tezeb <tezeb+github@outoftheblue.pl> | 2017-01-10 00:18:23 +0100 |
---|---|---|
committer | tezeb <tezeb+github@outoftheblue.pl> | 2017-01-10 00:18:23 +0100 |
commit | 56b5674af3e59e95888774925f92f1ea0dc8e0f2 (patch) | |
tree | d23fb02609fb582623492e4a8b94ce281c09efa4 /src | |
parent | c3f44c2cd1adb36013cc54638e1fd507d5121ad5 (diff) |
creation of transaction entity for better process-mgmt
Diffstat (limited to 'src')
-rw-r--r-- | src/enums.h | 41 | ||||
-rw-r--r-- | src/executor.cpp | 12 | ||||
-rw-r--r-- | src/executor.h | 2 | ||||
-rw-r--r-- | src/imitatepass.cpp | 3 | ||||
-rw-r--r-- | src/imitatepass.h | 3 | ||||
-rw-r--r-- | src/pass.cpp | 46 | ||||
-rw-r--r-- | src/pass.h | 24 | ||||
-rw-r--r-- | src/realpass.cpp | 2 | ||||
-rw-r--r-- | src/simpletransaction.cpp | 41 | ||||
-rw-r--r-- | src/simpletransaction.h | 48 | ||||
-rw-r--r-- | src/src.pro | 6 |
11 files changed, 180 insertions, 48 deletions
diff --git a/src/enums.h b/src/enums.h index 106d9bb0..3fd89af0 100644 --- a/src/enums.h +++ b/src/enums.h @@ -5,19 +5,34 @@ \class Enums \brief Enumerators for configuration items */ -class Enums { -public: - /** - * @brief Enums::clipBoardType enum - * 0 Never - * 1 Always - * 2 On demand - */ - enum clipBoardType { - CLIPBOARD_NEVER = 0, - CLIPBOARD_ALWAYS = 1, - CLIPBOARD_ON_DEMAND = 2 - }; +namespace Enums { + +enum clipBoardType { + CLIPBOARD_NEVER = 0, + CLIPBOARD_ALWAYS = 1, + CLIPBOARD_ON_DEMAND = 2 +}; + +enum PROCESS { + GIT_INIT = 0, + GIT_ADD, + GIT_COMMIT, + GIT_RM, + GIT_PULL, + GIT_PUSH, + PASS_SHOW, + PASS_INSERT, + PASS_REMOVE, + PASS_INIT, + GPG_GENKEYS, + PASS_MOVE, + PASS_COPY, + GIT_MOVE, + GIT_COPY, + // have to be last!!! + PROCESS_COUNT, + INVALID }; +} #endif // ENUMS_H diff --git a/src/executor.cpp b/src/executor.cpp index a41a8548..eab71ff8 100644 --- a/src/executor.cpp +++ b/src/executor.cpp @@ -173,6 +173,18 @@ void Executor::setEnvironment(const QStringList &env) { } /** + * @brief Executor::cancelNext cancels execution of first process in queue + * if it's not already running + * + * @return id of the cancelled process or -1 on error + */ +int Executor::cancelNext() { + if (running || m_execQueue.isEmpty()) + return -1; // TODO(bezet): definitely throw here + return m_execQueue.dequeue().id; +} + +/** * @brief Executor::finished called when an executed process finishes * @param exitCode * @param exitStatus diff --git a/src/executor.h b/src/executor.h index c5b9e784..2a8b67cd 100644 --- a/src/executor.h +++ b/src/executor.h @@ -82,6 +82,8 @@ public: QString *process_out, QString *process_err = Q_NULLPTR); void setEnvironment(const QStringList &env); + + int cancelNext(); private slots: void finished(int exitCode, QProcess::ExitStatus exitStatus); signals: diff --git a/src/imitatepass.cpp b/src/imitatepass.cpp index a976fc6e..e6b78f62 100644 --- a/src/imitatepass.cpp +++ b/src/imitatepass.cpp @@ -3,6 +3,8 @@ #include "qtpasssettings.h" #include <QDirIterator> +using namespace Enums; + /** * @brief ImitatePass::ImitatePass for situaions when pass is not available * we imitate the behavior of pass https://www.passwordstore.org/ @@ -389,3 +391,4 @@ void ImitatePass::executeGit(PROCESS id, const QStringList &args, QString input, executeWrapper(id, QtPassSettings::getGitExecutable(), args, input, readStdout, readStderr); } + diff --git a/src/imitatepass.h b/src/imitatepass.h index 1340e140..999e7ffc 100644 --- a/src/imitatepass.h +++ b/src/imitatepass.h @@ -2,6 +2,7 @@ #define IMITATEPASS_H #include "pass.h" +#include <queue> /*! \class ImitatePass @@ -21,6 +22,8 @@ class ImitatePass : public Pass { QString input = QString(), bool readStdout = true, bool readStderr = true); +protected: + public: ImitatePass(); virtual ~ImitatePass() {} diff --git a/src/pass.cpp b/src/pass.cpp index 9327f4f2..0d2e4b47 100644 --- a/src/pass.cpp +++ b/src/pass.cpp @@ -6,6 +6,7 @@ #include <map> using namespace std; +using namespace Enums; /** * @brief Pass::Pass wrapper for using either pass or the pass imitation @@ -33,7 +34,6 @@ void Pass::executeWrapper(PROCESS id, const QString &app, const QStringList &args, QString input, bool readStdout, bool readStderr) { dbg() << app << args; - transactionAdd(id); exec.execute(id, QtPassSettings::getPassStore(), app, args, input, readStdout, readStderr); } @@ -172,23 +172,43 @@ void Pass::finished(int id, int exitCode, const QString &out, // TODO(bezet): remove ! dbg() << id << exitCode << out << err; - static const map<PROCESS, const char *> emitMsg = { - {GIT_PULL, QT_TR_NOOP("git pull successful")}, - {GIT_PUSH, QT_TR_NOOP("git push successful")}}; PROCESS pid = static_cast<PROCESS>(id); - map<PROCESS, const char *>::const_iterator i = emitMsg.find(pid); - if (exitCode != 0) { emit processErrorExit(exitCode, err); - return; - } else if (i != emitMsg.end()) { - emit statusMsg(tr(i->second), 2000); - // don't return here, need to emit finishedAny too - } else if (pid == PASS_SHOW) { + return; + } + switch (pid) { + case GIT_INIT: + emit finishedGitInit(out, err); + break; + case GIT_PULL: + emit finishedGitPull(out, err); + break; + case GIT_PUSH: + emit finishedGitPush(out, err); + break; + case PASS_SHOW: emit finishedShow(out); - return; + break; + case PASS_INSERT: + emit finishedInsert(out, err); + break; + case PASS_REMOVE: + emit finishedRemove(out, err); + break; + case PASS_INIT: + emit finishedInit(out, err); + break; + case PASS_MOVE: + emit finishedMove(out, err); + break; + case PASS_COPY: + emit finishedCopy(out, err); + break; + default: + dbg() << "Unhandled process type" << pid; + break; } - emit finishedAny(out, err); } /** @@ -24,23 +24,7 @@ class Pass : public QObject { protected: Executor exec; - enum PROCESS { - GIT_INIT = 0, - GIT_ADD, - GIT_COMMIT, - GIT_RM, - GIT_PULL, - GIT_PUSH, - PASS_SHOW, - PASS_INSERT, - PASS_REMOVE, - PASS_INIT, - GPG_GENKEYS, - PASS_MOVE, - PASS_COPY, - GIT_MOVE, - GIT_COPY, - }; + typedef Enums::PROCESS PROCESS; public: Pass(); @@ -76,6 +60,7 @@ protected: void executeWrapper(PROCESS id, const QString &app, const QStringList &args, QString input, bool readStdout = true, bool readStderr = true); + private slots: void finished(int id, int exitCode, const QString &out, const QString &err); @@ -89,15 +74,14 @@ signals: void finishedAny(const QString &, const QString &); void finishedGitInit(const QString &, const QString &); - void finishedGitAdd(const QString &, const QString &); - void finishedGitCommit(const QString &, const QString &); - void finishedGitRm(const QString &, const QString &); void finishedGitPull(const QString &, const QString &); void finishedGitPush(const QString &, const QString &); void finishedShow(const QString &); void finishedInsert(const QString &, const QString &); void finishedRemove(const QString &, const QString &); void finishedInit(const QString &, const QString &); + void finishedMove(const QString &, const QString &); + void finishedCopy(const QString &, const QString &); void finishedGenerate(const QString &, const QString &); void finishedGenerateGPGKeys(const QString &, const QString &); }; diff --git a/src/realpass.cpp b/src/realpass.cpp index cf946997..6717193f 100644 --- a/src/realpass.cpp +++ b/src/realpass.cpp @@ -1,6 +1,8 @@ #include "realpass.h" #include "qtpasssettings.h" +using namespace Enums; + RealPass::RealPass() {} /** diff --git a/src/simpletransaction.cpp b/src/simpletransaction.cpp new file mode 100644 index 00000000..ca36d570 --- /dev/null +++ b/src/simpletransaction.cpp @@ -0,0 +1,41 @@ +#include "simpletransaction.h" +#include "debughelper.h" +#include <utility> + +using std::pair; +using namespace Enums; + +void simpleTransaction::transactionStart() { + dbg() << "START" << transactionDepth; + transactionDepth++; +} + +void simpleTransaction::transactionAdd(PROCESS id) { + dbg() << "ADD" << transactionDepth << id; + if (transactionDepth > 0) { + lastInTransaction = id; + } else { + transactionQueue.push(pair<PROCESS, PROCESS>(id, id)); + } +} + +void simpleTransaction::transactionEnd(PROCESS pid) { + dbg() << "END" << transactionDepth; + if (transactionDepth > 0) { + transactionDepth--; + if (transactionDepth == 0 && lastInTransaction != INVALID) { + transactionQueue.push(pair<PROCESS, PROCESS>(lastInTransaction, pid)); + lastInTransaction = INVALID; + } + } +} + +PROCESS simpleTransaction::transactionIsOver(PROCESS id) { + dbg() << "OVER" << transactionDepth << id; + if (!transactionQueue.empty() && id == transactionQueue.front().first) { + PROCESS ret = transactionQueue.front().second; + transactionQueue.pop(); + return ret; + } + return INVALID; +} diff --git a/src/simpletransaction.h b/src/simpletransaction.h new file mode 100644 index 00000000..c61a241a --- /dev/null +++ b/src/simpletransaction.h @@ -0,0 +1,48 @@ +#ifndef SIMPLETRANSACTION_H +#define SIMPLETRANSACTION_H + +#include "enums.h" +#include <queue> + +class simpleTransaction { + + int transactionDepth; + Enums::PROCESS lastInTransaction; + std::queue<std::pair<Enums::PROCESS, Enums::PROCESS>> transactionQueue; + +public: + simpleTransaction() + : transactionDepth(0), lastInTransaction(Enums::INVALID) {} + /** + * @brief transactionStart this function is used to mark start of the sequence + * of processes that shall be treated as one + * operation. + */ + void transactionStart(); + /** + * @brief transactionAdd If called after call to transactionStart() and before + * transactionEnd(), this method marks given process as + * next step in transaction. Otherwise it marks given + * process as the only step in transaction(it's value is + * treated as transaction result). + * + * @param id process that shall be treated as part of transaction + */ + void transactionAdd(Enums::PROCESS); + /** + * @brief transactionEnd marks end of transaction + * + * @param pid value that will be used as a result of transaction + */ + void transactionEnd(Enums::PROCESS); + /** + * @brief transactionIsOver checks wheather currently finished process is last + * in current transaction + * + * @return result of transaction as set by transactionAdd or transactionEnd if + * the transaction is over or PROCESS::INVALID if it's not yet over + */ + Enums::PROCESS transactionIsOver(Enums::PROCESS); +}; + +#endif // SIMPLETRANSACTION_H diff --git a/src/src.pro b/src/src.pro index 56a330b0..0a5811a5 100644 --- a/src/src.pro +++ b/src/src.pro @@ -37,7 +37,8 @@ SOURCES += main.cpp \ pass.cpp \ realpass.cpp \ imitatepass.cpp \ - executor.cpp + executor.cpp \ + simpletransaction.cpp HEADERS += mainwindow.h \ configdialog.h \ @@ -58,7 +59,8 @@ HEADERS += mainwindow.h \ imitatepass.h \ datahelpers.h \ debughelper.h \ - executor.h + executor.h \ + simpletransaction.h FORMS += mainwindow.ui \ configdialog.ui \ |