$search
00001 /* 00002 * Copyright (c) 2009, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include <stdint.h> 00031 00032 #include "plugin_manager.h" 00033 #include "plugin.h" 00034 00035 #include <ros/console.h> 00036 #include <ros/package.h> 00037 00038 #include <wx/confbase.h> 00039 00040 namespace rviz 00041 { 00042 00043 PluginManager::PluginManager() 00044 { 00045 00046 } 00047 00048 PluginManager::~PluginManager() 00049 { 00050 00051 } 00052 00053 void PluginManager::loadDescriptions() 00054 { 00055 ros::package::V_string plugins; 00056 ros::package::getPlugins("rviz", "plugin", plugins); 00057 00058 ros::package::V_string::iterator it = plugins.begin(); 00059 ros::package::V_string::iterator end = plugins.end(); 00060 for (; it != end; ++it) 00061 { 00062 const std::string& plugin = *it; 00063 loadDescription(plugin); 00064 } 00065 } 00066 00067 PluginPtr PluginManager::getPlugin(const std::string& description_file) 00068 { 00069 L_Plugin::iterator it = plugins_.begin(); 00070 L_Plugin::iterator end = plugins_.end(); 00071 for (; it != end; ++it) 00072 { 00073 const PluginPtr& plugin = *it; 00074 00075 if (plugin->getDescriptionPath() == description_file) 00076 { 00077 return plugin; 00078 } 00079 } 00080 00081 return PluginPtr(); 00082 } 00083 00084 PluginPtr PluginManager::getPluginByPackage(const std::string& package) 00085 { 00086 L_Plugin::iterator it = plugins_.begin(); 00087 L_Plugin::iterator end = plugins_.end(); 00088 for (; it != end; ++it) 00089 { 00090 const PluginPtr& plugin = *it; 00091 00092 if (plugin->getPackageName() == package) 00093 { 00094 return plugin; 00095 } 00096 } 00097 00098 return PluginPtr(); 00099 } 00100 00101 PluginPtr PluginManager::getPluginByDisplayName(const std::string& display_name) 00102 { 00103 L_Plugin::iterator it = plugins_.begin(); 00104 L_Plugin::iterator end = plugins_.end(); 00105 for (; it != end; ++it) 00106 { 00107 const PluginPtr& plugin = *it; 00108 00109 if (plugin->getDisplayTypeInfoByDisplayName(display_name)) 00110 { 00111 return plugin; 00112 } 00113 } 00114 00115 return PluginPtr(); 00116 } 00117 00118 PluginPtr PluginManager::loadDescription(const std::string& description_path) 00119 { 00120 PluginPtr plugin = getPlugin(description_path); 00121 if (plugin) 00122 { 00123 return plugin; 00124 } 00125 00126 plugin.reset(new Plugin); 00127 00128 try 00129 { 00130 plugin->loadDescription(description_path); 00131 } 00132 catch (std::runtime_error& e) 00133 { 00134 ROS_ERROR("Error loading plugin from description [%s]: %s", description_path.c_str(), e.what()); 00135 } 00136 00137 plugins_.push_back(plugin); 00138 00139 return plugin; 00140 } 00141 00142 void PluginManager::loadConfig(const boost::shared_ptr<wxConfigBase>& config) 00143 { 00144 int i = 0; 00145 while (1) 00146 { 00147 wxString package_key, autoload_key; 00148 package_key.Printf( wxT("Plugins/Plugin%d/Package"), i ); 00149 autoload_key.Printf( wxT("Plugins/Plugin%d/AutoLoad"), i ); 00150 00151 wxString package; 00152 bool autoload; 00153 00154 if (!config->Read(package_key, &package)) 00155 { 00156 break; 00157 } 00158 00159 if (!config->Read(autoload_key, &autoload)) 00160 { 00161 break; 00162 } 00163 00164 PluginPtr plugin = getPluginByPackage((const char*)package.char_str()); 00165 if (plugin) 00166 { 00167 plugin->setAutoLoad(autoload); 00168 plugin->autoLoad(); 00169 } 00170 00171 ++i; 00172 } 00173 } 00174 00175 void PluginManager::saveConfig(const boost::shared_ptr<wxConfigBase>& config) 00176 { 00177 int i = 0; 00178 L_Plugin::iterator it = plugins_.begin(); 00179 L_Plugin::iterator end = plugins_.end(); 00180 for (; it != end; ++it, ++i) 00181 { 00182 const PluginPtr& plugin = *it; 00183 00184 wxString package_key, autoload_key; 00185 package_key.Printf( wxT("Plugins/Plugin%d/Package"), i ); 00186 autoload_key.Printf( wxT("Plugins/Plugin%d/AutoLoad"), i ); 00187 00188 config->Write(package_key, wxString::FromAscii(plugin->getPackageName().c_str())); 00189 config->Write(autoload_key, plugin->isAutoLoad()); 00190 } 00191 } 00192 00193 }