Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsModTwitter.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsModTwitter.cpp
4  *
5  * Description: Twitter 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 <curl/curl.h>
14 
15 #include <Wt/WTheme>
16 #include <Wt/WLogger>
17 #include <Wt/Utils>
18 
19 #include <gdwtcore/gdToolbar.h>
20 
21 #include <Logger/WsLogger.h>
22 
23 #include "WsModTwitter.h"
24 
25 extern "C" {
26  void WsModTwitterInit(void)
27  {
28  void* hndl = dlopen("libarchive.so", RTLD_NOW | RTLD_GLOBAL);
29  if ( hndl == NULL ) {
30  fprintf(stderr, "cannot load libarchive.so shared library! %s\n", dlerror());
31  return;
32  }
33  }
34 }
35 
36 
37 using namespace Wt;
38 
40  : WsEditorWidget(parent), m_cb(0), m_cbState(true)
41 { }
42 
44 { }
45 
47 {
50  string path = WApplication::instance()->internalPath();
52  std::string prop(user->getProperty(path, "WsModTwitter", "twitter_enable", "true"));
53  m_cb = new WCheckBox("Enable twitter ?");
54  if ( prop == "true" ) {
55  m_cbState = true;
56  m_cb->setCheckState(Wt::Checked);
57  } else {
58  m_cbState = false;
59  m_cb->setCheckState(Wt::Unchecked);
60  }
61  addWidget(m_cb);
62 }
63 
65 {
67  string path = WApplication::instance()->internalPath();
69  if ( m_cb->checkState() != m_cbState )
70  if ( user->saveProperty(path, "WsModTwitter", "twitter_enable", m_cb->checkState() ? "true" : "false") == ErrorCode::Failure ) return false;
71  return true;
72 }
73 
74 
75 
76 
77 WsModTwitter::WsModTwitter() : WsModule(), m_buttonReTweet(0)
78 {
79 }
80 
82 {
83 }
84 
85 WWidget* WsModTwitter::createContentsMenuBar(WContainerWidget* parent) const
86 {
87  bool b = true;
88  if ( asString(option("inContentMenuBar")) == "false" )
89  b = false;
90  if ( !b ) return 0;
91  WsModTwitter* self = const_cast<WsModTwitter*>(this);
92  self->buildMenuBar();
93  return 0;
94 }
95 
97 {
98  m_buttonReTweet = 0;
100 }
101 
102 WWidget* WsModTwitter::createContents(WContainerWidget* parent) const
103 {
104  return 0;
105 }
106 
107 WsEditorWidget* WsModTwitter::createEditor(WContainerWidget* parent) const
108 {
109  WsTwitterEditorWidget* editor = new WsTwitterEditorWidget(parent);
110  return editor;
111 }
112 
113 WWidget* WsModTwitter::createAdmin(WContainerWidget* parent) const
114 {
115  return 0;
116 }
117 
119 {
120  if ( !contentButtonsBar() ) return 0;
121  gdToolbarItem* pTbItem = contentButtonsBar()->addToolbarItem("", " ", "");
122  // pTbItem->clicked().connect(SLOT(t 0his, WsModTwitter::handleTwitterClick));
123  m_buttonReTweet = pTbItem->button();
124  m_buttonReTweet->setIcon(wApp->theme()->resourcesUrl() + "wittyshare/Icons/Twitter_Retweet-16x16.png");
125  m_buttonReTweet->setLinkTarget(Wt::TargetNewWindow);
126  wApp->internalPathChanged().connect(SLOT(this, WsModTwitter::handleTwitterClick));
127  return 0;
128 }
129 
131 {
132  if ( !m_buttonReTweet ) return;
133  WsUser* pUser = WsApp->wsUser();
134  std::string curPath = wApp->internalPath();
135  boost::algorithm::replace_all(curPath, "&amp;", "&");
136  std::string tFile = pUser->getRootPath() + curPath;
137  if ( boost::filesystem::is_directory(tFile) ) return;
138  NodePtr pRootNode = pUser->getAccessRoot();
139  if ( !pRootNode ) {
140  wApp->log("notice") << "WsModTwitter::handleTwitterClick() : Path = " << curPath << ", getAccessRoot() is null";
141  return;
142  }
143  NodePtr pNode = pRootNode.get()->eatPath(curPath);
144  if ( !pNode ) {
145  wApp->log("notice") << "WsModTwitter::handleTwitterClick() : Path = " << curPath << ", getAccessRoot() eatPath is null";
146  return;
147  }
148  std::string sText = pNode.get()->getDisplayName(true);
149  std::string sServerUrl = WsGlobalProperties::instance()->get("global", "serverUrl", "http://localhost");
150  std::string sFullUrl = sServerUrl + curPath;
151  char* pCurl = curl_escape(sFullUrl.c_str(), sFullUrl.size());
152  std::string sCurl(pCurl);
153  curl_free(pCurl);
154  boost::algorithm::replace_all(sCurl, "/", "%2F");
155  boost::algorithm::replace_all(sCurl, " ", "%20");
156  pCurl = curl_escape(sText.c_str(), sText.size());
157  std::string sTextEnc(pCurl);
158  curl_free(pCurl);
159  boost::algorithm::replace_all(sTextEnc, "/", "%2F");
160  boost::algorithm::replace_all(sTextEnc, " ", "%20");
161  std::string twitterUrl;
162  twitterUrl += "https://twitter.com/intent/tweet?original_referer=";
163  // twitterUrl += "http%3A%2F%2Fwww.eurofer.eu%2Findex.php%2Feng%2FNews-Media%2FPress-Releases%2FENVI-Committee-backs-life-cycle-approach-to-vehicle-emissions";
164  // twitterUrl += Wt::Utils::urlEncode(sFullUrl);
165  twitterUrl += sCurl;
166  twitterUrl += "&text=";
167  // twitterUrl += "ENVI%20Committee%20backs%20life%20cycle%20approach%20to%20vehicle%20emissions";
168  // twitterUrl += Wt::Utils::urlEncode(sText);
169  twitterUrl += sTextEnc;
170  twitterUrl += "&tw_p=tweetbutton&url=";
171  // twitterUrl += "http%3A%2F%2Fwww.eurofer.eu%2Findex.php%2Feng%2FNews-Media%2FPress-Releases%2FENVI-Committee-backs-life-cycle-approach-to-vehicle-emissions";
172  // twitterUrl += Wt::Utils::urlEncode(sFullUrl);
173  twitterUrl += sCurl;
174  twitterUrl += "&via=";
175  twitterUrl += asString(option("account")).narrow();
176  m_buttonReTweet->setLink(twitterUrl);
177 }
178 
179 std::string WsModTwitter::description() const
180 {
181  return "WsModTwitter wittyShare module";
182 }
183 
WsUser * wsUser()
WsModulesLoader & WsModules()
std::string description() const
Return the description of the module.
boost::shared_ptr< WsAbstractNode > NodePtr
Wt::WWidget * createAdmin(Wt::WContainerWidget *parent=0) const
Create the contents for an administrator.
std::string pathWithoutPrefix(const std::string &path)
int saveProperty(const std::string &path, const std::string &section, const std::string &attr, const std::string &val)
sets one property of the node and save it on disk.
Definition: WsUser.cpp:99
std::string getProperty(const std::string &path, const std::string &section, const string &attr, const std::string &def)
returns one property of a node If the property does not exist, a empty string is returned.
Definition: WsUser.cpp:91
virtual void destroyContentsMenuBar()
void WsModTwitterInit(void)
const int Failure
static WsApplication * wsInstance()
Facility function to access the application object.
Definition: WsApplication.h:39
Wt::WPushButton * m_buttonReTweet
Definition: WsModTwitter.h:54
Wt::WCheckBox * m_cb
Definition: WsModTwitter.h:30
WsContentButtonsBar * contentButtonsBar()
WittyShare provide a container to add some buttons like export page in pdf, ..., This function allow ...
Definition: WsModule.cpp:42
WsEditorWidget * createEditor(Wt::WContainerWidget *parent=0) const
Create the contents for an editor (create a view of options).
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
Wt::WWidget * createContentsMenuBar(Wt::WContainerWidget *parent=0) const
Create the functionalities.
virtual void load()
Definition: WsModule.cpp:26
Wt::WWidget * buildMenuBar()
virtual void destroyContentsMenuBar()
Definition: WsModule.cpp:47
const boost::any & option(const std::string &attribute) const
Get an options value.
Definition: WsOption.cpp:62
std::string get(const std::string &section, const std::string &id, const std::string &def)
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 WsGlobalProperties * instance()
void handleTwitterClick()
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
virtual bool saveEditor()
WsTwitterEditorWidget(Wt::WContainerWidget *parent=0)
virtual void load()
Wt::WWidget * createContents(Wt::WContainerWidget *parent=0) const
Create the contents.