Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsModDirectoryView2.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsModDirectoryView2.cpp
4  *
5  * Description: Zip Wittyshare plugin
6  *
7  * Created: 03/07/12 11:07:15
8  *
9  * Author: Benoit Daccache, ben.daccache@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/WString>
23 #include <Wt/WDateTime>
24 #include <Wt/WStandardItemModel>
25 #include <Wt/WTheme>
26 
27 #include <gdcore/gdCore.h>
28 
29 #include <gdwtcore/gdWTextDelegate.h>
30 
32 #include <User/WsUser.h>
34 #include <Logger/WsLogger.h>
35 
36 #include <WsModule/WsModule.h>
37 
38 #include "WsModDirectoryView2.h"
39 
40 using namespace Wt;
41 
42 extern "C" {
44  {
45  void* hndl = dlopen("libwt.so", RTLD_NOW | RTLD_GLOBAL);
46  if ( hndl == NULL ) {
47  fprintf(stderr, "cannot load wt.so shared library! %s\n", dlerror());
48  return;
49  }
50  }
51 }
52 
53 
54 
56 { }
57 
58 WsDirectoryItem::WsDirectoryItem(const WString& text) : WStandardItem(text)
59 { }
60 
62 {
63  m_node = pNode;
64 }
65 
67 {
68  return m_node;
69 }
70 
71 
72 
73 
74 WsDirectoryView2::WsDirectoryView2(WContainerWidget* parent)
75  : WTableView(parent)
76 {
77  addStyleClass("WsDirectoryView2");
78 }
79 
81 {
82 }
83 
85 {
86  WTableView::load();
87  WStandardItemModel* pModelView = new WStandardItemModel(0, 4, this);
88  pModelView->setHeaderData(0, WString::tr("WsDirectoryView2-Name"));
89  pModelView->setHeaderData(1, WString::tr("WsDirectoryView2-Desc"));
90  pModelView->setHeaderData(2, WString::tr("WsDirectoryView2-Size"));
91  pModelView->setHeaderData(3, WString::tr("WsDirectoryView2-Date"));
92  pModelView->setSortRole(UserRole);
94  std::string path = asString(option("rootPath")).narrow();
95  if ( path.size() < 1 )
96  path = WsApplication::wsInstance()->internalPath();
97  std::string sWithoutPrefix = WsApp->WsModules().pathWithoutPrefix(path);
98  //std::vector<NodePtr> vDirContent = user->getDirContents(path);
99  NodePtr accessTree = user->getAccessRoot();
100  if (!accessTree.get()) {
101  wApp->log("error") << "WsDirectoryView2::load() : accessTree is NULL";
102  return;
103  }
104  NodePtr tmp = accessTree.get()->eatPath(path);
105  if (!tmp) {
106  wApp->log("error") << "WsDirectoryView2::load() : eatPath returned null";
107  return;
108  }
109  std::vector<NodePtr> vDirContent = tmp.get()->getAll();
110  int rc = vDirContent.size();
111  long rowsCount = asNumber(option("rowsCount"));
112  if ( rowsCount != -1 ) rc = rowsCount;
113  for (int row = 0; row < rc; ++row) {
114  std::string sName = vDirContent[row].get()->getName();
115  std::string sDesc = vDirContent[row].get()->getProperties().get()->get( "global", "short_description", "");
116  uintmax_t lSize = vDirContent[row].get()->getSize();
117  time_t tTime = vDirContent[row].get()->getModifyDate();
118  std::string strDate;
119  struct tm ttm;
120  char c[100];
121  localtime_r(&tTime, &ttm);
122  strftime(c, 100, "%Y-%m-%d", &ttm);
123  strDate = c;
124  std::vector<WStandardItem*> pRow;
125  WsDirectoryItem* newItem = new WsDirectoryItem(WString::fromUTF8(sName));
126  newItem->setNode(vDirContent[row]);
127  if ( vDirContent[row].get()->isDirectory() ) {
128  newItem->setData("1" + vDirContent[row].get()->getPath().string(), UserRole); // le path complet du parent
129  newItem->setIcon(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/folder.png");
130  } else {
131  newItem->setData("2" + vDirContent[row].get()->getPath().string(), UserRole); // le path complet du parent
132  newItem->setIcon(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/file.png");
133  }
134  pRow.push_back(newItem);
135  WsDirectoryItem* strDesc = new WsDirectoryItem();
136  strDesc->setNode(vDirContent[row]);
137  strDesc->setData(sDesc, UserRole); // le path complet du parent
138  pRow.push_back(strDesc);
139  std::string strSize = boost::lexical_cast<std::string>(lSize);
140  WsDirectoryItem* colSize = new WsDirectoryItem(strSize);
141  colSize->setData(lSize, UserRole);
142  pRow.push_back(colSize);
143  WsDirectoryItem* colDate = new WsDirectoryItem(strDate);
144  colDate->setData(strDate, UserRole);
145  pRow.push_back(colDate);
146  pModelView->invisibleRootItem()->appendRow(pRow);
147  }
148  setModel(pModelView);
149  setColumnResizeEnabled(true);
150  setSortingEnabled(true);
151  setAlternatingRowColors(true);
152  setSelectionMode(ExtendedSelection);
153  setColumnAlignment(2, AlignRight);
154  setColumnAlignment(3, AlignRight);
155  long lRowHeight = asNumber(option("lRowHeight"));
156  if ( lRowHeight < 0 )
157  setRowHeight(38);
158  else
159  setRowHeight(lRowHeight);
160  if ( asString(option("hideHeader")).narrow() == "true" )
161  setHeaderHeight(0);
162  std::string columnsWidth = asString(option("columnsWidth")).narrow();
163  if ( columnsWidth.size() < 1 ) {
164  setColumnWidth(0, 180);
165  setColumnWidth(1, 300);
166  } else {
167  std::vector<std::string> sVectColumnsWidth;
168  gd_core_string2vector(sVectColumnsWidth, columnsWidth.c_str(), ",");
169  for (int iCol = 0; iCol < sVectColumnsWidth.size(); ++iCol) {
170  double width = asNumber(sVectColumnsWidth[iCol]);
171  if ( width != -1 )
172  setColumnWidth(iCol, width);
173  }
174  }
175  std::string columnsToHide = asString(option("hideColumns")).narrow();
176  std::vector<std::string> sVectColumnsToHide;
177  gd_core_string2vector(sVectColumnsToHide, columnsToHide.c_str(), ",");
178  for (int iCol = 0; iCol < sVectColumnsToHide.size(); ++iCol) {
179  int col = asNumber(sVectColumnsToHide[iCol]);
180  setColumnHidden(col, true);
181  }
182  gdWTextDelegate* delegate2 = new gdWTextDelegate(this);
183  delegate2->setRowHeight(rowHeight().value() - 2);
184  setItemDelegateForColumn(1, delegate2);
185  selectionChanged().connect(SLOT(this, WsDirectoryView2::onViewSelectionChanged));
186  // pModelView->sort(0);
187 }
188 
190 {
191  WModelIndexSet pSet = selectedIndexes();
192  WModelIndexSet::iterator it = pSet.begin();
193  if ( pSet.empty() ) return;
194  WModelIndex idx = *it;
195  std::string str = asString(idx.data(UserRole)).toUTF8();
196  if ( str[0] == '1' )
197  boost::algorithm::replace_first(str, "1", "");
198  else
199  boost::algorithm::replace_first(str, "2", "");
200  wApp->setInternalPath(str, true);
201 }
202 
203 
204 
205 
206 
207 
208 
209 
210 
212  : WsModule()
213 {
214  wApp->messageResourceBundle().use(wApp->docRoot() + wApp->resourcesUrl() + "WsModDirectoryView2/Transl/WsModDirectoryView2");
215 }
216 
218 {
219 }
220 
221 WWidget* WsModDirectoryView2::createContentsMenuBar(WContainerWidget* parent) const
222 {
223  return 0;
224 }
225 
226 WWidget* WsModDirectoryView2::createContents(WContainerWidget* parent) const
227 {
228  WsDirectoryView2* dirView = new WsDirectoryView2(parent);
229  dirView->setOptions(options());
230  return dirView;
231 }
232 
233 WsEditorWidget* WsModDirectoryView2::createEditor(WContainerWidget* parent) const
234 {
235  return 0;
236 }
237 
238 WWidget* WsModDirectoryView2::createAdmin(WContainerWidget* parent) const
239 {
240  return 0;
241 }
242 
244 {
245  return "WsModDirectoryView2 wittyShare module";
246 }
247 
Global properties.
WsUser * wsUser()
boost::shared_ptr< WsAbstractNode > NodePtr
static WsApplication * wsInstance()
Facility function to access the application object.
Definition: WsApplication.h:39
void setNode(NodePtr pNode)
const std::vector< WsOption > & options() const
Get all options.
Definition: WsOption.cpp:70
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
WsEditorWidget * createEditor(Wt::WContainerWidget *parent=0) const
Create the contents for an editor (create a view of options).
const boost::any & option(const std::string &attribute) const
Get an options value.
Definition: WsOption.cpp:62
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
WsDirectoryView2(Wt::WContainerWidget *parent=0)
Wt::WWidget * createContents(Wt::WContainerWidget *parent=0) const
Create the contents.
Wt::WWidget * createAdmin(Wt::WContainerWidget *parent=0) const
Create the contents for an administrator.
void setOptions(const std::vector< WsOption > &vOptions)
Set all options.
Definition: WsOption.cpp:57
Wt::WWidget * createContentsMenuBar(Wt::WContainerWidget *parent=0) const
Create the functionalities.
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
std::string description() const
Return the description of the module.
void WsModDirectoryView2Init(void)
Abstract Node class.
User class.