summaryrefslogtreecommitdiff
path: root/modemlights/modem-applet.c
blob: cd0a05c37884878b04e92ce244804a95cae27975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
/* Copyright (C) 2004 Carlos Garnacho
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib/gi18n.h>
#include <mate-panel-applet.h>
#include <fcntl.h>
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <libxml/tree.h>
#include <errno.h>

#ifdef __FreeBSD__
#include <sys/ioctl.h>
#include <termios.h>
#include <libutil.h>
#endif

#include "modem-applet.h"

#define MODEM_APPLET_GET_PRIVATE(obj)  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_MODEM_APPLET, ModemAppletPrivate))
#define NETWORK_TOOL "network-admin"
#define END_OF_REQUEST "<!-- GST: end of request -->\n"
#define BUF_SIZE 1024

typedef void (*DirectiveCallback) (ModemApplet*, xmlDoc*);
typedef struct _BackendDirective   BackendDirective;
typedef struct _ModemAppletPrivate ModemAppletPrivate;

struct _ModemAppletPrivate
{
  /* applet UI stuff */
  GtkBuilder     *builder;
  GtkIconTheme   *icon_theme;
  GdkPixbuf      *icon;
  GtkWidget      *image;
  GtkActionGroup *action_group;

  /* auth dialog */
  GtkWidget    *auth_dialog;
  GtkWidget    *auth_dialog_label;
  GtkWidget    *auth_dialog_entry;

  /* report window */
  GtkWidget    *report_window;
  GtkWidget    *report_window_image;
  GtkWidget    *report_window_progress;

  guint directives_id;
  guint progress_id;
  guint tooltip_id;
  guint info_id;
  guint timeout_id;

  /* for communicating with the backend */
  gint config_id;
  gint pid;
  int  read_fd;
  int  write_fd;
  FILE *read_stream;
  FILE *write_stream;
  GSList *directives;
  gboolean directive_running;

  /* interface data */
  gboolean configured;  /* is configured? */
  gboolean enabled;     /* is enabled? */
  gboolean is_isdn;     /* is an isdn device? */
  gchar *dev;           /* device name */
  gchar *lock_file;     /* lock file */

  gboolean  has_root;
};

struct _BackendDirective
{
  DirectiveCallback callback;
  GSList *directive;
  gboolean show_report;
};

static void modem_applet_class_init (ModemAppletClass *class);
static void modem_applet_init       (ModemApplet      *applet);
static void modem_applet_finalize   (GObject          *object);

static gboolean update_tooltip      (ModemApplet *applet);
static gboolean dispatch_directives (ModemApplet *applet);
static gboolean update_info         (ModemApplet *applet);

static void modem_applet_change_size (MatePanelApplet *applet, guint size);

static void modem_applet_change_background (MatePanelApplet *app,
					    MatePanelAppletBackgroundType type,
					    GdkColor  *colour,
					    GdkPixmap *pixmap);

static void on_modem_applet_about_clicked (GtkAction   *action,
					   ModemApplet *applet);
static void on_modem_applet_activate      (GtkAction   *action,
					   ModemApplet *applet);
static void on_modem_applet_deactivate    (GtkAction   *action,
					   ModemApplet *applet);
static void on_modem_applet_properties_clicked (GtkAction   *action,
						ModemApplet *applet);
static void on_modem_applet_help_clicked  (GtkAction   *action,
					   ModemApplet *applet);

static void launch_backend                (ModemApplet      *applet,
					   gboolean          root_auth);
static void shutdown_backend              (ModemApplet *applet,
					   gboolean     backend_alive,
					   gboolean     already_waiting);

static gpointer parent_class;

static const GtkActionEntry menu_actions[] = {
  { "Activate", GTK_STOCK_EXECUTE, N_("_Activate"),
    NULL, NULL,
    G_CALLBACK (on_modem_applet_activate) },
  { "Deactivate", GTK_STOCK_STOP, N_("_Deactivate"),
    NULL, NULL,
    G_CALLBACK (on_modem_applet_deactivate) },
  { "Properties", GTK_STOCK_PROPERTIES, N_("_Properties"),
    NULL, NULL,
    G_CALLBACK (on_modem_applet_properties_clicked) },
  { "Help", GTK_STOCK_HELP, N_("_Help"),
    NULL, NULL,
    G_CALLBACK (on_modem_applet_help_clicked) },
  { "About", GTK_STOCK_ABOUT, N_("_About"),
    NULL, NULL,
    G_CALLBACK (on_modem_applet_about_clicked) }
};

