Wt examples  4.10.0
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SourceView Class Reference

View class for source code. More...

#include <SourceView.h>

Inheritance diagram for SourceView:
[legend]

Public Member Functions

 SourceView (ItemDataRole fileNameRole, ItemDataRole contentRole, ItemDataRole filePathRole)
 Constructor.
 
virtual ~SourceView ()
 Destructor.
 
bool setIndex (const WModelIndex &index)
 Sets the model index.
 
virtual std::unique_ptr< WWidget > renderView ()
 Returns the widget that renders the view.
 

Private Member Functions

std::string imageExtension (const std::string &fileName)
 

Private Attributes

WModelIndex index_
 The index that is currently displayed.
 
Wt::ItemDataRole fileNameRole_
 The role that is currently displayed.
 
Wt::ItemDataRole contentRole_
 
Wt::ItemDataRole filePathRole_
 
std::shared_ptr< WMemoryResource > imageResource_
 

Detailed Description

View class for source code.

A view class is used so that no server-side memory is used while displaying a potentially large file.

Definition at line 28 of file SourceView.h.

Constructor & Destructor Documentation

◆ SourceView()

SourceView::SourceView ( ItemDataRole  fileNameRole,
ItemDataRole  contentRole,
ItemDataRole  filePathRole 
)

Constructor.

The fileNameRole will be used to retrieve data from a file to be displayed. If no data is set for this role, then contentRole should hold the data as a string.

Definition at line 20 of file SourceView.C.

23 : fileNameRole_(fileNameRole),
24 contentRole_(contentRole),
25 filePathRole_(filePathRole),
27{}
Wt::ItemDataRole filePathRole_
Definition SourceView.h:66
Wt::ItemDataRole fileNameRole_
The role that is currently displayed.
Definition SourceView.h:64
std::shared_ptr< WMemoryResource > imageResource_
Definition SourceView.h:68
Wt::ItemDataRole contentRole_
Definition SourceView.h:65

◆ ~SourceView()

SourceView::~SourceView ( )
virtual

Destructor.

Definition at line 29 of file SourceView.C.

30{ }

Member Function Documentation

◆ imageExtension()

std::string SourceView::imageExtension ( const std::string &  fileName)
private

Definition at line 203 of file SourceView.C.

204{
205 static const char *imageExtensions[] = {
206 ".png", ".gif", ".jpg", "jpeg", ".ico", 0
207 };
208
209 fs::path p(fileName);
210 std::string extension = fs::extension(p);
211
212 for (const char **s = imageExtensions; *s != 0; ++s)
213 if (*s == extension)
214 return extension.substr(1);
215
216 return std::string();
217}

◆ renderView()

std::unique_ptr< WWidget > SourceView::renderView ( )
virtual

Returns the widget that renders the view.

Returns he view contents: renders the file to a WText widget. WViewWidget deletes this widget after every rendering step.

Definition at line 97 of file SourceView.C.

