1 /*
2   Gpredict: Real-time satellite tracking and orbit prediction program
3 
4   Copyright (C)  2001-2017  Alexandru Csete, OZ9AEC.
5 
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, visit http://www.fsf.org/
18 */
19 #ifdef HAVE_CONFIG_H
20 #include <build-config.h>
21 #endif
22 #include <glib/gi18n.h>
23 #include <gtk/gtk.h>
24 #include <time.h>
25 
26 #include "sat-cfg.h"
27 #include "sat-pref-formats.h"
28 #include "sat-pref-qth.h"
29 
30 /* *INDENT-OFF* */
31 static const gchar *tztips =
32     N_("Display local time instead of UTC. Note: The local time is "
33        "that of your operating system and not the local time at the "
34        "location, which you select as " "tracking reference.");
35 
36 static const gchar *tftips =
37     N_("Enter a format string using the following codes:\n\n"
38        "\t%Y\tYear with century.\n" "\t%m\tMonth (01-12).\n"
39        "\t%d\tDay of the month (01-31).\n" "\t%j\tDay of the year (001-366).\n"
40        "\t%H\tHour (00-23).\n" "\t%M\tMinute (00-59).\n"
41        "\t%S\tSeconds (00-59).\n\n"
42        "See the user manual for more codes and examples.");
43 
44 static const gchar *nsewtips =
45     N_("Checking this box will cause geographical "
46        "coordinates to be displayed using a suffix "
47        "instead of sign (eg. 23.43\302\260W "
48        "instead of -23.43\302\260).");
49 
50 static const gchar *imptips =
51     N_("Display distances using Imperial units, for "
52        "example miles instead of kilometres.");
53 /* *INDENT-ON* */
54 
55 
56 static GtkWidget *tzcheck;      /* "time zone" check button */
57 static GtkWidget *tfentry;      /* time format entry */
58 static GtkWidget *tflabel;      /* time format label, preview */
59 static GtkWidget *tfreset;      /* time format reset button */
60 static GtkWidget *nsewcheck;    /* N/S/W/E check button */
61 static GtkWidget *impcheck;     /* Use imperial units */
62 static guint    timer;
63 static gboolean useimporg;      /* original value for use imperial */
64 
65 static gboolean tfprev_cb(gpointer data);
66 static void     systog_cb(GtkToggleButton * togglebutton, gpointer user_data);
67 static void     reset_cb(GtkWidget * button, gpointer data);
68 
69 /** Create and initialise widgets for number formats tab. */
sat_pref_formats_create()70 GtkWidget      *sat_pref_formats_create()
71 {
72     GtkWidget      *vbox, *tfbox;
73     gchar          *text;
74 
75     /* use local time */
76     tzcheck =
77         gtk_check_button_new_with_label(_("Show local time instead of UTC."));
78     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tzcheck),
79                                  sat_cfg_get_bool
80                                  (SAT_CFG_BOOL_USE_LOCAL_TIME));
81     gtk_widget_set_tooltip_text(tzcheck, _(tztips));
82 
83     /* time format */
84     tfentry = gtk_entry_new();
85     gtk_entry_set_max_length(GTK_ENTRY(tfentry), TIME_FORMAT_MAX_LENGTH);
86     gtk_widget_set_tooltip_text(tfentry, _(tftips));
87 
88     text = sat_cfg_get_str(SAT_CFG_STR_TIME_FORMAT);
89     gtk_entry_set_text(GTK_ENTRY(tfentry), text);
90     g_free(text);
91 
92     tflabel = gtk_label_new("--/--/-- --:--:--");
93 
94     /* periodic update of preview label */
95     timer = g_timeout_add(1000, tfprev_cb, NULL);
96 
97     /* reset button */
98     tfreset = gtk_button_new_with_label(_("Reset"));
99     g_signal_connect(tfreset, "clicked", G_CALLBACK(reset_cb), NULL);
100     gtk_widget_set_tooltip_text(tfreset, _("Reset to default value"));
101 
102     tfbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
103     gtk_box_set_homogeneous(GTK_BOX(tfbox), FALSE);
104     gtk_box_pack_start(GTK_BOX(tfbox), gtk_label_new(_("Time format:")),
105                        FALSE, FALSE, 0);
106     gtk_box_pack_start(GTK_BOX(tfbox), tfentry, TRUE, TRUE, 0);
107     gtk_box_pack_start(GTK_BOX(tfbox), tflabel, FALSE, FALSE, 0);
108     gtk_box_pack_start(GTK_BOX(tfbox), tfreset, FALSE, FALSE, 5);
109 
110     /* N/S/W/E */
111     nsewcheck =
112         gtk_check_button_new_with_label(_
113                                         ("Use N/S/E/W for geographical coordinates."));
114     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(nsewcheck),
115                                  sat_cfg_get_bool(SAT_CFG_BOOL_USE_NSEW));
116     gtk_widget_set_tooltip_text(nsewcheck, _(nsewtips));
117 
118     /* unit */
119     useimporg = sat_cfg_get_bool(SAT_CFG_BOOL_USE_IMPERIAL);
120     impcheck =
121         gtk_check_button_new_with_label(_
122                                         ("Use Imperial units instead of Metric."));
123     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(impcheck), useimporg);
124     gtk_widget_set_tooltip_text(impcheck, _(imptips));
125     /* connect sat-pref-qth hook */
126     g_signal_connect(impcheck, "toggled", G_CALLBACK(systog_cb), NULL);
127 
128     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
129     gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
130     gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);
131     gtk_box_pack_start(GTK_BOX(vbox), tzcheck, FALSE, FALSE, 0);
132     gtk_box_pack_start(GTK_BOX(vbox), tfbox, FALSE, FALSE, 0);
133     gtk_box_pack_start(GTK_BOX(vbox),
134                        gtk_separator_new(GTK_ORIENTATION_HORIZONTAL),
135                        FALSE, FALSE, 0);
136     gtk_box_pack_start(GTK_BOX(vbox), nsewcheck, FALSE, FALSE, 0);
137     gtk_box_pack_start(GTK_BOX(vbox),
138                        gtk_separator_new(GTK_ORIENTATION_HORIZONTAL),
139                        FALSE, FALSE, 0);
140     gtk_box_pack_start(GTK_BOX(vbox), impcheck, FALSE, FALSE, 0);
141 
142     return vbox;
143 }
144 
145 /** User pressed cancel. Any changes to config must be cancelled. */
sat_pref_formats_cancel()146 void sat_pref_formats_cancel()
147 {
148     /* restore imperial setting to it's original value */
149     sat_cfg_set_bool(SAT_CFG_BOOL_USE_IMPERIAL, useimporg);
150 
151     g_source_remove(timer);
152 }
153 
154 /** User pressed OK. Any changes should be stored in config. */
sat_pref_formats_ok()155 void sat_pref_formats_ok()
156 {
157     g_source_remove(timer);
158 
159     sat_cfg_set_bool(SAT_CFG_BOOL_USE_LOCAL_TIME,
160                      gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tzcheck)));
161     sat_cfg_set_str(SAT_CFG_STR_TIME_FORMAT,
162                     gtk_entry_get_text(GTK_ENTRY(tfentry)));
163     sat_cfg_set_bool(SAT_CFG_BOOL_USE_NSEW,
164                      gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
165                                                   (nsewcheck)));
166     sat_cfg_set_bool(SAT_CFG_BOOL_USE_IMPERIAL,
167                      gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
168                                                   (impcheck)));
169 }
170 
tfprev_cb(gpointer data)171 static gboolean tfprev_cb(gpointer data)
172 {
173     const gchar    *fmtstr;
174 
175     (void)data;
176 
177     GTimeVal        tval;
178     time_t          t;
179     guint           size;
180     gchar           buff[TIME_FORMAT_MAX_LENGTH + 1];
181 
182     /* Unix time in sec since 01-Jan-1970 */
183     g_get_current_time(&tval);
184     t = (time_t) tval.tv_sec;
185 
186     fmtstr = gtk_entry_get_text(GTK_ENTRY(tfentry));
187 
188     /* format either local time or UTC depending on check box */
189     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tzcheck)))
190         size = strftime(buff, TIME_FORMAT_MAX_LENGTH, fmtstr, localtime(&t));
191     else
192         size = strftime(buff, TIME_FORMAT_MAX_LENGTH, fmtstr, gmtime(&t));
193 
194     if (size < TIME_FORMAT_MAX_LENGTH)
195         buff[size] = '\0';
196     else
197         buff[TIME_FORMAT_MAX_LENGTH] = '\0';
198 
199     gtk_label_set_text(GTK_LABEL(tflabel), buff);
200 
201     return TRUE;
202 }
203 
204 /** Manage system toggle button signals */
systog_cb(GtkToggleButton * togglebutton,gpointer user_data)205 static void systog_cb(GtkToggleButton * togglebutton, gpointer user_data)
206 {
207     (void)user_data;
208 
209     sat_cfg_set_bool(SAT_CFG_BOOL_USE_IMPERIAL,
210                      gtk_toggle_button_get_active(togglebutton));
211     sat_pref_qth_sys_changed(gtk_toggle_button_get_active(togglebutton));
212 }
213 
214 /** Reset time format string to default */
reset_cb(GtkWidget * button,gpointer data)215 static void reset_cb(GtkWidget * button, gpointer data)
216 {
217     gchar          *fmtstr;
218 
219     (void)button;
220     (void)data;
221 
222     fmtstr = sat_cfg_get_str_def(SAT_CFG_STR_TIME_FORMAT);
223     gtk_entry_set_text(GTK_ENTRY(tfentry), fmtstr);
224     g_free(fmtstr);
225 }
226