OpenWalnut  1.4.0
WModuleMetaInformation.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <algorithm>
00026 #include <string>
00027 #include <vector>
00028 
00029 #include "../common/WException.h"
00030 #include "../common/WLogger.h"
00031 #include "../common/WStructuredTextParser.h"
00032 #include "WModule.h"
00033 
00034 #include "WModuleMetaInformation.h"
00035 
00036 WModuleMetaInformation::WModuleMetaInformation( std::string name ):
00037     m_name( name ),
00038     m_loaded( false )
00039 {
00040     // initialize members
00041 }
00042 
00043 WModuleMetaInformation::WModuleMetaInformation( boost::shared_ptr< WModule > module ):
00044     m_name( module->getName() ),
00045     m_description( module->getDescription() ),
00046     m_loaded( false ),
00047     m_localPath( module->getLocalPath() )
00048 {
00049     // check whether file exists
00050     boost::filesystem::path metafile = module->getLocalPath() / "META";
00051     if( !boost::filesystem::exists( metafile ) )
00052     {
00053         return;
00054     }
00055 
00056     // try loading it
00057     try
00058     {
00059         m_metaData = WStructuredTextParser::StructuredValueTree( metafile );
00060         // is there a module definition?
00061         // If there is no meta info for this module, assume we could not load a meta file
00062         m_loaded = m_metaData.exists( m_name );
00063         if( !m_loaded )
00064         {
00065             wlog::error( "Module (" + m_name + ")" ) << "Meta file loaded but no entry for module \"" << m_name << "\" found. Ignoring.";
00066         }
00067     }
00068     catch( const WException& e )
00069     {
00070         wlog::error( "Module (" + m_name + ")" ) << "Meta file load failed. Message: " << e.what();
00071     }
00072 }
00073 
00074 WModuleMetaInformation::~WModuleMetaInformation()
00075 {
00076     // cleanup
00077 }
00078 
00079 std::string WModuleMetaInformation::getName() const
00080 {
00081     return m_name;
00082 }
00083 
00084 boost::filesystem::path WModuleMetaInformation::getIcon() const
00085 {
00086     // return a default if not meta data was loaded
00087     if( !m_loaded )
00088     {
00089         return boost::filesystem::path();
00090     }
00091 
00092     // find key-value pair
00093     return m_localPath / m_metaData.getValue< boost::filesystem::path >( m_name + "/icon", boost::filesystem::path( "icon.png" ) );
00094 }
00095 
00096 bool WModuleMetaInformation::isIconAvailable() const
00097 {
00098     if( m_loaded )
00099     {
00100         return m_metaData.exists( m_name + "/icon", true );
00101     }
00102     else
00103     {
00104         return false;
00105     }
00106 }
00107 
00108 std::string WModuleMetaInformation::getWebsite() const
00109 {
00110     // return a default if not meta data was loaded
00111     if( !m_loaded )
00112     {
00113         return "";
00114     }
00115 
00116     // find key-value pair
00117     return m_metaData.getValue< std::string >( m_name + "/website", "" );
00118 }
00119 
00120 std::string WModuleMetaInformation::getDescription() const
00121 {
00122     // return a default if not meta data was loaded
00123     if( !m_loaded )
00124     {
00125         return m_description;
00126     }
00127 
00128     // find key-value pair
00129     return m_metaData.getValue< std::string >( m_name + "/description", m_description );
00130 }
00131 
00132 boost::filesystem::path WModuleMetaInformation::getHelp() const
00133 {
00134     // return a default if not meta data was loaded
00135     if( !m_loaded )
00136     {
00137         return boost::filesystem::path();
00138     }
00139 
00140     // find key-value pair
00141     return m_localPath / m_metaData.getValue< boost::filesystem::path >( m_name + "/help", boost::filesystem::path( "help.html" ) );
00142 }
00143 
00144 std::vector< WModuleMetaInformation::Author > WModuleMetaInformation::getAuthors() const
00145 {
00146     std::vector< WModuleMetaInformation::Author > r;
00147 
00148     // did we find some author info? If not, add a nice default OpenWalnut author
00149     WModuleMetaInformation::Author ow = { "OpenWalnut Project", "http://www.openwalnut.org", "", "Design, Development, and Bug Fixing" };
00150 
00151     // return a default if not meta data was loaded
00152     if( !m_loaded )
00153     {
00154         r.push_back( ow );
00155         return r;
00156     }
00157 
00158     // how much author information is available?
00159     std::vector< std::string > authors = m_metaData.getValues< std::string >( m_name + "/author" );
00160 
00161     if( authors.empty() )
00162     {
00163         r.push_back( ow );
00164         return r;
00165     }
00166 
00167     // for each author, get some associated data if available
00168     // prepare some memory
00169     r.resize( authors.size() );
00170     for( std::vector< std::string >::const_iterator i = authors.begin(); i != authors.end(); ++i )
00171     {
00172         r[ i - authors.begin() ].m_name = *i;
00173         r[ i - authors.begin() ].m_email = m_metaData.getValue< std::string >( m_name + "/" + *i + "/email", "" );
00174         r[ i - authors.begin() ].m_what = m_metaData.getValue< std::string >( m_name + "/" + *i + "/what", "" );
00175         r[ i - authors.begin() ].m_url = m_metaData.getValue< std::string >( m_name + "/" + *i + "/url", "" );
00176     }
00177 
00178     return r;
00179 }
00180 
00181 std::vector< WModuleMetaInformation::Online > WModuleMetaInformation::getOnlineResources() const
00182 {
00183     std::vector< WModuleMetaInformation::Online > r;
00184     // return a default if not meta data was loaded
00185     if( !m_loaded )
00186     {
00187         return r;
00188     }
00189 
00190     // get the "online"-subtrees
00191     typedef std::vector< WStructuredTextParser::StructuredValueTree > TreeList;
00192     TreeList onlineInfos = m_metaData.getSubTrees( m_name + "/online" );
00193     for( TreeList::const_iterator i = onlineInfos.begin(); i != onlineInfos.end(); ++i )
00194     {
00195         WModuleMetaInformation::Online o;
00196 
00197         // get all info:
00198 
00199         // these are required
00200         o.m_name = ( *i ).getValue< std::string >( "name", "" );
00201         o.m_url = ( *i ).getValue< std::string >( "url", "" );
00202         if( o.m_name.empty() || o.m_url.empty() )
00203         {
00204             continue;
00205         }
00206 
00207         // optional
00208         o.m_description = ( *i ).getValue< std::string >( "description", "" );
00209 
00210         // add
00211         r.push_back( o );
00212     }
00213 
00214     return r;
00215 }
00216 
00217 std::vector< std::string > WModuleMetaInformation::getTags() const
00218 {
00219     // return a default if not meta data was loaded
00220     if( !m_loaded )
00221     {
00222         return std::vector< std::string >();
00223     }
00224 
00225     // find key-value pair
00226     return m_metaData.getValues< std::string >( m_name + "/tag" );
00227 }
00228 
00229 std::vector< WModuleMetaInformation::Screenshot > WModuleMetaInformation::getScreenshots() const
00230 {
00231     std::vector< WModuleMetaInformation::Screenshot > r;
00232     // return a default if not meta data was loaded
00233     if( !m_loaded )
00234     {
00235         return r;
00236     }
00237 
00238     // get the "screenshot"-subtrees
00239     typedef std::vector< WStructuredTextParser::StructuredValueTree > TreeList;
00240     TreeList screenshotInfos = m_metaData.getSubTrees( m_name + "/screenshot" );
00241     for( TreeList::const_iterator i = screenshotInfos.begin(); i != screenshotInfos.end(); ++i )
00242     {
00243         WModuleMetaInformation::Screenshot s;
00244 
00245         // get all info:
00246 
00247         // these are required
00248         s.m_filename = ( *i ).getValue< boost::filesystem::path >( "filename", "" );
00249         if( s.m_filename.empty() )
00250         {
00251             continue;
00252         }
00253 
00254         // optional
00255         s.m_description = ( *i ).getValue< std::string >( "description", "" );
00256 
00257         // add
00258         r.push_back( s );
00259     }
00260 
00261     return r;
00262 }
00263