source: src-qt4/EasyPBI/preferencesDialog.cpp @ e177abf

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since e177abf was e177abf, checked in by Ken Moore <ken@…>, 20 months ago

Update EasyPBI to version 2.1
Changes: FreeBSD ports dialog merged into the settings dialog, save buttons now dynamicly enabled, use portsnap for all ports fetching/updating

  • Property mode set to 100644
File size: 16.7 KB
Line 
1#include "preferencesDialog.h"
2#include "ui_preferencesDialog.h"
3#include <QDebug>
4
5PreferencesDialog::PreferencesDialog(QWidget *parent) :
6    QDialog(parent),
7    ui(new Ui::PreferencesDialog)
8{
9       
10       //Setup Initial state of GUI objects and connect signal/slots
11       ui->setupUi(this);  //load the main UI from designer
12        //Setup the dialog icons
13        ui->push_pbioutdir->setIcon(Backend::icon("load"));
14        ui->push_sigfile->setIcon(Backend::icon("file"));
15        ui->push_clearcache->setIcon(Backend::icon("clear"));
16        ui->push_moduledir->setIcon(Backend::icon("load"));
17        ui->push_icondir->setIcon(Backend::icon("load"));
18        ui->push_defaulticon->setIcon(Backend::icon("file"));
19        ui->push_suutility->setIcon(Backend::icon("file"));
20        ui->push_makeport->setIcon(Backend::icon("file"));
21        ui->push_pbicreate->setIcon(Backend::icon("file"));
22        ui->push_resetutils->setIcon(Backend::icon("refresh"));
23        ui->tool_otherPortsDir->setIcon(Backend::icon("load"));
24        ui->push_updatePorts->setIcon(Backend::icon("refresh"));
25       
26        //Setup the internal settings structure
27        settings = new Config();
28        settings->scanForExternalUtilities();   
29        settings->loadSettingsFile();
30        //initialize the QProcess
31        process = new QProcess(this);
32        process->setProcessEnvironment( QProcessEnvironment::systemEnvironment() );
33        connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(updateStatusBar()) );
34        connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)) );
35        //Put the current settings into the GUI
36        resetGUI();
37}
38
39PreferencesDialog::~PreferencesDialog(){
40}
41
42//Setup the UI
43void PreferencesDialog::resetGUI(){
44  //Put the current settings into all the dialog widgets
45        //pbi tab
46        ui->line_pbioutdir->setText(settings->value("pbidir"));
47        ui->check_signpbi->setChecked(settings->check("usesignature"));
48        ui->line_sigfile->setText(settings->value("sigfile"));
49        ui->check_usetmpfs->setChecked(settings->check("usetmpfs"));
50        ui->check_usepkgcache->setChecked(settings->check("usecache"));
51        ui->list_badpkg->clear();
52        ui->list_badpkg->addItems(settings->list("skippkgs"));
53        //paths tab
54        ui->line_moduledir->setText(settings->value("moduledir"));
55        ui->line_icondir->setText(settings->value("icondir"));
56        ui->line_defaulticon->setText(settings->value("defaulticon"));
57        ui->line_suutility->setText(settings->value("su_cmd"));
58        ui->line_pbicreate->setText(settings->value("pbi_create"));
59        ui->line_makeport->setText(settings->value("pbi_makeport"));
60        //ports tab
61        ui->list_portsDirectories->clear();
62        QStringList pDirs; pDirs<< tr("Current User (~/EasyPBI/ports)") << tr("All Users (/usr/ports)") << tr("Other");
63        ui->list_portsDirectories->addItems(pDirs);
64        QString portsdir = settings->value("portsdir");
65        if(portsdir.endsWith("/")){ portsdir.chop(1); }
66        if(portsdir == (QDir::homePath()+"/EasyPBI/ports") ){
67          ui->list_portsDirectories->setCurrentIndex(0); //User dir
68        }else if(portsdir == "/usr/ports"){
69          ui->list_portsDirectories->setCurrentIndex(1); //system dir   
70        }else{
71          ui->list_portsDirectories->setCurrentIndex(2); //other
72          ui->line_otherPortsDir->setText(portsdir);
73        }
74        ui->progressBar->setMaximum(0); ui->progressBar->setMinimum(0); //default to a "loading" indicator
75        updateGUI();
76}
77
78void PreferencesDialog::updateGUI(){
79        //Now adjust the visibility of the necessary elements
80        //pbi tab
81        if( ui->check_signpbi->isChecked() ){ ui->groupBox_sig->setVisible(TRUE); }
82        else{ ui->groupBox_sig->setVisible(FALSE); }
83        if( ui->check_usepkgcache->isChecked() ){ ui->groupBox_pkgcache->setVisible(TRUE); }
84        else{ ui->groupBox_pkgcache->setVisible(FALSE); }
85        //ports tab
86        int pIndex = ui->list_portsDirectories->currentIndex();
87        if(pIndex == 2){
88          ui->line_otherPortsDir->setVisible(TRUE); ui->tool_otherPortsDir->setVisible(TRUE);
89        }else{
90          ui->line_otherPortsDir->setVisible(FALSE); ui->tool_otherPortsDir->setVisible(FALSE); 
91        }
92        QString pDir;
93        switch (pIndex){
94        case 0: 
95                pDir = QDir::homePath()+"/EasyPBI/ports"; break;
96        case 1:
97                pDir = "/usr/ports"; break;
98        default:
99                pDir = ui->line_otherPortsDir->text();
100        }       
101        QString pUpdate;
102        if(!pDir.isEmpty()){ pUpdate = getLastPortsUpdate(pDir); } //check for existance and last update
103        if( !pUpdate.isEmpty() ){
104          ui->push_fetchPorts->setVisible(FALSE);
105          ui->push_updatePorts->setVisible(TRUE);
106          ui->label_portsUpdated->setText( pUpdate );
107        }else{
108          ui->push_fetchPorts->setVisible(TRUE);
109          ui->push_updatePorts->setVisible(FALSE);
110          ui->label_portsUpdated->setText( tr("Nonexistent") );
111        }
112        if(process->state() == QProcess::NotRunning){
113          ui->list_portsDirectories->setEnabled(TRUE);
114          ui->line_otherPortsDir->setEnabled(TRUE); ui->tool_otherPortsDir->setEnabled(TRUE);
115          ui->label_status->setVisible(FALSE); ui->progressBar->setVisible(FALSE);
116          ui->push_fetchPorts->setEnabled(TRUE); ui->push_updatePorts->setEnabled(TRUE);
117          ui->buttonBox->setEnabled(TRUE);
118        }else{
119          ui->list_portsDirectories->setEnabled(FALSE);
120          ui->line_otherPortsDir->setEnabled(FALSE); ui->tool_otherPortsDir->setEnabled(FALSE);
121          ui->label_status->setVisible(TRUE); ui->progressBar->setVisible(TRUE);
122          ui->push_fetchPorts->setEnabled(FALSE); ui->push_updatePorts->setEnabled(FALSE);
123          ui->buttonBox->setEnabled(FALSE);
124        }
125}
126
127//Save/Cancel buttons
128void PreferencesDialog::on_buttonBox_accepted(){
129    //Save the settings in the GUI to the stucture
130    settings->setValue("pbidir", ui->line_pbioutdir->text() );
131    settings->setCheck("usesignature", ui->check_signpbi->isChecked() );
132    settings->setValue("sigfile", ui->line_sigfile->text() );
133    settings->setCheck("usetmpfs", ui->check_usetmpfs->isChecked() );
134    settings->setCheck("usecache", ui->check_usepkgcache->isChecked() );
135    QStringList badpkgs;
136    for(int i=0; i<ui->list_badpkg->count(); i++){
137      badpkgs << ui->list_badpkg->item(i)->text();
138    }
139    badpkgs.removeDuplicates();
140    settings->setList("skippkgs", badpkgs);
141    QString portsDir;
142    int pIndex = ui->list_portsDirectories->currentIndex();
143    if( pIndex == 0 ){portsDir = QDir::homePath()+"/EasyPBI/ports"; }
144    else if( pIndex == 1){ portsDir = "/usr/ports"; }
145    else{ portsDir = ui->line_otherPortsDir->text(); }   
146    settings->setValue("portsdir", portsDir );
147    settings->setValue("moduledir", ui->line_moduledir->text() );
148    settings->setValue("icondir", ui->line_icondir->text() );
149    settings->setValue("defaulticon", ui->line_defaulticon->text() );
150    settings->setValue("su_cmd", ui->line_suutility->text() );
151    settings->setValue("pbi_create", ui->line_pbicreate->text() );
152    settings->setValue("pbi_makeport", ui->line_makeport->text() );
153    //Now save the settings to disk
154    settings->saveSettingsFile();
155    //Now close the dialog
156    this->close();
157}
158
159void PreferencesDialog::on_buttonBox_rejected(){
160    //Don't save any of the settings and just close the dialog
161    this->close();
162}
163
164// ===== PBI Settings Tab =====
165void PreferencesDialog::on_push_pbioutdir_clicked(){
166  //Get the desired directory
167  QString seldir = QFileDialog::getExistingDirectory(this, tr("Select PBI Output Directory"), QDir::homePath() );
168  if(seldir.isEmpty()){ return; } //action cancelled
169  //Save the selection to the GUI
170  ui->line_pbioutdir->setText(seldir); 
171}
172
173void PreferencesDialog::on_push_sigfile_clicked(){
174  //Get the desired directory
175  QString seldir = QFileDialog::getOpenFileName(this, tr("Select Digital Signature File"), QDir::homePath(), tr("Signature Files (*)") );
176  if(seldir.isEmpty()){ return; } //action cancelled
177  //Save the selection to the GUI
178  ui->line_sigfile->setText(seldir); 
179}
180
181void PreferencesDialog::on_push_addpkg_clicked(){
182  //Get the desired directory
183  QString seldir = QFileDialog::getOpenFileName(this, tr("Select the Package to Ignore"), settings->value("cachedir"), tr("Package Files (*.txz)") );
184  if(seldir.isEmpty()){ return; } //action cancelled
185  //get just the package name (ignore the version and extension)
186  QString pkg = seldir.remove(seldir.section("-",-1));
187  pkg.append("*");
188  //Save the selection to the GUI
189  ui->list_badpkg->addItem(pkg); 
190}
191
192void PreferencesDialog::on_push_rmpkg_clicked(){
193  //get the currently selected item
194  int row = ui->list_badpkg->currentRow();
195  if(row == -1){ return; } //nothing selected
196  //Remove it from the list
197  delete ui->list_badpkg->takeItem(row);
198}
199
200void PreferencesDialog::on_push_clearcache_clicked(){
201  //Verify that this is what the user really wants to do
202  QMessageBox verify(this);
203  verify.setText(tr("Are you sure you wish to clear the PBI package cache?"));
204  verify.setInformativeText(tr("This will delete all the files and directories in the cache directory. The next time you start a PBI build, it will begin to rebuild all the packages from source."));
205  verify.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
206  verify.setDefaultButton(QMessageBox::Yes);
207  int ret = verify.exec();
208  if(ret != QMessageBox::Yes){return;}
209  //Remove all files/directories in the EasyPBI cache dir
210  QString cmd = "cd "+settings->value("cachedir")+"; rm -rf *";
211  system(cmd.toUtf8());
212}
213
214void PreferencesDialog::on_check_signpbi_stateChanged(int state){
215  if(state == Qt::Checked){ ui->groupBox_sig->setVisible(TRUE); }
216  else{ ui->groupBox_sig->setVisible(FALSE); }
217}
218
219void PreferencesDialog::on_check_usepkgcache_stateChanged(int state){
220  if(state == Qt::Checked){ ui->groupBox_pkgcache->setVisible(TRUE); }
221  else{ ui->groupBox_pkgcache->setVisible(FALSE); }
222}
223
224// ===== Paths Tab ===== 
225void PreferencesDialog::on_push_moduledir_clicked(){
226  //Get the desired directory
227  QString seldir = QFileDialog::getExistingDirectory(this, tr("Select Default Module Directory"), QDir::homePath() );
228  if(seldir.isEmpty()){ return; } //action cancelled
229  //Save the selection to the GUI
230  ui->line_moduledir->setText(seldir); 
231}
232
233void PreferencesDialog::on_push_icondir_clicked(){
234  //Get the desired directory
235  QString seldir = QFileDialog::getExistingDirectory(this, tr("Select Default Directory To Find Icon Files"), QDir::homePath() );
236  if(seldir.isEmpty()){ return; } //action cancelled
237  //Save the selection to the GUI
238  ui->line_icondir->setText(seldir); 
239}
240void PreferencesDialog::on_push_defaulticon_clicked(){
241  //Get the desired directory
242  QString seldir = QFileDialog::getOpenFileName(this, tr("Select Default Icon FIle"), QDir::homePath(), tr("PNG Images (*.png)") );
243  if(seldir.isEmpty()){ return; } //action cancelled
244  //Save the selection to the GUI
245  ui->line_defaulticon->setText(seldir); 
246}
247
248// ===== Utilities Tab =====
249void PreferencesDialog::on_push_suutility_clicked(){
250  //Get the desired directory
251  QString seldir = QFileDialog::getOpenFileName(this, tr("Select Graphical Switch-User Utility"), QDir::homePath() );
252  if(seldir.isEmpty()){ return; } //action cancelled
253  //Save the selection to the GUI
254  ui->line_suutility->setText(seldir); 
255}
256
257void PreferencesDialog::on_push_pbicreate_clicked(){
258  //Get the desired directory
259  QString seldir = QFileDialog::getOpenFileName(this, tr("Select 'pbi_create' File Location"), QDir::homePath(), "(pbi_create)" );
260  if(seldir.isEmpty()){ return; } //action cancelled
261  //Save the selection to the GUI
262  ui->line_pbicreate->setText(seldir); 
263}
264
265void PreferencesDialog::on_push_makeport_clicked(){
266  //Get the desired directory
267  QString seldir = QFileDialog::getOpenFileName(this, tr("Select 'pbi_makeport' File Location"), QDir::homePath(), "(pbi_makeport)" );
268  if(seldir.isEmpty()){ return; } //action cancelled
269  //Save the selection to the GUI
270  ui->line_makeport->setText(seldir); 
271}
272
273void PreferencesDialog::on_push_resetutils_clicked(){
274  //Verify that this is what the user really wants to do
275  QMessageBox verify(this);
276  verify.setText(tr("Are you sure you wish to return to the automatically detected utility paths?"));
277  verify.setInformativeText(tr("This will remove any customized path settings for the external utilities."));
278  verify.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
279  verify.setDefaultButton(QMessageBox::Yes);
280  int ret = verify.exec();
281  if(ret != QMessageBox::Yes){return;}
282  //Rescan for external utilities
283  settings->scanForExternalUtilities();
284  //Reset the values in the structure (will revert to detected values if any)
285  settings->setValue("pbi_makeport","");
286  settings->setValue("pbi_create","");
287  settings->setValue("su_cmd","");
288  //Reset the utilities on the GUI     
289  ui->line_suutility->setText(settings->value("su_cmd"));
290  ui->line_pbicreate->setText(settings->value("pbi_create"));
291  ui->line_makeport->setText(settings->value("pbi_makeport"));
292}
293
294// ======  Ports Tab ======
295void PreferencesDialog::on_list_portsDirectories_currentIndexChanged(int index){
296  updateGUI(); 
297}
298
299void PreferencesDialog::on_line_otherPortsDir_textChanged(QString text){
300  updateGUI(); 
301}
302void PreferencesDialog::on_tool_otherPortsDir_clicked(){
303  //Get the desired directory
304  QString seldir = QFileDialog::getExistingDirectory(this, tr("Select FreeBSD Ports Directory"), QDir::homePath() );
305  if(seldir.isEmpty()){ return; } //action cancelled
306  //Save the selection to the GUI
307  ui->line_otherPortsDir->setText(seldir);
308  updateGUI();
309}
310
311void PreferencesDialog::on_push_fetchPorts_clicked(){
312  int pIndex = ui->list_portsDirectories->currentIndex();
313  QString pDir;
314    if( pIndex == 0 ){pDir = QDir::homePath()+"/EasyPBI/ports"; }
315    else if( pIndex == 1){ pDir = "/usr/ports"; }
316    else{ pDir = ui->line_otherPortsDir->text(); }
317  fetchPorts(pDir,TRUE); //is a new ports directory
318  updateGUI();
319}
320
321void PreferencesDialog::on_push_updatePorts_clicked(){
322  int pIndex = ui->list_portsDirectories->currentIndex();
323  QString pDir;
324    if( pIndex == 0 ){pDir = QDir::homePath()+"/EasyPBI/ports"; }
325    else if( pIndex == 1){ pDir = "/usr/ports"; }
326    else{ pDir = ui->line_otherPortsDir->text(); }
327  fetchPorts(pDir,FALSE); //is an existing ports directory
328  updateGUI();
329}
330   
331void PreferencesDialog::updateStatusBar(){
332  QString tmp = process->readAllStandardOutput();
333  if(tmp.endsWith("\n")){ tmp.chop(1); }
334  tmp = tmp.section("\n",-1);
335  tmp = tmp.remove("\n").simplified();
336  //qDebug() << "Status Update:" << tmp;
337  ui->label_status->clear();
338  ui->label_status->setText(tmp);
339}
340
341void PreferencesDialog::processFinished(int exitCode,QProcess::ExitStatus status){
342  qDebug() << " - Ports update finished:" << exitCode << status;
343  //Inform the user of the result
344  if(exitCode == 0){
345    //Finished successfully
346    QMessageBox::information(this, tr("Success"), tr("The FreeBSD ports tree has been successfully updated") );
347  }else{
348    //Finished with error
349    QString msg = process->readAllStandardError();
350    if(msg.isEmpty()){ msg = process->readAllStandardOutput(); }
351    QMessageBox::information(this, tr("Failure"), QString(tr("The FreeBSD ports tree has failed to update.")+"\n"+tr("Last Message: %1") ).arg(msg) );
352  }
353  updateGUI(); //Update the interface times and such
354}
355
356QString PreferencesDialog::getLastPortsUpdate(QString ports){
357  QDir dir(ports);
358  QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, QDir::Time); //sort by most recently changed
359  QString out;
360  if(list.length() > 2){ //good ports tree
361    out = list[0].lastModified().toString();
362  }
363  return out;
364}
365
366void PreferencesDialog::fetchPorts(QString portsDir, bool isNew){
367  bool needRoot = TRUE;
368  QString localPSDB = QDir::homePath()+"/EasyPBI/.portSnapDB";
369  if( portsDir.startsWith( QDir::homePath() )){ needRoot = FALSE; }
370 
371  // == Generate the System Command ==
372  QString cmd;
373  bool usePortsnap = FALSE;
374  //Graphical SU utility as needed
375  if(needRoot){ 
376    ui->label_status->setText(tr("Requesting Root Access")); //Put placeholder info
377    cmd = ui->line_suutility->text() + " \""; //use the switch-user utility
378  }
379  //Figure out whether to use portsnap or not
380  if( !isNew && QFile::exists(portsDir+"/.svn") ){
381    //Use Subversion
382    cmd.append("svn update "+portsDir);
383  }else{
384    //Use Portsnap
385    cmd.append("portsnap fetch ");
386    if(isNew){ cmd.append("extract "); }
387    else{ cmd.append("update "); }
388    cmd.append("-p "+portsDir);
389    if(!needRoot){ cmd.append(" -d "+localPSDB); }
390    usePortsnap=TRUE;
391   
392  }
393  if(needRoot){ cmd.append("\""); } //close the quotes
394 
395  //If using portsnap, make sure the intermediate file is there in the EasyPBI directory
396  if( usePortsnap ){
397    QString pspath = QDir::homePath()+"/EasyPBI/.portsnapNI.sh"; // Non-Interactive (NI) version of portsnap
398    if( !QFile::exists(pspath) ){
399      //Create the Non-Interactive version of portsnap if it does not exist yet
400      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;
401      //qDebug() << "system call:" << pscmd;
402      system(pscmd.toUtf8());
403    }
404    if(!needRoot && !QFile::exists(localPSDB)){
405      //Create the local portsnap database directory
406      QString tmpcmd = "mkdir "+localPSDB;
407      system(tmpcmd.toUtf8());
408    }
409    cmd.replace("portsnap",pspath);
410  }
411  //Now run the command
412  process->start(cmd);
413  qDebug() << "Ports fetch started: CMD:" << cmd;       
414}
Note: See TracBrowser for help on using the repository browser.