Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsSearchView.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-Today G. Deleeuw
3  *
4  * See the LICENSE file for terms of use.
5  */
6 
7 #include <stdio.h>
8 
9 #include <boost/algorithm/string/replace.hpp>
10 #include <boost/lexical_cast.hpp>
11 
12 #include <iostream>
13 #include <time.h>
14 
15 #include <Wt/WHBoxLayout>
16 #include <Wt/WVBoxLayout>
17 #include <Wt/WText>
18 #include <Wt/WItemDelegate>
19 #include <Wt/WTheme>
20 
21 #include <gdcore/gdCore.h>
22 
23 #include <gdwtcore/gdWTextDelegate.h>
24 
25 #include <Search/WsSearch.h>
26 
27 #include "WsSearchView.h"
28 #include "WsApplication.h"
29 
30 using namespace Wt;
31 
32 WsSearchItem::WsSearchItem() : WStandardItem(), m_node(0)
33 { }
34 
35 WsSearchItem::WsSearchItem(const WString& text) : WStandardItem(text), m_node(0)
36 { }
37 
39 {
40  m_node = pNode;
41 }
42 
44 {
45  return m_node;
46 }
47 
48 
49 
50 
51 
52 WsSearchView::WsSearchView(std::string sSearch, WContainerWidget* parent)
53  : WTableView(parent), m_pModelView(0), m_bLogSearch(true), m_wNavBar(0)
54 {
55  //if ( WsLayoutProperties::instance()->get("global", "by_object_stylesheet", "false") == "true" )
56  if ( WString::tr("byObjectStyleSheet").narrow() == "true" )
57  wApp->useStyleSheet(wApp->theme()->resourcesUrl() + "wittyshare/Css/WsSearchView.css");
58  parent->addStyleClass("WsSearchView");
59  if (WsLayoutProperties::instance()->get("sitemap", "log", "false") == "true")
60  m_bLogSearch = true;
61  wApp->messageResourceBundle().use(wApp->docRoot() + wApp->resourcesUrl() + "wittyshare/Transl/WsSearchView");
62  m_pModelView = new WStandardItemModel(0, 4);
63  m_pModelView->setHeaderData(0, WString::tr("WsSearchView-Name"));
64  m_pModelView->setHeaderData(1, WString::tr("WsSearchView-Desc"));
65  m_pModelView->setHeaderData(2, WString::tr("WsSearchView-Size"));
66  m_pModelView->setHeaderData(3, WString::tr("WsSearchView-Date"));
67  m_pModelView->setSortRole(UserRole);
68  WsUser* pUser = WsApp->wsUser();
69  m_sRootPath = pUser->getRootPath(); // /var/www/demo_site
70  NodePtr pRootNode = pUser->getAccessRoot();
71  if ( !pRootNode.get() ) {
72  wApp->log("notice") << "WsSearchView::WsSearchView() : getAccessRoot() is null";
73  return;
74  }
75  std::vector<WsResultItem> vResSearch = pUser->getSearchResults(sSearch);
76  for (int row = 0; row < vResSearch.size(); ++row) {
77  NodePtr pNode = pRootNode.get()->eatPath(vResSearch[row].getPath().string());
78  if ( !pNode.get() ) {
79  wApp->log("notice") << "WsSearchView::WsSearchView() : Path = " << ", getAccessRoot() eatPath is null";
80  continue;
81  }
82  if ( pNode.get()->isDirectory() ) continue;
83  if ( pNode.get()->getProperties().get()->get("global", "in_view", "true") != "true" ) continue;
84  // split sSearch separator space, then replace sDesc :"?<splitX>?" by "<b>splitX</b>"
85  std::string sName = vResSearch[row].getName(true);
86  std::string sDesc = vResSearch[row].getBody();
87  long lSize = vResSearch[row].getSize();
88  time_t tTime = vResSearch[row].getModifyDate();
89  // wApp->log("notice") << "WsSearchView::WsSearchView() tTime = " << tTime;
90  std::vector<std::string> searchWords;
91  gd_core_string2vector(searchWords, sSearch.c_str(), " ");
92  for (int iWord = 0; iWord < searchWords.size(); ++iWord) {
93  boost::algorithm::replace_all(sDesc, "\u0002", "<strong>"); // u0002 = start text
94  boost::algorithm::replace_all(sDesc, "\u0003", "</strong>"); // u0003 = end text
95  }
96  std::string strDate;
97  struct tm ttm;
98  char c[100];
99  localtime_r(&tTime, &ttm);
100  strftime(c, 100, "%Y-%m-%d", &ttm);
101  strDate = c;
102  std::vector<WStandardItem*> pRow;
103  WsSearchItem* newItem = new WsSearchItem(WString::fromUTF8(sName));
104  newItem->setNode(&vResSearch[row]);
105  newItem->setData(vResSearch[row].getPath().string(), UserRole); // le path complet du parent
106  newItem->setIcon(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/file.png");
107  pRow.push_back(newItem);
108  WsSearchItem* strDesc = new WsSearchItem();
109  strDesc->setNode(&vResSearch[row]);
110  strDesc->setData(sDesc, UserRole); // le path complet du parent
111  pRow.push_back(strDesc);
112  std::string strSize = boost::lexical_cast<std::string>(lSize);
113  WsSearchItem* colSize = new WsSearchItem(strSize);
114  colSize->setData(lSize, UserRole);
115  pRow.push_back(colSize);
116  WsSearchItem* colDate = new WsSearchItem(strDate);
117  colDate->setData(strDate, UserRole);
118  pRow.push_back(colDate);
119  m_pModelView->invisibleRootItem()->appendRow(pRow);
120  }
121  // m_wNavBar = createPageNavigationBar();
122  setModel(m_pModelView);
123  setColumnResizeEnabled(true);
124  setSortingEnabled(true);
125  setAlternatingRowColors(true);
126  setSelectionMode(ExtendedSelection);
127  setColumnAlignment(2, AlignRight);
128  setRowHeight(38);
129  setColumnWidth(0, 300);
130  setColumnWidth(1, 470);
131  setColumnWidth(2, 80);
132  setColumnWidth(3, 80);
133  setHeight((rowHeight() * 50)); // 50 rows, TODO push this object as a module and set this as option
134  WItemDelegate* delegate = new WItemDelegate(parent);
135  delegate->setTextFormat("%s");
136  setItemDelegate(delegate);
137  gdWTextDelegate* delegate2 = new gdWTextDelegate(parent);
138  delegate2->setRowHeight(rowHeight().value() - 2);
139  setItemDelegateForColumn(1, delegate2);
140  doubleClicked().connect(SLOT(this, WsSearchView::onViewDblClick));
141  selectionChanged().connect(SLOT(this, WsSearchView::onViewSelectionChanged));
142 }
143 
145 {
146  if ( m_pModelView ) {
147  delete m_pModelView;
148  m_pModelView = 0;
149  }
150 }
151 
153 {
154  return m_wNavBar;
155 }
156 
157 void WsSearchView::onViewDblClick(WModelIndex idx, WMouseEvent mouseEvent)
158 {
159  WModelIndex idxcol0;
160  if ( idx.column() != 0 ) {
161  if ( idx.parent().isValid() )
162  idxcol0 = idx.parent().child(idx.row(), 0);
163  else
164  idxcol0 = idx.child(idx.row(), 0);
165  } else
166  idxcol0 = idx;
167  if ( !idxcol0.isValid() ) return;
168  std::string str = asString(idxcol0.data(UserRole)).toUTF8();
169  wApp->setInternalPath(str, true);
170 }
171 
173 {
174  WModelIndexSet pSet = selectedIndexes();
175  WModelIndexSet::iterator it = pSet.begin();
176  if ( pSet.empty() ) return;
177  WModelIndex idx = *it;
178  std::string str = asString(idx.data(UserRole)).toUTF8();
179  wApp->setInternalPath(str, true);
180 }
181 
std::string m_sRootPath
Definition: WsSearchView.h:33
Wt::WStandardItemModel * m_pModelView
Definition: WsSearchView.h:32
boost::shared_ptr< WsAbstractNode > NodePtr
void setNode(WsAbstractNode *pNode)
Contains Abstract metods on nodes and variables.
WsAbstractNode * node()
WsAbstractNode * m_node
Definition: WsSearchView.h:18
std::vector< WsResultItem > getSearchResults(const std::string &terms)
search for all matching results of terms using WsSearch class
Definition: WsUser.cpp:146
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
bool m_bLogSearch
Definition: WsSearchView.h:34
void onViewSelectionChanged()
void onViewDblClick(Wt::WModelIndex, Wt::WMouseEvent)
WsSearchView(std::string sSearch, Wt::WContainerWidget *parent=0)
WWidget * m_wNavBar
Definition: WsSearchView.h:35
const std::string getRootPath()
return the root path of the filesystem tree, example : /var/www/demo_site
Definition: WsUser.cpp:55
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
static WsLayoutProperties * instance()
Get the singleton instance.
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
Wt::WWidget * navBar()