00001 #include "plugin.h" 00002 #include <qmessagebox.h> 00003 00004 bool Plugin::fromXML( QDomElement &elem ) { 00005 if ( elem.isNull() ) 00006 return false; 00007 00008 if ( std::string( "Plugin" ).compare( (const char*)elem.tagName() ) ) 00009 return false; 00010 00011 mFName = readTextTag( elem, "FName" ); 00012 mPlugName = readTextTag( elem, "PlugName" ); 00013 mDesc = readTextTag( elem, "Desc" ); 00014 00015 return true; 00016 } 00017 00018 bool Plugin::toXML( QDomDocument &doc, 00019 QDomElement &elem, 00020 std::string id ) { 00021 QDomElement base = doc.createElement( "Plugin" ); 00022 if ( id != "" ) 00023 base.setAttribute( "id", (char*)id.c_str() ); 00024 elem.appendChild( base ); 00025 00026 writeTextTag( doc, base, "FName", mFName.c_str() ); 00027 writeTextTag( doc, base, "PlugName", mPlugName.c_str() ); 00028 writeTextTag( doc, base, "Desc", mDesc ); 00029 00030 return true; 00031 } 00032 00033 void Plugin::execPlugin( GLIMSProject *proj ) { 00034 LIBHANDLE hPlug; 00035 EXECFUNC pFunc; 00036 00037 hPlug = loadPlugin(); 00038 00039 if ( !hPlug ) 00040 return; 00041 00042 // GET NAME AND CHECK FOR REQUIRED SYMBOLS 00043 00044 #ifdef WIN32 00045 pFunc = (EXECFUNC) GetProcAddress( (HINSTANCE)hPlug, 00046 "activate" ); 00047 #else 00048 pFunc = (EXECFUNC)dlsym( hPlug, "activate" ); 00049 00050 #endif 00051 00052 if ( pFunc == NULL ) { 00053 QMessageBox::information( NULL, "Plugin Error", "Failed to execute plugin" ); 00054 return; 00055 } 00056 00057 (pFunc)( proj ); 00058 00059 unloadPlugin( hPlug ); 00060 } 00061 00062 bool Plugin::loadFromFile( std::string fname ) { 00063 LIBHANDLE hPlug; // PLUGIN HANDLE 00064 EXECFUNC pExec; 00065 GETNAMEFUNC pGetName; 00066 GETDESCFUNC pGetDesc; 00067 char sep = QDir::separator(); 00068 char fnamebuf[256]; 00069 00070 if ( fname == "" ) 00071 return false; 00072 00073 mFName = fname; 00074 hPlug = loadPlugin(); 00075 if ( !hPlug ) 00076 return false; 00077 00078 // GET NAME AND CHECK FOR REQUIRED SYMBOLS 00079 00080 #ifdef WIN32 00081 pGetName = (GETNAMEFUNC)GetProcAddress( hPlug, "getName" ); 00082 pGetDesc = (GETDESCFUNC)GetProcAddress( hPlug, "getDesc" ); 00083 pExec = (EXECFUNC)GetProcAddress( hPlug, "activate" ); 00084 00085 #else 00086 pGetName = (GETNAMEFUNC)dlsym( hPlug, "getName" ); 00087 pGetDesc = (GETDESCFUNC)dlsym( hPlug, "getDesc" ); 00088 pExec = (EXECFUNC)dlsym( hPlug, "activate" ); 00089 00090 #endif 00091 00092 00093 if ( pGetName == NULL ) 00094 return false; 00095 mPlugName = (pGetName)(); 00096 00097 if ( pGetDesc == NULL ) 00098 return false; 00099 mDesc = (pGetDesc)(); 00100 00101 if ( pExec == NULL ) 00102 return false; 00103 00104 unloadPlugin( hPlug ); 00105 00106 // COPY THE FILE 00107 int pos = mFName.find_last_of( "\\" ); 00108 if ( pos == -1 ) 00109 pos = mFName.find_last_of( "/" ); 00110 pos++; 00111 00112 mFName = mFName.substr( pos ); 00113 sprintf( fnamebuf, ".%cplugins%c%s", sep, sep, mFName.c_str() ); 00114 mFName = fnamebuf; 00115 00116 std::ifstream infile( fname.c_str(), std::ios::binary ); 00117 if ( !infile ) 00118 return false; 00119 00120 std::ofstream outfile( mFName.c_str(), std::ios::binary ); 00121 if ( !outfile ) { 00122 infile.close(); 00123 return false; 00124 } 00125 00126 outfile << infile.rdbuf(); 00127 outfile.close(); 00128 infile.close(); 00129 00130 return true; 00131 } 00132 00133 LIBHANDLE Plugin::loadPlugin( ) { 00134 LIBHANDLE hPlug; 00135 00136 #ifdef WIN32 00137 hPlug = LoadLibraryA( mFName.c_str() ); 00138 if ( !hPlug ) 00139 return NULL; 00140 00141 #else 00142 hPlug = dlopen( mFName.c_str(), RTLD_NOW ); 00143 if ( !hPlug ) { 00144 std::cout << dlerror() << std::endl; 00145 return NULL; 00146 } 00147 00148 #endif 00149 00150 return hPlug; 00151 } 00152 00153 void Plugin::unloadPlugin( LIBHANDLE hPlug ) { 00154 #ifdef WIN32 00155 FreeLibrary( hPlug ); 00156 #else 00157 if ( hPlug ) dlclose( hPlug ); 00158 #endif 00159 } 00160 00161
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |