package_interpreter.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 
4 #include <gtkmm.h>
5 
6 #include <boost/filesystem.hpp>
7 #include <boost/property_tree/xml_parser.hpp>
8 #include <boost/property_tree/ptree.hpp>
9 #include <boost/foreach.hpp>
10 
11 #ifndef ROOT_PATH
12 #define ROOT_PATH @ROOT_PATH@
13 #endif
14 
15 using namespace std;
16 using namespace boost;
17 using namespace boost::property_tree;
18 
19 class InterpreterGUI: public Gtk::Window
20 {
21  public:
23  {
24  //Set the title of the window and size
25  set_title("Package interpreter");
26  set_default_size(1100,800);
27 
28  //Get all paths from a root path must be the src/ folder of a catkin workspace
29  getPaths(ROOT_PATH);
30  getPackages();
31 
32  //Create the Tree model
33  ref_tree_model = Gtk::ListStore::create(columns);
34 
35  //Set the tree model to the tree view
36  tree_view.set_model(ref_tree_model);
37 
38  //Add rows to the tree view
39  Gtk::TreeModel::Row row;
40  for(uint i=0;i<packages.size();i++)
41  {
42  row = *(ref_tree_model->append());
43 
44  row[columns.col_package] = packages[i].name;
45  row[columns.col_author] = packages[i].author;
46 
47  string maintainer_plus_email = packages[i].maintainer + " (" + packages[i].maintainer_email + ")";
48  row[columns.col_maintainer] = maintainer_plus_email;
49  row[columns.col_licence] = packages[i].license;
50  }
51 
52  //Append columns to the tree view
53  tree_view.append_column("Package", columns.col_package);
54  tree_view.append_column("Author", columns.col_author);
55  tree_view.append_column("Maintainer", columns.col_maintainer);
56  tree_view.append_column("License", columns.col_licence);
57 
58  //Set tree view properties
59 // tree_view.set_headers_clickable();
60  tree_view.set_reorderable();
61  tree_view.set_enable_search();
62 
63  //Add the scrolled window to the window and the tree view to the scrolled window
64  add(scrolled_window);
65  scrolled_window.add(tree_view);
66 
67  //Show all
68  show_all_children();
69  }
70 
71  virtual ~InterpreterGUI()
72  {
73  }
74 
75  protected:
76 
77  //The PackageXML structure, holds package info
78  struct PackageXML
79  {
80  boost::filesystem::path path;
81  string name;
82  string author;
83  string maintainer;
85  string license;
86  };
87 
88  //Tree model columns, define the columns of the tree view
89  class ModelColumns : public Gtk::TreeModel::ColumnRecord
90  {
91  public:
92 
94  {
95  add(col_package);
96  add(col_author);
97  add(col_maintainer);
98  add(col_licence);
99  }
100 
101  Gtk::TreeModelColumn<Glib::ustring> col_package;
102  Gtk::TreeModelColumn<Glib::ustring> col_author;
103  Gtk::TreeModelColumn<Glib::ustring> col_maintainer;
104  Gtk::TreeModelColumn<Glib::ustring> col_licence;
105  };
106 
107  //Get all PackageXML structures
108  void getPackages()
109  {
110  for(uint i=0;i<paths.size();i++)
111  interpreterFile(paths[i]);
112  }
113 
114  const ptree& empty_ptree()
115  {
116  static ptree t;
117  return t;
118  }
119 
120  //Read a package.xml file and push the obtained PackageXML structure to the vector
121  void interpreterFile(boost::filesystem::path path)
122  {
123  std::filebuf fb;
124  if(!fb.open(path.string().c_str(),std::ios::in))
125  {
126  cout<<"Cannot open file: "<<path.string()<<endl;
127  return;
128  }
129 
130  std::istream is(&fb);
131 
132  // populate tree structure pt
133  using boost::property_tree::ptree;
134  ptree pt;
135  read_xml(is, pt);
136 
137  PackageXML package;
138  package.path = path;
139 
140  BOOST_FOREACH(ptree::value_type const& v, pt.get_child("package") )
141  {
142  if( v.first == "name" )
143  package.name = v.second.get_value<string>();
144 
145  if( v.first == "maintainer" )
146  {
147  package.maintainer = v.second.get_value<string>();
148 
149  const ptree & attributes = v.second.get_child("<xmlattr>", empty_ptree());
150  BOOST_FOREACH(const ptree::value_type &va, attributes)
151  if(va.first == "email")
152  package.maintainer_email = va.second.get_value<string>();
153  }
154 
155  if( v.first == "author" )
156  package.author = v.second.get_value<string>();
157 
158  if( v.first == "license" )
159  package.license = v.second.get_value<string>();
160 
161  if(package.name.empty())
162  package.name = "NO NAME";
163 
164  if(package.maintainer.empty())
165  package.maintainer = "NO MAINTAINER";
166 
167  if(package.author.empty())
168  package.author = "NO AUTHOR";
169 
170  if(package.license.empty())
171  package.license = "NO LICENSE";
172  }
173 
174  packages.push_back(package);
175 
176  fb.close();
177  }
178 
179  void getPaths(boost::filesystem::path top_path)
180  {
181  //Get all packages.xml paths, with boost recursive iterator
182  boost::filesystem::recursive_directory_iterator dir(top_path), end;
183  while (dir != end)
184  {
185  if (dir->path().filename() == "package.xml")
186  paths.push_back(dir->path());
187 
188  ++dir;
189  }
190  }
191 
192  vector<boost::filesystem::path> paths;
193  vector<PackageXML> packages;
194 
196  Gtk::TreeView tree_view;
197  Glib::RefPtr<Gtk::ListStore> ref_tree_model;
198  Gtk::ScrolledWindow scrolled_window;
199 };
200 
201 int main(int argc,char**argv)
202 {
203  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
204 
205  InterpreterGUI gui;
206 
207  return app->run(gui);
208 }
209 
int main(int argc, char **argv)
Gtk::TreeModelColumn< Glib::ustring > col_author
Gtk::TreeModelColumn< Glib::ustring > col_package
const ptree & empty_ptree()
void interpreterFile(boost::filesystem::path path)
#define ROOT_PATH
Gtk::TreeView tree_view
Gtk::TreeModelColumn< Glib::ustring > col_licence
boost::filesystem::path path
void getPaths(boost::filesystem::path top_path)
Glib::RefPtr< Gtk::ListStore > ref_tree_model
vector< PackageXML > packages
Gtk::ScrolledWindow scrolled_window
vector< boost::filesystem::path > paths
Gtk::TreeModelColumn< Glib::ustring > col_maintainer


toolkit_diagnostics
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:53