File: gtk-event-list.c

package info (click to toggle)
gpredict 2.3-72-gc596101-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,264 kB
  • sloc: ansic: 39,918; makefile: 469; python: 143; sh: 85
file content (876 lines) | stat: -rw-r--r-- 29,655 bytes parent folder | download | duplicates (2)
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
/*
  Gpredict: Real-time satellite tracking and orbit prediction program

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

  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/
*/
#ifdef HAVE_CONFIG_H
#include <build-config.h>
#endif
#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "config-keys.h"
#include "gpredict-utils.h"
#include "gtk-event-list.h"
#include "gtk-event-list-popup.h"
#include "gtk-sat-data.h"
#include "mod-cfg-get-param.h"
#include "locator.h"
#include "orbit-tools.h"
#include "sat-cfg.h"
#include "sat-log.h"
#include "sat-vis.h"
#include "sat-info.h"
#include "sgpsdp/sgp4sdp4.h"


#define EVENT_LIST_COL_DEF (EVENT_LIST_FLAG_NAME | EVENT_LIST_FLAG_AZ | EVENT_LIST_FLAG_EL | EVENT_LIST_FLAG_TIME)

/** Column titles indexed with column symb. refs. */
const gchar    *EVENT_LIST_COL_TITLE[EVENT_LIST_COL_NUMBER] = {
    N_("Satellite"),
    N_("Catnum"),
    N_("Az"),
    N_("El"),
    N_("Event"),
    N_("AOS/LOS"),
    N_("Decayed"),              /*should never be seen */
    N_("BOLD")                  /*should never be seen */
};

/** Column title hints indexed with column symb. refs. */
const gchar    *EVENT_LIST_COL_HINT[EVENT_LIST_COL_NUMBER] = {
    N_("Satellite Name"),
    N_("Catalogue Number"),
    N_("Azimuth"),
    N_("Elevation"),
    N_("Next event type (A: AOS, L: LOS)"),
    N_("Countdown until next event")
};

/* field alignments */
const gfloat    EVENT_LIST_COL_XALIGN[EVENT_LIST_COL_NUMBER] = {
    0.0,                        // name
    0.5,                        // catnum
    1.0,                        // az
    1.0,                        // el
    0.5,                        // event type
    1.0,                        // time
};

/* column head alignments */
const gfloat    EVENT_LIST_HEAD_XALIGN[EVENT_LIST_COL_NUMBER] = {
    0.0,                        // name
    0.5,                        // catnum
    0.5,                        // az
    0.5,                        // el
    0.5,                        // event type
    1.0,                        // time
};

static void     gtk_event_list_class_init(GtkEventListClass * class);
static void     gtk_event_list_init(GtkEventList * list);
static void     gtk_event_list_destroy(GtkWidget * widget);
static GtkTreeModel *create_and_fill_model(GHashTable * sats);
static void     event_list_add_satellites(gpointer key,
                                          gpointer value, gpointer user_data);
static gboolean event_list_update_sats(GtkTreeModel * model,
                                       GtkTreePath * path,
                                       GtkTreeIter * iter, gpointer data);

/* cell rendering related functions */
static void     check_and_set_cell_renderer(GtkTreeViewColumn * column,
                                            GtkCellRenderer * renderer,
                                            gint i);
static void     evtype_cell_data_function(GtkTreeViewColumn * col,
                                          GtkCellRenderer * renderer,
                                          GtkTreeModel * model,
                                          GtkTreeIter * iter, gpointer column);
static void     time_cell_data_function(GtkTreeViewColumn * col,
                                        GtkCellRenderer * renderer,
                                        GtkTreeModel * model,
                                        GtkTreeIter * iter, gpointer column);
static void     degree_cell_data_function(GtkTreeViewColumn * col,
                                          GtkCellRenderer * renderer,
                                          GtkTreeModel * model,
                                          GtkTreeIter * iter, gpointer column);
