source: src-qt4/EasyPBI/portsDialog.cpp @ 1620346

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 15 months ago

Initial import of PC-BSD /current/ SVN repo

  • Property mode set to 100644
File size: 9.5 KB
Line 
1#include "portsDialog.h"
2#include "ui_portsDialog.h"
3#include <QDebug>
4
5portsDialog::portsDialog(QWidget *parent, QString localPortsDir, QString sucmd) :
6    QDialog(parent),
7    ui(new Ui::portsDialog)
8{
9       
10       //Setup Initial state of GUI objects and connect signal/slots
11       ui->setupUi(this);  //load the main UI from designer
12
13        //Setup the local variables
14        localPortsTree = localPortsDir;
15        systemPortsTree = "/usr/ports";
16        suCMD = sucmd;
17        localPorts = FALSE;
18        systemPorts = FALSE;
19        p = new QProcess(this);
20        //p->setProcessChannelMode(QProcess::MergedChannels);
21        p->setProcessEnvironment( QProcessEnvironment::systemEnvironment() );
22        connect(p,SIGNAL(readyReadStandardOutput()),this,SLOT(updateStatusBar()) );
23        connect(p,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)) );
24       
25        //Now check for the ports trees at those locations
26        checkForPorts();
27        //Make sure that the progress group is invisible until needed
28        ui->groupUpdate->setVisible(FALSE);
29        //Setup signal/slots
30        connect(ui->push_close,SIGNAL(clicked()), this, SLOT(slotClose()) );
31        connect(ui->push_updatelocal,SIGNAL(clicked()), this, SLOT(slotGetLocalPorts()) );
32        connect(ui->push_updatesystem,SIGNAL(clicked()), this, SLOT(slotGetSystemPorts()) );
33}
34
35portsDialog::~portsDialog(){
36}
37
38void portsDialog::checkForPorts(){
39  //Check the Local Ports Tree
40  QDir ldir(localPortsTree);
41  QFileInfoList llist = ldir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, QDir::Time); //sort by most recently changed
42  if(llist.length() > 0){
43        localPorts =TRUE;
44        ui->push_updatelocal->setText( tr("Update Local Ports") );
45        ui->label_localinfo->setText( QString(tr("The local ports tree was last updated on %1")).arg( llist[0].lastModified().toString() ));
46  }else{
47    //The local ports tree is missing
48    localPorts = FALSE;
49    ui->push_updatelocal->setText( tr("Fetch Local Ports") );
50    ui->label_localinfo->setText( QString(tr("The local ports tree does not exist at %1")).arg(localPortsTree) );
51  }
52  //Check the System Ports Tree
53  if( !suCMD.isEmpty() ){
54    QDir dir(systemPortsTree);
55    QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, QDir::Time); //sort by most recently changed
56    if(list.length() > 2){ //allow for a couple directories to still be there, and not have the full ports tree
57        systemPorts = TRUE; //files and/or directories exist
58        ui->push_updatesystem->setText( tr("Update System Ports") );
59        ui->label_systeminfo->setText( QString(tr("The system ports tree was last updated on %1")).arg( list[0].lastModified().toString() ));
60    }else{
61        systemPorts = FALSE;
62    }
63  }else{
64    //Disable the system ports option if no switch-user utility available
65    ui->groupSystem->setEnabled(FALSE);
66  }
67  if(!systemPorts){
68    //The system ports tree is missing
69    ui->push_updatesystem->setText( tr("Fetch System Ports") );
70    ui->label_systeminfo->setText( QString(tr("The system ports tree does not exist at %1")).arg(systemPortsTree) );
71  }
72}
73
74void portsDialog::slotClose(){
75  //Close the UI
76  this->close();
77}
78
79void portsDialog::slotGetLocalPorts(){
80  qDebug() << "Starting local download of FreeBSD Ports tree";
81  //Disable the interface buttons
82  ui->push_updatesystem->setEnabled(FALSE);
83  ui->push_updatelocal->setEnabled(FALSE);
84  ui->push_close->setEnabled(FALSE);
85  //Now make sure the progress box is visible
86  ui->groupUpdate->setVisible(TRUE);
87  ui->progressBar->setValue(0);
88    //Begin creating the network variables
89    QNetworkReply *currentDL;
90    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
91    //Start the download
92    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)) );
93    currentDL = manager->get(QNetworkRequest(QUrl("ftp://ftp.freebsd.org/pub/FreeBSD/ports/ports/ports.tar.gz")));
94    //Update the status
95    ui->label_status->setText(tr("Downloading the FreeBSD ports tree..."));
96    connect(currentDL, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateProgressBar(qint64,qint64)) );
97
98}
99
100void portsDialog::slotGetSystemPorts(){
101  //Disable the interface buttons
102  ui->push_updatesystem->setEnabled(FALSE);
103  ui->push_updatelocal->setEnabled(FALSE);
104  ui->push_close->setEnabled(FALSE);
105  //Now make sure the progress box is visible
106  ui->groupUpdate->setVisible(TRUE);
107  ui->progressBar->setMaximum(0); ui->progressBar->setMinimum(0); //default to a "loading" indicator
108  ui->label_status->setText(tr("Requesting Root Access"));
109  QString cmd = suCMD + " \""; //use the swutch-user utility
110  bool usePortsnap = FALSE;
111  if(systemPorts){
112    //Update the ports tree       
113    if( QFile::exists("/usr/ports/.svn") ){
114      cmd.append("svn update /usr/ports");
115    }else{
116      cmd.append("portsnap fetch update");
117      usePortsnap = TRUE;
118    }
119  }else{
120    //Fresh installation of the ports tree
121    cmd.append("portsnap fetch extract");
122    usePortsnap = TRUE;
123  }
124  cmd.append("\""); //close the quotes
125  //If using portsnap, make sure the intermediate file is there in the EasyPBI directory
126  if( usePortsnap ){
127    QString pspath = localPortsTree;
128    pspath.chop( pspath.length() - pspath.lastIndexOf("/") ); //remove the ports dir from the path to get the base path
129    pspath.append(".portsnapNI.sh");
130    if( !QFile::exists(pspath) ){
131      QString pscmd ="echo \"#!/bin/sh \n#Non-Interactive portsnap workaround for EasyPBI \ncat /usr/sbin/portsnap | sed 's|! -t 0|-z '1'|g' | /bin/sh -s \\$@\" > "+pspath+"; chmod 755 "+pspath;
132      //qDebug() << "system call:" << pscmd;
133      system(pscmd.toUtf8());
134    }
135    cmd.replace("portsnap",pspath);
136  }
137  //Now run the command
138  p->start(cmd);
139  qDebug() << "System ports update started: CMD:" << cmd;
140}
141
142void portsDialog::updateProgressBar(qint64 bytesReceived, qint64 bytesTotal){
143  //Determine the percentage
144  int percent = 0;
145  if(bytesTotal != 0){
146    percent = (bytesReceived*100)/bytesTotal;
147  }
148  //Update the progress bar
149  ui->progressBar->setValue(percent);
150  ui->progressBar->update();
151}
152
153void portsDialog::downloadFinished(QNetworkReply *reply){
154  //Disable the progress bar
155  ui->progressBar->setMaximum(0); ui->progressBar->setMinimum(0); //default to a "loading" indicator
156  //See if the Download was successful
157  qDebug() << "Ports download completion code:" << reply->error();
158  if( reply->error() != QNetworkReply::NoError){
159    QMessageBox::warning(this, tr("Download Failed"),tr("Downloading the FreeBSD ports tree failed. Please check your internet connection and try again."));
160    return;
161  }
162  //Download Succesful, save it to file
163  QFile file(localPortsTree+".tar.gz");
164  if( !file.open(QIODevice::WriteOnly)){   //if file could not be opened
165    QMessageBox::warning(this, tr("Saving Failed"),tr("Saving the FreeBSD ports tree file failed. Please try again."));
166    reply->deleteLater();
167    return;
168  }
169  file.write(reply->readAll());
170  file.close();
171  //Saving the file successful, delete the downloaded data from memory
172  reply->deleteLater();
173  QTimer::singleShot(1000, this, SLOT(extractLocalPorts()) ); //wait 1 sec to start process
174}
175
176void portsDialog::extractLocalPorts(){
177  //Now start the extraction
178  QString pFile = localPortsTree.section("/",-1) + ".tar.gz";
179  QString pDir = localPortsTree + ".tar.gz";
180  pDir.remove("/"+pFile); //cut the file name off the end
181  if(pDir.endsWith("/")){ pDir.chop(1); } //remove any extra "/" on the end
182  if(localPorts){ //remove the old tree if it exists
183    qDebug() << "Removing current local ports tree";
184    ui->label_status->setText(tr("Removing the old ports tree from the EasyPBI directory."));
185    QString rcmd = "cd "+pDir+"/ports; rm -rf `ls | grep -v distfiles`";
186    system(rcmd.toUtf8()); 
187  } 
188  //extract the new tree and remove the downloaded file
189  ui->label_status->setText(tr("Extracting the ports tree into the EasyPBI directory. (This may take a while)"));
190  QString cmd1 = "tar xvf "+pFile;
191  p->setWorkingDirectory(pDir); //Make sure we start in the proper directory
192  p->start(cmd1);
193  qDebug() << "Starting ports extraction process: CMD:" << cmd1;
194}
195
196void portsDialog::updateStatusBar(){
197  QString tmp = p->readAllStandardOutput();
198  if(tmp.endsWith("\n")){ tmp.chop(1); }
199  tmp = tmp.section("\n",-1);
200  tmp = tmp.remove("\n").simplified();
201  //qDebug() << "Status Update:" << tmp;
202  ui->label_status->clear();
203  ui->label_status->setText(tmp);
204  //Re-size the dialog to the smallest possible size with this text
205  //QTimer::singleShot(1,this,SLOT(resizeDialog()));
206}
207
208void portsDialog::resizeDialog(){
209  this->resize(0,0); //will automatically get larger to show everything at the current size
210}
211
212void portsDialog::processFinished(int exitCode,QProcess::ExitStatus status){
213  qDebug() << " - Ports update finished:" << exitCode << status;
214  //Enable the interface buttons
215  ui->push_updatesystem->setEnabled(TRUE);
216  ui->push_updatelocal->setEnabled(TRUE);
217  ui->push_close->setEnabled(TRUE);
218  //Now make sure the progress box is invisible
219  ui->groupUpdate->setVisible(FALSE);   
220       
221  //If the ports package exists in the EasyPBI directory, remove it
222  if(QFile::exists(localPortsTree+".tar.gz") ){ 
223    QString cmd = "rm "+localPortsTree+".tar.gz";
224    system(cmd.toUtf8());
225  }
226  //Inform the user of the result
227  if(exitCode == 0){
228    //Finished successfully
229    QMessageBox::information(this, tr("Success"), tr("The FreeBSD ports tree has been successfully updated") );
230  }else{
231    //Finished with error
232    QString msg = p->readAllStandardError();
233    QMessageBox::information(this, tr("Failure"), QString(tr("The FreeBSD ports tree has failed to update.")+"\n"+tr("Last Message: %1") ).arg(msg) );
234  }
235  checkForPorts(); //Update the interface times and such
236}
Note: See TracBrowser for help on using the repository browser.