"Fossies" - the Fresh Open Source Software Archive

Member "tipograf-0.5/src/init.cc" (13 Dec 2003, 7323 Bytes) of package /linux/privat/old/tipograf-0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <stdlib.h>
    2 
    3 #include <qtranslator.h>
    4 #include <qapplication.h>
    5 #include <qfile.h>
    6 #include <qstring.h>
    7 #include <qmessagebox.h>
    8 #include <qtextstream.h>
    9 #include <qstringlist.h>
   10 #include <qstylefactory.h>
   11 
   12 #include "init.h"
   13 #include "firststartImpl.h"
   14 
   15 extern QApplication * appTipo;
   16 extern QTranslator  * transTipo;
   17 
   18 // ============================================================
   19 /** This function initializes Tipograf.
   20  *  It checks if a config file ~/.tipografrc
   21  *  exists and reads out the language setting.
   22  *  Otherwise it assumes that this is the first
   23  *  start of the application and displays the
   24  *  initial setup dialog.
   25  *  @return false on normal operation
   26  *  @return true on first start
   27  */
   28 tipoInit * init()
   29 {
   30   tipoInit * tiInitVals= new tipoInit;            // Structure for return values used in constructor.
   31   QString sLangFile    = "";                      // Name of the Qt message file.
   32   QString sCfgFileName = resolveConfigFileName(); // Name of config file (exits on error).
   33   QStringList slLanguageTxt;                      // List of supported languages.
   34   QStringList slGUIStyleTxt;                      // List of supported GUI styles.
   35   QFile fCfg(sCfgFileName);                       // Config file.
   36   QString sLine     = "";                         // Line read from config file.
   37   int iEqualPos     = 0;                          // Position of the equal sign within the line.
   38   int iConvInt      = 0;                          // Converted int.
   39   bool bConvOk      = false;                      // Holds the return value of conversions.
   40   QString sParseErr = appTipo->tr("The following errors did occur\n"
   41                   "while parsing the configuration\n"
   42                   "file:\n\n");   // Beginning of the parse error message.
   43   bool bParseErr    = false;                      // Signals a parse error.
   44 
   45   slLanguageTxt += appTipo->tr("English");
   46   slLanguageTxt += appTipo->tr("Deutsch");
   47 
   48   slGUIStyleTxt = QStyleFactory::keys();          // All styles supported by this version of Qt.
   49 
   50   /*  ========================================
   51    *  First start init.
   52    *  ========================================
   53    */
   54   if(!fCfg.exists()) {
   55     // Ask for language setting.
   56     firststartImpl fsi(slLanguageTxt,
   57                slGUIStyleTxt,
   58                0, "fsi", true);
   59     fsi.exec();
   60     switch(fsi.getLanguage()) {
   61     case 0:
   62       // English is the default.
   63       break;
   64     case 1:
   65       sLangFile = "tipograf_de.qm";
   66 #ifdef TRANSDIR
   67       transTipo->load(sLangFile, TRANSDIR);
   68 #else
   69 #error TRANSDIR not defined. Translator will not work.
   70 #endif
   71       appTipo->installTranslator(transTipo);
   72       break;
   73     default:
   74       // Error.
   75       QMessageBox::critical(0,
   76                 appTipo->tr("Initialization"),
   77                 appTipo->tr("The first start dialog returned\n"
   78                     "a language value which is not\n"
   79                     "supported. This is a bug in\n"
   80                     "file init.cc, function init()."));
   81       exit(-1);
   82       break;
   83     }
   84     // Set GUI style.
   85     QApplication::setStyle(QStyleFactory::create(slGUIStyleTxt[fsi.getGUIStyle()]));
   86 
   87     tiInitVals->bFirstStart       = true;
   88     tiInitVals->iTipoLanguageInit = fsi.getLanguage();
   89     tiInitVals->iGUIStyleInit     = fsi.getGUIStyle();
   90     tiInitVals->sPathA2psInit     = "a2ps";
   91     tiInitVals->sPathGvInit       = "gv";
   92     return(tiInitVals);
   93   } // end of first start.
   94 
   95 
   96   /*  ========================================
   97    *  Normal start init.
   98    *  ========================================
   99    */
  100   // Unable to open file for reading.
  101   if(!fCfg.open(IO_ReadOnly)) {
  102     QMessageBox::critical(0,
  103               appTipo->tr("Initialization"),
  104               appTipo->tr("File: ")
  105               + fCfg.name() +
  106               appTipo->tr("\nTipograf is unable to open the file\n"
  107                       "for reading. This means Tipograf cannot\n"
  108                       "load its settings from that file. The\n"
  109                       "directory part of the path is supposed to\n"
  110                       "be your home directory. If this is not the\n"
  111                       "case you must set the \"HOME\" variable to\n"
  112                       "a reasonable value in order to run\n"
  113                       "Tipograf successfully.\n\n"
  114                       "In a BASH or SH shell you can\n"
  115                       "accomplish this by the following\n"
  116                       "command:\n"
  117                       "export HOME=/path/to/my/directory\n\n"
  118                       "In a CSH or TCSH shell you can\n"
  119                       "accomplish this by the following\n"
  120                       "command:\n"
  121                       "setenv HOME /path/to/my/directory\n\n"
  122                       "After setting the \"HOME\" variable\n"
  123                       "come back and restart Tipograf."));
  124     exit(-1);
  125   }
  126   // File opened successfully.
  127   QTextStream ts(&fCfg);
  128   // Parse file.
  129   bParseErr = false;
  130   while(!ts.eof()) {
  131     sLine = ts.readLine();
  132     if(sLine.contains("=")) {
  133       iEqualPos = sLine.find("=");
  134 
  135       // Extract TipoLanguage.
  136       if(sLine.contains("cbTipoLanguage")) {
  137     sLine = sLine.remove(0, iEqualPos+1);
  138     iConvInt = sLine.toInt(&bConvOk, 10);
  139     if(bConvOk == true) {
  140       if(iConvInt==0 || iConvInt==1) {
  141         if(iConvInt==1) {
  142           sLangFile = "tipograf_de.qm";
  143 #ifdef TRANSDIR
  144           transTipo->load(sLangFile, TRANSDIR);
  145 #else
  146           // This should never happen...
  147           transTipo->load(sLangFile, ".");
  148 #endif
  149           appTipo->installTranslator(transTipo);
  150         }
  151       } else {
  152         bParseErr = true;
  153         sParseErr += appTipo->tr("Tipo language index out of range.\n");
  154       }
  155     } else {
  156       bParseErr = true;
  157       sParseErr += appTipo->tr("Tipo language index is not a number.\n");
  158     }
  159       }
  160 
  161     } // end if contains '='
  162   } // end while
  163   
  164   // Close file.
  165   fCfg.close();
  166 
  167   // Check for parse error.
  168   if(bParseErr) {
  169     QMessageBox::critical(0,
  170               appTipo->tr("Initialization"),
  171               sParseErr);
  172     exit(-1);
  173   }
  174   
  175   tiInitVals->bFirstStart       = false;
  176   tiInitVals->iTipoLanguageInit = 0;
  177   tiInitVals->iGUIStyleInit     = 0;
  178   tiInitVals->sPathA2psInit     = "";
  179   tiInitVals->sPathGvInit       = "";
  180   return(tiInitVals);
  181 }
  182 
  183 // ============================================================
  184 /** Figures out absolute path to config file.
  185  *  It checks the HOME env variable via getenv.
  186  *  In case getenv returns an error it kills the
  187  *  program since it does not make sense to run 
  188  *  user applications without HOME being set.
  189  *  @return $HOME/.tipografrc
  190  */
  191 QString resolveConfigFileName()
  192 {
  193   QString sConfigFileName = "";
  194   // Query HOME env variable.
  195   if(!getenv("HOME")) {
  196     QMessageBox::critical(0,
  197               appTipo->tr("Resolve config file name"),
  198               appTipo->tr("The environment variable \"HOME\"\n"
  199                       "appears not to be set. This means\n"
  200                       "Tipograf is not able to identify\n"
  201                       "your home directory in which it must\n"
  202                       "place its configuration file. You\n"
  203                       "must set the \"HOME\" variable to a\n"
  204                       "reasonable value in order to run\n"
  205                       "Tipograf.\n\n"
  206                       "In a BASH or SH shell you can\n"
  207                       "accomplish this by the following\n"
  208                       "command:\n"
  209                       "export HOME=/path/to/my/directory\n\n"
  210                       "In a CSH or TCSH shell you can\n"
  211                       "accomplish this by the following\n"
  212                       "command:\n"
  213                       "setenv HOME /path/to/my/directory\n\n"
  214                       "After setting the \"HOME\" variable\n"
  215                       "come back and restart Tipograf."));
  216     exit(-1);
  217   }
  218   QString sHomeVal = getenv("HOME");
  219   sConfigFileName  = sHomeVal + "/.tipografrc";
  220   return(sConfigFileName);
  221 }