CMS 3D CMS Logo

CmsShowSearchFiles.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <iostream>
3 #include <fstream>
4 #include <cstdlib>
5 #include <set>
6 #include "TGClient.h"
7 #include "TGHtml.h"
8 #include "TGButton.h"
9 #include "TGMenu.h"
10 #include "TGLabel.h"
11 #include "TGTextEntry.h"
12 #include "TPluginManager.h"
13 #include "TUrl.h"
14 #include "TSocket.h"
15 #include "TSystem.h"
16 #include "TVirtualX.h"
17 #include "TPRegexp.h"
18 #include "TVirtualX.h"
22 
23 class FWHtml : public TGHtml {
24 public:
25  FWHtml(const TGWindow* p, int w, int h, int id = -1) : TGHtml(p, w, h, id) {}
26 
27  int IsVisited(const char* iCheck) override {
28  std::string check(GetBaseUri());
29  check += iCheck;
30  int value = m_visited.find(check) == m_visited.end() ? kFALSE : kTRUE;
31  return value;
32  }
33  void addToVisited(const char* iToVisit) { m_visited.insert(iToVisit); }
34 
35 private:
36  std::set<std::string> m_visited;
37 };
38 
39 const static unsigned int s_columns = 3;
40 const static char* const s_prefixes[][s_columns] = {{"http://", "Web site known by you", nullptr},
41  {"file:", "Local file [you must type full path name]", nullptr},
42  {"dcap://", "dCache [FNAL]", nullptr},
43  {"rfio://", "Castor [CERN]", nullptr}
44 
45 };
46 
47 static const std::string s_httpPrefix("http:");
48 static const std::string s_filePrefix("file:");
49 static const std::string s_rootPostfix(".root");
50 
52  const char* filename, const char* windowname, const TGWindow* p, UInt_t w, UInt_t h)
53  : TGTransientFrame(gClient->GetDefaultRoot(), p, w, h) {
54  TGVerticalFrame* vf = new TGVerticalFrame(this);
55  this->AddFrame(vf, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5));
56  TGHorizontalFrame* urlFrame = new TGHorizontalFrame(this);
57  vf->AddFrame(urlFrame, new TGLayoutHints(kLHintsExpandX, 5, 0, 5, 5));
58 
59  TGLabel* urlLabel = new TGLabel(urlFrame, "URL");
60  urlFrame->AddFrame(urlLabel, new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 1, 1, 1, 1));
61  m_choosePrefix = new TGTextButton(urlFrame, "Choose Prefix");
62  urlFrame->AddFrame(m_choosePrefix, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
63 
64  m_file = new TGTextEntry(urlFrame);
65  urlFrame->AddFrame(m_file, new TGLayoutHints(kLHintsExpandX, 1, 0, 1, 1));
66  m_file->Connect("TextChanged(const char*)", "CmsShowSearchFiles", this, "fileEntryChanged(const char*)");
67  m_file->Connect("ReturnPressed()", "CmsShowSearchFiles", this, "updateBrowser()");
68 
69  m_webFile = new FWHtml(vf, 1, 1);
70  m_webFile->Connect("MouseDown(const char*)", "CmsShowSearchFiles", this, "hyperlinkClicked(const char*)");
71  vf->AddFrame(m_webFile, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 1, 1, 1, 1));
72 
73  TGHorizontalFrame* buttonFrame = new TGHorizontalFrame(vf);
74  vf->AddFrame(buttonFrame, new TGLayoutHints(kLHintsExpandX, 1, 10, 1, 10));
75 
76  m_openButton = new TGTextButton(buttonFrame, "Open");
77  buttonFrame->AddFrame(m_openButton, new TGLayoutHints(kLHintsRight, 5, 5, 1, 1));
78  m_openButton->SetEnabled(kFALSE);
79  m_openButton->Connect("Clicked()", "CmsShowSearchFiles", this, "openClicked()");
80 
81  TGTextButton* cancel = new TGTextButton(buttonFrame, "Cancel");
82  buttonFrame->AddFrame(cancel, new TGLayoutHints(kLHintsRight, 5, 5, 1, 1));
83  cancel->Connect("Clicked()", "CmsShowSearchFiles", this, "UnmapWindow()");
84 
85  SetWindowName(windowname);
86 
88  Form("http://cmsshow-rels.web.cern.ch/cmsShow-rels/samples/%d/", fireworks::supportedDataFormatsVersion()[0]);
89 
90  if (!path.empty())
91  fwLog(fwlog::kInfo) << "Search files at " << path << "." << std::endl;
92 
94 
95  MapSubwindows();
96  Layout();
97  m_prefixMenu = nullptr;
98  m_choosePrefix->Connect("Clicked()", "CmsShowSearchFiles", this, "showPrefixes()");
99 }
100 
102 
103 //______________________________________________________________________________
104 
106  // printf ("pref chosen %d \n", iIndex);
107  m_file->SetText(m_prefixes[iIndex].c_str(), kFALSE);
108  m_openButton->SetEnabled(kFALSE);
109 
110  if (m_prefixComplete[iIndex]) {
111  //gClient->NeedRedraw(this);
112  gClient->NeedRedraw(m_choosePrefix);
113  gClient->NeedRedraw(m_webFile);
114  gClient->ProcessEventsFor(this);
115  updateBrowser();
116  } else {
117  std::string p;
119  }
120 }
121 
122 //______________________________________________________________________________
123 
124 void CmsShowSearchFiles::fileEntryChanged(const char* iFileName) {
125  std::string fileName = iFileName;
126  size_t index = fileName.find_last_of('.');
128  if (index != std::string::npos) {
129  postfix = fileName.substr(index, std::string::npos);
130  }
131  if (postfix == s_rootPostfix) {
132  m_openButton->SetEnabled(kTRUE);
133  } else {
134  m_openButton->SetEnabled(kFALSE);
135  }
136 }
137 
138 //______________________________________________________________________________
139 
141  std::string n = m_file->GetText();
143 }
144 
145 //______________________________________________________________________________
146 
147 void CmsShowSearchFiles::hyperlinkClicked(const char* iLink) {
148  m_file->SetText(iLink, kTRUE);
149 
150  m_webFile->addToVisited(iLink);
151  std::string fileName = iLink;
152  size_t index = fileName.find_last_of('.');
153  std::string postfix = fileName.substr(index, std::string::npos);
154 
155  if (postfix != s_rootPostfix) {
156  updateBrowser();
157  } else {
158  openClicked();
159  }
160 }
161 
162 //______________________________________________________________________________
163 
165  m_openCalled = true;
166  this->UnmapWindow();
167 }
168 
169 //______________________________________________________________________________
170 
172  if (nullptr == m_prefixMenu) {
173  m_prefixMenu = new TGPopupMenu(this);
174  const char* const(*itEnd)[s_columns] = s_prefixes + sizeof(s_prefixes) / sizeof(const char* [3]);
175  int index = 0;
176  for (const char* const(*it)[s_columns] = s_prefixes; it != itEnd; ++it, ++index) {
177  //only add the protocols this version of the code actually can load
178  std::string prefix = std::string((*it)[0]).substr(0, std::string((*it)[0]).find_first_of(':') + 1);
179  if (s_httpPrefix == prefix || s_filePrefix == prefix ||
180  (gPluginMgr->FindHandler("TSystem", prefix.c_str()) &&
181  gPluginMgr->FindHandler("TSystem", prefix.c_str())->CheckPlugin() != -1)) {
182  m_prefixMenu->AddEntry((std::string((*it)[0]) + " (" + ((*it)[1]) + ")").c_str(), index);
183  m_prefixes.push_back((*it)[0]);
184  m_prefixComplete.push_back(nullptr != (*it)[2]);
185  }
186  }
187  m_prefixMenu->Connect("Activated(Int_t)", "CmsShowSearchFiles", this, "prefixChoosen(Int_t)");
188  }
189  m_prefixMenu->PlaceMenu(m_choosePrefix->GetX(), m_choosePrefix->GetY(), true, true);
190 }
191 
192 //______________________________________________________________________________
193 
194 //Copied from TGHtmlBrowser
195 static std::string readRemote(const char* url) {
196  // Read (open) remote files.
197 
198  char* buf = nullptr;
199  TUrl fUrl(url);
200 
201  TString msg = "GET ";
202  msg += fUrl.GetProtocol();
203  msg += "://";
204  msg += fUrl.GetHost();
205  msg += ":";
206  msg += fUrl.GetPort();
207  msg += "/";
208  msg += fUrl.GetFile();
209  msg += "\r\n";
210 
211  TString uri(url);
212  if (!uri.BeginsWith("http://"))
213  return std::string();
214  TSocket s(fUrl.GetHost(), fUrl.GetPort());
215  if (!s.IsValid())
216  return std::string();
217  if (s.SendRaw(msg.Data(), msg.Length()) == -1)
218  return std::string();
219  Int_t size = 1024 * 1024;
220  buf = (char*)calloc(size, sizeof(char));
221  if (s.RecvRaw(buf, size) == -1) {
222  free(buf);
223  return std::string();
224  }
225  std::string returnValue(buf);
226  free(buf);
227  return returnValue;
228 }
229 
230 //______________________________________________________________________________
231 
233  // std::cout << "CmsShowSearchFiles::sendToWebBrowser " << fileName << std::endl ;
234 
235  size_t index = fileName.find_first_of(':');
236  if (index != std::string::npos) {
237  ++index;
238  } else {
239  index = 0;
240  }
241  std::string prefix = fileName.substr(0, index);
242 
243  m_webFile->Clear();
244  if (prefix == s_httpPrefix) {
245  gVirtualX->SetCursor(GetId(), gVirtualX->CreateCursor(kWatch));
246  //If you clicked a hyperlink then the cursor is still a hand but we now
247  // want it to be a watch
248  gVirtualX->SetCursor(m_webFile->GetId(), gVirtualX->CreateCursor(kWatch));
249  //If we don't call ProcessEventsFor then the cursor will not be updated
250  gClient->ProcessEventsFor(this);
251  TUrl url(fileName.c_str());
252  std::string buffer = readRemote(url.GetUrl());
253 
254  if (!buffer.empty()) {
255  m_webFile->SetBaseUri(url.GetUrl());
256  m_webFile->ParseText(const_cast<char*>(buffer.c_str()));
257  } else {
258  readError();
259  }
260  gVirtualX->SetCursor(GetId(), gVirtualX->CreateCursor(kPointer));
261  gVirtualX->SetCursor(m_webFile->GetId(), gVirtualX->CreateCursor(kPointer));
262  } else {
263  readInfo();
264  }
265  m_webFile->Layout();
266 }
267 
268 //______________________________________________________________________________
270  const static char* s_readError[] = {
271  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
272  "\"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\"> ",
273  "<HTML><HEAD><TITLE>HTTP Read Error</TITLE> ",
274  "<META http-equiv=Content-Type content=\"text/html; charset=UTF-8\"></HEAD> ",
275  "<BODY> ",
276  "<P>Unknown error while trying to get file via http</P>",
277  "</BODY></HTML> ",
278  nullptr};
279 
280  m_webFile->SetBaseUri("");
281  for (int i = 0; s_readError[i]; i++) {
282  m_webFile->ParseText(const_cast<char*>(s_readError[i]));
283  }
284 }
285 
287  const static char* s_noBrowserMessage[] = {
288  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
289  "\"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\"> ",
290  "<HTML><HEAD><TITLE>No Browser Available</TITLE> ",
291  "<META http-equiv=Content-Type content=\"text/html; charset=UTF-8\"></HEAD> ",
292  "<BODY> ",
293  //"No file browser is available for this prefix. You can still type the full URL into the above text box to open the EDM ROOT file.<BR>",
294  //"Only a prefix beginning in <STRONG>http:</STRONG> which contains a site name (e.g. http://www.site.org) is supported for browsing."
295  "<b>Welcome....</b><BR>",
296  "<BR>",
297  "<b>You may look at examples:</b><BR>",
298  "If you are in Europe, open example data files at CERN : ",
299  " <a href=",
300  Form("http://fireworks.web.cern.ch/fireworks/%d/", fireworks::supportedDataFormatsVersion()[0]),
301  "> http://fireworks.web.cern.ch/fireworks/ </a><BR>",
302  "If you are in US, open example data files at UCSD: ",
303  " <a href=",
304  Form("http://uaf-2.t2.ucsd.edu/fireworks/%d/", fireworks::supportedDataFormatsVersion()[0]),
305  ">http://uaf-2.t2.ucsd.edu/fireworks/ </a><BR>",
306  "<BR>"
307  "<b>You also may load files with Choose Prefix </b><BR>"
308  "</BODY></HTML> ",
309  nullptr};
310 
311  m_webFile->SetBaseUri("");
312  for (int i = 0; s_noBrowserMessage[i]; i++) {
313  m_webFile->ParseText((char*)s_noBrowserMessage[i]);
314  }
315 }
316 //______________________________________________________________________________
317 
319  DontCallClose();
320  Connect("CloseWindow()", "CmsShowSearchFiles", this, "UnmapWindow()");
321  m_openCalled = false;
322  MapWindow();
323  gClient->WaitForUnmap(this);
324 
325  if (!m_openCalled) {
326  return std::string();
327  }
328  return m_file->GetText();
329 }
size
Write out results.
void addToVisited(const char *iToVisit)
T w() const
std::vector< bool > m_prefixComplete
static const char *const s_prefixes[][s_columns]
TGTextButton * m_choosePrefix
static const std::string s_filePrefix("file:")
void fileEntryChanged(const char *)
int * supportedDataFormatsVersion()
Definition: fwPaths.cc:35
FWHtml(const TGWindow *p, int w, int h, int id=-1)
TGPopupMenu * m_prefixMenu
int IsVisited(const char *iCheck) override
Definition: value.py:1
TGTextButton * m_openButton
static const std::string s_httpPrefix("http:")
std::vector< std::string > m_prefixes
static const unsigned int s_columns
#define fwLog(_level_)
Definition: fwLog.h:45
std::set< std::string > m_visited
tuple msg
Definition: mps_check.py:286
CmsShowSearchFiles(const char *filename, const char *windowname, const TGWindow *p=nullptr, UInt_t w=1, UInt_t h=1)
void sendToWebBrowser(std::string &iWebFile)
void hyperlinkClicked(const char *)
static std::string readRemote(const char *url)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
std::string chooseFileFromURL()
This opens the dialog window and returns once the user has choosen, returns an empty string if cancel...
static const std::string s_rootPostfix(".root")