File: gtk-sat-module.c

package info (click to toggle)
gpredict 1.1-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,280 kB
  • ctags: 2,873
  • sloc: ansic: 34,903; sh: 10,151; makefile: 417
file content (1565 lines) | stat: -rw-r--r-- 47,961 bytes parent folder | download
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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
  Gpredict: Real-time satellite tracking and orbit prediction program

  Copyright (C)  2001-2009  Alexandru Csete, OZ9AEC.

  Authors: Alexandru Csete <oz9aec@gmail.com>

  Comments, questions and bugreports should be submitted via
  http://sourceforge.net/projects/gpredict/
  More details can be found at the project home page:

  http://gpredict.oz9aec.net/
 
  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, visit http://www.fsf.org/
*/
/** \brief Main module container.
 *
 * The GtkSatModule widget is the top level container that contains the
 * individual views.
 *
 * more on layout ...
 *
 * Unfortunately, the GtkPaned widgets do not offer a sensible way to divide
 * the space between the children, e.g. set_position (50%). The only way to
 * set the gutter position is using pixel values, which by the way is in 
 * contradiction with the philosophy behind the use of containers in Gtk+.
 * We try to work around this silly shortcoming by doing all sorts of hack
 * around the size-allocate signal.
 */
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <sys/time.h>
#include "sgpsdp/sgp4sdp4.h"
#include "sat-log.h"
#include "gpredict-utils.h"
#include "config-keys.h"
#include "sat-cfg.h"
#include "mod-cfg.h"
#include "mod-cfg-get-param.h"
#include "mod-mgr.h"
#ifdef HAVE_CONFIG_H
#  include <build-config.h>
#endif
#include "time-tools.h"
#include "orbit-tools.h"
#include "predict-tools.h"
#include "gtk-sat-module.h"
#include "gtk-sat-module-popup.h"
#include "gtk-sat-module-tmg.h"
#include "gtk-sat-list.h"
#include "gtk-sat-map.h"
#include "gtk-polar-view.h"
#include "gtk-single-sat.h"
#include "gtk-rig-ctrl.h"
#include "gtk-rot-ctrl.h"
#include "compat.h"

//#ifdef G_OS_WIN32
//#  include "libc_internal.h"
//#  include "libc_interface.h"
//#endif

static void     gtk_sat_module_class_init     (GtkSatModuleClass   *class);
static void     gtk_sat_module_init           (GtkSatModule        *module);
static void     gtk_sat_module_destroy        (GtkObject           *object);
static void     gtk_sat_module_read_cfg_data  (GtkSatModule *module,
                                               const gchar *cfgfile);

static void     gtk_sat_module_load_sats      (GtkSatModule *module);
/* static gboolean gtk_sat_module_sats_are_equal (gconstpointer a, */
/*                            gconstpointer b); */
static gboolean gtk_sat_module_timeout_cb     (gpointer module);
static void     gtk_sat_module_update_sat     (gpointer key,
                                               gpointer val,
                                               gpointer data);
static void     gtk_sat_module_popup_cb       (GtkWidget *button,
                                               gpointer data);

static void     update_header                 (GtkSatModule *module);
static void     update_child                  (GtkWidget *child, gdouble tstamp);
static void     create_module_layout          (GtkSatModule *module);

static GtkWidget *create_view                 (GtkSatModule *module, guint num);

static void     fix_child_allocations   (GtkWidget *widget, gpointer data);

static void     reload_sats_in_child (GtkWidget *widget, GtkSatModule *module);



static GtkVBoxClass *parent_class = NULL;


GType
gtk_sat_module_get_type ()
{
    static GType gtk_sat_module_type = 0;

    if (!gtk_sat_module_type) {
        static const GTypeInfo gtk_sat_module_info = {
            sizeof (GtkSatModuleClass),
            NULL,  /* base_init */
            NULL,  /* base_finalize */
            (GClassInitFunc) gtk_sat_module_class_init,
            NULL,  /* class_finalize */
            NULL,  /* class_data */
            sizeof (GtkSatModule),
            5,     /* n_preallocs */
            (GInstanceInitFunc) gtk_sat_module_init,
        };

        gtk_sat_module_type = g_type_register_static (GTK_TYPE_VBOX,
                                                      "GtkSatModule",
                                                      &gtk_sat_module_info,
                                                      0);
    }

    return gtk_sat_module_type;
}


static void
gtk_sat_module_class_init (GtkSatModuleClass *class)
{
    GObjectClass      *gobject_class;
    GtkObjectClass    *object_class;
    GtkWidgetClass    *widget_class;
    GtkContainerClass *container_class;

    gobject_class   = G_OBJECT_CLASS (class);
    object_class    = (GtkObjectClass*) class;
    widget_class    = (GtkWidgetClass*) class;
    container_class = (GtkContainerClass*) class;

    parent_class = g_type_class_peek_parent (class);

    object_class->destroy = gtk_sat_module_destroy;

}



static void
gtk_sat_module_init (GtkSatModule *module)
{

    /* initialise data structures */
    module->win = NULL;

    module->qth = g_try_new0 (qth_t, 1);
    module->qth->lat = 0.0;
    module->qth->lon = 0.0;
    module->qth->alt = 0;

    module->satellites = g_hash_table_new_full (g_int_hash,
                                                g_int_equal,
                                                g_free,
                                                g_free);
    
    module->rotctrlwin = NULL;
    module->rotctrl    = NULL;
    module->rigctrlwin = NULL;

    module->state = GTK_SAT_MOD_STATE_DOCKED;
    module->busy = FALSE;

    module->layout = GTK_SAT_MOD_LAYOUT_1;
    module->view_1 = GTK_SAT_MOD_VIEW_MAP;
    module->view_2 = GTK_SAT_MOD_VIEW_POLAR;
    module->view_3 = GTK_SAT_MOD_VIEW_SINGLE;

    module->vpanedpos = -1;
    module->hpanedpos = -1;

    module->timerid = 0;
    
    module->throttle = 1;
    module->rtNow = 0.0;
    module->rtPrev = 0.0;
    module->tmgActive = FALSE;
    module->tmgPdnum = 0.0;
    module->tmgCdnum = 0.0;
    module->tmgReset = FALSE;
    
}


