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