static gint     event_cell_compare_function(GtkTreeModel * model,
                                            GtkTreeIter * a,
                                            GtkTreeIter * b,
                                            gpointer user_data);

static gboolean popup_menu_cb(GtkWidget * treeview, gpointer list);
static gboolean button_press_cb(GtkWidget * treeview,
                                GdkEventButton * event, gpointer list);
static void     view_popup_menu(GtkWidget * treeview,
                                GdkEventButton * event, gpointer list);
static void     row_activated_cb(GtkTreeView * tree_view,
                                 GtkTreePath * path,
                                 GtkTreeViewColumn * column, gpointer list);

static GtkBoxClass *parent_class = NULL;

GType gtk_event_list_get_type()
{
    static GType    gtk_event_list_type = 0;

    if (!gtk_event_list_type)
    {
        static const GTypeInfo gtk_event_list_info = {
            sizeof(GtkEventListClass),
            NULL,               /* base_init */
            NULL,               /* base_finalize */
            (GClassInitFunc) gtk_event_list_class_init,
            NULL,               /* class_finalize */
            NULL,               /* class_data */
            sizeof(GtkEventList),
            5,                  /* n_preallocs */
            (GInstanceInitFunc) gtk_event_list_init,
            NULL
        };

        gtk_event_list_type = g_type_register_static(GTK_TYPE_BOX,
                                                     "GtkEventList",
                                                     &gtk_event_list_info, 0);
    }

    return gtk_event_list_type;
}

static void gtk_event_list_class_init(GtkEventListClass * class)
{
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
    widget_class->destroy = gtk_event_list_destroy;
    parent_class = g_type_class_peek_parent(class);
}

static void gtk_event_list_init(GtkEventList * list)
{
    (void)list;
}

static void gtk_event_list_destroy(GtkWidget * widget)
{
    GtkEventList   *evlist = GTK_EVENT_LIST(widget);

    g_key_file_set_integer(evlist->cfgdata, MOD_CFG_EVENT_LIST_SECTION,
                           MOD_CFG_EVENT_LIST_SORT_COLUMN,
                           evlist->sort_column);

    g_key_file_set_integer(evlist->cfgdata, MOD_CFG_EVENT_LIST_SECTION,
                           MOD_CFG_EVENT_LIST_SORT_ORDER, evlist->sort_order);

    (*GTK_WIDGET_CLASS(parent_class)->destroy) (widget);
}

/**
 * Create a new GtkEventList widget.
 * @param cfgdata Pointer to the module configuration data.
 * @param sats Hash table containing the satellites tracked by the parent module.
 * @param qth Pointer to the QTH used by this module.
 * @param columns Visible columns (currently not in use).
 */
