Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsLatestNews2.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsLatestNews2.cpp
4  *
5  * Description: latest news Wittyshare plugin
6  *
7  * Created: 03/07/12 11:07:15
8  *
9  * Author: Guy Deleeuw, Guy.Deleeuw@gmail.com
10  *
11  */
12 
13 #include <boost/filesystem.hpp>
14 
15 #include <iostream>
16 #include <dlfcn.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #include <Wt/WDateTime>
23 #include <Wt/WStandardItemModel>
24 
25 #include <gdcore/gdCore.h>
26 
27 #include <gdwtcore/gdWTextDelegate.h>
28 
30 #include <User/WsUser.h>
32 
33 #include <WsModule/WsModule.h>
34 
35 #include "WsLatestNews.h"
36 #include "WsLatestNews2.h"
37 
38 #define WSMODLATESTNEWS "wsmodlatestnews"
39 #define NB_NEWS "nb_news"
40 #define ROOT "root"
41 
42 
43 using namespace Wt;
44 
45 WsLatestNews2::WsLatestNews2(WContainerWidget* parent)
46  : WContainerWidget(parent), m_pTableView(0)
47 {
48  addStyleClass("WsLatestNews2");
49 }
50 
52 { }
53 
55 {
56  WContainerWidget::load();
57  setTable();
58  WText* pTitle = new WText(asString(option("title")).toUTF8());
59  pTitle->addStyleClass("WsTitle");
60  addWidget(pTitle);
61  if ( m_pTableView )
62  addWidget(m_pTableView);
63  if ( asString(option("useReadAll")) == "true" ) {
64  std::string link = asString(option("link")).toUTF8();
65  boost::replace_all(link, "&amp;", "&");
66  std::string text = asString(option("text")).toUTF8();
67  WAnchor* pAnchor = new WAnchor(WLink(WLink::InternalPath, link), text);
68  pAnchor->addStyleClass("WsReadAll");
69  addWidget(pAnchor);
70  }
71 }
72 
74 {
76  std::string path = WsApplication::wsInstance()->internalPath();
77  std::string sWithoutPrefix = WsApp->WsModules().pathWithoutPrefix(path);
78  std::string rootPath = asString(option("rootPath")).narrow();
79  boost::algorithm::replace_all(rootPath, "&amp;", "&");
80  if ( rootPath.size() < 1 ) rootPath = user->getProperty(sWithoutPrefix, WSMODLATESTNEWS, ROOT, "");
81  NodePtr root;
82  if (rootPath.size() == 0)
83  root = user->getAccessRoot();
84  else {
85  //root = user->getAccessRoot(rootPath);
86  root = user->getAccessRoot();
87  root = root.get()->eatPath(rootPath);
88  }
89  if (root.get() == 0) {
90  LOG(DEBUG) << "WsLatestNews2::setTable cannot get root ! root = " << root << " rootPath " << rootPath;
91  return;
92  }
93  WsLatestNews latest(root);
95  int nbl = boost::lexical_cast<int>(props->get(WSMODLATESTNEWS, NB_NEWS, "10"));
96  int rowsCount = asNumber(option("rowsCount"));
97  if ( rowsCount > 0 ) nbl = rowsCount;
98  std::vector<NodePtr> news = latest.getLatestNews(nbl);
99  WStandardItemModel* m_pModelView = new WStandardItemModel(0, 4, this);
100  // TODO : add option show/hide header
101  m_pModelView->setHeaderData(0, WString::tr("WsModLatestNews2-Name"));
102  m_pModelView->setHeaderData(1, WString::tr("WsModLatestNews2-Desc"));
103  m_pModelView->setHeaderData(2, WString::tr("WsModLatestNews2-Size"));
104  m_pModelView->setHeaderData(3, WString::tr("WsModLatestNews2-Date"));
105  m_pModelView->setSortRole(UserRole);
106  for (int i = 0; i < news.size(); ++i) {
107  if ( !(
108  news[i].get()->getPath().extension().string() == ".fhtml" ||
109  news[i].get()->getPath().extension().string() == ".pdf" ||
110  news[i].get()->getPath().extension().string() == ".rpg" ||
111  news[i].get()->getPath().extension().string() == ".zip"
112  ) )
113  continue;
114  if ( news[i].get()->getProperties().get()->get("global", "in_view", "true") == "false" ) continue;
115  boost::filesystem::path wsRes = news[i].get()->getPath();
116  bool bRes = false;
117  while (wsRes.string() != "/") {
118  wsRes = wsRes.parent_path();
119  if ( wsRes.filename().string() == "ws.res" ) {
120  bRes = true;
121  break;
122  }
123  if ( wsRes.filename().string() == "ws" ) {
124  bRes = true;
125  break;
126  }
127  continue;
128  }
129  if ( bRes ) continue;
130  std::vector<WStandardItem*> pRow;
131  WDateTime dateTime;
132  dateTime.setTime_t(boost::lexical_cast<time_t>(dynamic_cast<WsFileNode*>(news[i].get())->getPublishDate()));
133  std::string sName = news[i].get()->getName();
134  std::string sPath = news[i].get()->getPath().string();
135  WStandardItem* nameItem = new WStandardItem(sName);
136  nameItem->setData(sPath, UserRole);
137  pRow.push_back(nameItem);
138  std::string sDesc;
139  sDesc += "<p class='ls2Date'>";
140  sDesc += dateTime.toString("yyyy-MM-dd").toUTF8();
141  sDesc += "</p>";
142  if ( asString(option("useDisplayName")) == "true" ) {
143  std::string dispName = news[i].get()->getDisplayName(true);
144  boost::algorithm::replace_all(dispName, "&", "&amp;");
145  sDesc += "<p>" + dispName + "</p>";
146  } else
147  sDesc += news[i].get()->getProperties().get()->get( "global", "short_description", "");
148  WStandardItem* descItem = new WStandardItem(sDesc);
149  // WLink lnk;
150  // lnk.setUrl("http://www.google.be");
151  // descItem->setData(lnk, LinkRole);
152  descItem->setData(sDesc, UserRole);
153  pRow.push_back(descItem);
154  std::stringstream sstr;
155  sstr << news[i].get()->getSize();
156  std::string sSize(sstr.str());
157  sstr.clear();
158  WStandardItem* sizeItem = new WStandardItem(sSize);
159  long lSize = news[i].get()->getSize();
160  sizeItem->setData(sSize, UserRole);
161  pRow.push_back(sizeItem);
162  WStandardItem* dateItem = new WStandardItem(dateTime.toString("yyyy-MM-dd : hh-mm-ss"));
163  dateItem->setData(dateTime, UserRole);
164  pRow.push_back(dateItem);
165  m_pModelView->invisibleRootItem()->appendRow(pRow);
166  }
167  m_pModelView->sort(3, Wt::DescendingOrder);
168  m_pTableView = new WTableView();
169  m_pTableView->setModel(m_pModelView);
170  m_pTableView->setColumnResizeEnabled(true);
171  m_pTableView->setSortingEnabled(true);
172  m_pTableView->setAlternatingRowColors(true);
173  m_pTableView->setSelectionMode(SingleSelection);
174  m_pTableView->setColumnAlignment(2, AlignRight);
175  m_pTableView->setColumnAlignment(3, AlignRight);
176  long rowHeight = asNumber(option("rowHeight"));
177  if ( rowHeight < 1 )
178  m_pTableView->setRowHeight(38);
179  else
180  m_pTableView->setRowHeight(rowHeight);
181  std::string hideHeader = asString(option("hideHeader")).narrow();
182  if ( hideHeader == "true" )
183  m_pTableView->setHeaderHeight(0);
184  std::string columnsWidth = asString(option("columnsWidth")).narrow();
185  if ( columnsWidth.size() < 1 ) {
186  m_pTableView->setColumnWidth(0, 180);
187  m_pTableView->setColumnWidth(1, 300);
188  } else {
189  std::vector<std::string> sVectColumnsWidth;
190  gd_core_string2vector(sVectColumnsWidth, columnsWidth.c_str(), ",");
191  for (int iCol = 0; iCol < sVectColumnsWidth.size(); ++iCol) {
192  double width = asNumber(sVectColumnsWidth[iCol]);
193  if ( width != -1 )
194  m_pTableView->setColumnWidth(iCol, width);
195  }
196  }
197  std::string columnsToHide = asString(option("hideColumns")).narrow();
198  std::vector<std::string> sVectColumns;
199  gd_core_string2vector(sVectColumns, columnsToHide.c_str(), ",");
200  for (int iCol = 0; iCol < sVectColumns.size(); ++iCol) {
201  int col = asNumber(sVectColumns[iCol]);
202  m_pTableView->setColumnHidden(col, true);
203  }
204  gdWTextDelegate* delegate2 = new gdWTextDelegate(this);
205  delegate2->setRowHeight(m_pTableView->rowHeight().value());
206  m_pTableView->setItemDelegateForColumn(1, delegate2);
207  m_pTableView->selectionChanged().connect(SLOT(this, WsLatestNews2::onViewSelectionChanged));
208 }
209 
211 {
212  WModelIndexSet pSet = m_pTableView->selectedIndexes();
213  WModelIndexSet::iterator it = pSet.begin();
214  if ( pSet.empty() ) return;
215  WModelIndex idx = *it;
216  std::string str = asString(idx.data(UserRole)).toUTF8();
217  wApp->setInternalPath(str, true);
218 }
219 
Global properties.
WsUser * wsUser()
boost::shared_ptr< WsAbstractNode > NodePtr
#define WSMODLATESTNEWS
#define DEBUG
Definition: WsLogger.h:27
std::string getProperty(const std::string &path, const std::string &section, const string &attr, const std::string &def)
returns one property of a node If the property does not exist, a empty string is returned.
Definition: WsUser.cpp:91
Global properties class.
vector< NodePtr > getLatestNews(int nb)
static WsApplication * wsInstance()
Facility function to access the application object.
Definition: WsApplication.h:39
#define NB_NEWS
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
#define LOG
Definition: WsLogger.h:22
void onViewSelectionChanged()
WsLatestNews2(Wt::WContainerWidget *parent=0)
const boost::any & option(const std::string &attribute) const
Get an options value.
Definition: WsOption.cpp:62
std::string get(const std::string &section, const std::string &id, const std::string &def)
Wt::WTableView * m_pTableView
Definition: WsLatestNews2.h:33
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
#define ROOT
static WsGlobalProperties * instance()
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
Abstract Node class.
User class.