static void
gtk_sat_module_destroy (GtkObject *object)
{
    GtkSatModule *module = GTK_SAT_MODULE (object);

    
    /* stop timeout */
    if (module->timerid > 0)
        g_source_remove (module->timerid);

    /* destroy time controller */
    if (module->tmgActive) {
        gtk_widget_destroy (module->tmgWin);
        module->tmgActive = FALSE;
    }
    
    /* destroy radio and rotator controllers */
    if (module->rigctrlwin) {
        gtk_widget_destroy (module->rigctrlwin);
    }
    if (module->rotctrlwin) {
        gtk_widget_destroy (module->rotctrlwin);
    }

    /* clean up QTH */
    if (module->qth) {
        qth_data_free (module->qth);
        module->qth = NULL;
    }

    /* clean up satellites */
    if (module->satellites) {
        g_hash_table_destroy (module->satellites);
        module->satellites = NULL;
    }

    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/**** FIXME: Program goes into infinite loop when there is something
      wrong with cfg file. */
GtkWidget *
gtk_sat_module_new (const gchar *cfgfile)
{
    GtkWidget *widget;
    GtkWidget *butbox;


    /* Read configuration data.
       If cfgfile is not existing or is NULL, start the wizard
       in order to create a new configuration.
    */
    if ((cfgfile == NULL) || !g_file_test (cfgfile, G_FILE_TEST_EXISTS)) {

        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%s: Module %s is not valid."),
                     __FUNCTION__, cfgfile);

        return NULL;
    }

    /* create module widget */
    widget = g_object_new (GTK_TYPE_SAT_MODULE, NULL);

    g_signal_connect (widget, "realize",
                      G_CALLBACK (fix_child_allocations), NULL);

    /* load configuration; note that this will also set the module name */
    gtk_sat_module_read_cfg_data (GTK_SAT_MODULE (widget), cfgfile);

    /* module state */
    if ((g_key_file_has_key (GTK_SAT_MODULE (widget)->cfgdata,
                             MOD_CFG_GLOBAL_SECTION,
                             MOD_CFG_STATE, NULL)) &&
        sat_cfg_get_bool (SAT_CFG_BOOL_MOD_STATE)) {

        GTK_SAT_MODULE (widget)->state =
            g_key_file_get_integer (GTK_SAT_MODULE (widget)->cfgdata,
                                    MOD_CFG_GLOBAL_SECTION,
                                    MOD_CFG_STATE, NULL);
    }
    else {
        GTK_SAT_MODULE (widget)->state = GTK_SAT_MOD_STATE_DOCKED;
    }

    /* initialise time keeping vars to current time */
    GTK_SAT_MODULE (widget)->rtNow = get_current_daynum ();
    GTK_SAT_MODULE (widget)->rtPrev = get_current_daynum ();
    GTK_SAT_MODULE (widget)->tmgPdnum = get_current_daynum ();
    GTK_SAT_MODULE (widget)->tmgCdnum = get_current_daynum ();


    /* load satellites */
    gtk_sat_module_load_sats (GTK_SAT_MODULE (widget));
    
    /* create buttons */
    GTK_SAT_MODULE (widget)->popup_button =
        gpredict_mini_mod_button ("gpredict-mod-popup.png",
                                  _("Module options / shortcuts"));
    g_signal_connect (GTK_SAT_MODULE (widget)->popup_button, "clicked",
                      G_CALLBACK (gtk_sat_module_popup_cb), widget);

    GTK_SAT_MODULE (widget)->close_button =
        gpredict_mini_mod_button ("gpredict-mod-close.png",
                                  _("Close this module."));
    g_signal_connect (GTK_SAT_MODULE (widget)->close_button, "clicked",
                      G_CALLBACK (gtk_sat_module_close_cb), widget);

    /* create header; header should not be updated more than
       once pr. second.
    */
    GTK_SAT_MODULE (widget)->header = gtk_label_new (NULL);
    GTK_SAT_MODULE (widget)->head_count = 0;
    GTK_SAT_MODULE (widget)->head_timeout = 
        (GTK_SAT_MODULE(widget)->timeout > 1000 ? 1 : 
         (guint) floor (1000/GTK_SAT_MODULE(widget)->timeout));

    /* Event timeout
       Update every minute FIXME: user configurable
    */
    GTK_SAT_MODULE (widget)->event_timeout = 
        (GTK_SAT_MODULE(widget)->timeout > 60000 ? 1 : 
         (guint) floor (60000/GTK_SAT_MODULE(widget)->timeout));
    /* force update the first time */
    GTK_SAT_MODULE (widget)->event_count = GTK_SAT_MODULE (widget)->event_timeout;


    butbox = gtk_hbox_new (FALSE, 0);
    gtk_box_pack_start (GTK_BOX (butbox),
                        GTK_SAT_MODULE (widget)->header,
                        FALSE, FALSE, 10);
    gtk_box_pack_end (GTK_BOX (butbox),
                      GTK_SAT_MODULE (widget)->close_button,
                      FALSE, FALSE, 0);
    gtk_box_pack_end (GTK_BOX (butbox),
                      GTK_SAT_MODULE (widget)->popup_button,
                      FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (widget), butbox, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (widget), gtk_hseparator_new (), FALSE, FALSE, 0);

    create_module_layout (GTK_SAT_MODULE (widget));


    gtk_widget_show_all (widget);

    /* start timeout */
    GTK_SAT_MODULE(widget)->timerid = g_timeout_add (GTK_SAT_MODULE(widget)->timeout,
                                                     gtk_sat_module_timeout_cb,
                                                     widget);
    

    return widget;
}


/** \brief Create module layout and add views.
 *
 * It is assumed that module->layout and module->view_x have
 * sensible values.
 */
