]> Pileus Git - aweather/blob - src/main.c
Pre-load GObjects for GtkBuilder
[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 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <glib/gstdio.h>
22
23 #include <gis.h>
24
25 #include "aweather-gui.h"
26 #include "aweather-location.h"
27
28 static gint log_levels = 0;
29
30 static void log_func(const gchar *log_domain, GLogLevelFlags log_level,
31               const gchar *message, gpointer udata)
32 {
33         if (log_level & log_levels) {
34                 if (log_level == G_LOG_LEVEL_DEBUG)
35                         g_fprintf(stderr, "DEBUG: (%p) %s\n",
36                                         g_thread_self(), message);
37                 else
38                         g_log_default_handler(log_domain, log_level, message, udata);
39         }
40 }
41
42 static void on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
43 {
44         g_message("main: log_level_changed");
45         gint value = gtk_spin_button_get_value_as_int(spinner);
46         log_levels = (1 << (value+1))-1;
47 }
48
49 gboolean set_location(gpointer _gui)
50 {
51         AWeatherGui *gui = _gui;
52         gchar *site = g_object_get_data(G_OBJECT(gui), "site");
53         for (city_t *city = cities; city->type; city++) {
54                 if (city->type == LOCATION_CITY && g_str_equal(city->code, site)) {
55                         gis_viewer_set_location(gui->viewer,
56                                 city->pos.lat, city->pos.lon, EARTH_R/25);
57                         break;
58                 }
59         }
60         return FALSE;
61 }
62
63
64 /********
65  * Main *
66  ********/
67 int main(int argc, char *argv[])
68 {
69         /* Defaults */
70         gint     debug   = 6;
71         gchar   *site    = "KIND";
72         gboolean offline = FALSE;
73
74         /* Arguments */
75         gint     opt_debug   = 0;
76         gchar   *opt_site    = NULL;
77         gboolean opt_auto    = FALSE;
78         gboolean opt_offline = FALSE;
79         GOptionEntry entries[] = {
80                 //long      short flg type                 location      description                 arg desc
81                 {"debug",   'd',  0,  G_OPTION_ARG_INT,    &opt_debug,   "Change default log level", "[1-7]"},
82                 {"site",    's',  0,  G_OPTION_ARG_STRING, &opt_site,    "Set initial site",         NULL},
83                 {"offline", 'o',  0,  G_OPTION_ARG_NONE,   &opt_offline, "Run in offline mode",      NULL},
84                 {"auto",    'a',  0,  G_OPTION_ARG_NONE,   &opt_auto,    "Auto update radar (todo)", NULL},
85                 {NULL}
86         };
87
88         /* Init */
89         GError *error = NULL;
90         g_thread_init(NULL);
91         gdk_threads_init();
92         if (!gtk_init_with_args(&argc, &argv, "aweather", entries, NULL, &error)) {
93                 g_print("%s\n", error->message);
94                 g_error_free(error);
95                 return -1;
96         }
97         gtk_gl_init(&argc, &argv);
98
99         /* Do some logging here for aweather_gui_new */
100         if (opt_debug) log_levels = (1 << (opt_debug+1))-1;
101         else           log_levels = (1 << (6+1))-1;
102         g_log_set_handler(NULL, G_LOG_LEVEL_MASK, log_func, NULL);
103
104         /* Set up AWeather */
105         gdk_threads_enter();
106         /* Pre-load some type for gtkbuilder */
107         GIS_TYPE_OPENGL;
108         AWEATHER_TYPE_GUI;
109         GtkBuilder *builder = gtk_builder_new();
110         if (!gtk_builder_add_from_file(builder, PKGDATADIR "/main.ui", &error))
111                 g_error("Failed to create gtk builder: %s", error->message);
112         AWeatherGui *gui = AWEATHER_GUI(gtk_builder_get_object(builder, "main_window"));
113         g_signal_connect(gui, "destroy", gtk_main_quit, NULL);
114
115         gint     prefs_debug   = gis_prefs_get_integer(gui->prefs, "aweather/log_level", NULL);
116         gchar   *prefs_site    = gis_prefs_get_string(gui->prefs,  "aweather/initial_site", NULL);
117         gboolean prefs_offline = gis_prefs_get_boolean(gui->prefs, "gis/offline", NULL);
118
119         debug   = (opt_debug   ?: prefs_debug   ?: debug);
120         site    = (opt_site    ?: prefs_site    ?: site);
121         offline = (opt_offline ?: prefs_offline ?: offline);
122
123         gis_viewer_set_offline(gui->viewer, offline);
124         log_levels = (1 << (debug+1))-1;
125
126         GObject *action = aweather_gui_get_object(gui, "prefs_general_log");
127         g_signal_connect(action, "changed", G_CALLBACK(on_log_level_changed), NULL);
128
129         g_object_set_data(G_OBJECT(gui), "site", site);
130         g_idle_add(set_location, gui);
131
132         gtk_widget_show_all(GTK_WIDGET(gui));
133         gtk_main();
134         gdk_threads_leave();
135         gdk_display_close(gdk_display_get_default());
136         return 0;
137 }