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  return false;
78  }
79  //--- register all types and widgets defined in the plugin
80  plugin->registerTypesOnFactory();
81  return true;
82 }
83 
85  foreach( QString pluginfile, dir.entryList() ) {
86  if ( ! QLibrary::isLibrary( pluginfile ) ) continue;
87  if ( loadPlugin( dir.absoluteFilePath(pluginfile) ) ) {
88  Logger::info( "Loaded Plugin \"" + pluginfile + "\"");
89  }
90  }
91 }
92 
94  QStringList pluginPaths = params.getParametersWithPrefixList( "TOTAL99", "pluginPath" );
95  foreach( QString path, pluginPaths ) {
96  QString value = params.getValue( "TOTAL99/"+path );
97  if ( ! QFileInfo( value ).isDir() ) {
98  Logger::warning( "Ignoring un-existing plugin path \"" + value + "\"" );
99  continue;
100  }
101  loadPlugins( QDir( value ) );
102  }
103 }
104 
106 #ifdef FARSA_WIN
107  confBasePath = qApp->applicationDirPath() + "/../conf";
108  pluginBasePath = qApp->applicationDirPath() + "/../plugins";
109 #else
110  confBasePath = qApp->applicationDirPath() + "/../share/FARSA/conf";
111  pluginBasePath = qApp->applicationDirPath() + "/../share/FARSA/plugins";
112 #endif
113 #ifdef FARSA_LINUX
114  confUserPath = QString(getenv("HOME")) + "/.FARSA/total99";
115  pluginUserPath = QString(getenv("HOME")) + "/.FARSA/total99/plugins";
116 #endif
117 #ifdef FARSA_MAC
118  confUserPath = QString(getenv("HOME")) + "/Library/Application Support/FARSA/Total99";
119  pluginUserPath = QString(getenv("HOME")) + "/Library/Application Support/FARSA/Total99/plugins";
120 #endif
121 #ifdef FARSA_WIN
122  confUserPath = QString(getenv("APPDATA")) + "/FARSA/Total99";
123  pluginUserPath = QString(getenv("APPDATA")) + "/FARSA/Total99/plugins";
124 #endif
125 
126  QDir dir;
127  dir.mkpath( confUserPath );
128  dir.mkpath( pluginUserPath );
129 
130  // Loading plugins from default directories
131  loadPlugins( QDir( pluginBasePath ) );
132  loadPlugins( QDir( pluginUserPath ) );
133 
134  uiTemplate = "kids";
135  Logger::info( "Total99 Resources Initialized" );
136 }
137 
138 // All the suff below is to restore the warning state on Windows
139 #if defined(_MSC_VER)
140  #pragma warning(pop)
141 #endif
142