Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsModMenuImages.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 <dlfcn.h>
8 #include <stdio.h>
9 
10 #include <iostream>
11 
12 #include <boost/algorithm/string/replace.hpp>
13 #include <boost/filesystem.hpp>
14 
15 #include <Wt/WLogger>
16 #include <Wt/WText>
17 
18 #include <Logger/WsLogger.h>
19 
20 #include <Main/WsApplication.h>
21 
22 // TODO : pas top je sais
24 
25 #include "WsModMenuImages.h"
26 
27 extern "C" {
29  {
30  void* hndl = dlopen("libwt.so", RTLD_NOW | RTLD_GLOBAL);
31  if ( hndl == NULL ) {
32  fprintf(stderr, "cannot load libwt.so shared library! %s\n", dlerror());
33  return;
34  }
35  }
36 }
37 
38 using namespace Wt;
39 
40 // Class WsMenuImages
41 // ==================
42 WsMenuImages::WsMenuImages(WContainerWidget* parent)
43  : WContainerWidget(parent), m_nStartImage(0), m_nMaxImages(3), m_pTimer(0)
44 {
45  addStyleClass("WsMenuImages");
46 }
47 
49 {
50 }
51 
53 {
54  int nMaxImages = asNumber(option("maxImages"));
55  if ( nMaxImages > 0 )
56  setMaxImages(nMaxImages);
57  WContainerWidget::load();
58  buildVector();
59  buildMenu();
60  if ( m_vDirectories.size() > m_nMaxImages ) {
61  // On ajoute le timer au widget, les objets de ce type ne sont pas détruits par un clear();
62  m_pTimer = new WTimer(this);
63  m_pTimer->setInterval(5000);
64  m_pTimer->timeout().connect(SLOT(this, WsMenuImages::doTimeout));
65  m_pTimer->start();
66  }
67 }
68 
69 // TODO : voir avec Koen si pas moyen de stocker l'objet, actu 2 x la construction de pImg il faudrait qu'il ne soit pas detruit lors d'1 clear()
71 {
72  std::string endPath(GlobalConfig::PathToImages); // "/ws.res/images
73  WsUser* pUser = WsApp->wsUser();
74  std::string sWithoutPrefix = WsApp->WsModules().pathWithoutPrefix(WsApp->internalPath());
75  NodePtr startNode = pUser->getAccessRoot();
76  if ( !startNode.get() ) {
77  WApplication::instance()->log("ERROR") << "WsMenuImages::buildVector : startNode is NULL !";
78  return;
79  }
80  NodePtr selNode = startNode.get()->eatPath(sWithoutPrefix);
81  if ( !selNode.get() ) {
82  WApplication::instance()->log("ERROR") << "WsMenuImages::buildVector : selNode is NULL !";
83  return;
84  }
85  if ( selNode.get()->isRegularFile() ) {
86  selNode = selNode.get()->getParent();
87  if ( !selNode.get() ) {
88  WApplication::instance()->log("ERROR") << "WsMenuImages::buildVector : selected node parent is NULL !" << sWithoutPrefix;
89  return;
90  }
91  }
92  std::vector<NodePtr> dirNode = startNode.get()->getDirectories();
93  for (std::vector<NodePtr>::iterator it = dirNode.begin(); it != dirNode.end(); ++it) {
94  NodePtr curNode = *it;
95  // TODO : voir avec Ben
96  // if ( curNode.get()->getProperties().get()->get("global", "in_menu"Images, DefaultProperties::InMenuImages) != "true" ) continue;
97  std::string curDir = curNode.get()->getFullPath().string() + endPath;
98  if ( !boost::filesystem::exists(curDir) ) continue;
99  // TODO : necessaire de passer par le module ? (pour les options ?)
100  WsImages2* pImg = new WsImages2(); //dynamic_cast<WsImages2*>(WsApp->WsModules().module("WsModImages2")->createContents());
101  pImg->setOptions(options());
102  std::string curPathUrl = curNode.get()->getFullPath().string();
103  boost::algorithm::replace_first(curPathUrl, wApp->docRoot(), "");
104  pImg->setOption("imagesPath", std::string(curPathUrl + endPath));
105  pImg->build();
106  if ( !pImg->count() ) {
107  delete pImg;
108  continue;
109  }
110  m_vDirectories.push_back(curPathUrl + endPath);
111  // m_vDisplayNames.push_back(curNode.get()->getDisplayName());
112  delete pImg;
113  }
114 }
115 
117 {
118  if ( (m_nStartImage + 1) >= (m_vDirectories.size() - m_nMaxImages) ) m_nStartImage = 0;
119  else ++m_nStartImage;
120  clear();
121  buildMenu();
122 }
123 
125 {
126  // TODO : check 2 img or 4 with m_nMaxImages 3
127  // TODO : add a internalPathChanged to reload the menu with the correct images for the new path
128  if ( m_pTimer )
129  m_pTimer->stop();
130  int maxImg = m_nMaxImages;
131  if ( maxImg > m_vDirectories.size() ) maxImg = m_vDirectories.size();
132  for (int countDir = m_nStartImage; countDir < maxImg; ++countDir) {
133  if ( countDir > m_vDirectories.size() ) {
134  break;
135  }
136  WsImages2* pImg = new WsImages2(); //dynamic_cast<WsImages2*>(WsApp->WsModules().module("WsModImages2")->createContents());
137  pImg->setOptions(options());
138  pImg->setOption("imagesPath", m_vDirectories[countDir]);
139  pImg->setOption("useImageTitle", std::string("true"));
140  pImg->build();
141  // WContainerWidget* cwImage = new WContainerWidget();
142  // std::string title = m_vDisplayNames[countDir];
143  // boost::algorithm::replace_all(title, "&", "&amp;");
144  // WText* pTextTitle = new WText(title);
145  // pTextTitle->addStyleClass("WsMenuImagesTitle");
146  // cwImage->addWidget(pTextTitle);
147  // cwImage->addWidget(pImg);
148  // addWidget(cwImage);
149  addWidget(pImg);
150  }
151  if ( m_pTimer )
152  m_pTimer->start();
153 }
154 
155 void WsMenuImages::setMaxImages(int nMaxImages)
156 {
157  m_nMaxImages = nMaxImages;
158 }
159 
160 
161 
162 // Class WsModMenuImages
163 // =====================
165  : WsModule()
166 {
167 }
168 
170 {
171 }
172 
173 WWidget* WsModMenuImages::createContentsMenuBar(WContainerWidget* parent) const
174 {
175  return 0;
176 }
177 
178 WWidget* WsModMenuImages::createContents(WContainerWidget* parent) const
179 {
180  WsMenuImages* pMenuImg = new WsMenuImages(parent);
181  pMenuImg->setOptions(options());
182  return pMenuImg;
183 }
184 
185 WsEditorWidget* WsModMenuImages::createEditor(WContainerWidget* parent) const
186 {
187  return 0;
188 }
189 
190 WWidget* WsModMenuImages::createAdmin(WContainerWidget* parent) const
191 {
192  return 0;
193 }
194 
196 {
197  return true;
198 }
199 
200 std::string WsModMenuImages::description() const
201 {
202  return "WsModMenuImages wittyShare module";
203 }
204 
const std::string PathToImages
void setMaxImages(int nMaxImages)
Set the number of images in the menu. If the number of images is more than this value a timer rotate ...
boost::shared_ptr< WsAbstractNode > NodePtr
Wt::WWidget * createContentsMenuBar(Wt::WContainerWidget *parent=0) const
Create the functionalities.
void build()
Build the vector.
Definition: WsImages2.cpp:96
a wittyShare module
bool saveEditor() const
Wt::WTimer * m_pTimer
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
int count()
Return the number of images.
Definition: WsImages2.cpp:172
void setOption(const std::string &attribute, boost::any value)
Set an options if previously set, update the value.
Definition: WsOption.cpp:47
std::vector< std::string > m_vDirectories
std::string description() const
Return the description of the module.
void WsModMenuImagesInit(void)
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
void setOptions(const std::vector< WsOption > &vOptions)
Set all options.
Definition: WsOption.cpp:57
WsEditorWidget * createEditor(Wt::WContainerWidget *parent=0) const
Create the contents for an editor (create a view of options).
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
Wt::WWidget * createAdmin(Wt::WContainerWidget *parent=0) const
Create the contents for an administrator.
Wt::WWidget * createContents(Wt::WContainerWidget *parent=0) const
Create the contents.
WsMenuImages(Wt::WContainerWidget *parent=0)
CTor.
a wittyShare class that render images randomly