From dcdc78332431ff2b258c54a99d6fac382c6a0595 Mon Sep 17 00:00:00 2001
From: Mark Tiefenbruck <mark@fluxbox.org>
Date: Mon, 12 May 2008 12:16:37 -0700
Subject: only reload the keys file if the contents have changed

---
 ChangeLog      |  2 ++
 src/Keys.cc    | 37 ++++++++++++-------------------------
 src/Keys.hh    | 23 +++++++++--------------
 src/fluxbox.cc |  6 +++---
 src/fluxbox.hh |  1 +
 5 files changed, 27 insertions(+), 42 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e441093..89a93d6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
  (Format: Year/Month/Day)
 Changes for 1.1
 *08/05/12:
+   * Only reload the keys file if the contents have changed (Mark)
+     Keys.cc/hh fluxbox.cc/hh
    * Modifying the apps file no longer requires a reconfigure (Mark)
      Remember.cc/hh
 *08/05/09:
diff --git a/src/Keys.cc b/src/Keys.cc
index eb3d9d5..ec1fde0 100644
--- a/src/Keys.cc
+++ b/src/Keys.cc
@@ -170,7 +170,9 @@ Keys::t_key::~t_key() {
 
 
 
-Keys::Keys() : next_key(0) { }
+Keys::Keys(): next_key(0) {
+    m_reloader.setReloadCmd(FbTk::RefCount<FbTk::Command<void> >(new FbTk::SimpleCommand<Keys>(*this, &Keys::reload)));
+}
 
 Keys::~Keys() {
     ungrabKeys();
@@ -255,27 +257,25 @@ void Keys::grabWindow(Window win) {
 /**
     Load and grab keys
     TODO: error checking
-    @return true on success else false
 */
-bool Keys::load(const char *filename) {
+void Keys::reload() {
     // an intentionally empty file will still have one root mapping
     bool firstload = m_map.empty();
 
-    if (!filename) {
+    if (m_filename.empty()) {
         if (firstload)
             loadDefaults();
-        return false;
+        return;
     }
 
     FbTk::App::instance()->sync(false);
 
     // open the file
-    ifstream infile(filename);
+    ifstream infile(m_filename.c_str());
     if (!infile) {
         if (firstload)
             loadDefaults();
-
-        return false; // failed to open file
+        return; // failed to open file
     }
 
     // free memory of previous grabs
@@ -301,9 +301,7 @@ bool Keys::load(const char *filename) {
         }
     } // end while eof
 
-    m_filename = filename;
     keyMode("default");
-    return true;
 }
 
 /**
@@ -321,19 +319,6 @@ void Keys::loadDefaults() {
     keyMode("default");
 }
 
-bool Keys::save(const char *filename) const {
-    //!!
-    //!! TODO: fix keybinding saving
-    //!! (we probably need to save key actions
-    //!! as strings instead of creating new Commands)
-
-    // open file for writing
-    //    ofstream outfile(filename);
-    //    if (!outfile)
-    return false;
-    //    return true;
-}
-
 bool Keys::addBinding(const string &linebuffer) {
 
     vector<string> val;
@@ -583,8 +568,10 @@ void Keys::unregisterWindow(Window win) {
  deletes the tree and load configuration
  returns true on success else false
 */
-bool Keys::reconfigure(const char *filename) {
-    return load(filename);
+void Keys::reconfigure() {
+    m_filename = FbTk::StringUtil::expandFilename(Fluxbox::instance()->getKeysFilename());
+    m_reloader.setMainFile(m_filename);
+    m_reloader.checkReload();
 }
 
 void Keys::keyMode(const string& keyMode) {
diff --git a/src/Keys.hh b/src/Keys.hh
index 7ebb0a7..68ff4ec 100644
--- a/src/Keys.hh
+++ b/src/Keys.hh
@@ -23,6 +23,7 @@
 #define KEYS_HH
 
 #include "FbTk/NotCopyable.hh"
+#include "FbTk/AutoReloadHelper.hh"
 
 #include <X11/Xlib.h>
 #include <string>
@@ -57,17 +58,6 @@ public:
     /// destructor
     ~Keys();
 
-    /**
-       Load configuration from file
-       @return true on success, else false
-    */
-    bool load(const char *filename = 0);
-    /**
-       Save keybindings to a file
-       Note: the file will be overwritten
-       @return true on success, else false
-     */
-    bool save(const char *filename = 0) const;
     /// bind a key action from a string
     /// @return false on failure
     bool addBinding(const std::string &binding);
@@ -83,12 +73,12 @@ public:
     /// unregister window
     void unregisterWindow(Window win);
 
+    const std::string& filename() const { return m_filename; }
     /**
        Reload configuration from filename
        @return true on success, else false
     */
-    bool reconfigure(const char *filename);
-    const std::string& filename() const { return m_filename; }
+    void reconfigure();
     void keyMode(const std::string& keyMode);
 private:
     class t_key; // helper class to build a 'keytree'
@@ -104,13 +94,18 @@ private:
     void ungrabButtons();
     void grabWindow(Window win);
 
-    // Load default keybindings for when there are errors loading the initial one
+    /**
+       Load configuration from file
+    */
+    void reload();
+    // Load default keybindings for when there are errors loading the keys file
     void loadDefaults();
     void setKeyMode(t_key *keyMode);
 
 
     // member variables
     std::string m_filename;
+    FbTk::AutoReloadHelper m_reloader;
     t_key *m_keylist;
     keyspace_t m_map;
 
diff --git a/src/fluxbox.cc b/src/fluxbox.cc
index 70503a5..076ae45 100644
--- a/src/fluxbox.cc
+++ b/src/fluxbox.cc
@@ -307,7 +307,7 @@ Fluxbox::Fluxbox(int argc, char **argv, const char *dpy_name, const char *rcfile
     // Create keybindings handler and load keys file
     // Note: this needs to be done before creating screens
     m_key.reset(new Keys);
-    m_key->load(StringUtil::expandFilename(*m_rc_keyfile).c_str());
+    m_key->reconfigure();
 
     vector<int> screens;
     int i;
@@ -751,7 +751,7 @@ void Fluxbox::handleEvent(XEvent * const e) {
             XRefreshKeyboardMapping(&e->xmapping);
             FbTk::KeyUtil::instance().init(); // reinitialise the key utils
             // reconfigure keys (if the mapping changes, they don't otherwise update
-            m_key->reconfigure(StringUtil::expandFilename(*m_rc_keyfile).c_str());
+            m_key->reconfigure();
         }
         break;
     case CreateNotify:
@@ -1443,7 +1443,7 @@ void Fluxbox::real_reconfigure() {
     for_each(m_screen_list.begin(), m_screen_list.end(), mem_fun(&BScreen::reconfigure));
 
     //reconfigure keys
-    m_key->reconfigure(StringUtil::expandFilename(*m_rc_keyfile).c_str());
+    m_key->reconfigure();
 
     // and atomhandlers
     for (AtomHandlerContainerIt it= m_atomhandler.begin();
diff --git a/src/fluxbox.hh b/src/fluxbox.hh
index 8b22a04..fc86125 100644
--- a/src/fluxbox.hh
+++ b/src/fluxbox.hh
@@ -120,6 +120,7 @@ public:
     const std::string &getMenuFilename() const { return *m_rc_menufile; }
     const std::string &getSlitlistFilename() const { return *m_rc_slitlistfile; }
     const std::string &getAppsFilename() const { return *m_rc_appsfile; }
+    const std::string &getKeysFilename() const { return *m_rc_keyfile; }
     int colorsPerChannel() const { return *m_rc_colors_per_channel; }
     int getTabsPadding() const { return *m_rc_tabs_padding; }
 
-- 
cgit v0.11.2