static void
create_module_layout (GtkSatModule *module)
{

    switch (module->layout) {
        
        /* one child, no setup needed here */
    case GTK_SAT_MOD_LAYOUT_1:
        module->child_1 = create_view (module, 1);
        module->child_2 = NULL;
        module->child_3 = NULL;
        gtk_container_add (GTK_CONTAINER (module), module->child_1);
        break;

        /* two children, we need a vpaned
           child_1 on top */
    case GTK_SAT_MOD_LAYOUT_2:
        module->vpaned = gtk_vpaned_new ();
        gtk_container_add (GTK_CONTAINER (module), module->vpaned);

        module->child_1 = create_view (module, 1);
        module->child_2 = create_view (module, 2);
        module->child_3 = NULL;

        gtk_paned_pack1 (GTK_PANED (module->vpaned),
                         module->child_1, TRUE, TRUE);
        gtk_paned_pack2 (GTK_PANED (module->vpaned),
                         module->child_2, TRUE, TRUE);

        break;

        /* one child on top, two at the bottom */
    case GTK_SAT_MOD_LAYOUT_3:
        module->vpaned = gtk_vpaned_new ();
        module->hpaned = gtk_hpaned_new ();
        gtk_container_add (GTK_CONTAINER (module), module->vpaned);
        gtk_paned_pack2 (GTK_PANED (module->vpaned),
                         module->hpaned,
                         TRUE, TRUE);

        module->child_1 = create_view (module, 1);
        module->child_2 = create_view (module, 2);
        module->child_3 = create_view (module, 3);

        gtk_paned_pack1 (GTK_PANED (module->vpaned),
                         module->child_1, TRUE, TRUE);
        gtk_paned_pack1 (GTK_PANED (module->hpaned),
                         module->child_2, TRUE, TRUE);
        gtk_paned_pack2 (GTK_PANED (module->hpaned),
                         module->child_3, TRUE, TRUE);

        break;

        /* two childre on top, on at the bottom */
    case GTK_SAT_MOD_LAYOUT_4:
        module->vpaned = gtk_vpaned_new ();
        module->hpaned = gtk_hpaned_new ();
        gtk_container_add (GTK_CONTAINER (module), module->vpaned);
        gtk_paned_pack1 (GTK_PANED (module->vpaned),
                         module->hpaned,
                         TRUE, TRUE);

        module->child_1 = create_view (module, 1);
        module->child_2 = create_view (module, 2);
        module->child_3 = create_view (module, 3);

        gtk_paned_pack1 (GTK_PANED (module->hpaned),
                         module->child_1, TRUE, TRUE);
        gtk_paned_pack2 (GTK_PANED (module->hpaned),
                         module->child_2, TRUE, TRUE);
        gtk_paned_pack2 (GTK_PANED (module->vpaned),
                         module->child_3, TRUE, TRUE);

        break;

    default:
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%s:%d: Invalid module layout (%d)"),
                     __FILE__, __LINE__, module->layout);
        break;

    }
}


static GtkWidget *
create_view (GtkSatModule *module, guint num)
{
    GtkWidget *view;
    gtk_sat_mod_view_t viewt = GTK_SAT_MOD_VIEW_LIST;

    /* get view type */
    switch (num) {
    case 1:
        viewt = module->view_1;
        break;
    case 2:
        viewt = module->view_2;
        break;
    case 3:
        viewt = module->view_3;
        break;
    default:
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%s:%d: Invalid child number (%d)"),
                     __FILE__, __LINE__, num);
        break;
    }

    /* create the corresponding child widget */
    switch (viewt) {

    case GTK_SAT_MOD_VIEW_LIST:
        view = gtk_sat_list_new (module->cfgdata,
                                 module->satellites,
                                 module->qth,
                                 0);
        break;

    case GTK_SAT_MOD_VIEW_MAP:
        view = gtk_sat_map_new (module->cfgdata,
                                module->satellites,
                                module->qth);
        break;

    case GTK_SAT_MOD_VIEW_POLAR:
        view = gtk_polar_view_new (module->cfgdata,
                                   module->satellites,
                                   module->qth);
        break;

    case GTK_SAT_MOD_VIEW_SINGLE:
        view = gtk_single_sat_new (module->cfgdata,
                                   module->satellites,
                                   module->qth,
                                   0);

        break;

    default:
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%s:%d: Invalid child type (%d)\nUsing GtkSatList..."),
                     __FILE__, __LINE__, num);

        view = gtk_sat_list_new (module->cfgdata,
                                 module->satellites,
                                 module->qth,
                                 0);
        break;

    }


    return view;
}


/** \brief Read moule configuration data.
 *  \ingroup satmodpriv
 *  \param module The GtkSatModule to which the configuration will be applied.
 *  \param cfgfile The configuration file.
 */