G_DEFINE_TYPE (ModemApplet, modem_applet, PANEL_TYPE_APPLET)

static void
modem_applet_class_init (ModemAppletClass *class)
{
  GObjectClass     *object_class;
  MatePanelAppletClass *applet_class;

  object_class = G_OBJECT_CLASS (class);
  applet_class = MATE_PANEL_APPLET_CLASS (class);
  parent_class = g_type_class_peek_parent (class);

  object_class->finalize    = modem_applet_finalize;
  applet_class->change_size = modem_applet_change_size;
  applet_class->change_background = modem_applet_change_background;

  g_type_class_add_private (object_class, sizeof (ModemAppletPrivate));
}

static void
modem_applet_init (ModemApplet *applet)
{
  ModemAppletPrivate *priv;
  GdkPixbuf *pixbuf;

  g_set_application_name ( _("Modem Monitor"));

  priv = MODEM_APPLET_GET_PRIVATE (applet);

  priv->builder = gtk_builder_new ();
  gtk_builder_add_from_file (priv->builder, GTK_BUILDERDIR "/modemlights.ui", NULL);
  priv->icon = NULL;
  priv->icon_theme = gtk_icon_theme_get_default ();
  priv->image = gtk_image_new ();

  priv->auth_dialog       = GTK_WIDGET (gtk_builder_get_object (priv->builder, "auth_dialog"));
  priv->auth_dialog_label = GTK_WIDGET (gtk_builder_get_object (priv->builder, "auth_dialog_label"));
  priv->auth_dialog_entry = GTK_WIDGET (gtk_builder_get_object (priv->builder, "auth_dialog_entry"));

  priv->report_window          = GTK_WIDGET (gtk_builder_get_object (priv->builder, "report_window"));
  priv->report_window_image    = GTK_WIDGET (gtk_builder_get_object (priv->builder, "report_window_image"));
  priv->report_window_progress = GTK_WIDGET (gtk_builder_get_object (priv->builder, "report_window_progress"));

  g_signal_connect (G_OBJECT (priv->report_window), "delete-event",
		    G_CALLBACK (gtk_widget_hide), NULL);

  pixbuf = gtk_icon_theme_load_icon (priv->icon_theme, "mate-modem-monitor-applet", 48, 0, NULL);
  gtk_image_set_from_pixbuf (GTK_IMAGE (priv->report_window_image), pixbuf);
  g_object_unref (pixbuf);

  priv->configured = FALSE;
  priv->enabled = FALSE;
  priv->dev = NULL;
  priv->lock_file = NULL;

  priv->has_root = FALSE;

  priv->directives = NULL;
  priv->directives_id = g_timeout_add (250, (GSourceFunc) dispatch_directives, applet);
  priv->directive_running = FALSE;
  priv->tooltip_id = g_timeout_add_seconds (1, (GSourceFunc) update_tooltip, applet);

  launch_backend (applet, FALSE);
  gtk_container_add (GTK_CONTAINER (applet), priv->image);
}

static void
modem_applet_finalize (GObject *object)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (object);

  if (priv)
    {
      shutdown_backend (MODEM_APPLET (object), TRUE, TRUE);

      gtk_widget_destroy (priv->auth_dialog);
      gtk_widget_destroy (priv->report_window);
      g_object_unref (priv->icon);
      g_object_unref (priv->action_group);

      g_free (priv->dev);
      g_free (priv->lock_file);
    }

  if (G_OBJECT_CLASS (parent_class)->finalize)
    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

static void
modem_applet_change_size (MatePanelApplet *applet,
			  guint        size)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);

  if (priv->icon)
    g_object_unref (priv->icon);

  /* this might be too much overload, maybe should we get just one icon size and scale? */
  priv->icon = gtk_icon_theme_load_icon (priv->icon_theme,
					 "mate-modem", size, 0, NULL);
  gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), priv->icon);
}