GtkWidget      *gtk_event_list_new(GKeyFile * cfgdata, GHashTable * sats,
                                   qth_t * qth, guint32 columns)
{
    GtkWidget      *widget;
    GtkEventList   *evlist;
    GtkTreeModel   *model, *filter, *sortable;
    guint           i;
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;

    (void)columns;

    widget = g_object_new(GTK_TYPE_EVENT_LIST, NULL);
    evlist = GTK_EVENT_LIST(widget);

    evlist->update = gtk_event_list_update;

    evlist->cfgdata = cfgdata;
    evlist->sort_column = EVENT_LIST_COL_TIME;
    evlist->sort_order = GTK_SORT_ASCENDING;
    if (g_key_file_has_key(evlist->cfgdata, MOD_CFG_EVENT_LIST_SECTION,
                           MOD_CFG_EVENT_LIST_SORT_COLUMN, NULL))
    {
        evlist->sort_column = g_key_file_get_integer(evlist->cfgdata,
                                                     MOD_CFG_EVENT_LIST_SECTION,
                                                     MOD_CFG_EVENT_LIST_SORT_COLUMN,
                                                     NULL);
        if ((evlist->sort_column > EVENT_LIST_COL_NUMBER)
            || (evlist->sort_column < 0))
        {
            evlist->sort_column = EVENT_LIST_COL_TIME;
        }
    }
    if (g_key_file_has_key(evlist->cfgdata,
                           MOD_CFG_EVENT_LIST_SECTION,
                           MOD_CFG_EVENT_LIST_SORT_ORDER, NULL))
    {
        evlist->sort_order = g_key_file_get_integer(evlist->cfgdata,
                                                    MOD_CFG_EVENT_LIST_SECTION,
                                                    MOD_CFG_EVENT_LIST_SORT_ORDER,
                                                    NULL);
        if ((evlist->sort_order > 1) || (evlist->sort_order < 0))
        {
            evlist->sort_order = GTK_SORT_ASCENDING;
        }
    }

    evlist->satellites = sats;
    evlist->qth = qth;

    evlist->flags = EVENT_LIST_COL_DEF;
    evlist->treeview = gtk_tree_view_new();
    gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(evlist->treeview),
                                 GTK_TREE_VIEW_GRID_LINES_NONE);

    /* create treeview columns */
    for (i = 0; i < EVENT_LIST_COL_NUMBER; i++)
    {
        renderer = gtk_cell_renderer_text_new();
        g_object_set(G_OBJECT(renderer), "xalign", EVENT_LIST_COL_XALIGN[i],
                     NULL);

        column =
            gtk_tree_view_column_new_with_attributes(_
                                                     (EVENT_LIST_COL_TITLE[i]),
                                                     renderer, "text", i,
                                                     "weight",
                                                     EVENT_LIST_COL_BOLD,
                                                     NULL);

        gtk_tree_view_insert_column(GTK_TREE_VIEW(evlist->treeview),
                                    column, -1);
        gtk_tree_view_column_set_alignment(column, EVENT_LIST_HEAD_XALIGN[i]);
        gtk_tree_view_column_set_sort_column_id(column, i);
        check_and_set_cell_renderer(column, renderer, i);

        /* hide columns that have not been specified */
        if (!(evlist->flags & (1 << i)))
        {
            gtk_tree_view_column_set_visible(column, FALSE);
        }
    }

    /* create model and finalise treeview */
    model = create_and_fill_model(evlist->satellites);
    filter = gtk_tree_model_filter_new(model, NULL);
    sortable = gtk_tree_model_sort_new_with_model(filter);
    evlist->sortable = sortable;
    gtk_tree_model_filter_set_visible_column(GTK_TREE_MODEL_FILTER(filter),
                                             EVENT_LIST_COL_DECAY);
    gtk_tree_view_set_model(GTK_TREE_VIEW(evlist->treeview), sortable);

    /* The time sort function needs to be special */
    gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(sortable),
                                    EVENT_LIST_COL_TIME,
                                    event_cell_compare_function, NULL, NULL);

    gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortable),
                                         evlist->sort_column,
                                         evlist->sort_order);

    g_object_unref(model);
    g_object_unref(filter);
    g_object_unref(sortable);

    g_signal_connect(evlist->treeview, "button-press-event",
                     G_CALLBACK(button_press_cb), widget);
    g_signal_connect(evlist->treeview, "popup-menu",
                     G_CALLBACK(popup_menu_cb), widget);
    g_signal_connect(evlist->treeview, "row-activated",
                     G_CALLBACK(row_activated_cb), widget);

    evlist->swin = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(evlist->swin),
                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    gtk_container_add(GTK_CONTAINER(evlist->swin), evlist->treeview);

    gtk_box_pack_start(GTK_BOX(widget), evlist->swin, TRUE, TRUE, 0);
    gtk_widget_show_all(widget);

    return widget;
}

/** Create and file the tree model for the even list. */
static GtkTreeModel *create_and_fill_model(GHashTable * sats)
{
    GtkListStore   *liststore;

    liststore = gtk_list_store_new(EVENT_LIST_COL_NUMBER, G_TYPE_STRING,        // name
                                   G_TYPE_INT,  // catnum
                                   G_TYPE_DOUBLE,       // az
                                   G_TYPE_DOUBLE,       // el
                                   G_TYPE_BOOLEAN,      // TRUE if AOS, FALSE if LOS
                                   G_TYPE_DOUBLE,       // time
                                   G_TYPE_BOOLEAN,      // decayed 
                                   G_TYPE_INT); // bold for storing weight

    g_hash_table_foreach(sats, event_list_add_satellites, liststore);

    return GTK_TREE_MODEL(liststore);
}

