9 #include <boost/algorithm/string.hpp> 
   11 #include <Wt/WApplication.h> 
   13 #include <Wt/WImage.h> 
   15 #include <Wt/cpp17/filesystem.hpp> 
   18 namespace fs = cpp17::filesystem;
 
   23     : fileNameRole_(fileNameRole),
 
   24       contentRole_(contentRole),
 
   25       filePathRole_(filePathRole),
 
   39         || (!fp.empty() && !fs::is_directory(fp))) {
 
   54   strcpy(spool, 
"/tmp/wtXXXXXX");
 
   56   int i = mkstemp(spool);
 
   59   char spool[2 * L_tmpnam];
 
   62   return std::string(spool);
 
   67   if (boost::iends_with(fileName, 
".h")
 
   68       || boost::iends_with(fileName, 
".C")
 
   69       || boost::iends_with(fileName, 
".cpp"))
 
   71   else if (boost::iends_with(fileName, 
".xml"))
 
   73   else if (boost::iends_with(fileName, 
".html"))
 
   75   else if (boost::iends_with(fileName, 
".java"))
 
   77   else if (boost::iends_with(fileName, 
".js"))
 
   79   else if (boost::iends_with(fileName, 
".css"))
 
   87   std::size_t outputFileSize = (std::size_t)fs::file_size(fileName);
 
   88   std::fstream file (fileName.c_str(), std::ios::in | std::ios::binary);
 
   89   char* memblock = 
new char [outputFileSize];
 
   90   file.read(memblock, (std::streamsize)outputFileSize);
 
   92   std::string data = std::string(memblock, outputFileSize);
 
  101     auto result = std::make_unique<WText>();
 
  102     result->setInline(
false);
 
  111   if (cpp17::any_has_value(contentsData))
 
  114   std::string fileName =
 
  117   std::string filePath;
 
  118   if (cpp17::any_has_value(filePathData))
 
  125   if (content != 
"" && content.substr(0, 100).find(
"-*- C++ -*-")
 
  126       != std::string::npos)
 
  129   std::string outputFileName;
 
  132     std::string inputFileName;
 
  134     if (cpp17::any_has_value(filePathData))
 
  135       inputFileName = filePath;
 
  138       std::ofstream out(inputFileName.c_str(),
 
  139                         std::ios::out | std::ios::binary);
 
  140       out.write(content.c_str(), (std::streamsize)content.length());
 
  146     std::string sourceHighlightCommand = 
"source-highlight ";
 
  147     sourceHighlightCommand += 
"--src-lang=" + lang + 
" ";
 
  148     sourceHighlightCommand += 
"--out-format=xhtml ";
 
  149     sourceHighlightCommand += 
"--input=" + inputFileName + 
" ";
 
  150     sourceHighlightCommand += 
"--output=" + outputFileName + 
" ";
 
  152     std::cerr << sourceHighlightCommand << std::endl;
 
  153     bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
 
  155     if (sourceHighlightOk)
 
  161     unlink(outputFileName.c_str());
 
  163     if (!cpp17::any_has_value(filePathData))
 
  164       unlink(inputFileName.c_str());
 
  170     if (!boost::iends_with(fileName, 
".jar")
 
  171         && !boost::iends_with(fileName, 
".war")
 
  172         && !boost::iends_with(fileName, 
".class"))
 
  175   std::unique_ptr<WWidget> result;
 
  178     std::unique_ptr<WImage> image(std::make_unique<WImage>());
 
  182                             (
int)content.length());
 
  184     result = std::move(image);
 
  185   } 
else if (lang != 
"") {
 
  186     auto text = std::make_unique<WText>();
 
  187     text->setTextFormat(TextFormat::UnsafeXHTML);
 
  188     text->setText(content);
 
  189     result = std::move(text);
 
  191     auto text = std::make_unique<WText>();
 
  192     text->setTextFormat(TextFormat::Plain);
 
  193     text->setText(content);
 
  194     result = std::move(text);
 
  197   result->setInline(
false);
 
  198   WApplication::instance()
 
  199     ->doJavaScript(result->jsRef() + 
".parentNode.scrollTop = 0;");
 
  205   static const char *imageExtensions[] = {
 
  206     ".png", 
".gif", 
".jpg", 
"jpeg", 
".ico", 0
 
  209   fs::path p(fileName);
 
  210   std::string extension = p.extension().string();
 
  212   for (
const char **s = imageExtensions; *s != 0; ++s)
 
  214       return extension.substr(1);
 
  216   return std::string();
 
std::string readFileToString(const std::string &fileName)
std::string getLanguageFromFileExtension(const std::string &fileName)
std::string tempFileName()
virtual std::unique_ptr< WWidget > renderView()
Returns the widget that renders the view.
Wt::ItemDataRole filePathRole_
WModelIndex index_
The index that is currently displayed.
std::string imageExtension(const std::string &fileName)
bool setIndex(const WModelIndex &index)
Sets the model index.
Wt::ItemDataRole fileNameRole_
The role that is currently displayed.
std::shared_ptr< WMemoryResource > imageResource_
SourceView(ItemDataRole fileNameRole, ItemDataRole contentRole, ItemDataRole filePathRole)
Constructor.
Wt::ItemDataRole contentRole_
virtual ~SourceView()
Destructor.
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
std::string toUTF8() const
WString asString(const cpp17::any &v, const WString &formatString=WString())