]> Pileus Git - aweather/blob - src/main.c
Misc win32 fixes
[aweather] / src / main.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _XOPEN_SOURCE
19 #include <sys/time.h>
20 #include <config.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtkgl.h>
23 #include <glib/gstdio.h>
24
25 #include <grits.h>
26
27 #include "aweather-gui.h"
28 #include "aweather-location.h"
29
30 static gint log_levels = 0;
31
32 static void log_func(const gchar *log_domain, GLogLevelFlags log_level,
33               const gchar *message, gpointer udata)
34 {
35         if (log_level & log_levels) {
36                 if (log_level == G_LOG_LEVEL_DEBUG)
37                         g_fprintf(stderr, "DEBUG: (%p) %s\n",
38                                         g_thread_self(), message);
39                 else
40                         g_log_default_handler(log_domain, log_level, message, udata);
41         }
42 }
43
44 static void on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
45 {
46         g_message("main: log_level_changed");
47         gint value = gtk_spin_button_get_value_as_int(spinner);
48         log_levels = (1 << (value+1))-1;
49 }
50
51 gboolean set_location_time(AWeatherGui *gui, char *site, char *time)
52 {
53         /* Set time
54          *   Do this before setting setting location
55          *   so that it doesn't refresh twice */
56         struct tm tm = {};
57         strptime(time, "%Y-%m-%d %H:%M", &tm);
58         time_t sec = mktime(&tm);
59         if (sec > 0)
60                 grits_viewer_set_time(gui->viewer, sec);
61         g_debug("date = [%s] == %lu\n", time, sec);
62
63         /* Set location */
64         for (city_t *city = cities; city->type; city++) {
65                 if (city->type == LOCATION_CITY && g_str_equal(city->code, site)) {
66                         grits_viewer_set_location(gui->viewer,
67                                 city->pos.lat, city->pos.lon, EARTH_R/35);
68                         break;
69                 }
70         }
71         return FALSE;
72 }
73
74
75 /********
76  * Main *
77  ********/
78 int main(int argc, char *argv[])
79 {
80         /* Defaults */
81         gint     debug   = 6;
82         gchar   *site    = "";
83         gchar   *time    = "";
84         gboolean offline = FALSE;
85
86         /* Arguments */
87         gint     opt_debug   = 0;
88         gchar   *opt_site    = NULL;
89         gchar   *opt_time    = NULL;
90         gboolean opt_auto    = FALSE;
91         gboolean opt_offline = FALSE;
92         GOptionEntry entries[] = {
93                 //long      short flg type                 location      description                 arg desc
94                 {"debug",   'd',  0,  G_OPTION_ARG_INT,    &opt_debug,   "Change default log level", "[1-7]"},
95                 {"site",    's',  0,  G_OPTION_ARG_STRING, &opt_site,    "Set initial site",         NULL},
96                 {"time",    't',  0,  G_OPTION_ARG_STRING, &opt_time,    "Set initial date/time",    NULL},
97                 {"offline", 'o',  0,  G_OPTION_ARG_NONE,   &opt_offline, "Run in offline mode",      NULL},
98                 {"auto",    'a',  0,  G_OPTION_ARG_NONE,   &opt_auto,    "Auto update radar (todo)", NULL},
99                 {NULL}
100         };
101
102         /* Init */
103         GError *error = NULL;
104         g_thread_init(NULL);
105         gdk_threads_init();
106         if (!gtk_init_with_args(&argc, &argv, "aweather", entries, NULL, &error)) {
107                 g_print("%s\n", error->message);
108                 g_error_free(error);
109                 return -1;
110         }
111         gtk_gl_init(&argc, &argv);
112
113         /* Do some logging here for aweather_gui_new */
114         if (opt_debug) log_levels = (1 << (opt_debug+1))-1;
115         else           log_levels = (1 << (6+1))-1;
116         g_log_set_handler(NULL, G_LOG_LEVEL_MASK, log_func, NULL);
117
118         /* Set up AWeather */
119         gdk_threads_enter();
120         /* Pre-load some type for gtkbuilder */
121         GRITS_TYPE_OPENGL;
122         AWEATHER_TYPE_GUI;
123         GtkBuilder *builder = gtk_builder_new();
124         if (!gtk_builder_add_from_file(builder, PKGDATADIR "/main.ui", &error))
125                 g_error("Failed to create gtk builder: %s", error->message);
126         AWeatherGui *gui = AWEATHER_GUI(gtk_builder_get_object(builder, "main_window"));
127         g_signal_connect(gui, "destroy", gtk_main_quit, NULL);
128
129         gint     prefs_debug   = grits_prefs_get_integer(gui->prefs, "aweather/log_level", NULL);
130         gchar   *prefs_site    = grits_prefs_get_string(gui->prefs,  "aweather/initial_site", NULL);
131         gboolean prefs_offline = grits_prefs_get_boolean(gui->prefs, "grits/offline", NULL);
132
133         debug   = (opt_debug   ?: prefs_debug   ?: debug);
134         site    = (opt_site    ?: prefs_site    ?: site);
135         time    = (opt_time    ?:                  time);
136         offline = (opt_offline ?: prefs_offline ?: offline);
137
138         set_location_time(gui, site, time);
139         grits_viewer_set_offline(gui->viewer, offline);
140         log_levels = (1 << (debug+1))-1;
141
142         GObject *action = aweather_gui_get_object(gui, "prefs_general_log");
143         g_signal_connect(action, "changed", G_CALLBACK(on_log_level_changed), NULL);
144
145         gtk_widget_show_all(GTK_WIDGET(gui));
146         gtk_main();
147         gdk_threads_leave();
148         gdk_display_close(gdk_display_get_default());
149         return 0;
150 }