Added common utilities source directory to unit test include path
[ptas] / getmehome / lib-timeout-home-widget.c
1 /*
2  * This file is part of hildon-timeout-home-widget-example
3  *
4  * Copyright (C) 2009 Nokia Corporation. All rights reserved.
5  *
6  * This maemo code example is licensed under a MIT-style license,
7  * that can be found in the file called "COPYING" in the root
8  * directory.
9  *
10  */
11
12 #include <gtk/gtk.h>
13 #include <hildon/hildon.h>
14 #include <time.h>
15 #include <stdlib.h>
16
17 #include "lib-timeout-home-widget.h"
18
19 #include "location-provider.h"
20 #include "coordinate-system.h"
21
22 HD_DEFINE_PLUGIN_MODULE (TimeOutPlugin, time_out_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
23
24 GtkTextBuffer *debugBuffer = NULL;
25
26 static void printDebug(const char *msg)
27 {
28     if (debugBuffer != NULL) {
29         gtk_text_buffer_insert_at_cursor(debugBuffer, msg, -1);
30         gtk_text_buffer_insert_at_cursor(debugBuffer, "\n", -1);
31     }
32 }
33
34 char debugStr[1024];
35 #define debug(...) sprintf(debugStr, __VA_ARGS__); printDebug(debugStr)
36
37
38
39 void start_location_tracking();
40 void stop_location_tracking();
41
42 // Railway station
43 //KKJ destinationX = 2552414;
44 //KKJ destinationY = 6673664;
45 // Home
46 KKJ destinationX = 2544607;
47 KKJ destinationY = 6683661;
48
49 #define IDLE 0
50 #define SEARCHING_LOCATION 1
51 #define LOCATION_RECEIVED 2
52
53 static int widget_state = IDLE;
54
55 void get_me_home(KKJ x, KKJ y)
56 {
57     time_t t;
58     struct tm *tmp;
59
60     char hour[4];
61     char minute[4];
62     char day[4];
63     char month[4];
64     char year[6];
65
66     // Get the current time
67     t = time(NULL);
68     tmp = localtime(&t);
69     if (tmp == NULL) {
70         return;
71     }
72
73     // Format needed parts from the current time
74     strftime(hour, sizeof(hour), "%H", tmp);
75     strftime(minute, sizeof(minute), "%M", tmp);
76     strftime(day, sizeof(day), "%d", tmp);
77     strftime(month, sizeof(month), "%m", tmp);
78     strftime(year, sizeof(year), "%Y", tmp);
79
80     debug("Hour %s minute %s", hour, minute);
81
82     // Format the URL
83     char url[1024];
84     // http://www.reittiopas.fi/?from=poi*Current+location*2552414*6673664&to=poi*Home*2544607*6683661&hour=12&minute=18&timetype=departure&day=14&month=02&year=2010
85     sprintf(url, "http://www.reittiopas.fi/?from=poi*Current+location*%u*%u&to=poi*Home*%u*%u&hour=%s&minute=%s&timetype=departure&day=%s&month=%s&year=%s", x, y, destinationX, destinationY, hour, minute, day, month, year);
86
87     // Open the browser
88     char command[1024];
89     sprintf(command, "browser_dbuscmd.sh load_url \"%s\"", url);
90     system(command);
91 }
92
93 static void location_listener(double latitude, double longitude)
94 {
95     KKJ x, y;
96
97 //    debug("got location: %f, %f", latitude, longitude);
98
99     if (widget_state == SEARCHING_LOCATION) {
100         widget_state = LOCATION_RECEIVED;
101
102         WGS84lola_to_KKJxy(longitude, latitude, &x, &y);
103
104         get_me_home(x, y);
105
106         stop_location_tracking();
107         widget_state = IDLE;
108     }
109 }
110
111 int locationTrackingOn = 0;
112 void start_location_tracking() {
113     if (!locationTrackingOn) {
114         // Setup location tracking
115         setup_location_provider();
116         set_location_listener(location_listener);
117         start_location_provider();
118
119         locationTrackingOn = 1;
120         debug("Location tracking started");
121     }
122 }
123
124 void stop_location_tracking() {
125    if (locationTrackingOn)  {
126        stop_location_provider();
127        cleanup_location_provider();
128
129        locationTrackingOn = 0;
130        debug("Location tracking stopped");
131    }
132 }
133
134 void search_button_clicked(GtkButton *button, gpointer user_data)
135 {
136     if (widget_state == IDLE) {
137         widget_state = SEARCHING_LOCATION;
138         start_location_tracking();
139     }
140 }
141
142 static GtkWidget *build_ui(void)
143 {
144     GtkVBox *contents = GTK_VBOX(gtk_vbox_new(0, FALSE));
145     GtkLabel *label = GTK_LABEL(gtk_label_new("Get me home"));
146 //     HildonPickerButton *action;
147 //     action = HILDON_PICKER_BUTTON (hildon_picker_button_new (HILDON_SIZE_FINGER_HEIGHT,
148 //                                         HILDON_BUTTON_ARRANGEMENT_VERTICAL));
149 //     HildonTouchSelector *action_selector;
150 //     action_selector = HILDON_TOUCH_SELECTOR (hildon_touch_selector_new_text ());
151 //     hildon_button_set_title (HILDON_BUTTON (action), "Action");
152 //     hildon_touch_selector_append_text (action_selector, "Blank Screen");
153 //     hildon_touch_selector_append_text (action_selector, "Suspend");
154 //     hildon_touch_selector_append_text (action_selector, "Turn Off");
155 //     hildon_picker_button_set_selector (action, action_selector);
156 //     hildon_picker_button_set_active (action, 0);
157
158     GtkWidget* getmehomeButton = hildon_gtk_button_new(HILDON_SIZE_AUTO);
159     gtk_button_set_label(GTK_BUTTON(getmehomeButton), "Get Me Home");
160
161     g_signal_connect(getmehomeButton, "clicked", G_CALLBACK(search_button_clicked), NULL);
162
163 //     HildonTimeButton *time;
164 //     time = HILDON_TIME_BUTTON (hildon_time_button_new (HILDON_SIZE_FINGER_HEIGHT,
165 //                                         HILDON_BUTTON_ARRANGEMENT_VERTICAL));
166 //     hildon_time_button_set_time (time, 22, 00);
167
168     GtkHBox *buttons = GTK_HBOX(gtk_hbox_new(0, TRUE));
169     gtk_container_add(GTK_CONTAINER(buttons), GTK_WIDGET(getmehomeButton));
170 //     gtk_container_add (GTK_CONTAINER (buttons), GTK_WIDGET (action));
171 //     gtk_container_add (GTK_CONTAINER (buttons), GTK_WIDGET (time));
172
173     GtkWidget *debugView = hildon_text_view_new();  // gtk_text_view_new();
174     gtk_widget_set_size_request(GTK_WIDGET(debugView), 400, 200);
175     debugBuffer = hildon_text_view_get_buffer(HILDON_TEXT_VIEW(debugView)); // gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
176     GtkWidget* debugScroller = gtk_scrolled_window_new(NULL, NULL);
177     gtk_container_add(GTK_CONTAINER(debugScroller), GTK_WIDGET(debugView));
178
179     gtk_box_pack_start(GTK_BOX(contents), GTK_WIDGET(label), FALSE, FALSE, 0);
180     gtk_box_pack_end(GTK_BOX(contents), GTK_WIDGET(buttons), FALSE, FALSE, 0);
181     gtk_box_pack_end(GTK_BOX(contents), GTK_WIDGET(debugScroller), FALSE, FALSE, 0);
182     gtk_widget_show_all(GTK_WIDGET(contents));
183
184     return GTK_WIDGET(contents);
185
186     /*GtkWidget* getmehomeButton = hildon_gtk_button_new(HILDON_SIZE_AUTO);
187     gtk_button_set_label(GTK_BUTTON(getmehomeButton), "Get Me Home");
188     g_signal_connect(getmehomeButton, "clicked", G_CALLBACK(search_button_clicked), NULL);
189
190     gtk_widget_show_all(GTK_WIDGET(getmehomeButton));
191
192     return GTK_WIDGET(getmehomeButton);*/
193 }
194
195 static void
196 time_out_plugin_init (TimeOutPlugin *desktop_plugin)
197 {
198     GtkWidget *contents = build_ui ();
199     gtk_container_add (GTK_CONTAINER (desktop_plugin), contents);
200 }
201
202 static void
203 time_out_plugin_class_init (TimeOutPluginClass *class) {}
204
205 static void
206 time_out_plugin_class_finalize (TimeOutPluginClass *class) {}