total99resources.cpp
1 /********************************************************************************
2  * FARSA - Total99 *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #include "total99resources.h"
21 #include <cstdlib>
22 #include <QApplication>
23 #include <QFile>
24 #include <QDir>
25 #include <QFileInfo>
26 #include <QPluginLoader>
27 #include <QStringList>
28 #include "configurationparameters.h"
29 #include "factory.h"
30 #include "farsaplugin.h"
31 #include "icubsimulator.h"
32 #include "icubsimulatorviewer.h"
33 #include "evorobotviewer.h"
34 #include "logger.h"
35 
36 // All the suff below is to avoid warnings on Windows about the use of the
37 // unsafe function getenv
38 #if defined(_MSC_VER)
39  #pragma warning(push)
40  #pragma warning(disable:4996)
41 #endif
42 
43 using namespace farsa;
44 
45 QString Total99Resources::findResource( QString resourceName ) {
46  //--- search in the user directory
47  if ( QFile::exists( confUserPath + "/" + resourceName ) ) {
48  return ( confUserPath + "/" + resourceName );
49  }
50  //--- search in the template user directory
51  if ( QFile::exists( confUserPath + "/templates/" + uiTemplate + "/" + resourceName ) ) {
52  return ( confUserPath + "/templates/" + uiTemplate + "/" + resourceName );
53  }
54  //--- search in the global directory
55  if ( QFile::exists( confBasePath + "/" + resourceName ) ) {
56  return ( confBasePath + "/" + resourceName );
57  }
58  //--- search in the global template directory
59  if ( QFile::exists( confBasePath + "/templates/" + uiTemplate + "/" + resourceName ) ) {
60  return ( confBasePath + "/templates/" + uiTemplate + "/" + resourceName );
61  }
62  //--- not find anything
63  return QString();
64 }
65 
71 
72 bool Total99Resources::loadPlugin( QString filename ) {
73  QPluginLoader loader( filename );
74  FarsaPlugin* plugin = qobject_cast<FarsaPlugin*>( loader.instance() );
75  if ( !plugin ) {
76  // error on loading the plugin
77  Logger::error( "ERROR LOADING PLUGIN: "+loader.errorString() );
78  return false;
79  }
80  //--- register all types and widgets defined in the plugin
81  plugin->registerTypesOnFactory();
82  return true;
83 }
84 
86  foreach( QString pluginfile, dir.entryList() ) {
87  if ( ! QLibrary::isLibrary( pluginfile ) ) continue;
88  if ( loadPlugin( dir.absoluteFilePath(pluginfile) ) ) {
89  Logger::info( "Loaded Plugin \"" + pluginfile + "\"");
90  }
91  }
92 }
93 
95  QStringList pluginPaths = params.getParametersWithPrefixList( "TOTAL99", "pluginPath" );
96  foreach( QString path, pluginPaths ) {
97  QString value = params.getValue( "TOTAL99/"+path );
98  if ( ! QFileInfo( value ).isDir() ) {
99  Logger::warning( "Ignoring un-existing plugin path \"" + value + "\"" );
100  continue;
101  }
102  loadPlugins( QDir( value ) );
103  }
104 }
105 
107 #ifdef FARSA_WIN
108  confBasePath = qApp->applicationDirPath() + "/../conf";
109  pluginBasePath = qApp->applicationDirPath() + "/../plugins";
110 #else
111  confBasePath = qApp->applicationDirPath() + "/../share/FARSA/conf";
112  pluginBasePath = qApp->applicationDirPath() + "/../share/FARSA/plugins";
113 #endif
114 #ifdef FARSA_LINUX
115  confUserPath = QString(getenv("HOME")) + "/.FARSA/total99";
116  pluginUserPath = QString(getenv("HOME")) + "/.FARSA/total99/plugins";
117 #endif
118 #ifdef FARSA_MAC
119  confUserPath = QString(getenv("HOME")) + "/Library/Application Support/FARSA/Total99";
120  pluginUserPath = QString(getenv("HOME")) + "/Library/Application Support/FARSA/Total99/plugins";
121 #endif
122 #ifdef FARSA_WIN
123  confUserPath = QString(getenv("APPDATA")) + "/FARSA/Total99";
124  pluginUserPath = QString(getenv("APPDATA")) + "/FARSA/Total99/plugins";
125 #endif
126 
127  QDir dir;
128  dir.mkpath( confUserPath );
129  dir.mkpath( pluginUserPath );
130 
131  // Loading plugins from default directories
132  loadPlugins( QDir( pluginBasePath ) );
133  loadPlugins( QDir( pluginUserPath ) );
134 
135  uiTemplate = "kids";
136  Logger::info( "Total99 Resources Initialized" );
137 }
138 
139 // All the suff below is to restore the warning state on Windows
140 #if defined(_MSC_VER)
141  #pragma warning(pop)
142 #endif
143