static void
gtk_sat_module_read_cfg_data (GtkSatModule *module, const gchar *cfgfile)
{
    gchar   *buffer = NULL;
    gchar   *qthfile;
    gchar   *confdir;
    gchar  **buffv;
    GError  *error = NULL;

    module->cfgdata = g_key_file_new ();
    g_key_file_set_list_separator (module->cfgdata, ';');

    /* Bail out with error message if data can not be read */
    if (!g_key_file_load_from_file (module->cfgdata, cfgfile,
                                    G_KEY_FILE_KEEP_COMMENTS, &error)) {

        g_key_file_free (module->cfgdata);

        sat_log_log (SAT_LOG_LEVEL_ERROR,
                     _("%s: Could not load config data from %s (%s)."),
                     __FUNCTION__, cfgfile, error->message);
        
        g_clear_error (&error);

        return;
    }

    /* debug message */
    sat_log_log (SAT_LOG_LEVEL_DEBUG,
                 _("%s: Reading configuration from %s"),
                 __FUNCTION__, cfgfile);

    /* set module name */
    buffer = g_path_get_basename (cfgfile);
    buffv = g_strsplit (buffer, ".mod", 0);
    module->name = g_strdup (buffv[0]);
    g_free (buffer);
    g_strfreev(buffv);

    /* get qth file */
    buffer = mod_cfg_get_str (module->cfgdata,
                              MOD_CFG_GLOBAL_SECTION,
                              MOD_CFG_QTH_FILE_KEY,
                              SAT_CFG_STR_DEF_QTH);

    confdir = get_user_conf_dir ();
    qthfile = g_strconcat (confdir, G_DIR_SEPARATOR_S, buffer, NULL);

    /* load QTH data */
    if (!qth_data_read (qthfile, module->qth)) {

        /* QTH file was not found for some reason */
        g_free (buffer);
        g_free (qthfile);

        /* remove cfg key */
        g_key_file_remove_key (module->cfgdata,
                               MOD_CFG_GLOBAL_SECTION,
                               MOD_CFG_QTH_FILE_KEY,
                               NULL);

        /* save modified cfg data to file */
        mod_cfg_save (module->name, module->cfgdata);

        /* try SAT_CFG_STR_DEF_QTH */
        buffer = sat_cfg_get_str (SAT_CFG_STR_DEF_QTH);
        qthfile = g_strconcat (confdir, G_DIR_SEPARATOR_S, buffer, NULL);

        if (!qth_data_read (qthfile, module->qth)) {

            sat_log_log (SAT_LOG_LEVEL_ERROR,
                         _("%s: Can not load default QTH file %s; using built-in defaults"),
                         __FUNCTION__, buffer);

            /* settings are really screwed up; we need some safe values here */
            module->qth->name = g_strdup (_("Error"));
            module->qth->loc  = g_strdup (_("Error"));
            module->qth->lat  = 0.0;
            module->qth->lon  = 0.0;
            module->qth->alt  = 0;
        }
    }

    g_free (buffer);
    g_free (confdir);
    g_free (qthfile);


    /* get timeout value */
    module->timeout = mod_cfg_get_int (module->cfgdata,
                                       MOD_CFG_GLOBAL_SECTION,
                                       MOD_CFG_TIMEOUT_KEY,
                                       SAT_CFG_INT_MODULE_TIMEOUT);

    /* layout */
    module->layout = mod_cfg_get_int (module->cfgdata,
                                      MOD_CFG_GLOBAL_SECTION,
                                      MOD_CFG_LAYOUT,
                                      SAT_CFG_INT_MODULE_LAYOUT);

    /* view 1 */
    module->view_1 = mod_cfg_get_int (module->cfgdata,
                                      MOD_CFG_GLOBAL_SECTION,
                                      MOD_CFG_VIEW_1,
                                      SAT_CFG_INT_MODULE_VIEW_1);

    /* view 2 */
    module->view_2 = mod_cfg_get_int (module->cfgdata,
                                      MOD_CFG_GLOBAL_SECTION,
                                      MOD_CFG_VIEW_2,
                                      SAT_CFG_INT_MODULE_VIEW_2);

    /* view 3 */
    module->view_3 = mod_cfg_get_int (module->cfgdata,
                                      MOD_CFG_GLOBAL_SECTION,
                                      MOD_CFG_VIEW_3,
                                      SAT_CFG_INT_MODULE_VIEW_3);

}


/** \brief Read satellites into memory.
 *
 * This function reads the list of satellites from the configfile and
 * and then adds each satellite to the hash table.
 */
static void
gtk_sat_module_load_sats      (GtkSatModule *module)
{
    gint   *sats = NULL;
    gsize   length;
    GError *error = NULL;
    guint   i;
    sat_t  *sat;
    guint  *key = NULL;
    guint   succ = 0;

    /* get list of satellites from config file; abort in case of error */
    sats = g_key_file_get_integer_list (module->cfgdata,
                                        MOD_CFG_GLOBAL_SECTION,
                                        MOD_CFG_SATS_KEY,
                                        &length,
                                        &error);
            
    if (error != NULL) {
        sat_log_log (SAT_LOG_LEVEL_ERROR,
                     _("%s: Failed to get list of satellites (%s)"),
                     __FUNCTION__, error->message);

        g_clear_error (&error);

        /* GLib API says nothing about the contents in case of error */
        if (sats) {
            g_free (sats);
        }

        return;
    }
            
    /* read each satellite into hash table */
    for (i = 0; i < length; i++) {

        sat = g_new (sat_t, 1);

        if (gtk_sat_data_read_sat (sats[i], sat)) {

            /* the satellite could not be read */
            sat_log_log (SAT_LOG_LEVEL_ERROR,
                         _("%s: Error reading data for #%d"),
                         __FUNCTION__, sats[i]);

            g_free (sat);
        }
        else {
            /* check whether satellite is already in list
               in order to avoid duplicates
            */

            key = g_new0 (guint, 1);
            *key = sats[i];

            if (g_hash_table_lookup (module->satellites, key) == NULL) {

                gtk_sat_data_init_sat (sat, module->qth);

                g_hash_table_insert (module->satellites,
                                     key,
                                     sat);

                succ++;

                sat_log_log (SAT_LOG_LEVEL_DEBUG,
                             _("%s: Read data for #%d"),
                             __FUNCTION__, sats[i]);


            }
            else {
                sat_log_log (SAT_LOG_LEVEL_WARN,
                             _("%s: Sat #%d already in list"),
                             __FUNCTION__, sats[i]);

                /* it is not needed in this case */
                g_free (sat);
            }

        }
    }

    sat_log_log (SAT_LOG_LEVEL_MSG,
                 _("%s: Read %d out of %d satellites"),
                 __FUNCTION__,
                 succ,
                 length);

    g_free (sats);

}



/** \brief Compare two sat_t data structures.
 *
 * The function returns TRUE if 
 * sat1->catnum == sat2->catnum.
 */
/* static gboolean */
/* gtk_sat_module_sats_are_equal (gconstpointer sat1, gconstpointer sat2) */
/* { */
/*     return ( SAT(sat1)->tle.catnr == SAT(sat2)->tle.catnr ); */

/* } */


/** \brief Module timeout callback.
 */
