2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
22 #include <glib/gstdio.h>
24 #ifdef MAC_INTEGRATION
25 #include <gtkosxapplication.h>
30 #include "aweather-gui.h"
31 #include "aweather-location.h"
33 static gint log_levels = 0;
35 static int int2log(int level) {
36 level = G_LOG_LEVEL_ERROR << level;
37 level = (level<<1) - 1;
38 level = level & G_LOG_LEVEL_MASK;
42 static void log_func(const gchar *log_domain, GLogLevelFlags log_level,
43 const gchar *message, gpointer udata)
45 if (log_level & log_levels) {
46 if (log_level == G_LOG_LEVEL_DEBUG)
47 g_fprintf(stderr, "DEBUG: (%p) %s\n",
48 g_thread_self(), message);
50 g_log_default_handler(log_domain, log_level, message, udata);
54 #if ! GTK_CHECK_VERSION(3,0,0)
55 static void xdg_open(GtkWidget *widget, const gchar *link, gpointer user_data)
57 gchar *argv[] = {"xdg-open", (gchar*)link, NULL};
58 g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
62 static void on_log_level_changed(GtkSpinButton *spinner, AWeatherGui *self)
64 g_message("main: log_level_changed");
65 log_levels = int2log(gtk_spin_button_get_value_as_int(spinner));
68 static void set_location_time(AWeatherGui *gui, char *site, char *time)
71 * Do this before setting setting location
72 * so that it doesn't refresh twice */
74 int year, mon, day, hour, min;
75 sscanf(time, "%d-%d-%d %d:%d", &year, &mon, &day, &hour, &min);
76 time_t sec = mktime(&(struct tm){0, year-1900, mon-1, day, hour, min});
78 grits_viewer_set_time(gui->viewer, sec);
79 g_debug("date = [%s] == %lu\n", time, sec);
84 for (city_t *city = cities; city->type; city++) {
85 if (city->type == LOCATION_CITY && g_str_equal(city->code, site)) {
86 grits_viewer_set_location(gui->viewer,
87 city->pos.lat, city->pos.lon, EARTH_R/35);
94 static void set_toggle_action(AWeatherGui *gui, const char *action, gboolean enabled)
96 GObject *object = aweather_gui_get_object(gui, action);
97 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(object), enabled);
100 static void setup_mac(AWeatherGui *gui)
102 #ifdef MAC_INTEGRATION
103 GtkWidget *menu = aweather_gui_get_widget(gui, "main_menu");
104 GtkosxApplication *app = g_object_new(GTKOSX_TYPE_APPLICATION, NULL);
105 gtk_widget_hide(menu);
106 gtkosx_application_set_menu_bar(app, GTK_MENU_SHELL(menu));
107 gtkosx_application_set_use_quartz_accelerators(app, TRUE);
108 gtkosx_application_ready(app);
109 //gtkosx_application_sync_menubar(app)
116 int main(int argc, char *argv[])
119 gint debug = 2; // G_LOG_LEVEL_WARNING
122 gboolean autoupdate = FALSE;
123 gboolean offline = FALSE;
124 gboolean fullscreen = FALSE;
128 gchar *opt_site = NULL;
129 gchar *opt_time = NULL;
130 gboolean opt_offline = FALSE;
131 gboolean opt_autoupdate = FALSE;
132 gboolean opt_fullscreen = FALSE;
133 GOptionEntry entries[] = {
134 //long short flg type location description arg desc
135 {"debug", 'd', 0, G_OPTION_ARG_INT, &opt_debug, "Change default log level", "[0-5]"},
136 {"site", 's', 0, G_OPTION_ARG_STRING, &opt_site, "Set initial site", "SITE"},
137 {"time", 't', 0, G_OPTION_ARG_STRING, &opt_time, "Set initial date/time", "DATE"},
138 {"offline", 'o', 0, G_OPTION_ARG_NONE, &opt_offline, "Run in offline mode", NULL},
139 {"autoupdate", 'a', 0, G_OPTION_ARG_NONE, &opt_autoupdate, "Auto update radar", NULL},
140 {"fullscreen", 'f', 0, G_OPTION_ARG_NONE, &opt_fullscreen, "Open in fullscreen mode", NULL},
144 /* All times in UTC */
145 g_setenv("TZ", "UTC", TRUE);
148 GError *error = NULL;
149 if (!gtk_init_with_args(&argc, &argv, "aweather", entries, NULL, &error)) {
150 g_print("%s\n", error->message);
155 /* Use external handler for link buttons */
156 #if ! GTK_CHECK_VERSION(3,0,0)
157 gtk_link_button_set_uri_hook((GtkLinkButtonUriFunc)xdg_open, NULL, NULL);
158 gtk_about_dialog_set_url_hook((GtkAboutDialogActivateLinkFunc)xdg_open, NULL, NULL);
159 gtk_about_dialog_set_email_hook((GtkAboutDialogActivateLinkFunc)xdg_open, NULL, NULL);
162 /* Setup debug level for aweather_gui_new */
163 g_log_set_handler(NULL, G_LOG_LEVEL_MASK, log_func, NULL);
164 log_levels = int2log(opt_debug >= 0 ? opt_debug : debug);
166 /* Set up AWeather */
167 /* Pre-load some types for gtkbuilder */
170 GtkBuilder *builder = gtk_builder_new();
171 if (!gtk_builder_add_from_file(builder, PKGDATADIR "/main.ui", &error))
172 g_error("Failed to create gtk builder: %s", error->message);
173 AWeatherGui *gui = AWEATHER_GUI(gtk_builder_get_object(builder, "main_window"));
174 g_signal_connect(gui, "destroy", gtk_main_quit, NULL);
175 GObject *action = aweather_gui_get_object(gui, "prefs_general_log");
176 g_signal_connect(action, "changed", G_CALLBACK(on_log_level_changed), NULL);
178 /* Finish setting up options */
180 gint prefs_debug = grits_prefs_get_integer(gui->prefs, "aweather/log_level", &err);
181 gchar *prefs_site = grits_prefs_get_string(gui->prefs, "aweather/initial_site", NULL);
182 gboolean prefs_offline = grits_prefs_get_boolean(gui->prefs, "grits/offline", NULL);
183 gint prefs_autoupdate = grits_prefs_get_boolean(gui->prefs, "aweather/update_enab", NULL);
185 debug = (opt_debug >= 0 ? opt_debug :
186 err == NULL ? prefs_debug : debug);
187 site = (opt_site ?: prefs_site ?: site);
188 time = (opt_time ?: time);
189 offline = (opt_offline ?: prefs_offline ?: offline);
190 autoupdate = (opt_autoupdate ?: prefs_autoupdate ?: autoupdate);
191 fullscreen = (opt_fullscreen ?: fullscreen);
193 log_levels = int2log(debug);
194 set_location_time(gui, site, time);
195 grits_viewer_set_offline(gui->viewer, offline);
196 set_toggle_action(gui, "update", autoupdate);
197 set_toggle_action(gui, "fullscreen", fullscreen);
200 /* Done with init, show gui */
201 gtk_widget_show_all(GTK_WIDGET(gui));
202 set_toggle_action(gui, "fullscreen", fullscreen); // Resest widget hiding
203 setup_mac(gui); // done after show_all
205 //gdk_display_close(gdk_display_get_default());