USGS

Isis 3.0 Object Programmers' Reference

Home

Preference.cpp

Go to the documentation of this file.
00001 
00024 #include <fstream>
00025 #include <iostream>
00026 #include <iomanip>
00027 
00028 #include <QDir>
00029 #include <QThreadPool>
00030 
00031 #include "Preference.h"
00032 #include "FileName.h"
00033 #include "IException.h"
00034 
00035 using namespace std;
00036 namespace Isis {
00040   Preference::Preference() : Isis::Pvl() {
00041     atexit(Shutdown);
00042   }
00043 
00044   void Preference::Load(const QString &file) {
00045     // Read the input PVL preference file
00046     Isis::Pvl pvl;
00047 
00048     if(Isis::FileName(file).fileExists()) {
00049       pvl.Read(file);
00050     }
00051 
00052     // Override parameters each time load is called
00053     for(int i = 0; i < pvl.Groups(); i++) {
00054       Isis::PvlGroup &inGroup = pvl.Group(i);
00055       if(this->HasGroup(inGroup.Name())) {
00056         Isis::PvlGroup &outGroup = this->FindGroup(inGroup.Name());
00057         for(int k = 0; k < inGroup.Keywords(); k++) {
00058           Isis::PvlKeyword &inKey = inGroup[k];
00059           while(outGroup.HasKeyword(inKey.Name())) {
00060             outGroup.DeleteKeyword(inKey.Name());
00061           }
00062           outGroup += inKey;
00063         }
00064       }
00065       else {
00066         this->AddGroup(inGroup);
00067       }
00068     }
00069 
00070     // Configure Isis to use the user performance preferences where
00071     //   appropriate.
00072     if (HasGroup("Performance")) {
00073       PvlGroup &performance = FindGroup("Performance");
00074       if (performance.HasKeyword("GlobalThreads")) {
00075         IString threadsPreference = performance["GlobalThreads"][0];
00076 
00077         if (threadsPreference.DownCase() != "optimized") {
00078           // We need a no-iException conversion here
00079           int threads = threadsPreference.ToQt().toInt();
00080 
00081           if (threads > 0) {
00082             QThreadPool::globalInstance()->setMaxThreadCount(threads);
00083           }
00084         }
00085       }
00086     }
00087   }
00088 
00089 
00090   // Instantiation and initialization of static member variables
00091   Preference *Preference::p_preference = NULL;
00092   bool Preference::p_unitTest = false;
00093 
00094 
00095   Preference &Preference::Preferences(bool unitTest) {
00096     if(p_preference == NULL) {
00097       p_unitTest = unitTest;
00098       // Create the singleton
00099       p_preference = new Preference();
00100 
00101       // Make sure the user has a .Isis directory
00102       Isis::FileName setup("$HOME/.Isis");
00103       if(!setup.fileExists()) {
00104         QDir dir;
00105         QString dirName(IString(setup.expanded()).ToQt());
00106         dir.mkdir(dirName);
00107       }
00108 
00109       // If its a unitTest then load with the unitTest preference file
00110       if(unitTest) {
00111         p_preference->Load("$ISISROOT/src/base/objs/Preference/TestPreferences");
00112       }
00113       // Otherwise load the Isis system and personal preferences.
00114       else {
00115         p_preference->Load("$ISISROOT/IsisPreferences");
00116 
00117         Isis::FileName userPref("$HOME/.Isis/IsisPreferences");
00118         if(userPref.fileExists()) {
00119           p_preference->Load("$HOME/.Isis/IsisPreferences");
00120         }
00121       }
00122     }
00123     // Note: Keep this here
00124     // During unitTests some other class may call Preferences before the
00125     // unitTest does. This would cause the preferences to be loaded with
00126     // the system and user preferences instead of the TestPreferences.
00127     else if(unitTest) {
00128       p_unitTest = unitTest;
00129       p_preference->Clear();
00130       p_preference->Load("$ISISROOT/src/base/objs/Preference/TestPreferences");
00131     }
00132 
00133     return *p_preference;
00134   }
00135 
00136   void Preference::Shutdown() {
00137     if(p_preference) {
00138       delete p_preference;
00139       p_preference = NULL;
00140     }
00141   }
00142 } // end namespace isis