static gboolean
gtk_sat_module_timeout_cb     (gpointer module)
{
    GtkSatModule *mod = GTK_SAT_MODULE (module);
    gboolean       needupdate = FALSE;
    GdkWindowState state;
    gdouble   delta;


    if (mod->busy) {

        sat_log_log (SAT_LOG_LEVEL_WARN,
                     _("%s: Previous cycle missed it's deadline."),
                     __FUNCTION__);

        return TRUE;

    }


    /* in docked state, update only if tab is visible */
    switch (mod->state) {

    case GTK_SAT_MOD_STATE_DOCKED:

        if (mod_mgr_mod_is_visible (GTK_WIDGET (module))) {
            needupdate = TRUE;
        }
        break;

    default:
        state = gdk_window_get_state (GDK_WINDOW (GTK_WIDGET (module)->window));

        if (state & GDK_WINDOW_STATE_ICONIFIED) {
            needupdate = FALSE;
        }
        else {
            needupdate = TRUE;
        }
        break;
    }

    if (needupdate) {

        mod->busy = TRUE;


        mod->rtNow = get_current_daynum ();

        /* Update time if throttle != 0 */
        if (mod->throttle) {

            delta = mod->throttle * (mod->rtNow - mod->rtPrev);
            mod->tmgCdnum = mod->tmgPdnum + delta;

        }
        /* else nothing to do since tmg_time_set updates
           mod->tmgCdnum every time
        */

        /* time to update header? */
        mod->head_count++;
        if (mod->head_count == mod->head_timeout) {

            /* reset counter */
            mod->head_count = 0;
            
            update_header (mod);
        }

        /* time to update events? */
        if (mod->event_count == mod->event_timeout) {

            /* reset counter, this will make gtk_sat_module_update_sat
               recalculate events
            */
            mod->event_count = 0;
        }

        /* update satellite data */
        g_hash_table_foreach (mod->satellites,
                              gtk_sat_module_update_sat,
                              module);

        /* update children */
        if (mod->child_1 != NULL)
            update_child (mod->child_1, mod->tmgCdnum);

        if (mod->child_2 != NULL)
            update_child (mod->child_2, mod->tmgCdnum);

        if (mod->child_3 != NULL)
            update_child (mod->child_3, mod->tmgCdnum);

        /* update satellite data (it may have got out of sync during child updates) */
        g_hash_table_foreach (mod->satellites,
                              gtk_sat_module_update_sat,
                              module);

        /* send notice to radio and rotator controller */
        if (mod->rigctrl)
            gtk_rig_ctrl_update (GTK_RIG_CTRL (mod->rigctrl), mod->tmgCdnum);
        if (mod->rotctrl)
            gtk_rot_ctrl_update (GTK_ROT_CTRL (mod->rotctrl), mod->tmgCdnum);


        mod->event_count++;

        /* store time keeping variables */
        mod->rtPrev = mod->rtNow;
        mod->tmgPdnum = mod->tmgCdnum;

        if (mod->tmgActive) {

            /* update time control spin buttons when we are
               in RT or SRT mode */
            if (mod->throttle) {
                tmg_update_widgets (mod);
            }

        }


        mod->busy = FALSE;

    }

    return TRUE;
}


static void
update_child (GtkWidget *child, gdouble tstamp)
{
    if (IS_GTK_SAT_LIST(child)) {
        GTK_SAT_LIST (child)->tstamp = tstamp;
        gtk_sat_list_update (child);
    }

    else if (IS_GTK_SAT_MAP(child)) {
        GTK_SAT_MAP (child)->tstamp = tstamp;
        gtk_sat_map_update (child);
    }

    else if (IS_GTK_POLAR_VIEW(child)) {
        GTK_POLAR_VIEW (child)->tstamp = tstamp;
        gtk_polar_view_update (child);
    }

    else if (IS_GTK_SINGLE_SAT(child)) {
        GTK_SINGLE_SAT (child)->tstamp = tstamp;
        gtk_single_sat_update (child);
    }

    else {
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%f:%d: Unknown child type"),
                     __FILE__, __LINE__);
    }
}


/** \brief Update a given satellite.
 *  \param key The hash table key (catnum)
 *  \param val The hash table value (sat_t structure)
 *  \param data User data (the GtkSatModule widget).
 *
 * This function updates the tracking data for a given satelite. It is called by
 * the timeout handler for each element in the hash table.
 */
