11 #include <Wt/WApplication> 12 #include <Wt/WContainerWidget> 13 #include <Wt/WEnvironment> 14 #include <Wt/WLineEdit> 15 #include <Wt/WGridLayout> 16 #include <Wt/WHBoxLayout> 17 #include <Wt/WPushButton> 20 #include <Wt/WTreeView> 21 #include <Wt/WVBoxLayout> 22 #include <Wt/WViewWidget> 24 #include <boost/filesystem/operations.hpp> 25 #include <boost/filesystem/exception.hpp> 26 #include <boost/filesystem/convenience.hpp> 27 #include <boost/algorithm/string.hpp> 33 namespace fs = boost::filesystem;
38 #if BOOST_FILESYSTEM_VERSION < 3 39 return p.empty() ? std::string() : *--p.end();
41 return p.empty() ? std::string() : (*--p.end()).
string();
46 static std::string
stem(
const fs::path& p)
49 std::size_t pos = fn.find(
'.');
50 if (pos == std::string::npos)
53 return fn.substr(0, pos);
61 std::string path = p.string();
63 return path.substr(0, path.length() - fn.length() - 1);
72 const std::string& examplesRoot,
73 const std::string& examplesType)
74 : deployPath_(deployPath),
75 examplesRoot_(examplesRoot),
76 examplesType_(examplesType)
78 wApp->internalPathChanged().connect
86 WApplication *app = wApp;
89 std::string example = app->internalPathNextPart(
deployPath_);
91 if (example.find(
"..") != std::string::npos
92 || example.find(
'/') != std::string::npos
93 || example.find(
'\\') != std::string::npos) {
94 app->setInternalPathValid(
false);
102 const std::string& example)
108 exists = fs::exists(exampleDir);
109 }
catch (std::exception&) {
113 WApplication::instance()->setInternalPathValid(
false);
114 addWidget(
new WText(
"No such example: " + exampleDir));
118 model_ =
new WStandardItemModel(0, 1,
this);
125 WApplication::instance()->setTitle(tr(
"srcview.title." + example));
127 new WText(tr(
"srcview.title." +
examplesType_ +
"." + example));
128 title->setInternalPathEncoding(
true);
149 WStandardItem *w =
model_->item(0);
152 if (w->rowCount() > 0)
160 WVBoxLayout *topLayout =
new WVBoxLayout();
161 topLayout->addWidget(title);
163 WHBoxLayout *gitLayout =
new WHBoxLayout();
166 topLayout->addLayout(gitLayout, 1);
167 gitLayout->setResizable(0);
175 setLayout(topLayout);
176 setStyleClass(
"maindiv");
184 std::string ext = fs::extension(path);
188 else if (ext ==
".C" || ext ==
".cpp")
195 const fs::path& path)
197 static const char *supportedFiles[] = {
198 ".C",
".cpp",
".h",
".css",
".xml",
".png",
".gif",
".csv",
".ico", 0
203 parent->appendRow(dir);
206 std::set<fs::path> paths;
208 fs::directory_iterator end_itr;
209 for (fs::directory_iterator i(path); i != end_itr; ++i)
212 std::vector<FileItem*> classes, files;
213 std::vector<fs::path> dirs;
215 while (!paths.empty()) {
216 fs::path p = *paths.begin();
220 if (fs::is_symlink(p))
224 if (fs::is_regular(p)) {
225 std::string ext = fs::extension(p);
226 bool supported =
false;
227 for (
const char **s = supportedFiles; *s != 0; ++s)
239 if (!companion.empty()) {
240 std::set<fs::path>::iterator it_companion = paths.find(companion);
242 if (it_companion != paths.end()) {
243 std::string className =
stem(p);
244 escapeText(className);
245 std::string label =
"<i>class</i> " + className;
248 new FileItem(
"/icons/cppclass.png", label, std::string());
249 classItem->setFlags(classItem->flags() | ItemIsXHTMLText);
255 (*it_companion).string());
256 classItem->appendRow(header);
257 classItem->appendRow(cpp);
259 classes.push_back(classItem);
260 paths.erase(it_companion);
264 files.push_back(file);
266 }
else if (fs::is_directory(p)) {
271 files.push_back(file);
277 for (
unsigned int i = 0; i < classes.size(); i++)
278 parent->appendRow(classes[i]);
280 for (
unsigned int i = 0; i < files.size(); i++)
281 parent->appendRow(files[i]);
283 for (
unsigned int i = 0; i < dirs.size(); i++)
285 }
catch (fs::filesystem_error& e) {
286 std::cerr << e.what() << std::endl;
291 const fs::path& srcPath,
292 const std::string packageName)
294 fs::directory_iterator end_itr;
297 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
299 if (fs::is_regular(p)) {
301 packageItem =
new FileItem(
"/icons/package.png", packageName,
"");
302 parent->appendRow(packageItem);
307 packageItem->appendRow(file);
311 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
313 if (fs::is_directory(p)) {
314 std::string pn = packageName;
325 const fs::path& path)
329 parent->appendRow(dir);
332 std::vector<fs::path> files, dirs;
334 fs::directory_iterator end_itr;
335 for (fs::directory_iterator i(path); i != end_itr; ++i) {
337 if (fs::is_directory(p)) {
341 parent->appendRow(dir);
353 for (
unsigned int i = 0; i < dirs.size(); i++)
356 for (
unsigned int i = 0; i < files.size(); i++) {
359 parent->appendRow(file);
369 WModelIndex selected = *
exampleView_->selectedIndexes().begin();
static const int FileNameRole
Wt::WStandardItemModel * model_
static std::string stem(const fs::path &p)
WStandardItem which stores a file.
void showFile()
Displayed the currently selected file.
void setExample(const std::string &exampleDir, const std::string &example)
static const int FilePathRole
Wt::WTreeView * exampleView_
std::string examplesRoot_
static const int ContentsRole
ExampleSourceViewer(const std::string &deployPath, const std::string &examplesRoot, const std::string &examplesType)
Constructor.
void javaTraverseDir(Wt::WStandardItem *parent, const boost::filesystem::path &path)
std::string examplesType_
void javaTraversePackages(Wt::WStandardItem *parent, const boost::filesystem::path &srcPath, const std::string packageName)
View class for source code.
void cppTraverseDir(Wt::WStandardItem *parent, const boost::filesystem::path &path)
static std::string filename(const fs::path &p)
bool setIndex(const Wt::WModelIndex &index)
Sets the model index.
static bool comparePaths(const fs::path &p1, const fs::path &p2)
fs::path parent_path(const fs::path &p)
static fs::path getCompanion(const fs::path &path)