11 #include <Wt/WApplication.h>
12 #include <Wt/WContainerWidget.h>
13 #include <Wt/WEnvironment.h>
14 #include <Wt/WLineEdit.h>
15 #include <Wt/WGridLayout.h>
16 #include <Wt/WHBoxLayout.h>
17 #include <Wt/WPushButton.h>
18 #include <Wt/WTable.h>
20 #include <Wt/WTreeView.h>
21 #include <Wt/WVBoxLayout.h>
22 #include <Wt/WViewWidget.h>
24 #include <boost/filesystem/operations.hpp>
25 #include <boost/filesystem/exception.hpp>
26 #include <boost/filesystem/convenience.hpp>
27 #include <boost/algorithm/string.hpp>
32 namespace fs = boost::filesystem;
37 #if BOOST_FILESYSTEM_VERSION < 3
38 return p.empty() ? std::string() : *--p.end();
40 return p.empty() ? std::string() : (*--p.end()).
string();
45 static std::string
stem(
const fs::path& p)
48 std::size_t pos = fn.find(
'.');
49 if (pos == std::string::npos)
52 return fn.substr(0, pos);
60 std::string path = p.string();
62 return path.substr(0, path.length() - fn.length() - 1);
71 const std::string& examplesRoot,
72 const std::string& examplesType)
73 : deployPath_(deployPath),
74 examplesRoot_(examplesRoot),
75 examplesType_(examplesType)
77 wApp->internalPathChanged().connect
90 if (example.find(
"..") != std::string::npos
91 || example.find(
'/') != std::string::npos
92 || example.find(
'\\') != std::string::npos) {
101 const std::string& example)
107 exists = fs::exists(exampleDir);
108 }
catch (std::exception&) {
112 WApplication::instance()->setInternalPathValid(
false);
113 addWidget(std::make_unique<WText>(
"No such example: " + exampleDir));
117 model_ = std::make_shared<WStandardItemModel>(0, 1);
124 WApplication::instance()->setTitle(
tr(
"srcview.title." + example));
125 std::unique_ptr<WText> title(std::make_unique<WText>(
127 title->setInternalPathEncoding(
true);
129 auto exampleView = std::make_unique<WTreeView>();
162 auto topLayout = std::make_unique<WVBoxLayout>();
163 topLayout->addWidget(std::move(title));
165 auto gitLayout = std::make_unique<WHBoxLayout>();
167 gitLayout->
addWidget(std::move(exampleView), 0);
168 gitLayout->addWidget(std::move(sourceView), 1);
169 topLayout->addLayout(std::move(gitLayout), 1);
187 std::string ext = fs::extension(path);
191 else if (ext ==
".C" || ext ==
".cpp")
198 const fs::path& path)
200 static const char *supportedFiles[] = {
201 ".C",
".cpp",
".h",
".css",
".xml",
".png",
".gif",
".csv",
".ico", 0
204 auto dir = std::make_unique<FileItem>(
"/icons/yellow-folder-open.png",
208 parent->appendRow(std::move(dir));
211 std::set<fs::path> paths;
213 fs::directory_iterator end_itr;
214 for (fs::directory_iterator i(path); i != end_itr; ++i)
217 std::vector<std::unique_ptr<FileItem>> classes, files;
218 std::vector<fs::path> dirs;
220 while (!paths.empty()) {
221 fs::path p = *paths.begin();
225 if (fs::is_symlink(p))
229 if (fs::is_regular(p)) {
230 std::string ext = fs::extension(p);
231 bool supported =
false;
232 for (
const char **s = supportedFiles; *s != 0; ++s)
244 if (!companion.empty()) {
245 std::set<fs::path>::iterator it_companion = paths.find(companion);
247 if (it_companion != paths.end()) {
248 std::string className =
stem(p);
249 escapeText(className);
250 std::string label =
"<i>class</i> " + className;
252 std::unique_ptr<FileItem> classItem =
253 std::make_unique<FileItem>(
"/icons/cppclass.png", label, std::string());
257 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
260 = std::make_unique<FileItem>(
"/icons/document.png",
262 (*it_companion).string());
263 classItem->appendRow(std::move(header));
264 classItem->appendRow(std::move(cpp));
266 classes.push_back(std::move(classItem));
267 paths.erase(it_companion);
270 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
272 files.push_back(std::move(file));
274 }
else if (fs::is_directory(p)) {
278 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
280 files.push_back(std::move(file));
286 for (
unsigned int i = 0; i < classes.size(); i++)
287 parent->appendRow(std::move(classes[i]));
289 for (
unsigned int i = 0; i < files.size(); i++)
290 parent->appendRow(std::move(files[i]));
292 for (
unsigned int i = 0; i < dirs.size(); i++)
294 }
catch (fs::filesystem_error& e) {
295 std::cerr << e.what() << std::endl;
300 const fs::path& srcPath,
301 const std::string packageName)
303 fs::directory_iterator end_itr;
306 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
308 if (fs::is_regular(p)) {
310 auto item = std::make_unique<FileItem>(
"/icons/package.png", packageName,
"");
311 packageItem = item.get();
312 parent->appendRow(std::move(item));
316 = std::make_unique<FileItem>(
"/icons/javaclass.png",
filename(p),
322 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
324 if (fs::is_directory(p)) {
325 std::string pn = packageName;
336 const fs::path& path)
339 = std::make_unique<FileItem>(
"/icons/yellow-folder-open.png",
342 parent->appendRow(std::move(dir));
345 std::vector<fs::path> files, dirs;
347 fs::directory_iterator end_itr;
348 for (fs::directory_iterator i(path); i != end_itr; ++i) {
350 if (fs::is_directory(p)) {
353 = std::make_unique<FileItem>(
"/icons/package-folder-open.png",
356 parent->appendRow(std::move(dir));
368 for (
auto item : dirs)
371 for (
auto item : files) {
373 = std::make_unique<FileItem>(
"/icons/document.png",
filename(item),
375 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