SourceView Class Reference
[Git model example]

View class for source code. More...

#include <SourceView.h>

Inheritance diagram for SourceView:

Inheritance graph
[legend]

List of all members.

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::WWidgetrenderView ()
 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::WMemoryResourceimageResource_


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 26 of file SourceView.h.


Constructor & Destructor Documentation

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]

Destructor.

Definition at line 25 of file SourceView.C.

00026 { }


Member Function Documentation

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 91 of file SourceView.C.

00092 {
00093   if (!index_.isValid()) {
00094     // no content
00095     WText *result = new WText();
00096     result->setInline(false);
00097     return result;
00098   }
00099 
00100   /*
00101    * read the contents, from string or file name
00102    */
00103   boost::any contentsData = index_.data(contentRole_);
00104   std::string content;
00105   if (!contentsData.empty())
00106    content = boost::any_cast<const std::string&>(contentsData);
00107   boost::any fileNameData = index_.data(fileNameRole_);
00108   std::string fileName = 
00109     boost::any_cast<const std::string&>(fileNameData);
00110   boost::any filePathData = index_.data(filePathRole_);
00111   std::string filePath;
00112   if (!filePathData.empty())
00113     filePath = boost::any_cast<const std::string&>(filePathData);
00114 
00115   /*
00116    * determine source language, for source highlight
00117    */
00118   std::string lang = getLanguageFromFileExtension(fileName);
00119   if (content != "" && content.substr(0, 100).find("-*- C++ -*-")
00120       != std::string::npos)
00121     lang = "cpp";
00122 
00123   std::string outputFileName;
00124 
00125   if (lang != "") {
00126     std::string inputFileName;
00127 
00128     if (!filePathData.empty())
00129       inputFileName = filePath;
00130     else {
00131       inputFileName = tempFileName();
00132       std::ofstream out(inputFileName.c_str(), 
00133                         std::ios::out | std::ios::binary);
00134       out.write(content.c_str(), content.length());
00135       out.close();
00136     }
00137     
00138     outputFileName = tempFileName();
00139 
00140     std::string sourceHighlightCommand = "source-highlight ";
00141     sourceHighlightCommand += "--src-lang=" + lang + " ";
00142     sourceHighlightCommand += "--out-format=xhtml ";
00143     sourceHighlightCommand += "--input=" + inputFileName + " ";
00144     sourceHighlightCommand += "--output=" + outputFileName + " ";
00145 
00146     std::cerr << sourceHighlightCommand << std::endl;
00147     bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
00148 
00149     if (sourceHighlightOk)
00150       content = readFileToString(outputFileName);
00151     else {
00152       content = readFileToString(inputFileName);
00153       lang = "";
00154     }
00155     unlink(outputFileName.c_str());
00156 
00157     if (filePathData.empty())
00158       unlink(inputFileName.c_str());
00159   } 
00160 
00161   if (content == "")
00162     // do not load binary files, we would need to perform proper UTF-8
00163     // transcoding to display them
00164     if (!boost::iends_with(fileName, ".jar")
00165         && !boost::iends_with(fileName, ".war")
00166         && !boost::iends_with(fileName, ".class"))
00167       content = readFileToString(fileName);
00168 
00169   delete imageResource_;
00170   imageResource_ = 0;
00171 
00172   WWidget *result = 0;
00173 
00174   if (!imageExtension(fileName).empty()) {
00175     WImage *image = new WImage();
00176     imageResource_ = new WMemoryResource(this);
00177     imageResource_->setMimeType("mime/" + imageExtension(fileName));
00178     imageResource_->setData((const unsigned char*)content.data(),
00179                             content.length());
00180     image->setResource(imageResource_);
00181     result = image;
00182   } else if (lang != "") {
00183     WText *text = new WText();
00184     text->setTextFormat(XHTMLUnsafeText);
00185     text->setText(content);
00186     result = text;
00187   } else {
00188     WText *text = new WText();
00189     text->setTextFormat(PlainText);
00190     text->setText(content);
00191     result = text;
00192   }
00193 
00194   result->setInline(false);
00195   WApplication::instance()
00196     ->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;");
00197   return result;
00198 }

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

Definition at line 200 of file SourceView.C.

00201 {
00202   static const char *imageExtensions[] = {
00203     ".png", ".gif", ".jpg", "jpeg", ".ico", 0
00204   };
00205 
00206   fs::path p(fileName);
00207   std::string extension = fs::extension(p);
00208 
00209   for (const char **s = imageExtensions; *s != 0; ++s)
00210     if (*s == extension)
00211       return extension.substr(1);
00212 
00213   return std::string();
00214 }


Member Data Documentation

The index that is currently displayed.

Definition at line 57 of file SourceView.h.

The role that is currently displayed.

Definition at line 60 of file SourceView.h.

int SourceView::contentRole_ [private]

Definition at line 61 of file SourceView.h.

Definition at line 62 of file SourceView.h.

Definition at line 64 of file SourceView.h.


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

Generated on Fri Mar 26 17:12:12 2010 for Wt by doxygen 1.5.6