static void
modem_applet_change_background (MatePanelApplet *app,
				MatePanelAppletBackgroundType type,
				GdkColor  *colour,
				GdkPixmap *pixmap)
{
  ModemApplet *applet = MODEM_APPLET (app);
  GtkRcStyle *rc_style;
  GtkStyle *style;

  /* reset style */
  gtk_widget_set_style (GTK_WIDGET (applet), NULL);
  rc_style = gtk_rc_style_new ();
  gtk_widget_modify_style (GTK_WIDGET (applet), rc_style);
  g_object_unref (rc_style);

  switch (type)
    {
    case PANEL_NO_BACKGROUND:
      break;
    case PANEL_COLOR_BACKGROUND:
      gtk_widget_modify_bg (GTK_WIDGET (applet),
			    GTK_STATE_NORMAL, colour);
      break;
    case PANEL_PIXMAP_BACKGROUND:
      style = gtk_style_copy (GTK_WIDGET (applet)->style);

      if (style->bg_pixmap[GTK_STATE_NORMAL])
        g_object_unref (style->bg_pixmap[GTK_STATE_NORMAL]);

      style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref (pixmap);
      gtk_widget_set_style (GTK_WIDGET (applet), style);
      g_object_unref (style);
      break;
    }
}

static gboolean
pulse_progressbar (GtkWidget *progressbar)
{
  gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progressbar));
  return TRUE;
}

/* XML manipulation functions */
static xmlNodePtr
get_root_node (xmlDoc *doc)
{
  return xmlDocGetRootElement (doc);
}

static xmlNodePtr
find_first_element (xmlNodePtr node, const gchar *name)
{
  xmlNodePtr n;
	
  g_return_val_if_fail (node != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);

  for (n = node->children; n; n = n->next)
    if (n->name && (strcmp (name, (char *) n->name) == 0))
      break;

  return n;
}

static xmlNodePtr
find_next_element (xmlNodePtr node, const gchar *name)
{
  xmlNodePtr n;
	
  g_return_val_if_fail (node != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);

  for (n = node->next; n; n = n->next)
    if (n->name && (strcmp (name, (char *) n->name) == 0))
      break;

  return n;
}

static guchar *
element_get_attribute (xmlNodePtr node, const gchar *attribute)
{
  xmlAttrPtr a;

  g_return_val_if_fail (node != NULL, NULL);
  a = node->properties;

  while (a)
    {
      if (a->name && (strcmp ((char *) a->name, attribute) == 0))
        return xmlNodeGetContent (a->children);

      a = a->next;
    }

  return NULL;
}

static guchar *
element_get_child_content (xmlNodePtr node, const gchar *tag)
{
  xmlNodePtr child, n;

  child = find_first_element (node, tag);
  if (!child)
    return NULL;

  for (n = child->children; n; n = n->next)
    if (n->type == XML_TEXT_NODE)
      return xmlNodeGetContent (n);

  return NULL;
}

static xmlNodePtr
find_dialup_interface_node (xmlNodePtr root)
{
  xmlNodePtr  node;
  gchar      *type;

  node = find_first_element (root, "interface");

  while (node)
    {
      type = (char *) element_get_attribute (node, "type");

      if (type && (strcmp (type, "modem") == 0 || strcmp (type, "isdn") == 0))
        {
	  g_free (type);
	  return node;
	}

      g_free (type);
      node = find_next_element (node, "interface");
    }

  return NULL;
}

/* backend communication functions */
static gchar *
compose_directive_string (GSList *directive)
{
  GString *dir;
  gchar   *arg, *s, *str;
  GSList  *elem;

  elem = directive;
  dir = g_string_new ("");

  while (elem)
    {
      arg = elem->data;

      for (s = arg; *s; s++)
        {
	  /* escape needed chars */
	  if ((*s == '\\') ||
	      ((*s == ':') && (* (s + 1) == ':')))
	    g_string_append_c (dir, '\\');

	  g_string_append_c (dir, *s);
	}

      g_string_append (dir, "::");
      elem = elem->next;
    }

  g_string_append_c (dir, '\n');

  str = dir->str;
  g_string_free (dir, FALSE);

  return str;
}

static void
poll_backend (ModemAppletPrivate *priv)
{
  struct pollfd fd;

  fd.fd = priv->read_fd;
  fd.events = POLLIN || POLLPRI;

  while (poll (&fd, 1, 100) <= 0)
    {
      while (gtk_events_pending ())
	gtk_main_iteration ();
    }
}

