#include "TApplication.h"
#include "TROOT.h"
#include "TRemoteObject.h"
#include "TSystem.h"
#include "TBrowser.h"
#include "TOrdCollection.h"
#include "TList.h"
#include "TClass.h"
ClassImp(TRemoteObject);
TRemoteObject::TRemoteObject()
{
}
TRemoteObject::TRemoteObject(const char *name, const char *title,
const char *classname) : TNamed(name, title)
{
fIsFolder = kFALSE;
fClassName = classname;
if ((fClassName == "TSystemDirectory") ||
(fClassName == "TFile"))
fIsFolder = kTRUE;
if (!strcmp(classname, "TSystemDirectory") ||
!strcmp(classname, "TSystemFile")) {
gSystem->GetPathInfo(name, fFileStat);
}
Long_t raddr = (Long_t) this;
fRemoteAddress = raddr;
}
TRemoteObject::~TRemoteObject()
{
}
void TRemoteObject::Browse(TBrowser *b)
{
TList *ret;
TRemoteObject *robj;
const char *file;
if (fClassName == "TSystemFile") {
if (b)
b->ExecuteDefaultAction(this);
return;
}
if (fClassName == "TKey") {
if (b->GetRefreshFlag())
b->SetRefreshFlag(kFALSE);
gApplication->SetBit(TApplication::kProcessRemotely);
TObject *obj = (TObject *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseKey(\"%s\");", GetName()));
if (b && obj) {
if (obj->IsA()->GetMethodWithPrototype("SetDirectory", "TDirectory*"))
gROOT->ProcessLine(Form("((%s *)0x%lx)->SetDirectory(0);", obj->ClassName(), obj));
obj->Browse(b);
b->SetRefreshFlag(kTRUE);
}
}
if (fClassName == "TSystemDirectory") {
if (b->GetRefreshFlag())
b->SetRefreshFlag(kFALSE);
gApplication->SetBit(TApplication::kProcessRemotely);
ret = (TList *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseDirectory(\"%s\");", GetTitle()));
if (ret) {
TIter next(ret);
while ((robj = (TRemoteObject *)next())) {
file = robj->GetName();
if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
continue;
b->Add(robj, robj->GetName());
}
}
return;
}
if (fClassName == "TFile") {
if (b->GetRefreshFlag())
b->SetRefreshFlag(kFALSE);
gApplication->SetBit(TApplication::kProcessRemotely);
ret = (TList *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseFile(\"%s\");", GetName()));
if (ret) {
TIter next(ret);
while ((robj = (TRemoteObject *)next())) {
file = robj->GetName();
b->Add(robj, robj->GetName());
}
}
return;
}
}
TList *TRemoteObject::Browse()
{
TList *objects = new TList;
static Int_t level = 0;
const char *name = GetTitle();
TRemoteObject *sdir;
if (GetName()[0] == '.' && GetName()[1] == '.')
SetName(gSystem->BaseName(name));
TSystemDirectory dir(name, name);
TList *files = dir.GetListOfFiles();
if (files) {
files->Sort();
TIter next(files);
TSystemFile *file;
TString fname;
while ((file=(TSystemFile*)next())) {
fname = file->GetName();
if (file->IsDirectory()) {
level++;
TString sdirpath;
if (!strcmp(fname.Data(), "."))
sdirpath = name;
else if (!strcmp(fname.Data(), ".."))
sdirpath = gSystem->DirName(name);
else {
sdirpath = name;
if (!sdirpath.EndsWith("/"))
sdirpath += "/";
sdirpath += fname.Data();
}
sdir = new TRemoteObject(fname.Data(), sdirpath.Data(), "TSystemDirectory");
objects->Add(sdir);
level--;
}
}
TIter nextf(files);
while ((file=(TSystemFile*)nextf())) {
fname = file->GetName();
if (!file->IsDirectory()) {
sdir = new TRemoteObject(fname.Data(), gSystem->WorkingDirectory(), "TSystemFile");
objects->Add(sdir);
}
}
delete files;
}
return objects;
}
Bool_t TRemoteObject::GetFileStat(FileStat_t *buf)
{
buf->fDev = fFileStat.fDev;
buf->fIno = fFileStat.fIno;
buf->fMode = fFileStat.fMode;
buf->fUid = fFileStat.fUid;
buf->fGid = fFileStat.fGid;
buf->fSize = fFileStat.fSize;
buf->fMtime = fFileStat.fMtime;
buf->fIsLink = fFileStat.fIsLink;
return kTRUE;
}
void TRemoteObject::Streamer(TBuffer &b)
{
if (b.IsReading()) {
b >> fFileStat.fDev;
b >> fFileStat.fIno;
b >> fFileStat.fMode;
b >> fFileStat.fUid;
b >> fFileStat.fGid;
b >> fFileStat.fSize;
b >> fFileStat.fMtime;
b >> fFileStat.fIsLink;
b >> fIsFolder;
b >> fRemoteAddress;
b >> fClassName;
b >> fKeyObjectName;
b >> fKeyClassName;
}
else {
b << fFileStat.fDev;
b << fFileStat.fIno;
b << fFileStat.fMode;
b << fFileStat.fUid;
b << fFileStat.fGid;
b << fFileStat.fSize;
b << fFileStat.fMtime;
b << fFileStat.fIsLink;
b << fIsFolder;
b << fRemoteAddress;
b << fClassName;
b << fKeyObjectName;
b << fKeyClassName;
}
TNamed::Streamer(b);
}