1  /*****************************************************************************
2  *                                                                           *
3  *  Elmer, A Finite Element Software for Multiphysical Problems              *
4  *                                                                           *
5  *  Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland    *
6  *                                                                           *
7  *  This program is free software; you can redistribute it and/or            *
8  *  modify it under the terms of the GNU General Public License              *
9  *  as published by the Free Software Foundation; either version 2           *
10  *  of the License, or (at your option) any later version.                   *
11  *                                                                           *
12  *  This program is distributed in the hope that it will be useful,          *
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
15  *  GNU General Public License for more details.                             *
16  *                                                                           *
17  *  You should have received a copy of the GNU General Public License        *
18  *  along with this program (in file fem/GPL-2); if not, write to the        *
19  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,         *
20  *  Boston, MA 02110-1301, USA.                                              *
21  *                                                                           *
22  *****************************************************************************/
23 
24 /*****************************************************************************
25  *                                                                           *
26  *  ElmerGUI egini                                                           *
27  *                                                                           *
28  *****************************************************************************
29  *                                                                           *
30  *  Authors: Mikko Lyly, Juha Ruokolainen and Peter Råback                   *
31  *  Email:   Juha.Ruokolainen@csc.fi                                         *
32  *  Web:     http://www.csc.fi/elmer                                         *
33  *  Address: CSC - IT Center for Science Ltd.                                 *
34  *           Keilaranta 14                                                   *
35  *           02101 Espoo, Finland                                            *
36  *                                                                           *
37  *  Original Date: 15 Mar 2008                                               *
38  *                                                                           *
39  *****************************************************************************/
40 
41 #include <iostream>
42 #include "egini.h"
43 
44 using namespace std;
45 
EgIni(QWidget * parent)46 EgIni::EgIni(QWidget *parent)
47   : QDialog(parent)
48 {
49   iniLoaded = false;
50 
51   // Determine ini-file location and name:
52   //--------------------------------------
53   QString elmerGuiHome;
54   QString paraViewHome;
55 
56 #ifdef __APPLE__DONTGOHERE_TODO
57   //QString iniFileName = this->homePath +  "/edf/egini.xml";
58   QString iniFileName = QDir::homePath() +  "/edf/egini.xml";
59 #else
60   QString iniFileName = QCoreApplication::applicationDirPath() + "/../share/ElmerGUI/edf/egini.xml";  // @TODO: fix path to share/ElmerGUI/edf
61 
62   elmerGuiHome = QString(getenv("ELMERGUI_HOME"));
63 
64   if(!elmerGuiHome.isEmpty())
65     iniFileName = elmerGuiHome + "/edf/egini.xml";
66 
67   paraViewHome = QString(getenv("PARAVIEW_HOME"))+"/bin";
68 #endif
69 
70   // Load initialization file:
71   //---------------------------
72 #if WITH_QT5
73   cout << "Load " << string(iniFileName.toLatin1()) << "...";
74 #else
75   cout << "Load " << string(iniFileName.toAscii()) << "...";
76 #endif
77   cout.flush();
78 
79   QFile file(iniFileName);
80   QString errStr;
81   int errRow;
82   int errCol;
83 
84   if(!file.exists()) {
85 
86     QMessageBox::information(window(), tr("Eg ini-file loader: ") + iniFileName,
87 			     tr("Initialization file does not exist"));
88     return;
89 
90   } else {
91 
92     if(!iniDoc.setContent(&file, true, &errStr, &errRow, &errCol)) {
93 
94       QMessageBox::information(window(), tr("Eg ini-file loader: ") + iniFileName,
95 			       tr("Parse error at line %1, col %2:\n%3")
96 			       .arg(errRow).arg(errCol).arg(errStr));
97       file.close();
98       return;
99 
100     } else {
101 
102       if(iniDoc.documentElement().tagName() != "egini") {
103 	QMessageBox::information(window(), tr("Eg ini-file loader: ") + iniFileName,
104 				 tr("This is not an eg initialization file"));
105 	file.close();
106 	return;
107       }
108     }
109   }
110 
111   cout << " done" << endl;
112   file.close();
113   iniLoaded = true;
114 }
115 
116 
~EgIni()117 EgIni::~EgIni()
118 {
119 }
120 
121 
isPresent(QString tag)122 bool EgIni::isPresent(QString tag)
123 {
124   if(!iniLoaded)
125     return false;
126 
127   root = iniDoc.documentElement();
128   element = root.firstChildElement(tag);
129 
130   if(element.isNull())
131     return false;
132 
133   return true;
134 }
135 
136 
isSet(QString tag)137 bool EgIni::isSet(QString tag)
138 {
139   if(!iniLoaded)
140     return false;
141 
142   root = iniDoc.documentElement();
143   element = root.firstChildElement(tag);
144 
145   if(element.isNull())
146     return false;
147 
148   if(element.text().trimmed() != "0")
149     return true;
150 
151   return false;
152 }
153 
154 
value(QString tag)155 QString EgIni::value(QString tag)
156 {
157   if(!iniLoaded)
158     return "";
159 
160   root = iniDoc.documentElement();
161   element = root.firstChildElement(tag);
162 
163   if(element.isNull())
164     return "";
165 
166   return element.text().trimmed();
167 }
168