bool PluginManagerImpl::check_plugin_validity(const app_PluginRef &plugin, grt::Module *module)
{
  if (plugin->pluginType() == GUI_PLUGIN_TYPE)
  {
    // not much that can be tested here, maybe check if the dll actually exists
    return true;
  }
  else if (plugin->pluginType() == STANDALONE_GUI_PLUGIN_TYPE
           || plugin->pluginType() == NORMAL_PLUGIN_TYPE)
  {
    // check if the module matches and the function exists
    if (plugin->moduleName() != module->name())
    {
      g_warning("Plugin '%s' from module %s declares moduleName() as '%s', which doesn't match the module it belongs to",
                plugin->name().c_str(), module->name().c_str(), plugin->moduleName().c_str());
      return false;
    }
    
    {
      std::string f= plugin->moduleFunctionName();
      if (!module->has_function(f))
      {
         g_warning("Plugin '%s' from module %s has invalid moduleFunctionName '%s'",
                  plugin->name().c_str(), module->name().c_str(), f.c_str());
        return false;
      }
    } 
    return true;
  }
  else if (plugin->pluginType() == INTERNAL_PLUGIN_TYPE)
  {
    return true;
  }
  else if (0 == (*plugin->pluginType()).find(CUSTOM_PLUGIN_TYPE))
  {
    return true;
  }
  else
  {    
    g_warning("Plugin '%s' from module %s has invalid type '%s'", plugin->name().c_str(), 
              module->name().c_str(), plugin->pluginType().c_str());
  }
  return false;
}
std::string PluginManagerImpl::open_gui_plugin(const app_PluginRef &plugin, const grt::BaseListRef &args,
  GUIPluginFlags flags)
{
  if (!plugin.is_valid())
    throw std::invalid_argument("Attempt to open an invalid plugin");
  
  GRTDispatcher::Ref dispatcher = _grtm->get_dispatcher();
  if (*plugin->pluginType() == GUI_PLUGIN_TYPE)
  {
    if (_grtm->in_main_thread())
      return open_gui_plugin_main(plugin, args, flags);
    else
    {
      // Request the plugin to be executed and opened by the frontend in the main thread.
      DispatcherCallback<std::string>::Ref cb = DispatcherCallback<std::string>::create_callback(
        boost::bind(&PluginManagerImpl::open_gui_plugin_main, this, plugin, args, flags)
      );

      dispatcher->call_from_main_thread(cb, false, false);
      
      grt::Module *module= _grtm->get_grt()->get_module(_plugin_source_module[plugin->name()]);

      // Build the handle name ourselves.
      return make_open_plugin_id(module, plugin->moduleFunctionName(), args);
    }
  }
  else if (*plugin->pluginType() == STANDALONE_GUI_PLUGIN_TYPE)
  {
    if (_grtm->in_main_thread())
      open_standalone_plugin_main(plugin, args);
    else
    {
      // Request the plugin to be executed and opened by the frontend in the main thread.
      DispatcherCallback<void>::Ref cb = DispatcherCallback<void>::create_callback(
        boost::bind(&PluginManagerImpl::open_standalone_plugin_main, this, plugin, args)
      );
      dispatcher->call_from_main_thread(cb, false, false);
    }
  }
  else if (*plugin->pluginType() == INTERNAL_PLUGIN_TYPE)
  {
    if (_grtm->in_main_thread())
      open_normal_plugin_grt(_grtm->get_grt(), plugin, args);
    else
    {
      // Request the plugin to be executed and opened by the frontend in the main thread.
      DispatcherCallback<grt::ValueRef>::Ref cb = DispatcherCallback<grt::ValueRef>::create_callback(
        boost::bind(&PluginManagerImpl::open_normal_plugin_grt, this, _grtm->get_grt(), plugin, args)
      );
      
      dispatcher->call_from_main_thread(cb, false, false);
    }
  }
  else // A normal plugin implemented by a GRT module.
  {
    // Opening a normal plugin is usually done in the context of the grt thread and we want to
    // continue that way. But if we are currently in the main thread switch here to the grt thread
    // for opening the plugin.
    if (_grtm->in_main_thread())
    {
      _grtm->get_dispatcher()->execute_sync_function("Open normal plugin",
        boost::bind(&PluginManagerImpl::open_normal_plugin_grt, this, _1, plugin, args));
    }
    else
      open_normal_plugin_grt(_grtm->get_grt(), plugin, args);
  }
  return "";
}