~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/core/kernel/WModuleMetaInformation.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <algorithm>
 
26
#include <string>
 
27
#include <vector>
 
28
 
 
29
#include "../common/WException.h"
 
30
#include "../common/WLogger.h"
 
31
#include "../common/WStructuredTextParser.h"
 
32
#include "WModule.h"
 
33
 
 
34
#include "WModuleMetaInformation.h"
 
35
 
 
36
WModuleMetaInformation::WModuleMetaInformation( std::string name ):
 
37
    m_name( name ),
 
38
    m_loaded( false )
 
39
{
 
40
    // initialize members
 
41
}
 
42
 
 
43
WModuleMetaInformation::WModuleMetaInformation( boost::shared_ptr< WModule > module ):
 
44
    m_name( module->getName() ),
 
45
    m_description( module->getDescription() ),
 
46
    m_loaded( false ),
 
47
    m_localPath( module->getLocalPath() )
 
48
{
 
49
    // check whether file exists
 
50
    boost::filesystem::path metafile = module->getLocalPath() / "META";
 
51
    if( !boost::filesystem::exists( metafile ) )
 
52
    {
 
53
        return;
 
54
    }
 
55
 
 
56
    // try loading it
 
57
    try
 
58
    {
 
59
        m_metaData = WStructuredTextParser::StructuredValueTree( metafile );
 
60
        // is there a module definition?
 
61
        // If there is no meta info for this module, assume we could not load a meta file
 
62
        m_loaded = m_metaData.exists( m_name );
 
63
        if( !m_loaded )
 
64
        {
 
65
            wlog::error( "Module (" + m_name + ")" ) << "Meta file loaded but no entry for module \"" << m_name << "\" found. Ignoring.";
 
66
        }
 
67
    }
 
68
    catch( const WException& e )
 
69
    {
 
70
        wlog::error( "Module (" + m_name + ")" ) << "Meta file load failed. Message: " << e.what();
 
71
    }
 
72
}
 
73
 
 
74
WModuleMetaInformation::~WModuleMetaInformation()
 
75
{
 
76
    // cleanup
 
77
}
 
78
 
 
79
std::string WModuleMetaInformation::getName() const
 
80
{
 
81
    return m_name;
 
82
}
 
83
 
 
84
boost::filesystem::path WModuleMetaInformation::getIcon() const
 
85
{
 
86
    // return a default if not meta data was loaded
 
87
    if( !m_loaded )
 
88
    {
 
89
        return boost::filesystem::path();
 
90
    }
 
91
 
 
92
    // find key-value pair
 
93
    return m_localPath / m_metaData.getValue< boost::filesystem::path >( m_name + "/icon", boost::filesystem::path( "icon.png" ) );
 
94
}
 
95
 
 
96
bool WModuleMetaInformation::isIconAvailable() const
 
97
{
 
98
    if( m_loaded )
 
99
    {
 
100
        return m_metaData.exists( m_name + "/icon", true );
 
101
    }
 
102
    else
 
103
    {
 
104
        return false;
 
105
    }
 
106
}
 
107
 
 
108
std::string WModuleMetaInformation::getWebsite() const
 
109
{
 
110
    // return a default if not meta data was loaded
 
111
    if( !m_loaded )
 
112
    {
 
113
        return "";
 
114
    }
 
115
 
 
116
    // find key-value pair
 
117
    return m_metaData.getValue< std::string >( m_name + "/website", "" );
 
118
}
 
119
 
 
120
std::string WModuleMetaInformation::getDescription() const
 
121
{
 
122
    // return a default if not meta data was loaded
 
123
    if( !m_loaded )
 
124
    {
 
125
        return m_description;
 
126
    }
 
127
 
 
128
    // find key-value pair
 
129
    return m_metaData.getValue< std::string >( m_name + "/description", m_description );
 
130
}
 
131
 
 
132
boost::filesystem::path WModuleMetaInformation::getHelp() const
 
133
{
 
134
    // return a default if not meta data was loaded
 
135
    if( !m_loaded )
 
136
    {
 
137
        return boost::filesystem::path();
 
138
    }
 
139
 
 
140
    // find key-value pair
 
141
    return m_localPath / m_metaData.getValue< boost::filesystem::path >( m_name + "/help", boost::filesystem::path( "help.html" ) );
 
142
}
 
143
 
 
144
std::vector< WModuleMetaInformation::Author > WModuleMetaInformation::getAuthors() const
 