static xmlDoc*
read_xml (ModemApplet *applet, gboolean show_report)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  gchar    buffer[BUF_SIZE], *s;
  GString *str;
  xmlDoc  *doc = NULL;
  gboolean backend_alive;

  str = g_string_new ("");
  backend_alive = (waitpid (priv->pid, NULL, WNOHANG) == 0);

  /* if show_report, create pulse timeout and show window */
  if (show_report)
    {
      priv->progress_id = g_timeout_add (200, (GSourceFunc) pulse_progressbar, priv->report_window_progress);
      gtk_window_set_screen (GTK_WINDOW (priv->report_window), gtk_widget_get_screen (GTK_WIDGET (applet)));
      gtk_widget_show (priv->report_window);
    }

  while (backend_alive && !g_strrstr (str->str, END_OF_REQUEST))
    {
      poll_backend (priv);
      fgets (buffer, BUF_SIZE, priv->read_stream);
      g_string_append (str, buffer);

      while (gtk_events_pending ())
        gtk_main_iteration ();

      backend_alive = (waitpid (priv->pid, NULL, WNOHANG) == 0);
    }

  /* if show_report, hide window and so */
  if (show_report)
    {
      g_source_remove (priv->progress_id);
      priv->progress_id = 0;
      gtk_widget_hide (priv->report_window);
    }

  s = str->str;

  while (*s && (*s != '<'))
    s++;

  if (strcmp (s, END_OF_REQUEST) != 0)
    doc = xmlParseDoc ((xmlChar *) s);

  g_string_free (str, TRUE);

  return doc;
}

static void
queue_directive (ModemApplet       *applet,
		 DirectiveCallback  callback,
		 gboolean           show_report,
		 const gchar       *dir,
		 ...)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  BackendDirective   *directive;
  GSList  *list = NULL;
  va_list  ap;
  gchar   *arg;

  list = g_slist_prepend (list, g_strdup (dir));
  va_start (ap, dir);

  while ((arg = va_arg (ap, gchar *)) != NULL)
    list = g_slist_prepend (list, g_strdup (arg));

  va_end (ap);
  list = g_slist_reverse (list);

  directive = g_new0 (BackendDirective, 1);
  directive->callback    = callback;
  directive->directive   = list;
  directive->show_report = show_report;

  priv->directives = g_slist_append (priv->directives, directive);
}

static gboolean
dispatch_directives (ModemApplet *applet)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  BackendDirective   *directive;
  xmlDoc             *doc;
  gchar              *dir;
  GSList             *elem;

  if (priv->directive_running)
    return TRUE;

  priv->directive_running = TRUE;
  elem = priv->directives;

  while (elem)
    {
      directive = elem->data;

      dir = compose_directive_string (directive->directive);
      fputs (dir, priv->write_stream);
      g_free (dir);

      doc = read_xml (applet, directive->show_report);

      if (directive->callback)
	directive->callback (applet, doc);

      if (doc)
	xmlFreeDoc (doc);

      g_slist_foreach (directive->directive, (GFunc) g_free, NULL);
      g_slist_free (directive->directive);

      elem = elem->next;
    }

  g_slist_foreach (priv->directives, (GFunc) g_free, NULL);
  g_slist_free (priv->directives);
  priv->directives = NULL;
  priv->directive_running = FALSE;

  return TRUE;
}

static void
shutdown_backend (ModemApplet *applet, gboolean backend_alive, gboolean already_waiting)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);

  if (priv->info_id)
    {
      g_source_remove (priv->info_id);
      priv->info_id = 0;
    }

  if (priv->timeout_id)
    {
      g_source_remove (priv->timeout_id);
      priv->timeout_id = 0;
    }

  if (priv->tooltip_id)
    {
      g_source_remove (priv->tooltip_id);
      priv->tooltip_id = 0;
    }

  if (backend_alive)
    kill (priv->pid, 9);

  if (!already_waiting)
    {
      /* don't leave zombies */
      while (waitpid (priv->pid, NULL, WNOHANG) <= 0)
        {
	  usleep (2000);

	  while (gtk_events_pending ())
	    gtk_main_iteration ();
	}
    }

  /* close remaining streams and fds */
  fclose (priv->read_stream);
  fclose (priv->write_stream);
  close  (priv->read_fd);
  close  (priv->write_fd);
}

