c46d5a3136a13df88d0bc640c878dac489ea53ed
[vlp.git] / src / lgconfig / lgconfig.cpp
1
2
3 #include <qapp.h>
4 #include <qframe.h>
5 #include <qmlined.h>
6 #include <qmenubar.h>
7 #include <qpopmenu.h>
8 #include <qdialog.h>
9 #include <qbttngrp.h>
10 #include <qlabel.h>
11 #include <qlined.h>
12 #include <qlistbox.h>
13 #include <qpushbt.h>
14 #include <qradiobt.h>
15 #include <qlist.h>
16 #include <qfile.h>
17 #include <qcombo.h>
18 #include <qtooltip.h>
19 #include <qfont.h>
20 #include <qpixmap.h>
21 #include <qmsgbox.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <libconfig.h>
27
28 class VLPEntry
29 {
30 public:
31  int ID;
32  char addr[255];
33  int type;  /* 0 - explicit */
34  char progdir[255],homedir[255];
35  char item[255];
36 };
37
38 class QInstall: public QFrame
39 {
40  Q_OBJECT
41 public:
42   QMenuBar *bar;  
43   QListBox *nodelist;
44   QPushButton *infob;
45   QList<VLPEntry> Nodes;
46
47   QInstall();
48   bool check_id(int);
49   bool check_addr(char*);
50
51 public slots:
52
53   void SetOptions();
54   void AddNode();
55   void DelNode();
56   void Info();
57
58 };
59
60
61 QApplication *app;
62
63 QInstall::QInstall()
64 {
65   QFont f("Helvetica",12,QFont::Bold);
66   QPixmap mp;
67
68   infob = new QPushButton(this);
69   
70  
71
72   bar = new QMenuBar(this);
73   bar->insertItem("Configure",this,SLOT(SetOptions()));
74   bar->insertItem("Quit",app,SLOT(quit()));
75   bar->setFont(f);
76   setCaption("VLP Configuration Tool");
77   infob->setGeometry(0,bar->height(),200,30);
78   if( mp.load("logo.bmp"))
79  {
80   infob->setPixmap(mp);
81   infob->resize(mp.width(),mp.height());
82
83  }
84   resize(infob->width(),infob->height()+bar->height());
85   Nodes.clear();
86 }
87
88
89 bool QInstall::check_id(int id)
90 {
91  VLPEntry *pom;
92
93  pom = Nodes.first();
94  while (pom!=NULL)
95  {
96   if (pom->ID == id) return(FALSE);
97   pom=Nodes.next();
98   }
99  return(TRUE);
100 }
101
102
103
104 bool QInstall::check_addr(char *addr)
105 {
106  VLPEntry *pom;
107
108  pom = Nodes.first();
109  while (pom!=NULL)
110  {
111   if (strcmp(pom->addr,addr) == 0) return(FALSE);
112   pom=Nodes.next();
113   }
114  return(TRUE);
115 }
116
117
118 void QInstall::Info()
119 {
120 }
121
122
123 void QInstall::AddNode()
124 {
125  QDialog dlg(this,"",TRUE);
126  QLabel* tmpQLabel;
127  QLineEdit *id,*addr,*progs,*home;
128  QPushButton *okbtn,*cancelbtn;
129  VLPEntry *pom;
130  char pomstr[255];
131
132 tmpQLabel = new QLabel( &dlg, "Label_2" );
133 tmpQLabel->setGeometry( 110, 10, 180, 30 );
134 tmpQLabel->setFrameStyle( 49 );
135 tmpQLabel->setText( "Virtual Processor Properties" );
136
137 id = new QLineEdit( &dlg, "LineEdit_1" );
138 id->setGeometry( 130, 50, 50, 30 );
139 id->setText( "" );
140
141 tmpQLabel = new QLabel( &dlg, "Label_3" );
142 tmpQLabel->setGeometry( 20, 50, 90, 30 );
143 tmpQLabel->setText( "Node number" );
144
145 tmpQLabel = new QLabel( &dlg, "Label_4" );
146 tmpQLabel->setGeometry( 20, 90, 80, 30 );
147 tmpQLabel->setText( "IP Address" );
148
149 addr = new QLineEdit( &dlg, "LineEdit_2" );
150 addr->setGeometry( 130, 90, 120, 30 );
151 addr->setText( "" );
152
153 tmpQLabel = new QLabel( &dlg, "Label_5" );
154 tmpQLabel->setGeometry( 20, 130, 100, 30 );
155 tmpQLabel->setText( "Connection type" );
156
157 QComboBox* tmpQComboBox;
158 tmpQComboBox = new QComboBox( FALSE, &dlg, "ComboBox_1" );
159 tmpQComboBox->setGeometry( 130, 130, 100, 30 );
160 tmpQComboBox->setSizeLimit( 2 );
161 tmpQComboBox->setAutoResize( FALSE );
162 tmpQComboBox->insertItem( "Explicit" );
163
164 tmpQLabel = new QLabel( &dlg, "Label_6" );
165 tmpQLabel->setGeometry( 20, 170, 110, 30 );
166 tmpQLabel->setText( "Programs directory" );
167
168 progs = new QLineEdit( &dlg, "LineEdit_4" );
169 progs->setGeometry( 130, 170, 230, 30 );
170 progs->setText( "" );
171
172 tmpQLabel = new QLabel( &dlg, "Label_7" );
173 tmpQLabel->setGeometry( 20, 210, 100, 30 );
174 tmpQLabel->setText( "VLP directory" );
175
176 home = new QLineEdit( &dlg, "LineEdit_5" );
177 home->setGeometry( 130, 210, 230, 30 );
178 home->setText( "" );
179
180         okbtn = new QPushButton( &dlg, "PushButton_5" );
181         okbtn->setGeometry( 80, 250, 100, 30 );
182         okbtn->setText( "Ok" );
183         connect(okbtn,SIGNAL(clicked()),&dlg,SLOT(accept()));
184         cancelbtn = new QPushButton( &dlg, "PushButton_6" );
185         cancelbtn->setGeometry( 210, 250, 100, 30 );
186         cancelbtn->setText( "Cancel" );
187         connect(cancelbtn,SIGNAL(clicked()),&dlg,SLOT(reject()));
188         dlg.resize( 380, 300 );
189     if (dlg.exec())
190     {
191      pom = new VLPEntry;
192      pom->ID = atoi(id->text());
193      if  (check_id(pom->ID))
194      { 
195       strcpy(pom->addr,addr->text());
196       if (check_addr(pom->addr))
197      {
198     
199      if (strcmp(tmpQComboBox->currentText(),"Explicit")==0) pom->type=0;
200      strcpy(pom->progdir,progs->text());
201      strcpy(pom->homedir,home->text());
202      Nodes.append(pom);
203      sprintf(pomstr,"Node: %d       Addr:%s       Home dir: %s",pom->ID,pom->addr,pom->homedir);
204      nodelist->insertItem(pomstr);      
205      strcpy(pom->item,pomstr);
206      } else QMessageBox::message("Error!","Only one VLP on a single computer!","Ok");
207      }
208       else QMessageBox::message("Error!","ID must be unique!","Ok");
209       }
210
211 }
212
213 void QInstall::DelNode()
214 {
215  char pom[255];
216  VLPEntry *vpom;
217
218  if (nodelist->currentItem()>=0)
219  {
220    strcpy(pom,nodelist->text(nodelist->currentItem()));
221    vpom = Nodes.first();
222    while (vpom!=NULL)
223     {
224       if (strcmp(pom,vpom->item)==0) break;
225       vpom = Nodes.next();
226      }
227    if (vpom!=NULL)
228        if (QMessageBox::query("Delete VLP","Are you sure?","Yes","No"))
229        {
230         nodelist->removeItem(nodelist->currentItem());
231         Nodes.remove(vpom);
232         }
233   }
234 }
235
236
237 void QInstall::SetOptions()
238 {
239   QDialog dlg(this,"",TRUE);
240   QLabel* tmpQLabel;
241   QPushButton *addbtn,*delbtn,*okbtn,*cancelbtn;
242   VLPEntry *pom;
243   int i,j;
244   char pomstr[255];
245         
246         //dlg.setStyle(WindowsStyle);
247         nodelist = new QListBox( &dlg, "ListBox_1" );
248         nodelist->setGeometry( 20, 40, 480, 160 );
249
250         tmpQLabel = new QLabel( &dlg, "Label_1" );
251         tmpQLabel->setGeometry( 20, 10, 100, 30 );
252         tmpQLabel->setText( "Nodes:" );
253
254         addbtn = new QPushButton( &dlg, "PushButton_1" );
255         addbtn->setGeometry( 30, 210, 100, 30 );
256         addbtn->setText( "Add VLP" );
257         connect(addbtn,SIGNAL(clicked()),this,SLOT(AddNode()));
258         delbtn = new QPushButton( &dlg, "PushButton_2" );
259         delbtn->setGeometry( 150, 210, 100, 30 );
260         delbtn->setText( "Del VLP" );
261         connect(delbtn,SIGNAL(clicked()),this,SLOT(DelNode()));
262         okbtn = new QPushButton( &dlg, "PushButton_3" );
263         okbtn->setGeometry( 270, 210, 100, 30 );
264         okbtn->setText( "Save files" );
265         connect(okbtn,SIGNAL(clicked()),&dlg,SLOT(accept()));
266         cancelbtn = new QPushButton( &dlg, "PushButton_4" );
267         cancelbtn->setGeometry( 390, 210, 100, 30 );
268         cancelbtn->setText( "Cancel" );
269         connect(cancelbtn,SIGNAL(clicked()),&dlg,SLOT(reject()));
270         dlg.resize( 520, 260 );
271
272
273          if (dlg.exec()){
274               if (!Nodes.isEmpty())
275             {
276               pom = Nodes.first();
277               while (pom!=NULL)
278               {
279                 j = Nodes.at();
280                 sprintf(pomstr,"%s.cfg",pom->addr);
281                 
282                 config_t cfg;
283                 config_setting_t *root, *setting;
284                 config_init(&cfg);
285                 
286                 root = config_root_setting(&cfg);
287                 
288                 setting = config_setting_get_member(root, "progdir");
289                 if(!setting) {
290                   setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
291                 }
292                 config_setting_set_string(setting, pom->progdir);
293                 
294                 setting = config_setting_get_member(root, "homedir");
295                 if(!setting) {
296                   setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
297                 }
298                 config_setting_set_string(setting, pom->homedir);
299                 
300                 setting = config_setting_get_member(root, "node_number");
301                 if(!setting) {
302                   setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
303                 }
304                 config_setting_set_int(setting, pom->ID);
305                 
306                 if (pom->type == 0)
307                 {
308                   setting = config_setting_get_member(root, "type");
309                   if(!setting) {
310                     setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
311                   }
312                   config_setting_set_string(setting, "explicit");
313                 }
314                 for(i=0;i<Nodes.count();i++)
315                 {
316                   if (pom!=Nodes.at(i))
317                   {
318                     setting = config_setting_get_member(root, "host");
319                     if(!setting) {
320                       setting = config_setting_add(root, "host", CONFIG_TYPE_STRING);
321                     }
322                     config_setting_set_string(setting, Nodes.at(i)->addr);
323                   };
324                 }
325                 
326                 if(!config_write_file(&cfg, pomstr))
327                 {
328                     fprintf(stderr, "Error while writing to file: %s.\n", pomstr);
329                 }                
330                 config_destroy(&cfg);
331                 
332                 pom = Nodes.at(j);
333                 pom = Nodes.next();
334                }
335              }
336
337             };
338
339
340
341 }
342
343
344 #include "lgconfig.moc"
345
346 int main( int argc, char **argv )
347 {
348
349     app = new QApplication(argc,argv);
350     QInstall cfg;
351     //app->setStyle(WindowsStyle);
352     app->setMainWidget(&cfg);
353     cfg.show();
354     return app->exec();
355 }