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

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since f2b22c1 was 47239e7, checked in by Ken Moore <ken@…>, 15 months ago

Adjust a couple user messages that used to reference the old Ports dialog, and remove a few compiler warnings about unused variables.

  • Property mode set to 100644
File size: 16.8 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  index = 0; //Remove compiler warning about unused variable
297  updateGUI(); 
298}
299
300void PreferencesDialog::on_line_otherPortsDir_textChanged(QString text){
301  text.clear(); //Remove compiler warning about unused variable
302  updateGUI(); 
303}
304void PreferencesDialog::on_tool_otherPortsDir_clicked(){
305  //Get the desired directory
306  QString seldir = QFileDialog::getExistingDirectory(this, tr("Select FreeBSD Ports Directory"), QDir::homePath() );
307  if(seldir.isEmpty()){ return; } //action cancelled
308  //Save the selection to the GUI
309  ui->line_otherPortsDir->setText(seldir);
310  updateGUI();
311}
312
313void PreferencesDialog::on_push_fetchPorts_clicked(){
314  int pIndex = ui->list_portsDirectories->currentIndex();
315  QString pDir;
316    if( pIndex == 0 ){pDir = QDir::homePath()+"/EasyPBI/ports"; }
317    else if( pIndex == 1){ pDir = "/usr/ports"; }
318    else{ pDir = ui->line_otherPortsDir->text(); }
319  fetchPorts(pDir,TRUE); //is a new ports directory
320  updateGUI();
321}
322
323void PreferencesDialog::on_push_updatePorts_clicked(){
324  int pIndex = ui->list_portsDirectories->currentIndex();
325  QString pDir;
326    if( pIndex == 0 ){pDir = QDir::homePath()+"/EasyPBI/ports"; }
327    else if( pIndex == 1){ pDir = "/usr/ports"; }
328    else{ pDir = ui->line_otherPortsDir->text(); }
329  fetchPorts(pDir,FALSE); //is an existing ports directory
330  updateGUI();
331}
332   
333void PreferencesDialog::updateStatusBar(){
334  QString tmp = process->readAllStandardOutput();
335  if(tmp.endsWith("\n")){ tmp.chop(1); }
336  tmp = tmp.section("\n",-1);
337  tmp = tmp.remove("\n").simplified();
338  //qDebug() << "Status Update:" << tmp;
339  ui->label_status->clear();
340  ui->label_status->setText(tmp);
341}
342
343void PreferencesDialog::processFinished(int exitCode,QProcess::ExitStatus status){
344  qDebug() << " - Ports update finished:" << exitCode << status;
345  //Inform the user of the result
346  if(exitCode == 0){
347    //Finished successfully
348    QMessageBox::information(this, tr("Success"), tr("The FreeBSD ports tree has been successfully updated") );
349  }else{
350    //Finished with error
351    QString msg = process->readAllStandardError();
352    if(msg.isEmpty()){ msg = process->readAllStandardOutput(); }
353    QMessageBox::information(this, tr("Failure"), QString(tr("The FreeBSD ports tree has failed to update.")+"\n"+tr("Last Message: %1") ).arg(msg) );
354  }
355  updateGUI(); //Update the interface times and such
356}
357
358QString PreferencesDialog::getLastPortsUpdate(QString ports){
359  QDir dir(ports);
360  QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, QDir::Time); //sort by most recently changed
361  QString out;
362  if(list.length() > 2){ //good ports tree
363    out = list[0].lastModified().toString();
364  }
365  return out;
366}
367
368void PreferencesDialog::fetchPorts(QString portsDir, bool isNew){
369  bool needRoot = TRUE;
370  QString localPSDB = QDir::homePath()+"/EasyPBI/.portSnapDB";
371  if( portsDir.startsWith( QDir::homePath() )){ needRoot = FALSE; }
372 
373  // == Generate the System Command ==
374  QString cmd;
375  bool usePortsnap = FALSE;
376  //Graphical SU utility as needed
377  if(needRoot){ 
378    ui->label_status->setText(tr("Requesting Root Access")); //Put placeholder info
379    cmd = ui->line_suutility->text() + " \""; //use the switch-user utility
380  }
381  //Figure out whether to use portsnap or not
382  if( !isNew && QFile::exists(portsDir+"/.svn") ){
383    //Use Subversion
384    cmd.append("svn update "+portsDir);
385  }else{
386    //Use Portsnap
387    cmd.append("portsnap fetch ");
388    if(isNew){ cmd.append("extract "); }
389    else{ cmd.append("update "); }
390    cmd.append("-p "+portsDir);
391    if(!needRoot){ cmd.append(" -d "+localPSDB); }
392    usePortsnap=TRUE;
393   
394  }
395  if(needRoot){ cmd.append("\""); } //close the quotes
396 
397  //If using portsnap, make sure the intermediate file is there in the EasyPBI directory
398  if( usePortsnap ){
399    QString pspath = QDir::homePath()+"/EasyPBI/.portsnapNI.sh"; // Non-Interactive (NI) version of portsnap
400    if( !QFile::exists(pspath) ){
401      //Create the Non-Interactive version of portsnap if it does not exist yet
402      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;
403      //qDebug() << "system call:" << pscmd;
404      system(pscmd.toUtf8());
405    }
406    if(!needRoot && !QFile::exists(localPSDB)){
407      //Create the local portsnap database directory
408      QString tmpcmd = "mkdir "+localPSDB;
409      system(tmpcmd.toUtf8());
410    }
411    cmd.replace("portsnap",pspath);
412  }
413  //Now run the command
414  process->start(cmd);
415  qDebug() << "Ports fetch started: CMD:" << cmd;       
416}
Note: See TracBrowser for help on using the repository browser.