Busy indicator added
[gps-tracker] / gps-tracker.c
1 #include <glib.h>
2
3 #include <hildon/hildon.h>
4 #include <location/location-gpsd-control.h>
5 #include <location/location-gps-device.h>
6 #include <location/location-misc.h>
7 #include <location/location-distance-utils.h>
8
9 static GtkWidget *window = NULL;
10
11 static void
12 on_gps_device_changed (LocationGPSDevice *device, gpointer data)
13 {
14   GtkLabel *info = (GtkLabel*)data;
15   GString *msg;
16         if (!device)
17                 return;
18
19   msg = g_string_new("");
20         if (device->fix) {
21                 if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
22                         g_print ("time = %f\n", device->fix->time);
23       g_string_append_printf(msg, "time = %f\n", device->fix->time);
24     }
25
26                 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
27                         g_print ("lat = %f, long = %f\n",
28                                         device->fix->latitude,
29                                         device->fix->longitude);
30                         g_string_append_printf (msg, "lat = %f, long = %f\n",
31                                         device->fix->latitude,
32                                         device->fix->longitude);
33     }
34
35                 if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
36                         g_print ("alt = %f\n", device->fix->altitude);
37                         g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
38     }
39
40                 if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
41                         g_print ("speed = %f\n", device->fix->speed);
42                         g_string_append_printf (msg, "speed = %f\n", device->fix->speed);
43     }
44
45                 if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
46                         g_print ("track = %f\n", device->fix->track);
47                         g_string_append_printf (msg, "track = %f\n", device->fix->track);
48     }
49
50                 if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
51                         g_print ("climb = %f\n", device->fix->climb);
52                         g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
53     }
54
55                 g_print ("Accuracy values:\n");
56                 g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
57                                 "eps = %e, epc = %e\n",
58                                 device->fix->ept,
59                                 device->fix->eph,
60                                 device->fix->epv,
61                                 device->fix->epd,
62                                 device->fix->eps,
63                                 device->fix->epc);
64         }
65         
66         g_print ("Satellites in view: %d\n", device->satellites_in_view);
67         g_print ("Satellites in use: %d\n", device->satellites_in_use);
68         g_print ("GPS status: %d\n", device->status);
69
70   hildon_gtk_window_set_progress_indicator(GTK_WINDOW(window), device->status == LOCATION_GPS_DEVICE_STATUS_NO_FIX);
71
72
73         if (device->cell_info) {
74                 if (device->cell_info->flags & LOCATION_CELL_INFO_GSM_CELL_INFO_SET)
75                         g_print ("Mobile Coutry Code GSM: %d\n", device->cell_info->gsm_cell_info.mcc);
76
77                 if (device->cell_info->flags & LOCATION_CELL_INFO_WCDMA_CELL_INFO_SET)
78                         g_print ("Mobile Coutry Code WCDMA: %d\n", device->cell_info->wcdma_cell_info.mcc);
79         }
80
81   gtk_label_set_text(info, msg->str);
82   //gtk_widget_show(GTK_WIDGET(info));
83   g_string_free(msg, TRUE);
84 }
85
86 static void
87 on_gps_error (LocationGPSDevice *device, gpointer data)
88 {
89         g_error ("GPS error");
90 }
91
92 static void
93 on_gps_stop (LocationGPSDevice *device, gpointer data)
94 {
95         g_warning ("GPS stopped");
96 }
97
98 static void
99 on_gps_start (LocationGPSDevice *device, gpointer data)
100 {
101         g_warning ("GPS started");
102 }
103
104 int main (int argc, char **argv)
105 {
106         HildonProgram *program = NULL;
107         GtkWidget *picker_button = NULL;
108   GtkVBox *vbox;
109   GtkWidget *status_label;
110   GtkButton *start_button;
111
112         hildon_gtk_init (&argc, &argv);
113         LocationGPSDControl *control;
114         LocationGPSDevice *device;
115
116         program = hildon_program_get_instance ();
117         g_set_application_name("GPS tracker");
118
119         window = hildon_stackable_window_new ();
120         hildon_program_add_window (program, HILDON_WINDOW (window));
121
122   vbox = (void*)gtk_vbox_new(FALSE, 0);
123
124         /* Create a picker button */
125         picker_button = hildon_date_button_new (HILDON_SIZE_AUTO,
126                         HILDON_BUTTON_ARRANGEMENT_VERTICAL);
127
128         /* Set a title to the button*/
129         hildon_button_set_title (HILDON_BUTTON (picker_button), "Pick a date");
130
131   start_button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_THUMB_HEIGHT));
132   gtk_button_set_label (start_button, "Start");
133   gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(start_button), FALSE, FALSE, 8);
134   gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(picker_button), FALSE, FALSE, 0);
135   status_label = gtk_label_new("Hier kommt der Status hin\nUnd hier ist die 2. Zeile");
136   gtk_box_pack_start_defaults(GTK_BOX(vbox), status_label);
137         /* Add vbox to main window */
138         gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET(vbox));
139
140         g_signal_connect (G_OBJECT (window), "destroy",
141                         G_CALLBACK (gtk_main_quit), NULL);
142
143         control = location_gpsd_control_get_default ();
144   hildon_banner_show_information(GTK_WIDGET(window), NULL, "Hi there!");
145         location_gpsd_control_start (control);
146
147         /*
148          * Note that in real life one may want to use some other method and interval
149          * than LOCATION_METHOD_USER_SELECTED and LOCATION_INTERVAL_DEFAULT,
150          * respectively. For more information on possible values for these parameters
151          * please see liblocation online documentation.
152          */
153         g_object_set (G_OBJECT (control), 
154                         "preferred-method", LOCATION_METHOD_USER_SELECTED,
155                         "preferred-interval", LOCATION_INTERVAL_DEFAULT,
156                         NULL);
157
158         device  = g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
159
160         g_signal_connect (control, "error",             G_CALLBACK (on_gps_error),              NULL);
161         g_signal_connect (control, "gpsd-running",      G_CALLBACK (on_gps_start),              NULL);
162         g_signal_connect (control, "gpsd-stopped",      G_CALLBACK (on_gps_stop),               NULL);
163         g_signal_connect (device,  "changed",           G_CALLBACK (on_gps_device_changed),     status_label);
164
165         gtk_widget_show_all (GTK_WIDGET (window));
166
167   hildon_gtk_window_set_progress_indicator(GTK_WINDOW(window), 1);
168         gtk_main ();
169
170         location_gpsd_control_stop (control);
171
172         g_object_unref (device);
173         g_object_unref (control);
174
175         return 0;
176 }