From cf11e9c960636434f8b398f47b1e55d27ad18e4e Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 4 Aug 2011 11:02:04 +0200 Subject: Move most of the resource loading code into ResourceManager_base I mostly do this to avoid code duplication between fluxbox and fluxbox-update_configs. --- src/FbTk/LResource.cc | 30 +++------------------------- src/FbTk/LResource.hh | 3 +-- src/FbTk/Resource.cc | 39 ++++++++++++++++++++++++++++-------- src/FbTk/Resource.hh | 15 +++++++++----- src/fluxbox.cc | 8 +++++++- util/fluxbox-update_configs.cc | 45 ++++++++++++++++++++---------------------- 6 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc index de90b95..18209cb 100644 --- a/src/FbTk/LResource.cc +++ b/src/FbTk/LResource.cc @@ -116,34 +116,12 @@ LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int setLua(l); } -void LResourceManager::load(const std::string &filename, const std::string &fallback) { - _FB_USES_NLS; +void LResourceManager::doLoad(const std::string &filename) { m_l->checkstack(1); lua::stack_sentry s(*m_l); - m_filename = filename; - - try { - m_l->loadfile(filename.c_str()); - m_l->call(0, 0); - } - catch(lua::exception &e) { - std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", - "Failed trying to read rc file") << ":" << filename << std::endl; - std::cerr << "Fluxbox: " << e.what() << std::endl; - std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFileTrying, "Retrying with", - "Retrying rc file loading with (the following file)") - << ": " << fallback << std::endl; - try { - m_l->loadfile(fallback.c_str()); - m_l->call(0, 0); - } - catch(lua::exception &e) { - std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "") - << ": " << fallback << std::endl; - std::cerr << "Fluxbox: " << e.what() << std::endl; - } - } + m_l->loadfile(filename.c_str()); + m_l->call(0, 0); } bool LResourceManager::save(const char *filename, const char *) { @@ -153,8 +131,6 @@ bool LResourceManager::save(const char *filename, const char *) { if(filename == NULL) filename = m_filename.c_str(); - std::cerr << "XXX SAVING " << filename << std::endl; - m_l->getfield(lua::REGISTRYINDEX, dump_resources); m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); m_l->pushstring(filename); diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh index 30f0e02..a198197 100644 --- a/src/FbTk/LResource.hh +++ b/src/FbTk/LResource.hh @@ -45,7 +45,6 @@ public: * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled */ LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0); - void load(const std::string &filename, const std::string &fallback); virtual bool save(const char *filename, const char *); virtual void addResource(Resource_base &r); virtual void removeResource(Resource_base &r); @@ -55,9 +54,9 @@ public: private: void doAddResource(Resource_base &r); void doRemoveResource(Resource_base &r); + virtual void doLoad(const std::string &filename); Lua *m_l; - std::string m_filename; Timer m_savetimer; }; diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc index 37555b3..8f63322 100644 --- a/src/FbTk/Resource.cc +++ b/src/FbTk/Resource.cc @@ -38,6 +38,33 @@ using std::string; namespace FbTk { +void ResourceManager_base::load(const std::string &filename, const std::string &fallback) { + _FB_USES_NLS; + + m_filename = filename; + + try { + doLoad(filename); + } + catch(std::runtime_error &e) { + std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", + "Failed trying to read rc file") << ":" << filename << std::endl; + std::cerr << "Fluxbox: " << e.what() << std::endl; + std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFileTrying, "Retrying with", + "Retrying rc file loading with (the following file)") + << ": " << fallback << std::endl; + try { + doLoad(fallback); + } + catch(std::runtime_error &e) { + std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "") + << ": " << fallback << std::endl; + std::cerr << "Fluxbox: " << e.what() << std::endl; + throw; + } + } +} + void ResourceManager_base::addResource(Resource_base &r) { m_resourcelist.push_back(&r); m_resourcelist.unique(); @@ -87,7 +114,6 @@ ResourceManager::ResourceManager(const std::string &root, const std::string &alt ResourceManager_base(root), m_db_lock(0), m_database(0), - m_filename(filename ? filename : ""), m_alt_root(alt_root) { static bool xrm_initialized = false; @@ -96,6 +122,7 @@ ResourceManager::ResourceManager(const std::string &root, const std::string &alt xrm_initialized = true; } + m_filename = filename ? filename : ""; if (lock_db) lock(); } @@ -108,11 +135,9 @@ ResourceManager::~ResourceManager() { /** reloads all resources from resourcefile - @return true on success else false + throws an exception in case of failure */ -bool ResourceManager::load(const char *filename) { - m_filename = StringUtil::expandFilename(filename).c_str(); - +void ResourceManager::doLoad(const std::string &filename) { // force reload (lock will ensure it exists) if (m_database) { delete m_database; @@ -122,7 +147,7 @@ bool ResourceManager::load(const char *filename) { lock(); if (!m_database) { unlock(); - return false; + throw std::runtime_error(""); } XrmValue value; @@ -146,8 +171,6 @@ bool ResourceManager::load(const char *filename) { } unlock(); - - return true; } /** diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index c4a7951..700fe70 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh @@ -92,6 +92,11 @@ public: virtual ~ResourceManager_base() {} + /// Load all resources registered to this class + /// if loading of filename fails, it tries to load fallback + /// if that fails, it throws an exception + void load(const std::string &filename, const std::string &fallback); + /// Save all resouces registered to this class /// @return true on success virtual bool save(const char *filename, const char *mergefilename=0) = 0; @@ -130,8 +135,12 @@ public: ResourceList::const_iterator end() { return m_resourcelist.end(); } protected: + /// does the actual loading + virtual void doLoad(const std::string &filename) = 0; + ResourceList m_resourcelist; const std::string m_root; + std::string m_filename; }; class ResourceManager: public ResourceManager_base @@ -143,10 +152,6 @@ public: const char *filename, bool lock_db); virtual ~ResourceManager(); - /// Load all resources registered to this class - /// @return true on success - virtual bool load(const char *filename); - /// Save all resouces registered to this class /// @return true on success virtual bool save(const char *filename, const char *mergefilename=0); @@ -173,6 +178,7 @@ public: } } protected: + virtual void doLoad(const std::string &filename); int m_db_lock; @@ -180,7 +186,6 @@ private: XrmDatabaseHelper *m_database; - std::string m_filename; std::string m_alt_root; }; diff --git a/src/fluxbox.cc b/src/fluxbox.cc index 1dc3105..c7f6ee2 100644 --- a/src/fluxbox.cc +++ b/src/fluxbox.cc @@ -1151,7 +1151,13 @@ string Fluxbox::getDefaultDataFilename(const char *name) const { /// loads resources void Fluxbox::load_rc() { - m_resourcemanager.load(getRcFilename(), DEFAULT_INITFILE); + try { + m_resourcemanager.load(getRcFilename(), DEFAULT_INITFILE); + } + catch(std::runtime_error &) { + // This should only happen if system-wide init file is broken. + // Not much we can do about that, so we just ignore it + } if (m_rc_menufile->empty()) m_rc_menufile.setDefaultValue(); diff --git a/util/fluxbox-update_configs.cc b/util/fluxbox-update_configs.cc index c7e7f4d..7d3371d 100644 --- a/util/fluxbox-update_configs.cc +++ b/util/fluxbox-update_configs.cc @@ -74,7 +74,7 @@ void save_all_files(); /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ -void update_add_mouse_evens_to_keys(FbTk::ResourceManager& rm, +void update_add_mouse_evens_to_keys(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); @@ -111,7 +111,7 @@ void update_add_mouse_evens_to_keys(FbTk::ResourceManager& rm, } -void update_move_groups_entries_to_apps_file(FbTk::ResourceManager& rm, +void update_move_groups_entries_to_apps_file(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { FbTk::StringResource rc_groupfile(rm, "~/.fluxbox/groups", @@ -148,7 +148,7 @@ void update_move_groups_entries_to_apps_file(FbTk::ResourceManager& rm, } -void update_move_toolbar_wheeling_to_keys_file(FbTk::ResourceManager& rm, +void update_move_toolbar_wheeling_to_keys_file(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); @@ -187,7 +187,7 @@ void update_move_toolbar_wheeling_to_keys_file(FbTk::ResourceManager& rm, -void update_move_modkey_to_keys_file(FbTk::ResourceManager& rm, +void update_move_modkey_to_keys_file(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); string new_keyfile = ""; @@ -222,7 +222,7 @@ void update_move_modkey_to_keys_file(FbTk::ResourceManager& rm, -void update_window_patterns_for_iconbar(FbTk::ResourceManager& rm, +void update_window_patterns_for_iconbar(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { // this needs to survive after going out of scope @@ -251,7 +251,7 @@ void update_window_patterns_for_iconbar(FbTk::ResourceManager& rm, } -void update_move_titlebar_actions_to_keys_file(FbTk::ResourceManager& rm, +void update_move_titlebar_actions_to_keys_file(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); string new_keyfile = ""; @@ -288,7 +288,7 @@ void update_move_titlebar_actions_to_keys_file(FbTk::ResourceManager& rm, } -void update_added_starttabbing_command(FbTk::ResourceManager& rm, +void update_added_starttabbing_command(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); string new_keyfile = ""; @@ -302,7 +302,7 @@ void update_added_starttabbing_command(FbTk::ResourceManager& rm, -void update_disable_icons_in_tabs_for_backwards_compatibility(FbTk::ResourceManager& rm, +void update_disable_icons_in_tabs_for_backwards_compatibility(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { FbTk::BoolResource *show = @@ -316,7 +316,7 @@ void update_disable_icons_in_tabs_for_backwards_compatibility(FbTk::ResourceMana -void update_change_format_of_split_placement_menu(FbTk::ResourceManager& rm, +void update_change_format_of_split_placement_menu(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { FbTk::StringResource *placement = @@ -344,7 +344,7 @@ void update_change_format_of_split_placement_menu(FbTk::ResourceManager& rm, -void update_update_keys_file_for_nextwindow_syntax_changes(FbTk::ResourceManager& rm, +void update_update_keys_file_for_nextwindow_syntax_changes(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); @@ -401,7 +401,7 @@ void update_update_keys_file_for_nextwindow_syntax_changes(FbTk::ResourceManager -void update_keys_for_ongrip_onwindowborder(FbTk::ResourceManager& rm, +void update_keys_for_ongrip_onwindowborder(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_keyfile = read_file(keyfilename); @@ -421,7 +421,7 @@ void update_keys_for_ongrip_onwindowborder(FbTk::ResourceManager& rm, -void update_keys_for_activetab(FbTk::ResourceManager& rm, +void update_keys_for_activetab(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_file = read_file(keyfilename); @@ -439,7 +439,7 @@ void update_keys_for_activetab(FbTk::ResourceManager& rm, // NextWindow {static groups} => NextWindow {static groups} (workspace=[current]) -void update_limit_nextwindow_to_current_workspace(FbTk::ResourceManager& rm, +void update_limit_nextwindow_to_current_workspace(FbTk::ResourceManager_base& rm, const FbTk::FbString& keyfilename, const FbTk::FbString& appsfilename) { string whole_file = read_file(keyfilename); @@ -530,7 +530,7 @@ void update_limit_nextwindow_to_current_workspace(FbTk::ResourceManager& rm, struct Update { int version; - void (*update)(FbTk::ResourceManager& rm, const FbTk::FbString&, const FbTk::FbString&); + void (*update)(FbTk::ResourceManager_base& rm, const FbTk::FbString&, const FbTk::FbString&); }; const Update UPDATES[] = { @@ -618,16 +618,13 @@ int main(int argc, char **argv) { rc_filename = getenv("HOME") + string("/.fluxbox/init"); FbTk::ResourceManager resource_manager("session", "Session", rc_filename.c_str(),false); - if (!resource_manager.load(rc_filename.c_str())) { - // couldn't load rc file - cerr<<_FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "Failed trying to read rc file")<<":"<