OpenWalnut  1.5.0dev
WModuleMetaInformation.cpp
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 <memory>
27 #include <string>
28 #include <vector>
29 
30 #include "../common/WException.h"
31 #include "../common/WLogger.h"
32 #include "../common/WStructuredTextParser.h"
33 #include "WModule.h"
34 #include "WModuleMetaInformation.h"
35 
37  m_name( name ),
38  m_loaded( false )
39 {
40  // initialize members
41 }
42 
43 WModuleMetaInformation::WModuleMetaInformation( std::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 
75 {
76  // cleanup
77 }
78 
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 
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 
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 
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  {
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  {
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 
264 bool WModuleMetaInformation::valueExists( std::string path ) const
265 {
266  if( !m_loaded )
267  {
268  return false;
269  }
270  return m_metaData.exists( path, true );
271 }
272 
Basic exception handler.
Definition: WException.h:39
virtual const char * what() const
Returns the message string set on throw.
Definition: WException.cpp:90
std::string getDescription() const
A module description.
std::vector< std::string > getTags() const
A list of tags provided for the module.
virtual ~WModuleMetaInformation()
Destructor.
bool isIconAvailable() const
Check whether the meta info contained an icon.
std::vector< Author > getAuthors() const
A list of authors.
WStructuredTextParser::StructuredValueTree m_metaData
The tree representing the data.
std::string m_description
The default description if none was specified in the META file.
bool valueExists(std::string path) const
Check whether the value specified by "path" exists.
boost::filesystem::path getHelp() const
Path to a text or HTML file containing some module help.
boost::filesystem::path m_localPath
The module local path.
std::string m_name
The name of the module providing this meta information.
std::vector< Online > getOnlineResources() const
A list of online resources.
WModuleMetaInformation(std::string name)
Constructor.
std::string getName() const
The name of the module.
bool m_loaded
If true, m_metaData should be queried in all getters.
std::string getWebsite() const
The URL to a module website.
std::vector< Screenshot > getScreenshots() const
Returns the list of screenshots.
boost::filesystem::path getIcon() const
Get the icon path.
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:298
Structure to contain all supported author information.
Structure to encapsulate the META info online resources.
std::string m_description
Online resource's description.
std::string m_name
Online resource's name.
std::string m_url
The url to the resource.
Structure to encapsulate a screenshot info.
boost::filesystem::path m_filename
The screenshot filename.
std::string m_description
The description text shown for the screenshot.