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