98{
99 if (!index_.isValid()) {
100 // no content
101 auto result = std::make_unique<WText>();
102 result->setInline(false);
103 return std::move(result);
104 }
105
106 /*
107 * read the contents, from string or file name
108 */
109 cpp17::any contentsData = index_.data(contentRole_);
110 std::string content;
111 if (cpp17::any_has_value(contentsData))
112 content = asString(contentsData).toUTF8();
113 cpp17::any fileNameData = index_.data(fileNameRole_);
114 std::string fileName =
115 asString(fileNameData).toUTF8();
116 cpp17::any filePathData = index_.data(filePathRole_);
117 std::string filePath;
118 if (cpp17::any_has_value(filePathData))
119 filePath = asString(filePathData).toUTF8();
120
121 /*
122 * determine source language, for source highlight
123 */
124 std::string lang = getLanguageFromFileExtension(fileName);
125 if (content != "" && content.substr(0, 100).find("-*- C++ -*-")
126 != std::string::npos)
127 lang = "cpp";
128
129 std::string outputFileName;
130
131 if (lang != "") {
132 std::string inputFileName;
133
134 if (cpp17::any_has_value(filePathData))
135 inputFileName = filePath;
136 else {
137 inputFileName = tempFileName();
138 std::ofstream out(inputFileName.c_str(),
139 std::ios::out | std::ios::binary);
140 out.write(content.c_str(), (std::streamsize)content.length());
141 out.close();
142 }
143
144 outputFileName = tempFileName();
145
146 std::string sourceHighlightCommand = "source-highlight ";
147 sourceHighlightCommand += "--src-lang=" + lang + " ";
148 sourceHighlightCommand += "--out-format=xhtml ";
149 sourceHighlightCommand += "--input=" + inputFileName + " ";
150 sourceHighlightCommand += "--output=" + outputFileName + " ";
151
152 std::cerr << sourceHighlightCommand << std::endl;
153 bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
154
155 if (sourceHighlightOk)
156 content = readFileToString(outputFileName);
157 else {
158 content = readFileToString(inputFileName);
159 lang = "";
160 }
161 unlink(outputFileName.c_str());
162
163 if (!cpp17::any_has_value(filePathData))
164 unlink(inputFileName.c_str());
165 }
166
167 if (content == "")
168 // do not load binary files, we would need to perform proper UTF-8
169 // transcoding to display them
170 if (!boost::iends_with(fileName, ".jar")
171 && !boost::iends_with(fileName, ".war")
172 && !boost::iends_with(fileName, ".class"))
173 content = readFileToString(fileName);
174
175 std::unique_ptr<WWidget> result;
176
177 if (!imageExtension(fileName).empty()) {
178 std::unique_ptr<WImage> image(std::make_unique<WImage>());
179 imageResource_ = std::make_shared<WMemoryResource>();
180 imageResource_->setMimeType("mime/" + imageExtension(fileName));
181 imageResource_->setData((const unsigned char*)content.data(),
182 (int)content.length());
183 image->setImageLink(WLink(imageResource_));
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);
190 } else {
191 auto text = std::make_unique<WText>();
192 text->setTextFormat(TextFormat::Plain);
193 text->setText(content);
194 result = std::move(text);
195 }
196
197 result->setInline(false);
198 WApplication::instance()
199 ->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;");
200 return std::move(result);
201}
std::string readFileToString(const std::string &fileName)
Definition SourceView.C:85
std::string getLanguageFromFileExtension(const std::string &fileName)
Definition SourceView.C:65
std::string tempFileName()
Definition SourceView.C:50
WModelIndex index_
The index that is currently displayed.
Definition SourceView.h:61
std::string imageExtension(const std::string &fileName)
Definition SourceView.C:203

◆ setIndex()

bool SourceView::setIndex ( const WModelIndex &  index)

Sets the model index.

Returns true whether the view will be rerendered. The view will only be rerendered if the index contains new data.

Definition at line 32 of file SourceView.C.

33{
34 if (index != index_ && index.isValid()) {
35 std::string fp = !cpp17::any_has_value(index.data(filePathRole_)) ? std::string()
36 : asString(index.data(filePathRole_)).toUTF8();
37
38 if (cpp17::any_has_value(index.data(contentRole_))
39 || (!fp.empty() && !fs::is_directory(fp))) {
40 index_ = index;
41 update();
42
43 return true;
44 }
45 }
46
47 return false;
48}

Member Data Documentation

◆ contentRole_

Wt::ItemDataRole SourceView::contentRole_
private

Definition at line 65 of file SourceView.h.

◆ fileNameRole_

Wt::ItemDataRole SourceView::fileNameRole_
private

The role that is currently displayed.

Definition at line 64 of file SourceView.h.

◆ filePathRole_

Wt::ItemDataRole SourceView::filePathRole_
private

Definition at line 66 of file SourceView.h.

◆ imageResource_

std::shared_ptr<WMemoryResource> SourceView::imageResource_
private

Definition at line 68 of file SourceView.h.

◆ index_

WModelIndex SourceView::index_
private

The index that is currently displayed.

Definition at line 61 of file SourceView.h.


The documentation for this class was generated from the following files: