Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PluginManager.C

00001 /* $Id: PluginManager.C,v 1.2 2000/11/03 22:03:28 rpav Exp $
00002  *
00003  * SFW - Studio FrameWork
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Desc
00024  *
00025  */ 
00026 
00027 #include <string>
00028 #include <sys/types.h>
00029 #include <dirent.h>
00030 #include <SFW/GC.H>
00031 #include <SFW/Log.H>
00032 #include <SFW/Plugin.H>
00033 #include <SFW/PluginManager.H>
00034 
00035 using namespace SFW;
00036 
00037 SFW::PluginManager SFW::PM;
00038 
00039 TYPE_INIT(SFW::PluginSet);
00040 
00041 PluginManager::PluginManager() {
00042     /* Default place to look for base plugins */
00043     /* CHECK: This should be fixed, but something has to happen
00044      *        to handle plugins with dependencies.
00045      */
00046 #if 0    
00047     addPath(DEF_PLUGIN_PATH);
00048 #endif
00049 }
00050 
00051 PluginManager::~PluginManager() {
00052 
00053 }
00054 
00055 bool PluginManager::addPath(string _path) {
00056     mutex.lock();
00057 
00058     if(_path.find(':') != string::npos) {
00059     addMultiple(_path);
00060     goto end;
00061     }
00062     
00063     if(paths.count(_path) != 0)
00064     goto error;
00065 
00066     paths.insert(_path);
00067     addPluginsFromPath(plugins, _path);
00068 
00069  end:
00070     mutex.unlock();
00071     return true;
00072 
00073  error:
00074     mutex.unlock();
00075     return false;
00076 }
00077 
00078 bool PluginManager::delPath(string _path) {
00079     mutex.lock();
00080     bool ret = static_cast<bool>(paths.erase(_path));
00081     mutex.unlock();
00082 
00083     return ret;
00084 }
00085 
00086 Plugin* PluginManager::get(Plugin* _p) {
00087     mutex.lock();
00088     PluginSet::iterator i = plugins.find(_p);
00089     mutex.unlock();
00090 
00091     if(i != plugins.end())
00092         return *i;
00093     else
00094         return (Plugin*)0;
00095 }
00096 
00097 PluginSet* PluginManager::getSet() {
00098     PluginSet *pl = new PluginSet();
00099     string path;
00100 
00101     GC.use(pl);
00102 
00103     mutex.lock();
00104     PluginSet::iterator pli = plugins.begin();
00105 
00106     while(pli != plugins.end()) {
00107     pl->insert(*pli);
00108     pli++;
00109     }
00110     mutex.unlock();
00111     
00112     return pl;
00113 }
00114 
00115 PluginSet* PluginManager::getSet(string _type) {
00116     PluginSet *pl = new PluginSet();
00117 
00118     GC.use(pl);
00119 
00120     mutex.lock();
00121     PluginSet::iterator pli = plugins.begin();
00122 
00123     while(pli != plugins.end()) {
00124     if((*pli)->getType() == _type) {
00125         pl->insert(*pli);
00126     }
00127 
00128     pli++;
00129     }
00130     mutex.unlock();
00131     
00132     return pl;
00133 }
00134 
00135 bool PluginManager::add(Plugin *p) {
00136     mutex.lock();
00137 
00138     if(plugins.count(p)) {
00139     mutex.unlock();
00140     return false;
00141     }
00142 
00143     plugins.insert(p);
00144 
00145     mutex.unlock();
00146     return true;
00147 }
00148 
00149 bool PluginManager::remove(Plugin *p) {
00150     mutex.lock();
00151     
00152     if(!plugins.count(p)) {
00153     mutex.unlock();
00154     return false;
00155     }
00156 
00157     plugins.erase(p);
00158     delete p;
00159 
00160     mutex.unlock();
00161     return true;
00162 }
00163 
00164 /***
00165  *** Private functions
00166  ***
00167  **/
00168 
00169 void PluginManager::addPluginsFromPath(PluginSet &pl, string path) {
00170     mutex.lock();
00171     DIR *d = opendir(path.c_str());
00172     
00173     if(!d) {
00174     mutex.unlock();
00175     return;
00176     }
00177 
00178     for(dirent *de = readdir(d); de; de = readdir(d)) {
00179     string name = de->d_name;
00180     if(name.length() < 3)
00181         continue;
00182     
00183     string ext  = name.substr(name.length()-3,3);
00184     if(ext == ".so") {
00185             Plugin *p = new Plugin(path + "/" + name);
00186 
00187             if(!plugins.count(p))
00188                 pl.insert(p);
00189             else
00190                 delete p;
00191         }
00192     }
00193     
00194     closedir(d);
00195     mutex.unlock();
00196 }
00197 
00198 void PluginManager::addMultiple(string &_path) {
00199     string::size_type idx      = _path.find(':');
00200     string            curPath;
00201 
00202     while(idx != string::npos) {
00203     curPath = _path.substr(0, idx);
00204     _path   = _path.substr(idx+1);
00205     idx     = _path.find(':');
00206 
00207     addPath(curPath);
00208     }
00209 
00210     if(!_path.empty())
00211     addPath(_path);
00212 }
00213 
00214 /***
00215  *** Related
00216  **/
00217 
00218 bool Comparator::operator() (const Plugin *p1, const Plugin *p2) const {
00219     return (const_cast<Plugin*>(p1)->getPath() <
00220         const_cast<Plugin*>(p2)->getPath());
00221 }

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000