static void
gtk_sat_module_update_sat    (gpointer key, gpointer val, gpointer data)
{
    sat_t        *sat;
    GtkSatModule *module;
    gdouble       daynum;
    double        age;
    obs_set_t     obs_set = {0,0,0,0};
    geodetic_t    sat_geodetic = {0,0,0,0};
    geodetic_t    obs_geodetic = {0,0,0,0};
    gdouble       maxdt;

    g_return_if_fail ((val != NULL) && (data != NULL));

    sat = SAT(val);
    module = GTK_SAT_MODULE (data);


    /* get current time (real or simulated */
    daynum = module->tmgCdnum;

    /* update events if the event counter has been reset
       and the other requirements are fulfilled
    */
    if ((GTK_SAT_MODULE (module)->event_count == 0) &&
        (sat->otype != ORBIT_TYPE_GEO) &&
        (sat->otype != ORBIT_TYPE_DECAYED) &&
        has_aos (sat, module->qth))    {

        /* Note that has_aos may return TRUE for geostationary sats
           whose orbit deviate from a true-geostat orbit, however,
           find_aos and find_los will not go beyond the time limit
           we specify (in those cases they return 0.0 for AOS/LOS times.
           We use SAT_CFG_INT_PRED_LOOK_AHEAD for upper time limit
        */
        maxdt = (gdouble) sat_cfg_get_int (SAT_CFG_INT_PRED_LOOK_AHEAD);
        sat->aos = find_aos (sat, module->qth, daynum, maxdt);
        sat->los = find_los (sat, module->qth, daynum, maxdt);

    }


    /*** FIXME: we don't need to do this every time! */
    obs_geodetic.lon = module->qth->lon * de2ra;
    obs_geodetic.lat = module->qth->lat * de2ra;
    obs_geodetic.alt = module->qth->alt / 1000.0;
    obs_geodetic.theta = 0;


    sat->jul_utc = daynum;
    sat->tsince = (sat->jul_utc - sat->jul_epoch) * xmnpda;

    /*** FIXME: STS-122 FIX: Shuttle is fixed at launch site before launch
         Actually, first TLE set seem invalid before MET = 00:03:?? = 00:04:00
     */
    /* check that we have a GtkMet child */
    /**** FIXME: GtkMet may not be child 2.... */
    if G_UNLIKELY(FALSE) {

    }
    else {

        /* call the norad routines according to the deep-space flag */
        if (sat->flags & DEEP_SPACE_EPHEM_FLAG)
            SDP4 (sat, sat->tsince);
        else
            SGP4 (sat, sat->tsince);

        /* scale position and velocity to km and km/sec */
        Convert_Sat_State (&sat->pos, &sat->vel);

        /* get the velocity of the satellite */
        Magnitude (&sat->vel);
        sat->velo = sat->vel.w;
        Calculate_Obs (sat->jul_utc, &sat->pos, &sat->vel, &obs_geodetic, &obs_set);
        Calculate_LatLonAlt (sat->jul_utc, &sat->pos, &sat_geodetic);
        
        /*** FIXME: should we ensure sat_geodetic.lon stays between -pi and pi? */
        while (sat_geodetic.lon < -pi)
            sat_geodetic.lon += twopi;
        
        while (sat_geodetic.lon > (pi))
            sat_geodetic.lon -= twopi;
        
        sat->az = Degrees (obs_set.az);
        sat->el = Degrees (obs_set.el);
        sat->range = obs_set.range;
        sat->range_rate = obs_set.range_rate;
        sat->ssplat = Degrees (sat_geodetic.lat);
        sat->ssplon = Degrees (sat_geodetic.lon);
        sat->alt = sat_geodetic.alt;
        sat->ma = Degrees (sat->phase);
        sat->ma *= 256.0/360.0;
        sat->phase = Degrees (sat->phase);

        /* same formulas, but the one from predict is nicer */
        //sat->footprint = 2.0 * xkmper * acos (xkmper/sat->pos.w);
        sat->footprint = 12756.33 * acos (xkmper / (xkmper+sat->alt));
        age = sat->jul_utc - sat->jul_epoch;
        sat->orbit = (long) floor((sat->tle.xno * xmnpda/twopi +
                                   age * sat->tle.bstar * ae) * age +
                                  sat->tle.xmo/twopi) + sat->tle.revnum - 1;


        /*** FIXME: Squint + AOS / LOS code */

    }
}



/** \brief Module options
 *
 * Invoke module-wide popup menu
 */
static void
gtk_sat_module_popup_cb       (GtkWidget *button, gpointer data)
{
    gtk_sat_module_popup (GTK_SAT_MODULE (data));
}


/** \brief Close module.
 *  \param button The button widget that received the signal.
 *  \param data Pointer the GtkSatModule widget, which should be destroyed.
 *
 * This function is called when the user clicks on the "close" minibutton.
 * The functions checks the state of the module. If the module is docked
 * it is removed from the mod-mgr notebook whereafter it is destroyed.
 * if the module is either in undocked or fullscreen state, the parent
 * window is destroyed, which will automatically destroy the module as
 * well.
 *
 * NOTE: Don't use button, since we don't know what kind of widget it is
 *       (it may be button or menu item).
 */
void
gtk_sat_module_close_cb       (GtkWidget *button, gpointer data)
{
    GtkSatModule *module = GTK_SAT_MODULE (data);
    gchar        *name;
    gint          retcode;

    name = g_strdup (module->name);

    sat_log_log (SAT_LOG_LEVEL_DEBUG,
                 _("%s: Module %s recevied CLOSE signal."),
                 __FUNCTION__, name);

    /* save configuration to ensure that dynamic data like state is stored */
    mod_cfg_save (module->name, module->cfgdata);

    switch (module->state) {

    case GTK_SAT_MOD_STATE_DOCKED:
        sat_log_log (SAT_LOG_LEVEL_DEBUG,
                     _("%s: Module %s is in DOCKED state."),
                     __FUNCTION__, name);

        retcode = mod_mgr_remove_module (GTK_WIDGET (module));

        if (retcode) {
            sat_log_log (SAT_LOG_LEVEL_BUG,
                         _("%s: Module %s was not found in mod-mgr (%d)\n"\
                           "Internal state is corrupt?"),
                         __FUNCTION__, name, retcode);
        }

        break;

    case GTK_SAT_MOD_STATE_WINDOW:
        sat_log_log (SAT_LOG_LEVEL_DEBUG,
                     _("%s: Module %s is in WINDOW state."),
                     __FUNCTION__, name);

        retcode = mod_mgr_remove_module (GTK_WIDGET (module));

        if (retcode) {
            sat_log_log (SAT_LOG_LEVEL_BUG,
                         _("%s: Module %s was not found in mod-mgr (%d)\n"\
                           "Internal state is corrupt?"),
                         __FUNCTION__, name, retcode);
        }

        /* increase referene count */
        g_object_ref (module);

        /* remove module from window, destroy window */
        gtk_container_remove (GTK_CONTAINER (GTK_SAT_MODULE (module)->win),
                              GTK_WIDGET (module));
        gtk_widget_destroy (GTK_SAT_MODULE (module)->win);
        GTK_SAT_MODULE (module)->win = NULL;

        /* release module */
        g_object_unref (module);

        break;

    case GTK_SAT_MOD_STATE_FULLSCREEN:
        sat_log_log (SAT_LOG_LEVEL_DEBUG,
                     _("%s: Module %s is in FULLSCREEN state."),
                     __FUNCTION__, name);

        retcode = mod_mgr_remove_module (GTK_WIDGET (module));

        if (retcode) {
            sat_log_log (SAT_LOG_LEVEL_BUG,
                         _("%s: Module %s was not found in mod-mgr (%d)\n"\
                           "Internal state is corrupt?"),
                         __FUNCTION__, name, retcode);
        }


        /* increase referene count */
        g_object_ref (module);

        /* remove module from window, destroy window */
        gtk_container_remove (GTK_CONTAINER (GTK_SAT_MODULE (module)->win),
                              GTK_WIDGET (module));
        gtk_widget_destroy (GTK_SAT_MODULE (module)->win);
        GTK_SAT_MODULE (module)->win = NULL;

        /* release module */
        g_object_unref (module);

        break;

    default:
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%s: Module %s has unknown state: %d"),
                     __FUNCTION__, name, module->state);
        break;
    }

    /* appearantly, module will be destroyed when removed from notebook */
    /* gtk_widget_destroy (GTK_WIDGET (module)); */

    sat_log_log (SAT_LOG_LEVEL_MSG,
                 _("%s: Module %s closed."),
                 __FUNCTION__, name);

    g_free (name);
             
}



