Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsModLinksList.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 #include <Wt/WAnchor>
18 #include <Wt/WImage>
19 #include <Wt/WLink>
20 #include <Wt/WString>
21 
22 #include <gdcore/gdCore.h>
23 
24 #include <Logger/WsLogger.h>
25 #include <User/WsUser.h>
26 
27 #include <Main/WsApplication.h>
28 // TODO : pas top je sais
30 
31 #include "WsModLinksList.h"
32 
33 extern "C" {
34  void WsModLinksListInit(void)
35  {
36  void* hndl = dlopen("libwt.so", RTLD_NOW | RTLD_GLOBAL);
37  if ( hndl == NULL ) {
38  fprintf(stderr, "cannot load libwt.so shared library! %s\n", dlerror());
39  return;
40  }
41  }
42 }
43 
44 using namespace Wt;
45 
46 // Class WsLinksList
47 // ==================
48 WsLinksList::WsLinksList(WContainerWidget* parent)
49  : WContainerWidget(parent)
50 {
51  addStyleClass("WsLinksList");
52 }
53 
55 {
56 }
57 
59 {
60  Wt::WContainerWidget::load();
61  std::string sClass = asString(option("class")).toUTF8();
62  if ( sClass.size() > 0 )
63  addStyleClass(sClass);
64  std::string title = asString(option("title")).toUTF8();
65  if ( title.size() > 0 ) {
66  WText* pText = new WText(title);
67  pText->addStyleClass("WsTitle");
68  addWidget(pText);
69  }
70  std::string linkType = asString(option("linkType")).toUTF8(); // Url, Resource, InternalPath
71  std::string link = asString(option("link")).toUTF8();
72  std::string text = asString(option("text")).toUTF8();
73  std::string icon = asString(option("icon")).toUTF8();
74  std::string iconText = asString(option("iconText")).toUTF8();
75  std::string target = asString(option("target")).toUTF8(); // TargetSelf, TargetThisWindow, TargetNewWindow
76  if ( linkType.size() < 1 ) linkType = "InternalPath";
78  std::string fullRootPath = pUser->getRootPath();
79  std::string rootUrl = fullRootPath;
80  boost::replace_all(rootUrl, wApp->docRoot(), "");
81  std::string path = WsApplication::wsInstance()->internalPath();
82  std::string sWithoutPrefix = WsApp->WsModules().pathWithoutPrefix(path);
83  std::string rootPath = asString(option("rootPath")).narrow();
84  boost::algorithm::replace_all(rootPath, "&amp;", "&");
85  if ( rootPath.size() < 1 ) rootPath = path;
86  NodePtr root = pUser->getAccessRoot();
87  if ( !root ) {
88  LOG(ERROR) << "WsLinksList::load() cannot get root !";
89  return;
90  }
91  NodePtr startNode = root->eatPath(rootPath);
92  if (startNode.get() == 0) {
93  LOG(ERROR) << "WsLinksList::load() cannot eatPath !" << rootPath;
94  return;
95  }
96  std::vector<NodePtr> dirNode = startNode.get()->getFiles();
97  for (std::vector<NodePtr>::iterator it = dirNode.begin(); it != dirNode.end(); ++it) {
98  NodePtr curNode = *it;
99  boost::filesystem::path path(curNode.get()->getPath());
100  if ( ! ( path.extension() == ".jpeg"
101  || path.extension() == ".jpg"
102  || path.extension() == ".png"
103  ) ) continue;
104  if ( curNode.get()->isDirectory() ) continue;
105  std::string pngPath = curNode.get()->getPath().string();
106  boost::filesystem::path pdfFile = pngPath;
107  pdfFile.replace_extension(".pdf");
108  std::string sFileObject = pdfFile.string();
109  if ( sFileObject.size() < 1 ) continue;
110  std::string linkType = "InternalPath";
111  std::string sUrl;
112  NodePtr pNode = root->eatPath(sFileObject);
113  if ( !pNode.get() ) {
114  pdfFile.replace_extension(".zip");
115  sFileObject = pdfFile.string();
116  pNode = root->eatPath(sFileObject);
117  if ( !pNode.get() ) {
118  pdfFile.replace_extension(".url");
119  sFileObject = pdfFile.string();
120  pNode = root->eatPath(sFileObject);
121  if ( !pNode )
122  continue;
123  std::string conct = fullRootPath + sFileObject;
124  std::ifstream f(conct.c_str());
125  std::getline(f, sUrl); // url without CR
126  boost::algorithm::replace_all(sUrl, "link=", "");
127  sFileObject = sUrl;
128  linkType = "Url";
129  }
130  }
131  std::string sShortDesc = pNode.get()->getProperties().get()->get("global", "short_description", "");
132  WsAnchor* pAnchor = dynamic_cast<WsAnchor*>(WsApp->WsModules().module("WsModAnchor")->createContents());
133  pAnchor->setOptions(options());
134  pAnchor->setOption("linkType", linkType);
135  pAnchor->setOption("link", sFileObject);
136  bool bUseExt = false;
137  if ( asString(option("useExt")) == "true" ) bUseExt = true;
138  if ( asString(option("useText")) == "false" )
139  pAnchor->setOption("text", std::string());
140  else
141  pAnchor->setOption("text", pNode.get()->getDisplayName(bUseExt));
142  pAnchor->setOption("icon", std::string(rootUrl + pngPath));
143  if ( asString(option("useText")) == "false" )
144  pAnchor->setOption("iconText", std::string());
145  else
146  pAnchor->setOption("iconText", pNode.get()->getDisplayName(bUseExt));
147  pAnchor->setOption("target", std::string("TargetNewWindow"));
148  if ( asString(option("useShortDesc")) == "false" )
149  pAnchor->setOption("title", std::string());
150  else
151  pAnchor->setOption("title", sShortDesc);
152  // pAnchor->load();
153  addWidget(pAnchor);
154  }
155 }
156 
157 
158 
159 // Class WsModLinksList
160 // =====================
162  : WsModule()
163 {
164 }
165 
167 {
168 }
169 
170 WWidget* WsModLinksList::createContentsMenuBar(WContainerWidget* parent) const
171 {
172  return 0;
173 }
174 
175 WWidget* WsModLinksList::createContents(WContainerWidget* parent) const
176 {
177  WsLinksList* pLinksList = new WsLinksList(parent);
178  pLinksList->setOptions(options());
179  return pLinksList;
180 }
181 
182 WsEditorWidget* WsModLinksList::createEditor(WContainerWidget* parent) const
183 {
184  return 0;
185 }
186 
187 WWidget* WsModLinksList::createAdmin(WContainerWidget* parent) const
188 {
189  return 0;
190 }
191 
193 {
194  return true;
195 }
196 
197 std::string WsModLinksList::description() const
198 {
199  return "WsModLinksList wittyShare module";
200 }
201 
WsUser * wsUser()
boost::shared_ptr< WsAbstractNode > NodePtr
static WsApplication * wsInstance()
Facility function to access the application object.
Definition: WsApplication.h:39
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
#define LOG
Definition: WsLogger.h:22
a wittyShare widget
void setOption(const std::string &attribute, boost::any value)
Set an options if previously set, update the value.
Definition: WsOption.cpp:47
const boost::any & option(const std::string &attribute) const
Get an options value.
Definition: WsOption.cpp:62
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
void setOptions(const std::vector< WsOption > &vOptions)
Set all options.
Definition: WsOption.cpp:57
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
#define ERROR
Definition: WsLogger.h:42
User class.