Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsContentButtonsBar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-Today Guy Deleeuw
3  *
4  * See the LICENSE file for terms of use.
5 */
6 
7 #include <stdio.h>
8 
9 #include <Wt/WTheme>
10 #include <Wt/WLabel>
11 #include <Wt/WRegExpValidator>
12 
13 #include "WsApplication.h"
14 #include "WsContentButtonsBar.h"
15 
17 
18 using namespace Wt;
19 
21  : gdToolbar(parent), m_pDialog(0), m_pLE_Name(0), m_pButOk(0), m_bDebug(true)
22 {
23  if ( WString::tr("byObjectStyleSheet").narrow() == "true" )
24  wApp->useStyleSheet(wApp->theme()->resourcesUrl() + "wittyshare/Css/WsContentButtonBar.css");
25  addStyleClass("WsContentButtonsBar");
26  WsApp->setContentButtonsBar(this);
27  WsUser* pUser = WsApp->wsUser();
28  gdToolbarItem* pTbItem = 0;
29  if ( pUser->isAdministrator() || pUser->isEditor() ) {
30  pTbItem = addToolbarItem(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/edit.png", "", "Edit current node !");
31  pTbItem->setUrl("/Edit");
32  pTbItem->clicked().connect(SLOT(this, WsContentButtonsBar::doMenuEditPage));
33  pTbItem = addToolbarItem(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/folder_add.png", "", "Create a new folder");
34  pTbItem->clicked().connect(SLOT(this, WsContentButtonsBar::doFolderNew));
35  pTbItem = addToolbarItem(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/file_add.png", "", "Create a new file");
36  pTbItem->clicked().connect(SLOT(this, WsContentButtonsBar::doFileNew));
37  pTbItem = addToolbarItem(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/folder_delete.png", "", "Delete current node");
38  pTbItem->clicked().connect(SLOT(this, WsContentButtonsBar::doDeleteNode));
39  pTbItem = addToolbarItem(wApp->theme()->resourcesUrl() + "gdwtcore/Icons/file_upload.png", "", "Upload a file");
40  pTbItem->setUrl("/FileUpload");
41  pTbItem->clicked().connect(SLOT(this, WsContentButtonsBar::doFileUpload));
42  }
43  for (int iModule = 0; iModule < WsApp->WsModules().modules().size(); ++iModule) {
44  WsModule* curModule = WsApp->WsModules().modules()[iModule]->module;
45  curModule->createContentsMenuBar(this);
46  }
47 }
48 
50 {
51  // for (int iModule = 0; iModule < WsApp->WsModules().modules().size(); ++iModule)
52  // {
53  // WsModule* curModule = WsApp->WsModules().modules()[iModule]->module;
54  // curModule->destroyContentsMenuBar();
55  // }
56 }
57 
58 void WsContentButtonsBar::doMenuEditPage(gdToolbarItem* pTbItem, WMouseEvent ev)
59 {
60  std::string str = pTbItem->url() + wApp->internalPath();
61  std::string id = "";
62  WsUser* pUser = WsApp->wsUser();
63  //Test if path is a file or a directory
64  NodePtr pNode = pUser->getAccessRoot()->eatPath(wApp->internalPath());
65  if(!pNode.get()) return;
66 
67  if(!pNode.get()->isDirectory()){
68  int ret = WsApp->wsUser()->isLocked(wApp->internalPath(), id);
69  if (ret != 1) {
70  WMessageBox::show("Warning", "The file is currently locked by \"" + id + "\". Please try again later.", Ok);
71  return;
72  }
73  }
74  setNewInternalPath(pTbItem->url(), WsApp->WsModules().pathWithoutPrefix(wApp->internalPath()), true);
75 }
76 
77 void WsContentButtonsBar::doFolderNew(gdToolbarItem* pTbItem, WMouseEvent ev)
78 {
79  m_pDialog = new WDialog("New Folder");
80  m_pDialog->finished().connect(SLOT(this, WsContentButtonsBar::doEndFolderNew));
81  Wt::WRegExpValidator* validator = new Wt::WRegExpValidator("[a-zA-Z0-9._& ]{2,48}");
82  m_pLE_Name = new Wt::WLineEdit(m_pDialog->contents());
83  m_pLE_Name->setValidator(validator);
84  m_pLE_Name->keyWentUp().connect(SLOT(this, WsContentButtonsBar::doFolderNameChanged));
85  m_pLE_Name->enterPressed().connect(SLOT(this, WsContentButtonsBar::doFolderEnterPressed));
86  m_pLE_Name->setTextSize(16);
87  m_pLE_Name->setFocus(true);
88  WLabel* label = new Wt::WLabel(" : Name of the new Folder ", m_pDialog->contents());
89  label->setBuddy(m_pLE_Name);
90  m_pButOk = new Wt::WPushButton("OK", m_pDialog->footer());
91  m_pButOk->clicked().connect(m_pDialog, &Wt::WDialog::accept);
92  m_pButOk->disable();
93  WPushButton* cancel = new Wt::WPushButton("Cancel", m_pDialog->footer());
94  cancel->clicked().connect(m_pDialog, &Wt::WDialog::reject);
95  m_pDialog->show();
96 }
97 
98 void WsContentButtonsBar::doFileNew(gdToolbarItem* pTbItem, WMouseEvent ev)
99 {
100  m_pDialog = new WDialog("New File");
101  m_pDialog->finished().connect(SLOT(this, WsContentButtonsBar::doEndFileNew));
102  Wt::WRegExpValidator* validator = new Wt::WRegExpValidator("[a-zA-Z0-9._& ]{2,48}");
103  m_pLE_Name = new Wt::WLineEdit(m_pDialog->contents());
104  m_pLE_Name->setValidator(validator);
105  m_pLE_Name->keyWentUp().connect(SLOT(this, WsContentButtonsBar::doFileNameChanged));
106  m_pLE_Name->enterPressed().connect(SLOT(this, WsContentButtonsBar::doFileEnterPressed));
107  m_pLE_Name->setTextSize(16);
108  m_pLE_Name->setFocus(true);
109  WLabel* label = new Wt::WLabel(" : Name of the new File ", m_pDialog->contents());
110  label->setBuddy(m_pLE_Name);
111  m_pButOk = new Wt::WPushButton("OK", m_pDialog->footer());
112  m_pButOk->clicked().connect(m_pDialog, &Wt::WDialog::accept);
113  m_pButOk->disable();
114  WPushButton* cancel = new Wt::WPushButton("Cancel", m_pDialog->footer());
115  cancel->clicked().connect(m_pDialog, &Wt::WDialog::reject);
116  m_pDialog->show();
117 }
118 
120 {
121  if ( m_pLE_Name->validate() )
122  m_pDialog->accept();
123 }
124 
126 {
127  if ( m_pLE_Name->validate() )
128  m_pDialog->accept();
129 }
130 
132 {
133  m_pButOk->setDisabled(m_pLE_Name->validate() != Wt::WValidator::Valid);
134 }
135 
137 {
138  m_pButOk->setDisabled(m_pLE_Name->validate() != Wt::WValidator::Valid);
139 }
140 
142 {
143  if ( m_pDialog->result() == Wt::WDialog::Accepted ) {
144  std::string newName = m_pLE_Name->text().toUTF8();
145  boost::algorithm::replace_all(newName, "&amp;", "&");
146  std::string currentPath = WsApp->WsModules().pathWithoutPrefix(wApp->internalPath());
147  boost::algorithm::replace_all(currentPath, "&amp;", "&");
148  /* Remove useless / */
149  if (currentPath == "/")
150  currentPath = "";
151 
152  WsUser* pUser = WsApp->wsUser();
153  //Test if path is a file or a directory
154  NodePtr pNode = pUser->getAccessRoot()->eatPath(currentPath);
155  if(!pNode.get()) return;
156 
157  if(!pNode.get()->isDirectory())
158  currentPath = path(currentPath).parent_path().string();
159 
160  std::string path2Create = currentPath + "/" + newName;
161  pUser->createNode(path2Create, WsUser::Directory);
162  setNewInternalPath("/Edit" , path2Create, true);
163  }
164  delete m_pDialog;
165  m_pButOk = 0;
166  m_pLE_Name = 0;
167  m_pDialog = 0;
168 }
169 
171 {
172  if ( m_pDialog->result() == Wt::WDialog::Accepted ) {
173  std::string newName = m_pLE_Name->text().toUTF8();
174  boost::algorithm::replace_all(newName, "&amp;", "&");
175  boost::filesystem::path pathName(newName);
176  if ( pathName.extension().string() == "" ) newName += ".fhtml";
177  std::string currentPath = WsApp->WsModules().pathWithoutPrefix(wApp->internalPath());
178  boost::algorithm::replace_all(currentPath, "&amp;", "&");
179  WsUser* pUser = WsApp->wsUser();
180  //Test if path is a file or a directory
181  NodePtr pNode = pUser->getAccessRoot()->eatPath(currentPath);
182  if(!pNode.get()) return;
183 
184  if(!pNode.get()->isDirectory())
185  currentPath = path(currentPath).parent_path().string();
186 
187  std::string path2Create = currentPath + "/" + newName;
188  boost::algorithm::replace_all(path2Create, "//", "/");
189  pUser->createNode(path2Create, WsUser::File);
190  setNewInternalPath("/Edit" , path2Create, true);
191  }
192  delete m_pDialog;
193  m_pButOk = 0;
194  m_pLE_Name = 0;
195  m_pDialog = 0;
196 }
197 
198 void WsContentButtonsBar::doDeleteNode(gdToolbarItem* pTbItem, Wt::WMouseEvent ev)
199 {
200  WsUser* pUser = WsApp->wsUser();
201  //std::string newPath = wApp->internalPath();
202  std::string newPath = WsApp->WsModules().pathWithoutPrefix(wApp->internalPath());
203  boost::algorithm::replace_all(newPath, "&amp;", "&");
204  WMessageBox* pBox = new WMessageBox("Confirm", "Delete : " + newPath, Wt::Information, Wt::Yes | Wt::No);
205  pBox->buttonClicked().connect(boost::bind(&WsContentButtonsBar::doMBoxRespons, this, pBox));
206  pBox->show();
207 }
208 
209 void WsContentButtonsBar::doMBoxRespons(WMessageBox* pBox)
210 {
211  int res = pBox->buttonResult();
212  delete pBox;
213  if ( res != Wt::Yes ) return;
214  WsUser* pUser = WsApp->wsUser();
215  //std::string newPath = wApp->internalPath();
216  std::string newPath = WsApp->WsModules().pathWithoutPrefix(wApp->internalPath());
217  boost::algorithm::replace_all(newPath, "&amp;", "&");
218  pUser->deleteNode(newPath);
219  wApp->setInternalPath(path(newPath).parent_path().string(), true);
220 }
221 
222 void WsContentButtonsBar::doFileUpload(gdToolbarItem* pTbItem, WMouseEvent ev)
223 {
224  WsUser* pUser = WsApp->wsUser();
225  std::string sDocumentRoot = pUser->getRootPath(); // /var/www/demo_site
226  std::string fullPath = sDocumentRoot + wApp->internalPath();
227  std::string currentPath = wApp->internalPath();
228  boost::algorithm::replace_all(fullPath, "&amp;", "&");
229  //Test if path is a file or a directory
230 
231  NodePtr pNode = pUser->getAccessRoot()->eatPath(wApp->internalPath());
232  if(!pNode.get()) return;
233 
234  if(!pNode.get()->isDirectory())
235  currentPath = path(currentPath).parent_path().string();
236 
237  if ( pUser->isAdministrator() || pUser->isEditor() ) {
238  setNewInternalPath(pTbItem->url() , currentPath, true);
239  }
240 }
241 
242 void WsContentButtonsBar::setNewInternalPath(std::string sub, std::string url, bool refresh)
243 {
244  if (sub == wApp->internalPath()) {
245  sub = WsApp->WsModules().pathWithoutPrefix(sub);
246  } else {
247  boost::algorithm::replace_all(url, sub, "");
248  }
249  std::string newPath = sub + url;
250  wApp->setInternalPath(newPath, refresh);
251 }
boost::shared_ptr< WsAbstractNode > NodePtr
WsContentButtonsBar(Wt::WContainerWidget *parent=0)
void doFileUpload(gdToolbarItem *pTbItem, Wt::WMouseEvent ev)
void doMBoxRespons(Wt::WMessageBox *pBox)
void doDeleteNode(gdToolbarItem *pTbItem, Wt::WMouseEvent ev)
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
Wt::WLineEdit * m_pLE_Name
void doFileNew(gdToolbarItem *pTbItem, Wt::WMouseEvent ev)
void doMenuEditPage(gdToolbarItem *pTbItem, Wt::WMouseEvent ev)
void doFolderNew(gdToolbarItem *pTbItem, Wt::WMouseEvent ev)
Wt::WPushButton * m_pButOk
bool isEditor()
Definition: WsUser.cpp:179
virtual Wt::WWidget * createContentsMenuBar(Wt::WContainerWidget *parent=0) const =0
Create the functionalities.
const std::string getRootPath()
return the root path of the filesystem tree, example : /var/www/demo_site
Definition: WsUser.cpp:55
int createNode(const string &path, NodeType type)
create a directory or File. If the node is a WsDirNode than it will be only accessible to the Admin a...
Definition: WsUser.cpp:105
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
void setNewInternalPath(std::string sub, std::string url, bool refresh=false)
bool isAdministrator()
Definition: WsUser.cpp:184
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
int deleteNode(const string &path)
delete a node. The user must be an Admin on editor to remove the node In case of a WsDirNode...
Definition: WsUser.cpp:115