/** \brief Configure module.
 *  \param button The button widget that received the signal.
 *  \param data Pointer the GtkSatModule widget, which should be reconfigured
 *
 * This function is called when the user clicks on the "configure" minibutton.
 * The function incokes the mod_cfg_edit funcion, which has the same look and feel
 * as the dialog used to create a new module.
 *
 * NOTE: Don't use button, since we don't know what kind of widget it is
 *       (it may be button or menu item).
 */
void
gtk_sat_module_config_cb       (GtkWidget *button, gpointer data)
{
    GtkSatModule        *module = GTK_SAT_MODULE (data);
    GtkWidget           *parent;
    GtkWidget           *toplevel;
    gchar               *name;
    gchar               *cfgfile;
    mod_cfg_status_t     retcode;
    gtk_sat_mod_state_t  laststate;
    gint w,h;


    if (module->win != NULL)
        toplevel = module->win;
    else
        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (data));

    name = g_strdup (module->name);

    sat_log_log (SAT_LOG_LEVEL_DEBUG,
                 _("%s: Module %s recevied CONFIG signal."),
                 __FUNCTION__, name);

    /* stop timeout */
    if (!g_source_remove (module->timerid)) {
        /* internal error, since the timerid appears
           to be invalid.
        */
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%s: Could not stop timeout callback\n"\
                       "%s: Source ID %d seems invalid."),
                     __FUNCTION__, __FUNCTION__, module->timerid);

    }
    else {
        module->timerid = -1;

        retcode = mod_cfg_edit (name, module->cfgdata, toplevel);

        if (retcode == MOD_CFG_OK) {
            /* save changes */
            retcode = mod_cfg_save (name, module->cfgdata);

            if (retcode != MOD_CFG_OK) {

                /**** FIXME: dialog */
                sat_log_log (SAT_LOG_LEVEL_ERROR,
                             _("%s: Module configuration failed for some reason."),
                             __FUNCTION__);

                /* don't try to reload config since it may be
                   invalid; keep original
                */

            }
            else {

                /* store state and size */
                laststate = module->state;
                w = GTK_WIDGET (module)->allocation.width;
                h = GTK_WIDGET (module)->allocation.height;

                gtk_sat_module_close_cb (NULL, module);

                gchar *confdir = get_modules_dir ();
                cfgfile = g_strconcat (confdir, G_DIR_SEPARATOR_S, name, ".mod", NULL);
                g_free (confdir);

                module = GTK_SAT_MODULE (gtk_sat_module_new (cfgfile));
                module->state = laststate;
                
                switch (laststate) {

                case GTK_SAT_MOD_STATE_DOCKED:

                    /* re-open module by adding it to the mod-mgr */
                    mod_mgr_add_module (GTK_WIDGET (module), TRUE);

                    /* get size allocation from parent and set some reasonable
                       initial GtkPaned positions */
                    parent = gtk_widget_get_parent (GTK_WIDGET (module));
                    module->hpanedpos = parent->allocation.width / 2;
                    module->vpanedpos = parent->allocation.height / 2;
                    gtk_sat_module_fix_size (GTK_WIDGET (module));

                    break;

                case GTK_SAT_MOD_STATE_WINDOW:

                    /* add to module manager */
                    mod_mgr_add_module (GTK_WIDGET (module), FALSE);

                    /* create window */
                    module->win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
                    gtk_window_set_title (GTK_WINDOW (module->win),
                                          module->name);
                    gtk_window_set_default_size (GTK_WINDOW (module->win),
                                                 w, h);

                    /** FIXME: window icon and such */

                    /* add module to window */
                    gtk_container_add (GTK_CONTAINER (module->win),
                                       GTK_WIDGET (module));

                    /* show window */
                    gtk_widget_show_all (module->win);

                    break;

                case GTK_SAT_MOD_STATE_FULLSCREEN:

                    /* add to module manager */
                    mod_mgr_add_module (GTK_WIDGET (module), FALSE);

                    /* create window */
                    module->win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
                    gtk_window_set_title (GTK_WINDOW (module->win),
                                          module->name);
                    gtk_window_set_default_size (GTK_WINDOW (module->win),
                                                 w, h);

                    /** FIXME: window icon and such */

                    /* add module to window */
                    gtk_container_add (GTK_CONTAINER (module->win),
                                       GTK_WIDGET (module));

                    /* show window */
                    gtk_widget_show_all (module->win);

                    gtk_window_fullscreen (GTK_WINDOW (module->win));

                    break;

                default:
                    sat_log_log (SAT_LOG_LEVEL_BUG,
                                 _("%s: Module %s has unknown state: %d"),
                                 __FUNCTION__, name, module->state);
                    break;
                }
                    

                g_free (cfgfile);

            }

        }
        else {
            /* user cancelled => just re-start timer */
            module->timerid = g_timeout_add (module->timeout,
                                             gtk_sat_module_timeout_cb,
                                             data);
            
        }
    }

    g_free (name);

}