145
{
 
146
    std::vector< WModuleMetaInformation::Author > r;
 
147
 
 
148
    // did we find some author info? If not, add a nice default OpenWalnut author
 
149
    WModuleMetaInformation::Author ow = { "OpenWalnut Project", "http://www.openwalnut.org", "", "Design, Development, and Bug Fixing" };
 
150
 
 
151
    // return a default if not meta data was loaded
 
152
    if( !m_loaded )
 
153
    {
 
154
        r.push_back( ow );
 
155
        return r;
 
156
    }
 
157
 
 
158
    // how much author information is available?
 
159
    std::vector< std::string > authors = m_metaData.getValues< std::string >( m_name + "/author" );
 
160
 
 
161
    if( authors.empty() )
 
162
    {
 
163
        r.push_back( ow );
 
164
        return r;
 
165
    }
 
166
 
 
167
    // for each author, get some associated data if available
 
168
    // prepare some memory
 
169
    r.resize( authors.size() );
 
170
    for( std::vector< std::string >::const_iterator i = authors.begin(); i != authors.end(); ++i )
 
171
    {
 
172
        r[ i - authors.begin() ].m_name = *i;
 
173
        r[ i - authors.begin() ].m_email = m_metaData.getValue< std::string >( m_name + "/" + *i + "/email", "" );
 
174
        r[ i - authors.begin() ].m_what = m_metaData.getValue< std::string >( m_name + "/" + *i + "/what", "" );
 
175
        r[ i - authors.begin() ].m_url = m_metaData.getValue< std::string >( m_name + "/" + *i + "/url", "" );
 
176
    }
 
177
 
 
178
    return r;
 
179
}
 
180
 
 
181
std::vector< WModuleMetaInformation::Online > WModuleMetaInformation::getOnlineResources() const
 
182
{
 
183
    std::vector< WModuleMetaInformation::Online > r;
 
184
    // return a default if not meta data was loaded
 
185
    if( !m_loaded )
 
186
    {
 
187
        return r;
 
188
    }
 
189
 
 
190
    // get the "online"-subtrees
 
191
    typedef std::vector< WStructuredTextParser::StructuredValueTree > TreeList;
 
192
    TreeList onlineInfos = m_metaData.getSubTrees( m_name + "/online" );
 
193
    for( TreeList::const_iterator i = onlineInfos.begin(); i != onlineInfos.end(); ++i )
 
194
    {
 
195
        WModuleMetaInformation::Online o;
 
196
 
 
197
        // get all info:
 
198
 
 
199
        // these are required
 
200
        o.m_name = ( *i ).getValue< std::string >( "name", "" );
 
201
        o.m_url = ( *i ).getValue< std::string >( "url", "" );
 
202
        if( o.m_name.empty() || o.m_url.empty() )
 
203
        {
 
204
            continue;
 
205
        }
 
206
 
 
207
        // optional
 
208
        o.m_description = ( *i ).getValue< std::string >( "description", "" );
 
209
 
 
210
        // add
 
211
        r.push_back( o );
 
212
    }
 
213
 
 
214
    return r;
 
215
}
 
216
 
 
217
std::vector< std::string > WModuleMetaInformation::getTags() const
 
218
{
 
219
    // return a default if not meta data was loaded
 
220
    if( !m_loaded )
 
221
    {
 
222
        return std::vector< std::string >();
 
223
    }
 
224
 
 
225
    // find key-value pair
 
226
    return m_metaData.getValues< std::string >( m_name + "/tag" );
 
227
}
 
228
 
 
229
std::vector< WModuleMetaInformation::Screenshot > WModuleMetaInformation::getScreenshots() const
 
230
{
 
231
    std::vector< WModuleMetaInformation::Screenshot > r;
 
232
    // return a default if not meta data was loaded
 
233
    if( !m_loaded )
 
234
    {
 
235
        return r;
 
236
    }
 
237
 
 
238
    // get the "screenshot"-subtrees
 
239
    typedef std::vector< WStructuredTextParser::StructuredValueTree > TreeList;
 
240
    TreeList screenshotInfos = m_metaData.getSubTrees( m_name + "/screenshot" );
 
241
    for( TreeList::const_iterator i = screenshotInfos.begin(); i != screenshotInfos.end(); ++i )
 
242
    {
 
243
        WModuleMetaInformation::Screenshot s;
 
244
 
 
245
        // get all info:
 
246
 
 
247
        // these are required
 
248
        s.m_filename = ( *i ).getValue< boost::filesystem::path >( "filename", "" );
 
249
        if( s.m_filename.empty() )
 
250
        {
 
251
            continue;
 
252
        }
 
253
 
 
254
        // optional
 
255
        s.m_description = ( *i ).getValue< std::string >( "description", "" );
 
256
 
 
257
        // add
 
258
        r.push_back( s );
 
259
    }
 
260
 
 
261
    return r;
 
262
}
 
263