source: src-qt4/EasyPBI/config.cpp @ 0c42827

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 0c42827 was 0c42827, checked in by Ken Moore <ken@…>, 8 months ago

Update EasyPBI to 2.3:
Changes Include:
Add ability to build from ports directly again.
Add legacy port-build options to the GUI.
Add item to show current package compile-time options.
Add ability to create a new module from either ports/packages.
Fix a bug in the default wrapper script for 10.x PBI's (container path is /usr/local/ now when running)

  • Property mode set to 100644
File size: 15.9 KB
Line 
1#include "config.h"
2
3Config::Config() {
4    //Clear internal variables
5    badPackages.clear(); exProgStruct.clear(); defaultSettings.clear();
6    valueStruct.clear(); detStruct.clear(); TFstruct.clear();
7    //Setup internal structures
8        //exProgStruct=[ pbi_makeport, pbi_create, su_utility ]
9    exProgStruct << "" << "" << "";
10        //valueStruct=[ pbiOutputDir, moduleOutputDir, pkgCacheDir, portsDir, iconDir, defaultIcon, sigFile ]
11    valueStruct << "" << "" << "" << "" << "" << "" << "";
12        //detStruct=[ pbi_makeport, pbi_create, su_utility, portsDir ]
13    detStruct << "" << "" << "" << "";
14        //defaultSettings=[ current version#, programDir, pbiOutputDir, moduleOutputDir, pkgCacheDir, iconFile, settingsFile, iconDir]
15        QString tmp = QDir::homePath()+"/EasyPBI/";
16    defaultSettings << "2.3" << tmp << tmp+"PBI/" << tmp+"Modules/" << tmp+".cache/" << tmp+"defaulticon.png" << tmp+".preferences" << QDir::homePath();
17        //TFstruct=[ is64-bitArch, portsAvailable, useDigitalSig, useTMPFS, usePkgCache, pbi_makeport_available, pbi_create_available, su_available ]
18    TFstruct << FALSE << FALSE << FALSE << TRUE << TRUE << FALSE << FALSE << FALSE;
19  }
20
21Config::~Config(){
22}
23
24void Config::readMachineArch(){
25  //set the "is64bit" flag
26  QString arch;
27  QProcess p;
28  p.start("uname -m");
29  if (p.waitForFinished()) {
30      while (p.canReadLine()) {
31          arch = p.readLine().simplified();
32      }
33  }
34  qDebug() << "architecture:" << arch;
35  TFstruct[0] = (  arch == "amd64" );   
36}
37
38bool Config::checkDirectoryStructure(){
39 //Check for EasyPBI directory structure and create it if it is not there
40  if( !QDir(defaultSettings[1]).exists() ){
41    qDebug() << "Creating the EasyPBI directory in current user's home folder:" << defaultSettings[1];
42    QDir dir;
43    dir.root();
44    if( !dir.mkdir(defaultSettings[1]) ){ //Create the program directory
45      //Could not create the Program directory
46      qDebug() << " - Error with home directory permissions, could not create directory";
47      return FALSE;
48    }
49  } 
50  if( !QDir(defaultSettings[3]).exists() ){
51    QDir dir;
52    dir.root();
53    if( !dir.mkdir(defaultSettings[3]) ){ //module directory
54      qDebug() << " - Error with directory permissions, could not create directory" << defaultSettings[3];
55      return FALSE;
56    }
57  } 
58  if( !QDir(defaultSettings[2]).exists() ){
59    QDir dir;
60    dir.root();
61    if( !dir.mkdir(defaultSettings[2]) ){ // PBI output directory
62      qDebug() << " - Error with directory permissions, could not create directory" << defaultSettings[2];
63      return FALSE;
64    }
65  } 
66  if( !QDir(defaultSettings[4]).exists() ){
67    QDir dir;
68    dir.root();
69    if( !dir.mkdir(defaultSettings[4]) ){ // pkg cache
70      qDebug() << " - Error with directory permissions, could not create directory" << defaultSettings[4];
71      return FALSE;
72    }
73  } 
74
75  //Check for the defaulticons.png file
76  if( !QFile(defaultSettings[5]).exists() ){
77    //file does not exist, copy the included resource to the file
78    qDebug() << " - Copying the default icon to the EasyPBI directory";
79    QFile::copy(":/png/icons/defaulticon.png",defaultSettings[5]);
80   
81    QFile::setPermissions(defaultSettings[5],QFile::ReadGroup | QFile::WriteGroup | QFile::ReadOwner | QFile::WriteOwner | QFile::ReadOther | QFile::WriteOther);
82  }
83  return TRUE;
84}
85
86void Config::loadSettingsFile(){
87  //Return to the defaults
88  returnToDefaults();
89  //Make sure the preferences file exists, otherwise just load the defaults
90  if( !QFile::exists(defaultSettings[6]) ){ return; }
91  //Read the preferences file
92  QFile file(defaultSettings[6]);
93  //Open the file, otherwise use defaults
94  if( !file.open(QIODevice::ReadOnly | QIODevice::Text) ){ 
95    qDebug() << "Could not open preferences file for reading:" << defaultSettings[6];
96    returnToDefaults();
97    return; 
98  }
99  QTextStream in(&file);
100  QString savedversion;
101  while( !in.atEnd() ){
102    QString line = in.readLine().simplified();
103    //Now parse out the values
104    if( line.startsWith("<version>") ){ savedversion = readSavedValue("<version>",line); }
105    else if( line.startsWith("<pbioutdir>") ){ valueStruct[0] = readSavedValue("<pbioutdir>",line); }
106    else if( line.startsWith("<moduleoutdir>") ){ valueStruct[1] = readSavedValue("<moduleoutdir>",line); }
107    else if( line.startsWith("<cachedir>") ){ valueStruct[2] = readSavedValue("<cachedir>",line); }
108    else if( line.startsWith("<portsdir>") ){ valueStruct[3] = readSavedValue("<portsdir>",line); }
109    else if( line.startsWith("<icondir>") ){ valueStruct[4] = readSavedValue("<icondir>",line); }
110    else if( line.startsWith("<defaulticon>") ){ valueStruct[5] = readSavedValue("<defaulticon>",line); }
111    else if( line.startsWith("<sigfile>") ){ valueStruct[6] = readSavedValue("<sigfile>",line); }
112    else if( line.startsWith("<badpackages>") ){ badPackages = readSavedValue("<badpackages>",line).split(";",QString::SkipEmptyParts); }
113    else if( line.startsWith("<usesignature>") ){ TFstruct[2] = (readSavedValue("<usesignature>",line).toLower() == "true"); }
114    else if( line.startsWith("<usetmpfs>") ){ TFstruct[3] = (readSavedValue("<usetmpfs>",line).toLower() == "true"); }
115    else if( line.startsWith("<usepkgcache>") ){ TFstruct[4] = (readSavedValue("<usepkgcache>",line).toLower() == "true"); }
116    else if( line.startsWith("<pbi_makeport>") ){ exProgStruct[0] = readSavedValue("<pbi_makeport>",line); }
117    else if( line.startsWith("<pbi_create>") ){ exProgStruct[1] = readSavedValue("<pbi_create>",line); }
118    else if( line.startsWith("<su_utility>") ){ exProgStruct[2] = readSavedValue("<su_utility>",line); }
119  }
120  file.close();
121  //Now update any missing values in the structures
122  checkStructures();
123}
124
125void Config::saveSettingsFile(){
126        //exProgStruct=[ pbi_makeport, pbi_create, su_utility ]
127        //detStruct=[ pbi_makeport, pbi_create, su_utility, portsDir ]
128  //Get the preferences file
129  QFile file(defaultSettings[6]);
130  //Open the file, otherwise use defaults
131  if( !file.open(QIODevice::WriteOnly | QIODevice::Text) ){ 
132    qDebug() << "Could not open preferences file for writing:" << defaultSettings[6];
133    return; 
134  }
135  QTextStream out(&file);
136  out << "---AUTOMATICALLY GENERATED---\n---DO NOT MODIFY THIS FILE BY HAND---\n";
137  out << "<version>"+ defaultSettings[0] +"</version>\n";
138  if(valueStruct[0]!=defaultSettings[2]){ out << "<pbioutdir>"+ valueStruct[0] + "</pbioutdir>\n"; }
139  if(valueStruct[1]!=defaultSettings[3]){out << "<moduleoutdir>"+valueStruct[1]+"</moduleoutdir>\n"; }
140  if(valueStruct[2]!=defaultSettings[4]){out << "<cachedir>"+valueStruct[2]+"</cachedir>\n"; }
141  if(valueStruct[3]!=detStruct[3]){out << "<portsdir>"+valueStruct[3]+"</portsdir>\n"; }
142  if(valueStruct[4]!=defaultSettings[7]){out << "<icondir>"+valueStruct[4]+"</icondir>\n"; }
143  if(valueStruct[5]!=defaultSettings[5]){out << "<defaulticon>"+valueStruct[5]+"</defaulticon>\n"; }
144  if(!valueStruct[6].isEmpty()){ out << "<sigfile>"+valueStruct[6]+"</sigfile>\n"; }
145  if(exProgStruct[0] != detStruct[0]){ out << "<pbi_makeport>"+exProgStruct[0]+"</pbi_makeport>\n"; }
146  if(exProgStruct[1] != detStruct[1]){ out << "<pbi_create>"+exProgStruct[1]+"</pbi_create>\n"; }
147  if(exProgStruct[2] != detStruct[2]){ out << "<su_utility>"+exProgStruct[2]+"</su_utility>\n"; }
148  if(TFstruct[2]){ out << "<usesignature>TRUE</usesignature>\n"; }
149  if(TFstruct[3]){ out << "<usetmpfs>TRUE</usetmpfs>\n"; }
150  if(TFstruct[4]){ out << "<usepkgcache>TRUE</usepkgcache>\n"; }
151  if(badPackages.length() >0){ out << "<badpackages>"+badPackages.join(";")+"</badpackages>\n"; }
152  //Now close the file
153  file.close();
154}
155
156void Config::scanForExternalUtilities(){
157  bool sufound = FALSE;
158  bool pbifound1 = FALSE;
159  bool pbifound2 = FALSE;
160  //Setup the commands to look for (lists in order of preference)
161  QStringList suCMD;
162  suCMD << "pc-su" << "qsu" << "gksu" << "kdesu"; //graphical "switch user" utilities
163  QString pbiCMD1 = "pbi_makeport";  //command to create a PBI from ports
164  QString pbiCMD2 = "pbi_create"; //command to create a PBI from local sources
165 
166  //Get the current application path
167  QString cpath = QCoreApplication::applicationDirPath();
168  if(cpath.endsWith("/.sbin")){ cpath.chop(6); } //Fix for PBI installation of EasyPBI
169  //Set the search paths
170  QStringList paths;
171  paths <<"/usr/local/bin/"<<"/usr/local/sbin/"<<"/usr/bin/"<<"/usr/sbin/"<<cpath+"/bin/"<<cpath+"/sbin/";
172
173  //Perform the Search
174  for(int i=0; i<paths.length(); i++){
175    //PBI build commands
176    if(!pbifound1){
177      if(QFile::exists(paths[i]+pbiCMD1)){
178        pbifound1 = TRUE;
179        detStruct[0] = paths[i]+pbiCMD1; //pbi_makeport
180      }
181    }
182    if(!pbifound2){
183      if(QFile::exists(paths[i]+pbiCMD2)){
184        pbifound2 = TRUE;
185        detStruct[1] = paths[i]+pbiCMD2; //pbi_create
186      }
187    }
188    //SU utility
189    if(!sufound){
190      for(int j=0; j<suCMD.length(); j++){
191        if(QFile::exists(paths[i]+suCMD[j])){
192          sufound = TRUE;
193          detStruct[2] = paths[i]+suCMD[j];  //su utility
194          break;
195        }
196      }             
197    }
198  } // end loop over paths
199 
200  //Now search for the FreeBSD ports tree
201    QString ret;
202    QStringList portsLocations;
203    //Set the locations to search for the ports tree
204    portsLocations << "/usr/ports" << defaultSettings[1]+"ports";
205    //Search the locations
206    for(int i=0; i<portsLocations.size(); i++){
207      if( QDir(portsLocations[i]).exists() ){
208        if( QFile::exists(portsLocations[i]+"/COPYRIGHT") ){
209          detStruct[3]=portsLocations[i];
210        }
211      }
212    }
213   
214  //Now Check the structures and set any internal flags
215  checkStructures();
216}
217
218void Config::returnToDefaults(){
219    //Clear the structures
220    for(int i=0; i<valueStruct.length(); i++){ valueStruct[i].clear(); }
221    for(int i=0; i<exProgStruct.length(); i++){ exProgStruct[i].clear(); }
222    for(int i=1; i<TFstruct.length(); i++){ TFstruct[i] = FALSE; } //skip the 64-bit architecture flag
223    //Now re-populate the structures from the defaults
224    checkStructures();
225}
226
227QString Config::value(QString var){
228//defaultSettings=[ current version#, programDir, pbiOutputDir,
229        //moduleOutputDir, pkgCacheDir, iconFile, settingsFile, iconDir]
230    var = var.toLower();
231    if(var=="pbi_makeport"){ return exProgStruct[0]; }
232    else if(var=="pbi_create"){ return exProgStruct[1]; }
233    else if(var=="su_cmd"){ return exProgStruct[2]; }
234    else if(var=="pbidir"){ return valueStruct[0]; }
235    else if(var=="moduledir"){ return valueStruct[1]; }
236    else if(var=="cachedir"){ return valueStruct[2]; }
237    else if(var=="portsdir"){ return valueStruct[3]; }
238    else if(var=="icondir"){ return valueStruct[4]; }
239    else if(var=="defaulticon"){ return valueStruct[5]; }
240    else if(var=="sigfile"){ return valueStruct[6]; }
241    else if(var=="version"){ return defaultSettings[0]; }
242    else if(var=="progdir"){ return defaultSettings[1]; }
243    else{ 
244      qDebug() << "invalid value requested:" << var;
245      return ""; 
246    }
247}
248
249void Config::setValue(QString var, QString val){
250    var = var.toLower();
251    if( !val.isEmpty() ){
252      if( !QFile::exists(val) ){ 
253        qDebug() << "WARNING! \""+var+"\" setting could not be changed: desired file does not exist:" + val;
254      }
255      else if(var=="pbi_makeport"){ exProgStruct[0]=val; }
256      else if(var=="pbi_create"){ exProgStruct[1]=val; }
257      else if(var=="su_cmd"){ exProgStruct[2]=val; }
258      else if(var=="pbidir"){ valueStruct[0]=val; }
259      else if(var=="moduledir"){ valueStruct[1]=val; }
260      else if(var=="cachedir"){ valueStruct[2]=val; }
261      else if(var=="portsdir"){ valueStruct[3]=val; }
262      else if(var=="icondir"){ valueStruct[4]=val; }
263      else if(var=="defaulticon"){ valueStruct[5]=val; }
264      else if(var=="sigfile"){ valueStruct[6]=val; }
265      else{ qDebug() << "invalid setvalue requested:" << var; }
266     
267    }else{ //reset default value
268        //detStruct=[ pbi_makeport, pbi_create, su_utility, portsDir ]
269      if(var=="pbi_makeport"){ exProgStruct[0]=detStruct[0]; }
270      else if(var=="pbi_create"){ exProgStruct[1]=detStruct[1]; }
271      else if(var=="su_cmd"){ exProgStruct[2]=detStruct[2]; }
272      else if(var=="pbidir"){ valueStruct[0]=defaultSettings[2]; }
273      else if(var=="moduledir"){ valueStruct[1]=defaultSettings[3]; }
274      else if(var=="cachedir"){ valueStruct[2]=defaultSettings[4]; }
275      else if(var=="portsdir"){ valueStruct[3]=detStruct[3]; }
276      else if(var=="icondir"){ valueStruct[4]=defaultSettings[7]; }
277      else if(var=="defaulticon"){ valueStruct[5]=defaultSettings[5]; }
278      else if(var=="sigfile"){ valueStruct[6]=""; }
279      else{ qDebug() << "invalid setvalue requested:" << var; }     
280    }
281  checkStructures();
282}
283
284QStringList Config::list(QString var){
285    var = var.toLower();
286    if(var=="skippkgs"){ return badPackages; }
287    else{ 
288      qDebug() << "invalid list requested:"<<var;
289      return QStringList();
290    }
291}
292
293void Config::setList(QString var, QStringList val){
294  var=var.toLower();
295  if(var=="skippkgs"){ badPackages = val; }
296  else{ qDebug() << "invalid setlist requested:"<<var; }
297}
298
299bool Config::check(QString var){
300  var = var.toLower();
301  if(var=="is64bit"){ return TFstruct[0]; }
302  else if(var=="isportsavailable"){ return TFstruct[1]; }
303  else if(var=="usesignature"){ return TFstruct[2]; }
304  else if(var=="usetmpfs"){ return TFstruct[3]; }
305  else if(var=="usecache"){ return TFstruct[4]; }
306  else if(var=="ismakeportavailable"){ return TFstruct[5]; }
307  else if(var=="iscreateavailable"){ return TFstruct[6]; }
308  else if(var=="issuavailable"){ return TFstruct[7]; }
309  else{ 
310    qDebug() << "invalid check requested:" << var;
311    return FALSE; 
312  }
313}
314
315void Config::setCheck(QString var, bool val){
316  var = var.toLower();
317  if(var=="usesignature"){ TFstruct[2] = val; }
318  else if(var=="usetmpfs"){ TFstruct[3] = val; }
319  else if(var=="usecache"){ TFstruct[4] = val; }
320  else{ qDebug() << "invalid setcheck requested:" << var; }
321}
322
323// *********************************************************
324// -----------PRIVATE FUNCTIONS FOR INTERNAL USE-----------
325//**********************************************************
326
327QString Config::readSavedValue(QString var, QString line){
328  //var = "<something>"
329  //line = line with the value on it
330  QString varend = var;
331  varend.replace("<","</");
332  QString out = line.section(var,1,1).section(varend,0,0).simplified();
333  return out;
334}
335
336void Config::checkStructures(){
337  //Check and replace any empty variables with the defaults     
338    //exProgStruct=[ pbi_makeport, pbi_create, su_utility ]
339    //valueStruct=[ pbiOutputDir, moduleOutputDir, pkgCacheDir, portsDir, iconDir, defaultIcon, sigFile ]
340    //detStruct=[ pbi_makeport, pbi_create, su_utility, portsDir ]
341    //defaultSettings=[ current version#, programDir, pbiOutputDir, moduleOutputDir, pkgCacheDir, iconFile, settingsFile, iconDir]
342    //TFstruct=[ is64-bitArch, portsAvailable, useDigitalSig, useTMPFS, usePkgCache, pbi_makeport_available, pbi_create_available, su_available ]
343
344  //Internal Variables
345  // -- PBI output dir
346  if( valueStruct[0].isEmpty() ){ valueStruct[0] = defaultSettings[2]; }
347  // -- Module output dir
348  if( valueStruct[1].isEmpty() ){ valueStruct[1] = defaultSettings[3]; }
349  // -- Package Cache dir
350  if( valueStruct[2].isEmpty() ){ valueStruct[2] = defaultSettings[4]; }
351  // -- FreeBSD ports tree
352  if( valueStruct[3].isEmpty() ){ 
353    if(detStruct[3].isEmpty()){ TFstruct[1] = FALSE; }
354    else{ valueStruct[3] = detStruct[3]; TFstruct[1]=TRUE; }
355  }else if( QFile::exists(valueStruct[3]+"/COPYRIGHT") ){ 
356    TFstruct[1]=TRUE; 
357  }
358 
359  // -- Icon dir
360  if( valueStruct[4].isEmpty() ){ valueStruct[4] = defaultSettings[7]; }
361  // -- default icon file
362  if( valueStruct[5].isEmpty() ){ valueStruct[5] = defaultSettings[5]; }
363 
364  //External utilities
365  // -- pbi_makeport
366  if( exProgStruct[0].isEmpty() ){ 
367    if(detStruct[0].isEmpty()){ TFstruct[5] = FALSE; }
368    else{ exProgStruct[0] = detStruct[0]; TFstruct[5]=TRUE; }
369  }
370  // -- pbi_create
371  if( exProgStruct[1].isEmpty() ){ 
372    if(detStruct[1].isEmpty()){ TFstruct[6] = FALSE; }
373    else{ exProgStruct[1] = detStruct[1]; TFstruct[6]=TRUE; }
374  }
375  // -- graphical su utility
376  if( exProgStruct[2].isEmpty() ){ 
377    if(detStruct[2].isEmpty()){ TFstruct[7] = FALSE; }
378    else{ exProgStruct[2] = detStruct[2]; TFstruct[7]=TRUE; }
379  }
380}
Note: See TracBrowser for help on using the repository browser.