static void
update_header (GtkSatModule *module)
{
    gchar *fmtstr;
    time_t t;
    guint size;
    gchar buff[TIME_FORMAT_MAX_LENGTH+1];



    t = (module->tmgCdnum - 2440587.5)*86400.;

    fmtstr = sat_cfg_get_str (SAT_CFG_STR_TIME_FORMAT);
    
    /* format either local time or UTC depending on check box */
    if (sat_cfg_get_bool (SAT_CFG_BOOL_USE_LOCAL_TIME))
        size = strftime (buff, TIME_FORMAT_MAX_LENGTH, fmtstr, localtime (&t));
    else
        size = strftime (buff, TIME_FORMAT_MAX_LENGTH, fmtstr, gmtime (&t));

    if (size < TIME_FORMAT_MAX_LENGTH)
        buff[size]='\0';
    else
        buff[TIME_FORMAT_MAX_LENGTH]='\0';

    gtk_label_set_text (GTK_LABEL (module->header), buff);
    g_free (fmtstr);

    if (module->tmgActive)
        tmg_update_state (module);
}



/** \brief Fix size allocations for children.
 *
 * This function is called when the GtkSatModule has been realised.
 * The purpose of this function is to attempt to divide the space
 * between the children on a 50/50 basis based on the size allocation
 * of the GtkSatModule.
 *
 * Sometimes the size allocation will not be available, e.g. when docking
 * back to an existing notebook. Therefore, we first try to use the
 * stored gutter positions module->vpanedpos and module->hpanedpos.
 */
static void
fix_child_allocations            (GtkWidget *widget, gpointer data)
{
    GtkSatModule *module = GTK_SAT_MODULE (widget);


    switch (module->layout) {
        
        /* nothing to do */
    case GTK_SAT_MOD_LAYOUT_1:
        break;

    case GTK_SAT_MOD_LAYOUT_2:
        if (module->vpanedpos != -1) {
            gtk_paned_set_position (GTK_PANED (module->vpaned),
                                    module->vpanedpos);
        }
        else if (widget->allocation.height > 0) {
            gtk_paned_set_position (GTK_PANED (module->vpaned),
                                    widget->allocation.height / 2);
        }
        else {
        }
        break;

        /* one child on top, two at the bottom */
    case GTK_SAT_MOD_LAYOUT_3:
    case GTK_SAT_MOD_LAYOUT_4:
        if (module->vpanedpos == -1) {
            gtk_paned_set_position (GTK_PANED (module->vpaned),
                                    widget->allocation.height / 2);
        }
        else {
            gtk_paned_set_position (GTK_PANED (module->vpaned),
                                    module->vpanedpos);
        }

        if (module->hpanedpos == -1) {
            gtk_paned_set_position (GTK_PANED (module->hpaned),
                                    widget->allocation.width / 2);
        }
        else {
            gtk_paned_set_position (GTK_PANED (module->hpaned),
                                    module->hpanedpos);
        }

        break;

    default:
        break;

    }
}


/** \brief Fix size allocations for children.
 *  \param module The GtkSatModule widget.
 *
 * This function is a public wrapper around the fix_allocations function.
 */
void
gtk_sat_module_fix_size (GtkWidget *module)
{
    fix_child_allocations (module, NULL);
}



static gboolean empty (gpointer key, gpointer val, gpointer data)
{
    /* TRUE => sat removed from hash table */
    return TRUE;
}


/** \brief Reload satellites.
 *  \param module Pointer to a GtkSatModule widget.
 *
 * This function is used to reload the satellites in a module. This is can be
 * useful when:
 *
 *   1. The TLE files have been updated.
 *   2. The module configuration has changed (i.e. which satellites to track).
 *
 * The function assumes that module->cfgdata has already been updated, and so
 * all it has to do is to free module->satellites and re-execute the satellite
 * loading sequence.
 */
void
gtk_sat_module_reload_sats    (GtkSatModule *module)
{

    g_return_if_fail (IS_GTK_SAT_MODULE (module));

    /* lock module */
    module->busy = TRUE;

    sat_log_log (SAT_LOG_LEVEL_MSG,
                 _("%s: Reloading satellites for module %s"),
                 __FUNCTION__, module->name);

    /* remove each element from the hash table, but keep the hash table */
    g_hash_table_foreach_remove (module->satellites, empty, NULL);

    /* reset event counter so that next AOS/LOS gets re-calculated */
    module->event_count = 0;       

    /* load satellites */
    gtk_sat_module_load_sats (module);

    /* update children */
    if (GTK_SAT_MODULE (module)->child_1 != NULL)
        reload_sats_in_child (GTK_SAT_MODULE (module)->child_1, GTK_SAT_MODULE (module));
    
    if (GTK_SAT_MODULE (module)->child_2 != NULL)
        reload_sats_in_child (GTK_SAT_MODULE (module)->child_2, GTK_SAT_MODULE (module));
    
    if (GTK_SAT_MODULE (module)->child_3 != NULL)
        reload_sats_in_child (GTK_SAT_MODULE (module)->child_3, GTK_SAT_MODULE (module));
    
    /* FIXME: radio and rotator controller */
    
    /* unlock module */
    module->busy = FALSE;

}


/** \brief Reload satellites in view */
static void
reload_sats_in_child (GtkWidget *widget, GtkSatModule *module)
{
 
 
    if (IS_GTK_SINGLE_SAT (G_OBJECT (widget))) {
        gtk_single_sat_reload_sats (widget, module->satellites);
    }

    else if (IS_GTK_POLAR_VIEW (widget)) {
        gtk_polar_view_reload_sats (widget, module->satellites);
    }

    else if (IS_GTK_SAT_MAP (widget)) {
        gtk_sat_map_reload_sats (widget, module->satellites);
    }

    else if (IS_GTK_SAT_LIST (widget)) {
    }


    else {
        sat_log_log (SAT_LOG_LEVEL_BUG,
                     _("%f:%d: Unknown child type"),
                     __FILE__, __LINE__);
    }

}


/** \brief Re-configure module.
 *  \param module The module.
 *  \param local Flag indicating whether reconfiguration is requested from 
 *               local configuration dialog.
 *
 */
void
gtk_sat_module_reconf         (GtkSatModule *module, gboolean local)
{
}