1 /* -*- mode:C; indent-tabs-mode:t; tab-width:8; c-basic-offset:8; -*- */
2
3 /* gnome-netinfo - A GUI Interface for network utilities
4 * Copyright (C) 2002 by German Poo-Caaman~o
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <gtk/gtk.h>
26 #include <glib/gi18n.h>
27 #include <glib/gprintf.h>
28
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <signal.h>
33 #include <string.h>
34
35 #include "callbacks.h"
36 #include "traceroute.h"
37 #include "info.h"
38 #include "ping.h"
39 #include "netstat.h"
40 #include "scan.h"
41 #include "lookup.h"
42 #include "finger.h"
43 #include "whois.h"
44 #include "utils.h"
45 #include "gn-combo-history.h"
46
47 /* Ping callbacks */
48 void
on_ping_activate(GtkWidget * widget,gpointer data)49 on_ping_activate (GtkWidget * widget, gpointer data)
50 {
51 Netinfo *pinger = data;
52 GtkEntry *entry_host;
53 gchar *text;
54
55 g_return_if_fail (pinger != NULL);
56
57 if (pinger->running) {
58 ping_stop (pinger);
59 } else {
60 if (netinfo_validate_host (pinger)) {
61 entry_host = GTK_ENTRY (
62 gtk_bin_get_child (GTK_BIN (pinger->host)));
63 text = g_strdup (gtk_entry_get_text (entry_host));
64
65 gn_combo_history_add (pinger->history, text);
66
67 g_free (text);
68
69 ping_do (pinger);
70 }
71 }
72 }
73 void
on_ping_toggled(GtkToggleButton * button,gpointer data)74 on_ping_toggled (GtkToggleButton *button, gpointer data)
75 {
76 Netinfo *info = data;
77
78 gtk_widget_set_sensitive (info->count,
79 gtk_toggle_button_get_active (button));
80 }
81
82 /* Traceroute callbacks */
83 void
on_traceroute_activate(GtkWidget * widget,gpointer data)84 on_traceroute_activate (GtkWidget * widget, gpointer data)
85 {
86 Netinfo *tracer = data;
87 GtkEntry *entry_host;
88 gchar *text;
89
90 g_return_if_fail (tracer != NULL);
91
92 if (tracer->running) {
93 traceroute_stop (tracer);
94 } else {
95 if (netinfo_validate_host (tracer)) {
96 entry_host = GTK_ENTRY (
97 gtk_bin_get_child (GTK_BIN (tracer->host)));
98 text = g_strdup (gtk_entry_get_text (entry_host));
99
100 gn_combo_history_add (tracer->history, text);
101
102 g_free (text);
103
104 traceroute_do (tracer);
105 }
106 }
107 }
108
109 void
on_netstat_activate(GtkWidget * widget,gpointer data)110 on_netstat_activate (GtkWidget * widget, gpointer data)
111 {
112 Netinfo *netstat = data;
113
114 g_return_if_fail (netstat != NULL);
115
116 if (netstat->running) {
117 netstat_stop (netstat);
118 } else {
119 netstat_do (netstat);
120 }
121 }
122
123 /* Info callbacks */
124 void
on_configure_button_clicked(GtkButton * button,gpointer data)125 on_configure_button_clicked (GtkButton *button, gpointer data)
126 {
127 GString *command_line;
128 GtkComboBox *combo;
129 GtkTreeModel *model;
130 GtkTreeIter iter;
131 GtkWidget *dialog;
132 Netinfo *info;
133 GError *error;
134 gchar *nic;
135
136 g_return_if_fail (data != NULL);
137 info = (Netinfo *) data;
138
139 combo = GTK_COMBO_BOX (info->combo);
140 model = gtk_combo_box_get_model (combo);
141
142 if (gtk_combo_box_get_active_iter (combo, &iter)) {
143 gchar *network_tool_path;
144
145 gtk_tree_model_get (model, &iter, 2, &nic, -1);
146
147 network_tool_path = util_find_program_in_path ("nm-connection-editor", NULL);
148 if (network_tool_path != NULL) {
149 command_line = g_string_new ("nm-connection-editor");
150 } else {
151 network_tool_path = util_find_program_in_path ("network-admin", NULL);
152
153 command_line = g_string_new (network_tool_path);
154 g_string_append (command_line, " --configure ");
155 g_string_append (command_line, nic);
156 }
157
158 if (!g_spawn_command_line_async (command_line->str, &error)) {
159 dialog = gtk_message_dialog_new (GTK_WINDOW (info->main_window),
160 GTK_DIALOG_DESTROY_WITH_PARENT,
161 GTK_MESSAGE_ERROR,
162 GTK_BUTTONS_CLOSE,
163 "%s", error->message);
164 gtk_dialog_run (GTK_DIALOG (dialog));
165 gtk_widget_destroy (dialog);
166 }
167
168 g_free (network_tool_path);
169 g_string_free (command_line, TRUE);
170 }
171 }
172
173 /* Scan callbacks */
174 void
on_scan_activate(GtkWidget * widget,gpointer data)175 on_scan_activate (GtkWidget * widget, gpointer data)
176 {
177 Netinfo *scan = data;
178 GtkEntry *entry_host;
179 gchar *text;
180
181 g_return_if_fail (scan != NULL);
182
183 if (scan->running) {
184 scan_stop (scan);
185 } else {
186 if (netinfo_validate_host (scan)) {
187 entry_host = GTK_ENTRY (
188 gtk_bin_get_child (GTK_BIN (scan->host)));
189 text = g_strdup (gtk_entry_get_text (entry_host));
190
191 gn_combo_history_add (scan->history, text);
192
193 g_free (text);
194
195 scan_do (scan);
196 }
197 }
198 }
199
200 /* Lookup callbacks */
201 void
on_lookup_activate(GtkWidget * widget,gpointer data)202 on_lookup_activate (GtkWidget * widget, gpointer data)
203 {
204 Netinfo *lookup = data;
205 GtkEntry *entry_host;
206 gchar *text;
207
208 g_return_if_fail (lookup != NULL);
209
210 if (lookup->running) {
211 lookup_stop (lookup);
212 } else {
213 if (netinfo_validate_domain (lookup)) {
214 entry_host = GTK_ENTRY (
215 gtk_bin_get_child (GTK_BIN (lookup->host)));
216 text = g_strdup (gtk_entry_get_text (entry_host));
217
218 gn_combo_history_add (lookup->history, text);
219
220 g_free (text);
221
222 lookup_do (lookup);
223 }
224 }
225 }
226
227 /* Finger callbacks */
228 void
on_finger_activate(GtkWidget * widget,gpointer data)229 on_finger_activate (GtkWidget * widget, gpointer data)
230 {
231 Netinfo *finger = data;
232 GtkEntry *entry_host;
233 gchar *text;
234
235 g_return_if_fail (finger != NULL);
236
237 if (finger->running) {
238 finger_stop (finger);
239 } else {
240 entry_host = GTK_ENTRY (
241 gtk_bin_get_child (GTK_BIN (finger->host)));
242 text = g_strdup (gtk_entry_get_text (entry_host));
243 g_strstrip (text);
244
245 if (strlen(text) > 0)
246 gn_combo_history_add (finger->history, text);
247
248 g_free (text);
249
250 entry_host = GTK_ENTRY (
251 gtk_bin_get_child (GTK_BIN (finger->user)));
252 text = g_strdup (gtk_entry_get_text (entry_host));
253 g_strstrip (text);
254
255 if (strlen(text) > 0)
256 gn_combo_history_add (finger->history_user, text);
257
258 g_free (text);
259
260 finger_do (finger);
261 }
262 }
263
264 /* Whois callbacks */
265 void
on_whois_activate(GtkWidget * widget,gpointer data)266 on_whois_activate (GtkWidget * widget, gpointer data)
267 {
268 Netinfo *whois = data;
269 GtkEntry *entry_host;
270 gchar *text;
271
272 g_return_if_fail (whois != NULL);
273
274 if (whois->running) {
275 whois_stop (whois);
276 } else {
277 if (netinfo_validate_domain (whois)) {
278 entry_host = GTK_ENTRY (
279 gtk_bin_get_child (GTK_BIN (whois->host)));
280 text = g_strdup (gtk_entry_get_text (entry_host));
281
282 gn_combo_history_add (whois->history, text);
283
284 g_free (text);
285
286 whois_do (whois);
287 }
288 }
289 }
290
291 gboolean
gn_quit_app(GtkWidget * widget,gpointer data)292 gn_quit_app (GtkWidget * widget, gpointer data)
293 {
294 gint status, pid;
295
296 pid = getpid () + 1;
297 while (waitpid (-1, &status, WNOHANG) == 0) {
298 if (waitpid (pid, &status, WNOHANG) == 0)
299 kill (pid, SIGKILL);
300 pid ++;
301 }
302
303 netinfo_progress_indicator_stop (NULL);
304
305 gtk_main_quit ();
306
307 return TRUE;
308 }
309
310 void
on_beep_activate(GtkWidget * menu_item,gpointer data)311 on_beep_activate (GtkWidget *menu_item, gpointer data)
312 {
313 Netinfo *ni_data = (Netinfo *) data;
314 ni_data->has_beep =! ni_data->has_beep;
315 }
316
317 void
on_about_activate(gpointer window,GtkWidget * menu_item)318 on_about_activate (gpointer window, GtkWidget *menu_item)
319 {
320 const gchar *authors[] = {
321 "Germán Poo Caamaño <gpoo@gnome.org>",
322 "William Jon McCann <mccann@jhu.edu>",
323 "Carlos Garcia Campos <carlosgc@gnome.org>",
324 "Rodrigo Moya <rodrigo@gnome-db.org>",
325 NULL
326 };
327 const gchar *artists[] = {
328 "Hylke Bons <h.bons@student.rug.nl>",
329 NULL
330 };
331 const gchar *documentors[] = { NULL };
332 const gchar *translator_credits = _("translator-credits");
333 gchar copyright[1024];
334 GtkWindow *parent;
335
336 parent = (GtkWindow *) window;
337
338 /* Translators: %s is the name of the copyright holder */
339 g_sprintf (copyright, _("Copyright \xc2\xa9 2003-2008 %s"), "Germán Poo Caamaño");
340
341 gtk_show_about_dialog (parent,
342 /* Dear translator: This is the name of the application */
343 "program-name", _("Network Tools"),
344 "name", _("Network Tools"),
345 "authors", authors,
346 "comments", _("Graphical user interface for common network utilities"),
347 "copyright", copyright,
348 "artists", artists,
349 "documenters", documentors,
350 "license", "GPL 2+",
351 "logo-icon-name", "gnome-nettool",
352 "translator-credits", translator_credits,
353 "version", PACKAGE_VERSION,
354 "website", PACKAGE_URL,
355 "wrap-license", TRUE,
356 NULL);
357 }
358
359 static Netinfo *
get_netinfo_for_page(GtkNotebook * notebook,gint page_num)360 get_netinfo_for_page (GtkNotebook * notebook, gint page_num)
361 {
362 Netinfo *netinfo = NULL;
363
364 switch (page_num) {
365 case INFO:
366 netinfo = g_object_get_data (G_OBJECT (notebook), "info");
367 break;
368 case PING:
369 netinfo = g_object_get_data (G_OBJECT (notebook), "pinger");
370 break;
371 case TRACEROUTE:
372 netinfo = g_object_get_data (G_OBJECT (notebook), "tracer");
373 break;
374 case NETSTAT:
375 netinfo = g_object_get_data (G_OBJECT (notebook), "netstat");
376 break;
377 case PORTSCAN:
378 netinfo = g_object_get_data (G_OBJECT (notebook), "scan");
379 break;
380 case LOOKUP:
381 netinfo = g_object_get_data (G_OBJECT (notebook), "lookup");
382 break;
383 case FINGER:
384 netinfo = g_object_get_data (G_OBJECT (notebook), "finger");
385 break;
386 case WHOIS:
387 netinfo = g_object_get_data (G_OBJECT (notebook), "whois");
388 break;
389 default:
390 g_warning ("Unknown notebook page");
391 }
392
393 return netinfo;
394 }
395
396 void
on_copy_activate(gpointer notebook,GtkWidget * menu_item)397 on_copy_activate (gpointer notebook, GtkWidget *menu_item)
398 {
399 gint page;
400 Netinfo *netinfo;
401
402 g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
403
404 page = gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook));
405
406 netinfo = get_netinfo_for_page (GTK_NOTEBOOK (notebook), page);
407 if (!netinfo)
408 return;
409
410 if (netinfo->copy_output != NULL) {
411 (netinfo->copy_output) ((gpointer) netinfo, NULL);
412 }
413 }
414
415 void
on_clear_history_activate(gpointer notebook,GtkWidget * menu_item)416 on_clear_history_activate (gpointer notebook, GtkWidget *menu_item)
417 {
418 Netinfo *netinfo;
419
420 g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
421
422 /* Pages all share a history id for host entry except whois */
423 netinfo = g_object_get_data (G_OBJECT (notebook), "pinger");
424 gn_combo_history_clear (netinfo->history);
425
426 netinfo = g_object_get_data (G_OBJECT (notebook), "finger");
427 gn_combo_history_clear (netinfo->history);
428 gn_combo_history_clear (netinfo->history_user);
429
430 netinfo = g_object_get_data (G_OBJECT (notebook), "whois");
431 gn_combo_history_clear (netinfo->history);
432 }
433
434 void
on_page_switch(GtkNotebook * notebook,gpointer page,guint page_num,gpointer data)435 on_page_switch (GtkNotebook * notebook,
436 gpointer page,
437 guint page_num,
438 gpointer data)
439 {
440 Netinfo *netinfo;
441 char *title;
442
443 netinfo = get_netinfo_for_page (notebook, page_num);
444 if (!netinfo)
445 return;
446
447 if (netinfo->running) {
448 netinfo_progress_indicator_start (netinfo);
449 if (netinfo->stbar_text) {
450 gtk_statusbar_pop (GTK_STATUSBAR (netinfo->status_bar), 0);
451 gtk_statusbar_push (GTK_STATUSBAR (netinfo->status_bar),
452 0, netinfo->stbar_text);
453 }
454 } else {
455 netinfo_progress_indicator_stop (netinfo);
456 gtk_statusbar_pop (GTK_STATUSBAR (netinfo->status_bar), 0);
457 gtk_statusbar_push (GTK_STATUSBAR (netinfo->status_bar),
458 0, _("Idle"));
459 }
460
461 /* Dear Translator: This is the Window Title. 'Network Tools' is the
462 * name of the application */
463 title = g_strdup_printf (_("%s - Network Tools"),
464 gtk_label_get_text (GTK_LABEL (netinfo->page_label)));
465 gtk_window_set_title (GTK_WINDOW (netinfo->main_window), title);
466 g_free (title);
467 }
468
469 void
on_help_activate(gpointer window,GtkWidget * menu_item)470 on_help_activate (gpointer window, GtkWidget *menu_item)
471 {
472 GdkScreen *screen;
473 GError *error = NULL;
474
475 g_return_if_fail (GTK_IS_WINDOW (window));
476
477 screen = gtk_widget_get_screen (window);
478 gtk_show_uri (screen, "help:gnome-nettool",
479 gtk_get_current_event_time (), &error);
480
481 if (error) {
482 GtkWidget *dialog;
483 dialog = gtk_message_dialog_new (
484 GTK_WINDOW (window),
485 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
486 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
487 "%s", _("Unable to open help file"));
488 gtk_message_dialog_format_secondary_text (
489 GTK_MESSAGE_DIALOG (dialog),
490 "%s", error->message);
491 g_signal_connect (dialog, "response",
492 G_CALLBACK (gtk_widget_destroy), NULL);
493 gtk_window_present (GTK_WINDOW (dialog));
494
495 g_error_free (error);
496 }
497 }
498