WModuleMetaInformation.cpp 6.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25
#include <algorithm>
26
#include <string>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
27
#include <vector>
28

29 30 31
#include "../common/WException.h"
#include "../common/WLogger.h"
#include "../common/WStructuredTextParser.h"
32
#include "WModule.h"
33

34 35 36
#include "WModuleMetaInformation.h"

WModuleMetaInformation::WModuleMetaInformation( std::string name ):
37 38
    m_name( name ),
    m_loaded( false )
39 40 41 42
{
    // initialize members
}

43 44
WModuleMetaInformation::WModuleMetaInformation( boost::shared_ptr< WModule > module ):
    m_name( module->getName() ),
45 46 47
    m_description( module->getDescription() ),
    m_loaded( false ),
    m_localPath( module->getLocalPath() )
48
{
49 50 51 52 53 54 55 56
    // check whether file exists
    boost::filesystem::path metafile = module->getLocalPath() / "META";
    if( !boost::filesystem::exists( metafile ) )
    {
        return;
    }

    // try loading it
57 58
    try
    {
59
        m_metaData = WStructuredTextParser::StructuredValueTree( metafile );
60 61 62 63 64 65 66
        // is there a module definition?
        // If there is no meta info for this module, assume we could not load a meta file
        m_loaded = m_metaData.exists( m_name );
        if( !m_loaded )
        {
            wlog::error( "Module (" + m_name + ")" ) << "Meta file loaded but no entry for module \"" << m_name << "\" found. Ignoring.";
        }
67 68 69
    }
    catch( const WException& e )
    {
70
        wlog::error( "Module (" + m_name + ")" ) << "Meta file load failed. Message: " << e.what();
71
    }
72 73 74 75 76 77 78
}

WModuleMetaInformation::~WModuleMetaInformation()
{
    // cleanup
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
std::string WModuleMetaInformation::getName() const
{
    return m_name;
}

boost::filesystem::path WModuleMetaInformation::getIcon() const
{
    // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return boost::filesystem::path();
    }

    // find key-value pair
    return m_localPath / m_metaData.getValue< boost::filesystem::path >( m_name + "/icon", boost::filesystem::path( "icon.png" ) );
}

std::string WModuleMetaInformation::getWebsite() const
{
    // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return "";
    }

    // find key-value pair
    return m_metaData.getValue< std::string >( m_name + "/website", "" );
}

std::string WModuleMetaInformation::getDescription() const
{
    // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return m_description;
    }

    // find key-value pair
    return m_metaData.getValue< std::string >( m_name + "/description", m_description );
}

boost::filesystem::path WModuleMetaInformation::getHelp() const
{
    // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return boost::filesystem::path();
    }

    // find key-value pair
    return m_localPath / m_metaData.getValue< boost::filesystem::path >( m_name + "/help", boost::filesystem::path( "help.html" ) );
}

std::vector< WModuleMetaInformation::Author > WModuleMetaInformation::getAuthors() const
{
    std::vector< WModuleMetaInformation::Author > r;

    // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return r;
    }

    // how much author information is available?
    std::vector< std::string > authors = m_metaData.getValues< std::string >( m_name + "/author" );
    // prepare some memory
    r.resize( authors.size() );

    // for each author, get some associated data if available
    for( std::vector< std::string >::const_iterator i = authors.begin(); i != authors.end(); ++i )
    {
        r[ i - authors.begin() ].m_email = m_metaData.getValue< std::string >( m_name + "/" + *i + "/email", "" );
        r[ i - authors.begin() ].m_what = m_metaData.getValue< std::string >( m_name + "/" + *i + "/what", "" );
        r[ i - authors.begin() ].m_url = m_metaData.getValue< std::string >( m_name + "/" + *i + "/url", "" );
    }

    // did we find some author info? If not, add a nice default OpenWalnut author
    if( !r.size() )
    {
        WModuleMetaInformation::Author ow = { "OpenWalnut Project", "", "http://www.openwalnut.org", "Design, Development and Bug fixing" };
        r.push_back( ow );
    }

    return r;
}

std::vector< WModuleMetaInformation::Online > WModuleMetaInformation::getOnlineResources() const
{
    std::vector< WModuleMetaInformation::Online > r;
    // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return r;
    }

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    // get the "online"-subtrees
    typedef std::vector< WStructuredTextParser::StructuredValueTree > TreeList;
    TreeList onlineInfos = m_metaData.getSubTrees( m_name + "/online" );
    for( TreeList::const_iterator i = onlineInfos.begin(); i != onlineInfos.end(); ++i )
    {
        WModuleMetaInformation::Online o;

        // get all info:

        // these are required
        o.m_name = ( *i ).getValue< std::string >( "name", "" );
        o.m_url = ( *i ).getValue< std::string >( "url", "" );
        if( o.m_name.empty() || o.m_url.empty() )
        {
            continue;
        }

        // optional
        o.m_description = ( *i ).getValue< std::string >( "description", "" );

        // add
        r.push_back( o );
    }

198 199 200
    return r;
}

201 202 203 204 205 206 207 208 209 210 211 212
std::vector< std::string > WModuleMetaInformation::getTags() const
{
   // return a default if not meta data was loaded
    if( !m_loaded )
    {
        return std::vector< std::string >();
    }

    // find key-value pair
    return m_metaData.getValues< std::string >( m_name + "/tag" );
}