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 <Wt/cpp17/filesystem.hpp>
29 namespace fs = cpp17::filesystem;
34 return p.empty() ? std::string() : (*--p.end()).
string();
38 static std::string
stem(
const fs::path& p)
41 std::size_t pos = fn.find(
'.');
42 if (pos == std::string::npos)
45 return fn.substr(0, pos);
53 std::string path = p.string();
55 return path.substr(0, path.length() - fn.length() - 1);
64 const std::string& examplesRoot,
65 const std::string& examplesType)
66 : deployPath_(deployPath),
67 examplesRoot_(examplesRoot),
68 examplesType_(examplesType)
70 wApp->internalPathChanged().connect
83 if (example.find(
"..") != std::string::npos
84 || example.find(
'/') != std::string::npos
85 || example.find(
'\\') != std::string::npos) {
94 const std::string& example)
100 exists = fs::exists(exampleDir);
101 }
catch (std::exception&) {
105 WApplication::instance()->setInternalPathValid(
false);
106 addWidget(std::make_unique<WText>(
"No such example: " + exampleDir));
110 model_ = std::make_shared<WStandardItemModel>(0, 1);
117 WApplication::instance()->setTitle(
tr(
"srcview.title." + example));
118 std::unique_ptr<WText> title(std::make_unique<WText>(
120 title->setInternalPathEncoding(
true);
122 auto exampleView = std::make_unique<WTreeView>();
155 auto topLayout = std::make_unique<WVBoxLayout>();
156 topLayout->addWidget(std::move(title));
158 auto gitLayout = std::make_unique<WHBoxLayout>();
160 gitLayout->
addWidget(std::move(exampleView), 0);
161 gitLayout->addWidget(std::move(sourceView), 1);
162 topLayout->addLayout(std::move(gitLayout), 1);
180 std::string ext = path.extension().string();
184 else if (ext ==
".C" || ext ==
".cpp")
191 const fs::path& path)
193 static const char *supportedFiles[] = {
194 ".C",
".cpp",
".h",
".css",
".xml",
".png",
".gif",
".csv",
".ico", 0
197 auto dir = std::make_unique<FileItem>(
"/icons/yellow-folder-open.png",
201 parent->appendRow(std::move(dir));
204 std::set<fs::path> paths;
206 fs::directory_iterator end_itr;
207 for (fs::directory_iterator i(path); i != end_itr; ++i)
210 std::vector<std::unique_ptr<FileItem>> classes, files;
211 std::vector<fs::path> dirs;
213 while (!paths.empty()) {
214 fs::path p = *paths.begin();
218 if (fs::is_symlink(p))
222 if (fs::is_regular_file(p)) {
223 std::string ext = p.extension().string();
224 bool supported =
false;
225 for (
const char **s = supportedFiles; *s != 0; ++s)
237 if (!companion.empty()) {
238 std::set<fs::path>::iterator it_companion = paths.find(companion);
240 if (it_companion != paths.end()) {
241 std::string className =
stem(p);
242 escapeText(className);
243 std::string label =
"<i>class</i> " + className;
245 std::unique_ptr<FileItem> classItem =
246 std::make_unique<FileItem>(
"/icons/cppclass.png", label, std::string());
250 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
253 = std::make_unique<FileItem>(
"/icons/document.png",
255 (*it_companion).string());
256 classItem->appendRow(std::move(header));
257 classItem->appendRow(std::move(cpp));
259 classes.push_back(std::move(classItem));
260 paths.erase(it_companion);
263 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
265 files.push_back(std::move(file));
267 }
else if (fs::is_directory(p)) {
271 = std::make_unique<FileItem>(
"/icons/document.png",
filename(p),
273 files.push_back(std::move(file));
279 for (
unsigned int i = 0; i < classes.size(); i++)
280 parent->appendRow(std::move(classes[i]));
282 for (
unsigned int i = 0; i < files.size(); i++)
283 parent->appendRow(std::move(files[i]));
285 for (
unsigned int i = 0; i < dirs.size(); i++)
287 }
catch (fs::filesystem_error& e) {
288 std::cerr << e.what() << std::endl;
293 const fs::path& srcPath,
294 const std::string packageName)
296 fs::directory_iterator end_itr;
299 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
301 if (fs::is_regular_file(p)) {
303 auto item = std::make_unique<FileItem>(
"/icons/package.png", packageName,
"");
304 packageItem = item.get();
305 parent->appendRow(std::move(item));
309 = std::make_unique<FileItem>(
"/icons/javaclass.png",
filename(p),
315 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
317 if (fs::is_directory(p)) {
318 std::string pn = packageName;
329 const fs::path& path)
332 = std::make_unique<FileItem>(
"/icons/yellow-folder-open.png",
335 parent->appendRow(std::move(dir));
338 std::vector<fs::path> files, dirs;
340 fs::directory_iterator end_itr;
341 for (fs::directory_iterator i(path); i != end_itr; ++i) {
343 if (fs::is_directory(p)) {
346 = std::make_unique<FileItem>(
"/icons/package-folder-open.png",
349 parent->appendRow(std::move(dir));
361 for (
auto item : dirs)
364 for (
auto item : files) {
366 = std::make_unique<FileItem>(
"/icons/document.png",
filename(item),
368 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 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 cppTraverseDir(WStandardItem *parent, const cpp17::filesystem::path &path)
std::string examplesType_
std::string examplesRoot_
void showFile()
Displayed the currently selected file.
void javaTraverseDir(WStandardItem *parent, const cpp17::filesystem::path &path)
void javaTraversePackages(WStandardItem *parent, const cpp17::filesystem::path &srcPath, const std::string packageName)
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