#include <SourceView.h>
Public Member Functions | |
SourceView (int fileNameRole, int contentRole, int filePathRole) | |
Constructor. | |
virtual | ~SourceView () |
Destructor. | |
bool | setIndex (const Wt::WModelIndex &index) |
Sets the model index. | |
virtual Wt::WWidget * | renderView () |
Returns the widget that renders the view. | |
Private Member Functions | |
std::string | imageExtension (const std::string &fileName) |
Private Attributes | |
Wt::WModelIndex | index_ |
The index that is currently displayed. | |
int | fileNameRole_ |
The role that is currently displayed. | |
int | contentRole_ |
int | filePathRole_ |
Wt::WMemoryResource * | imageResource_ |
A view class is used so that no server-side memory is used while displaying a potentially large file.
Definition at line 26 of file SourceView.h.
SourceView::SourceView | ( | int | fileNameRole, | |
int | contentRole, | |||
int | 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 18 of file SourceView.C.
00019 : fileNameRole_(fileNameRole), 00020 contentRole_(contentRole), 00021 filePathRole_(filePathRole), 00022 imageResource_(0) 00023 {}
SourceView::~SourceView | ( | ) | [virtual] |
bool SourceView::setIndex | ( | const Wt::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 28 of file SourceView.C.
00029 { 00030 if (index != index_ && index.isValid()) { 00031 std::string fp = index.data(filePathRole_).empty() ? std::string() 00032 : boost::any_cast<std::string>(index.data(filePathRole_)); 00033 00034 if (!index.data(contentRole_).empty() 00035 || (!fp.empty() && !fs::is_directory(fp))) { 00036 index_ = index; 00037 update(); 00038 00039 return true; 00040 } 00041 } 00042 00043 return false; 00044 }
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.
Implements Wt::WViewWidget.
Definition at line 93 of file SourceView.C.
00094 { 00095 if (!index_.isValid()) { 00096 // no content 00097 WText *result = new WText(); 00098 result->setInline(false); 00099 return result; 00100 } 00101 00102 /* 00103 * read the contents, from string or file name 00104 */ 00105 boost::any contentsData = index_.data(contentRole_); 00106 std::string content; 00107 if (!contentsData.empty()) 00108 content = boost::any_cast<const std::string&>(contentsData); 00109 boost::any fileNameData = index_.data(fileNameRole_); 00110 std::string fileName = 00111 boost::any_cast<const std::string&>(fileNameData); 00112 boost::any filePathData = index_.data(filePathRole_); 00113 std::string filePath; 00114 if (!filePathData.empty()) 00115 filePath = boost::any_cast<const std::string&>(filePathData); 00116 00117 /* 00118 * determine source language, for source highlight 00119 */ 00120 std::string lang = getLanguageFromFileExtension(fileName); 00121 if (content != "" && content.substr(0, 100).find("-*- C++ -*-") 00122 != std::string::npos) 00123 lang = "cpp"; 00124 00125 std::string outputFileName; 00126 00127 if (lang != "") { 00128 std::string inputFileName; 00129 00130 if (!filePathData.empty()) 00131 inputFileName = filePath; 00132 else { 00133 inputFileName = tempFileName(); 00134 std::ofstream out(inputFileName.c_str(), 00135 std::ios::out | std::ios::binary); 00136 out.write(content.c_str(), content.length()); 00137 out.close(); 00138 } 00139 00140 outputFileName = tempFileName(); 00141 00142 std::string sourceHighlightCommand = "source-highlight "; 00143 sourceHighlightCommand += "--src-lang=" + lang + " "; 00144 sourceHighlightCommand += "--out-format=xhtml "; 00145 sourceHighlightCommand += "--input=" + inputFileName + " "; 00146 sourceHighlightCommand += "--output=" + outputFileName + " "; 00147 00148 std::cerr << sourceHighlightCommand << std::endl; 00149 bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0; 00150 00151 if (sourceHighlightOk) 00152 content = readFileToString(outputFileName); 00153 else { 00154 content = readFileToString(inputFileName); 00155 lang = ""; 00156 } 00157 unlink(outputFileName.c_str()); 00158 00159 if (filePathData.empty()) 00160 unlink(inputFileName.c_str()); 00161 } 00162 00163 if (content == "") 00164 // do not load binary files, we would need to perform proper UTF-8 00165 // transcoding to display them 00166 if (!boost::iends_with(fileName, ".jar") 00167 && !boost::iends_with(fileName, ".war") 00168 && !boost::iends_with(fileName, ".class")) 00169 content = readFileToString(fileName); 00170 00171 delete imageResource_; 00172 imageResource_ = 0; 00173 00174 WWidget *result = 0; 00175 00176 if (!imageExtension(fileName).empty()) { 00177 WImage *image = new WImage(); 00178 imageResource_ = new WMemoryResource(this); 00179 imageResource_->setMimeType("mime/" + imageExtension(fileName)); 00180 imageResource_->setData((const unsigned char*)content.data(), 00181 content.length()); 00182 image->setResource(imageResource_); 00183 result = image; 00184 } else if (lang != "") { 00185 WText *text = new WText(); 00186 text->setTextFormat(XHTMLUnsafeText); 00187 text->setText(content); 00188 result = text; 00189 } else { 00190 WText *text = new WText(); 00191 text->setTextFormat(PlainText); 00192 text->setText(content); 00193 result = text; 00194 } 00195 00196 result->setInline(false); 00197 WApplication::instance() 00198 ->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;"); 00199 return result; 00200 }
std::string SourceView::imageExtension | ( | const std::string & | fileName | ) | [private] |
Definition at line 202 of file SourceView.C.
00203 { 00204 static const char *imageExtensions[] = { 00205 ".png", ".gif", ".jpg", "jpeg", ".ico", 0 00206 }; 00207 00208 fs::path p(fileName); 00209 std::string extension = fs::extension(p); 00210 00211 for (const char **s = imageExtensions; *s != 0; ++s) 00212 if (*s == extension) 00213 return extension.substr(1); 00214 00215 return std::string(); 00216 }
Wt::WModelIndex SourceView::index_ [private] |
int SourceView::fileNameRole_ [private] |
int SourceView::contentRole_ [private] |
Definition at line 61 of file SourceView.h.
int SourceView::filePathRole_ [private] |
Definition at line 62 of file SourceView.h.
Wt::WMemoryResource* SourceView::imageResource_ [private] |
Definition at line 64 of file SourceView.h.