/**
 * Add satellites. This function is a g_hash_table_foreach() callback.
 * @param key The key of the satellite in the hash table.
 * @param value Pointer to the satellite (sat_t structure) that should be added.
 * @param user_data Pointer to the GtkListStore where the satellite should be added
 *
 * This function is called by by the create_and_fill_models() function for adding
 * the satellites to the internal liststore.
 */
static void event_list_add_satellites(gpointer key, gpointer value,
                                      gpointer user_data)
{
    GtkListStore   *store = GTK_LIST_STORE(user_data);
    GtkTreeIter     item;
    sat_t          *sat = SAT(value);

    (void)key;

    gtk_list_store_append(store, &item);
    gtk_list_store_set(store, &item,
                       EVENT_LIST_COL_NAME, sat->nickname,
                       EVENT_LIST_COL_CATNUM, sat->tle.catnr,
                       EVENT_LIST_COL_AZ, sat->az,
                       EVENT_LIST_COL_EL, sat->el,
                       EVENT_LIST_COL_EVT, (sat->el >= 0) ? TRUE : FALSE,
                       EVENT_LIST_COL_TIME, 0.0,
                       EVENT_LIST_COL_DECAY, !decayed(sat), -1);
}

/** Update satellites */
void gtk_event_list_update(GtkWidget * widget)
{
    GtkTreeModel   *model;
    GtkEventList   *evlist = GTK_EVENT_LIST(widget);

    /* first, do some sanity checks */
    if ((evlist == NULL) || !IS_GTK_EVENT_LIST(evlist))
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Invalid GtkEventList!"), __func__);
        return;
    }

    /* get and tranverse the model */
    model =
        gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER
                                        (gtk_tree_model_sort_get_model
                                         (GTK_TREE_MODEL_SORT
                                          (gtk_tree_view_get_model
                                           (GTK_TREE_VIEW
                                            (evlist->treeview))))));

    /* save the sort information */
    gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(evlist->sortable),
                                         &(evlist->sort_column),
                                         &(evlist->sort_order));

    /* update */
    gtk_tree_model_foreach(model, event_list_update_sats, evlist);

#if 0
    /* check refresh rate */
    if (evlist->counter < evlist->refresh)
    {
        evlist->counter++;
    }
    else
    {
        evlist->counter = 1;

        /* get and tranverse the model */
        model =
            gtk_tree_model_filter_get_model(gtk_tree_view_get_model
                                            (GTK_TREE_VIEW(evlist->treeview)));


        /* update */
        gtk_tree_model_foreach(model, event_list_update_sats, evlist);
    }
#endif
}

