commit 1e782b9289b873dade38db6dfbcddd2aa65afdcb
parent c55fa8a7463d6234b9c250a8c6ca139d52581aec
Author: cfillion <cfillion@users.noreply.github.com>
Date: Thu, 24 Aug 2017 23:49:20 -0400
refactor handling of the global ReaPack and Config instances
Diffstat:
22 files changed, 145 insertions(+), 161 deletions(-)
diff --git a/src/about.cpp b/src/about.cpp
@@ -46,8 +46,8 @@ enum {
ACTION_COPY_URL, ACTION_LOCATE
};
-About::About(ReaPack *reapack)
- : Dialog(IDD_ABOUT_DIALOG), m_reapack(reapack)
+About::About()
+ : Dialog(IDD_ABOUT_DIALOG)
{
}
@@ -78,7 +78,7 @@ void About::onInit()
setAnchor(getControl(IDC_ACTION), AnchorAll);
setAnchor(getControl(IDOK), AnchorAll);
- auto data = m_serializer.read(m_reapack->config()->windowState.about, 1);
+ auto data = m_serializer.read(g_reapack->config()->windowState.about, 1);
restoreState(data);
}
@@ -86,7 +86,7 @@ void About::onClose()
{
Serializer::Data data;
saveState(data);
- m_reapack->config()->windowState.about = m_serializer.write(data);
+ g_reapack->config()->windowState.about = m_serializer.write(data);
}
void About::onCommand(const int id, int)
@@ -421,13 +421,12 @@ const Package *AboutIndexDelegate::currentPackage() const
void AboutIndexDelegate::findInBrowser()
{
- ReaPack *reapack = m_dialog->reapack();
- Browser *browser = reapack->browsePackages();
+ Browser *browser = g_reapack->browsePackages();
if(!browser)
return;
const Package *pkg = currentPackage();
- const string &name = pkg->displayName(reapack->config()->browser.showDescs);
+ const string &name = pkg->displayName(g_reapack->config()->browser.showDescs);
ostringstream stream;
stream << '^' << quoted(name) << "$ ^" << quoted(m_index->name()) << '$';
@@ -451,7 +450,7 @@ void AboutIndexDelegate::aboutPackage()
void AboutIndexDelegate::itemCopy()
{
- Config *config = m_dialog->reapack()->config();
+ Config *config = g_reapack->config();
if(const Package *pkg = currentPackage())
m_dialog->setClipboard(pkg->displayName(config->browser.showDescs));
@@ -470,8 +469,7 @@ void AboutIndexDelegate::install()
if(!choice)
return;
- ReaPack *reapack = m_dialog->reapack();
- Remote remote = reapack->remote(m_index->name());
+ Remote remote = g_reapack->remote(m_index->name());
if(!remote) {
// In case the user uninstalled the repository while this dialog was opened
@@ -481,7 +479,7 @@ void AboutIndexDelegate::install()
return;
}
- const InstallOpts &installOpts = reapack->config()->install;
+ const InstallOpts &installOpts = g_reapack->config()->install;
if(choice == INSTALL_ALL && boost::logic::indeterminate(remote.autoInstall())
&& !installOpts.autoInstall) {
@@ -495,19 +493,19 @@ void AboutIndexDelegate::install()
switch(btn) {
case IDYES:
remote.setAutoInstall(true);
- reapack->config()->remotes.add(remote);
+ g_reapack->config()->remotes.add(remote);
break;
case IDCANCEL:
return;
}
}
- Transaction *tx = reapack->setupTransaction();
+ Transaction *tx = g_reapack->setupTransaction();
if(!tx)
return;
- reapack->setRemoteEnabled(remote);
+ g_reapack->setRemoteEnabled(remote);
tx->synchronize(remote, choice == INSTALL_ALL);
tx->runTasks();
diff --git a/src/about.hpp b/src/about.hpp
@@ -31,7 +31,6 @@ class Index;
class ListView;
class Menu;
class Metadata;
-class ReaPack;
class RichEdit;
class TabBar;
struct Link;
@@ -42,7 +41,7 @@ class About : public Dialog {
public:
typedef std::shared_ptr<AboutDelegate> DelegatePtr;
- About(ReaPack *);
+ About();
void setDelegate(const DelegatePtr &, bool focus = true);
template<typename T>
bool testDelegate() { return dynamic_cast<T *>(m_delegate.get()) != nullptr; }
@@ -51,7 +50,6 @@ public:
void setMetadata(const Metadata *, bool substitution = false);
void setAction(const std::string &);
- ReaPack *reapack() const { return m_reapack; }
TabBar *tabs() const { return m_tabs; }
RichEdit *desc() const { return m_desc; }
ListView *menu() const { return m_menu; }
@@ -71,7 +69,6 @@ private:
int m_currentIndex;
std::map<int, std::vector<const Link *> > m_links;
- ReaPack *m_reapack;
TabBar *m_tabs;
RichEdit *m_desc;
ListView *m_menu;
diff --git a/src/api.cpp b/src/api.cpp
@@ -19,8 +19,6 @@
#include <reaper_plugin_functions.h>
-ReaPack *API::reapack = nullptr;
-
APIDef::APIDef(const APIFunc *func)
: m_func(func)
{
diff --git a/src/api.hpp b/src/api.hpp
@@ -18,8 +18,6 @@
#ifndef REAPACK_API_HPP
#define REAPACK_API_HPP
-class ReaPack;
-
struct APIFunc {
const char *cKey;
void *cImpl;
@@ -43,8 +41,6 @@ private:
};
namespace API {
- extern ReaPack *reapack;
-
// api_misc.cpp
extern APIFunc BrowsePackages;
extern APIFunc CompareVersions;
diff --git a/src/api_misc.cpp b/src/api_misc.cpp
@@ -21,13 +21,12 @@
#include "browser.hpp"
#include "reapack.hpp"
-using namespace API;
using namespace std;
DEFINE_API(void, BrowsePackages, ((const char*, filter)),
R"(Opens the package browser with the given filter string.)",
{
- if(Browser *browser = reapack->browsePackages())
+ if(Browser *browser = g_reapack->browsePackages())
browser->setFilter(filter);
});
diff --git a/src/api_package.cpp b/src/api_package.cpp
@@ -26,7 +26,6 @@
#include "remote.hpp"
#include "transaction.hpp"
-using namespace API;
using namespace std;
struct PackageEntry {
@@ -46,11 +45,11 @@ The repository index is downloaded asynchronously if the cached copy doesn't exi
// the one given by the user may be deleted while we download the idnex
const Registry::Entry entryCopy = entry->regEntry;
- const Remote &repo = reapack->remote(entryCopy.remote);
+ const Remote &repo = g_reapack->remote(entryCopy.remote);
if(!repo)
return false;
- Transaction *tx = reapack->setupTransaction();
+ Transaction *tx = g_reapack->setupTransaction();
if(!tx)
return false;
@@ -64,7 +63,7 @@ The repository index is downloaded asynchronously if the cached copy doesn't exi
const Package *pkg = indexes.front()->find(entryCopy.category, entryCopy.package);
if(pkg)
- reapack->about()->setDelegate(make_shared<AboutPackageDelegate>(pkg, entryCopy.version));
+ g_reapack->about()->setDelegate(make_shared<AboutPackageDelegate>(pkg, entryCopy.version));
});
tx->runTasks();
diff --git a/src/api_repo.cpp b/src/api_repo.cpp
@@ -25,15 +25,14 @@
#include <boost/lexical_cast.hpp>
#include <boost/logic/tribool_io.hpp> // required to get correct tribool casts
-using namespace API;
using namespace std;
DEFINE_API(bool, AboutRepository, ((const char*, repoName)),
R"(Show the about dialog of the given repository. Returns true if the repository exists in the user configuration.
The repository index is downloaded asynchronously if the cached copy doesn't exist or is older than one week.)",
{
- if(const Remote &repo = reapack->remote(repoName)) {
- reapack->about(repo);
+ if(const Remote &repo = g_reapack->remote(repoName)) {
+ g_reapack->about(repo);
return true;
}
@@ -48,7 +47,7 @@ R"(Add or modify a repository. Set url to nullptr (or empty string in Lua) to ke
autoInstall: usually set to 2 (obey user setting).)",
{
try {
- const Remote &existing = reapack->remote(name);
+ const Remote &existing = g_reapack->remote(name);
if(!url || strlen(url) == 0)
url = existing.url().c_str();
@@ -59,12 +58,12 @@ autoInstall: usually set to 2 (obey user setting).)",
return false;
}
- Remote remote = reapack->remote(name);
+ Remote remote = g_reapack->remote(name);
remote.setName(name);
remote.setUrl(url);
remote.setAutoInstall(boost::lexical_cast<tribool>(autoInstall));
- if(!reapack->addSetRemote(remote, enable))
+ if(!g_reapack->addSetRemote(remote, enable))
return false;
}
catch(const reapack_error &e) {
@@ -79,7 +78,7 @@ autoInstall: usually set to 2 (obey user setting).)",
}
if(commit)
- reapack->commitConfig();
+ g_reapack->commitConfig();
return true;
});
@@ -91,7 +90,7 @@ R"(Get the infos of the given repository.
autoInstall: 0=manual, 1=when sychronizing, 2=obey user setting)",
{
- const Remote &remote = reapack->remote(name);
+ const Remote &remote = g_reapack->remote(name);
if(!remote)
return false;
diff --git a/src/archive.cpp b/src/archive.cpp
@@ -71,16 +71,17 @@ struct ImportArchive {
IndexPtr m_lastIndex;
};
-void Archive::import(const auto_string &path, ReaPack *reapack)
+void Archive::import(const auto_string &path)
{
- ImportArchive state{make_shared<ArchiveReader>(path), &reapack->config()->remotes};
+ ImportArchive state{make_shared<ArchiveReader>(path),
+ &g_reapack->config()->remotes};
stringstream toc;
if(const int err = state.m_reader->extractFile(ARCHIVE_TOC, toc))
throw reapack_error(format("Cannot locate the table of contents (%d)") % err);
// starting import, do not abort process (eg. by throwing) at this point
- if(!(state.m_tx = reapack->setupTransaction()))
+ if(!(state.m_tx = g_reapack->setupTransaction()))
return;
string line;
@@ -108,7 +109,7 @@ void Archive::import(const auto_string &path, ReaPack *reapack)
}
}
- reapack->config()->write();
+ g_reapack->config()->write();
state.m_tx->runTasks();
}
@@ -241,7 +242,7 @@ bool FileExtractor::run()
}
size_t Archive::create(const auto_string &path, vector<string> *errors,
- ThreadPool *pool, ReaPack *reapack)
+ ThreadPool *pool)
{
size_t count = 0;
vector<ThreadTask *> jobs;
@@ -260,7 +261,7 @@ size_t Archive::create(const auto_string &path, vector<string> *errors,
}
};
- for(const Remote &remote : reapack->config()->remotes.getEnabled()) {
+ for(const Remote &remote : g_reapack->config()->remotes.getEnabled()) {
bool addedRemote = false;
for(const Registry::Entry &entry : reg.getEntries(remote.name())) {
diff --git a/src/archive.hpp b/src/archive.hpp
@@ -22,15 +22,14 @@
#include "path.hpp"
#include "thread.hpp"
-class ReaPack;
class ThreadPool;
typedef void *zipFile;
namespace Archive {
- void import(const auto_string &path, ReaPack *);
+ void import(const auto_string &path);
size_t create(const auto_string &path,
- std::vector<std::string> *errors, ThreadPool *pool, ReaPack *);
+ std::vector<std::string> *errors, ThreadPool *pool);
};
class ArchiveReader {
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -51,9 +51,8 @@ enum Action {
enum Timers { TIMER_FILTER = 1, TIMER_ABOUT };
-Browser::Browser(ReaPack *reapack)
- : Dialog(IDD_BROWSER_DIALOG), m_reapack(reapack),
- m_loadState(Init), m_currentIndex(-1)
+Browser::Browser()
+ : Dialog(IDD_BROWSER_DIALOG), m_loadState(Init), m_currentIndex(-1)
{
}
@@ -112,7 +111,7 @@ void Browser::onInit()
setAnchor(getControl(IDCANCEL), AnchorAll);
setAnchor(m_applyBtn, AnchorAll);
- auto data = m_serializer.read(m_reapack->config()->windowState.browser, 1);
+ auto data = m_serializer.read(g_reapack->config()->windowState.browser, 1);
restoreState(data);
m_list->restoreState(data);
@@ -124,7 +123,7 @@ void Browser::onClose()
Serializer::Data data;
saveState(data);
m_list->saveState(data);
- m_reapack->config()->windowState.browser = m_serializer.write(data);
+ g_reapack->config()->windowState.browser = m_serializer.write(data);
}
void Browser::onCommand(const int id, const int event)
@@ -192,7 +191,7 @@ void Browser::onCommand(const int id, const int event)
refresh(true);
break;
case ACTION_MANAGE:
- m_reapack->manageRemotes();
+ g_reapack->manageRemotes();
break;
case ACTION_FILTERTYPE:
m_typeFilter = boost::none;
@@ -459,7 +458,7 @@ void Browser::displayButton()
menu.addSeparator();
const auto i = menu.addAction(AUTO_STR("Show &descriptions"), ACTION_SHOWDESCS);
- if(m_reapack->config()->browser.showDescs)
+ if(g_reapack->config()->browser.showDescs)
menu.check(i);
menu.addAction(AUTO_STR("&Refresh repositories"), ACTION_REFRESH);
@@ -504,7 +503,7 @@ bool Browser::isFiltered(Package::Type type) const
void Browser::toggleDescs()
{
- auto &config = m_reapack->config()->browser;
+ auto &config = g_reapack->config()->browser;
config.showDescs = !config.showDescs;
fillList();
}
@@ -529,7 +528,7 @@ void Browser::updateAbout()
{
stopTimer(TIMER_ABOUT);
- About *about = m_reapack->about(false);
+ About *about = g_reapack->about(false);
if(!about)
return;
@@ -555,7 +554,7 @@ void Browser::refresh(const bool stale)
break;
}
- const vector<Remote> &remotes = m_reapack->config()->remotes.getEnabled();
+ const vector<Remote> &remotes = g_reapack->config()->remotes.getEnabled();
if(remotes.empty()) {
if(!isVisible() || stale) {
@@ -571,7 +570,7 @@ void Browser::refresh(const bool stale)
return;
}
- if(Transaction *tx = m_reapack->setupTransaction()) {
+ if(Transaction *tx = g_reapack->setupTransaction()) {
const bool firstLoad = m_loadState == Init;
m_loadState = Loading;
@@ -677,7 +676,7 @@ auto Browser::makeEntry(const Package *pkg,
const Registry::Entry ®Entry, const IndexPtr &index)
const -> Entry
{
- const auto &instOpts = m_reapack->config()->install;
+ const auto &instOpts = g_reapack->config()->install;
const Version *latest = pkg->lastVersion(instOpts.bleedingEdge, regEntry.version);
const Version *current = nullptr;
@@ -786,7 +785,7 @@ string Browser::getValue(const Column col, const Entry &entry) const
return display;
}
case NameColumn: {
- const auto &config = m_reapack->config()->browser;
+ const auto &config = g_reapack->config()->browser;
if(pkg)
return pkg->displayName(config.showDescs);
@@ -823,7 +822,7 @@ string Browser::getValue(const Column col, const Entry &entry) const
Remote Browser::getRemote(const Entry &entry) const
{
- return m_reapack->remote(getValue(RemoteColumn, entry));
+ return g_reapack->remote(getValue(RemoteColumn, entry));
}
bool Browser::match(const Entry &entry) const
@@ -880,7 +879,7 @@ void Browser::aboutPackage(const int index, const bool focus)
const Entry *entry = getEntry(index);
if(entry && entry->package) {
- m_reapack->about()->setDelegate(make_shared<AboutPackageDelegate>(
+ g_reapack->about()->setDelegate(make_shared<AboutPackageDelegate>(
entry->package, entry->regEntry.version), focus);
}
}
@@ -888,14 +887,14 @@ void Browser::aboutPackage(const int index, const bool focus)
void Browser::aboutRemote(const int index, const bool focus)
{
if(const Entry *entry = getEntry(index)) {
- m_reapack->about()->setDelegate(
+ g_reapack->about()->setDelegate(
make_shared<AboutIndexDelegate>(entry->index), focus);
}
}
void Browser::installLatestAll()
{
- InstallOpts &installOpts = m_reapack->config()->install;
+ InstallOpts &installOpts = g_reapack->config()->install;
const bool isEverything = (size_t)m_list->selectionSize() == m_entries.size();
if(isEverything && !installOpts.autoInstall) {
@@ -1092,7 +1091,7 @@ bool Browser::apply()
if(m_actions.empty())
return true;
- Transaction *tx = m_reapack->setupTransaction();
+ Transaction *tx = g_reapack->setupTransaction();
if(!tx)
return false;
diff --git a/src/browser.hpp b/src/browser.hpp
@@ -35,7 +35,6 @@
class Index;
class ListView;
class Menu;
-class ReaPack;
class Remote;
class Version;
@@ -43,7 +42,7 @@ typedef std::shared_ptr<const Index> IndexPtr;
class Browser : public Dialog {
public:
- Browser(ReaPack *);
+ Browser();
void refresh(bool stale = false);
void setFilter(const std::string &);
@@ -150,7 +149,6 @@ private:
void aboutRemote(int index, bool focus = true);
void aboutPackage(int index, bool focus = true);
- ReaPack *m_reapack;
LoadState m_loadState;
int m_currentIndex;
diff --git a/src/import.cpp b/src/import.cpp
@@ -34,8 +34,8 @@ using namespace std;
static const auto_char *TITLE = AUTO_STR("ReaPack: Import repositories");
static const string DISCOVER_URL = "https://reapack.com/repos";
-Import::Import(ReaPack *reapack)
- : Dialog(IDD_IMPORT_DIALOG), m_reapack(reapack), m_pool(nullptr), m_state(OK)
+Import::Import()
+ : Dialog(IDD_IMPORT_DIALOG), m_pool(nullptr), m_state(OK)
{
}
@@ -117,7 +117,7 @@ void Import::fetch()
if(m_pool) // ignore repeated presses on OK
return;
- const auto &opts = m_reapack->config()->network;
+ const auto &opts = g_reapack->config()->network;
size_t index = 0;
stringstream stream(getText(m_url));
@@ -167,7 +167,7 @@ bool Import::read(MemoryDownload *dl, const size_t idx)
try {
IndexPtr index = Index::load({}, dl->contents().c_str());
- Remote remote = m_reapack->remote(index->name());
+ Remote remote = g_reapack->remote(index->name());
const bool exists = !remote.isNull();
if(exists && remote.url() != dl->url()) {
@@ -226,12 +226,12 @@ void Import::processQueue()
m_state = Close;
if(commit)
- m_reapack->commitConfig();
+ g_reapack->commitConfig();
}
bool Import::import(const ImportData &data)
{
- if(!m_reapack->addSetRemote(data.remote))
+ if(!g_reapack->addSetRemote(data.remote))
return false;
FS::write(Index::pathFor(data.remote.name()), data.contents);
diff --git a/src/import.hpp b/src/import.hpp
@@ -26,13 +26,12 @@
#include <string>
class MemoryDownload;
-class ReaPack;
class ThreadPool;
class Import : public Dialog
{
public:
- Import(ReaPack *);
+ Import();
protected:
void onInit() override;
@@ -61,7 +60,6 @@ private:
bool import(const ImportData &);
void setWaiting(bool);
- ReaPack *m_reapack;
ThreadPool *m_pool;
State m_state;
std::deque<ImportData> m_queue;
diff --git a/src/main.cpp b/src/main.cpp
@@ -25,8 +25,6 @@
using namespace std;
-static ReaPack *reapack = nullptr;
-
#define REQUIRED_API(name) {(void **)&name, #name, true}
#define OPTIONAL_API(name) {(void **)&name, #name, false}
@@ -70,7 +68,7 @@ static bool loadAPI(void *(*getFunc)(const char *))
static bool commandHook(const int id, const int flag)
{
- return reapack->execActions(id, flag);
+ return g_reapack->execActions(id, flag);
}
static void menuHook(const char *name, HMENU handle, int f)
@@ -137,33 +135,33 @@ static bool checkLocation(REAPER_PLUGIN_HINSTANCE module)
static void setupActions()
{
- reapack->setupAction("REAPACK_SYNC", "ReaPack: Synchronize packages",
- &reapack->syncAction, bind(&ReaPack::synchronizeAll, reapack));
+ g_reapack->setupAction("REAPACK_SYNC", "ReaPack: Synchronize packages",
+ &g_reapack->syncAction, bind(&ReaPack::synchronizeAll, g_reapack));
- reapack->setupAction("REAPACK_BROWSE", "ReaPack: Browse packages...",
- &reapack->browseAction, bind(&ReaPack::browsePackages, reapack));
+ g_reapack->setupAction("REAPACK_BROWSE", "ReaPack: Browse packages...",
+ &g_reapack->browseAction, bind(&ReaPack::browsePackages, g_reapack));
- reapack->setupAction("REAPACK_IMPORT", "ReaPack: Import repositories...",
- &reapack->importAction, bind(&ReaPack::importRemote, reapack));
+ g_reapack->setupAction("REAPACK_IMPORT", "ReaPack: Import repositories...",
+ &g_reapack->importAction, bind(&ReaPack::importRemote, g_reapack));
- reapack->setupAction("REAPACK_MANAGE", "ReaPack: Manage repositories...",
- &reapack->configAction, bind(&ReaPack::manageRemotes, reapack));
+ g_reapack->setupAction("REAPACK_MANAGE", "ReaPack: Manage repositories...",
+ &g_reapack->configAction, bind(&ReaPack::manageRemotes, g_reapack));
- reapack->setupAction("REAPACK_ABOUT", bind(&ReaPack::aboutSelf, reapack));
+ g_reapack->setupAction("REAPACK_ABOUT", bind(&ReaPack::aboutSelf, g_reapack));
}
static void setupAPI()
{
- reapack->setupAPI(&API::AboutInstalledPackage);
- reapack->setupAPI(&API::AboutRepository);
- reapack->setupAPI(&API::AddSetRepository);
- reapack->setupAPI(&API::BrowsePackages);
- reapack->setupAPI(&API::CompareVersions);
- reapack->setupAPI(&API::EnumOwnedFiles);
- reapack->setupAPI(&API::FreeEntry);
- reapack->setupAPI(&API::GetEntryInfo);
- reapack->setupAPI(&API::GetOwner);
- reapack->setupAPI(&API::GetRepositoryInfo);
+ g_reapack->setupAPI(&API::AboutInstalledPackage);
+ g_reapack->setupAPI(&API::AboutRepository);
+ g_reapack->setupAPI(&API::AddSetRepository);
+ g_reapack->setupAPI(&API::BrowsePackages);
+ g_reapack->setupAPI(&API::CompareVersions);
+ g_reapack->setupAPI(&API::EnumOwnedFiles);
+ g_reapack->setupAPI(&API::FreeEntry);
+ g_reapack->setupAPI(&API::GetEntryInfo);
+ g_reapack->setupAPI(&API::GetOwner);
+ g_reapack->setupAPI(&API::GetRepositoryInfo);
}
extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
@@ -173,7 +171,7 @@ extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
plugin_register("-hookcommand", (void *)commandHook);
plugin_register("-hookcustommenu", (void *)menuHook);
- delete reapack;
+ delete g_reapack;
return 0;
}
@@ -187,7 +185,7 @@ extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
if(!checkLocation(instance))
return 0;
- reapack = API::reapack = new ReaPack(instance);
+ new ReaPack(instance);
setupActions();
setupAPI();
diff --git a/src/manager.cpp b/src/manager.cpp
@@ -48,10 +48,9 @@ enum {
ACTION_IMPORT_ARCHIVE, ACTION_EXPORT_ARCHIVE
};
-Manager::Manager(ReaPack *reapack)
+Manager::Manager()
: Dialog(IDD_CONFIG_DIALOG),
- m_reapack(reapack), m_config(reapack->config()), m_list(nullptr),
- m_changes(0), m_importing(false)
+ m_list(nullptr), m_changes(0), m_importing(false)
{
}
@@ -84,7 +83,7 @@ void Manager::onInit()
setAnchor(getControl(IDCANCEL), AnchorAll);
setAnchor(m_apply, AnchorAll);
- auto data = m_serializer.read(m_reapack->config()->windowState.manager, 1);
+ auto data = m_serializer.read(g_reapack->config()->windowState.manager, 1);
restoreState(data);
m_list->restoreState(data);
@@ -97,7 +96,7 @@ void Manager::onTimer(const int id)
{
stopTimer(id);
- if(About *about = m_reapack->about(false)) {
+ if(About *about = g_reapack->about(false)) {
if(about->testDelegate<AboutIndexDelegate>())
aboutRepo(false);
}
@@ -108,7 +107,7 @@ void Manager::onClose()
Serializer::Data data;
saveState(data);
m_list->saveState(data);
- m_reapack->config()->windowState.manager = m_serializer.write(data);
+ g_reapack->config()->windowState.manager = m_serializer.write(data);
}
void Manager::onCommand(const int id, int)
@@ -157,20 +156,20 @@ void Manager::onCommand(const int id, int)
exportArchive();
break;
case ACTION_AUTOINSTALL:
- toggle(m_autoInstall, m_config->install.autoInstall);
+ toggle(m_autoInstall, g_reapack->config()->install.autoInstall);
break;
case ACTION_BLEEDINGEDGE:
- toggle(m_bleedingEdge, m_config->install.bleedingEdge);
+ toggle(m_bleedingEdge, g_reapack->config()->install.bleedingEdge);
break;
case ACTION_PROMPTOBSOLETE:
- toggle(m_promptObsolete, m_config->install.promptObsolete);
+ toggle(m_promptObsolete, g_reapack->config()->install.promptObsolete);
break;
case ACTION_NETCONFIG:
setupNetwork();
break;
case ACTION_RESETCONFIG:
- m_config->resetOptions();
- m_config->restoreDefaultRemotes();
+ g_reapack->config()->resetOptions();
+ g_reapack->config()->restoreDefaultRemotes();
refresh();
break;
case ACTION_SELECT:
@@ -201,7 +200,7 @@ void Manager::onCommand(const int id, int)
default:
const int action = id >> 8;
if(action == ACTION_ABOUT)
- m_reapack->about(getRemote(id & 0xff));
+ g_reapack->about(getRemote(id & 0xff));
break;
}
}
@@ -322,7 +321,7 @@ void Manager::refresh()
m_list->clear();
- for(const Remote &remote : m_config->remotes) {
+ for(const Remote &remote : g_reapack->config()->remotes) {
if(m_uninstall.count(remote))
continue;
@@ -420,7 +419,7 @@ void Manager::refreshIndex()
for(size_t i = 0; i < selection.size(); i++)
remotes[i] = getRemote(selection[i]);
- if(Transaction *tx = m_reapack->setupTransaction())
+ if(Transaction *tx = g_reapack->setupTransaction())
tx->fetchIndexes(remotes, true);
}
@@ -475,7 +474,7 @@ void Manager::copyUrl()
void Manager::aboutRepo(const bool focus)
{
- m_reapack->about(getRemote(m_list->currentIndex()), focus);
+ g_reapack->about(getRemote(m_list->currentIndex()), focus);
}
void Manager::importExport()
@@ -495,7 +494,7 @@ bool Manager::importRepo()
return true;
m_importing = true;
- const auto ret = Dialog::Show<Import>(instance(), handle(), m_reapack);
+ const auto ret = Dialog::Show<Import>(instance(), handle());
m_importing = false;
return ret != 0;
@@ -512,7 +511,7 @@ void Manager::importArchive()
return;
try {
- Archive::import(path, m_reapack);
+ Archive::import(path);
}
catch(const reapack_error &e) {
const auto_string &desc = make_autostring(e.what());
@@ -542,7 +541,7 @@ void Manager::exportArchive()
try {
vector<string> errors;
- const size_t count = Archive::create(path, &errors, pool, m_reapack);
+ const size_t count = Archive::create(path, &errors, pool);
const auto finish = [=] {
Dialog::Destroy(progress);
@@ -599,7 +598,7 @@ void Manager::launchBrowser()
if(m_changes && promptApply())
apply();
- m_reapack->browsePackages();
+ g_reapack->browsePackages();
}
void Manager::options()
@@ -608,17 +607,17 @@ void Manager::options()
UINT index = menu.addAction(
AUTO_STR("&Install new packages when synchronizing"), ACTION_AUTOINSTALL);
- if(m_autoInstall.value_or(m_config->install.autoInstall))
+ if(m_autoInstall.value_or(g_reapack->config()->install.autoInstall))
menu.check(index);
index = menu.addAction(
AUTO_STR("Enable &pre-releases globally (bleeding edge)"), ACTION_BLEEDINGEDGE);
- if(m_bleedingEdge.value_or(m_config->install.bleedingEdge))
+ if(m_bleedingEdge.value_or(g_reapack->config()->install.bleedingEdge))
menu.check(index);
index = menu.addAction(
AUTO_STR("Prompt to uninstall obsolete packages"), ACTION_PROMPTOBSOLETE);
- if(m_promptObsolete.value_or(m_config->install.promptObsolete))
+ if(m_promptObsolete.value_or(g_reapack->config()->install.promptObsolete))
menu.check(index);
menu.addAction(AUTO_STR("&Network settings..."), ACTION_NETCONFIG);
@@ -632,8 +631,8 @@ void Manager::options()
void Manager::setupNetwork()
{
- if(IDOK == Dialog::Show<NetworkConfig>(instance(), handle(), &m_config->network))
- m_config->write();
+ if(IDOK == Dialog::Show<NetworkConfig>(instance(), handle(), &g_reapack->config()->network))
+ g_reapack->config()->write();
}
bool Manager::confirm() const
@@ -660,7 +659,7 @@ bool Manager::apply()
if(!m_changes)
return true;
- Transaction *tx = m_reapack->setupTransaction();
+ Transaction *tx = g_reapack->setupTransaction();
if(!tx)
return false;
@@ -668,19 +667,19 @@ bool Manager::apply()
set<Remote> syncList;
if(m_autoInstall) {
- m_config->install.autoInstall = m_autoInstall.value();
+ g_reapack->config()->install.autoInstall = m_autoInstall.value();
if(m_autoInstall.value()) {
- const auto &enabledNow = m_config->remotes.getEnabled();
+ const auto &enabledNow = g_reapack->config()->remotes.getEnabled();
copy(enabledNow.begin(), enabledNow.end(), inserter(syncList, syncList.end()));
}
}
if(m_bleedingEdge)
- m_config->install.bleedingEdge = m_bleedingEdge.value();
+ g_reapack->config()->install.bleedingEdge = m_bleedingEdge.value();
if(m_promptObsolete)
- m_config->install.promptObsolete = m_promptObsolete.value();
+ g_reapack->config()->install.promptObsolete = m_promptObsolete.value();
for(const auto &pair : m_mods) {
Remote remote = pair.first;
@@ -690,7 +689,7 @@ bool Manager::apply()
continue;
if(mods.enable) {
- m_reapack->setRemoteEnabled(remote, *mods.enable);
+ g_reapack->setRemoteEnabled(remote, *mods.enable);
if(*mods.enable)
syncList.insert(remote);
@@ -699,7 +698,7 @@ bool Manager::apply()
}
if(mods.autoInstall) {
remote.setAutoInstall(*mods.autoInstall);
- m_config->remotes.add(remote);
+ g_reapack->config()->remotes.add(remote);
const bool isEnabled = mods.enable.value_or(remote.isEnabled());
@@ -709,14 +708,14 @@ bool Manager::apply()
}
for(const Remote &remote : m_uninstall) {
- m_reapack->uninstall(remote);
+ g_reapack->uninstall(remote);
syncList.erase(remote);
}
for(const Remote &remote : syncList)
tx->synchronize(remote);
- tx->onFinish(bind(&Config::write, m_config));
+ tx->onFinish(bind(&Config::write, g_reapack->config()));
tx->runTasks();
reset();
@@ -752,7 +751,7 @@ Remote Manager::getRemote(const int index) const
const ListView::Row &row = m_list->row(index);
const string &remoteName = from_autostring(row[0]);
- return m_config->remotes.get(remoteName);
+ return g_reapack->config()->remotes.get(remoteName);
}
NetworkConfig::NetworkConfig(NetworkOpts *opts)
diff --git a/src/manager.hpp b/src/manager.hpp
@@ -27,8 +27,6 @@
#include <map>
#include <set>
-class Config;
-class ReaPack;
class Remote;
struct NetworkOpts;
@@ -36,7 +34,7 @@ typedef boost::logic::tribool tribool;
class Manager : public Dialog {
public:
- Manager(ReaPack *);
+ Manager();
void refresh();
bool importRepo();
@@ -84,8 +82,6 @@ private:
void reset();
HWND m_apply;
- ReaPack *m_reapack;
- Config *m_config;
ListView *m_list;
size_t m_changes;
diff --git a/src/obsquery.hpp b/src/obsquery.hpp
@@ -24,7 +24,6 @@
#include <vector>
-class Config;
class ListView;
class ObsoleteQuery : public Dialog {
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -39,6 +39,8 @@ using namespace std;
const char *ReaPack::VERSION = "1.2beta2";
const char *ReaPack::BUILDTIME = __DATE__ " " __TIME__;
+ReaPack *ReaPack::s_instance = nullptr;
+
#ifdef _WIN32
// Removes temporary files that could not be removed by an installation task
// (eg. extensions dll that were in use by REAPER).
@@ -79,6 +81,9 @@ ReaPack::ReaPack(REAPER_PLUGIN_HINSTANCE instance)
m_tx(nullptr), m_progress(nullptr), m_browser(nullptr), m_manager(nullptr),
m_about(nullptr), m_instance(instance), m_useRootPath(resourcePath())
{
+ assert(!s_instance);
+ s_instance = this;
+
m_mainWindow = GetMainHwnd();
DownloadContext::GlobalInit();
@@ -107,6 +112,8 @@ ReaPack::~ReaPack()
delete m_config;
DownloadContext::GlobalCleanup();
+
+ s_instance = nullptr;
}
int ReaPack::setupAction(const char *name, const ActionCallback &callback)
@@ -240,7 +247,7 @@ void ReaPack::manageRemotes()
return;
}
- m_manager = Dialog::Create<Manager>(m_instance, m_mainWindow, this);
+ m_manager = Dialog::Create<Manager>(m_instance, m_mainWindow);
m_manager->show();
m_manager->setCloseHandler([=] (INT_PTR) {
@@ -283,7 +290,7 @@ About *ReaPack::about(const bool instantiate)
else if(!instantiate)
return nullptr;
- m_about = Dialog::Create<About>(m_instance, m_mainWindow, this);
+ m_about = Dialog::Create<About>(m_instance, m_mainWindow);
m_about->setCloseHandler([=] (INT_PTR) {
Dialog::Destroy(m_about);
@@ -307,7 +314,7 @@ Browser *ReaPack::browsePackages()
return nullptr;
}
- m_browser = Dialog::Create<Browser>(m_instance, m_mainWindow, this);
+ m_browser = Dialog::Create<Browser>(m_instance, m_mainWindow);
m_browser->refresh();
m_browser->setCloseHandler([=] (INT_PTR) {
Dialog::Destroy(m_browser);
@@ -326,7 +333,7 @@ Transaction *ReaPack::setupTransaction()
return m_tx;
try {
- m_tx = new Transaction(m_config);
+ m_tx = new Transaction;
}
catch(const reapack_error &e) {
const auto_string &desc = make_autostring(e.what());
diff --git a/src/reapack.hpp b/src/reapack.hpp
@@ -37,6 +37,8 @@ class Remote;
class Transaction;
struct APIFunc;
+#define g_reapack (ReaPack::instance())
+
class ReaPack {
public:
typedef std::function<void ()> ActionCallback;
@@ -44,13 +46,14 @@ public:
static const char *VERSION;
static const char *BUILDTIME;
+ static ReaPack *instance() { return s_instance; }
+ static std::string resourcePath();
+
gaccel_register_t syncAction;
gaccel_register_t browseAction;
gaccel_register_t importAction;
gaccel_register_t configAction;
- static std::string resourcePath();
-
ReaPack(REAPER_PLUGIN_HINSTANCE);
~ReaPack();
@@ -82,6 +85,8 @@ public:
Config *config() const { return m_config; }
private:
+ static ReaPack *s_instance;
+
void createDirectories();
void registerSelf();
void teardownTransaction();
diff --git a/src/task.cpp b/src/task.cpp
@@ -23,6 +23,7 @@
#include "errors.hpp"
#include "filesystem.hpp"
#include "index.hpp"
+#include "reapack.hpp"
#include "transaction.hpp"
using namespace std;
@@ -76,7 +77,7 @@ bool InstallTask::start()
push(ex, ex->path());
}
else {
- const NetworkOpts &opts = tx()->config()->network;
+ const NetworkOpts &opts = g_reapack->config()->network;
FileDownload *dl = new FileDownload(targetPath, src->url(), opts);
push(dl, dl->path());
}
diff --git a/src/transaction.cpp b/src/transaction.cpp
@@ -23,6 +23,7 @@
#include "errors.hpp"
#include "filesystem.hpp"
#include "index.hpp"
+#include "reapack.hpp"
#include "remote.hpp"
#include "task.hpp"
@@ -30,9 +31,8 @@
using namespace std;
-Transaction::Transaction(Config *config)
- : m_isCancelled(false), m_config(config),
- m_registry(Path::prefixRoot(Path::REGISTRY))
+Transaction::Transaction()
+ : m_isCancelled(false), m_registry(Path::prefixRoot(Path::REGISTRY))
{
m_threadPool.onPush([this] (ThreadTask *task) {
task->onFinish([=] {
@@ -58,7 +58,7 @@ void Transaction::synchronize(const Remote &remote,
m_syncedRemotes.insert(remote.name());
- InstallOpts opts = m_config->install;
+ InstallOpts opts = g_reapack->config()->install;
if(forceAutoInstall)
opts.autoInstall = *forceAutoInstall;
@@ -69,7 +69,7 @@ void Transaction::synchronize(const Remote &remote,
for(const Package *pkg : ri->packages())
synchronize(pkg, opts);
- if(m_config->install.promptObsolete && !remote.isProtected()) {
+ if(opts.promptObsolete && !remote.isProtected()) {
for(const Registry::Entry &entry : m_registry.getEntries(ri->name())) {
if(!entry.pinned && !ri->find(entry.category, entry.package))
m_obsolete.insert(entry);
@@ -124,6 +124,8 @@ vector<IndexPtr> Transaction::getIndexes(const vector<Remote> &remotes) const
void Transaction::fetchIndex(const Remote &remote, const bool stale,
const function<void (const IndexPtr &)> &cb)
{
+ const auto &netConfig = g_reapack->config()->network;
+
const auto load = [=] {
const IndexPtr ri = loadIndex(remote);
if(cb && ri)
@@ -134,14 +136,13 @@ void Transaction::fetchIndex(const Remote &remote, const bool stale,
time_t mtime = 0, now = time(nullptr);
FS::mtime(path, &mtime);
- const time_t threshold = m_config->network.staleThreshold;
+ const time_t threshold = netConfig.staleThreshold;
if(!stale && mtime && (!threshold || mtime > now - threshold)) {
load();
return;
}
- auto dl = new FileDownload(path, remote.url(),
- m_config->network, Download::NoCacheFlag);
+ auto dl = new FileDownload(path, remote.url(), netConfig, Download::NoCacheFlag);
dl->setName(remote.name());
dl->onFinish([=] {
@@ -385,7 +386,7 @@ void Transaction::inhibit(const Remote &remote)
void Transaction::promptObsolete()
{
- if(!m_config->install.promptObsolete || m_obsolete.empty())
+ if(!g_reapack->config()->install.promptObsolete || m_obsolete.empty())
return;
vector<Registry::Entry> selected;
diff --git a/src/transaction.hpp b/src/transaction.hpp
@@ -31,7 +31,6 @@
#include <unordered_set>
class ArchiveReader;
-class Config;
class Path;
class Remote;
struct InstallOpts;
@@ -46,7 +45,7 @@ public:
typedef std::function<void()> CleanupHandler;
typedef std::function<bool(std::vector<Registry::Entry> &)> ObsoleteHandler;
- Transaction(Config *);
+ Transaction();
void onFinish(const VoidSignal::slot_type &slot) { m_onFinish.connect(slot); }
void setCleanupHandler(const CleanupHandler &cb) { m_cleanupHandler = cb; }
@@ -67,7 +66,6 @@ public:
Receipt *receipt() { return &m_receipt; }
Registry *registry() { return &m_registry; }
- const Config *config() { return m_config; }
ThreadPool *threadPool() { return &m_threadPool; }
void registerAll(bool add, const Registry::Entry &);
@@ -98,7 +96,6 @@ private:
void finish();
bool m_isCancelled;
- const Config *m_config;
Registry m_registry;
Receipt m_receipt;