e3f623d972d086be7563f3100220168d6a40b1b5
[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 KKJ destinationX = 2552414;
43 KKJ destinationY = 6673664;
44
45 #define IDLE 0
46 #define SEARCHING_LOCATION 1
47 #define LOCATION_RECEIVED 2
48
49 static int widget_state = IDLE;
50
51 void get_me_home(KKJ x, KKJ y)
52 {
53     time_t t;
54     struct tm *tmp;
55
56     char hour[4];
57     char minute[4];
58     char day[4];
59     char month[4];
60     char year[6];
61
62     // Get the current time
63     t = time(NULL);
64     tmp = localtime(&t);
65     if (tmp == NULL) {
66         return;
67     }
68
69     // Format needed parts from the current time
70     strftime(hour, sizeof(hour), "%H", tmp);
71     strftime(minute, sizeof(minute), "%M", tmp);
72     strftime(day, sizeof(day), "%d", tmp);
73     strftime(month, sizeof(month), "%m", tmp);
74     strftime(year, sizeof(year), "%Y", tmp);
75
76     debug("Hour %s minute %s", hour, minute);
77
78     // Format the URL
79     char url[1024];
80     // 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
81     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);
82
83     // Open the browser
84     char command[1024];
85     sprintf(command, "browser_dbuscmd.sh load_url \"%s\"", url);
86     system(command);
87 }
88
89 static void location_listener(double latitude, double longitude)
90 {
91     KKJ x, y;
92
93 //    debug("got location: %f, %f", latitude, longitude);
94
95     if (widget_state == SEARCHING_LOCATION) {
96         widget_state = LOCATION_RECEIVED;
97
98         WGS84lola_to_KKJxy(longitude, latitude, &x, &y);
99
100         get_me_home(x, y);
101
102         stop_location_tracking();
103         widget_state = IDLE;
104     }
105 }
106
107 int locationTrackingOn = 0;
108 void start_location_tracking() {
109     if (!locationTrackingOn) {
110         // Setup location tracking
111         setup_location_provider();
112         set_location_listener(location_listener);
113         start_location_provider();
114
115         locationTrackingOn = 1;
116         debug("Location tracking started");
117     }
118 }
119
120 void stop_location_tracking() {
121    if (locationTrackingOn)  {
122        stop_location_provider();
123        cleanup_location_provider();
124
125        locationTrackingOn = 0;
126        debug("Location tracking stopped");
127    }
128 }
129
130 void search_button_clicked(GtkButton *button, gpointer user_data)
131 {
132     if (widget_state == IDLE) {
133         widget_state = SEARCHING_LOCATION;
134         start_location_tracking();
135     }
136 }
137
138 static GtkWidget *build_ui(void)
139 {
140     GtkVBox *contents = GTK_VBOX(gtk_vbox_new(0, FALSE));
141     GtkLabel *label = GTK_LABEL(gtk_label_new("Get me home"));
142 //     HildonPickerButton *action;
143 //     action = HILDON_PICKER_BUTTON (hildon_picker_button_new (HILDON_SIZE_FINGER_HEIGHT,
144 //                                         HILDON_BUTTON_ARRANGEMENT_VERTICAL));
145 //     HildonTouchSelector *action_selector;
146 //     action_selector = HILDON_TOUCH_SELECTOR (hildon_touch_selector_new_text ());
147 //     hildon_button_set_title (HILDON_BUTTON (action), "Action");
148 //     hildon_touch_selector_append_text (action_selector, "Blank Screen");
149 //     hildon_touch_selector_append_text (action_selector, "Suspend");
150 //     hildon_touch_selector_append_text (action_selector, "Turn Off");
151 //     hildon_picker_button_set_selector (action, action_selector);
152 //     hildon_picker_button_set_active (action, 0);
153
154     GtkWidget* getmehomeButton = hildon_gtk_button_new(HILDON_SIZE_AUTO);
155     gtk_button_set_label(GTK_BUTTON(getmehomeButton), "Get Me Home");
156
157     g_signal_connect(getmehomeButton, "clicked", G_CALLBACK(search_button_clicked), NULL);
158
159 //     HildonTimeButton *time;
160 //     time = HILDON_TIME_BUTTON (hildon_time_button_new (HILDON_SIZE_FINGER_HEIGHT,
161 //                                         HILDON_BUTTON_ARRANGEMENT_VERTICAL));
162 //     hildon_time_button_set_time (time, 22, 00);
163
164     GtkHBox *buttons = GTK_HBOX(gtk_hbox_new(0, TRUE));
165     gtk_container_add(GTK_CONTAINER(buttons), GTK_WIDGET(getmehomeButton));
166 //     gtk_container_add (GTK_CONTAINER (buttons), GTK_WIDGET (action));
167 //     gtk_container_add (GTK_CONTAINER (buttons), GTK_WIDGET (time));
168
169     GtkWidget *debugView = hildon_text_view_new();  // gtk_text_view_new();
170     gtk_widget_set_size_request(GTK_WIDGET(debugView), 400, 200);
171     debugBuffer = hildon_text_view_get_buffer(HILDON_TEXT_VIEW(debugView)); // gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
172     GtkWidget* debugScroller = gtk_scrolled_window_new(NULL, NULL);
173     gtk_container_add(GTK_CONTAINER(debugScroller), GTK_WIDGET(debugView));
174
175     gtk_box_pack_start(GTK_BOX(contents), GTK_WIDGET(label), FALSE, FALSE, 0);
176     gtk_box_pack_end(GTK_BOX(contents), GTK_WIDGET(buttons), FALSE, FALSE, 0);
177     gtk_box_pack_end(GTK_BOX(contents), GTK_WIDGET(debugScroller), FALSE, FALSE, 0);
178     gtk_widget_show_all(GTK_WIDGET(contents));
179
180     return GTK_WIDGET(contents);
181
182     /*GtkWidget* getmehomeButton = hildon_gtk_button_new(HILDON_SIZE_AUTO);
183     gtk_button_set_label(GTK_BUTTON(getmehomeButton), "Get Me Home");
184     g_signal_connect(getmehomeButton, "clicked", G_CALLBACK(search_button_clicked), NULL);
185
186     gtk_widget_show_all(GTK_WIDGET(getmehomeButton));
187
188     return GTK_WIDGET(getmehomeButton);*/
189 }
190
191 static void
192 time_out_plugin_init (TimeOutPlugin *desktop_plugin)
193 {
194     GtkWidget *contents = build_ui ();
195     gtk_container_add (GTK_CONTAINER (desktop_plugin), contents);
196 }
197
198 static void
199 time_out_plugin_class_init (TimeOutPluginClass *class) {}
200
201 static void
202 time_out_plugin_class_finalize (TimeOutPluginClass *class) {}