/* functions for extracting the interface information from the XML */
static void
update_popup_buttons (ModemApplet *applet)
{
  GtkAction *action;
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);

  action = gtk_action_group_get_action (priv->action_group, "Activate");
  gtk_action_set_sensitive (action, priv->configured && !priv->enabled);

  action = gtk_action_group_get_action (priv->action_group, "Deactivate");
  gtk_action_set_sensitive (action, priv->configured && priv->enabled);
}

static void
get_interface_data (ModemApplet *applet, xmlNodePtr iface)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  xmlNodePtr configuration;
  gchar *text, *device;

  g_return_if_fail (iface != NULL);

  text = (char *) element_get_child_content (iface, "enabled");
  priv->enabled = (*text == '1');
  g_free (text);

  g_free (priv->dev);
  priv->dev = (char *) element_get_child_content (iface, "dev");

  g_free (priv->lock_file);
  configuration = find_first_element (iface, "configuration");

  if (configuration)
    {
      priv->configured = TRUE;
      text = (char *) element_get_child_content (configuration, "serial_port");

      if (text)
        {
	  /* Modem device */
	  device = strrchr (text, '/');
	  priv->lock_file = g_strdup_printf ("/var/lock/LCK..%s", device + 1);
	  g_free (text);

	  priv->is_isdn = FALSE;
        }
      else
        {
	  /* isdn device */
	  priv->lock_file = g_strdup ("/var/lock/LCK..capi_0");
	  priv->is_isdn = TRUE;
	}
    }
  else
    {
      priv->lock_file = NULL;
      priv->configured = FALSE;
    }
}

static gint
get_connection_time (const gchar *lock_file)
{
  struct stat st;

  if (stat (lock_file, &st) == 0)
    return (gint) (time (NULL) - st.st_mtime);

  return 0;
}

static gboolean
update_tooltip (ModemApplet *applet)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  gchar *text;
  gint   t, t1, t2;

  if (priv->enabled)
    {
      if (!priv->lock_file)
        text = g_strdup (_("Connection active, but could not get connection time"));
      else
        {
          t = get_connection_time (priv->lock_file);

          if (t < (60 * 60 * 24))
            {
              t1 = t / 3600; /* hours */
              t2 = (t - (t1 * 3600)) / 60; /* minutes */
            }
          else
            {
              t1 = t / (3600 * 24); /* days */
              t2 = (t - (t1 * 3600 * 24)) / 3600; /* hours */
            }

          text = g_strdup_printf (_("Time connected: %.1d:%.2d"), t1, t2);
        }
    }
  else
    text = g_strdup (_("Not connected"));

  gtk_widget_set_tooltip_text (GTK_WIDGET (applet), text);
  g_free (text);

  return TRUE;
}

static void
rerun_backend_callback (ModemApplet *applet, xmlDoc *doc)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  gchar    *text, *password;
  gint      response;
  gboolean  enable;

  shutdown_backend (applet, FALSE, FALSE);
  launch_backend   (applet, TRUE);

  enable = !priv->enabled;

  text = (enable) ?
    _("To connect to your Internet service provider, you need administrator privileges") :
    _("To disconnect from your Internet service provider, you need administrator privileges");

  gtk_label_set_text (GTK_LABEL (priv->auth_dialog_label), text);
  gtk_window_set_screen (GTK_WINDOW (priv->auth_dialog),
			 gtk_widget_get_screen (GTK_WIDGET (applet)));

  gtk_widget_grab_focus (priv->auth_dialog_entry);
  response = gtk_dialog_run (GTK_DIALOG (priv->auth_dialog));
  gtk_widget_hide (priv->auth_dialog);
  password = (gchar *) gtk_entry_get_text (GTK_ENTRY (priv->auth_dialog_entry));

  if (response == GTK_RESPONSE_OK)
    {

      password = (gchar *) gtk_entry_get_text (GTK_ENTRY (priv->auth_dialog_entry));
      fputs (password, priv->write_stream);
      fputs ("\n", priv->write_stream);

      while (fflush (priv->write_stream) != 0);

      queue_directive (applet, NULL, enable,
		       "enable_iface", priv->dev, (enable) ? "1" : "0", NULL);
    }
  else
    {
      shutdown_backend (applet, TRUE, FALSE);
      launch_backend   (applet, FALSE);
    }

  /* stab the root password */
  memset (password, ' ', sizeof (password));
  gtk_entry_set_text (GTK_ENTRY (priv->auth_dialog_entry), "");
}