/** Update data in each column in a given row */
static gboolean event_list_update_sats(GtkTreeModel * model,
                                       GtkTreePath * path,
                                       GtkTreeIter * iter, gpointer data)
{
    GtkEventList   *evlist = GTK_EVENT_LIST(data);
    guint          *catnum;
    sat_t          *sat;
    gdouble         number, now;

    (void)path;

    /* get the catalogue number for this row
       then look it up in the hash table
     */
    catnum = g_new0(guint, 1);
    gtk_tree_model_get(model, iter, EVENT_LIST_COL_CATNUM, catnum, -1);
    sat = SAT(g_hash_table_lookup(evlist->satellites, catnum));

    if (sat == NULL)
    {
        /* satellite not tracked anymore => remove */
        sat_log_log(SAT_LOG_LEVEL_INFO,
                    _("%s: Failed to get data for #%d."), __func__, *catnum);

        gtk_list_store_remove(GTK_LIST_STORE(model), iter);

        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Satellite #%d removed from list."),
                    __func__, *catnum);
    }
    else
    {
        /* update data */
        now = evlist->tstamp;

        if (sat->el > 0.0)
        {
            if (sat->los > 0.0)
            {
                number = sat->los - now;
            }
            else
            {
                number = -1.0;  /* Sat is staionary or no event */
            }
        }
        else
        {
            if (sat->aos > 0.0)
            {
                number = sat->aos - now;
            }
            else
            {
                number = -1.0;  /* Sat is staionary or no event */
            }
        }

        /* store new data */
        gtk_list_store_set(GTK_LIST_STORE(model), iter,
                           EVENT_LIST_COL_AZ, sat->az,
                           EVENT_LIST_COL_EL, sat->el,
                           EVENT_LIST_COL_EVT, (sat->el >= 0) ? TRUE : FALSE,
                           EVENT_LIST_COL_TIME, number,
                           EVENT_LIST_COL_DECAY, !decayed(sat),
                           EVENT_LIST_COL_BOLD,
                           (sat->el >
                            0.0) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
                           -1);
    }

    g_free(catnum);

    /* Return value not documented what to return, but it seems that
       FALSE continues to next row while TRUE breaks
     */
    return FALSE;
}

/** Set cell renderer function. */
static void check_and_set_cell_renderer(GtkTreeViewColumn * column,
                                        GtkCellRenderer * renderer, gint i)
{
    switch (i)
    {
        /* Event type */
    case EVENT_LIST_COL_AZ:
    case EVENT_LIST_COL_EL:
        gtk_tree_view_column_set_cell_data_func(column,
                                                renderer,
                                                degree_cell_data_function,
                                                GUINT_TO_POINTER(i), NULL);
        break;

        /* Event type */
    case EVENT_LIST_COL_EVT:
        gtk_tree_view_column_set_cell_data_func(column,
                                                renderer,
                                                evtype_cell_data_function,
                                                GUINT_TO_POINTER(i), NULL);
        break;

        /* time countdown */
    case EVENT_LIST_COL_TIME:
        gtk_tree_view_column_set_cell_data_func(column,
                                                renderer,
                                                time_cell_data_function,
                                                GUINT_TO_POINTER(i), NULL);
        break;

    default:
        break;
    }
}

/**
 * Render column containg event type.
 *
 * Event type can be AOS or LOS depending on whether the satellite is within
 * range or not. AOS will rendern an "A", LOS will render an "L".
 */
static void evtype_cell_data_function(GtkTreeViewColumn * col,
                                      GtkCellRenderer * renderer,
                                      GtkTreeModel * model,
                                      GtkTreeIter * iter, gpointer column)
{
    (void)col;

    gboolean        value;
    gchar          *buff;
    guint           coli = GPOINTER_TO_UINT(column);

    /* get field value from cell */
    gtk_tree_model_get(model, iter, coli, &value, -1);

    if (value == TRUE)
    {
        buff = g_strdup(_("LOS"));
    }
    else
    {
        buff = g_strdup(_("AOS"));
    }

    /* render the cell */
    g_object_set(renderer, "text", buff, NULL);
    g_free(buff);
}

/* AOS/LOS; convert julian date to string */
static void time_cell_data_function(GtkTreeViewColumn * col,
                                    GtkCellRenderer * renderer,
                                    GtkTreeModel * model,
                                    GtkTreeIter * iter, gpointer column)
{
    (void)col;

    gdouble         number;
    gchar          *buff;
    guint           coli = GPOINTER_TO_UINT(column);

    guint           h, m, s;

    /* get cell data */
    gtk_tree_model_get(model, iter, coli, &number, -1);

    /* format the time code */
    if (number < 0.0)
    {
        buff = g_strdup(_("Never"));
    }
    else
    {
        /* convert julian date to seconds */
        s = (guint) (number * 86400);

        /* extract hours */
        h = (guint) floor(s / 3600);
        s -= 3600 * h;

        /* extract minutes */
        m = (guint) floor(s / 60);
        s -= 60 * m;

        if (h > 0)
        {
            buff = g_strdup_printf("%02d:%02d:%02d", h, m, s);
        }
        else
        {
            buff = g_strdup_printf("%02d:%02d", m, s);
        }
    }

    /* render the cell */
    g_object_set(renderer, "text", buff, NULL);
    g_free(buff);
}

