Added upstream version.
[vlp.git] / 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
27 class VLPEntry
28 {
29 public:
30  int ID;
31  char addr[255];
32  int type;  /* 0 - explicit */
33  char progdir[255],homedir[255];
34  char item[255];
35 };
36
37 class QInstall: public QFrame
38 {
39  Q_OBJECT
40 public:
41   QMenuBar *bar;  
42   QListBox *nodelist;
43   QPushButton *infob;
44   QList<VLPEntry> Nodes;
45
46   QInstall();
47   bool check_id(int);
48   bool check_addr(char*);
49
50 public slots:
51
52   void SetOptions();
53   void AddNode();
54   void DelNode();
55   void Info();
56
57 };
58
59
60 QApplication *app;
61
62 QInstall::QInstall()
63 {
64   QFont f("Helvetica",12,QFont::Bold);
65   QPixmap mp;
66
67   infob = new QPushButton(this);
68   
69  
70
71   bar = new QMenuBar(this);
72   bar->insertItem("Configure",this,SLOT(SetOptions()));
73   bar->insertItem("Quit",app,SLOT(quit()));
74   bar->setFont(f);
75   setCaption("VLP Configuration Tool");
76   infob->setGeometry(0,bar->height(),200,30);
77   if( mp.load("logo.bmp"))
78  {
79   infob->setPixmap(mp);
80   infob->resize(mp.width(),mp.height());
81
82  }
83   resize(infob->width(),infob->height()+bar->height());
84   Nodes.clear();
85 }
86
87
88 bool QInstall::check_id(int id)
89 {
90  VLPEntry *pom;
91
92  pom = Nodes.first();
93  while (pom!=NULL)
94  {
95   if (pom->ID == id) return(FALSE);
96   pom=Nodes.next();
97   }
98  return(TRUE);
99 }
100
101
102
103 bool QInstall::check_addr(char *addr)
104 {
105  VLPEntry *pom;
106
107  pom = Nodes.first();
108  while (pom!=NULL)
109  {
110   if (strcmp(pom->addr,addr) == 0) return(FALSE);
111   pom=Nodes.next();
112   }
113  return(TRUE);
114 }
115
116
117 void QInstall::Info()
118 {
119 }
120
121
122 void QInstall::AddNode()
123 {
124  QDialog dlg(this,"",TRUE);
125  QLabel* tmpQLabel;
126  QLineEdit *id,*addr,*progs,*home;
127  QPushButton *okbtn,*cancelbtn;
128  VLPEntry *pom;
129  char pomstr[255];
130
131 tmpQLabel = new QLabel( &dlg, "Label_2" );
132 tmpQLabel->setGeometry( 110, 10, 180, 30 );
133 tmpQLabel->setFrameStyle( 49 );
134 tmpQLabel->setText( "Virtual Processor Properties" );
135
136 id = new QLineEdit( &dlg, "LineEdit_1" );
137 id->setGeometry( 130, 50, 50, 30 );
138 id->setText( "" );
139
140 tmpQLabel = new QLabel( &dlg, "Label_3" );
141 tmpQLabel->setGeometry( 20, 50, 90, 30 );
142 tmpQLabel->setText( "Node number" );
143
144 tmpQLabel = new QLabel( &dlg, "Label_4" );
145 tmpQLabel->setGeometry( 20, 90, 80, 30 );
146 tmpQLabel->setText( "IP Address" );
147
148 addr = new QLineEdit( &dlg, "LineEdit_2" );
149 addr->setGeometry( 130, 90, 120, 30 );
150 addr->setText( "" );
151
152 tmpQLabel = new QLabel( &dlg, "Label_5" );
153 tmpQLabel->setGeometry( 20, 130, 100, 30 );
154 tmpQLabel->setText( "Connection type" );
155
156 QComboBox* tmpQComboBox;
157 tmpQComboBox = new QComboBox( FALSE, &dlg, "ComboBox_1" );
158 tmpQComboBox->setGeometry( 130, 130, 100, 30 );
159 tmpQComboBox->setSizeLimit( 2 );
160 tmpQComboBox->setAutoResize( FALSE );
161 tmpQComboBox->insertItem( "Explicit" );
162
163 tmpQLabel = new QLabel( &dlg, "Label_6" );
164 tmpQLabel->setGeometry( 20, 170, 110, 30 );
165 tmpQLabel->setText( "Programs directory" );
166
167 progs = new QLineEdit( &dlg, "LineEdit_4" );
168 progs->setGeometry( 130, 170, 230, 30 );
169 progs->setText( "" );
170
171 tmpQLabel = new QLabel( &dlg, "Label_7" );
172 tmpQLabel->setGeometry( 20, 210, 100, 30 );
173 tmpQLabel->setText( "VLP directory" );
174
175 home = new QLineEdit( &dlg, "LineEdit_5" );
176 home->setGeometry( 130, 210, 230, 30 );
177 home->setText( "" );
178
179         okbtn = new QPushButton( &dlg, "PushButton_5" );
180         okbtn->setGeometry( 80, 250, 100, 30 );
181         okbtn->setText( "Ok" );
182         connect(okbtn,SIGNAL(clicked()),&dlg,SLOT(accept()));
183         cancelbtn = new QPushButton( &dlg, "PushButton_6" );
184         cancelbtn->setGeometry( 210, 250, 100, 30 );
185         cancelbtn->setText( "Cancel" );
186         connect(cancelbtn,SIGNAL(clicked()),&dlg,SLOT(reject()));
187         dlg.resize( 380, 300 );
188     if (dlg.exec())
189     {
190      pom = new VLPEntry;
191      pom->ID = atoi(id->text());
192      if  (check_id(pom->ID))
193      { 
194       strcpy(pom->addr,addr->text());
195       if (check_addr(pom->addr))
196      {
197     
198      if (strcmp(tmpQComboBox->currentText(),"Explicit")==0) pom->type=0;
199      strcpy(pom->progdir,progs->text());
200      strcpy(pom->homedir,home->text());
201      Nodes.append(pom);
202      sprintf(pomstr,"Node: %d       Addr:%s       Home dir: %s",pom->ID,pom->addr,pom->homedir);
203      nodelist->insertItem(pomstr);      
204      strcpy(pom->item,pomstr);
205      } else QMessageBox::message("Error!","Only one VLP on a single computer!","Ok");
206      }
207       else QMessageBox::message("Error!","ID must be unique!","Ok");
208       }
209
210 }
211
212 void QInstall::DelNode()
213 {
214  char pom[255];
215  VLPEntry *vpom;
216
217  if (nodelist->currentItem()>=0)
218  {
219    strcpy(pom,nodelist->text(nodelist->currentItem()));
220    vpom = Nodes.first();
221    while (vpom!=NULL)
222     {
223       if (strcmp(pom,vpom->item)==0) break;
224       vpom = Nodes.next();
225      }
226    if (vpom!=NULL)
227        if (QMessageBox::query("Delete VLP","Are you sure?","Yes","No"))
228        {
229         nodelist->removeItem(nodelist->currentItem());
230         Nodes.remove(vpom);
231         }
232   }
233 }
234
235
236 void QInstall::SetOptions()
237 {
238   QDialog dlg(this,"",TRUE);
239   QLabel* tmpQLabel;
240   QPushButton *addbtn,*delbtn,*okbtn,*cancelbtn;
241   VLPEntry *pom;
242   QFile *vlp_file;
243   int i,j;
244   char pomstr[255],line[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                 vlp_file = new QFile(pomstr);
282                 vlp_file->open(IO_WriteOnly);
283                 sprintf(line,"progdir=%s\n",pom->progdir);
284                 vlp_file->writeBlock(line,strlen(line));
285                 sprintf(line,"homedir=%s\n",pom->homedir);
286                 vlp_file->writeBlock(line,strlen(line));
287                 sprintf(line,"node_number=%d\n",pom->ID);
288                 vlp_file->writeBlock(line,strlen(line));
289                 if ( pom->type == 0)
290                  {
291                    sprintf(line,"type=explicit\n");
292                    vlp_file->writeBlock(line,strlen(line));
293                   }
294                 for(i=0;i<Nodes.count();i++)
295                   if (pom!=Nodes.at(i)) {
296                                          sprintf(line,"host=%s\n",Nodes.at(i)->addr);
297                                          vlp_file->writeBlock(line,strlen(line));
298                                           }; 
299                 vlp_file->close();
300                 pom = Nodes.at(j);
301                 pom = Nodes.next();
302                }
303              }
304
305             };
306
307
308
309 }
310
311
312 #include "lgconfig.moc"
313
314 int main( int argc, char **argv )
315 {
316
317     app = new QApplication(argc,argv);
318     QInstall cfg;
319     app->setStyle(WindowsStyle);
320     app->setMainWidget(&cfg);
321     cfg.show();
322     return app->exec();
323 }