CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CmsShowSearchFiles.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <iostream>
3 #include <fstream>
4 #include <stdlib.h>
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"
21 
22 class FWHtml : public TGHtml {
23 public:
24  FWHtml(const TGWindow* p, int w, int h, int id = -1):
25  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) {
34  m_visited.insert(iToVisit);
35  }
36 private:
37  std::set<std::string> m_visited;
38 };
39 
40 const static unsigned int s_columns = 3;
41 const static char* const s_prefixes[][s_columns] ={
42  {"http://", "Web site known by you",0},
43  {"file:","Local file [you must type full path name]",0},
44  {"dcap://","dCache [FNAL]",0},
45  {"rfio://","Castor [CERN]",0}
46 
47 };
48 
49 static const std::string s_httpPrefix("http:");
50 static const std::string s_filePrefix("file:");
51 static const std::string s_rootPostfix(".root");
52 
53 
54 namespace {
55 float
56 getURLResponseTime(const char* url)
57 {
58  TString com = "ping -q -c 1 -n " + TString(url) + "| tail -n 1";
59  FILE* p = gSystem->OpenPipe(com, "r");
60  TString l;
61  l.Gets(p);
62  gSystem->ClosePipe(p);
63 
64  TPMERegexp re("([\\d\\.]+)");
65  if (re.Match(l))
66  return re[1].Atof();
67  else
68  return -1;
69 }
70 }
71 
73  const char* windowname,
74  const TGWindow* p, UInt_t w, UInt_t h)
75  : TGTransientFrame(gClient->GetDefaultRoot(), p, w, h)
76 {
77  TGVerticalFrame* vf = new TGVerticalFrame(this);
78  this->AddFrame(vf,new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,5,5,5,5));
79  TGHorizontalFrame* urlFrame = new TGHorizontalFrame(this);
80  vf->AddFrame(urlFrame,new TGLayoutHints(kLHintsExpandX,5,0,5,5));
81 
82  TGLabel* urlLabel = new TGLabel(urlFrame,"URL");
83  urlFrame->AddFrame(urlLabel, new TGLayoutHints(kLHintsLeft|kLHintsCenterY,1,1,1,1));
84  m_choosePrefix = new TGTextButton(urlFrame,"Choose Prefix");
85  urlFrame->AddFrame(m_choosePrefix, new TGLayoutHints(kLHintsLeft,1,1,1,1));
86 
87  m_file= new TGTextEntry(urlFrame);
88  urlFrame->AddFrame(m_file, new TGLayoutHints(kLHintsExpandX,1,0,1,1));
89  m_file->Connect("TextChanged(const char*)", "CmsShowSearchFiles",this,"fileEntryChanged(const char*)");
90  m_file->Connect("ReturnPressed()", "CmsShowSearchFiles",this,"updateBrowser()");
91 
92  m_webFile = new FWHtml(vf,1,1);
93  m_webFile->Connect("MouseDown(const char*)","CmsShowSearchFiles",this,"hyperlinkClicked(const char*)");
94  vf->AddFrame(m_webFile, new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,1,1,1,1));
95 
96  TGHorizontalFrame* buttonFrame = new TGHorizontalFrame(vf);
97  vf->AddFrame(buttonFrame, new TGLayoutHints(kLHintsExpandX,1,10,1,10));
98 
99  m_openButton = new TGTextButton(buttonFrame,"Open");
100  buttonFrame->AddFrame(m_openButton, new TGLayoutHints(kLHintsRight,5,5,1,1));
101  m_openButton->SetEnabled(kFALSE);
102  m_openButton->Connect("Clicked()","CmsShowSearchFiles",this,"openClicked()");
103 
104  TGTextButton* cancel = new TGTextButton(buttonFrame,"Cancel");
105  buttonFrame->AddFrame(cancel, new TGLayoutHints(kLHintsRight,5,5,1,1));
106  cancel->Connect("Clicked()","CmsShowSearchFiles",this,"UnmapWindow()");
107 
108  SetWindowName(windowname);
109  float x1 = getURLResponseTime("lxplus.cern.ch");
110  float x2 = getURLResponseTime("uaf-2.t2.ucsd.edu");
111  // printf("timtes %f %f \n", x1, x2); fflush(stdout);
112 
114  if (x1 > 0 && x1 < x2)
115  path = Form("http://fireworks.web.cern.ch/fireworks/%d/",fireworks::supportedDataFormatsVersion()[0] );
116  else if (x2 > 0)
117  path = Form("http://uaf-2.t2.ucsd.edu/fireworks/%d/", fireworks::supportedDataFormatsVersion()[0]);
118 
119  if (!path.empty())
120  fwLog(fwlog::kInfo) << "Search files at " << path << "." << std::endl;
121 
122  sendToWebBrowser(path);
123 
124 
125  MapSubwindows();
126  Layout();
127  m_prefixMenu=0;
128  m_choosePrefix->Connect("Clicked()","CmsShowSearchFiles",this,"showPrefixes()");
129 }
130 
132 {
133  delete m_prefixMenu;
134 }
135 
136 //______________________________________________________________________________
137 
138 void
140 {
141  // printf ("pref chosen %d \n", iIndex);
142  m_file->SetText(m_prefixes[iIndex].c_str(),kFALSE);
143  m_openButton->SetEnabled(kFALSE);
144 
145  if(m_prefixComplete[iIndex]) {
146  //gClient->NeedRedraw(this);
147  gClient->NeedRedraw(m_choosePrefix);
148  gClient->NeedRedraw(m_webFile);
149  gClient->ProcessEventsFor(this);
150  updateBrowser();
151  } else {
152  std::string p;
153  sendToWebBrowser(p);
154  }
155 }
156 
157 //______________________________________________________________________________
158 
159 
160 void
162 {
163  std::string fileName =iFileName;
164  size_t index = fileName.find_last_of(".");
165  std::string postfix;
166  if(index != std::string::npos) {
167  postfix=fileName.substr(index,std::string::npos);
168  }
169  if(postfix ==s_rootPostfix) {
170  m_openButton->SetEnabled(kTRUE);
171  } else {
172  m_openButton->SetEnabled(kFALSE);
173  }
174 }
175 
176 //______________________________________________________________________________
177 
178 void
180 {
181  std::string n = m_file->GetText();
182  sendToWebBrowser(n);
183 }
184 
185 
186 //______________________________________________________________________________
187 
188 void
190 {
191  m_file->SetText(iLink,kTRUE);
192 
193  m_webFile->addToVisited(iLink);
194  std::string fileName =iLink;
195  size_t index = fileName.find_last_of(".");
196  std::string postfix = fileName.substr(index,std::string::npos);
197 
198  if(postfix !=s_rootPostfix) {
199  updateBrowser();
200  } else {
201  openClicked();
202  }
203 }
204 
205 //______________________________________________________________________________
206 
207 void
209 {
210  m_openCalled=true;
211  this->UnmapWindow();
212 }
213 
214 //______________________________________________________________________________
215 
216 void
218 {
219  if(0==m_prefixMenu) {
220  m_prefixMenu = new TGPopupMenu(this);
221  const char* const (*itEnd)[s_columns] = s_prefixes+sizeof(s_prefixes)/sizeof(const char*[3]);
222  int index = 0;
223  for(const char* const (*it)[s_columns] = s_prefixes;
224  it != itEnd;
225  ++it,++index) {
226  //only add the protocols this version of the code actually can load
227  std::string prefix = std::string((*it)[0]).substr(0,std::string((*it)[0]).find_first_of(":")+1);
228  if(s_httpPrefix==prefix ||
229  s_filePrefix==prefix ||
230  (gPluginMgr->FindHandler("TSystem",prefix.c_str()) &&
231  gPluginMgr->FindHandler("TSystem",prefix.c_str())->CheckPlugin() != -1)) {
232  m_prefixMenu->AddEntry((std::string((*it)[0])+" ("+((*it)[1])+")").c_str(),index);
233  m_prefixes.push_back((*it)[0]);
234  m_prefixComplete.push_back(0!=(*it)[2]);
235  }
236  }
237  m_prefixMenu->Connect("Activated(Int_t)","CmsShowSearchFiles",this,"prefixChoosen(Int_t)");
238  }
239  m_prefixMenu->PlaceMenu(m_choosePrefix->GetX(),m_choosePrefix->GetY(),true,true);
240 }
241 
242 
243 //______________________________________________________________________________
244 
245 
246  //Copied from TGHtmlBrowser
247 static std::string readRemote(const char *url)
248 {
249  // Read (open) remote files.
250 
251  char *buf = 0;
252  TUrl fUrl(url);
253 
254  TString msg = "GET ";
255  msg += fUrl.GetProtocol();
256  msg += "://";
257  msg += fUrl.GetHost();
258  msg += ":";
259  msg += fUrl.GetPort();
260  msg += "/";
261  msg += fUrl.GetFile();
262  msg += "\r\n";
263 
264  TString uri(url);
265  if (!uri.BeginsWith("http://"))
266  return std::string();
267  TSocket s(fUrl.GetHost(), fUrl.GetPort());
268  if (!s.IsValid())
269  return std::string();
270  if (s.SendRaw(msg.Data(), msg.Length()) == -1)
271  return std::string();
272  Int_t size = 1024*1024;
273  buf = (char *)calloc(size, sizeof(char));
274  if (s.RecvRaw(buf, size) == -1) {
275  free(buf);
276  return std::string();
277  }
278  std::string returnValue(buf);
279  free(buf);
280  return returnValue;
281 }
282 
283 //______________________________________________________________________________
284 
285 void
287 {
288  // std::cout << "CmsShowSearchFiles::sendToWebBrowser " << fileName << std::endl ;
289 
290  size_t index = fileName.find_first_of(":");
291  if(index != std::string::npos) {
292  ++index;
293  } else {
294  index = 0;
295  }
296  std::string prefix = fileName.substr(0,index);
297 
298  m_webFile->Clear();
299  if(prefix == s_httpPrefix) {
300  gVirtualX->SetCursor(GetId(),gVirtualX->CreateCursor(kWatch));
301  //If you clicked a hyperlink then the cursor is still a hand but we now
302  // want it to be a watch
303  gVirtualX->SetCursor(m_webFile->GetId(),gVirtualX->CreateCursor(kWatch));
304  //If we don't call ProcessEventsFor then the cursor will not be updated
305  gClient->ProcessEventsFor(this);
306  TUrl url(fileName.c_str());
307  std::string buffer = readRemote(url.GetUrl());
308 
309  if (buffer.size()) {
310  m_webFile->SetBaseUri(url.GetUrl());
311  m_webFile->ParseText(const_cast<char*>(buffer.c_str()));
312  }
313  else {
314  readError();
315  }
316  gVirtualX->SetCursor(GetId(),gVirtualX->CreateCursor(kPointer));
317  gVirtualX->SetCursor(m_webFile->GetId(),gVirtualX->CreateCursor(kPointer));
318  } else {
319  readInfo();
320  }
321  m_webFile->Layout();
322 }
323 
324 //______________________________________________________________________________
325 void
327 {
328 
329  const static char *s_readError[] = {
330  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\"> ",
331  "<HTML><HEAD><TITLE>HTTP Read Error</TITLE> ",
332  "<META http-equiv=Content-Type content=\"text/html; charset=UTF-8\"></HEAD> ",
333  "<BODY> ",
334  "<P>Unknown error while trying to get file via http</P>",
335  "</BODY></HTML> ",
336  0
337  };
338 
339  m_webFile->SetBaseUri("");
340  for (int i=0; s_readError[i]; i++) {
341  m_webFile->ParseText(const_cast<char *>(s_readError[i]));
342  }
343 }
344 
345 void
347 {
348  const static char *s_noBrowserMessage[] = {
349  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\"> ",
350  "<HTML><HEAD><TITLE>No Browser Available</TITLE> ",
351  "<META http-equiv=Content-Type content=\"text/html; charset=UTF-8\"></HEAD> ",
352  "<BODY> ",
353  //"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>",
354  //"Only a prefix beginning in <STRONG>http:</STRONG> which contains a site name (e.g. http://www.site.org) is supported for browsing."
355  "<b>Welcome....</b><BR>",
356  "<BR>",
357  "<b>You may look at examples:</b><BR>",
358  "If you are in Europe, open example data files at CERN : ", " <a href=" , Form("http://fireworks.web.cern.ch/fireworks/%d/",fireworks::supportedDataFormatsVersion()[0] ),"> http://fireworks.web.cern.ch/fireworks/ </a><BR>",
359  "If you are in US, open example data files at UCSD: ", " <a href=" , Form("http://uaf-2.t2.ucsd.edu/fireworks/%d/",fireworks::supportedDataFormatsVersion()[0] ),">http://uaf-2.t2.ucsd.edu/fireworks/ </a><BR>",
360  "<BR>"
361  "<b>You also may load files with Choose Prefix </b><BR>"
362  "</BODY></HTML> ",
363  0
364  };
365 
366  m_webFile->SetBaseUri("");
367  for (int i=0; s_noBrowserMessage[i]; i++) {
368  m_webFile->ParseText((char *)s_noBrowserMessage[i]);
369  }
370 
371 }
372 //______________________________________________________________________________
373 
376 {
377  DontCallClose();
378  Connect("CloseWindow()","CmsShowSearchFiles",this,"UnmapWindow()");
379  m_openCalled = false;
380  MapWindow();
381  gClient->WaitForUnmap(this);
382 
383  if(!m_openCalled) {
384  return std::string();
385  }
386  return m_file->GetText();
387 }
int i
Definition: DBlmapReader.cc:9
void addToVisited(const char *iToVisit)
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 *)
tuple path
else: Piece not in the list, fine.
int * supportedDataFormatsVersion()
Definition: fwPaths.cc:43
bool check(const DataFrame &df, bool capcheck, bool dvercheck)
FWHtml(const TGWindow *p, int w, int h, int id=-1)
TGPopupMenu * m_prefixMenu
int IsVisited(const char *iCheck) override
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
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:50
std::set< std::string > m_visited
void sendToWebBrowser(std::string &iWebFile)
void hyperlinkClicked(const char *)
tuple filename
Definition: lut2db_cfg.py:20
T w() const
static std::string readRemote(const char *url)
tuple size
Write out results.
std::string chooseFileFromURL()
This opens the dialog window and returns once the user has choosen, returns an empty string if cancel...
CmsShowSearchFiles(const char *filename, const char *windowname, const TGWindow *p=0, UInt_t w=1, UInt_t h=1)
static const std::string s_rootPostfix(".root")