/usr/lib/Wt/examples/wt-homepage/ExampleSourceViewer.C is in witty-examples 3.3.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | /*
* Copyright (C) 2009 Emweb bvba
*
* See the LICENSE file for terms of use.
*/
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WLineEdit>
#include <Wt/WGridLayout>
#include <Wt/WHBoxLayout>
#include <Wt/WPushButton>
#include <Wt/WTable>
#include <Wt/WText>
#include <Wt/WTreeView>
#include <Wt/WVBoxLayout>
#include <Wt/WViewWidget>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/algorithm/string.hpp>
#include "ExampleSourceViewer.h"
#include "FileItem.h"
using namespace Wt;
namespace fs = boost::filesystem;
// Same as p.filename() in latest boost::filesystem
static std::string filename(const fs::path& p)
{
#if BOOST_FILESYSTEM_VERSION < 3
return p.empty() ? std::string() : *--p.end();
#else
return p.empty() ? std::string() : (*--p.end()).string();
#endif
}
// Same as p.stem() in latest boost::filesystem
static std::string stem(const fs::path& p)
{
std::string fn = filename(p);
std::size_t pos = fn.find('.');
if (pos == std::string::npos)
return fn;
else
return fn.substr(0, pos);
}
// Should be same as p.parent_path() in latest boost::filesystem
// This is not entirely according to fs::path::parent_path() in 1.39.0
fs::path parent_path(const fs::path& p)
{
std::string fn = filename(p);
std::string path = p.string();
return path.substr(0, path.length() - fn.length() - 1);
}
static bool comparePaths(const fs::path& p1, const fs::path& p2)
{
return filename(p1) > filename(p2);
}
ExampleSourceViewer::ExampleSourceViewer(const std::string& deployPath,
const std::string& examplesRoot,
const std::string& examplesType)
: deployPath_(deployPath),
examplesRoot_(examplesRoot),
examplesType_(examplesType)
{
wApp->internalPathChanged().connect
(this, &ExampleSourceViewer::handlePathChange);
handlePathChange();
}
void ExampleSourceViewer::handlePathChange()
{
WApplication *app = wApp;
if (app->internalPathMatches(deployPath_)) {
std::string example = app->internalPathNextPart(deployPath_);
if (example.find("..") != std::string::npos
|| example.find('/') != std::string::npos
|| example.find('\\') != std::string::npos) {
app->setInternalPathValid(false);
setExample("INVALID_DIR", "INVALID");
} else
setExample(examplesRoot_ + example, example);
}
}
void ExampleSourceViewer::setExample(const std::string& exampleDir,
const std::string& example)
{
clear();
bool exists = false;
try {
exists = fs::exists(exampleDir);
} catch (std::exception&) {
}
if (!exists) {
WApplication::instance()->setInternalPathValid(false);
addWidget(new WText("No such example: " + exampleDir));
return;
}
model_ = new WStandardItemModel(0, 1, this);
if (examplesType_ == "CPP") {
cppTraverseDir(model_->invisibleRootItem(), exampleDir);
} else if (examplesType_ == "JAVA") {
javaTraverseDir(model_->invisibleRootItem(), exampleDir);
}
WApplication::instance()->setTitle(tr("srcview.title." + example));
WText *title =
new WText(tr("srcview.title." + examplesType_ + "." + example));
title->setInternalPathEncoding(true);
exampleView_ = new WTreeView();
exampleView_->setHeaderHeight(0);
exampleView_->resize(300, WLength::Auto);
exampleView_->setSortingEnabled(false);
exampleView_->setModel(model_);
exampleView_->expandToDepth(1);
exampleView_->setSelectionMode(SingleSelection);
exampleView_->setAlternatingRowColors(false);
exampleView_->selectionChanged().connect
(this, &ExampleSourceViewer::showFile);
sourceView_ = new SourceView(FileItem::FileNameRole,
FileItem::ContentsRole,
FileItem::FilePathRole);
sourceView_->setStyleClass("source-view");
/*
* Expand path to first file, to show something in the source viewer
*/
WStandardItem *w = model_->item(0);
do {
exampleView_->setExpanded(w->index(), true);
if (w->rowCount() > 0)
w = w->child(0);
else {
exampleView_->select(w->index(), Select);
w = 0;
}
} while (w);
WVBoxLayout *topLayout = new WVBoxLayout();
topLayout->addWidget(title);
WHBoxLayout *gitLayout = new WHBoxLayout();
gitLayout->addWidget(exampleView_, 0);
gitLayout->addWidget(sourceView_, 1);
topLayout->addLayout(gitLayout, 1);
gitLayout->setResizable(0);
/*
* FIXME, in plain HTML mode, we should set a minimum size to the source
* view, and remove this in enableAjax() ?
*/
// sourceView_->setHeight("100%");
setLayout(topLayout);
setStyleClass("maindiv");
}
/*
* Return the companion implementation/header file for a C++ source file.
*/
static fs::path getCompanion(const fs::path& path)
{
std::string ext = fs::extension(path);
if (ext == ".h")
return parent_path(path) / (stem(path) + ".C");
else if (ext == ".C" || ext == ".cpp")
return parent_path(path) / (stem(path) + ".h");
else
return fs::path();
}
void ExampleSourceViewer::cppTraverseDir(WStandardItem* parent,
const fs::path& path)
{
static const char *supportedFiles[] = {
".C", ".cpp", ".h", ".css", ".xml", ".png", ".gif", ".csv", ".ico", 0
};
FileItem* dir = new FileItem("/icons/yellow-folder-open.png", filename(path),
"");
parent->appendRow(dir);
parent = dir;
try {
std::set<fs::path> paths;
fs::directory_iterator end_itr;
for (fs::directory_iterator i(path); i != end_itr; ++i)
paths.insert(*i);
std::vector<FileItem*> classes, files;
std::vector<fs::path> dirs;
while (!paths.empty()) {
fs::path p = *paths.begin();
paths.erase(p);
// skip symbolic links and other files
if (fs::is_symlink(p))
continue;
// skip files with an extension we do not want to handle
if (fs::is_regular(p)) {
std::string ext = fs::extension(p);
bool supported = false;
for (const char **s = supportedFiles; *s != 0; ++s)
if (*s == ext) {
supported = true;
break;
}
if (!supported)
continue;
}
// see if we have one file of a class (.C, .h)
fs::path companion = getCompanion(p);
if (!companion.empty()) {
std::set<fs::path>::iterator it_companion = paths.find(companion);
if (it_companion != paths.end()) {
std::string className = stem(p);
escapeText(className);
std::string label = "<i>class</i> " + className;
FileItem *classItem =
new FileItem("/icons/cppclass.png", label, std::string());
classItem->setFlags(classItem->flags() | ItemIsXHTMLText);
FileItem *header = new FileItem("/icons/document.png", filename(p),
p.string());
FileItem *cpp = new FileItem("/icons/document.png",
filename(*it_companion),
(*it_companion).string());
classItem->appendRow(header);
classItem->appendRow(cpp);
classes.push_back(classItem);
paths.erase(it_companion);
} else {
FileItem *file = new FileItem("/icons/document.png", filename(p),
p.string());
files.push_back(file);
}
} else if (fs::is_directory(p)) {
dirs.push_back(p);
} else {
FileItem *file = new FileItem("/icons/document.png", filename(p),
p.string());
files.push_back(file);
}
}
std::sort(dirs.begin(), dirs.end(), comparePaths);
for (unsigned int i = 0; i < classes.size(); i++)
parent->appendRow(classes[i]);
for (unsigned int i = 0; i < files.size(); i++)
parent->appendRow(files[i]);
for (unsigned int i = 0; i < dirs.size(); i++)
cppTraverseDir(parent, dirs[i]);
} catch (fs::filesystem_error& e) {
std::cerr << e.what() << std::endl;
}
}
void ExampleSourceViewer::javaTraversePackages(WStandardItem *parent,
const fs::path& srcPath,
const std::string packageName)
{
fs::directory_iterator end_itr;
FileItem *packageItem = 0;
for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
fs::path p = *i;
if (fs::is_regular(p)) {
if (!packageItem) {
packageItem = new FileItem("/icons/package.png", packageName, "");
parent->appendRow(packageItem);
}
FileItem *file = new FileItem("/icons/javaclass.png", filename(p),
p.string());
packageItem->appendRow(file);
}
}
for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
fs::path p = *i;
if (fs::is_directory(p)) {
std::string pn = packageName;
if (!pn.empty())
pn += ".";
pn += filename(p);
javaTraversePackages(parent, p, pn);
}
}
}
void ExampleSourceViewer::javaTraverseDir(WStandardItem* parent,
const fs::path& path)
{
FileItem* dir = new FileItem("/icons/yellow-folder-open.png", filename(path),
"");
parent->appendRow(dir);
parent = dir;
std::vector<fs::path> files, dirs;
fs::directory_iterator end_itr;
for (fs::directory_iterator i(path); i != end_itr; ++i) {
fs::path p = *i;
if (fs::is_directory(p)) {
if (filename(p) == "src") {
FileItem* dir = new FileItem("/icons/package-folder-open.png",
filename(p), "");
parent->appendRow(dir);
javaTraversePackages(dir, p, "");
} else
dirs.push_back(p);
} else {
files.push_back(p);
}
}
std::sort(dirs.begin(), dirs.end(), comparePaths);
std::sort(files.begin(), files.end(), comparePaths);
for (unsigned int i = 0; i < dirs.size(); i++)
javaTraverseDir(parent, dirs[i]);
for (unsigned int i = 0; i < files.size(); i++) {
FileItem *file = new FileItem("/icons/document.png", filename(files[i]),
files[i].string());
parent->appendRow(file);
}
}
/*! \brief Displayed the currently selected file.
*/
void ExampleSourceViewer::showFile() {
if (exampleView_->selectedIndexes().empty())
return;
WModelIndex selected = *exampleView_->selectedIndexes().begin();
// expand a folder when clicked
if (exampleView_->model()->rowCount(selected) > 0
&& !exampleView_->isExpanded(selected))
exampleView_->setExpanded(selected, true);
// (for a file,) load data in source viewer
sourceView_->setIndex(selected);
}
|