experiments/src/total99resources.cpp

00001 /********************************************************************************
00002  *  FARSA - Total99                                                             *
00003  *  Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "total99resources.h"
00021 #include <cstdlib>
00022 #include <QApplication>
00023 #include <QFile>
00024 #include <QDir>
00025 #include <QFileInfo>
00026 #include <QPluginLoader>
00027 #include <QStringList>
00028 #include "configurationparameters.h"
00029 #include "factory.h"
00030 #include "farsaplugin.h"
00031 #include "icubsimulator.h"
00032 #include "icubsimulatorviewer.h"
00033 #include "evorobotviewer.h"
00034 #include "logger.h"
00035 
00036 // All the suff below is to avoid warnings on Windows about the use of the
00037 // unsafe function getenv
00038 #if defined(_MSC_VER)
00039     #pragma warning(push)
00040     #pragma warning(disable:4996)
00041 #endif
00042 
00043 using namespace farsa;
00044 
00045 QString Total99Resources::findResource( QString resourceName ) {
00046     //--- search in the user directory
00047     if ( QFile::exists( confUserPath + "/" + resourceName ) ) {
00048         return ( confUserPath + "/" + resourceName );
00049     }
00050     //--- search in the template user directory
00051     if ( QFile::exists( confUserPath + "/templates/" + uiTemplate + "/" + resourceName ) ) {
00052         return ( confUserPath + "/templates/" + uiTemplate + "/" + resourceName );
00053     }
00054     //--- search in the global directory
00055     if ( QFile::exists( confBasePath + "/" + resourceName ) ) {
00056         return ( confBasePath + "/" + resourceName );
00057     }
00058     //--- search in the global template directory
00059     if ( QFile::exists( confBasePath + "/templates/" + uiTemplate + "/" + resourceName ) ) {
00060         return ( confBasePath + "/templates/" + uiTemplate + "/" + resourceName );
00061     }
00062     //--- not find anything
00063     return QString();
00064 }
00065 
00066 QString Total99Resources::confBasePath;
00067 QString Total99Resources::confUserPath;
00068 QString Total99Resources::pluginBasePath;
00069 QString Total99Resources::pluginUserPath;
00070 QString Total99Resources::uiTemplate;
00071 
00072 bool Total99Resources::loadPlugin( QString filename ) {
00073     QPluginLoader loader( filename );
00074     FarsaPlugin* plugin = qobject_cast<FarsaPlugin*>( loader.instance() );
00075     if ( !plugin ) {
00076         // error on loading the plugin
00077         return false;
00078     }
00079     //--- register all types and widgets defined in the plugin
00080     plugin->registerTypesOnFactory();
00081     return true;
00082 }
00083 
00084 void Total99Resources::loadPlugins( QDir dir ) {
00085     foreach( QString pluginfile, dir.entryList() ) {
00086         if ( ! QLibrary::isLibrary( pluginfile ) ) continue;
00087         if ( loadPlugin( dir.absoluteFilePath(pluginfile) ) ) {
00088             Logger::info( "Loaded Plugin \"" + pluginfile + "\"");
00089         }
00090     }
00091 }
00092 
00093 void Total99Resources::loadPlugins( const farsa::ConfigurationParameters& params ) {
00094     QStringList pluginPaths = params.getParametersWithPrefixList( "TOTAL99", "pluginPath" );
00095     foreach( QString path, pluginPaths ) {
00096         QString value = params.getValue( "TOTAL99/"+path );
00097         if ( ! QFileInfo( value ).isDir() ) {
00098             Logger::warning( "Ignoring un-existing plugin path \"" + value + "\"" );
00099             continue;
00100         }
00101         loadPlugins( QDir( value ) );
00102     }
00103 }
00104 
00105 void Total99Resources::initialize() {
00106 #ifdef FARSA_WIN
00107     confBasePath = qApp->applicationDirPath() + "/../conf";
00108     pluginBasePath = qApp->applicationDirPath() + "/../plugins";
00109 #else
00110     confBasePath = qApp->applicationDirPath() + "/../share/FARSA/conf";
00111     pluginBasePath = qApp->applicationDirPath() + "/../share/FARSA/plugins";
00112 #endif
00113 #ifdef FARSA_LINUX
00114     confUserPath = QString(getenv("HOME")) + "/.FARSA/total99";
00115     pluginUserPath = QString(getenv("HOME")) + "/.FARSA/total99/plugins";
00116 #endif
00117 #ifdef FARSA_MAC
00118     confUserPath = QString(getenv("HOME")) + "/Library/Application Support/FARSA/Total99";
00119     pluginUserPath = QString(getenv("HOME")) + "/Library/Application Support/FARSA/Total99/plugins";
00120 #endif
00121 #ifdef FARSA_WIN
00122     confUserPath = QString(getenv("APPDATA")) + "/FARSA/Total99";
00123     pluginUserPath = QString(getenv("APPDATA")) + "/FARSA/Total99/plugins";
00124 #endif
00125 
00126     QDir dir;
00127     dir.mkpath( confUserPath );
00128     dir.mkpath( pluginUserPath );
00129 
00130     // Loading plugins from default directories
00131     loadPlugins( QDir( pluginBasePath ) );
00132     loadPlugins( QDir( pluginUserPath ) );
00133 
00134     uiTemplate = "kids";
00135     Logger::info( "Total99 Resources Initialized" );
00136 }
00137 
00138 // All the suff below is to restore the warning state on Windows
00139 #if defined(_MSC_VER)
00140     #pragma warning(pop)
00141 #endif
00142