# # # rename "src/monotone/AutomateCommand.h" # to "src/model/AutomateCommand.h" # # patch "src/model/AutomateCommand.h" # from [98bf0e83adf775c4980b514898bef9b06af63854] # to [745a331bc70e34f7e82c680da0f8f3318f359686] # ============================================================ --- src/monotone/AutomateCommand.h 98bf0e83adf775c4980b514898bef9b06af63854 +++ src/model/AutomateCommand.h 745a331bc70e34f7e82c680da0f8f3318f359686 @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2006 by Thomas Keller * + * Copyright (C) 2007 by Thomas Keller * * address@hidden * * * * This program is free software; you can redistribute it and/or modify * @@ -21,27 +21,77 @@ #ifndef AUTOMATE_COMMAND_H #define AUTOMATE_COMMAND_H +#include "MonotoneThread.h" +#include "Guitone.h" + #include +#include +#include -class AutomateCommand +class AutomateCommand : public QObject { + Q_OBJECT public: + AutomateCommand(const QString & db) : dbPath(db) {} virtual ~AutomateCommand() {} - inline void setAutomateData(const QString & d) { data = d; } - inline QString getAutomateData() const { return data; } - friend class MonotoneDelegate; - protected: - inline virtual bool handleError(int retCode) { - // Error handling has to happen in the derived classes - // where each command may decide whether it makes sense - // to continue program execution (i.e. the error is recoverable) - // or not - return false; + virtual void processTaskResult(const MonotoneTask &) = 0; + +protected slots: + virtual void taskAborted(const MonotoneTask & task) + { + // do sth more reasonable here in a subclass, + // i.e. try to query the data again + C(QString("Task %1 (thread %2) aborted (%3)") + .arg(task.getCommandNumber()) + .arg(task.getThreadNumber()) + .arg(QString::fromUtf8(task.getEncodedInput())) + ); } - virtual void parseOutput() = 0; - QString data; + virtual void taskFinished(const MonotoneTask & task) + { + int threadNumber = task.getThreadNumber(); + if (!queuedCommands.contains(threadNumber) || + queuedCommands[threadNumber].contains(task.getCommandNumber())) + { + return; + } + processTaskResult(task); + } + +protected: + void enqueueTask(const MonotoneTask & task) + { + QMutexLocker locker(&lock); + + MonotoneThread * thread = APP->manager()->getThread(dbPath); + int threadNumber = thread->getThreadNumber(); + // we want to avoid multiple thread connections + if (!connectedThreads.contains(threadNumber)) + { + connect( + thread, SIGNAL(taskAborted(const MonotoneTask &)), + this, SLOT(taskAborted(const MonotoneTask &)) + ); + + connect( + thread, SIGNAL(taskFinished(const MonotoneTask &)), + this, SLOT(taskFinished(const MonotoneTask &)) + ); + + connectedThreads.append(threadNumber); + } + queuedCommands[threadNumber].append( + thread->enqueueTask(task) + ); + } + +private: + QString dbPath; + QMutex lock; + QList connectedThreads; + QMap > queuedCommands; }; #endif