commit 9322d9fdf16c490fa5467f0bec0378597b338f5c
parent 4dee328be69f114333e69a321252f72fdfdaee82
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 21 Aug 2016 02:19:25 -0400
refactor configuration access throughout the code base
Diffstat:
10 files changed, 72 insertions(+), 75 deletions(-)
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -144,7 +144,7 @@ void Browser::onInit()
setAnchor(getControl(IDCANCEL), AnchorAll);
setAnchor(m_applyBtn, AnchorAll);
- auto data = m_serializer.read(m_reapack->config()->browser()->state, 1);
+ auto data = m_serializer.read(m_reapack->config()->browser.state, 1);
Dialog::restore(data);
m_list->restore(data);
assert(data.empty());
@@ -280,7 +280,7 @@ void Browser::onClose()
Serializer::Data data;
Dialog::save(data);
m_list->save(data);
- m_reapack->config()->browser()->state = m_serializer.write(data);
+ m_reapack->config()->browser.state = m_serializer.write(data);
}
void Browser::onTimer(const int id)
@@ -432,11 +432,10 @@ void Browser::displayButton()
menu.checkRadio(index);
}
- const auto config = m_reapack->config()->browser();
menu.addSeparator();
const auto i = menu.addAction(AUTO_STR("Show &descriptions"), ACTION_SHOWDESCS);
- if(config->showDescs)
+ if(m_reapack->config()->browser.showDescs)
menu.check(i);
menu.addAction(AUTO_STR("&Refresh repositories"), ACTION_REFRESH);
@@ -481,8 +480,8 @@ bool Browser::isFiltered(Package::Type type) const
void Browser::toggleDescs()
{
- auto config = m_reapack->config()->browser();
- config->showDescs = !config->showDescs;
+ auto &config = m_reapack->config()->browser;
+ config.showDescs = !config.showDescs;
fillList();
}
@@ -510,7 +509,7 @@ void Browser::refresh(const bool stale)
if(m_loading)
return;
- const vector<Remote> &remotes = m_reapack->config()->remotes()->getEnabled();
+ const vector<Remote> &remotes = m_reapack->config()->remotes.getEnabled();
if(!m_loaded && remotes.empty()) {
m_loaded = true;
@@ -626,7 +625,7 @@ void Browser::transferActions()
auto Browser::makeEntry(const Package *pkg, const Registry::Entry ®Entry)
const -> Entry
{
- const auto &instOpts = *m_reapack->config()->install();
+ const auto &instOpts = m_reapack->config()->install;
const Version *latest = pkg->lastVersion(instOpts.bleedingEdge, regEntry.version);
const Version *current = nullptr;
@@ -731,13 +730,13 @@ string Browser::getValue(const Column col, const Entry &entry) const
return display;
}
case NameColumn: {
- const auto config = m_reapack->config()->browser();
+ const auto &config = m_reapack->config()->browser;
if(pkg)
- return pkg->displayName(config->showDescs);
+ return pkg->displayName(config.showDescs);
else
return Package::displayName(regEntry.package,
- regEntry.description, config->showDescs);
+ regEntry.description, config.showDescs);
}
case CategoryColumn:
return pkg ? pkg->category()->name() : regEntry.category;
diff --git a/src/config.cpp b/src/config.cpp
@@ -62,9 +62,9 @@ Config::Config()
void Config::resetOptions()
{
- m_install = {false, false};
- m_browser = {true, ""};
- m_network = {"", true};
+ install = {false, false};
+ browser = {true, ""};
+ network = {"", true};
}
void Config::restoreSelfRemote()
@@ -72,12 +72,12 @@ void Config::restoreSelfRemote()
const string name = "ReaPack";
const string url = "https://github.com/cfillion/reapack/raw/master/index.xml";
- Remote remote = m_remotes.get(name);
+ Remote remote = remotes.get(name);
remote.setName(name);
remote.setUrl(url);
remote.protect();
- m_remotes.add(remote);
+ remotes.add(remote);
}
void Config::restoreDefaultRemotes()
@@ -94,14 +94,14 @@ void Config::restoreDefaultRemotes()
};
for(const auto &pair : repos) {
- Remote remote = m_remotes.get(pair.first);
+ Remote remote = remotes.get(pair.first);
if(!remote)
remote.setEnabled(false); // disable by default
remote.setName(pair.first);
remote.setUrl(pair.second);
- m_remotes.add(remote);
+ remotes.add(remote);
}
}
@@ -129,18 +129,18 @@ void Config::read(const Path &path)
{
m_path = make_autostring(path.join());
- m_install.autoInstall = getUInt(INSTALL_GRP,
- AUTOINSTALL_KEY, m_install.autoInstall) > 0;
- m_install.bleedingEdge = getUInt(INSTALL_GRP,
- PRERELEASES_KEY, m_install.bleedingEdge) > 0;
+ install.autoInstall = getUInt(INSTALL_GRP,
+ AUTOINSTALL_KEY, install.autoInstall) > 0;
+ install.bleedingEdge = getUInt(INSTALL_GRP,
+ PRERELEASES_KEY, install.bleedingEdge) > 0;
- m_browser.showDescs = getUInt(BROWSER_GRP,
- SHOWDESCS_KEY, m_browser.showDescs) > 0;
- m_browser.state = getString(BROWSER_GRP, STATE_KEY, m_browser.state);
+ browser.showDescs = getUInt(BROWSER_GRP,
+ SHOWDESCS_KEY, browser.showDescs) > 0;
+ browser.state = getString(BROWSER_GRP, STATE_KEY, browser.state);
- m_network.proxy = getString(NETWORK_GRP, PROXY_KEY, m_network.proxy);
- m_network.verifyPeer = getUInt(NETWORK_GRP,
- VERIFYPEER_KEY, m_network.verifyPeer) > 0;
+ network.proxy = getString(NETWORK_GRP, PROXY_KEY, network.proxy);
+ network.verifyPeer = getUInt(NETWORK_GRP,
+ VERIFYPEER_KEY, network.verifyPeer) > 0;
readRemotes();
restoreSelfRemote();
@@ -151,14 +151,14 @@ void Config::write()
{
setUInt(GENERAL_GRP, VERSION_KEY, m_version);
- setUInt(INSTALL_GRP, AUTOINSTALL_KEY, m_install.autoInstall);
- setUInt(INSTALL_GRP, PRERELEASES_KEY, m_install.bleedingEdge);
+ setUInt(INSTALL_GRP, AUTOINSTALL_KEY, install.autoInstall);
+ setUInt(INSTALL_GRP, PRERELEASES_KEY, install.bleedingEdge);
- setUInt(BROWSER_GRP, SHOWDESCS_KEY, m_browser.showDescs);
- setString(BROWSER_GRP, STATE_KEY, m_browser.state);
+ setUInt(BROWSER_GRP, SHOWDESCS_KEY, browser.showDescs);
+ setString(BROWSER_GRP, STATE_KEY, browser.state);
- setString(NETWORK_GRP, PROXY_KEY, m_network.proxy);
- setUInt(NETWORK_GRP, VERIFYPEER_KEY, m_network.verifyPeer);
+ setString(NETWORK_GRP, PROXY_KEY, network.proxy);
+ setUInt(NETWORK_GRP, VERIFYPEER_KEY, network.verifyPeer);
writeRemotes();
}
@@ -170,18 +170,17 @@ void Config::readRemotes()
for(unsigned int i = 0; i < m_remotesIniSize; i++) {
const string data = getString(REMOTES_GRP, ArrayKey(REMOTE_KEY, i));
- m_remotes.add(Remote::fromString(data));
+ remotes.add(Remote::fromString(data));
}
}
void Config::writeRemotes()
{
unsigned int i = 0;
- m_remotesIniSize = max((unsigned int)m_remotes.size(), m_remotesIniSize);
+ m_remotesIniSize = max((unsigned int)remotes.size(), m_remotesIniSize);
- for(auto it = m_remotes.begin(); it != m_remotes.end(); it++, i++) {
+ for(auto it = remotes.begin(); it != remotes.end(); it++, i++)
setString(REMOTES_GRP, ArrayKey(REMOTE_KEY, i), it->toString());
- }
cleanupArray(REMOTES_GRP, REMOTE_KEY, i, m_remotesIniSize);
diff --git a/src/config.hpp b/src/config.hpp
@@ -51,10 +51,11 @@ public:
void restoreDefaultRemotes();
bool isFirstRun() const { return m_isFirstRun; }
- InstallOpts *install() { return &m_install; }
- BrowserOpts *browser() { return &m_browser; }
- NetworkOpts *network() { return &m_network; }
- RemoteList *remotes() { return &m_remotes; }
+
+ InstallOpts install;
+ BrowserOpts browser;
+ NetworkOpts network;
+ RemoteList remotes;
private:
std::string getString(const auto_char *grp,
@@ -76,14 +77,9 @@ private:
bool m_isFirstRun;
unsigned int m_version;
- InstallOpts m_install;
- BrowserOpts m_browser;
- NetworkOpts m_network;
-
void readRemotes();
void restoreSelfRemote();
void writeRemotes();
- RemoteList m_remotes;
unsigned int m_remotesIniSize;
};
diff --git a/src/import.cpp b/src/import.cpp
@@ -90,8 +90,7 @@ void Import::fetch()
setWaiting(true);
- const NetworkOpts &opts = *m_reapack->config()->network();
- Download *dl = m_download = new Download({}, url, opts);
+ Download *dl = m_download = new Download({}, url, m_reapack->config()->network);
dl->onFinish([=] {
const Download::State state = dl->state();
@@ -191,7 +190,7 @@ bool Import::import(const Remote &remote)
}
Config *config = m_reapack->config();
- config->remotes()->add(remote);
+ config->remotes.add(remote);
config->write();
FS::write(Index::pathFor(remote.name()), m_download->contents());
diff --git a/src/manager.cpp b/src/manager.cpp
@@ -92,10 +92,10 @@ void Manager::onCommand(const int id, int)
uninstall();
break;
case ACTION_AUTOINSTALL:
- toggle(m_autoInstall, m_config->install()->autoInstall);
+ toggle(m_autoInstall, m_config->install.autoInstall);
break;
case ACTION_BLEEDINGEDGE:
- toggle(m_bleedingEdge, m_config->install()->bleedingEdge);
+ toggle(m_bleedingEdge, m_config->install.bleedingEdge);
break;
case ACTION_NETCONFIG:
setupNetwork();
@@ -222,7 +222,7 @@ void Manager::refresh()
m_list->clear();
- for(const Remote &remote : *m_config->remotes()) {
+ for(const Remote &remote : m_config->remotes) {
if(m_uninstall.count(remote))
continue;
@@ -374,12 +374,12 @@ 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(m_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(m_config->install.bleedingEdge))
menu.check(index);
menu.addAction(AUTO_STR("&Network settings..."), ACTION_NETCONFIG);
@@ -393,10 +393,7 @@ void Manager::options()
void Manager::setupNetwork()
{
- const auto ret = Dialog::Show<NetworkConfig>(instance(), handle(),
- m_config->network());
-
- if(ret == IDOK)
+ if(IDOK == Dialog::Show<NetworkConfig>(instance(), handle(), &m_config->network))
m_config->write();
}
@@ -430,10 +427,10 @@ bool Manager::apply()
return false;
if(m_autoInstall)
- m_config->install()->autoInstall = m_autoInstall.value();
+ m_config->install.autoInstall = m_autoInstall.value();
if(m_bleedingEdge)
- m_config->install()->bleedingEdge = m_bleedingEdge.value();
+ m_config->install.bleedingEdge = m_bleedingEdge.value();
for(const auto &pair : m_enableOverrides) {
const Remote &remote = pair.first;
@@ -481,7 +478,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 m_config->remotes.get(remoteName);
}
NetworkConfig::NetworkConfig(NetworkOpts *opts)
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -140,7 +140,7 @@ bool ReaPack::execActions(const int id, const int)
void ReaPack::synchronizeAll()
{
- const vector<Remote> &remotes = m_config->remotes()->getEnabled();
+ const vector<Remote> &remotes = m_config->remotes.getEnabled();
if(remotes.empty()) {
ShowMessageBox("No repository enabled, nothing to do!", "ReaPack", MB_OK);
@@ -171,7 +171,7 @@ void ReaPack::setRemoteEnabled(const bool enable, const Remote &remote)
if(m_tx->isCancelled())
return;
- m_config->remotes()->add(copy);
+ m_config->remotes.add(copy);
refreshManager();
});
}
@@ -186,7 +186,7 @@ void ReaPack::uninstall(const Remote &remote)
m_tx->onFinish([=] {
if(!m_tx->isCancelled())
- m_config->remotes()->remove(remote);
+ m_config->remotes.remove(remote);
});
}
@@ -218,7 +218,7 @@ void ReaPack::manageRemotes()
Remote ReaPack::remote(const string &name) const
{
- return m_config->remotes()->get(name);
+ return m_config->remotes.get(name);
}
void ReaPack::aboutSelf()
@@ -333,7 +333,7 @@ void ReaPack::fetchIndexes(const vector<Remote> &remotes,
void ReaPack::doFetchIndex(const Remote &remote, DownloadQueue *queue,
HWND parent, const bool stale)
{
- Download *dl = Index::fetch(remote, stale, *m_config->network());
+ Download *dl = Index::fetch(remote, stale, m_config->network);
if(!dl)
return;
diff --git a/src/remote.hpp b/src/remote.hpp
@@ -66,6 +66,9 @@ private:
class RemoteList {
public:
+ RemoteList() {}
+ RemoteList(const RemoteList &) = delete;
+
void add(const Remote &);
void remove(const Remote &remote) { remove(remote.name()); }
void remove(const std::string &name);
diff --git a/src/task.cpp b/src/task.cpp
@@ -67,7 +67,7 @@ void InstallTask::doStart()
const Path &path = it->first;
const Source *src = it->second;
- const NetworkOpts &opts = *transaction()->config()->network();
+ const NetworkOpts &opts = transaction()->config()->network;
Download *dl = new Download(src->fullName(), src->url(), opts);
dl->onFinish(bind(&InstallTask::saveSource, this, dl, src));
diff --git a/src/transaction.cpp b/src/transaction.cpp
@@ -70,11 +70,13 @@ Transaction::~Transaction()
delete m_registry;
}
-void Transaction::synchronize(const Remote &remote, const bool forceAutoInstall)
+void Transaction::synchronize(const Remote &remote,
+ const boost::optional<bool> forceAutoInstall)
{
- InstallOpts opts = *m_config->install();
+ InstallOpts opts = m_config->install;
+
if(forceAutoInstall)
- opts.autoInstall = true;
+ opts.autoInstall = *forceAutoInstall;
fetchIndex(remote, [=] {
IndexPtr ri;
@@ -126,7 +128,7 @@ void Transaction::fetchIndex(const Remote &remote, const IndexCallback &cb)
if(m_remotes.count(name) > 1)
return;
- Download *dl = Index::fetch(remote, true, *m_config->network());
+ Download *dl = Index::fetch(remote, true, m_config->network);
if(!dl) {
// the index was last downloaded less than a few seconds ago
diff --git a/src/transaction.hpp b/src/transaction.hpp
@@ -22,6 +22,7 @@
#include "receipt.hpp"
#include "registry.hpp"
+#include <boost/optional.hpp>
#include <boost/signals2.hpp>
#include <functional>
#include <memory>
@@ -50,7 +51,8 @@ public:
void onFinish(const VoidSignal::slot_type &slot) { m_onFinish.connect(slot); }
void setCleanupHandler(const CleanupHandler &cb) { m_cleanupHandler = cb; }
- void synchronize(const Remote &, bool forceAutoInstall = false);
+ void synchronize(const Remote &,
+ boost::optional<bool> forceAutoInstall = boost::none);
void install(const Version *);
void setPinned(const Package *, bool pinned);
void setPinned(const Registry::Entry &, bool pinned);
@@ -63,7 +65,7 @@ public:
const Receipt &receipt() const { return m_receipt; }
DownloadQueue *downloadQueue() { return &m_downloadQueue; }
- Config *config() { return m_config; }
+ const Config *config() { return m_config; }
bool saveFile(Download *, const Path &);
void addError(const std::string &msg, const std::string &title);
@@ -88,7 +90,7 @@ private:
void inhibit(const Remote &);
bool m_isCancelled;
- Config *m_config;
+ const Config *m_config;
Registry *m_registry;
Receipt m_receipt;