reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit 6491118f0c4fde8d874a8c4e630c5b6f803b8b7d
parent 727ad7e2747669b92c554daac5537559da6c0a38
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Mon,  2 May 2016 01:45:59 -0400

don't repeat "transaction cannot be created" errors for every task

Diffstat:
Msrc/browser.cpp | 28++++++++++++++++++----------
Msrc/browser.hpp | 2+-
Msrc/manager.cpp | 25++++++++++++-------------
Msrc/manager.hpp | 2+-
Msrc/reapack.cpp | 131+++++++++++++++++++++++++++++--------------------------------------------------
Msrc/reapack.hpp | 15++++-----------
Msrc/transaction.cpp | 9++++++---
Msrc/transaction.hpp | 2+-
8 files changed, 91 insertions(+), 123 deletions(-)

diff --git a/src/browser.cpp b/src/browser.cpp @@ -25,6 +25,7 @@ #include "reapack.hpp" #include "report.hpp" #include "resource.hpp" +#include "transaction.hpp" #include <boost/algorithm/string.hpp> #include <boost/range/adaptor/reversed.hpp> @@ -158,9 +159,7 @@ void Browser::onCommand(const int id, const int event) case IDOK: case IDAPPLY: if(confirm()) { - apply(); - - if(id == IDAPPLY) + if(!apply() || id == IDAPPLY) break; } else @@ -820,25 +819,34 @@ bool Browser::confirm() const return btn == IDYES; } -void Browser::apply() +bool Browser::apply() { if(m_actions.empty()) - return; + return true; + + Transaction *tx = m_reapack->setupTransaction(); + + if(!tx) + return false; disable(m_apply); for(const auto &pair : m_actions) { if(pair.second) - m_reapack->install(pair.second); + tx->install(pair.second); else - m_reapack->uninstall(pair.first->regEntry); + tx->uninstall(pair.first->regEntry); } m_actions.clear(); - m_reapack->runTasks(); - if(m_reapack->isRunning()) - fillList(); // update state column + if(!tx->runTasks()) { + // this is an asynchronous transaction + // update the state column right away to give visual feedback + fillList(); + } + + return true; } auto Browser::Entry::hash() const -> Hash diff --git a/src/browser.hpp b/src/browser.hpp @@ -109,7 +109,7 @@ private: void selectionDo(const std::function<void (int)> &); Tab currentTab() const; bool confirm() const; - void apply(); + bool apply(); void installLatest(int index, bool toggle = true); void reinstall(int index, bool toggle = true); diff --git a/src/manager.cpp b/src/manager.cpp @@ -26,6 +26,7 @@ #include "reapack.hpp" #include "remote.hpp" #include "resource.hpp" +#include "transaction.hpp" using namespace std; @@ -95,10 +96,7 @@ void Manager::onCommand(const int id, int) case IDOK: case IDAPPLY: if(confirm()) { - apply(); - reset(); - - if(id == IDAPPLY) + if(!apply() || id == IDAPPLY) break; // IDOK -> continue to next case (IDCANCEL) @@ -283,8 +281,13 @@ bool Manager::confirm() const return btn == IDYES; } -void Manager::apply() +bool Manager::apply() { + Transaction *tx = m_reapack->setupTransaction(); + + if(!tx) + return false; + if(m_autoInstall) m_config->install()->autoInstall = m_autoInstall.value(); @@ -295,21 +298,17 @@ void Manager::apply() const Remote &remote = pair.first; const bool enable = pair.second; - if(enable) - m_reapack->enable(remote); - else - m_reapack->disable(remote); + m_reapack->setRemoteEnabled(enable, remote); } for(const Remote &remote : m_uninstall) m_reapack->uninstall(remote); + tx->runTasks(); m_config->write(); + reset(); - if(m_reapack->isRunning()) - m_reapack->runTasks(); - else - m_reapack->refreshBrowser(); + return true; } void Manager::reset() diff --git a/src/manager.hpp b/src/manager.hpp @@ -52,7 +52,7 @@ private: void options(); bool confirm() const; - void apply(); + bool apply(); void reset(); HWND m_apply; diff --git a/src/reapack.cpp b/src/reapack.cpp @@ -63,7 +63,7 @@ static void CleanupTempFiles() ReaPack::ReaPack(REAPER_PLUGIN_HINSTANCE instance) : syncAction(), browseAction(),importAction(), configAction(), - m_transaction(nullptr), m_progress(nullptr), m_browser(nullptr), + m_tx(nullptr), m_progress(nullptr), m_browser(nullptr), m_import(nullptr), m_manager(nullptr), m_instance(instance) { m_mainWindow = GetMainHwnd(); @@ -135,83 +135,52 @@ void ReaPack::synchronizeAll() if(remotes.empty()) { ShowMessageBox("No repository enabled, nothing to do!", "ReaPack", MB_OK); - return; } - // do nothing is a transation is already running - Transaction *t = createTransaction(); + Transaction *tx = setupTransaction(); - if(!t) + if(!tx) return; for(const Remote &remote : remotes) - t->synchronize(remote, *m_config->install()); + tx->synchronize(remote, *m_config->install()); - t->runTasks(); + tx->runTasks(); } -void ReaPack::setRemoteEnabled(const Remote &original, const bool enable) +void ReaPack::setRemoteEnabled(const bool enable, const Remote &remote) { - Remote remote(original); - remote.setEnabled(enable); + assert(m_tx); - const auto apply = [=] { - m_config->remotes()->add(remote); - refreshManager(); - }; + Remote copy(remote); + copy.setEnabled(enable); - if(!hitchhikeTransaction()) { - apply(); - refreshBrowser(); - return; - } + m_tx->registerAll(copy); - m_transaction->registerAll(remote); + m_tx->onFinish([=] { + if(m_tx->isCancelled()) + return; - m_transaction->onFinish([=] { - if(!m_transaction->isCancelled()) - apply(); + m_config->remotes()->add(copy); + refreshManager(); }); } -void ReaPack::install(const Version *ver) -{ - if(!hitchhikeTransaction()) - return; - - m_transaction->install(ver); -} - void ReaPack::uninstall(const Remote &remote) { if(remote.isProtected()) return; - const auto apply = [=] { - m_config->remotes()->remove(remote); - }; - - if(!hitchhikeTransaction()) { - apply(); - refreshBrowser(); - return; - } + assert(m_tx); + m_tx->uninstall(remote); - m_transaction->uninstall(remote); - - m_transaction->onFinish([=] { - if(!m_transaction->isCancelled()) - apply(); + m_tx->onFinish([=] { + if(!m_tx->isCancelled()) + m_config->remotes()->remove(remote); }); } -void ReaPack::uninstall(const Registry::Entry &entry) -{ - if(hitchhikeTransaction()) - m_transaction->uninstall(entry); -} - void ReaPack::importRemote() { if(m_import) { @@ -266,8 +235,13 @@ void ReaPack::import(const Remote &remote, HWND parent) return; } else { + Transaction *tx = setupTransaction(); + + if(!tx) + return; + enable(existing); - runTasks(); + tx->runTasks(); m_config->write(); @@ -332,15 +306,20 @@ void ReaPack::about(const Remote &remote, HWND parent) const auto ret = Dialog::Show<About>(m_instance, parent, index); if(ret == About::InstallResult) { + Transaction *tx = setupTransaction(); + + if(!tx) + return; + enable(remote); - if(m_transaction) { // transaction is created by enable() + if(m_tx) { // transaction is created by enable() InstallOpts opts = *m_config->install(); opts.autoInstall = true; - m_transaction->synchronize(remote, opts); + tx->synchronize(remote, opts); } - runTasks(); + tx->runTasks(); } }, parent); } @@ -351,7 +330,7 @@ void ReaPack::browsePackages() m_browser->setFocus(); return; } - else if(m_transaction) { + else if(m_tx) { ShowMessageBox( "This feature cannot be used while packages are being installed. " "Try again later.", "Browse packages", MB_OK @@ -480,16 +459,16 @@ IndexPtr ReaPack::loadIndex(const Remote &remote, HWND parent) return nullptr; } -Transaction *ReaPack::createTransaction() +Transaction *ReaPack::setupTransaction() { if(m_progress && m_progress->isVisible()) m_progress->setFocus(); - if(m_transaction) - return nullptr; + if(m_tx) + return m_tx; try { - m_transaction = new Transaction; + m_tx = new Transaction; } catch(const reapack_error &e) { const auto_string &desc = make_autostring(e.what()); @@ -506,49 +485,35 @@ Transaction *ReaPack::createTransaction() assert(!m_progress); m_progress = Dialog::Create<Progress>(m_instance, m_mainWindow, - m_transaction->downloadQueue()); + m_tx->downloadQueue()); - m_transaction->onFinish([=] { + m_tx->onFinish([=] { Dialog::Destroy(m_progress); m_progress = nullptr; - const Receipt *receipt = m_transaction->receipt(); + const Receipt *receipt = m_tx->receipt(); - if(m_transaction->isCancelled() || !receipt->isEnabled()) + if(m_tx->isCancelled() || !receipt->isEnabled()) return; LockDialog managerLock(m_manager); LockDialog cleanupLock(m_browser); - if(m_transaction->taskCount() == 0 && !receipt->hasErrors()) + if(m_tx->taskCount() == 0 && !receipt->hasErrors()) ShowMessageBox("Nothing to do!", "ReaPack", MB_OK); else Dialog::Show<Report>(m_instance, m_mainWindow, receipt); }); - m_transaction->setCleanupHandler([=] { - delete m_transaction; - m_transaction = nullptr; + m_tx->setCleanupHandler([=] { + delete m_tx; + m_tx = nullptr; // refresh only once all onFinish slots were ran refreshBrowser(); }); - return m_transaction; -} - -bool ReaPack::hitchhikeTransaction() -{ - if(m_transaction) - return true; - else - return createTransaction() != nullptr; -} - -void ReaPack::runTasks() -{ - if(m_transaction) - m_transaction->runTasks(); + return m_tx; } void ReaPack::refreshManager() diff --git a/src/reapack.hpp b/src/reapack.hpp @@ -63,12 +63,9 @@ public: bool execActions(int id, int); void synchronizeAll(); - void setRemoteEnabled(const Remote &, bool enable); - void enable(const Remote &r) { setRemoteEnabled(r, true); } - void disable(const Remote &r) { setRemoteEnabled(r, false); } - void install(const Version *); + void setRemoteEnabled(bool enable, const Remote &); + void enable(const Remote &r) { setRemoteEnabled(true, r); } void uninstall(const Remote &); - void uninstall(const Registry::Entry &); void importRemote(); void import(const Remote &, HWND = nullptr); @@ -85,14 +82,10 @@ public: void fetchIndexes(const std::vector<Remote> &, const IndexesCallback &, HWND parent, bool stale = false); - void runTasks(); - bool isRunning() const { return m_transaction != nullptr; } - + Transaction *setupTransaction(); Config *config() const { return m_config; } private: - Transaction *createTransaction(); - bool hitchhikeTransaction(); void registerSelf(); void doFetchIndex(const Remote &remote, DownloadQueue *, HWND, bool stale); IndexPtr loadIndex(const Remote &remote, HWND); @@ -100,7 +93,7 @@ private: std::map<int, ActionCallback> m_actions; Config *m_config; - Transaction *m_transaction; + Transaction *m_tx; Progress *m_progress; Browser *m_browser; Import *m_import; diff --git a/src/transaction.cpp b/src/transaction.cpp @@ -319,7 +319,7 @@ void Transaction::addTask(Task *task) m_receipt.setEnabled(true); } -void Transaction::runTasks() +bool Transaction::runTasks() { m_registry->restore(); @@ -339,8 +339,11 @@ void Transaction::runTasks() m_registry->savepoint(); // get ready for new tasks - if(m_downloadQueue.idle()) - finish(); + if(!m_downloadQueue.idle()) + return false; + + finish(); + return true; } void Transaction::registerInHost(const bool add, const Registry::Entry &entry) diff --git a/src/transaction.hpp b/src/transaction.hpp @@ -62,7 +62,7 @@ public: void uninstall(const Remote &); void uninstall(const Registry::Entry &); void registerAll(const Remote &); - void runTasks(); + bool runTasks(); bool isCancelled() const { return m_isCancelled; } const Receipt *receipt() const { return &m_receipt; }