10 #include <Wt/WApplication.h>
11 #include <Wt/WContainerWidget.h>
12 #include <Wt/WEnvironment.h>
13 #include <Wt/WLineEdit.h>
14 #include <Wt/WGridLayout.h>
15 #include <Wt/WHBoxLayout.h>
16 #include <Wt/WPushButton.h>
17 #include <Wt/WTable.h>
19 #include <Wt/WTreeView.h>
20 #include <Wt/WVBoxLayout.h>
21 #include <Wt/WViewWidget.h>
23 #include <boost/filesystem/exception.hpp>
25 #include <boost/version.hpp>
26 #if BOOST_VERSION < 108500
27 #include <boost/filesystem/convenience.hpp>
29 #include <boost/filesystem/directory.hpp>
31 #include <boost/filesystem/operations.hpp>
33 #include <boost/algorithm/string.hpp>
39 namespace fs = boost::filesystem;
44 #if BOOST_FILESYSTEM_VERSION < 3
45 return p.empty() ? std::string() : *--p.end();
47 return p.empty() ? std::string() : (*--p.end()).
string();
52 static std::string
stem(
const fs::path& p)
55 std::size_t pos = fn.find(
'.');
56 if (pos == std::string::npos)
59 return fn.substr(0, pos);
67 std::string path = p.string();
69 return path.substr(0, path.length() - fn.length() - 1);
78 const std::string& examplesRoot,
79 const std::string& examplesType)
80 : deployPath_(deployPath),
81 examplesRoot_(examplesRoot),
82 examplesType_(examplesType)
84 wApp->internalPathChanged().connect
97 if (example.find(
"..") != std::string::npos
98 || example.find(
'/') != std::string::npos
99 || example.find(
'\\') != std::string::npos) {
108 const std::string& example)
114 exists = fs::exists(exampleDir);
115 }
catch (std::exception&) {
119 WApplication::instance()->setInternalPathValid(
false);
120 addWidget(std::make_unique<WText>(
"No such example: " + exampleDir));
124 model_ = std::make_shared<WStandardItemModel>(0, 1);
131 WApplication::instance()->setTitle(
tr(
"srcview.title." + example));
132 std::unique_ptr<WText> title(std::make_unique<WText>(
134 title->setInternalPathEncoding(
true);
136 auto exampleView = std::make_unique<WTreeView>();
169 auto topLayout = std::make_unique<WVBoxLayout>();
170 topLayout->addWidget(std::move(title));
172 auto gitLayout = std::make_unique<WHBoxLayout>();
174 gitLayout->
addWidget(std::move(exampleView), 0);
175 gitLayout->addWidget(std::move(sourceView), 1);
176 topLayout->addLayout(std::move(gitLayout), 1);
194 std::string ext = path.extension().string();
198 else if (ext ==
".C" || ext ==
".cpp")
205 const fs::path& path)
207 static const char *supportedFiles[] = {
208 ".C",
".cpp",
".h",
".css",
".xml",
".png",
".gif",
".csv",
".ico", 0
211 auto dir = std::make_unique<FileItem>(
"/icons/yellow-folder-open.png",
215 parent->appendRow(std::move(dir));
218 std::set<fs::path> paths;
220 fs::directory_iterator end_itr;
221 for (fs::directory_iterator i(path); i != end_itr; ++i)
224 std::vector<std::unique_ptr<FileItem>> classes, files;
225 std::vector<fs::path> dirs;
227 while (!paths.empty()) {
228 fs::path p = *paths.begin();
232 if (fs::is_symlink(p))
236 if (fs::is_regular_file(p)) {
237 std::string ext = p.extension().string();
238 bool supported =
false;
239 for (
const char **s = supportedFiles; *s != 0; ++s)
251 if (!companion.empty()) {
252 std::set<fs::path>::iterator it_companion = paths.find(companion);
254 if (it_companion != paths.end()) {
255 std::string className =
stem(p);
256 escapeText(className);
257 std::string label =
"<i>class</i> " + className;
259 std::unique_ptr<FileItem> classItem =
260 std::make_unique<FileItem>(
"/icons/cppclass.png", label, std::string());
264 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
267 = std::make_unique<FileItem>(
"/icons/document.png",
269 (*it_companion).string());
270 classItem->appendRow(std::move(header));
271 classItem->appendRow(std::move(cpp));
273 classes.push_back(std::move(classItem));
274 paths.erase(it_companion);
277 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
279 files.push_back(std::move(file));
281 }
else if (fs::is_directory(p)) {
285 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
287 files.push_back(std::move(file));
293 for (
unsigned int i = 0; i < classes.size(); i++)
294 parent->appendRow(std::move(classes[i]));
296 for (
unsigned int i = 0; i < files.size(); i++)
297 parent->appendRow(std::move(files[i]));
299 for (
unsigned int i = 0; i < dirs.size(); i++)
301 }
catch (fs::filesystem_error& e) {
302 std::cerr << e.what() << std::endl;
307 const fs::path& srcPath,
308 const std::string packageName)
310 fs::directory_iterator end_itr;
313 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
315 if (fs::is_regular_file(p)) {
317 auto item = std::make_unique<FileItem>(
"/icons/package.png", packageName,
"");
318 packageItem = item.get();
319 parent->appendRow(std::move(item));
323 = std::make_unique<FileItem>(
"/icons/javaclass.png",
filename(p),
329 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
331 if (fs::is_directory(p)) {
332 std::string pn = packageName;
343 const fs::path& path)
346 = std::make_unique<FileItem>(
"/icons/yellow-folder-open.png",
349 parent->appendRow(std::move(dir));
352 std::vector<fs::path> files, dirs;
354 fs::directory_iterator end_itr;
355 for (fs::directory_iterator i(path); i != end_itr; ++i) {
357 if (fs::is_directory(p)) {
360 = std::make_unique<FileItem>(
"/icons/package-folder-open.png",
363 parent->appendRow(std::move(dir));
375 for (
auto item : dirs)
378 for (
auto item : files) {
380 = std::make_unique<FileItem>(
"/icons/document.png",
filename(item),
382 parent->appendRow(std::move(file));
static fs::path getCompanion(const fs::path &path)
static std::string stem(const fs::path &p)
static std::string filename(const fs::path &p)
static bool comparePaths(const fs::path &p1, const fs::path &p2)
fs::path parent_path(const fs::path &p)
void cppTraverseDir(WStandardItem *parent, const boost::filesystem::path &path)
void setExample(const std::string &exampleDir, const std::string &example)
std::shared_ptr< WStandardItemModel > model_
ExampleSourceViewer(const std::string &deployPath, const std::string &examplesRoot, const std::string &examplesType)
Constructor.
void javaTraversePackages(WStandardItem *parent, const boost::filesystem::path &srcPath, const std::string packageName)
std::string examplesType_
std::string examplesRoot_
void showFile()
Displayed the currently selected file.
void javaTraverseDir(WStandardItem *parent, const boost::filesystem::path &path)
WStandardItem which stores a file.
static const Wt::ItemDataRole FileNameRole
static const Wt::ItemDataRole FilePathRole
static const Wt::ItemDataRole ContentsRole
bool setIndex(const WModelIndex &index)
Sets the model index.
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method) override
WModelIndexSet selectedIndexes() const
void select(const WModelIndex &index, SelectionFlag option=SelectionFlag::Select)
std::shared_ptr< WAbstractItemModel > model() const
Signal & selectionChanged()
void setSortingEnabled(bool enabled)
void setSelectionMode(SelectionMode mode)
std::string internalPathNextPart(const std::string &path) const
bool internalPathMatches(const std::string &path) const
void setInternalPathValid(bool valid)
void addWidget(std::unique_ptr< WWidget > widget, int stretch, WFlags< AlignmentFlag > alignment)
void setResizable(int index, bool enabled=true, const WLength &initialSize=WLength::Auto)
WModelIndex index() const
void appendRow(std::vector< std::unique_ptr< WStandardItem > > items)
WStandardItem * child(int row, int column=0) const
bool isExpanded(const WModelIndex &index) const
void setExpanded(const WModelIndex &, bool expanded)
virtual void setAlternatingRowColors(bool enable) override
virtual void setHeaderHeight(const WLength &height) override
virtual void setModel(const std::shared_ptr< WAbstractItemModel > &model) override
void expandToDepth(int depth)
virtual void resize(const WLength &width, const WLength &height) override