/* general floats with 2 digits + degree char. Used for Az and El */
static void degree_cell_data_function(GtkTreeViewColumn * col,
                                      GtkCellRenderer * renderer,
                                      GtkTreeModel * model,
                                      GtkTreeIter * iter, gpointer column)
{
    (void)col;

    gdouble         number;
    gchar          *buff;
    guint           coli = GPOINTER_TO_UINT(column);

    /* get the value */
    gtk_tree_model_get(model, iter, coli, &number, -1);

    /* format the number */
    buff = g_strdup_printf("%.2f\302\260", number);

    /* render column */
    g_object_set(renderer, "text", buff, NULL);
    g_free(buff);
}

/**
 * Function to compare to Event cells.
 * @param model Pointer to the GtkTreeModel.
 * @param a Pointer to the first element.
 * @param b Pointer to the second element.
 * @param user_data Always NULL (TBC).
 * @return See detailed description.
 *
 * This function is used by the SatList sort function to determine whether
 * AOS/LOS cell a is greater than b or not. The cells a and b contain the
 * time of the event in Julian days, thus the result can be computed by a
 * simple comparison between the two numbers contained in the cells.
 *
 * The function returns -1 if a < b; +1 if a > b; 0 otherwise.
 */
static gint event_cell_compare_function(GtkTreeModel * model,
                                        GtkTreeIter * a,
                                        GtkTreeIter * b, gpointer user_data)
{
    gint            result;
    gdouble         ta, tb;
    gint            sort_col = EVENT_LIST_COL_TIME;     /* hard coded as it is the only option */

    /* extracting from models was not working */
    /* with filter and sorting */

    (void)user_data;

    /* get a and b */
    gtk_tree_model_get(model, a, sort_col, &ta, -1);
    gtk_tree_model_get(model, b, sort_col, &tb, -1);

    if (ta < tb)
    {
        result = -1;
    }
    else if (ta > tb)
    {
        result = 1;
    }
    else
    {
        result = 0;
    }

    return result;
}

/** Reload configuration */
void gtk_event_list_reconf(GtkWidget * widget, GKeyFile * cfgdat)
{
    (void)cfgdat;
    (void)widget;
    sat_log_log(SAT_LOG_LEVEL_WARN, _("%s: FIXME I am not implemented"));
}

/**
 * Manage "popup-menu" events.
 * @param treeview The tree view in the GtkSatList widget
 * @param list Pointer to the GtkSatList widget.
 *
 */
static gboolean popup_menu_cb(GtkWidget * treeview, gpointer list)
{
    /* if there is no selection, select the first row */

    view_popup_menu(treeview, NULL, list);

    return TRUE;                /* we handled this */
}

/**
 * Manage button press events.
 * @param treeview The tree view in the GtkSatList widget.
 * @param event The event received.
 * @param list Pointer to the GtkSatList widget.
 */
static gboolean button_press_cb(GtkWidget * treeview, GdkEventButton * event,
                                gpointer list)
{

    /* single click with the right mouse button? */
    if (event->type == GDK_BUTTON_PRESS && event->button == 3)
    {
        /* optional: select row if no row is selected or only
         *  one other row is selected (will only do something
         *  if you set a tree selection mode as described later
         *  in the tutorial) */
        if (1)
        {
            GtkTreeSelection *selection;

            selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

            /* Note: gtk_tree_selection_count_selected_rows() does not
             *   exist in gtk+-2.0, only in gtk+ >= v2.2 ! */
            if (gtk_tree_selection_count_selected_rows(selection) <= 1)
            {
                GtkTreePath    *path;

                /* Get tree path for row that was clicked */
                if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
                                                  (gint) event->x,
                                                  (gint) event->y,
                                                  &path, NULL, NULL, NULL))
                {
                    gtk_tree_selection_unselect_all(selection);
                    gtk_tree_selection_select_path(selection, path);
                    gtk_tree_path_free(path);
                }
            }
        }                       /* end of optional bit */

        view_popup_menu(treeview, event, list);

        return TRUE;            /* we handled this */
    }

    return FALSE;               /* we did not handle this */
}

