1 /* Batch.c */
2 /**********************************************************************************************************
3 Copyright (c) 2002-2013 Abdul-Rahman Allouche. All rights reserved
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6 documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all copies or substantial portions
11 of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
16 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 DEALINGS IN THE SOFTWARE.
18 ************************************************************************************************************/
19
20
21 #include "../../Config.h"
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "../Common/Global.h"
26 #include "../Utils/Utils.h"
27 #include "../Utils/UtilsInterface.h"
28 #include "../Utils/Constants.h"
29 #include "../Utils/GabeditTextEdit.h"
30 #include "../Common/Run.h"
31 #include "../Common/Windows.h"
32 #include "../NetWork/Batch.h"
33 #include "RemoteCommand.h"
34 #ifndef G_OS_WIN32
35 #include <stdio.h>
36 #include <string.h>
37 #include <pwd.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #endif /* G_OS_WIN32*/
41
42 static GtkWidget* WinUserBatch = NULL;
43 static gchar selectedRow[100] = "-1";
44 static GtkWidget* EntryRemote = NULL;
45 static GtkWidget* EntryLogin = NULL;
46 static GtkWidget* EntryPassWord = NULL;
47 static gboolean Remote = FALSE;
48 static gchar* RemoteHost = NULL;
49 static gchar* RemoteUser = NULL;
50 static gchar* RemotePassWord = NULL;
51
52 static gint Nlist = 0;
53 static gint NlistTitle = 0;
54 static gchar** Titles = NULL;
55 static gchar*** List = NULL;
56
57 /********************************************************************************/
destroy_win_user_batch(GtkWidget * Win,gpointer data)58 static void destroy_win_user_batch(GtkWidget* Win, gpointer data)
59 {
60 destroy_children(Win);
61 WinUserBatch = NULL;
62 }
63 /********************************************************************************/
init_list()64 static void init_list()
65 {
66 Nlist = 0;
67 NlistTitle = 0;
68 Titles = NULL;
69 List = NULL;
70 }
71 /********************************************************************************/
free_list()72 static void free_list()
73 {
74 gint i;
75 gint j;
76 if(!Nlist || !NlistTitle)
77 {
78 init_list();
79 return;
80 }
81 for(i=0;i<NlistTitle;i++)
82 if(Titles[i])
83 g_free(Titles[i]);
84 if(Titles)
85 g_free(Titles);
86
87 for(i=0;i<Nlist;i++)
88 {
89 for(j=0;j<NlistTitle;j++)
90 if(List[i][j])
91 g_free(List[i][j]);
92 if(List[i])
93 g_free(List[i]);
94 }
95 if(List)
96 g_free(List);
97
98 init_list();
99
100 }
101 /********************************************************************************/
get_list_from_file(gchar * namefile)102 static void get_list_from_file(gchar* namefile)
103 {
104 FILE *fd;
105 gchar *t = NULL;
106 gint taille = BSIZE;
107 gint i;
108
109 free_list();
110 t=g_malloc(taille);
111
112 fd = FOpen(namefile, "r");
113 if(fd)
114 {
115 Titles = g_malloc(14*sizeof(gchar*));
116 for(i=0;i<14;i++)
117 {
118 Titles[i] = g_malloc(100*sizeof(gchar));
119 sprintf(Titles[i]," ");
120 }
121
122 NlistTitle = 14;
123 while(!feof(fd))
124 {
125 if(fgets(t,taille, fd))
126 {
127 if(!strstr(t,NamejobIdTitleBatch)) continue;
128
129 NlistTitle = sscanf(t,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s",
130 Titles[0],Titles[1],Titles[2],Titles[3],Titles[4],Titles[5],Titles[6],
131 Titles[7],Titles[8],Titles[9],Titles[10],Titles[11],Titles[12],Titles[13]);
132 if(strstr(t,"Job") && strstr(t,"id"))
133 {
134 gint i;
135 gint n=-1;
136 for(i=0;i<NlistTitle;i++)
137 {
138 if(strstr(Titles[i],"id"))
139 {
140 n = i;
141 }
142 }
143 if(n>0)
144 {
145 sprintf(Titles[n-1],"%s","Job id");
146 for(i=n;i<NlistTitle-1;i++)
147 sprintf(Titles[i],"%s",Titles[i+1]);
148 g_free(Titles[NlistTitle-1]);
149 NlistTitle--;
150 }
151 }
152 if(strstr(t," Time") && strstr(t,"Use"))
153 {
154 gint i;
155 gint n=-1;
156 for(i=0;i<NlistTitle;i++)
157 {
158 if(strstr(Titles[i],"Use"))
159 {
160 n = i;
161 }
162 }
163 if(n>0)
164 {
165 sprintf(Titles[n-1],"%s","Time Use");
166 for(i=n;i<NlistTitle-1;i++)
167 sprintf(Titles[i],"%s",Titles[i+1]);
168 g_free(Titles[NlistTitle-1]);
169 NlistTitle--;
170 }
171 }
172 break;
173 }
174 }
175 if(NlistTitle==0)
176 {
177 free_list();
178 return;
179 }
180 while(!feof(fd))
181 {
182 if(!fgets(t,taille, fd))
183 break;
184 if(List == NULL)
185 {
186 List = g_malloc(sizeof(gchar**));
187 List[0] = g_malloc(14*sizeof(gchar*));
188 for(i=0;i<14;i++)
189 List[0][i] = g_malloc(100*sizeof(gchar));
190 }
191 else
192 {
193 List = g_realloc(List,(Nlist+1)*sizeof(gchar**));
194 List[Nlist] = g_malloc(14*sizeof(gchar*));
195 for(i=0;i<14;i++)
196 List[Nlist][i] = g_malloc(100*sizeof(gchar));
197 }
198 i = sscanf(t,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s",
199 List[Nlist][0],List[Nlist][1],List[Nlist][2],List[Nlist][3],
200 List[Nlist][4],List[Nlist][5],List[Nlist][6],List[Nlist][7],
201 List[Nlist][8],List[Nlist][9],List[Nlist][10],List[Nlist][11],
202 List[Nlist][12],List[Nlist][13]);
203 if(i>1 && !strstr(List[Nlist][0],"--------") )
204 Nlist++;
205
206 if(i<2) break;
207 }
208 fclose(fd);
209 unlink (namefile);
210 }
211 g_free(t);
212 }
213 /********************************************************************************/
get_num_colonne_pid()214 static gint get_num_colonne_pid()
215 {
216 gint i = 0;
217
218 for(i=0;i<NlistTitle;i++)
219 {
220 if(strstr(Titles[i],NamejobIdTitleBatch))
221 return i;
222 }
223 return -1;
224 }
225 /********************************************************************************/
create_label_frame(GtkWidget * box,gchar * rowPath)226 static GtkWidget* create_label_frame(GtkWidget *box, gchar* rowPath)
227 {
228 GtkWidget *frame;
229 GtkWidget *vboxframe;
230 GtkWidget *Label;
231 gchar *title = NULL;
232 gchar *rowbatch = NULL;
233 gchar *str = NULL;
234 gint num = -1;
235 gint row = atoi(rowPath);
236
237 num = get_num_colonne_pid();
238 if(num>=0)
239 {
240 title = g_strdup(Titles[num]);
241 rowbatch = g_strdup(List[row][num]);
242 }
243 else
244 {
245 title = g_strdup(" ");
246 rowbatch = g_strdup(" ");
247 }
248 str = g_strdup_printf("%s : %s",title,rowbatch);
249 g_free(title);
250 g_free(rowbatch);
251
252 frame = gtk_frame_new (_("Jobs to kill"));
253 gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
254 gtk_box_pack_start (GTK_BOX( box), frame, TRUE, TRUE, 3);
255 gtk_widget_show (frame);
256
257 vboxframe = create_vbox(frame);
258 Label = gtk_label_new(str);
259 set_font (Label,FontsStyleResult.fontname);
260 gtk_label_set_justify(GTK_LABEL(Label),GTK_JUSTIFY_LEFT);
261 gtk_box_pack_start (GTK_BOX(vboxframe), Label, FALSE, FALSE, 3);
262 g_free(str);
263
264 return frame;
265 }
266 /********************************************************************************/
get_pid(gint row)267 static gchar *get_pid(gint row)
268 {
269 gint num = get_num_colonne_pid();
270 gchar*pid;
271 if(num>=0)
272 pid = g_strdup(List[row][num]);
273 else
274 pid = g_strdup(" ");
275
276 return pid;
277
278 }
279 /********************************************************************************/
kill_batch(GtkWidget * Win,gpointer data)280 static void kill_batch(GtkWidget *Win,gpointer data)
281 {
282 gchar *command = NULL;
283 gchar *scom = NULL;
284 gchar *t = NULL;
285 gchar *fout = g_strdup_printf("%s%stmp%sfout",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
286 gchar *ferr = g_strdup_printf("%s%stmp%sferr",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
287
288
289 scom = g_strdup(NameCommandBatchKill);
290
291 command = g_strdup_printf("%s %s",scom,get_pid(atoi(selectedRow)));
292
293 if(Remote)
294 {
295 remote_command (fout,ferr,command,RemoteHost,RemoteUser,RemotePassWord);
296 t = cat_file(ferr,FALSE);
297 }
298 else
299 {
300 t = run_command(command);
301 }
302 if(WinUserBatch)
303 {
304 destroy_children(WinUserBatch);
305 WinUserBatch = NULL;
306 run_batch_user(Remote,RemoteUser,RemoteHost,RemotePassWord);
307 }
308 if(t)
309 g_free(t);
310
311 g_free(scom);
312 g_free(command);
313 g_free(ferr);
314 g_free(fout);
315
316 }
317 /********************************************************************************/
create_kill_batch(GtkWidget * Win,gchar * rowPath)318 static void create_kill_batch(GtkWidget*Win, gchar* rowPath)
319 {
320 GtkWidget *fp;
321 GtkWidget *frame;
322 GtkWidget *vboxall;
323 GtkWidget *vboxwin;
324 GtkWidget *hbox;
325 GtkWidget *button;
326 gchar *title = g_strdup_printf(_("Kill a jobs"));
327
328 /* Principal Window */
329 fp = gtk_window_new(GTK_WINDOW_TOPLEVEL);
330 gtk_window_set_title(GTK_WINDOW(fp),title);
331 gtk_window_set_position(GTK_WINDOW(fp),GTK_WIN_POS_CENTER);
332 gtk_window_set_transient_for(GTK_WINDOW(fp),GTK_WINDOW(Fenetre));
333 gtk_window_set_modal (GTK_WINDOW (fp), TRUE);
334
335 gtk_widget_realize(fp);
336 init_child(fp,gtk_widget_destroy,_(" Kill "));
337 g_signal_connect(G_OBJECT(fp),"delete_event",(GCallback)destroy_children,NULL);
338
339 gtk_container_set_border_width (GTK_CONTAINER (fp), 5);
340 vboxall = create_vbox(fp);
341 vboxwin = vboxall;
342
343 frame = gtk_frame_new (NULL);
344 gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
345 gtk_frame_set_shadow_type( GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
346 gtk_container_add(GTK_CONTAINER(vboxall),frame);
347 gtk_widget_show (frame);
348
349 vboxall = create_vbox(frame);
350
351 hbox = create_hbox(vboxall);
352 frame = create_label_frame(hbox,rowPath);
353
354 hbox = create_hbox(vboxwin);
355 gtk_widget_realize(fp);
356
357 button = create_button(fp,_("Cancel"));
358 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
359 gtk_box_pack_start (GTK_BOX( hbox), button, TRUE, TRUE, 3);
360 g_signal_connect_swapped(G_OBJECT(button), "clicked",(GCallback)destroy_children,GTK_OBJECT(fp));
361 gtk_widget_show (button);
362
363 button = create_button(fp,_("OK"));
364 gtk_box_pack_start (GTK_BOX( hbox), button, TRUE, TRUE, 3);
365 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
366 gtk_widget_grab_default(button);
367 gtk_widget_show (button);
368 g_signal_connect_swapped(G_OBJECT(button), "clicked",(GCallback)kill_batch,GTK_OBJECT(Win));
369 g_signal_connect_swapped(G_OBJECT(button), "clicked",(GCallback)destroy_children,GTK_OBJECT(fp));
370
371
372 gtk_widget_show_all(fp);
373 }
374 /*************************************************************************************************/
eventDispatcher(GtkWidget * widget,GdkEventButton * event,gpointer user_data)375 static void eventDispatcher(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
376 {
377
378 GtkTreePath *path;
379 GtkTreeIter iter;
380 GtkTreeModel *model;
381
382 if (!event) return;
383 if (event->window == gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget))
384 && !gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget), event->x, event->y, NULL, NULL, NULL, NULL)) {
385 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)));
386 }
387 if(gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget), event->x, event->y, &path, NULL, NULL, NULL))
388 {
389 if(path)
390 {
391 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
392 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)), path);
393 sprintf(selectedRow ,"%s",gtk_tree_path_to_string(path));
394 gtk_tree_model_get_iter (model, &iter, path);
395 gtk_tree_path_free(path);
396 if (event->type == GDK_2BUTTON_PRESS && ((GdkEventButton *) event)->button == 1)
397 create_kill_batch(widget,selectedRow);
398 }
399 else sprintf(selectedRow,"-1");
400 }
401 GTK_WIDGET_GET_CLASS(widget)->button_press_event(widget, event);
402 }
403 /********************************************************************************/
create_gtk_list_batch()404 static GtkWidget* create_gtk_list_batch()
405 {
406 gint i;
407 gint j;
408 GtkWidget* gtklist = NULL;
409 gint *Width = NULL;
410 GtkListStore *store;
411 GtkTreeModel *model;
412 GtkCellRenderer *renderer;
413 GtkTreeViewColumn *column;
414 GtkTreeSelection *select;
415 GtkTreeIter iter;
416 GType* types = NULL;
417
418 if(NlistTitle<1) return gtklist;
419 Width = g_malloc(NlistTitle*sizeof(gint));
420 for (j=0;j<NlistTitle;j++)
421 {
422 Width[j] = strlen(Titles[j]);
423 for(i=0;i<Nlist;i++)
424 if(Width[j]<(gint)strlen(List[i][j]) ) Width[j] = strlen(List[i][j]);
425 }
426
427 types = g_malloc(NlistTitle*sizeof(GType));
428 for (i=0;i<NlistTitle;i++) types[i] = G_TYPE_STRING;
429 store = gtk_list_store_newv (NlistTitle, types);
430 g_free(types);
431 model = GTK_TREE_MODEL (store);
432
433 gtklist = gtk_tree_view_new_with_model (model);
434 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (gtklist), TRUE);
435 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (gtklist), TRUE);
436 gtk_tree_view_set_reorderable(GTK_TREE_VIEW (gtklist), TRUE);
437 for (j=0;j<NlistTitle;j++) Width[j] = (gint)(Width[j]*8);
438
439 for (i=0;i<NlistTitle;i++)
440 {
441 column = gtk_tree_view_column_new ();
442 gtk_tree_view_column_set_title (column, Titles[i]);
443 renderer = gtk_cell_renderer_text_new ();
444 gtk_tree_view_column_pack_start (column, renderer, TRUE);
445 gtk_tree_view_column_set_min_width(column, Width[i]);
446 gtk_tree_view_column_set_attributes (column, renderer, "text", i, NULL);
447 gtk_tree_view_append_column (GTK_TREE_VIEW (gtklist), column);
448 }
449 g_free( Width);
450
451 select = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtklist));
452 gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
453
454 for(i=0;i<Nlist;i++)
455 {
456 gtk_list_store_append(store, &iter);
457 for(j=0;j<NlistTitle;j++) gtk_list_store_set (store, &iter, j, List[i][j], -1);
458 }
459 g_signal_connect(gtklist, "button_press_event", G_CALLBACK(eventDispatcher), NULL);
460
461 return gtklist;
462 }
463 /********************************************************************************/
changed_host(GtkWidget * combo,gpointer data)464 static void changed_host(GtkWidget *combo,gpointer data)
465 {
466 GtkWidget **entry = (GtkWidget **)data;
467 gint nlistuser = 1;
468 G_CONST_RETURN gchar *hostname;
469 gint numhost = -1;
470 GtkWidget* combouser = NULL;
471 gint i;
472 GList *glist = NULL;
473
474 hostname = gtk_entry_get_text(GTK_ENTRY(entry[0]));
475 if(!this_is_an_object((GtkObject*)entry[1])) return;
476 combouser = g_object_get_data (G_OBJECT (entry[1]), "Combo");
477 if(!combouser) return;
478 if(recenthosts.nhosts>0)
479 {
480 for(i=0;i<recenthosts.nhosts;i++)
481 if(strcmp(hostname,recenthosts.hosts[i].hostname) == 0)
482 {
483 numhost = i;
484 break;
485 }
486 if(numhost<0)
487 return;
488
489 nlistuser = recenthosts.hosts[numhost].nusers;
490 for(i=0;i<recenthosts.hosts[numhost].nusers;i++)
491 glist = g_list_append(glist,recenthosts.hosts[numhost].users[i].username);
492 }
493 else
494 return;
495
496
497 for (i=0;i<nlistuser;i++)
498 gtk_combo_box_entry_set_popdown_strings( combouser, glist) ;
499
500
501 g_list_free(glist);
502 }
503 /********************************************************************************/
run_batch_remote_all(GtkWidget * win,gpointer data)504 static void run_batch_remote_all(GtkWidget*win,gpointer data)
505 {
506 run_batch_all(TRUE);
507 }
508 /********************************************************************************/
run_batch_remote_user(GtkWidget * win,gpointer data)509 static void run_batch_remote_user(GtkWidget*win,gpointer data)
510 {
511 if(RemoteUser) g_free(RemoteUser);
512 RemoteUser = NULL;
513 if(RemoteHost) g_free(RemoteHost);
514 RemoteHost = NULL;
515 if(RemotePassWord) g_free(RemotePassWord);
516 RemotePassWord = NULL;
517
518 run_batch_user(TRUE,NULL,NULL,NULL);
519 }
520 /********************************************************************************/
create_batch_remote_frame(GtkWidget * vboxall,GtkWidget ** entry,gboolean all)521 static GtkWidget *create_batch_remote_frame( GtkWidget *vboxall,GtkWidget **entry,gboolean all)
522 {
523 GtkWidget *frame;
524 GtkWidget *combo;
525 GtkWidget *vboxframe;
526 gushort i;
527 GtkWidget *Table;
528 gchar *tlisthost[NHOSTMAX];
529 gchar *tlistuser[NHOSTMAX];
530 gint nlisthost = 1;
531 gint nlistuser = 1;
532 G_CONST_RETURN gchar *localuser;
533
534 if(recenthosts.nhosts>0)
535 {
536 nlisthost = recenthosts.nhosts;
537 for(i=0;i<nlisthost;i++)
538 tlisthost[i] = g_strdup(recenthosts.hosts[i].hostname);
539 nlistuser = recenthosts.hosts[0].nusers;
540 for(i=0;i<recenthosts.hosts[0].nusers;i++)
541 tlistuser[i] = g_strdup(recenthosts.hosts[0].users[i].username);
542 }
543 else
544 {
545 localuser = get_local_user();
546 tlisthost[0] = g_strdup("hostname");
547 if(localuser)
548 tlistuser[0] = g_strdup(localuser);
549 else
550 tlistuser[0] = g_strdup("login");
551 }
552
553 frame = gtk_frame_new (_("Remote host"));
554 gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
555 gtk_container_add (GTK_CONTAINER (vboxall), frame);
556 gtk_widget_show (frame);
557
558 vboxframe = create_vbox(frame);
559 Table = gtk_table_new(2,3,FALSE);
560 gtk_container_add(GTK_CONTAINER(vboxframe),Table);
561
562 i = 0;
563 add_label_table(Table,_("Host name "),(gushort)(i),0);
564 add_label_table(Table," : ",(gushort)(i),1);
565 combo = create_combo_box_entry(tlisthost,nlisthost,TRUE,-1,-1);
566 gtk_table_attach(GTK_TABLE(Table),combo,2,3,i,i+1,
567 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND) ,
568 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND),
569 1,1);
570 gtk_widget_show (combo);
571 entry[0] = GTK_BIN(combo)->child;
572 g_object_set_data (G_OBJECT (entry[0]), "Combo",combo);
573 g_signal_connect(G_OBJECT(GTK_COMBO_BOX(combo)), "changed",G_CALLBACK(changed_host),entry);
574
575 i = 1;
576 add_label_table(Table,_("Login "),(gushort)(i),0);
577 add_label_table(Table," : ",(gushort)(i),1);
578 combo = create_combo_box_entry(tlistuser,nlistuser,TRUE,-1,-1);
579 gtk_table_attach(GTK_TABLE(Table),combo,2,3,i,i+1,
580 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND) ,
581 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND),
582 1,1);
583
584 entry[1] = GTK_BIN(combo)->child;
585 g_object_set_data (G_OBJECT (entry[1]), "Combo",combo);
586
587
588 i = 2;
589 if(defaultNetWorkProtocol==GABEDIT_NETWORK_SSH)
590 {
591 #ifdef G_OS_WIN32
592 add_label_table(Table,_("Password "),(gushort)(i),0);
593 add_label_table(Table," : ",(gushort)(i),1);
594 entry[i] = gtk_entry_new ();
595 gtk_entry_set_visibility(GTK_ENTRY (entry[i]),FALSE);
596 gtk_table_attach(GTK_TABLE(Table),entry[i],2,3,i,i+1,
597 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND) ,
598 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND),
599 1,1);
600 #else
601 entry[i] = gtk_entry_new ();
602 #endif
603 }
604 else
605 {
606 entry[i] = gtk_entry_new ();
607 }
608
609 if(fileopen.remotehost)
610 gtk_entry_set_text(GTK_ENTRY(entry[0]),fileopen.remotehost);
611 if(fileopen.remoteuser)
612 gtk_entry_set_text(GTK_ENTRY(entry[1]),fileopen.remoteuser);
613 if(fileopen.remotepass)
614 gtk_entry_set_text(GTK_ENTRY(entry[2]),fileopen.remotepass);
615
616 for (i=0;i<nlisthost;i++)
617 g_free(tlisthost[i]);
618 for (i=0;i<nlistuser;i++)
619 g_free(tlistuser[i]);
620
621 return frame;
622 }
623 /********************************************************************************/
create_batch_remote(gboolean all)624 void create_batch_remote(gboolean all)
625 {
626 GtkWidget *fp;
627 GtkWidget *frame;
628 GtkWidget *vboxall;
629 GtkWidget *vboxwin;
630 GtkWidget *hbox;
631 GtkWidget *button;
632 GtkWidget **entry;
633 gchar *title = g_strdup_printf(_("Batch Jobs at remote host "));
634
635 entry=g_malloc(3*sizeof(GtkWidget *));
636
637 /* Principal Window */
638 fp = gtk_window_new(GTK_WINDOW_TOPLEVEL);
639 gtk_window_set_title(GTK_WINDOW(fp),title);
640 gtk_window_set_position(GTK_WINDOW(fp),GTK_WIN_POS_CENTER);
641 gtk_window_set_transient_for(GTK_WINDOW(fp),GTK_WINDOW(Fenetre));
642 gtk_window_set_modal (GTK_WINDOW (fp), TRUE);
643
644 gtk_widget_realize(fp);
645 init_child(fp,gtk_widget_destroy,_(" Remote Batch Jobs "));
646 g_signal_connect(G_OBJECT(fp),"delete_event",(GCallback)destroy_children,NULL);
647
648 gtk_container_set_border_width (GTK_CONTAINER (fp), 5);
649 vboxall = create_vbox(fp);
650 vboxwin = vboxall;
651
652 frame = gtk_frame_new (NULL);
653 gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
654 gtk_frame_set_shadow_type( GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
655 gtk_container_add(GTK_CONTAINER(vboxall),frame);
656 gtk_widget_show (frame);
657
658 vboxall = create_vbox(frame);
659
660 hbox = create_hbox(vboxall);
661
662 frame = create_batch_remote_frame(hbox,entry,all);
663
664 EntryRemote = entry[0];
665 EntryLogin = entry[1];
666 EntryPassWord = entry[2];
667 /* boutons box */
668 hbox = create_hbox(vboxwin);
669 gtk_widget_realize(fp);
670
671 button = create_button(fp,_("Cancel"));
672 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
673 gtk_box_pack_start (GTK_BOX( hbox), button, TRUE, TRUE, 3);
674 g_signal_connect_swapped(G_OBJECT(button), "clicked",(GCallback)destroy_children,GTK_OBJECT(fp));
675 gtk_widget_show (button);
676
677 button = create_button(fp,_("OK"));
678 gtk_box_pack_start (GTK_BOX( hbox), button, TRUE, TRUE, 3);
679 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
680 gtk_widget_grab_default(button);
681 gtk_widget_show (button);
682 g_signal_connect_swapped(G_OBJECT (EntryPassWord ), "activate", (GCallback) gtk_button_clicked, GTK_OBJECT (button));
683
684 if(!all) g_signal_connect(G_OBJECT(button), "clicked",G_CALLBACK(run_batch_remote_user),(gpointer)NULL);
685 else g_signal_connect(G_OBJECT(button), "clicked",G_CALLBACK(run_batch_remote_all),(gpointer)NULL);
686 g_signal_connect_swapped(G_OBJECT(button), "clicked",(GCallback)destroy_children,GTK_OBJECT(fp));
687
688
689
690 /* Show all */
691 gtk_widget_show_all(fp);
692 }
693 /********************************************************************************/
run_batch_all(gboolean remote)694 void run_batch_all(gboolean remote)
695 {
696 gchar *fout = g_strdup_printf("%s%stmp%sfout",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
697 gchar *ferr = g_strdup_printf("%s%stmp%sferr",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
698
699
700 gchar *command;
701 GtkWidget* Text[2];
702 GtkWidget* Frame[2];
703 GtkWidget* Win;
704 gchar *strout;
705 gchar *Uidstrout;
706 gchar *strerr;
707 G_CONST_RETURN gchar *remoteuser = NULL;
708 G_CONST_RETURN gchar *remotehost = NULL;
709 G_CONST_RETURN gchar *remotepassword = NULL;
710 gchar *title = NULL;
711
712 Remote = remote;
713
714 #ifdef G_OS_WIN32
715 if(!remote)
716 {
717 Message(_("Sorry, This option is available for unix system only"),_("Error"),TRUE);
718 g_free(fout);
719 g_free(ferr);
720 return;
721 }
722 #endif
723 if(remote)
724 {
725 remotehost = gtk_entry_get_text(GTK_ENTRY(EntryRemote));
726 remoteuser = gtk_entry_get_text(GTK_ENTRY(EntryLogin));
727 remotepassword = gtk_entry_get_text(GTK_ENTRY(EntryPassWord));
728 command = g_strdup_printf("%s",NameCommandBatchAll);
729 title = g_strdup_printf(_("All Jobs in %s host "),remotehost);
730 /*rsh (fout,ferr,command, remoteuser,remotehost);*/
731 remote_command (fout,ferr,command,remotehost,remoteuser,remotepassword);
732 g_free(command);
733 add_host(remotehost,remoteuser,"","tmp");
734 }
735 else
736 {
737 command = g_strdup(NameCommandBatchAll);
738 title = g_strdup_printf(_("All Jobs in local host "));
739 run_local_command(fout,ferr,command,FALSE);
740 g_free(command);
741 }
742 Win = create_text_result_command(Text,Frame,title);
743 g_free(title);
744 strout = cat_file(fout,FALSE);
745 strerr = cat_file(ferr,FALSE);
746 if(!strout && !strerr)
747 destroy_children(Win);
748 else
749 {
750 if(strout)
751 {
752 Uidstrout = strout;
753 while(
754 Uidstrout[0] != '\0' && Uidstrout[0] != 'U' &&
755 Uidstrout[0] != 'I' && Uidstrout[0] != 'D'
756 )
757 Uidstrout++;
758 gabedit_text_insert (GABEDIT_TEXT(Text[0]), NULL, NULL, NULL,Uidstrout,-1);
759 g_free(strout);
760 }
761 if(strerr)
762 {
763 gabedit_text_insert (GABEDIT_TEXT(Text[1]), NULL, NULL, NULL,strerr,-1);
764 g_free(strerr);
765 }
766 gtk_widget_show_all(Win);
767 if(!strout)
768 gtk_widget_hide(Frame[0]);
769 }
770
771 g_free(fout);
772 g_free(ferr);
773 }
774 /********************************************************************************/
create_list_result_command(GtkWidget * gtklist,gchar * strerr,gchar * title)775 static GtkWidget* create_list_result_command(GtkWidget* gtklist,gchar* strerr,gchar* title)
776 {
777 GtkWidget *Win;
778 GtkWidget *frame;
779 GtkWidget *scr;
780 GtkWidget *hbox;
781 GtkWidget *vbox;
782 GtkWidget *vboxall;
783 GtkWidget *vboxwin;
784 GtkWidget *button;
785 GtkWidget* Text;
786 GtkWidget* Frame[2];
787
788
789 /* Principal Window */
790 Win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
791 gtk_window_set_title(GTK_WINDOW(Win),title);
792 gtk_window_set_position(GTK_WINDOW(Win),GTK_WIN_POS_CENTER);
793 gtk_window_set_transient_for(GTK_WINDOW(Win),GTK_WINDOW(Fenetre));
794 gtk_window_set_modal (GTK_WINDOW (Win), TRUE);
795
796 gtk_widget_realize(Win);
797 init_child(Win,gtk_widget_destroy,_(" List of Jobs "));
798 g_signal_connect(G_OBJECT(Win),"delete_event",(GCallback)destroy_children,NULL);
799
800 gtk_container_set_border_width (GTK_CONTAINER (Win), 5);
801 vboxall = create_vbox(Win);
802 vboxwin = vboxall;
803
804 frame = gtk_frame_new (NULL);
805 gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
806 gtk_frame_set_shadow_type( GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
807 gtk_container_add(GTK_CONTAINER(vboxall),frame);
808 gtk_widget_show (frame);
809 vboxall = create_vbox(frame);
810
811 Frame[0] = NULL;
812 if(gtklist)
813 {
814 frame = gtk_frame_new (_("Output"));
815 gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
816 gtk_frame_set_shadow_type( GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
817 gtk_container_add(GTK_CONTAINER(vboxall),frame);
818 gtk_widget_show (frame);
819 vbox = create_vbox(frame);
820 scr=gtk_scrolled_window_new(NULL,NULL);
821 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scr),
822 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
823
824 gtk_box_pack_start(GTK_BOX (vbox), scr,TRUE, TRUE, 2);
825 gtk_container_add(GTK_CONTAINER(scr),gtklist);
826 set_base_style(gtklist,50000,50000,50000);
827
828 Frame[0] = frame;
829 }
830
831 Text = create_text_widget(vboxall,_("Error"),&Frame[1]);
832 set_font (Text,FontsStyleResult.fontname);
833 set_base_style(Text,FontsStyleResult.BaseColor.red ,FontsStyleResult.BaseColor.green ,FontsStyleResult.BaseColor.blue);
834 set_text_style(Text,FontsStyleResult.TextColor.red ,0,0);
835 /* boutons box */
836 hbox = gtk_hbox_new (FALSE, 4);
837 gtk_box_pack_start (GTK_BOX(vboxwin), hbox, FALSE, FALSE, 5);
838 gtk_box_set_homogeneous(GTK_BOX(hbox), FALSE);
839 gtk_widget_realize(Win);
840 button = create_button(Win,_("OK"));
841 gtk_box_pack_end (GTK_BOX( hbox), button, FALSE, FALSE, 5);
842 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
843 gtk_widget_grab_default(button);
844 g_signal_connect_swapped(G_OBJECT(button), "clicked",(GCallback)destroy_win_user_batch,GTK_OBJECT(Win));
845 gtk_widget_show (button);
846 gtk_window_set_default_size (GTK_WINDOW(Win), 3*ScreenWidth/5, 3*ScreenHeight/5);
847 if(Frame[0])
848 gtk_widget_set_size_request(GTK_WIDGET(Frame[0]),-1,2*ScreenHeight/5);
849 if(strerr)
850 gabedit_text_insert (GABEDIT_TEXT(Text), NULL, NULL, NULL,strerr,-1);
851 return Win;
852 }
853 /********************************************************************************/
run_batch_user(gboolean remote,gchar * remoteuser,gchar * remotehost,gchar * remotepassword)854 void run_batch_user(gboolean remote,gchar *remoteuser,gchar *remotehost,gchar *remotepassword)
855 {
856 gchar *fout = g_strdup_printf("%s%stmp%sfout", gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
857 gchar *ferr = g_strdup_printf("%s%stmp%sferr", gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
858
859
860 gchar *command;
861 GtkWidget* gtklist;
862 GtkWidget* Win;
863 gchar *strerr;
864 G_CONST_RETURN gchar *localuser = NULL;
865 gchar localhost[100];
866 gchar *title = NULL;
867
868 Remote = remote;
869
870 #ifdef G_OS_WIN32
871 if(!remote)
872 {
873 Message(_("Sorry, This option is available for unix system only"),_("Error"),TRUE);
874 g_free(fout);
875 g_free(ferr);
876 return;
877 }
878 #else
879 localuser = get_local_user();
880 gethostname(localhost,100);
881 #endif /* G_OS_WIN32 */
882 if(!localuser && !remote)
883 {
884 Message(_("Sorry, I can not obtain user name"),_("Error"),TRUE);
885 g_free(fout);
886 g_free(ferr);
887 return;
888 }
889
890 if(remote)
891 {
892 if(!remotehost)
893 {
894 G_CONST_RETURN gchar* remotehost0 = gtk_entry_get_text(GTK_ENTRY(EntryRemote));
895 if(RemoteHost) g_free(RemoteHost);
896 RemoteHost = g_strdup(remotehost0);
897 remotehost = g_strdup(remotehost0);
898 }
899 if(!remoteuser)
900 {
901 G_CONST_RETURN gchar* remoteuser0 = gtk_entry_get_text(GTK_ENTRY(EntryLogin));
902 if(RemoteUser) g_free(RemoteUser);
903 RemoteUser = g_strdup(remoteuser0);
904 remoteuser = g_strdup(remoteuser0);
905 }
906 if(!remotepassword)
907 {
908 G_CONST_RETURN gchar* remotepassword0 = gtk_entry_get_text(GTK_ENTRY(EntryPassWord));
909 if(RemotePassWord) g_free(RemotePassWord);
910 RemotePassWord = g_strdup(remotepassword0);
911 remotepassword = g_strdup(remotepassword0);
912 }
913
914 if(strstr(NameCommandBatchUser,"-u")) command = g_strdup_printf("%s %s",NameCommandBatchUser,remoteuser);
915 else command = g_strdup_printf("%s",NameCommandBatchUser);
916 remote_command (fout,ferr,command,remotehost,remoteuser,remotepassword);
917 g_free(command);
918 add_host(remotehost,remoteuser,"","tmp");
919 title = g_strdup_printf(_("Process in host : \"%s\" ; for user : \"%s\" "), remotehost,remoteuser);
920 }
921 else
922 {
923
924 if(strstr(NameCommandBatchUser,"-u"))
925 command = g_strdup_printf("%s %s",NameCommandBatchUser,localuser);
926 else
927 command = g_strdup_printf("%s",NameCommandBatchUser);
928
929 run_local_command(fout,ferr,command,FALSE);
930 g_free(command);
931 title = g_strdup_printf(_("Jobs in host : \"%s\" ; for user : \"%s\" "), localhost,localuser);
932 }
933 if(WinUserBatch) destroy_children(WinUserBatch);
934
935 get_list_from_file(fout);
936 gtklist = create_gtk_list_batch();
937 strerr = cat_file(ferr,FALSE);
938 Win = create_list_result_command(gtklist,strerr,title);
939 g_free(title);
940 WinUserBatch = Win;
941 gtk_widget_show_all(Win);
942
943 g_free(fout);
944 g_free(ferr);
945
946 }
947 /********************************************************************************/
948