title: Using firebreath plugin to open a folder dialog and return folder selection asynchronously so the java Script will not be blocked
body: <p>I want only one folder choose dialog to be opened at any one time.
  Once the user selected the folder an event will be fired inform the javaScript of the selected folder.</p>
  
  <p>To open the dialog i included the code from the following gist in my project
  <a href="https://gist.github.com/1368648" rel="nofollow">DialogManager abstraction for FireBreath</a></p>
  
  <h1>pluginAPI cpp</h1>
  
  <p>I maintain a global pointer to the plugin so its functions can be called from any context</p>
  
  <p>Q: is this the best way?</p>
  
  <pre><code>#include "DialogManagerWin.h"
  pluginAPI * g_thePlugin;
  </code></pre>
  
  <h2>I set it in the pluginAPI CTOR</h2>
  
  <pre><code>g_thePlugin = this;
  </code></pre>
  
  <h1>pluginAPI.h</h1>
  
  <h2>This goes to pluginAPI</h2>
  
  <p>this is the event i want to give the javascript the selected folder</p>
  
  <pre><code>FB_JSAPI_EVENT(folderselected, 1, (const std::string&amp;));
  </code></pre>
  
  <h2>the API to call from javaScript:</h2>
  
  <p>calls the code from the gist to open the folder selection in another thread to avoid blocking and causing the browser to freeze.</p>
  
  <pre><code>void pluginAPI::SelectFolder(std::string initialFolderPath)
  {
    FB::PluginWindow* pluginWindow = pluginAPI::getPlugin()-&gt;GetWindow();
    DialogManager* dlg_mgr = DialogManager::get();
    dlg_mgr-&gt;OpenFolderDialog(m_host, pluginWindow, folderSelectorCallback);
  
    return;
  }
  </code></pre>
  
  <h2>The callback</h2>
  
  <p>this callback will be called once the user selected a folder</p>
  
  <pre><code>void folderSelectorCallback(const std::string&amp; folderSelected)
  {
      g_thePlugin-&gt;fire_folderselected(folderSelected);
      return ;
  }
  </code></pre>