static void
update_info_callback (ModemApplet *applet, xmlDoc *doc)
{
  xmlNodePtr iface;

  if (!doc)
    return;

  iface = find_dialup_interface_node (get_root_node (doc));
  if (!iface)
    return;

  get_interface_data (applet, iface);
  update_popup_buttons (applet);
}

static gboolean
update_info (ModemApplet *applet)
{
  queue_directive (applet, update_info_callback,
		   FALSE, "get", NULL);
  return TRUE;
}

static gboolean
check_backend (ModemApplet *applet)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  gint       status, pid = -1;
  GtkWidget *dialog;

  pid = waitpid (priv->pid, &status, WNOHANG);

  if (pid != 0)
    {
      if (errno == ECHILD || ((WIFEXITED (status)) && (WEXITSTATUS (status)) && (WEXITSTATUS(status) < 255)))
        {
	  dialog = gtk_message_dialog_new (NULL,
					   GTK_DIALOG_MODAL,
					   GTK_MESSAGE_WARNING,
					   GTK_BUTTONS_CLOSE,
					   _("The entered password is invalid"));
	  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
						    _("Check that you have typed it correctly and that "
						      "you haven't activated the \"caps lock\" key"));
	  gtk_dialog_run (GTK_DIALOG (dialog));
	  gtk_widget_destroy (dialog);
	}

      priv->timeout_id = 0;
      shutdown_backend (applet, FALSE, TRUE);
      launch_backend (applet, FALSE);

      return FALSE;
    }

  return TRUE;
}

static void
launch_backend (ModemApplet *applet, gboolean root_auth)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  gchar  *non_auth_args[] = { STB_SCRIPTS_DIR "/network-conf", NULL };
  gchar  *auth_args[]     = { SU_PATH, "-c", STB_SCRIPTS_DIR "/network-conf", NULL };
  gchar **args;
  int     p[2];

  pipe (p);
  priv->pid = forkpty (&priv->write_fd, NULL, NULL, NULL);
  args = (root_auth) ? auth_args : non_auth_args;

  if (priv->pid < 0)
    g_warning ("Could not spawn GST backend");
  else
    {
      if (priv->pid == 0)
        {
	  /* child process */
	  unsetenv("LC_ALL");
	  unsetenv("LC_MESSAGES");
	  unsetenv("LANG");
	  unsetenv("LANGUAGE");

	  dup2 (p[1], 1);
	  dup2 (p[1], 2);
	  close (p[0]);

	  execv (args[0], args);
	  exit (255);
	}
      else
        {
	  close (p[1]);

	  priv->read_fd = p[0];
	  priv->timeout_id = g_timeout_add_seconds (1, (GSourceFunc) check_backend, applet);
	  priv->info_id = g_timeout_add_seconds (3, (GSourceFunc) update_info, applet);
	  priv->read_stream = fdopen (priv->read_fd, "r");
	  priv->write_stream = fdopen (priv->write_fd, "w");
	  priv->has_root = root_auth;

	  setvbuf (priv->read_stream, NULL, _IONBF, 0);
	  fcntl (priv->read_fd, F_SETFL, 0);
	}
    }
}

static gboolean
launch_config_tool (GdkScreen *screen, gboolean is_isdn)
{
  gchar    *argv[4], *application;
  gboolean  ret;

  application = g_find_program_in_path (NETWORK_TOOL);

  if (!application)
    return FALSE;

  argv[0] = application;
  argv[1] = "--configure-type";
  argv[2] = (is_isdn) ? "isdn" : "modem";
  argv[3] = NULL;

  ret = gdk_spawn_on_screen (screen, NULL, argv, NULL, 0,
			     NULL, NULL, NULL, NULL);
  g_free (application);
  return ret;
}

static void
toggle_interface_non_root (ModemApplet *applet, gboolean enable)
{
  queue_directive (applet, rerun_backend_callback,
		   FALSE, "end", NULL);
}