static void row_activated_cb(GtkTreeView * tree_view,
                             GtkTreePath * path, GtkTreeViewColumn * column,
                             gpointer list)
{
    GtkTreeModel   *model;
    GtkTreeIter     iter;
    guint          *catnum;
    sat_t          *sat;

    (void)column;

    catnum = g_new0(guint, 1);
    model = gtk_tree_view_get_model(tree_view);
    gtk_tree_model_get_iter(model, &iter, path);
    gtk_tree_model_get(model, &iter, EVENT_LIST_COL_CATNUM, catnum, -1);

    sat = SAT(g_hash_table_lookup(GTK_EVENT_LIST(list)->satellites, catnum));

    if (sat == NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_INFO,
                    _("%s:%d Failed to get data for %d."),
                    __FILE__, __LINE__, *catnum);
    }
    else
    {
        show_sat_info(sat, gtk_widget_get_toplevel(GTK_WIDGET(list)));
    }

    g_free(catnum);
}

static void view_popup_menu(GtkWidget * treeview, GdkEventButton * event,
                            gpointer list)
{
    GtkTreeSelection *selection;
    GtkTreeModel   *model;
    GtkTreeIter     iter;
    guint          *catnum;
    sat_t          *sat;

    catnum = g_new0(guint, 1);

    /* get selected satellite */
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    if (gtk_tree_selection_get_selected(selection, &model, &iter))
    {
        gtk_tree_model_get(model, &iter, EVENT_LIST_COL_CATNUM, catnum, -1);

        sat =
            SAT(g_hash_table_lookup(GTK_EVENT_LIST(list)->satellites, catnum));

        if (sat == NULL)
        {
            sat_log_log(SAT_LOG_LEVEL_INFO,
                        _("%s:%d Failed to get data for %d."),
                        __FILE__, __LINE__, *catnum);
        }
        else
        {
            gtk_event_list_popup_exec(sat, GTK_EVENT_LIST(list)->qth, event,
                                      GTK_EVENT_LIST(list));
        }
    }
    else
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s:%d: There is no selection; skip popup."),
                    __FILE__, __LINE__);
    }

    g_free(catnum);
}

/** Reload reference to satellites (e.g. after TLE update). */
void gtk_event_list_reload_sats(GtkWidget * evlist, GHashTable * sats)
{
    GTK_EVENT_LIST(evlist)->satellites = sats;
}

/** Select satellite. */
void gtk_event_list_select_sat(GtkWidget * widget, gint catnum)
{
    GtkEventList   *list;
    GtkTreeModel   *model;
    GtkTreeSelection *selection;
    GtkTreeIter     iter;
    gint            i, n;
    gint            sat;

    list = GTK_EVENT_LIST(widget);
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(list->treeview));
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list->treeview));

    /* iterate over the satellite list until a amtch is found */
    n = g_hash_table_size(list->satellites);
    for (i = 0; i < n; i++)
    {

        if (gtk_tree_model_iter_nth_child(model, &iter, NULL, i))
        {
            gtk_tree_model_get(model, &iter, EVENT_LIST_COL_CATNUM, &sat, -1);
            if (sat == catnum)
            {
                gtk_tree_selection_select_iter(selection, &iter);
                i = n;
            }
        }
        else
        {
            sat_log_log(SAT_LOG_LEVEL_ERROR,
                        _("%s: GtkEventList has not child with index %d"),
                        __func__, i);
        }
    }
}