1 // Copyright (C) 2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony GEAY (EDF R&D)
21 #include "QMachineBrowser"
22 #include "QRemoteFileBrowser"
26 #include "QVBoxLayout"
27 #include "QPushButton"
28 #include "QMessageBox"
29 #include "QInputDialog"
30 #include "QXmlStreamReader"
31 #include "QProcessEnvironment"
35 constexpr const char localhost[]="localhost";
37 QMachineSelector::QMachineSelector(QWidget *parent):QComboBox(parent)
42 void QMachineSelector::initLocation()
44 this->assignToLocalhost();
47 void QMachineSelector::fillMachines()
49 this->fillMachinesFromCatalog();
50 this->fillMachinesFromSettings();
53 void QMachineSelector::appendEntry(const QString& entry)
55 for(int i=0;i<this->count();i++)
57 if(this->itemText(i)==entry)
60 this->insertItem(this->count(),entry);
61 this->setCurrentIndex(this->count()-1);
64 void QMachineSelector::fillMachinesFromCatalog()
66 constexpr const char APPLI[]="APPLI";
67 constexpr const char RESOURCES[]="CatalogResources.xml";
68 if(!QProcessEnvironment::systemEnvironment().contains(APPLI))
70 QString appli(QProcessEnvironment::systemEnvironment().value(APPLI));
71 QFileInfo fi(QDir::homePath(),appli);
72 if(!(fi.exists() && fi.isDir()))
74 QFileInfo fi2(QDir(fi.canonicalFilePath()),QString(RESOURCES));
75 if(!(fi2.exists() && fi2.isFile()))
77 QFile file(fi2.canonicalFilePath());
78 if(!file.open(QFile::ReadOnly | QFile::Text))
82 QXmlStreamReader reader;
83 reader.setDevice(&file);
85 while(!reader.atEnd())
87 if(reader.isStartElement())
89 if(reader.name()=="machine")
91 foreach(const QXmlStreamAttribute &attr, reader.attributes())
93 if(attr.name().toString()==QLatin1String("name"))
95 this->insertItem(this->count(),attr.value().toString());
106 void QMachineSelector::assignToLocalhost()
109 for(;i<this->count();i++)
110 if(this->itemText(i)==localhost)
114 this->insertItem(this->count(),QString(localhost));
115 this->setCurrentIndex(this->count()-1);
118 this->setCurrentIndex(i);
121 void QMachineSelector::fillMachinesFromSettings()
125 QMachineManager::QMachineManager(QWidget *parent):QWidget(parent),_pb(nullptr),_ms(nullptr)
127 QHBoxLayout *lay(new QHBoxLayout(this));
128 _pb=new QPushButton(this);
130 _pb->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
132 _ms=new QMachineSelector(this);
134 connect(_pb,SIGNAL(clicked()),this,SLOT(newEntryRequested()));
137 void QMachineManager::initLocation()
142 QString QMachineManager::getSelectedHost() const
144 return _ms->currentText();
147 void QMachineManager::newEntryRequested()
149 constexpr int timeEllapse=3000;
151 QString newEntry(QInputDialog::getItem(this,"Append new host","Hostname",QStringList(),/*current*/0,/*editable*/true,&isOK,Qt::Tool));
157 QStringList st(newEntry);
158 st << "-c" << "1" << "-w" << QString::number(timeEllapse/1000);//attempt to send one packet within timeEllapse ms
159 proc.start("ping",st);
161 if(proc.waitForFinished(-1))
163 if(proc.exitCode()!=0)
165 QMessageBox::information(this,"Information",QString("host %1 ping failed !").arg(newEntry));
171 QMessageBox::information(this,"Information",QString("host %1 ping failed !").arg(newEntry));
175 _ms->appendEntry(newEntry);
178 QMachineBrowser::QMachineBrowser(QWidget *parent):QWidget(parent),_msel(nullptr),_le(nullptr)
180 QVBoxLayout *lay(new QVBoxLayout(this));
181 _msel=new QMachineManager(this);
182 _le=new QLineEdit(this);
183 lay->addWidget(_msel);
185 connect(_le,SIGNAL(returnPressed()),this,SIGNAL(locationChanged()));
188 void QMachineBrowser::initLocation()
190 _msel->initLocation();
191 _le->setText(QDir::currentPath());
192 emit this->locationChanged();
195 QRemoteFileSystemModel *QMachineBrowser::generateModel()
197 FileLoader *fl(this->generateFileLoader());
198 return new QRemoteFileSystemModel(this,fl);
201 FileLoader *QMachineBrowser::generateFileLoader()
203 FileLoader *fl(nullptr);
204 QString host(_msel->getSelectedHost());
206 fl=new LocalFileLoader(_le->text());
208 fl=new RemoteFileLoader(host,_le->text());