static void
toggle_interface_root (ModemApplet *applet, gboolean enable)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  GtkWidget *dialog;
  gchar     *text;

  text = (enable) ?
    _("Do you want to connect?") :
    _("Do you want to disconnect?");

  dialog = gtk_message_dialog_new (NULL,
				   GTK_DIALOG_MODAL,
				   GTK_MESSAGE_QUESTION,
				   GTK_BUTTONS_NONE,
				   text);
  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
			  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			  (enable) ? _("C_onnect") : _("_Disconnect"),
			  GTK_RESPONSE_OK, NULL);
  gtk_window_set_screen (GTK_WINDOW (dialog),
			 gtk_widget_get_screen (GTK_WIDGET (applet)));

  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
    queue_directive (applet, NULL, enable,
		     "enable_iface", priv->dev, (enable) ? "1" : "0", NULL);

  gtk_widget_destroy (dialog);
}

static void
toggle_interface (ModemApplet *applet, gboolean enable)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);

  if (priv->has_root)
    toggle_interface_root (applet, enable);
  else
    toggle_interface_non_root (applet, enable);
}

static void
on_modem_applet_activate (GtkAction   *action,
			  ModemApplet *applet)
{
  toggle_interface (applet, TRUE);
}

static void
on_modem_applet_deactivate (GtkAction   *action,
			    ModemApplet *applet)
{
  toggle_interface (applet, FALSE);
}

static void
on_modem_applet_properties_clicked (GtkAction   *action,
				    ModemApplet *applet)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  GdkScreen *screen;
  GtkWidget *dialog;

  screen = gtk_widget_get_screen (GTK_WIDGET (applet));

  if (!launch_config_tool (screen, priv->is_isdn))
    {
      dialog = gtk_message_dialog_new (NULL,
				       GTK_DIALOG_DESTROY_WITH_PARENT,
				       GTK_MESSAGE_ERROR,
				       GTK_BUTTONS_CLOSE,
				       _("Could not launch network configuration tool"));
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
						_("Check that it's installed in the correct path "
						  "and that it has the correct permissions"));
      gtk_dialog_run (GTK_DIALOG (dialog));
      gtk_widget_destroy (dialog);
    }
}

static void
on_modem_applet_about_clicked (GtkAction   *action,
			       ModemApplet *applet)
{
  const gchar *authors[] = {
    "Carlos Garnacho Parro <carlosg@gnome.org>",
    NULL
  };
/*
  const gchar *documenters[] = {
    NULL
  };
*/
  gtk_show_about_dialog (NULL,
			 "version",            VERSION,
			 "copyright",          "Copyright \xC2\xA9 2004 Free Software Foundation. Inc.",
			 "comments",           _("Applet for activating and monitoring a dial-up network connection."),
			 "authors",            authors,
			 /* "documenters",        documenters, */
			 "translator-credits", _("translator-credits"),
			 "logo_icon_name",     "mate-modem-monitor-applet",
			 NULL);
}

static void
on_modem_applet_help_clicked (GtkAction   *action,
			      ModemApplet *applet)
{
  gtk_show_uri (gtk_widget_get_screen (GTK_WIDGET (applet)),
		"help:modemlights",
		gtk_get_current_event_time (),
		NULL);
}

static gboolean
modem_applet_fill (ModemApplet *applet)
{
  ModemAppletPrivate *priv = MODEM_APPLET_GET_PRIVATE (applet);
  gchar *ui_path;

  g_return_val_if_fail (PANEL_IS_APPLET (applet), FALSE);

  gtk_widget_show_all (GTK_WIDGET (applet));

  priv->action_group = gtk_action_group_new ("ModemLights Applet Actions");
  gtk_action_group_set_translation_domain (priv->action_group, GETTEXT_PACKAGE);
  gtk_action_group_add_actions (priv->action_group,
				menu_actions,
				G_N_ELEMENTS (menu_actions),
				applet);
  update_popup_buttons (applet);
  ui_path = g_build_filename (MODEM_MENU_UI_DIR, "modem-applet-menu.xml", NULL);
  mate_panel_applet_setup_menu_from_file (MATE_PANEL_APPLET (applet),
				     ui_path, priv->action_group);
  g_free (ui_path);

  return TRUE;
}

static gboolean
modem_applet_factory (MatePanelApplet *applet,
		      const gchar *iid,
		      gpointer     data)
{
  gboolean retval = FALSE;
    
  if (!strcmp (iid, "ModemLightsApplet"))
    retval = modem_applet_fill (MODEM_APPLET (applet)); 
  
  return retval;
}

MATE_PANEL_APPLET_OUT_PROCESS_FACTORY ("ModemAppletFactory",
				  TYPE_MODEM_APPLET,
				  "modem",
				  modem_applet_factory,
				  NULL)